Download presentation
Presentation is loading. Please wait.
1
1 Normalization Anomalies Boyce-Codd Normal Form 3 rd Normal Form
2
2 Anomalies uGoal of relational schema design is to avoid anomalies and redundancy. wUpdate anomaly : one occurrence of a fact is changed, but not all occurrences. wDeletion anomaly : valid fact is lost when a tuple is deleted.
3
3 Example of Bad Design Drinkers(name, addr, beersLiked, manf, favBeer) nameaddrbeersLikedmanffavBeer JanewayVoyagerBudA.B.WickedAle Janeway???WickedAlePete’s??? SpockEnterpriseBud???Bud Data is redundant, because each of the ???’s can be figured out by using the FD’s name -> addr favBeer and beersLiked -> manf.
4
4 This Bad Design Also Exhibits Anomalies nameaddrbeersLikedmanffavBeer JanewayVoyagerBudA.B.WickedAle JanewayVoyagerWickedAlePete’sWickedAle SpockEnterpriseBudA.B.Bud Update anomaly: if Janeway is transferred to Intrepid, will we remember to change each of her tuples? Deletion anomaly: If nobody likes Bud, we lose track of the fact that Anheuser-Busch manufactures Bud.
5
5 Boyce-Codd Normal Form uWe say a relation R is in BCNF if whenever X ->A is a nontrivial FD that holds in R, X is a superkey. wRemember: nontrivial means A is not a member of set X. wRemember, a superkey is any superset of a key (not necessarily a proper superset).
6
6 Example Drinkers(name, addr, beersLiked, manf, favBeer) FD’s: name->addr favBeer, beersLiked->manf uOnly key is {name, beersLiked}. uIn each FD, the left side is not a superkey. uAny one of these FD’s shows Drinkers is not in BCNF
7
7 Another Example Beers(name, manf, manfAddr) FD’s: name->manf, manf->manfAddr uOnly key is {name}. uname->manf does not violate BCNF, but manf->manfAddr does.
8
8 Decomposition into BCNF uGiven: relation R with FD’s F. uLook among the given FD’s for a BCNF violation X ->B. wIf any FD following from F violates BCNF, then there will surely be an FD in F itself that violates BCNF. uCompute X +. wNot all attributes, or else X is a superkey.
9
9 Decompose R Using X -> B uReplace R by relations with schemas: 1. R 1 = X +. 2. R 2 = R – (X + – X ). uProject given FD’s F onto the two new relations.
10
10 Decomposition Picture R-X +R-X + XX +-XX +-X R2R2 R1R1 R
11
11 Example Drinkers(name, addr, beersLiked, manf, favBeer) F = name->addr, name -> favBeer, beersLiked->manf uPick BCNF violation name->addr. uClose the left side: {name} + = {name, addr, favBeer}. uDecomposed relations: 1.Drinkers1(name, addr, favBeer) 2.Drinkers2(name, beersLiked, manf)
12
12 Example, Continued uWe are not done; we need to check Drinkers1 and Drinkers2 for BCNF. uProjecting FD’s is easy here. uFor Drinkers1(name, addr, favBeer), relevant FD’s are name->addr and name->favBeer. wThus, {name} is the only key and Drinkers1 is in BCNF.
13
13 Example, Continued uFor Drinkers2(name, beersLiked, manf), the only FD is beersLiked->manf, and the only key is {name, beersLiked}. wViolation of BCNF. ubeersLiked + = {beersLiked, manf}, so we decompose Drinkers2 into: 1.Drinkers3(beersLiked, manf) 2.Drinkers4(name, beersLiked)
14
14 Example, Concluded uThe resulting decomposition of Drinkers : 1.Drinkers1(name, addr, favBeer) 2.Drinkers3(beersLiked, manf) 3.Drinkers4(name, beersLiked) uNotice: Drinkers1 tells us about drinkers, Drinkers3 tells us about beers, and Drinkers4 tells us the relationship between drinkers and the beers they like.
15
15 Third Normal Form - Motivation uThere is one structure of FD’s that causes trouble when we decompose. uAB ->C and C ->B. wExample: A = street address, B = city, C = zip code. uThere are two keys, {A,B } and {A,C }. uC ->B is a BCNF violation, so we must decompose into AC, BC.
16
16 We Cannot Enforce FD’s uThe problem is that if we use AC and BC as our database schema, we cannot enforce the FD AB ->C by checking FD’s in these decomposed relations. uExample with A = street, B = city, and C = zip on the next slide.
17
17 An Unenforceable FD street zip 545 Tech Sq.02138 545 Tech Sq.02139 city zip Cambridge02138 Cambridge02139 Join tuples with equal zip codes. street city zip 545 Tech Sq.Cambridge02138 545 Tech Sq.Cambridge02139 Although no FD’s were violated in the decomposed relations, FD street city -> zip is violated by the database as a whole.
18
18 3NF Let’s Us Avoid This Problem u3 rd Normal Form (3NF) modifies the BCNF condition so we do not have to decompose in this problem situation. uAn attribute is prime if it is a member of any key. uX ->A violates 3NF if and only if X is not a superkey, and also A is not prime.
19
19 Example uIn our problem situation with FD’s AB ->C and C ->B, we have keys AB and AC. uThus A, B, and C are each prime. uAlthough C ->B violates BCNF, it does not violate 3NF.
20
20 More Examples uR(A, B, C, D) with FDs: AB->C, C->D, D->A uFind all the non-trivial FDs w/ closure test uA+ = A, B+=B, C+=CDA, D+=DA uAB+=ABCD, AC+=ACD, AD+=AD, BC+=ABCD, BD+=ABCD, CD+=ACD uIgnore ABC, ABD, BCD b/c they are superkeys uACD+=ACD uNew non-trivial FDs: C->A, AB->D, AC->D, BC->AD, BD->AC, CD->A
21
21 More Examples (cont’d) uFind all the keys: AB, BC, BD uFDs that violate the BCNF wC->D, D->A, C->A, AC->D, CD->A uDecomposition using C->D: BC, ACD uNo non-trivial FDs on BC, satisfies BCNF wAny two-attribute relation is always BCNF uFDs on ACD: C->D, D->A, C->A, AC->D, CD->A wKeys: C wD->A violates BCNF, further decompose into AD and CD uFinal results: AD, BC and CD
22
22 More Examples (cont’d) uFind all the keys: AB, BC, BD uFDs that violate the BCNF wC->D, D->A, C->A, AC->D, CD->A uDecomposition using D->A: BCD, AD uFDs on BCD: C->D, BC->D, BD->C wKeys: BC, BD wC->D violates BCNF, further decompose into BC and CD uFinal results: AD, BC, and CD
23
23 More Examples on 3NF uR(A, B, C, D) with FDs: AB->C, C->D, D->A uFind all the non-trivial FDs w/ closure test uNew non-trivial FDs: C->A, AB->D, AC->D, BC->AD, BD->AC, CD->A uFind all the keys: AB, BC, BD uAll satisfies the 3NF
24
24 What 3NF and BCNF Give You uThere are two important properties of a decomposition: 1.Recovery : it should be possible to project the original relations onto the decomposed schema, and then reconstruct the original. 2.Dependency Preservation : it should be possible to check in the projected relations whether all the given FD’s are satisfied.
25
25 3NF and BCNF, Continued uWe can get (1) with a BCNF decomposition. wExplanation needs to wait for relational algebra. uWe can get both (1) and (2) with a 3NF decomposition. uBut we can’t always get (1) and (2) with a BCNF decomposition. wstreet-city-zip is an example.
26
26 Homework Question 3.6.1 uR(ABCDE) with FD’s AB->C, C->D, D->B, D->E uCompute the closures: wA+=A, B+=B, C+=BCDE, D+=BDE, E+=E wAB+=ABCDE, AC+=ABCDE, AD+=ABCDE, AE+=AE, BC+=BCDE, BD+=BDE, BE+=BE, CD+=BCDE, CE+=BCDE, DE+=BDE wBCD+=BCDE, BCE+=BCDE, BDE+=BDE, CDE+=BCDE, BCDE+=BCDE uKeys: AB, AC, AD uAll BCNF violations (the minimal basis) wC->B, C->D, C->E, D->B, D->E
27
27 Homework Question 3.6.1 (II) uDecompose with C->B: AC and BCDE uIs BCDE in BCNF? wKey: C wViolating FD’s: D->B, D->E wDecompose into: CD, BDE uFinal BCNF relations: AC, CD and BDE uIs R in 3NF? wPrime: A, B, C, D wViolating FD: C->E, D->E
28
28 Homework Question 3.6.1 (III) uDecompose into 3NF with C->E: AC, BCDE wIs BCDE in 3NF? Key: C Violating FD’s: D → B, D → E Decompose into: CD and BDE wFinal results: AC, CD and BDE uDecompose into 3NF with D->E: ACD, BDE wIs ACD in 3NF? Key: AC and AD, so every attribute is prime. wIs BDE in 3NF? Key: D. Only FD’s: D->B, D->E, so yes wFinal results: ACD and BDE
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.