Introduction To Database Systems: Functional Dependencies
Introduction To Database Systems: Functional Dependencies
Werner Nutt
Functional Dependencies
1. Meaning of FDs
1. 2. 3.
Functional Dependencies
A FD is written X A or X Y Notation: X, Y, Z represent sets of attributes; A, B, C, represent single attributes X A (X determines A) is an assertion about a relation R: whenever two tuples of R agree on all the attributes of X, then they must also agree on the attribute A, or t1[X] = t2[X] implies t1[A] = t2[A] for all t1, t2 in R (analogously for X Y) Convention:We say X A holds in R Notation: No set braces in sets of attributes: just ABC, rather than {A,B,C }.
3
Example
Table Drinkers(name, addr, beersLiked, manf, favBeer)
Example FDs
Functional Dependencies
2. Keys and Superkeys
1. 2. 3.
Keys of Relations
R relation, K a set of attributes of R K is a superkey for relation R if K functionally determines all of R K is a key for R if K is a superkey, but no proper subset of K is a superkey (that is, K is a minimal superkey)
Sometimes we call keys also candidate keys, to indicate they are candidates for choosing the primary key
8
Example
Drinkers(name, addr, beersLiked, manf, favBeer) We have name addr favBeer beersLiked manf Therefore, {name, beersLiked} determine all the other attributes Hence, {name, beersLiked} is a superkey
9
Example (cntd.)
Neither {name} nor {beersLiked} is a superkey: name manf doesnt hold beersLiked addr doesnt hold Hence: {name, beersLiked} is a key There are no other keys, but lots of superkeys: Any superset of {name, beersLiked} is a superkey
10
11
Example Data
name Janeway Janeway Spock addr Voyager Voyager Enterprise beersLiked Bud WickedAle Bud manf A.B. Petes A.B. favBeer WickedAle WickedAle Bud
The relational key is {name, beersLiked} But in E/R, name is a key for Drinkers beersLiked is a key for Beers. The relation contains 2 tuples for Janeway entity 2 tuples for Bud entity.
12
Functional Dependencies
3. Inferring FDs
1. 2. 3.
14
Inferring FDs
We are given FDs X1 A1, X2 A2,, Xn An , and we want to know whether an FD YB must hold in any relation that satisfies the given FDs Example: If A B and B C hold, then surely A C holds
Inference Test
We are given a set of FDs and want to know whether YB follows from the given FDs. Test: We consider two tuples and assume they agree in all attributes of Y: Y 0000000. . . 0 00000?? . . .?
16
18
Closure Test
An easier test is based on the concept of attribute closure Let R be a relation, F a set of FDs over R, Y a set of attributes of R. The closure of Y with respect to F, written Y+, consists of all attributes that are determined by Y, given F. Observation: Y B follows from F if and only if B Y+
19
20
X Y+
A new Y+
21
Example
Contracts(cno, supplno, projno, depno, partno, qty, value) Short: CSPrDPaQV A designer has found the following set of FDs: C is a key, i.e., C SPrDPaQV A project purchases each part using a single contract, PrPa C A department purchases at most one part from a supplier, SD Pa His colleague has come up with a slightly different set: A project purchases each part using a single contract, PrPa C A contract determines project, supplier and department, C PrSD SPrD is a key, SDPr CPaQV Are the findings of the second designer different from those of the first?
22
23
24
Why?
ABCD a1b1cd1 a2b2cd2 d1=d2 because CD a1=a2 because DA a1b1c a2b2c
Projecting FDs
How can we find the FDs that hold on the projection of R? Basic Idea: 1. Start with the given FDs 2. Find all nontrivial FDs that follow from the given FDs (nontrivial = left and right sides disjoint) 3. Restrict to those FDs that involve only attributes of the projected schema
26
27
Optimizations
Suppose that Z is the set of all attributes of R. Then: + = Z+ = Z If X Y and X + = Z then Y + = Z
Example
Relation ABC with FDs A B and B C Project onto AC A+ = ABC A B, A C
(Optimization: We do not need to compute AB+ or AC+ )
30
Example: R(A,B)
{(1,2), (3,4)}
{(5,1)}
etc.
31
An FD is a Subset of Instances
For each FD X A, there is the subset of all instances that satisfy the FD Thus, we can identify an FD with a region in the space An FD is trivial if and only if it is represented by the entire space Example: A A.
32
{(1,2), (3,4)}
AB
{} {(1,2), (3,4), (1,3)} {(5,1)}
33
AB BC CD A
34
Entailment of FDs
F = { X1 A1,,Xn An } set of FDs An FD Y B follows from F or is entailed by F if every instance that satisfies all FDs in F also satisfies Y B
This can be visualized: If Y B follows from the set F = { X1 A1,,Xn An }, then in the space of instances the region for Y B must include the intersection of the regions for the FDs Xi Ai . That is: Every instance satisfying all the Xi Ai surely satisfies Y B. But an instance could satisfy Y B, yet not be in this intersection.
35
Example
AB
AC BC
36
References
In preparing the lectures I have used several sources. The main ones are the following: Books: A First Course in Database Systems, by J. Ullman and J. Widom Fundamentals of Database Systems, by R. Elmasri and S. Navathe Slides: The slides of this chapter are based on slides prepared by Jeff Ullman for his introductory course on database systems at Stanford University
37