270 likes | 358 Views
Normalization. Anomalies Boyce-Codd Normal Form 3 rd Normal Form. Source: Slides by Jeffrey Ullman. Anomalies. Goal of relational schema design is to avoid anomalies and redundancy. Update anomaly : one occurrence of a fact is changed, but not all occurrences.
E N D
Normalization Anomalies Boyce-Codd Normal Form 3rd Normal Form Source: Slides by Jeffrey Ullman
Anomalies • Goal of relational schema design is to avoid anomalies and redundancy. • Update anomaly: one occurrence of a fact is changed, but not all occurrences. • Deletion anomaly : a valid fact is lost when a tuple is deleted.
Example of Bad Design Consumers(name, addr, candiesLiked, manf, favCandy) name addr candiesLiked manf favCandy Janeway Voyager Twizzlers Hershey Smarties Janeway ??? Smarties Pete’s ??? Spock Enterprise Twizzlers ??? Twizzlers Data is redundant, because each of the ???’s can be figured out by using the FD’s name -> addr favCandy and candiesLiked -> manf.
This Bad Design AlsoExhibits Anomalies name addr candiesLiked manf favCandy Janeway Voyager Twizzlers Hershey Smarties Janeway Voyager Smarties Nestle Smarties Spock Enterprise Twizzlers Hershey Twizzlers • Update anomaly: if Janeway is transferred to Intrepid, • will we remember to change each of her tuples? • Deletion anomaly: If nobody likes Twizzlers, we lose track • of the fact that Hershey manufactures Twizzlers.
Problem & Solution • Conceptual modeling unnaturally puts attributes of two different entity sets in same relation schema • Use decompositions to break up schema involving such an “unhappy union” into several relation schemas • Must do this decomposition carefully: • Various normal forms have been proposed for relations • If a schema is in one of these normal forms, then we now that certain kinds of problems do not occur
Properties of a Decomposition • Lossless join: any tuple of the original relation can be reconstructed from corresponding tuples of the resulting relations • Dependency preservation: we can enforce any constraint on the original relation by enforcing some constraints on the resulting relations
When to Decompose? • If you frequently want the full information available in instances of the original relation, don’t decompose • Expensive to reconstruct original information • Instead, put up with the storage redundancy and put extra checks in apps to avoid anomalies
Boyce-Codd Normal Form • We say a relation R is in BCNF if whenever X ->Ais a nontrivial FD that holds in R, X is a superkey. • Remember:nontrivial means A is not a member of set X. • Remember, a superkey is any superset of a key (not necessarily a proper superset).
Example Consumers(name, addr, candiesLiked, manf, favCandy) FD’s: name->addr favCandy, candiesLiked->manf • Only key is {name, candiesLiked}. • In each FD, the left side is not a superkey. • Any one of these FD’s shows, Consumers is not in BCNF
Another Example Candies(name, manf, manfAddr) FD’s: name->manf, manf->manfAddr • Only key is {name} . • name->manf does not violate BCNF, but manf->manfAddr does.
Properties of BCNF • If a relation is in BCNF, then every attribute of every tuple records a piece of information that cannot be inferred, using FD’s, from any other information in the relation • Eliminates redundancy • If a relation is not in BCNF, there is always a lossless-join decomposition into a collection of BCNF relations • But may not be a dependency-preserving decomposition
Decomposition into BCNF • Given: relation R with set of FD’s F. • Compute keys for R (sets of attributes that functionally determine all other attributes) • Look among the given FD’s for a BCNF violation X ->B. • If any FD following from F violates BCNF, then there will surely be an FD in F itself that violates BCNF. • Compute X+. • Won't be all attributes, or else X would be a superkey and X ->B would not be a violation
Decompose R Using X -> B • Replace R by relations R1 and R2 with schemas: • R1 : X+ (all attributes "reachable" from X) • R2 : R – (X+ – X ) (X plus all attributes not "reachable" from X) • Project given FD’s F onto the two new relations (ignore FD's containing attributes no longer in the new relation). • Check if procedure must be repeated with R1and/or R2.
Decomposition Picture R1 R-X+ X X+-X R2 R
Example Consumers(name, addr, candiesLiked, manf, favCandy) F = name->addr, name -> favCandy, candiesLiked->manf • Pick BCNF violation name->addr. • Close the left side: {name}+ = {name, addr, favCandy}. • Decomposed relations: • Consumers1(name, addr, favCandy) • Consumers2(name, candiesLiked, manf)
Example, Continued • We are not done; we need to check Consumers1 and Consumers2 for BCNF. • Projecting FD’s is easy here. • For Consumers1(name, addr, favCandy), relevant FD’s are name->addr and name->favCandy. • Thus, {name} is the only key and Consumers1 is in BCNF.
Example, Continued • For Consumers2(name, candiesLiked, manf), the only FD is candiesLiked -> manf, and the only key is {name, candiesLiked}. • Violation of BCNF. • candiesLiked+ = {candiesLiked, manf}, so we decompose Consumers2 into: • Consumers3(candiesLiked, manf) • Consumers4(name, candiesLiked)
Example, Concluded • The resulting decomposition of Consumers : Consumers1(name, addr, favCandy) Consumers3(candiesLiked, manf) Consumers4(name, candiesLiked) • Notice: Consumers1 tells us about consumers, Consumers3 tells us about candies, and Consumers4 tells us the relationship between consumers and the candies they like.
BCNF Decomposition Not Unique • Suppose several dependencies violate BCNF. • Depending on which is used to guide the next decomposition, might get different collections of decomposed relations • Normalization theory does not help us decide among alternatives: designer must consider alternatives and choose based on semantics of the application
Third Normal Form - Motivation • There is one structure of FD’s that causes trouble when we decompose. • AB ->C and C ->B. • Example: A = street address, B = city, C = zip code. • There are two keys, {A,B } and {A,C }. • Think about why. • C ->B is a BCNF violation (why?), so we must decompose into AC, BC.
We Cannot Enforce FD’s • The 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. • Example with A = street, B = city, and C = zip on the next slide.
An Unenforceable FD • Suppose we have relation Addr with FD's: • street city -> zip • zip -> city • Keys are {street, city} and {street, zip} • After decomposing, we have relations • Addr1(street,zip) with no FD (!) • Addr2(city,zip) with FD zip -> city • Consider instances on next slide…
Join tuples with equal zip codes. street city zip 545 Tech Sq. Cambridge 02138 545 Tech Sq. Cambridge 02139 An Unenforceable FD (cont'd) Addr1: Addr2: street zip 545 Tech Sq. 02138 545 Tech Sq. 02139 city zip Cambridge 02138 Cambridge 02139 Although no FD’s were violated in the decomposed relations, FD street city -> zip is violated by the database as a whole.
3NF Lets Us Avoid This Problem • 3rd Normal Form (3NF) modifies the BCNF condition so we do not have to decompose in this problem situation. • An attribute is prime if it is a member of any key. • X ->Aviolates 3NF if and only if X is not a superkey, and also A is not prime.
Example • In our problem situation with FD’s AB ->C and C ->B, we have keys AB and AC. • Thus A, B, and C are each prime. • Although C ->B violates BCNF, it does not violate 3NF. • So no need to decompose.
What 3NF and BCNF Give You • There are two important properties of a decomposition: • Recovery (aka Lossless Join): it should be possible to project the original relations onto the decomposed schema, and then reconstruct the original. • Dependency Preservation : it should be possible to check in the projected relations whether all the (original) given FD’s are satisfied.
3NF and BCNF, Continued • We can get (1) with a BCNF decomposition. • Explanation needs to wait for relational algebra. • We can get both (1) and (2) with a 3NF decomposition. • But we can’t always get (1) and (2) with a BCNF decomposition. • street-city-zip is an example.