1 lecture 10: database design wednesday, january 26, 2005
TRANSCRIPT
1
Lecture 10:Database Design
Wednesday, January 26, 2005
2
Outline
• Design of a Relational schema (3.6)
3
Decompositions in General
R1 = projection of R on A1, ..., An, B1, ..., Bm
R2 = projection of R on A1, ..., An, C1, ..., Cp
R(A1, ..., An, B1, ..., Bm, C1, ..., Cp) R(A1, ..., An, B1, ..., Bm, C1, ..., Cp)
R1(A1, ..., An, B1, ..., Bm)R1(A1, ..., An, B1, ..., Bm) R2(A1, ..., An, C1, ..., Cp)R2(A1, ..., An, C1, ..., Cp)
4
Decomposition
• Sometimes it is correct:Name Price Category
Gizmo 19.99 Gadget
OneClick 24.99 Camera
Gizmo 19.99 Camera
Name Price
Gizmo 19.99
OneClick 24.99
Gizmo 19.99
Name Category
Gizmo Gadget
OneClick Camera
Gizmo Camera
Lossless decomposition
5
Incorrect Decomposition
• Sometimes it is not:
Name Price Category
Gizmo 19.99 Gadget
OneClick 24.99 Camera
Gizmo 19.99 Camera
Name Category
Gizmo Gadget
OneClick Camera
Gizmo Camera
Price Category
19.99 Gadget
24.99 Camera
19.99 Camera
What’sincorrect ??
Lossy decomposition
6
Decompositions in General
R(A1, ..., An, B1, ..., Bm, C1, ..., Cp) R(A1, ..., An, B1, ..., Bm, C1, ..., Cp)
If A1, ..., An B1, ..., Bm
Then the decomposition is lossless
R1(A1, ..., An, B1, ..., Bm)R1(A1, ..., An, B1, ..., Bm) R2(A1, ..., An, C1, ..., Cp)R2(A1, ..., An, C1, ..., Cp)
Example: name price, hence the first decomposition is lossless
Note: don’t need A1, ..., An C1, ..., Cp
7
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...
8
Boyce-Codd Normal Form
A simple condition for removing anomalies from relations:
In English:
Whenever a set of attributes of Ris determining another attribute, should determine all the attributes of R.
A relation R is in BCNF if:
If A1, ..., An B is a non-trivial dependency
in R , then {A1, ..., An} is a superkey for R
A relation R is in BCNF if:
If A1, ..., An B is a non-trivial dependency
in R , then {A1, ..., An} is a superkey for R
Yet another way:
for any set X, either X+ = X, or X+ = all attributes
9
BCNF Decomposition Algorithm
A’s OthersB’s
R1
Is there a 2-attribute relation that isnot in BCNF ?
Repeat choose A1, …, Am B1, …, Bn that violates the BNCF condition split R into R1(A1, …, Am, B1, …, Bn) and R2(A1, …, Am, [others]) continue with both R1 and R2
Until no more violations
Repeat choose A1, …, Am B1, …, Bn that violates the BNCF condition split R into R1(A1, …, Am, B1, …, Bn) and R2(A1, …, Am, [others]) continue with both R1 and R2
Until no more violations
R2
In practice, we havea better algorithm (next)
10
Example
What are the dependencies?SSN Name, City
What are the keys?{SSN, PhoneNumber}
Is it in BCNF?
Name SSN PhoneNumber City
Fred 123-45-6789 206-555-1234 Seattle
Fred 123-45-6789 206-555-6543 Seattle
Joe 987-65-4321 908-555-2121 Westfield
Joe 987-65-4321 908-555-1234 Westfield
11
Decompose it into BCNF
Name SSN City
Fred 123-45-6789 Seattle
Joe 987-65-4321 Westfield
SSN PhoneNumber
123-45-6789 206-555-1234
123-45-6789 206-555-6543
987-65-4321 908-555-2121
987-65-4321 908-555-1234
SSN Name, City
Let’s check anomalies:• Redundancy ?• Update ?• Delete ?
12
Example Decomposition Person(name, SSN, age, hairColor, phoneNumber)
SSN name, ageage hairColor
Decompose in BCNF (in class):
13
BCNF Decomposition Algorithm
BCNF_Decompose(R)
find X s.t.: X ≠X+ ≠ [all attributes]
if (not found) then “R is in BCNF”
let Y = X+ - X let Z = [all attributes] - X+ decompose R into R1(X Y) and R2(X Z) continue to decompose recursively R1 and R2
BCNF_Decompose(R)
find X s.t.: X ≠X+ ≠ [all attributes]
if (not found) then “R is in BCNF”
let Y = X+ - X let Z = [all attributes] - X+ decompose R into R1(X Y) and R2(X Z) continue to decompose recursively R1 and R2
14
Example BCNF DecompositionPerson(name, SSN, age, hairColor, phoneNumber)
SSN name, ageage hairColor
Iteration 1: PersonSSN+ = SSN, name, age, hairColorDecompose into: P(SSN, name, age, hairColor) Phone(SSN, phoneNumber)
Iteration 2: Page+ = age, hairColorDecompose: People(SSN, name, age) Hair(age, hairColor) Phone(SSN, phoneNumber)
Iteration 1: PersonSSN+ = SSN, name, age, hairColorDecompose into: P(SSN, name, age, hairColor) Phone(SSN, phoneNumber)
Iteration 2: Page+ = age, hairColorDecompose: People(SSN, name, age) Hair(age, hairColor) Phone(SSN, phoneNumber)
Find X s.t.: X ≠X+ ≠ [all attributes]
What isthe key ?
15
Example
• R(A,B,C,D) A B, B C
• In R: A+ = ABC ≠ ABCD– Split into R1(A,B,C), R2(A,D)
• In R1: B+ = BC ≠ ABC– Split into R11(B,C), R12(A,B)
• Final decomposition: R11(B,C), R12(A,B), R2(A,D)
• What happens if in R we first pick B+ ? Or AB+ ?
What arethe keys ?
16
3NF: A Problem with BCNF
Unit CompanyCompany, Product Unit
Unit CompanyCompany, Product Unit
Unit+ = Unit, Company
We loose the FD: Company, Product Unit !!
Unit Company Product
Unit Company Unit Product
Unit CompanyUnit Company
17
So What’s the Problem?
No problem so far. All local FD’s are satisfied.Let’s put all the data back into a single table again:
Unit Company
Galaga99 UW
Bingo UW
Unit Product
Galaga99 Databases
Bingo Databases
Unit Company Product
Galaga99 UW Databases
Bingo UW Databases
Unit CompanyUnit Company
Company, Product UnitCompany, Product UnitViolates the FD:
18
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, …
19
Solution: 3rd Normal Form (3NF)
A simple condition for removing anomalies from relations:
A relation R is in 3rd normal form if :
Whenever there is a nontrivial dependency A1, A2, ..., An Bfor R , then {A1, A2, ..., An } a super-key for R, or B is part of a key.
A relation R is in 3rd normal form if :
Whenever there is a nontrivial dependency A1, A2, ..., An Bfor R , then {A1, A2, ..., An } a super-key for R, or B is part of a key.
Tradeoff:BCNF = no anomalies, but may lose some FDs3NF = keeps all FDs, but may have some anomalies
20
3NF Decomposition Algorithm3NF_Decompose(R) let K = [all attributes that are part of some key]
find X s.t.: X+ - X - K ≠ and X+ ≠ [all attributes] if (not found) then “R is already in 3NF” let Y = X+ - X - K let Z = [all attributes] - (X Y) decompose into R1(X Y) and R2(X Z) decompose, recursively, R1 and R2
3NF_Decompose(R) let K = [all attributes that are part of some key]
find X s.t.: X+ - X - K ≠ and X+ ≠ [all attributes] if (not found) then “R is already in 3NF” let Y = X+ - X - K let Z = [all attributes] - (X Y) decompose into R1(X Y) and R2(X Z) decompose, recursively, R1 and R2
21
Example of 3NF decompositionR(A,B,C,D,E):R(A,B,C,D,E):
AB CC DD BD E
AB CC DD BD E
Keys: (need to compute X+, for several Xs) AB, AC, AD
K = {A, B, C, D}
Pick X = CC+ = BCDEC BDE is a BCNF violationFor 3NF: remove B, D (part of K):C E is a 3NF violationDecompose: R1(C, E), R2(A,B,C,D)
R1 is in 3NFR2 is in 3NF (because its keys: AB, AC, AD)
22BCNF
3NF v.s. BCNF DecompositionA B C D E F G H K
A B C D E E F G H K
E F G G H KA B C C D E
A B A B A B A B A B A B A BA B
3NF