Database Design: Wednesday, January 25, 2006
Database Design: Wednesday, January 25, 2006
Example:
name name color color category category department department color, color,category category price price
Closures: name+ = {name, color} {name, category}+ = {name, category, color, department, price} color+ = {color} 2
Closure Algorithm
Start Startwith withX={A1, X={A1,, ,An}. An}. Repeat :: Repeatuntil untilX Xdoesnt doesntchange change do do if if B ,,, B 11 ,B B nn C C is isaaFD FDand and B ,,, B 11 ,B B nn are areall allin inX X then then add addC Cto toX. X. Example: name name color color category category department department color, category color, category price price
{name, category}+ = { name, category, color, department, price } Hence: name, name,category category color, color,department, department,price price
3
Example
In class: R(A,B,C,D,E,F) A, A,B B C C A, D E A, D E B B D D A, F A, F B B
Compute {A,B}+
X = {A, B,
} }
4
Step 1: Compute X+, for every X: A+ A+= =A, A, B+ B+= =BD, BD, C+ C+= =C, C, D+ D+= =D D AB+ = ABCD, AC+ = AC, AD+ = ABCD AB+ = ABCD, AC+ = AC, AD+ = ABCD + ABCD (no need to compute why ?) ABC+ ABC+= =ABD+ ABD+= =ACD ACD+= = ABCD (no need to compute why ?) + + BCD = BCD, ABCD+ = BCD = BCD, ABCD+ =ABCD ABCD Step 2: Enumerate all FDs X Y, s.t. Y X+ and XY = : AB BC, AB CD, CD,AD AD BC, ABC ABC D, D,ABD ABD C, C,ACD ACD B B
6
Another Example
Enrollment(student, major, course, room, time)
student major major, course room course time
Normal Forms
First Normal Form = all attributes are atomic Second Normal Form (2NF) = old and obsolete Third Normal Form (3NF) = will discuss Boyce Codd Normal Form (BCNF) = will discuss Others...
9
Keys
A superkey is a set of attributes A1, ..., An s.t. for any other attribute B, we have A1, ..., An B A key is a minimal superkey
I.e. set of attributes which is a superkey and for which no subset is a superkey
10
Computing (Super)Keys
Compute X+ for all sets X If X+ = all attributes, then X is a key List only the minimal Xs
11
Example
Product(name, price, category, color)
name, name, category category price price category category color color
What is the key ?
12
Example
Product(name, price, category, color)
name, name, category category price price category category color color
What is the key ? (name, category) + = name, category, price, color Hence (name, category) is a key
13
Examples of Keys
Enrollment(student, address, course, room, time)
student student address address room, room,time time course course student, student,course course room, room,time time
14
Eliminating Anomalies
Main idea: X A is OK if X is a (super)key X A is not OK otherwise
15
Example
Name Fred Fred Joe Joe SSN 123-45-6789 123-45-6789 987-65-4321 987-65-4321 PhoneNumber 206-555-1234 206-555-6543 908-555-2121 908-555-1234 City Seattle Seattle Westfield Westfield
SSN SSN Name, Name,City City What the key? {SSN, PhoneNumber}
Key or Keys ?
Can we have more than one key ? Given R(A,B,C) define FDs s.t. there are two or more keys
17
Key or Keys ?
Can we have more than one key ? Given R(A,B,C) define FDs s.t. there are two or more keys AB AB C C BC BC A A
or
A A BC BC B B AC AC
18
what are the keys here ? Can you design FDs such that there are three keys ?
Bs
As
Others
R1
R2
10
Example
Name Fred Fred Joe Joe SSN 123-45-6789 123-45-6789 987-65-4321 987-65-4321 PhoneNumber 206-555-1234 206-555-6543 908-555-2121 908-555-1234 City Seattle Seattle Westfield Westfield
SSN SSN Name, Name,City City What the key? {SSN, PhoneNumber}
21
Example
Name Fred Joe SSN 123-45-6789 123-45-6789 987-65-4321 987-65-4321 SSN City 123-45-6789 Seattle 987-65-4321 Westfield PhoneNumber 206-555-1234 206-555-6543 908-555-2121 908-555-1234
22
11
Example Decomposition
Person(name, SSN, age, hairColor, phoneNumber) SSN name, age age hairColor Decompose in BCNF (in class):
23
12
R(A,B,C,D)
Example
A+ R(A,B,C,D) = ABC ABCD
A A B B B B C C
B+ R11(B,C)
R2(A,D)
What are the keys ?
13
Decompositions in General
R(A ,,..., ,,B ,,..., , C11 ,,..., )) R(A 11 ...,A A nn B 11 ...,B B m ...,C C pp m, C
R1 = projection of R on A1, ..., An, B1, ..., Bm R2 = projection of R on A1, ..., An, C1, ..., Cp
27
Theory of Decomposition
Sometimes it is correct:
Name Gizmo OneClick Gizmo Price 19.99 24.99 19.99 Category Gadget Camera Camera
Lossless decomposition
14
Incorrect Decomposition
Sometimes it is not:
Name Gizmo OneClick Gizmo Price 19.99 24.99 19.99 Category Gadget Camera Camera
Whats incorrect ??
Lossy decomposition
Decompositions in General
R(A ,,..., ,,B ,,..., , C11 ,,..., )) R(A 11 ...,A A nn B 11 ...,B B m ...,C C pp m, C
If A1, ..., An B1, ..., Bm Then the decomposition is lossless Note: dont need A1, ..., An C1, ..., Cp
30
15
Unit Unit Company Company We loose the FD: Company, Product Unit !!
31
Galaga99 UW Bingo UW
Galaga99 Bingo
Databases Databases
Unit Unit Company Company No problem so far. All local FDs are satisfied. Lets put all the data back into a single table again: Unit Company Product
Galaga99 Bingo
Violates the FD:
UW UW
Databases Databases
32
16
The Problem
We started with a table R and FD We decomposed R into BCNF tables R1, R2, with their own FD1, FD2, We can reconstruct R from R1, R2, But we cannot reconstruct FD from FD1, FD2,
33
34
17
if (not found) found) then then R R is is already already in in 3NF 3NF if (not
+ let let Y Y= =X X+ -- X X -- K K Z = [all attributes] let (X Y) Y) let Z = [all attributes]-- (X decompose into R1(X Y) and decompose into R1(X Y) and R2(X R2(X Z) Z) decompose, decompose, recursively, recursively, R1 R1 and and R2 R2
35
18
3NF
A B C C D E E F G G H K
BCNF
37
38
19
39
sname
Purchase Store
name
card-no
CreditCard
Person
ssn
40
20
Product
sname
Purchase Store
name
card-no
CreditCard
Person
ssn
42
21