1 multi-valued dependencies salman azhar multi-valued dependencies fourth normal form these slides...

23
1 Multi-valued Multi-valued Dependencies Dependencies Salman Azhar Salman Azhar Multi-valued Dependencies Multi-valued Dependencies Fourth Normal Form Fourth Normal Form These slides use some figures, definitions, and explanations from These slides use some figures, definitions, and explanations from Elmasri-Navathe’s Fundamentals of Database Systems Elmasri-Navathe’s Fundamentals of Database Systems and Molina-Ullman-Widom’s Database Systems and Molina-Ullman-Widom’s Database Systems

Post on 19-Dec-2015

226 views

Category:

Documents


2 download

TRANSCRIPT

1

Multi-valued Multi-valued DependenciesDependencies Salman Azhar Salman Azhar

Multi-valued DependenciesMulti-valued Dependencies

Fourth Normal FormFourth Normal Form

These slides use some figures, definitions, and explanations from These slides use some figures, definitions, and explanations from Elmasri-Navathe’s Fundamentals of Database SystemsElmasri-Navathe’s Fundamentals of Database Systems

and Molina-Ullman-Widom’s Database Systemsand Molina-Ullman-Widom’s Database Systems

2

A New Form of A New Form of RedundancyRedundancy

• Multi-valued dependencies (MVDs) Multi-valued dependencies (MVDs) express a condition among tuples of express a condition among tuples of a relation that exists when the table a relation that exists when the table (relation) is trying to represent(relation) is trying to represent– more than one many-many relationshipmore than one many-many relationship..– then certain columns (attributes) then certain columns (attributes)

become independent of one anotherbecome independent of one another – and their values must appear in all and their values must appear in all

combinationscombinations..

3

ExampleExample

CustomersCustomers(name, addr, (name, addr, phonesphones, , sodasLikedsodasLiked))• A customer’s phones are independent of A customer’s phones are independent of

the sodas they like.the sodas they like.• Many-many relations:Many-many relations:

– Customers Customers Phones Customers Phones Customers SodasSodas

• Each phone appear with each soda in all Each phone appear with each soda in all combinations.combinations.– E.g., For 3 phones (Home, Work, Cell) and 10 E.g., For 3 phones (Home, Work, Cell) and 10

sodas, we need 30 tuplessodas, we need 30 tuples• Repetition is unlike FD redundancy.Repetition is unlike FD redundancy.• There is only one FD: name There is only one FD: name addr addr

4

Tuples Implied by Tuples Implied by IndependenceIndependence

If we have pink tuples, thenThen the blue tuples must also be in the relation

name addr phones sodasLikedsue a p1 s1sue a p2 s2

sue a p2 s1sue a p1 s2

name-phone and name-soda relations are independent:

The green relationship implies existence of blue relations

5

Definition of MVDDefinition of MVD

• A A multi-valued dependencymulti-valued dependency (MVD) (MVD) XX Y :Y :– if an assertion that if two rows of a if an assertion that if two rows of a

table agree on all the attributes of table agree on all the attributes of XX,,– then their components may be then their components may be swappedswapped

in the set of attributes in the set of attributes YY,,– and the result will be two tuples that and the result will be two tuples that

are also in the relation.are also in the relation.

6

ExampleExample

• The The name-addr-phones-sodasLikedname-addr-phones-sodasLiked example illustrated two MVDsexample illustrated two MVDs– name name phones phones – name name sodasLiked sodasLiked..

7

Picture of MVD Picture of MVD X X YY

X Y others

equal

exchangeOther

attributes get copied remain the

same

If there is an FD X Y, then swapping

Ys components doesn’t change

anything.

We must exchange all

components of Ys (not just

some)

Every FD is an MVD.

8

MVD RulesMVD Rules

• Every FD is an MVD.Every FD is an MVD.– If If XX YY, then swapping , then swapping Y Y ’s between two ’s between two

tuples that agree on tuples that agree on XX doesn’t change the doesn’t change the tuples.tuples.

– Therefore, the “new” tuples are surely in Therefore, the “new” tuples are surely in the relation, and we know the relation, and we know X X YY..

• ComplementationComplementation::– If If XX YY, and , and ZZ is all the other attributes is all the other attributes

(complement of X and Y) then (complement of X and Y) then XX ZZ..– Reason: Swapping Ys that agree on X is the Reason: Swapping Ys that agree on X is the

same effect as swapping Zs. same effect as swapping Zs.

9

Splitting Doesn’t HoldSplitting Doesn’t Hold

• Recall in FDs we could split right side, but Recall in FDs we could split right side, but could NOT split left sidecould NOT split left side– Recall FD A, B Recall FD A, B C does not imply A C does not imply AC and BC and BCC– Recall FD ARecall FD A B, C does imply A B, C does imply AB and AB and ACC

• MVDs, we MVDs, we cannotcannot generally split the left side generally split the left side• BUT, we BUT, we cannot split the right side cannot split the right side

EITHER!!!EITHER!!!– E.g., E.g., name name areaCode phone areaCode phone

• in (408) 555-9999 and (812)-555-1111, you in (408) 555-9999 and (812)-555-1111, you can’t swap area codescan’t swap area codes

– sometimes you have to deal with several sometimes you have to deal with several attributes on the right side.attributes on the right side.

10

ExampleExample

• Consider phone has been split into area code Consider phone has been split into area code and number and adding manf giving the and number and adding manf giving the following relation:following relation:

Customers(Customers(name, name, areaCodeareaCode, phone, sodasLiked,, phone, sodasLiked, manfmanf))

• We can claim:We can claim:– A customer can have several phones,A customer can have several phones,

• the number divided between areaCode and phonethe number divided between areaCode and phone

– A customer can like several sodas,A customer can like several sodas,• each with its own manufacturer.each with its own manufacturer.

– Phones are sodasLiked are independent, so we getPhones are sodasLiked are independent, so we get• all possible combinations of all possible combinations of areaCode-phoneareaCode-phone pairs pairs

& & sodasLiked-manfsodasLiked-manf pairs pairs

11

Example, ContinuedExample, Continued

• Since the areaCode-phone pairs for a Since the areaCode-phone pairs for a customer are independent of the sodasLiked-customer are independent of the sodasLiked-manf pairs, we expect the following MVDs:manf pairs, we expect the following MVDs:

name name areaCode phone areaCode phone

name name sodasLiked manf sodasLiked manf• Observe that we can’t split the right hand Observe that we can’t split the right hand

sides:sides:– areaCode and phone are linkedareaCode and phone are linked– sodasLiked and manf are linkedsodasLiked and manf are linked

• in (408) 555-9999 and (812)-555-1111, you can’t swap in (408) 555-9999 and (812)-555-1111, you can’t swap area codes!!!area codes!!!

12

Example DataExample Data

Here is possible data satisfying these MVDs:name areaCode phone sodasLiked manfSue 812 555-1111 Pepsi PepsiCoSue 812 555-1111 Sprite CocaColaSue 408 555-9999 Pepsi PepsiCoSue 408 555-9999 Sprite CocaCola

We cannot swap area codes or phones by themselves.Swapping (812) And (408) gives us incorrect phone numbersNote: neither name areaCode nor name phone holds for this relation.

Now all possible combinations are

represented.Swapping any pairs does not yield new

rows.

13

Fourth Normal FormFourth Normal Form

• The redundancy caused by MVDs can’t The redundancy caused by MVDs can’t be removed by transforming the be removed by transforming the database schema to BCNF.database schema to BCNF.

• There is a stronger normal form, called There is a stronger normal form, called 44thth Normal for (4NF), that (intuitively): Normal for (4NF), that (intuitively):– treats MVDs as FDs when it comes to treats MVDs as FDs when it comes to

decompositiondecomposition– but but notnot when determining keys of the when determining keys of the

relation.relation.

14

4NF Definition4NF Definition

• A relation A relation R R is in 4NF if whenever is in 4NF if whenever X X YY is a nontrivial MVD, then is a nontrivial MVD, then XX is a is a superkey.superkey.

– Nontrivial means that:Nontrivial means that:1.1. YY is not a subset of is not a subset of XX

(swapping components does not change tuples)(swapping components does not change tuples)

2.2. XX and and Y Y are not, together, all the attributes. are not, together, all the attributes.(swapping components yields the same tuples)(swapping components yields the same tuples)

– Note that the definition of “superkey” Note that the definition of “superkey” still depends on FDs only.still depends on FDs only.

15

BCNF Versus 4NFBCNF Versus 4NF

• Remember thatRemember that– Every FD Every FD X X Y Y is also an MVD, is also an MVD, XX YY..

• Thus, if Thus, if RR is in 4NF, it is certainly in is in 4NF, it is certainly in BCNF.BCNF.– Because any BCNF violation is a 4NF Because any BCNF violation is a 4NF

violation.violation.

• However, However, RR could be in BCNF and could be in BCNF and not 4NF, because MVDs are not 4NF, because MVDs are “invisible” to BCNF.“invisible” to BCNF.

16

Decomposition and 4NFDecomposition and 4NF

• If If XX YY is a 4NF violation for is a 4NF violation for relation relation RR, we can decompose , we can decompose RR using the same technique as for using the same technique as for BCNF.BCNF.

1.1. XYXY is one of the decomposed is one of the decomposed relations.relations.

2.2. All but (All but (YY – – X)X) is the other. is the other.

17

ExampleExample

Customers(Customers(namename, addr, , addr, phonesphones, , sodasLikedsodasLiked))

FD: FD: name name addr addrMVDs: MVDs: name name phones phones

name name sodasLiked sodasLiked

• Key is {Key is {namename, , phonesphones,, sodasLikedsodasLiked}.}.• All dependencies violate 4NF because All dependencies violate 4NF because

name is not a superkey.name is not a superkey.

18

Example, ContinuedExample, Continued

• Decompose using Decompose using name name addr addr::

1.1. Customers1(Customers1(namename, addr), addr) In 4NF, only dependency is name In 4NF, only dependency is name addr. addr.

2.2. Customers2(Customers2(namename, , phonesphones, , sodasLikedsodasLiked)) Not in 4NF because MVDsNot in 4NF because MVDs

name name phones phones and and name name sodasLiked sodasLiked apply. apply.

No FD’s, so all three attributes form the No FD’s, so all three attributes form the key.key.

19

Example: Decompose Example: Decompose Customers2Customers2

• RecallRecall– Customers2(Customers2(namename, , phonesphones, , sodasLikedsodasLiked))

• Either MVDEither MVD– name name phones phones– name name sodasLiked sodasLiked

• tells us to decompose to:tells us to decompose to:– Customers3(Customers3(namename, , phonesphones))– Customers4(Customers4(namename, , sodasLikedsodasLiked))

20

Example: Decompose Example: Decompose Customers2Customers2• 44thth Normal Form Consists of: Normal Form Consists of:

– Customers1(Customers1(namename, addr), addr)• ContactContact

– Customers3(Customers3(namename, , phonesphones))• PhonePhone

– Customers4(Customers4(namename, , sodasLikedsodasLiked))• LikesLikes

• The following FD and MVDs are satisfied:The following FD and MVDs are satisfied:FD: FD: name name addr addrMVDs: MVDs: name name phones phones

name name sodasLiked sodasLiked

21

Continue Normal FormsContinue Normal Forms

• 55thth Normal Form Normal Form• 66thth Normal Form Normal Form• 77thth Normal Form Normal Form

22

Salman Teaching 4Salman Teaching 4thth Normal FormNormal Form

• How would I look teaching 937How would I look teaching 937thth Normal Form? Normal Form?

23

Salman Teaching 957Salman Teaching 957thth Normal FormNormal Form