19
CS 3630 Database Design and Implementation

CS 3630 Database Design and Implementation. 2 Design Methodology Three main phases 1.Conceptual database design Understanding client data E-R (EER) Model

Embed Size (px)

Citation preview

CS 3630 Database Design and Implementation

2

Design Methodology

Three main phases 1. Conceptual database design Understanding client data E-R (EER) Model Contract between clients and designers E-R Model could be used for any database system

2. Logical database design Step1: Mapping E-R Model to (relational) database schema Step2: Normalization

3. Physical database design

3

NormalizationRelational Database A set of normalized relations.

Normalization To get desired performance Data consistency

1NF, 2NF, 3NF, BCNF, 4NF and higher normal forms

We stop at BCNF

4

Example: DreamHome

Table schemas (after mapping): Staff (Sno, Name, Address, Phone, Bno) Branch (Bno, Address, Phone)

Another Approach: Staff_and_Branch (Sno, Name, Address, Phone, Bno, BAddress, BPhone)

5

ProblemsStaff_and_Branch (Sno, Name, Address, Phone, Bno, BAddress, BPhone)

Redundancy and Inconsistency Waste space : for each staff, store BAddress and BPhone Extra work on insertion : insert staff requires insertion of branch Extra work on updating : change BPhone, all staff in the branch Inconsistent data : when inserting and updating

Sno Name Address Phone Bno Baddress BPhone

S001 . . . . . . . . . . . . B20 20 main st, Platteville, WI 53818 348-8815

S012 . . . . . . . . . . . . B20 20 main st, Platteville, WI 53818 348-8815

S032 . . . . . . . . . . . . B20 20 main st, Platteville, WI 53818 348-8815

S034 . . . . . . . . . . . . B20 20 main st, Platteville, WI 53818 348-8825

S033 . . . . . . . . . . . . B20 25 main st, Platteville, WI 53818 348-8815

6

Example: DreamHomeTable schemas Staff (Sno, Name, Address, Phone, Bno) Branch (Bno, Address, Phone)

Another Approach: Staff_and_Branch (Sno, Name, Address, Phone, Bno, BAddress, BPhone)

Major Issue Data Redundancy Data Inconsistency

Normalization!

Functional Dependency!

7

Functions A function is a mapping

y = f(x)

y1 = f(x1)

y2 = f(x2)

If x1 != x2 Then

y1 != y2

NO!

If x1 = x2 Then

y1 = y2

YES!

8

ExamplesExample 1

y = f(x)

= x2

f(2) = f(-2)

Is it possible that

f(v1) != f(v2), but v1 = v2?

NO!

Example 2

y = g(x)

= x2 - 5x + 6

y1 = g(2) = 0

y2 = g(3) = 0

Is it possible thatg(u1) != g(u2), but u1 = u2?

NO!

9

Functions y = f(x)

y1 = f(x1)

y2 = f(x2)

x1 = x2 y1 = y2 True False

Same x value, then same y value.

x1 != x2 y1 != y2 True False

Different x values, then same or different y values.

Is it possible that val1 = val2 but f(val1) != f(val2)

NO! Not a function!

10

Functional Dependency

Table schema (DBDL) from E-R ModelR (A, B, C, D, E, F) PK: A AK: B, C FK: C references R1

Functional Dependency: B D

For any two records of R Same B value

Same D value

Different B valuesSame or different D values(We don’t care!)

D = f(B) No such a function to calculate the value of D from that of B!

11

Example

Staff_and_Branch (Sno, Name, Address, Bno, BAddress)

PK: Sno

AK: None

FK: None

FD: ???

12

Example

Staff_and_Branch (Sno, Name, Address, Bno, BAddress)

FD: Sno Name (YES!)

Address Name (NO!)

Name Address (NO!)

Bno BAddress (YES!)

BAddress Bno (YES!)

Sno Name Address Bno BAddress

S001 J. Clifton 102 main B01 1 westhill

S002 M. Smith 102 main B02 3 easttown

S013 M. Smith 20 main B01 1 westhill

13

Functional DependencyR (A, B, C, D, E, F) PK: A AK: B, C FK: C references R1

Functional Dependencies: B D

For any two records of R Same B value

Same D valueDifferent B values

Same or different D values(We don’t care!)

B D not trueFind two records of R

Same B valueDifferent D values

(val1 = val2 but f(val1) != f(val2))

14

Example

Property (Pno, Address, Type, Ono, Oname)

PK: Pno

AK: Address

FK: Ono references Owner

FD: ???

15

Example

Property (Pno, Address, Type, Ono, Oname)

Pno All (YES!)

Address All (YES!)

Ono All (NO!)

Ono OName (YES!)

OName Ono (NO!)

Pno Address Type Ono OName

P001 1 Moonnite House O01 J. Clifton

P002 3 Sunny House O02 M. Smith

P013 9 S. Drive, U22 Aparts O03 J. Clifton

P014 9 S. Drive, U15 Aparts O02 M. Smith

16

Example A B C D 10 X 200 CS 20 X 300 CS 10 Y 200 SE 30 Y 200 SE 10 Y 200 CS 30 X 100 CS

A C ? Yes B D? NO

Same value implies same valueDifferent values? We do not care!

A C?B D?

17

Example A B C D 10 X 200 CS 20 X 300 SE 10 Y 200 CS 30 Y 200 CS 10 Y 200 CS 10 V 250 XX 30 Z ? EE 20 W ? CJ

A C ? Possible B D? NO

Business rules! Must be true for all table instances!Must be part of table schema and apply to all table instances!Not just from some table instances!

Checking data entry if A C is true!

18

Functional DependencyDefinition Assume A and B are attributes of a table R. A B (B is functionally dependent on A) If each value of A in R is associated with exactly one value of B in R. A is called Determinant.

Any two records of R If they agree on A (have the same value on A), Then they will agree on B (have the same value on B).

If they have different values on A, Then they may have different values or the same value on B.

B = f(A)

Schedule

Assignment 5-1

Due Wednesday, February 25

19