This preview shows pages 1–9. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: Design Theory for Relational Databases
Functional Dependencies Decompositions Normal Forms
1 Functional Dependencies
x X >Y is an assertion about a relation R that whenever two tuples of R agree on all the attributes of X, then they must also agree on all attributes in set Y. Say "X >Y holds in R." Convention: ..., X, Y, Z represent sets of attributes; A, B, C,... represent single attributes. Convention: no set formers in sets of attributes, just ABC, rather than {A,B,C }. 2 Splitting Right Sides of FD's
X>A A ...A holds for R exactly when x
3 Example: FD's
Drinkers(name, addr, beersLiked, manf, favBeer) x Reasonable FD's to assert:
1. name > addr favBeer Note this FD is the same as name > addr and name > favBeer. 2. beersLiked > manf 4 Example: Possible Data
addr Voyager Voyager Enterprise beersLiked manf Bud A.B. WickedAle Pete's Bud A.B. me neway neway ock favBeer WickedAle WickedAle Bud Because name > addr Because name > favBeer Because beersLiked > manf 5 Keys of Relations
x K is a superkey for relation R if K functionally determines all of R. x K is a key for R if K is a superkey, but no proper subset of K is a superkey. 6 Example: Superkey
Drinkers(name, addr, beersLiked, manf, favBeer) x {name, beersLiked} is a superkey because together these attributes determine all the other attributes. name > addr favBeer beersLiked > manf 7 Example: Key
x {name, beersLiked} is a key because neither {name} nor {beersLiked} is a superkey. x There are no other keys, but lots of superkeys. Any superset of {name, beersLiked}. name doesn't > manf; beersLiked doesn't > addr. 8 Where Do Keys Come From?
1. Just assert a key K. The only FD's are K > A for all attributes A. 2. Assert FD's and deduce the keys by systematic exploration. 9 More FD's From "Physics"
x Example: "no two courses can meet in the same room at the same time" tells us: hour room > course. 10 Inferring FD's
x We are given FD's X1 > A1, X2 > A2, ..., Xn > An , and we want to know whether an FD Y > B must hold in any relation that satisfies the given FD's. x Important for design of good relation schemas. Example: If A > B and B > C hold, surely A > C holds, even if we don't say so. 11 Inference Test
x To test if Y > B, start by assuming two tuples agree in all attributes of Y. Y 0000000. . . 0 00000?? . . . ?
12 Inference Test (2)
x Use the given FD's to infer that these tuples must also agree in certain other attributes. If B is one of these attributes, then Y > B is true. Otherwise, the two tuples, with any forced equalities, form a twotuple relation that proves Y > B does not follow from the given FD's.
13 Closure Test
x An easier way to test is to compute the closure of Y, denoted Y +. x Basis: Y + = Y. x Induction: Look for an FD's left side X that is a subset of the current Y +. If the FD is X > A, add A to Y +. 14 X Y+ A new Y+ 15 Finding All Implied FD's
x Motivation: "normalization," the process where we break a relation schema into two or more schemas. x Example: ABCD with FD's AB >C, C >D, and D >A. Decompose into ABC, AD. What FD's hold in ABC ? Not only AB >C, but also C >A ! 16 Why?
ABCD a1b1cd1 a2b2cd2 d1=d2 because C > D a1=a2 because D > A comes from ABC a1b1c a2b2c Thus, tuples in the projection with equal C's have equal A's; C > A. 17 Basic Idea
1. Start with given FD's and find all nontrivial FD's that follow from the given FD's. 2. Restrict to those FD's that involve only attributes of the projected schema.
18 Nontrivial = right side not contained in the left. Simple, Exponential Algorithm
1. For each set of attributes X, compute X +. 2. Add X >A for all A in X + X. 3. However, drop XY >A whenever we discover X >A. 4. Finally, use only FD's involving projected attributes.
19 x Because XY >A follows from X >A in any projection. A Few Tricks
x No need to compute the closure of the empty set or of the set of all attributes. x If we find X + = all attributes, so is the closure of any superset of X. 20 Example: Projecting FD's
x ABC with FD's A >B and B >C. Project onto AC. A +=ABC ; yields A >B, A >C. B +=BC ; yields B >C. C +=C ; yields nothing. BC +=BC ; yields nothing. We do not need to compute AB + or AC +. 21 Example Continued
x Resulting FD's: A >B, A >C, and B >C. x Projection onto AC : A >C. Only FD that involves a subset of {A,C }. 22 A Geometric View of FD's
x Imagine the set of all instances of a particular relation. x That is, all finite sets of tuples that have the proper number of components. x Each instance is a point in this space. 23 Example: R(A,B) {(1,2), (3,4)} {(5,1)} {} {(1,2), (3,4), (1,3)}
24 An FD is a Subset of Instances
x For each FD X > A there is a subset of all instances that satisfy the FD. x We can represent an FD by a region in the space. x Trivial FD = an FD that is represented by the entire space. Example: A > A. 25 Example: A > B for R(A,B) {(1,2), (3,4)} {} A > B {(5,1)} {(1,2), (3,4), (1,3)}
26 Representing Sets of FD's
x If each FD is a set of relation instances, then a collection of FD's corresponds to the intersection of those sets. Intersection = all instances that satisfy all of the FD's. 27 Example
Instances satisfying A>B, B>C, and CD>A A>B B>C CD>A 28 Implication of FD's
x If an FD Y > B follows from FD's X1 > A1,...,Xn > An , then the region in the space of instances for Y > B must include the intersection of the regions for the FD's Xi > Ai . That is, every instance satisfying all the FD's Xi > Ai surely satisfies Y > B. But an instance could satisfy Y > B, yet not be in this intersection. 29 Example A>B A>C B>C 30 Relational Schema Design
x 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 : valid fact is lost when a tuple is deleted. 31 Example of Bad Design
Drinkers(name, addr, beersLiked, manf, favBeer) name Janeway Janeway Spock addr Voyager ??? Enterprise beersLiked Bud WickedAle Bud manf favBeer A.B. WickedAle Pete's ??? ??? Bud Data is redundant, because each of the ???'s can be figured out by using the FD's name > addr favBeer and beersLiked > manf. 32 This Bad Design Also Exhibits Anomalies
name Janeway Janeway Spock addr Voyager Voyager Enterprise beersLiked Bud WickedAle Bud manf favBeer A.B. WickedAle Pete's WickedAle A.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 AnheuserBusch manufactures Bud.
33 BoyceCodd Normal Form x We say a relation R is in BCNF if whenever X >Y is a nontrivial FD that holds in R, X is a superkey. Remember: nontrivial means Y is not contained in X. Remember, a superkey is any superset of a key (not necessarily a proper superset). 34 Example
Drinkers(name, addr, beersLiked, manf, favBeer) FD's: name>addr favBeer, beersLiked>manf x Only key is {name, beersLiked}. x In each FD, the left side is not a superkey. x Any one of these FD's shows Drinkers is not in BCNF
35 Another Example
Beers(name, manf, manfAddr) FD's: name>manf, manf>manfAddr x Only key is {name} . x name>manf does not violate BCNF, but manf>manfAddr does. 36 Decomposition into BCNF
x Given: relation R with FD's F. x Look among the given FD's for a BCNF violation X >Y. If any FD following from F violates BCNF, then there will surely be an FD in F itself that violates BCNF. Not all attributes, or else X is a superkey.
37 x Compute X +. Decompose R Using X > Y
x Replace R by relations with schemas:
1. R1 = X +. 2. R2 = R (X + X ). x Project given FD's F onto the two new relations. 38 Decomposition Picture
R1 RX + R2 X X +X R 39 Example: BCNF Decomposition
Drinkers(name, addr, beersLiked, manf, favBeer) F = name>addr, name > favBeer, beersLiked>manf x Pick BCNF violation name>addr. x Close the left side: {name}+ = {name, addr, favBeer}. x Decomposed relations: 1. Drinkers1(name, addr, favBeer) 2. Drinkers2(name, beersLiked, manf)
40 Example Continued
x We are not done; we need to check Drinkers1 and Drinkers2 for BCNF. x Projecting FD's is easy here. x For Drinkers1(name, addr, favBeer), relevant FD's are name>addr and name>favBeer. Thus, {name} is the only key and Drinkers1 is in BCNF.
41 Example Continued
x For Drinkers2(name, beersLiked, manf), the only FD is beersLiked>manf, and the only key is {name, beersLiked}. x beersLiked+ = {beersLiked, manf}, so we decompose Drinkers2 into:
1. Drinkers3(beersLiked, manf) 2. Drinkers4(name, beersLiked) Violation of BCNF. 42 Example Concluded
x The resulting decomposition of Drinkers :
1. Drinkers1(name, addr, favBeer) 2. Drinkers3(beersLiked, manf) 3. Drinkers4(name, beersLiked) x Notice: Drinkers1 tells us about drinkers, Drinkers3 tells us about beers, and Drinkers4 tells us the relationship between drinkers and the beers they like.
43 Third Normal Form Motivation
x There is one structure of FD's that causes trouble when we decompose. x AB >C and C >B. x There are two keys, {A,B } and {A,C }. x C >B is a BCNF violation, so we must decompose into AC, BC. Example: A = street address, B = city, C = zip code. 44 We Cannot Enforce FD's
x 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. x Example with A = street, B = city, and C = zip on the next slide.
45 An Unenforceable FD
street zip 545 Tech Sq.02138 545 Tech Sq.02139 city Cambridge Cambridge zip 02138 02139 Join tuples with equal zip codes. street city 545 Tech Sq.Cambridge 545 Tech Sq.Cambridge zip 02138 02139 Although no FD's were violated in the decomposed relations, FD street city > zip is violated by the database as a whole.
46 3NF Let's Us Avoid This Problem
x 3rd Normal Form (3NF) modifies the BCNF condition so we do not have to decompose in this problem situation. x An attribute is prime if it is a member of any key. x X >A violates 3NF if and only if X is not a superkey, and also A is not prime.
47 Example: 3NF
x In our problem situation with FD's AB >C and C >B, we have keys AB and AC. x Thus A, B, and C are each prime. x Although C >B violates BCNF, it does not violate 3NF. 48 What 3NF and BCNF Give You
x There are two important properties of a decomposition:
1. Lossless Join : 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.
49 3NF and BCNF Continued
x We can get (1) with a BCNF decomposition. x We can get both (1) and (2) with a 3NF decomposition. x But we can't always get (1) and (2) with a BCNF decomposition. streetcityzip is an example. 50 Testing for a Lossless Join
x If we project R onto R1, R2,..., Rk , can we recover R by rejoining? x Any tuple in R can be recovered from its projected fragments. x So the only question is: when we rejoin, do we ever get back something we didn't have originally?
51 The Chase Test
x Suppose tuple t comes back in the join. x Then t is the join of projections of some tuples of R, one for each Ri of the decomposition. x Can we use the given FD's to show that one of these tuples must be t ? 52 The Chase (2)
x Start by assuming t = abc... . x For each i, there is a tuple si of R that has a, b, c,... in the attributes of Ri. x si can have any values in other attributes. x We'll use the same letter as in t, but with a subscript, for these components.
53 Example: The Chase
x Let R = ABCD, and the decomposition be AB, BC, and CD. x Let the given FD's be C>D and B >A. x Suppose the tuple t = abcd is the join of tuples projected onto AB, BC, CD. 54 The tuples of R pro jected onto AB, BC, CD. The Tableau
B b b b3 C c1 c c D d1 d
Use C>D We've proved the second tuple must be t.
55 A a a2 a a3
Use B >A d2 d Summary of the Chase
1. If two rows agree in the left side of a FD, make their right sides agree too. 2. Always replace a subscripted symbol by the corresponding unsubscripted one, if possible. 3. If we ever get an unsubscripted row, we know any tuple in the projectjoin is in the original (the join is lossless). 4. Otherwise, the final tableau is a counterexample.
56 Example: Lossy Join
x Same relation R = ABCD and same decomposition. x But with only the FD C>D. 57 These projections rejoin to form abcd. The Tableau
B b b b3 C c1 c c D d1 d
Use C>D A a a2 a3 d2 d These three tuples are an example R that shows the join lossy. abcd is not in R, but we can project and rejoin to get abcd. 58 3NF Synthesis Algorithm
x We can always construct a decomposition into 3NF relations with a lossless join and dependency preservation. x Need minimal basis for the FD's: 1. Right sides are single attributes. 2. No FD can be removed. 3. No attribute can be removed from a left side.
59 Constructing a Minimal Basis
1. Split right sides. 2. Repeatedly try to remove an FD and see if the remaining FD's are equivalent to the original. 3. Repeatedly try to remove an attribute from a left side and see if the resulting FD's are equivalent to the original.
60 3NF Synthesis (2)
x One relation for each FD in the minimal basis. x If no key is contained in an FD, then add one relation whose schema is some key.
61 Schema is the union of the left and right sides. Example: 3NF Synthesis
x Relation R = ABCD. x FD's A>B and A>C. x Decomposition: AB and AC from the FD's, plus AD for a key. 62 Why It Works
x Preserves dependencies: each FD from a minimal basis is contained in a relation, thus preserved. x Lossless Join: use the chase to show that the row for the relation that contains a key can be made allunsubscripted variables. x 3NF: hard part a property of minimal bases.
63 ...
View
Full
Document
This note was uploaded on 02/21/2011 for the course CS 147 taught by Professor Dr.jeff during the Fall '07 term at SUNY Buffalo.
 Fall '07
 Dr.Jeff
 Relational Database

Click to edit the document details