Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Lecture 10: Database Design Wednesday, January 26, 2005.

Similar presentations


Presentation on theme: "1 Lecture 10: Database Design Wednesday, January 26, 2005."— Presentation transcript:

1 1 Lecture 10: Database Design Wednesday, January 26, 2005

2 2 Outline Design of a Relational schema (3.6)

3 3 Decompositions in General R 1 = projection of R on A 1,..., A n, B 1,..., B m R 2 = projection of R on A 1,..., A n, C 1,..., C p R(A 1,..., A n, B 1,..., B m, C 1,..., C p ) R 1 (A 1,..., A n, B 1,..., B m ) R 2 (A 1,..., A n, C 1,..., C p )

4 4 Decomposition Sometimes it is correct: NamePriceCategory Gizmo19.99Gadget OneClick24.99Camera Gizmo19.99Camera NamePrice Gizmo19.99 OneClick24.99 Gizmo19.99 NameCategory GizmoGadget OneClickCamera GizmoCamera Lossless decomposition

5 5 Incorrect Decomposition Sometimes it is not: NamePriceCategory Gizmo19.99Gadget OneClick24.99Camera Gizmo19.99Camera NameCategory GizmoGadget OneClickCamera GizmoCamera PriceCategory 19.99Gadget 24.99Camera 19.99Camera What’s incorrect ?? Lossy decomposition

6 6 Decompositions in General R(A 1,..., A n, B 1,..., B m, C 1,..., C p ) If A 1,..., A n  B 1,..., B m Then the decomposition is lossless R 1 (A 1,..., A n, B 1,..., B m ) R 2 (A 1,..., A n, C 1,..., C p ) Example: name  price, hence the first decomposition is lossless Note: don’t need A 1,..., A n  C 1,..., C p

7 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 8 Boyce-Codd Normal Form A simple condition for removing anomalies from relations: In English: Whenever a set of attributes of R is determining another attribute, should determine all the attributes of R. A relation R is in BCNF if: If A 1,..., A n  B is a non-trivial dependency in R, then {A 1,..., A n } is a superkey for R A relation R is in BCNF if: If A 1,..., A n  B is a non-trivial dependency in R, then {A 1,..., A n } is a superkey for R Yet another way: for any set X, either X + = X, or X + = all attributes

9 9 BCNF Decomposition Algorithm A’s Others B’s R1R1 Is there a 2-attribute relation that is not in BCNF ? Repeat choose A 1, …, A m  B 1, …, B n that violates the BNCF condition split R into R 1 (A 1, …, A m, B 1, …, B n ) and R 2 (A 1, …, A m, [others]) continue with both R 1 and R 2 Until no more violations R2R2 In practice, we have a better algorithm (next)

10 10 Example What are the dependencies? SSN  Name, City What are the keys? {SSN, PhoneNumber} Is it in BCNF? NameSSNPhoneNumberCity Fred123-45-6789206-555-1234Seattle Fred123-45-6789206-555-6543Seattle Joe987-65-4321908-555-2121Westfield Joe987-65-4321908-555-1234Westfield

11 11 Decompose it into BCNF NameSSNCity Fred123-45-6789Seattle Joe987-65-4321Westfield SSNPhoneNumber 123-45-6789206-555-1234 123-45-6789206-555-6543 987-65-4321908-555-2121 987-65-4321908-555-1234 SSN  Name, City Let’s check anomalies: Redundancy ? Update ? Delete ?

12 12 Example Decomposition Person(name, SSN, age, hairColor, phoneNumber) SSN  name, age age  hairColor Decompose in BCNF (in class):

13 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 14 Example BCNF Decomposition Person(name, SSN, age, hairColor, phoneNumber) SSN  name, age age  hairColor Iteration 1: Person SSN+ = SSN, name, age, hairColor Decompose into: P(SSN, name, age, hairColor) Phone(SSN, phoneNumber) Iteration 2: P age+ = age, hairColor Decompose: People(SSN, name, age) Hair(age, hairColor) Phone(SSN, phoneNumber) Iteration 1: Person SSN+ = SSN, name, age, hairColor Decompose into: P(SSN, name, age, hairColor) Phone(SSN, phoneNumber) Iteration 2: P age+ = age, hairColor Decompose: People(SSN, name, age) Hair(age, hairColor) Phone(SSN, phoneNumber) Find X s.t.: X ≠X + ≠ [all attributes] What is the key ?

15 15 Example R(A,B,C,D) A  B, B  C In R: A + = ABC ≠ ABCD –Split into R 1 (A,B,C), R 2 (A,D) In R 1 : B + = BC ≠ ABC –Split into R 11 (B,C), R 12 (A,B) Final decomposition: R 11 (B,C), R 12 (A,B), R 2 (A,D) What happens if in R we first pick B + ? Or AB + ? What are the keys ?

16 16 3NF: A Problem with BCNF Unit  Company Company, Product  Unit Unit + = Unit, Company We loose the FD: Company, Product  Unit !! UnitCompanyProduct UnitCompanyUnitProduct Unit  Company

17 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: UnitCompany Galaga99UW BingoUW UnitProduct Galaga99Databases BingoDatabases UnitCompanyProduct Galaga99UWDatabases BingoUWDatabases Unit  Company Company, Product  Unit Violates the FD:

18 18 The Problem We started with a table R and FD We decomposed R into BCNF tables R 1, R 2, … with their own FD 1, FD 2, … We can reconstruct R from R 1, R 2, … But we cannot reconstruct FD from FD 1, FD 2, …

19 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 A 1, A 2,..., A n  B for R, then {A 1, A 2,..., A n } 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 A 1, A 2,..., A n  B for R, then {A 1, A 2,..., A n } a super-key for R, or B is part of a key. Tradeoff: BCNF = no anomalies, but may lose some FDs 3NF = keeps all FDs, but may have some anomalies

20 20 3NF Decomposition Algorithm 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 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 21 Example of 3NF decomposition R(A,B,C,D,E): AB  C C  D D  B D  E AB  C C  D D  B D  E Keys: (need to compute X+, for several Xs) AB, AC, AD K = {A, B, C, D} Pick X = C C+ = BCDE C  BDE is a BCNF violation For 3NF: remove B, D (part of K): C  E is a 3NF violation Decompose: R1(C, E), R2(A,B,C,D) R1 is in 3NF R2 is in 3NF (because its keys: AB, AC, AD)

22 22 BCNF 3NF v.s. BCNF Decomposition ABCDEFGHK ABCDEEFGHK EFGGHK ABCCDE ABABABABABABAB AB 3NF


Download ppt "1 Lecture 10: Database Design Wednesday, January 26, 2005."

Similar presentations


Ads by Google