Functional Dependencies Meaning of FDs Keys and Superkeys

  • Slides: 32
Download presentation
Functional Dependencies Meaning of FD’s Keys and Superkeys Inferring FD’s 1

Functional Dependencies Meaning of FD’s Keys and Superkeys Inferring FD’s 1

Functional Dependencies u. X -> A is an assertion about a relation R that

Functional Dependencies u. X -> 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. w Say “X -> A holds in R. ” w Convention: …, X, Y, Z represent sets of attributes; A, B, C, … represent single attributes. w Convention: no set formers in sets of attributes, just ABC, rather than {A, B, C }. 2

Example Drivers(name, addr, Cars. Liked, manf, fav. Car) u Reasonable FD’s to assert: 1.

Example Drivers(name, addr, Cars. Liked, manf, fav. Car) u Reasonable FD’s to assert: 1. name -> addr 2. name -> fav. Car 3. Cars. Liked -> manf 3

Example Data name Janeway Spock addr Cars. Liked Voyager Mustang Voyager Corvette Enterprise Because

Example Data name Janeway Spock addr Cars. Liked Voyager Mustang Voyager Corvette Enterprise Because name -> addr manf Ford G. M. Mustang fav. Car Corve Ford Because name -> fav. Car Because Cars. Liked -> manf 4

FD’s With Multiple Attributes u. No need for FD’s with > 1 attribute on

FD’s With Multiple Attributes u. No need for FD’s with > 1 attribute on right. w But sometimes convenient to combine FD’s as a shorthand. w Example: name -> addr and name -> fav. Car become name -> addr fav. Car u > 1 attribute on left may be essential. w Example: bar Car -> price 5

Keys of Relations u K is a superkey for relation R if K functionally

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

Example Drivers(name, addr, Cars. Liked, manf, fav. Car) u {name, Cars. Liked} is a

Example Drivers(name, addr, Cars. Liked, manf, fav. Car) u {name, Cars. Liked} is a superkey because together these attributes determine all the other attributes. w name -> addr fav. Car w Cars. Liked -> manf 7

Example, Cont. u{name, Cars. Liked} is a key because neither {name} nor {Cars. Liked}

Example, Cont. u{name, Cars. Liked} is a key because neither {name} nor {Cars. Liked} is a superkey. w name doesn’t -> manf; Cars. Liked doesn’t > addr. u. There are no other keys, but lots of superkeys. w Any superset of {name, Cars. Liked}. 8

E/R and Relational Keys u. Keys in E/R concern entities. u. Keys in relations

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

Example Data name Janeway Spock addr Cars. Liked Voyager Mustang Voyager Corvette Enterprise manf

Example Data name Janeway Spock addr Cars. Liked Voyager Mustang Voyager Corvette Enterprise manf Ford G. M. Mustang fav. Car Corvette Ford Relational key = {name Cars. Liked} But in E/R, name is a key for Drivers, and Cars. Liked is a key for Cars. Note: 2 tuples for Janeway entity and 2 tuples for Mustang entity 10

Where Do Keys Come From? 1. Just assert a key K. w The only

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

More FD’s From “Physics” u. Example: “no two courses can meet in the same

More FD’s From “Physics” u. Example: “no two courses can meet in the same room at the same time” tells us: hour room -> course. 12

Inferring FD’s u. We are given FD’s X 1 -> A 1, X 2

Inferring FD’s u. We are given FD’s X 1 -> A 1, X 2 -> A 2, …, Xn -> An , and we want to know whether an FD Y -> B must hold in any relation that satisfies the given FD’s. w Example: If A -> B and B -> C hold, surely A -> C holds, even if we don’t say so. u. Important for design of good relation schemas. 13

Inference Test u. To test if Y -> B, start by assuming two tuples

Inference Test u. To test if Y -> B, start by assuming two tuples agree in all attributes of Y. Y 0000000. . . 0 00000? ? . . . ? 14

Inference Test – (2) u. Use the given FD’s to infer that these tuples

Inference Test – (2) u. Use the given FD’s to infer that these tuples must also agree in certain other attributes. w If B is one of these attributes, then Y -> B is true. w Otherwise, the two tuples, with any forced equalities, form a two-tuple relation that proves Y -> B does not follow from the given FD’s. 15

Closure Test u. An easier way to test is to compute the closure of

Closure Test u. An easier way to test is to compute the closure of Y, denoted Y +. u. Basis: Y + = Y. u. 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 +. 16

X Y+ A new Y+ 17

X Y+ A new Y+ 17

Finding All Implied FD’s u. Motivation: “normalization, ” the process where we break a

Finding All Implied FD’s u. Motivation: “normalization, ” the process where we break a relation schema into two or more schemas. u. Example: ABCD with FD’s AB ->C, C ->D, and D ->A. w Decompose into ABC, AD. What FD’s hold in ABC ? w Not only AB ->C, but also C ->A ! 18

Why? ABCD a 1 b 1 cd 1 a 2 b 2 cd 2

Why? ABCD a 1 b 1 cd 1 a 2 b 2 cd 2 comes from ABC a 1 b 1 c a 2 b 2 c d 1=d 2 because C -> D a 1=a 2 because D -> A Thus, tuples in the projection with equal C’s have equal A’s; C -> A. 19

Basic Idea 1. Start with given FD’s and find all nontrivial FD’s that follow

Basic Idea 1. Start with given FD’s and find all nontrivial FD’s that follow from the given FD’s. w Nontrivial = left and right sides disjoint. 2. Restrict to those FD’s that involve only attributes of the projected schema. 20

Simple, Exponential Algorithm 1. For each set of attributes X, compute X +. 2.

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. u Because XY ->A follows from X ->A in any projection. 4. Finally, use only FD’s involving projected 21 attributes.

A Few Tricks u. No need to compute the closure of the empty set

A Few Tricks u. No need to compute the closure of the empty set or of the set of all attributes. u. If we find X + = all attributes, so is the closure of any superset of X. 22

Example u. ABC with FD’s A ->B and B ->C. Project onto AC. w

Example u. ABC with FD’s A ->B and B ->C. Project onto AC. w A +=ABC ; yields A ->B, A ->C. • We do not need to compute AB + or AC +. w B +=BC ; yields B ->C. w C +=C ; yields nothing. w BC +=BC ; yields nothing. 23

Example --- Continued u. Resulting FD’s: A ->B, A ->C, and ->C. u. Projection

Example --- Continued u. Resulting FD’s: A ->B, A ->C, and ->C. u. Projection onto AC : A ->C. B w Only FD that involves a subset of {A, C }. 24

A Geometric View of FD’s u. Imagine the set of all instances of a

A Geometric View of FD’s u. Imagine the set of all instances of a particular relation. u. That is, all finite sets of tuples that have the proper number of components. u. Each instance is a point in this space. 25

Example: R(A, B) {(1, 2), (3, 4)} {} {(5, 1)} {(1, 2), (3, 4),

Example: R(A, B) {(1, 2), (3, 4)} {} {(5, 1)} {(1, 2), (3, 4), (1, 3)} 26

An FD is a Subset of Instances u For each FD X -> A

An FD is a Subset of Instances u For each FD X -> A there is a subset of all instances that satisfy the FD. u We can represent an FD by a region in the space. u Trivial FD = an FD that is represented by the entire space. w Example: A -> A. 27

Example: A -> B for R(A, B) {(1, 2), (3, 4)} A -> B

Example: A -> B for R(A, B) {(1, 2), (3, 4)} A -> B {} {(5, 1)} {(1, 2), (3, 4), (1, 3)} 28

Representing Sets of FD’s u. If each FD is a set of relation instances,

Representing Sets of FD’s u. If each FD is a set of relation instances, then a collection of FD’s corresponds to the intersection of those sets. w Intersection = all instances that satisfy all of the FD’s. 29

Example Instances satisfying A->B, B->C, and CD->A A->B B->C CD->A 30

Example Instances satisfying A->B, B->C, and CD->A A->B B->C CD->A 30

Implication of FD’s u. If an FD Y -> B follows from FD’s X

Implication of FD’s u. If an FD Y -> B follows from FD’s X 1 -> A 1, …, 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. w That is, every instance satisfying all the FD’s Xi -> Ai surely satisfies Y -> B. w But an instance could satisfy Y -> B, yet not be in this intersection. 31

Example A->B A->C B->C 32

Example A->B A->C B->C 32