chapter 3 topics to be discusses functional dependency key closure of function dependency 1
TRANSCRIPT
Chapter 3
Functional Dependency• A functional dependency (FD) is a constraint
between two sets of attributes in a relation from a database.
• Require that the value for a certain set of attributes determines uniquely the value for another set of attributes.
• A functional dependency is a generalization of the notion of a key.– Notation:– α→β (α determines β)– (α→β may take the form AB→C, A→BC, etc.)
2
Chapter 3
• Let R be a relation schema R and R
• The functional dependency
holds on R if and only if for any legal relations r(R), whenever any two tuples t1 and t2 of r agree on the attributes , they also agree on the attributes . That is,
t1[] = t2 [] t1[ ] = t2 [ ] • Example: Consider r(A,B ) with the following instance of r.
• On this instance, A B does NOT hold, but B A does hold.
3
1 41 53 7
Chapter 3
A B C D
A1 B1 C1 D1
A1 B2 C1 D2
A2 B2 C2 D2
A2 B3 C2 D3
A3 B3 C2 D4
5
• A->C• AB->C• AB->D• AD->B• AD->C
Find the functional dependency
Chapter 3
Example
Drinkers(name, addr, beersLiked, manf, favBeer)• Reasonable FD’s to assert:
1. name -> addr2. name -> favBeer3. beersLiked -> manf
6
Chapter 3
Example Data
7
name addr beersLiked manf favBeerJaneway Voyager Bud A.B. WickedAleJaneway Voyager WickedAle Pete’s WickedAleSpock Enterprise Bud A.B. Bud
Because name -> addr Because name -> favBeer
Because beersLiked -> manf
Chapter 3
FD’s With Multiple Attributes
• No need for FD’s with > 1 attribute on right.– But sometimes convenient to combine FD’s as
a shorthand.– Example: name -> addr and name ->
favBeer become name -> addr favBeer
• > 1 attribute on left may be essential.– Example: bar beer -> price
8
Chapter 3
Keys of Relations
• K is a superkey for relation R if K functionally determines all of R.
• K is a Candidate key for R if K is a superkey, but no proper subset of K is a superkey.
9
Chapter 3
Example
Drinkers(name, addr, beersLiked, manf,favBeer)
• {name, beersLiked} is a superkey because together these attributes determine all the other attributes.– name -> addr favBeer– beersLiked -> manf
10
Chapter 3
Example, Cont.
• {name, beersLiked} is a key because neither {name} nor {beersLiked} is a superkey.– name doesn’t -> manf; beersLiked doesn’t -> addr.
• There are no other keys, but lots of superkeys.– Any superset of {name, beersLiked}.
11
Chapter 3 12
A B C
A1 B1 C1
A1 B1 C2
A2 B1 C1
A2 B1 C3
A->BC->BAC->BCANDIDATE KEY AC->BPRIMARY KEY CAN BE ANY ONE
Find the super key ,candidate key and primary key
Chapter 3
• Find the super key ,candidate key and primary key• R(A,B,C,D)• A->C• AB->C• AB->D• AD->B• AD->C• Super key AB,AD,ABC,ABD,ABCD,• ABD,ACB,ABCD• Candidate key AB,AD• Primary key can be AB or AD
13
Chapter 3
Example Data
14
name addr beersLiked manf favBeerJaneway Voyager Bud A.B. WickedAleJaneway Voyager WickedAle Pete’s WickedAleSpock Enterprise 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.
Chapter 3
CLOUSRE OF A SET OF FUNCTIONAL DEPENDENCY
• Armstrong’s Axioms:– Reflexivity rule:• if α is a set of attributes and β is contained in α then
α→β• i.e. given AB→C, then A→B
– Augmentation rule:• given α→β and another set of attributes γ, then
γα→γβ
– Transitivity rule:• if α→β and β→γ , then α→γ
15
Chapter 3
• Other Rules:– Union rule:• if α→β and α→γ , then α→βγ
– Decomposition rule:• if α→βγ , then α→β and α→γ
– Pseudotransitivity rule:• if α→β and γβ→δ , then γα→δ
16
Chapter 3
Examp
17
ExampleExampleR=(A,B,C,G,H,I)R=(A,B,C,G,H,I)A->BA->BA->CA->CCG->HCG->HCG->ICG->IB->HB->H
Chapter 3
• Closure (F+)
• A->H Since A->B and B->H holds, we apply the transitivity rule.
• CG->HI. since cg->h and CG->I the union rule • AG->I. since A->C and CG->I pseudo transitivity
rule
18
Chapter 3
• To compute the closure of a set of functional dependencies F:
F + = Frepeat
for each functional dependency f in F+
apply reflexivity and augmentation rules on f add the resulting functional dependencies to F +
for each pair of functional dependencies f1and f2 in F +
if f1 and f2 can be combined using transitivity then add the resulting functional dependency to F +
until F + does not change any further
19
Chapter 3
• We can further simplify manual computation of F+ by using the following additional rules.– If holds and holds, then
holds (union)– If holds, then holds and
holds (decomposition)– If holds and holds, then
holds (pseudotransitivity)
20
Chapter 3
Closure of Attribute Sets• To test a set is super key or not• Given a set of attributes define the closure of under F (denoted by +)
as the set of attributes that are functionally determined by under F
• Algorithm to compute +, the closure of under F
result := ;while (changes to result) do
for each in F dobegin
if result then result := result end
21
Chapter 3
Example of Attribute Set Closure• R = (A, B, C, G, H, I)• F = {A B
A C CG HCG IB H}
• (AG)+
1. result = AG2. result = ABCG (A C and A B)3. result = ABCGH (CG H and CG AGBC)4. result = ABCGHI (CG I and CG AGBCH)
• Is AG a candidate key? 1. Is AG a super key?
1. Does AG R? == Is (AG)+ R2. Is any subset of AG a superkey?
1. Does A R? == Is (A)+ R2. Does G R? == Is (G)+ R
22
Chapter 3
Uses of Attribute ClosureThere are several uses of the attribute closure algorithm:• Testing for superkey:
– To test if is a superkey, we compute +, and check if + contains all attributes of R.• Testing functional dependencies
– To check if a functional dependency holds (or, in other words, is in F+), just check if +.
– That is, we compute + by using attribute closure, and then check if it contains . – Is a simple and cheap test, and very useful
• Computing closure of F– For each R, we find the closure +, and for each S +, we output a functional
dependency S.
23
Chapter 3
StudentID Semester Lecture TA
1234 6 Numerical Methods John
1200 4 Numerical Methods Peter1234 6 Visual Computing Thomas
1201 4 Numerical Methods Peter
1201 4 Physics II Simone
25
Excercise
Chapter 3
Solution
• StudentID → Semester. • {StudentID, Lecture} → TA • {StudentID, Lecture} → {TA, Semester}
26
Chapter 3
Find the super key of given function dependencyR(A,B,C,D)
• A->D• D->B• B->C• E->B
27
AE is super key