Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Normalization Anomalies Boyce-Codd Normal Form 3 rd Normal Form.

Similar presentations


Presentation on theme: "1 Normalization Anomalies Boyce-Codd Normal Form 3 rd Normal Form."— Presentation transcript:

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 Drivers(name, addr, carsLiked, manf, favcar) nameaddrcarsLikedmanffavcar JanewayVoyagerMustangFordCarolla Janeway???Carolla Toyota ??? SpockEnterpriseMustang???Mustang Data is redundant, because each of the ???’s can be figured out by using the FD’s name -> addr favcar and carsLiked -> manf.

4 4 This Bad Design Also Exhibits Anomalies nameaddrcarsLikedmanffavcar JanewayVoyagerMustangFordCarolla JanewayVoyagerCarolla Toyota Carolla SpockEnterpriseMustangFordMustang Update anomaly: if Janeway is transferred to Intrepid, will we remember to change each of her tuples? Deletion anomaly: If nobody likes Mustang, we lose track of the fact that Ford manufactures Mustang.

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 Drivers(name, addr, carsLiked, manf, favcar) FD’s: name->addr favcar, carsLiked->manf uOnly key is {name, carsLiked}. uIn each FD, the left side is not a superkey. uAny one of these FD’s shows Drivers is not in BCNF

7 7 Another Example cars(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 Drivers(name, addr, carsLiked, manf, favcar) F = name->addr, name -> favcar,carsLiked- >manf uPick BCNF violation name->addr. uClose the left side: {name} + = {name, addr, favcar}. uDecomposed relations: 1.Drivers1(name, addr, favcar) 2.Drivers2(name, carsLiked, manf)

12 12 Example, Continued uWe are not done; we need to check Drivers1 and Drivers2 for BCNF. uProjecting FD’s is easy here. uFor Drivers1(name, addr, favcar), relevant FD’s are name->addr and name->favcar. wThus, {name} is the only key and Drivers1 is in BCNF.

13 13 Example, Continued uFor Drivers2(name, carsLiked, manf), the only FD is carsLiked->manf, and the only key is {name, carsLiked}. wViolation of BCNF. ucarsLiked + = {carsLiked, manf}, so we decompose Drivers2 into: 1.Drivers3(carsLiked, manf) 2.Drivers4(name, carsLiked)

14 14 Example, Concluded uThe resulting decomposition of Drivers : 1.Drivers1(name, addr, favcar) 2.Drivers3(carsLiked, manf) 3.Drivers4(name, carsLiked) uNotice: Drivers1 tells us about Drivers, Drivers3 tells us about cars, and Drivers4 tells us the relationship between Drivers and the cars 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 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.

21 21 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.

22 22 Multivalued Dependencies Fourth Normal Form

23 23 Definition of MVD uA multivalued dependency (MVD) on R, X ->->Y, says that if two tuples of R agree on all the attributes of X, then their components in Y may be swapped, and the result will be two tuples that are also in the relation. ui.e., for each value of X, the values of Y are independent of the values of R-X-Y.

24 24 Example Drivers(name, addr, phones, carsLiked) uA Driver’s phones are independent of the cars they like. wname->->phones and name ->->carsLiked. uThus, each of a Driver’s phones appears with each of the cars they like in all combinations. uThis repetition is unlike FD redundancy. wname->addr is the only FD.

25 25 Tuples Implied by name->->phones If we have tuples: nameaddrphones carsLiked sueap1 b1 sueap2 b2 sueap2 b1 sueap1 b2 Then these tuples must also be in the relation.

26 26 Picture of MVD X ->->Y XY others equal exchange

27 27 MVD Rules uEvery FD is an MVD (promotion ). wIf X ->Y, then swapping Y ’s between two tuples that agree on X doesn’t change the tuples. wTherefore, the “new” tuples are surely in the relation, and we know X ->->Y. uComplementation : If X ->->Y, and Z is all the other attributes, then X ->->Z.

28 28 Splitting Doesn’t Hold uLike FD’s, we cannot generally split the left side of an MVD. uBut unlike FD’s, we cannot split the right side either --- sometimes you have to leave several attributes on the right side.

29 29 Example Drivers(name, areaCode, phone, carsLiked, manf) uA Driver can have several phones, with the number divided between areaCode and phone (last 7 digits). uA Driver can like several cars, each with its own manufacturer.

30 30 Example, Continued uSince the areaCode-phone combinations for a Driver are independent of the carsLiked-manf combinations, we expect that the following MVD’s hold: name ->-> areaCode phone name ->-> carsLiked manf

31 31 Example Data Here is possible data satisfying these MVD’s: nameareaCodephonecarsLikedmanf Sue650555-1111MustangFord Sue650555-1111CorvetteG.M. Sue415555-9999MustangFord Sue415555-9999CorvetteG.M. But we cannot swap area codes or phones by themselves. That is, neither name->->areaCode nor name->->phone holds for this relation.

32 32 Fourth Normal Form uThe redundancy that comes from MVD’s is not removable by putting the database schema in BCNF. uThere is a stronger normal form, called 4NF, that (intuitively) treats MVD’s as FD’s when it comes to decomposition, but not when determining keys of the relation.

33 33 4NF Definition uA relation R is in 4NF if: whenever X ->->Y is a nontrivial MVD, then X is a superkey. wNontrivial MVD means that: 1.Y is not a subset of X, and 2.X and Y are not, together, all the attributes. wNote that the definition of “superkey” still depends on FD’s only.

34 34 BCNF Versus 4NF uRemember that every FD X ->Y is also an MVD, X ->->Y. uThus, if R is in 4NF, it is certainly in BCNF. wBecause any BCNF violation is a 4NF violation (after conversion to an MVD). uBut R could be in BCNF and not 4NF, because MVD’s are “invisible” to BCNF.

35 35 Decomposition and 4NF uIf X ->->Y is a 4NF violation for relation R, we can decompose R using the same technique as for BCNF. 1.XY is one of the decomposed relations. 2.All but Y – X is the other.

36 36 Example Drivers(name, addr, phones, carsLiked) FD: name -> addr MVD’s: name ->-> phones name ->-> carsLiked uKey is {name, phones, carsLiked}. uAll dependencies violate 4NF.

37 37 Example, Continued uDecompose using name -> addr: 1.Drivers1(name, addr) uIn 4NF; only dependency is name -> addr. 2.Drivers2(name, phones, carsLiked) uNot in 4NF. MVD’s name ->-> phones and name ->-> carsLiked apply. No FD’s, so all three attributes form the key.

38 38 Example: Decompose Drivers2 uEither MVD name ->-> phones or name ->-> carsLiked tells us to decompose to: wDrivers3(name, phones) wDrivers4(name, carsLiked)


Download ppt "1 Normalization Anomalies Boyce-Codd Normal Form 3 rd Normal Form."

Similar presentations


Ads by Google