Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Functional Dependencies 函数依赖 Meaning of FD’s Keys and Superkeys Functional Dependencies.

Similar presentations


Presentation on theme: "1 Functional Dependencies 函数依赖 Meaning of FD’s Keys and Superkeys Functional Dependencies."— Presentation transcript:

1 1 Functional Dependencies 函数依赖 Meaning of FD’s Keys and Superkeys Functional Dependencies

2 2 uX → A 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 the attribute A. wSay “X → A holds in R.” wConvention: …, X, Y, Z represent sets of attributes; A, B, C,… represent single attributes. wConvention: no set formers in sets of attributes, just ABC, rather than {A,B,C }.

3 3 Example Movies(title, year,length,filmType,studioname) uReasonable FD’s to assert: 1.tile year → length 2.tile year → filmType 3.tile year → studioName titleyearlengthfilm typestudioNamestarName Star Wars Mighty Ducks Wayne’s World 1977 1991 1992 124 104 95 color Fox Disney Paramount Carrie Fisher Mark Hamill Harrison Ford Emilio Estevez Dana Carvey Mike Meyers

4 4 FD’s With Multiple Attributes uNo need for FD’s with > 1 attribute on right. wBut sometimes convenient to combine FD’s as a shorthand. wExample: tile year → length, tile year → filmType and tile year → studioName become tile year → length filmType studioName

5 5 Keys of Relations uK is a superkey for relation R if K functionally determines all of R. uK is a key for R if K is a superkey, but no proper subset of K is a superkey.

6 6 Example Movies(title, year,length,filmType,studioname) u {tile, year,starName} is a superkey because together these attributes determine all the other attributes. u{tile, year,starName,length,filmType} is a superkey too.

7 7 E/R and Relational Keys uKeys in E/R concern entities. uKeys in relations concern tuples. uUsually, one tuple corresponds to one entity, so the ideas are the same. uBut --- in poor relational designs, one entity can become several tuples, so E/R keys and Relational keys are different.

8 8 Example Drinkers addrname Beers manfname Bars name license addr Note: license = beer, full, none Sells Bars sell some beers. Likes Drinkers like some beers. Frequents Drinkers frequent some bars.

9 9 Example Data (Cont.) nameaddr beersLiked manffavBeer JanewayVoyager Bud A.B.WickedAle JanewayVoyager WickedAle Pete’sWickedAle SpockEnterprise Bud A.B.Bud Relational key = {name beersLiked} But in E/R, name is a key for Drinkers, and beersLiked is a key for Beers. Note: 2 tuples for Janeway entity and 2 tuples for Bud entity.

10 10 Where Do Keys Come From? 1.Just assert a key K. wThe only FD’s are K → A for all attributes A. 2.Assert FD’s and deduce the keys by systematic exploration. wE/R model gives us FD’s from entity-set keys and from many-one relationships.

11 11 More FD’s From “Physics” uExample: “no two courses can meet in the same room at the same time” tells us: hour room → course.

12 12 Rules About Functional Dependencies uThe Splitting/Combining Rule  We can replace a FD A 1 A 2 …A n → B 1 B 2 …B m by a set of FD’s A 1 A 2 …A n → B i,for i=1,2,…,m. This transformation we call splitting rules.  We can replace a set of FD’s A 1 A 2 …A n → B i for i=1,2,…,m by a set A 1 A 2 …A n → B 1 B 2 …B m,. This transformation we call combining rules.

13 13 Examples title year → length title year → studioName title year → length studioName

14 14 More Consider FD: title year → length if we try to split the left side into title → length year → length Then we get two false FD’s.

15 15 uA functional dependency A 1 A 2 …A n →B is said to be trivial( 平凡 ) if B is one of the A’s, otherwise is said to be nontrivial( 非平凡 ). wExample: Suppose Functional Dependencies title, year → title is a trivial dependency. Trivial Dependencies

16 16 The Closure of Attributes Suppose { A 1,A 2,…,A n } is a set of attributes and S is a set of FD’s. The closure of { A 1,A 2,…,A n } under the FD’s in S is the set of attributes B such that every relation that satisfies all the FD’s in the set S also satisfies A 1 A 2 …A n → B. That is, A 1 A 2 …A n → B follows from the FD’s of S.

17 17 Computing the c losure uAn easier way to test is to compute the closure of Y, denoted Y +. uBasis: Y + = Y. uInduction: 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 +.

18 18 Y+Y+ new Y + XA

19 19 Example uR = (A, B, C, G, H, I) uF = {A  B A  C CG  H CG  I B  H} u(AG) + 1.result = AG 2.result = ABCG(A  C and A  B) 3.result = ABCGH(CG  H and CG  AGBC) 4.result = ABCGHI(CG  I and CG  AGBCH) uIs AG a candidate key? 1.Is AG a super key? 1.Does AG  R? == Is (AG) +  R 2.Is any subset of AG a superkey? 1.Does A  R? == Is (A) +  R 2.Does G  R? == Is (G) +  R

20 20 例 设关系模式 R ( U,F ), 其中, U={A,B,C,D,E,I},F={A → D,AB → C,BI → C,E D → I,C → E}, 求( AC ) F + 。 解: (1) 令 X={AC}, 则 X(0)=AC 。 (2) 在 F 中找出左边是 AC 子集的函数依赖: A→D,C→E 。 (3) X(1)=X(0) ∪ D ∪ E=ACDE 。 Another Examples

21 21 (4) 很明显 X(1)≠X(0), 所以 X(i)=X(1), 并转向算法中的步 骤 (2) 。 (5) 在 F 中找出左边是 ACDE 子集的函数依赖: ED → I 。 (6) X(2)=X(1) ∪ I=ACDEI 。 (7) 虽然 X(2)≠X(1), 但是 F 中未用过的函数依赖的左边属性 已没有 X(2) 的子集, 所以, 可停止计算, 输出( AC ) F + = X(2)=ACDEI 。

22 22 uTransitive( 传递 ) Functional Dependencies wSuppose we have a relation R with three attributes A, B, and C, the FD’s A→B and B→C both hold for R. Then it is easy to see that the FD A→C also holds for R, So C is said to depend on A transitively, via B. The transitive rule

23 23 Examples uTwo of the FD’s wtitle year → studioName wstudioName → studioAddr uCombine the two FD’s above to get wtitle year → studioAddr titleyearlengthfilm typestudioNamestudioAddr Star Wars Mighty Ducks Wayne’s World 1977 1991 1992 124 104 95 color Fox Disney Paramount Holleywood Buena Vista Holleywood

24 24 Closing Sets of Functional Dependencies uAny set of given FD’s from which we can infer all the FD’s for a relation will be called a basis for that relation. If no proper subset of the FD’s in the basis can also derive the complete set of FD’s, then we say the basis is minimal, that is the closing set of the functional dependencies of the relation uConsider a relation R(A,B,C), One of its FD’s is {A → B,B → A, B → C,C → B} uAnother is {A → B,B → C,C → A}


Download ppt "1 Functional Dependencies 函数依赖 Meaning of FD’s Keys and Superkeys Functional Dependencies."

Similar presentations


Ads by Google