Chapter 3 Design Theory for Relational Databases

Chapter 3 Design Theory for Relational Databases 1 Contents      2 Functional Dependencies Decompositions Normal Forms (BCNF, 3NF) Multival...
Author: Everett Benson
0 downloads 2 Views 667KB Size
Chapter 3 Design Theory for Relational Databases

1

Contents 

   

2

Functional Dependencies Decompositions Normal Forms (BCNF, 3NF) Multivalued Dependencies (and 4NF) Reasoning About FD’s + MVD’s

Our example of chapter 2 Some questions: Beers(name, manf) Bars(name, addr, license) 1. Why do we design Drinkers(name, addr, phone) relations like the example? Likes(drinker, beer) 2. What makes a good Sells(bar, beer, price) relational database schema? Frequents(drinker, bar)

3

A theory : “dependencies” will be talked first

3. what we can do if it has flaws?

Functional Dependencies 

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. –

– –

4

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 }.

Functional Dependency (cont.)  

Exist in a relational schema as a constraint. Agree for all instances of the schema (t and u are any two tuples) A’s

B’s

t

We have functional dependency like this A1A2…B1B2…

u

5

If t and Then they u agree must agree here here

Why we call “functional” dependency?

Functional Dependency (cont.) Some examples Beers(name, manf) namemanf manfname ? 

Sells(bar, beer, price) Bar,beer  price

6

Splitting Right Sides of FD’s    

7

X->A1A2…An holds for R exactly when each of X->A1, X->A2,…, X->An hold for R. Example: A->BC is equivalent to A->B and A->C. There is no splitting rule for left sides. We’ll generally express FD’s with singleton right sides.

Trivial Functional Dependencies Sells(bar, beer, price) Bar, Beer  bar (trivial functional dependencies) Bar, beer  price (nontrivial function dependencies) C’s

A’s  B’s A’s  C’s

A’s

B’s

Example: FD’s Drinkers(name, addr, beersLiked, manf, favBeer)  Reasonable FD’s to assert: 1.

2.

9

name -> addr favBeer (combining rule)  Note this FD is the same as name -> addr and name -> favBeer. (splitting rule)

beersLiked -> manf

Example: Possible Data name Janeway Janeway Spock

addr Voyager Voyager Enterprise

Because name -> addr

beersLiked manf Bud A.B. WickedAle Pete’s Bud A.B.

Because name -> favBeer

Because beersLiked -> manf

10

favBeer WickedAle WickedAle Bud

Keys of Relations  

11

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. (minimality)

Example: Superkey Drinkers(name, addr, beersLiked, manf, favBeer)  {name, beersLiked} is a superkey because together these attributes determine all the other attributes. – –

12

name -> addr favBeer beersLiked -> manf

Example: Key 

{name, beersLiked} is a key because neither {name} nor {beersLiked} is a superkey. –



There are no other keys, but lots of superkeys. –

13

name doesn’t -> manf; beersLiked doesn’t -> addr.

Any superset of {name, beersLiked}.

Where Do Keys Come From? Just assert a key K.

1. –

2. 3.

The only FD’s are K -> A for all attributes A.

Assert FD’s and deduce the keys by systematic exploration. More FD’s From “Physics” - Example: “no two courses can meet in the same room at the same time” tells us: hour room -> course.

14

Inferring FD’s 

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 (instance) that satisfies the given FD’s. –



15

Three ways: 1. A simple test for it 2. Use FD to deduce 3. Calculate closure of y

Example: If A -> B and B -> C hold, surely A -> C holds, even if we don’t say so.

Important for design of good relation schemas.

1: Inference Test 

To test if Y -> B, start by assuming two tuples agree in all attributes of Y.

Y 0000000. . . 0 00000?? . . . ?

16

2: Inference Test 

Use the given FD’s to infer that these tuples must also agree in certain other attributes. – –

17

If B is one of these attributes, then Y -> B is true. 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.

2: Inference Test : example  

R(A,B,C) with FD’s: AB, BC To prove AC? A

B

C

a

b1

c1

a

b2

c2

Inference steps: 1) Assume two tuples that agree on A 2) Because AB, b1=b2 3) Because BC c1=c2

18

Inference rules Reflexivity: If {B1B2…Bm}  {A1,A2,…An} then A1,A2,…An  B1B2…Bm called trivial FD’s  Augmentation: If A1,A2,…An  B1B2…Bm then, A1,A2,…An C1,C2..Ck  B1B2…Bm C1,C2..Ck  Transitivity: If A1,A2,…An  B1B2…Bm,and B1B2…Bm  C1,C2..Ck then, A1,A2,…An  C1,C2..Ck 

19

3: Closure Test   

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



End: when Y+ can not be changed. X

A

new Y+

20

3: Closure Test: example 



 1. 2.

3.

21

R(A,B,C) with FD’s: AB, BC To prove AC?

Closure and Keys: if the closure of X is all attributes of a relation, then X is a key /superkey.

Calculating steps for A+ : A+ = A A+ = A, B A+ = A, B, C AC

Computing the closure of a set of attributes • The closure algorithm Closure Pushing out Pushing out

Initial set of attributes

22

3.7 (pp.76) can discovers all true FD’s.

• We need a FD’s ( minimal basis) to represent the full set of FD’s for a relation.

Closing sets of Functional dependencies Example: R(A,B,C) with all FD’s: AB, AC, BA, BC, CA, CB, ABC, ACB, BCA,…  We are free to choose any basis for the FD’s of R, a set of FD’s that imply all the FD’s that hold for R : FD1: AB, BA, BC, CB FD2: AB, BC, CA 

23

Given Versus Implied FD’s   



24

Typically, we state a few FD’s that are known to hold for a relation R Other FD’s may follow logically from the given FD’s; these are implied FD’s. Example: R(A,B,C) with FD’s: AB, BC, CA Given FD’s A C is implied FD

Finding All Implied FD’s 



Motivation: “normalization,” the process where we break a relation schema into two or more schemas. Example: ABCD with FD’s AB ->C, C ->D, and D ->A. – –

25

Decompose into ABC, AD. What FD’s hold in ABC ? Not only AB ->C, but also C ->A !

Why? ABCD

a1b1cd1

a2b2cd2

comes from ABC

26

a1b1c

a2b2c

d1=d2 because C -> D a1=a2 because D -> A

Thus, tuples in the projection with equal C’s have equal A’s; C -> A.

Basic Idea for projecting functional dependencies Start with given FD’s and find all nontrivial FD’s that follow from the given FD’s.

1. –

2.

27

Nontrivial = right side not contained in the left.

Restrict to those FD’s that involve only attributes of the projected schema.

Simple, Exponential Algorithm 1.

2. 3.

For each set of attributes X, compute X +. Add X ->A for all A in X + - X. However, drop XY ->A whenever we discover X ->A. 

4.

28

Because XY ->A follows from X ->A in any projection.

Finally, use only FD’s involving projected attributes.

A Few Tricks  

29

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

Example: Projecting FD’s 

ABC with FD’s A ->B and B ->C. Project onto AC. –

A +=ABC ; yields A ->B, A ->C. 

– – –

30

We do not need to compute AB + or AC +.

B +=BC ; yields B ->C. C +=C ; yields nothing. BC +=BC ; yields nothing.

Example -- Continued 



Resulting FD’s: A ->B, A ->C, and Projection onto AC : A ->C. –

31

Only FD that involves a subset of {A,C }.

B ->C.

Relational Schema Design 

Goal of relational schema design is to avoid anomalies and redundancy. – –

32

Update anomaly : one occurrence of a fact is changed, but not all occurrences. Deletion anomaly : valid fact is lost when a tuple is deleted.

Example of Bad Design Drinkers(name, addr, beersLiked, manf, favBeer) name Janeway Janeway Spock

addr Voyager ??? Enterprise

beersLiked Bud WickedAle Bud

manf A.B. Pete’s ???

favBeer WickedAle ??? Bud

Data is redundant, because each of the ???’s can be figured out by using the FD’s name -> addr favBeer and beersLiked -> manf.

33

This Bad Design Also Exhibits Anomalies

name Janeway Janeway Spock

addr Voyager Voyager Enterprise

beersLiked Bud WickedAle Bud

manf A.B. Pete’s A.B.

favBeer WickedAle WickedAle 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 Anheuser-Busch manufactures Bud.

34

Solve the problem Analysis result : Problems caused by FD’s Drinkers(name, addr, beersLiked, manf, favBeer)  decompose into smaller relations : Drinker= projection (name,addr, favBeer) (Drinkers) Likes= projection (name, beersLiked) (Drinkers) Beer = projection (beersliked, manf) (Drinkers)

Drinkers = Drinker join Likes join beer less

not more, not

Solve the problem (cont.) name Janeway Janeway Spock Janeway Spock

36

addr Voyager Voyager Enterprise voyager Enterprise

Any anomalies?

WickedAle

beersLiked Bud WickedAle Bud

manf A.B. Pete’s A.B.

favBeer WickedAle WickedAle Bud

Janeway

Bud

Janeway

WickedAle

Spock

Bud

Bud

Bud

A.B.

WickedAle

Peter’s

Remember our questions:   

Why do we design relations like the example? -good design What makes a good relational database schema? -no redundancy, no Update/delete anomalies, what we can do if it has flaws? -- decomposition

New Question:  any standards for a good design?  Normal forms: a condition on a relation schema that will eliminate problems  any standards or methods for a decomposition?

Boyce-Codd Normal Form 

We say a relation R is in BCNF if whenever X ->Y is a nontrivial FD that holds in R, X is a superkey. – –

38

Remember: nontrivial means Y is not contained in X. Remember, a superkey is any superset of a key (not necessarily a proper superset).

Example Drinkers(name, addr, beersLiked, manf, favBeer) FD’s: name->addr favBeer, beersLiked->manf 

 

39

Only key is {name, beersLiked}. In each FD, the left side is not a superkey. Any one of these FD’s shows Drinkers is not in BCNF

Another Example Beers(name, manf, manfAddr) FD’s: name->manf, manf->manfAddr  Only key is {name} .  name->manf does not violate BCNF, but manf>manfAddr does.

40

Decomposition into BCNF  

Given: relation R with FD’s F. Look among the given FD’s for a BCNF violation X ->Y. –



Compute X +. –

41

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.

Decompose R Using X -> Y 

Replace R by relations with schemas: 1. 2.



42

R1 = X +. R2 = R – (X + – X ).

Project given FD’s F onto the two new relations.

Decomposition Picture R1

R-X +

X R2

43

X +- X

R

Example: BCNF Decomposition

Drinkers(name, addr, beersLiked, manf, favBeer) F = name->addr, name -> favBeer, beersLiked->manf  Pick BCNF violation name->addr.  Close the left side: {name}+ = {name, addr, favBeer}.  Decomposed relations: 1. Drinkers1(name, addr, favBeer) 2. Drinkers2(name, beersLiked, manf)

44

Example -- Continued 

 

We are not done; we need to check Drinkers1 and Drinkers2 for BCNF. Projecting FD’s is easy here. For Drinkers1(name, addr, favBeer), relevant FD’s are name->addr and name->favBeer. –

45

Thus, {name} is the only key and Drinkers1 is in BCNF.

Example -- Continued For Drinkers2(name, beersLiked, manf), the only FD is beersLiked->manf, and the only key is {name, beersLiked}.



Violation of BCNF.





beersLiked+ = {beersLiked, manf}, so we decompose Drinkers2 into: 1. 2.

46

Drinkers3(beersLiked, manf) Drinkers4(name, beersLiked)

Example -- Concluded 

The resulting decomposition of Drinkers : 1. 2. 3.



47

Drinkers1(name, addr, favBeer) Drinkers3(beersLiked, manf) Drinkers4(name, beersLiked)

Notice: Drinkers1 tells us about drinkers, Drinkers3 tells us about beers, and Drinkers4 tells us the relationship between drinkers and the beers they like.

Classroom Exercise 

Any two-attribute relation R(A,B) is in BCNF

Several Cases: 1) No nontrivial FD’s 2) AB 3) AB, BA

Third Normal Form -- Motivation  

There is one structure of FD’s that causes trouble when we decompose. AB ->C and C ->B. –





49

Example: A = street address, B = city, code.

C = zip

There are two keys, {A,B } and {A,C }. C ->B is a BCNF violation, so we must decompose into AC, BC.

We Cannot Enforce FD’s 



50

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. Example with A = street, B = city, and C = zip on the next slide.

Zipcity keeps

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.

51

3NF Let’s Us Avoid This Problem 

 

52

3rd Normal Form (3NF) modifies the BCNF condition so we do not have to decompose in this problem situation. An attribute is prime if it is a member of any key. X ->A violates 3NF if and only if X is not a superkey, and also A is not prime.

Example: 3NF   

53

In our problem situation with FD’s AB ->C and C ->B, we have keys AB and AC. Thus A, B, and C are each prime. Although C ->B violates BCNF, it does not violate 3NF.

BCNF vs. 3NF

BCNF

3NF

54

conditions

example

If X ->Y is a nontrivial FD that holds in R, X is a superkey. If X ->Y is a nontrivial FD that holds in R, X is a superkey, or Y is a prime

R(A,B,C) with AB, AC R(A,B,C) with AB >C and C ->B.

2 NF: no nonkey attribute is dependent on only a portion of the primary key. 1 NF:

every component of every tuple is an atomic value.

Properties of a decomposition 

Elimination of anomalies by a decomposition, it needs other two properties: 1.

2.

55

Lossless Join : it should be possible to project the original relations onto the decomposed schema, and then reconstruct the original. Dependency Preservation : it should be possible to check in the projected relations whether all the given FD’s are satisfied.

What 3NF and BCNF Give You   

We can get (1: Lossless Join ) with a BCNF decomposition. We can get both (1) and (2: Dependency Preservation ) with a 3NF decomposition. But we can’t always get (1) and (2) with a BCNF decomposition. –

56

street-city-zip is an example.

Testing for a Lossless Join  



57

If we project R onto R1, R2,…, Rk , can we recover R by rejoining? Any tuple in R can be recovered from its projected fragments. So the only question is: when we rejoin, do we ever get back something we didn’t have originally?

Example  

Any tuple in R can be recovered from its projected fragments. As long as FD bc holds, the joining of two projected tuples Not less, not more. cannot produce a bogus tuple A B

C

A

B

C

A

B

B C

1

2

3

1

2

3

1

2

2

3

1

2

5

4

2

5

4

2

2 5

4

2

3

4

2

5

58

The Chase Test: To prove that a tuple t in the join, using FD’s in F, also to be a tuple in R.  Suppose tuple t comes back in the join.  Then t is the join of projections of some tuples of R, one for each Ri of the decomposition.  Can we use the given FD’s to show that one of these tuples must be t ? Belong to

R(A,B,C) Decomposed into R1(A,B) × R2(B,C) = Join of R1and R2 a

59

b c1

a2 b c

Because B C, c1= c , therefore a,b,c is in R

Tuple t (a,b,c)

The Chase – (cont.) 

 



60

Start by assuming t = abc… . For each i, there is a tuple si of R that has a, b, c,… in the attributes of Ri. si can have any values in other attributes. We’ll use the same letter as in t, but with a subscript, for these components.

Example: The Chase   

61

Let R = ABCD, and the decomposition be AB, BC, and CD. Let the given FD’s be C->D and B ->A. Suppose the tuple t = abcd is the join of tuples projected onto AB, BC, CD.

The tuples of R projected onto AB, BC, CD.

The Tableau A a a2 a3

a

Use B ->A

B b b b3

C c1 c c

D d1 d2 d d Use C->D

We’ve proved the second tuple must be t.

62

Summary of the Chase 1.

2.

3.

4.

63

If two rows agree in the left side of a FD, make their right sides agree too. Always replace a subscripted symbol by the corresponding unsubscripted one, if possible. If we ever get an unsubscripted row, we know any tuple in the project-join is in the original (the join is lossless). Otherwise, the final tableau is a counterexample.

Example: Lossy Join (more tuples)

64



Same relation R = ABCD and same decomposition: AB, BC, and CD.



But with only the FD C->D.

These projections rejoin to form abcd.

A a a2 a3

The Tableau B b b b3

C c1 c c

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. More tuples

65

D d1 d2 d Use C->D

d

Some results (until now)     

66

Some decompositions can not keep lossless join (lossy join). Use chase method to find out whether the decomposition is lossy join. BCNF decomposition is lossless join, sometimes it can not keep functional dependencies. Relations with 3NF keep lossless join and also functional dependencies. How to decompose relations to reach 3NF?

3NF Synthesis Algorithm 



We can always construct a decomposition into 3NF relations with a lossless join and dependency preservation. Need minimal basis for the FD’s: 1. 2. 3.

67

Right sides are single attributes. No FD can be removed. No attribute can be removed from a left side.

Constructing a Minimal Basis 1.

2.

3.

68

Split right sides. Repeatedly try to remove an FD and see if the remaining FD’s are equivalent to the original. Repeatedly try to remove an attribute from a left side and see if the resulting FD’s are equivalent to the original.

3NF Synthesis – method 1.

2.

Find a minimal basis for F One relation for each FD in the minimal basis. 1.

2.

3.

Schema is the union of the left and right sides. XA then (XA) is a schema.

If no key is contained in an FD, then add one relation whose schema is some key.

Algorithm 3.26 is on pp.103

69

Example: 3NF Synthesis 





70

Relation R = ABCD. FD’s A->B and A->C.

Key is AD

Decomposition: AB and AC from the FD’s, plus AD for a key.

Another example Relation R(A,B,C,D,E)  FD’s ABC, CB,AD key? R has two 3NF synthesis: keys: ABE, ACE. Add 1) A minimal basis one of them 2) R1(ABC) R2(CB) R3(AD) 3) R2 is a part of R1, delete R2 4) No key is in R1, R3, add a key R4(ABE) 

71

Why It Works 





Preserves dependencies: each FD from a minimal basis is contained in a relation, thus preserved. Lossless Join: use the chase to show that the row for the relation that contains a key can be made all-unsubscripted variables. 3NF: hard part – a property of minimal bases.

Question: 72 Why we say “BCNF decomposition” ,”3NF synthesis”?

A New Form of Redundancy 

A relation is trying to represent more than one manymany relationship. name addr phones beersLiked sue a p1 b1 sue a p2 b2 sue a p2 b1 sue a p1 b2

Then these tuples must also be in the relation.  

A drinker’s phones are independent of the beers they like. Thus, each of a drinker’s phones appears with each of the beers they like in all combinations.

Definition of MVD 



74

A multivalued dependency (MVD) on R, X >->Y , says that if two tuples of R agree on all the attributes of X, then their components in Y may be swapped, and the result will be two tuples that are also in the relation. i.e., for each value of X, the values of Y are independent of the values of R-X-Y.

Example: MVD Drinkers(name, addr, phones, beersLiked)  A drinker’s phones are independent of the beers they like. –





Thus, each of a drinker’s phones appears with each of the beers they like in all combinations. This repetition is unlike FD redundancy. –

75

name->->phones and name ->->beersLiked.

name->addr is the only FD.

Tuples Implied by name->->phones If we have tuples:

name sue sue sue sue

addr a a a a

phones p1 p2 p2 p1

beersLiked b1 b2 b1 b2

Then these tuples must also be in the relation.

76

Picture of MVD X ->->Y X

Y

equal exchange

77

others

MVD Rules 

Every FD is an MVD (promotion ). – –



78

If X ->Y, then swapping Y ’s between two tuples that agree on X doesn’t change the tuples. Therefore, the “new” tuples are surely in the relation, and we know X ->->Y.

Complementation : If X ->->Y, and Z is all the other attributes, then X ->->Z.

Splitting Doesn’t Hold  

79

Like FD’s, we cannot generally split the left side of an MVD. But unlike FD’s, we cannot split the right side either --- sometimes you have to leave several attributes on the right side.

Example: Multiattribute Right Sides Drinkers(name, areaCode, phone, beersLiked, manf)  A drinker can have several phones, with the number divided between areaCode and phone (last 7 digits).  A drinker can like several beers, each with its own manufacturer. 80

Example Continued 

81

Since the areaCode-phone combinations for a drinker are independent of the beersLikedmanf combinations, we expect that the following MVD’s hold: name ->-> areaCode phone name ->-> beersLiked manf

Example Data Here is possible data satisfying these MVD’s: name Sue Sue Sue Sue

areaCode 650 650 415 415

phone 555-1111 555-1111 555-9999 555-9999

beersLiked Bud WickedAle Bud WickedAle

manf A.B. Pete’s A.B. Pete’s

But we cannot swap area codes or phones by themselves. That is, neither name->->areaCode nor name->->phone holds for this relation.

82

Fourth Normal Form 



83

The redundancy that comes from MVD’s is not removable by putting the database schema in BCNF. There is a stronger normal form, called 4NF, that (intuitively) treats MVD’s as FD’s when it comes to decomposition, but not when determining keys of the relation.

4NF Definition A relation R is in 4NF if: whenever X ->->Y is a nontrivial MVD, then X is a superkey.





Nontrivial MVD means that: 1. 2.



84

Y is not a subset of X, and X and Y are not, together, all the attributes.

Note that the definition of “superkey” still depends on FD’s only.

BCNF Versus 4NF  

Remember that every FD X ->Y is also an MVD, X ->->Y. Thus, if R is in 4NF, it is certainly in BCNF. –



85

Because any BCNF violation is a 4NF violation (after conversion to an MVD).

But R could be in BCNF and not 4NF, because MVD’s are “invisible” to BCNF.

Decomposition and 4NF 

If X ->->Y is a 4NF violation for relation R, we can decompose R using the same technique as for BCNF. 1. 2.

86

XY is one of the decomposed relations. All but Y – X is the other.

Example: 4NF Decomposition Drinkers(name, addr, phones, beersLiked) FD: name -> addr MVD’s: name ->-> phones name ->-> beersLiked  Key is {name, phones, beersLiked}.  All dependencies violate 4NF.

87

Example Continued  1.

Decompose using name -> addr: Drinkers1(name, addr) 

2.

Drinkers2(name, phones, beersLiked) 

88

In 4NF; only dependency is name -> addr. Not in 4NF. MVD’s name ->-> phones and name >-> beersLiked apply. No FD’s, so all three attributes form the key.

Example: Decompose Drinkers2 

Either MVD name ->-> phones or name ->-> beersLiked tells us to decompose to: – –

89

Drinkers3(name, phones) Drinkers4(name, beersLiked)

Reasoning About MVD’s + FD’s 



90

Problem: given a set of MVD’s and/or FD’s that hold for a relation R, does a certain FD or MVD also hold in R ? Solution: Use a tableau to explore all inferences from the given set, to see if you can prove the target dependency.

Why Do We Care? 4NF technically requires an MVD violation.

1. –

2.

91

Need to infer MVD’s from given FD’s and MVD’s that may not be violations themselves.

When we decompose, we need to project FD’s + MVD’s.

Example: Chasing a Tableau With MVD’s and FD’s 





92

To apply a FD, equate symbols, as before. To apply an MVD, generate one or both of the tuples we know must also be in the relation represented by the tableau. We’ll prove: if A->->BC and D->C, then A->C.

The Tableau for A->C Goal: prove that c1 = c2. A a a

a

B b1 b2

b2

Use A->->BC (first row’s D with second row’s BC ).

93

C c1 c2 c2

c2

D d1 d2

d1

Use D->C (first and third row agree on D, therefore agree on C ).

Example: Transitive Law for MVD’s 

If A->->B and B->->C, then A->->C. – –

94

Obvious from the complementation rule if the Schema is ABC. But it holds no matter what the schema; we’ll assume ABCD.

The Tableau for A->->C Goal: derive tuple (a,b1,c2,d1). A a a

a a

B b1 b2

d1

b1 b1

Use A->->B to swap B from the first row into the second.

95

C c1 c2

c2 c2

D d1 d2

d2

Use B->->C to swap C from the third row into the first.

Rules for Inferring MVD’s + FD’s 

Start with a tableau of two rows. – – –

96

These rows agree on the attributes of the left side of the dependency to be inferred. And they disagree on all other attributes. Use unsubscripted variables where they agree, subscripts where they disagree.

Inference: Applying a FD 

Apply a FD X->Y by finding rows that agree on all attributes of X. Force the rows to agree on all attributes of Y. – –

97

Replace one variable by the other. If the replaced variable is part of the goal tuple, replace it there too.

Inference: Applying a MVD 

Apply a MVD X->->Y by finding two rows that agree in X. –

98

Add to the tableau one or both rows that are formed by swapping the Y-components of these two rows.

Inference: Goals 



99

To test whether U->V holds, we succeed by inferring that the two variables in each column of V are actually the same. If we are testing U->->V, we succeed if we infer in the tableau a row that is the original two rows with the components of V swapped.

Inference: Endgame   

Apply all the given FD’s and MVD’s until we cannot change the tableau. If we meet the goal, then the dependency is inferred. If not, then the final tableau is a counterexample relation. –

10 0



Satisfies all given dependencies. Original two rows violate target dependency.

Relationships Among Normal Forms Property

3NF

BCNF

4NF

Most

Yes

Yes

Eliminates Redundancy due to FDs Eliminates redundancy

Due to MVDs

10 1

Preserves FDs Preserves MVD Equal to original relation

No No Yes maybe maybe maybe Yes Yes

Yes maybe maybe Yes

Summary of the chapter       



10 2

Functional dependencies Keys of a relation Minimal basis for a set of FD’s BCNF and 3NF BCNF decomposition and 3NF synthesis (with lossless-join and dependency-preserving) Multivalued dependencies 4NF Reasoning about MVD and FD

Classroom exercise 3.7.1 Use the chase test to tell whether each of the following dependencies hold in a relation R(A,B,C,D,E) with the dependencies ABC, BD, and CE a) AD d) AE 10 3

Classroom Exercise 3.3.1 a) c) and 3.5.1

10 4

R(A,B,C,D) with a) FD’s ABC, CD, DA c) ABC, BCD, CDA, ADB 1. Indicate all the BCNF violations. 2. Decompose the relations, as necessary into collections of relations that are in BCNF 3. Indicate all the 3NF violations 4. Decompose the relations into 3NF.

Solution (a) 1) C->A, C->D, D->A, AC->D, CD->A 2) Key are AB, BC, and BD BCNF: R1(AC), R2(BC), R3(CD) Or R1(CD),R2(BC),R3(AD) or… 3) No 3NF violations and why? 4) R(A,B,C,D) is already in 3NF 10 5

Solution c

10 6

i) indicate all the BCNF violations  Consider the closures of all 15 nonempty subsets of {A,B,C,D}.  A+=A, B+=B, C+=C, and D+=D. Thus we get no new FD’s.  AB+=BC+=CD+=AD+=ABCD, AC+=AC, and BD+=BD. Thus we get new nontrivial FD’s: ABD, BCA, CDB, ADC.  ABC+=ABD+=ACD+=BCD+=ABCD. Thus we get new nontrivial FD’s: ABCD, ABDC, ACDB, BCEA.  ABCD+=ABCD, so we get no new FD’s.  To sum up, we can deduce 8 new nontrivial FD’s from the given 4 FD’s.  From above, we find that AB, BC, CD and AD are keys, and that all the nontrivial FD’s for R contain a key on the left side. Thus there’re no BCNF violations. ii) R(A,B,C,D) is already in BCNF. iii) No 3NF violations. iv) R(A,B,C,D) is already in 3NF.

Homework 

 

Exercise 3.2.1 Exercise 3.5.2 Exercise 3.6.3 a), c)

作业批改助教 (TA):章建 联系地址(contact): [email protected] 10 7

Homework for chapter 3 Exercise 3.2.1

Consider a relation with schema R(A,B,C,D) and FD’s ABC, CD and DA a) What are all the nontrieval FD’s that follow from the given FD’s? b) What are all the keys of R? c) What are all the superkeys for R that are not keys? 10 8

Solution

10 9

Exercise 3.6.3 a), c) a) R(A,B,C,D) with MVD’s AB, AC c) A relation R(A,B,C,D) with MVD ABC, and FD BD  

11 0

Find all the 4NF violations Decompose the relations into a collection of relation schemas in 4NF

Solution (a)

11 1

Solution ( c )

11 2