70
Lecture 6: Design Theory Lectures 6

Lecture 6: Design Theory · FDs for Relational Schema Design •High-level idea: why do we care about FDs? 1. Start with some relational schema 2. Find out its functional dependencies

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Lecture 6: Design Theory · FDs for Relational Schema Design •High-level idea: why do we care about FDs? 1. Start with some relational schema 2. Find out its functional dependencies

Lecture6:DesignTheory

Lectures6

Page 2: Lecture 6: Design Theory · FDs for Relational Schema Design •High-level idea: why do we care about FDs? 1. Start with some relational schema 2. Find out its functional dependencies

Announcements

• SolutionstoPS1arepostedonline.Gradescomingsoon!

• Projectpart1isout.• Checkyourgroupsandletusknowifyouhaveanyissues.• Wehaveassignedpeopletogroupsthathadonlytwomembers.

• ActivitiesandNotebooksarethereforyourbenefit!

Lecture6

Page 3: Lecture 6: Design Theory · FDs for Relational Schema Design •High-level idea: why do we care about FDs? 1. Start with some relational schema 2. Find out its functional dependencies

Lecture6:DesignTheoryI

Lecture6

Page 4: Lecture 6: Design Theory · FDs for Relational Schema Design •High-level idea: why do we care about FDs? 1. Start with some relational schema 2. Find out its functional dependencies

Today’sLecture

1. Normalforms&functionaldependencies• ACTIVITY:FindingFDs

2. Findingfunctionaldependencies

3. Closures,superkeys &keys• ACTIVITY:Thekeyorakey?

4

Lecture6

Page 5: Lecture 6: Design Theory · FDs for Relational Schema Design •High-level idea: why do we care about FDs? 1. Start with some relational schema 2. Find out its functional dependencies

1.Normalforms&functionaldependencies

5

Lecture6>Section1

Page 6: Lecture 6: Design Theory · FDs for Relational Schema Design •High-level idea: why do we care about FDs? 1. Start with some relational schema 2. Find out its functional dependencies

Whatyouwilllearnaboutinthissection

1. Overviewofdesigntheory&normalforms

2. Dataanomalies&constraints

3. Functionaldependencies

4. ACTIVITY:FindingFDs

6

Lecture6>Section1

Page 7: Lecture 6: Design Theory · FDs for Relational Schema Design •High-level idea: why do we care about FDs? 1. Start with some relational schema 2. Find out its functional dependencies

DesignTheory

• Designtheoryisabouthowtorepresentyourdatatoavoidanomalies.

• Itisamostlymechanicalprocess• Toolscancarryoutroutineportions

• Wehaveanotebookimplementingallalgorithms!• We’llplaywithitintheactivities!

Lecture6>Section1>Overview

Page 8: Lecture 6: Design Theory · FDs for Relational Schema Design •High-level idea: why do we care about FDs? 1. Start with some relational schema 2. Find out its functional dependencies

NormalForms• 1st NormalForm(1NF) =Alltablesareflat

• 2nd NormalForm =disused

• Boyce-Codd NormalForm(BCNF)

• 3rd NormalForm(3NF)

• 4thand5th NormalForms =seetextbooks

DBdesignsbasedonfunctionaldependencies,intendedtopreventdataanomalies

Ourfocusforthislecture+thenexttwoones

Lecture6>Section1>Overview

Page 9: Lecture 6: Design Theory · FDs for Relational Schema Design •High-level idea: why do we care about FDs? 1. Start with some relational schema 2. Find out its functional dependencies

1st NormalForm(1NF)

Student CoursesMary {CS564,CS368}Joe {CS564,CS552}… …

Violates1NF.

1NFConstraint:Typesmustbeatomic!

Student CoursesMary CS564Mary CS368Joe CS564Joe CS552

In1st NF

Lecture6>Section1>Overview

Page 10: Lecture 6: Design Theory · FDs for Relational Schema Design •High-level idea: why do we care about FDs? 1. Start with some relational schema 2. Find out its functional dependencies

DataAnomalies&Constraints

Lecture6>Section1>Dataanomalies&constraints

Page 11: Lecture 6: Design Theory · FDs for Relational Schema Design •High-level idea: why do we care about FDs? 1. Start with some relational schema 2. Find out its functional dependencies

ConstraintsPrevent(some)AnomaliesintheData

Student Course RoomMary CS564 B01Joe CS564 B01Sam CS564 B01.. .. ..

Ifeverycourseisinonlyoneroom,containsredundantinformation!

Apoorlydesigneddatabasecausesanomalies:

Lecture6>Section1>Dataanomalies&constraints

Page 12: Lecture 6: Design Theory · FDs for Relational Schema Design •High-level idea: why do we care about FDs? 1. Start with some relational schema 2. Find out its functional dependencies

ConstraintsPrevent(some)AnomaliesintheData

Student Course RoomMary CS564 B01Joe CS564 C12Sam CS564 B01.. .. ..

Ifweupdatetheroomnumberforonetuple,wegetinconsistentdata=anupdate anomaly

Apoorlydesigneddatabasecausesanomalies:

Lecture6>Section1>Dataanomalies&constraints

Page 13: Lecture 6: Design Theory · FDs for Relational Schema Design •High-level idea: why do we care about FDs? 1. Start with some relational schema 2. Find out its functional dependencies

ConstraintsPrevent(some)AnomaliesintheData

Student Course Room.. .. ..

Ifeveryonedropstheclass,welosewhatroomtheclassisin!=adelete anomaly

Apoorlydesigneddatabasecausesanomalies:

Lecture6>Section1>Dataanomalies&constraints

Page 14: Lecture 6: Design Theory · FDs for Relational Schema Design •High-level idea: why do we care about FDs? 1. Start with some relational schema 2. Find out its functional dependencies

ConstraintsPrevent(some)AnomaliesintheData

Student Course RoomMary CS564 B01Joe CS564 B01Sam CS564 B01.. .. ..

Similarly,wecan’treservearoomwithoutstudents=aninsertanomaly

Apoorlydesigneddatabasecausesanomalies:

… CS368 C12

Lecture6>Section1>Dataanomalies&constraints

Page 15: Lecture 6: Design Theory · FDs for Relational Schema Design •High-level idea: why do we care about FDs? 1. Start with some relational schema 2. Find out its functional dependencies

ConstraintsPrevent(some)AnomaliesintheData

Student CourseMary CS564Joe CS564Sam CS564.. ..

Course RoomCS564 B01CS368 C12

Today:developtheorytounderstandwhythisdesignmaybebetterand howtofindthisdecomposition…

Isthisformbetter?

• Redundancy?• Updateanomaly?• Deleteanomaly?• Insertanomaly?

Lecture6>Section1>Dataanomalies&constraints

Page 16: Lecture 6: Design Theory · FDs for Relational Schema Design •High-level idea: why do we care about FDs? 1. Start with some relational schema 2. Find out its functional dependencies

FunctionalDependencies

Lecture6>Section1>Functionaldependencies

Page 17: Lecture 6: Design Theory · FDs for Relational Schema Design •High-level idea: why do we care about FDs? 1. Start with some relational schema 2. Find out its functional dependencies

FunctionalDependency

A->Bmeansthat“whenevertwotuplesagreeonAthentheyagreeonB.”

Def:LetA,Bbesets ofattributesWewriteAà BorsayAfunctionallydeterminesBif,foranytuplest1 andt2:

t1[A]=t2[A]impliest1[B]=t2[B]

andwecallAà Bafunctionaldependency

Lecture6>Section1>Functionaldependencies

Page 18: Lecture 6: Design Theory · FDs for Relational Schema Design •High-level idea: why do we care about FDs? 1. Start with some relational schema 2. Find out its functional dependencies

APictureOfFDs

A1 … Am B1 … Bn

Defn (again):GivenattributesetsA={A1,…,Am} andB={B1,…Bn}inR,

Lecture6>Section1>Functionaldependencies

Page 19: Lecture 6: Design Theory · FDs for Relational Schema Design •High-level idea: why do we care about FDs? 1. Start with some relational schema 2. Find out its functional dependencies

A1 … Am B1 … Bn

APictureOfFDs

ti

tj

Defn (again):GivenattributesetsA={A1,…,Am} andB={B1,…Bn}inR,

Thefunctionaldependency Aà BonRholdsifforanyti,tj inR:

Lecture6>Section1>Functionaldependencies

Page 20: Lecture 6: Design Theory · FDs for Relational Schema Design •High-level idea: why do we care about FDs? 1. Start with some relational schema 2. Find out its functional dependencies

APictureOfFDsDefn (again):GivenattributesetsA={A1,…,Am} andB={B1,…Bn}inR,

Thefunctionaldependency Aà BonRholdsifforanyti,tj inR:

ti[A1]=tj[A1]ANDti[A2]=tj[A2]AND…ANDti[Am]=tj[Am]

A1 … Am B1 … Bn

ti

tj

Ift1,t2agreehere..

Lecture6>Section1>Functionaldependencies

Page 21: Lecture 6: Design Theory · FDs for Relational Schema Design •High-level idea: why do we care about FDs? 1. Start with some relational schema 2. Find out its functional dependencies

APictureOfFDsDefn (again):GivenattributesetsA={A1,…,Am} andB={B1,…Bn}inR,

Thefunctionaldependency Aà BonRholdsifforanyti,tj inR:

if ti[A1]=tj[A1]ANDti[A2]=tj[A2]AND…ANDti[Am]=tj[Am]

then ti[B1]=tj[B1]ANDti[B2]=tj[B2]AND…ANDti[Bn]=tj[Bn]

A1 … Am B1 … Bn

ti

tj

Ift1,t2agreehere.. …theyalsoagreehere!

Lecture6>Section1>Functionaldependencies

Page 22: Lecture 6: Design Theory · FDs for Relational Schema Design •High-level idea: why do we care about FDs? 1. Start with some relational schema 2. Find out its functional dependencies

FDsforRelationalSchemaDesign

• High-levelidea:whydowecareaboutFDs?

1. Startwithsomerelationalschema

2. Modelitsfunctionaldependencies(FDs)

3. Usethesetodesignabetterschema1. Onewhichminimizesthepossibilityofanomalies

Lecture6>Section1>Functionaldependencies

Page 23: Lecture 6: Design Theory · FDs for Relational Schema Design •High-level idea: why do we care about FDs? 1. Start with some relational schema 2. Find out its functional dependencies

FunctionalDependenciesasConstraints

Student Course RoomMary CS564 B01Joe CS564 B01Sam CS564 B01.. .. ..

Note:TheFD{Course}->{Room}holdsonthisinstance

Afunctionaldependencyisaformofconstraint

• Holds onsomeinstancesnotothers.

• Partoftheschema,helpsdefineavalidinstance.

Lecture6>Section1

Recall:aninstance ofaschemaisamultiset oftuplesconformingtothatschema,i.e.atable

Page 24: Lecture 6: Design Theory · FDs for Relational Schema Design •High-level idea: why do we care about FDs? 1. Start with some relational schema 2. Find out its functional dependencies

FunctionalDependenciesasConstraints

Student Course RoomMary CS564 B01Joe CS564 B01Sam CS564 B01.. .. ..

However,cannotprovethattheFD{Course}->{Room}ispartoftheschema

Notethat:• YoucancheckifanFDis

violated byexaminingasingleinstance;

• However,youcannotprovethatanFDispartoftheschemabyexaminingasingleinstance.• Thiswouldrequirechecking

everyvalidinstance

Lecture6>Section1

Page 25: Lecture 6: Design Theory · FDs for Relational Schema Design •High-level idea: why do we care about FDs? 1. Start with some relational schema 2. Find out its functional dependencies

25

MoreExamplesAnFDisaconstraintwhichholds,ordoesnothold onaninstance:

EmpID Name Phone PositionE0045 Smith 1234 ClerkE3542 Mike 9876 SalesrepE1111 Smith 9876 SalesrepE9999 Mary 1234 Lawyer

Lecture6>Section1>Functionaldependencies

Page 26: Lecture 6: Design Theory · FDs for Relational Schema Design •High-level idea: why do we care about FDs? 1. Start with some relational schema 2. Find out its functional dependencies

26

{Position}à {Phone}

EmpID Name Phone PositionE0045 Smith 1234 ClerkE3542 Mike 9876 ¬ SalesrepE1111 Smith 9876 ¬ SalesrepE9999 Mary 1234 Lawyer

MoreExamples

Lecture6>Section1>Functionaldependencies

Page 27: Lecture 6: Design Theory · FDs for Relational Schema Design •High-level idea: why do we care about FDs? 1. Start with some relational schema 2. Find out its functional dependencies

27

EmpID Name Phone PositionE0045 Smith 1234 ® ClerkE3542 Mike 9876 SalesrepE1111 Smith 9876 SalesrepE9999 Mary 1234 ® Lawyer

butnot {Phone}à {Position}

MoreExamples

Lecture6>Section1>Functionaldependencies

Page 28: Lecture 6: Design Theory · FDs for Relational Schema Design •High-level idea: why do we care about FDs? 1. Start with some relational schema 2. Find out its functional dependencies

ACTIVITY

28

Lecture6>Section1>ACTIVITY

A B C D E

1 2 4 3 63 2 5 1 81 4 4 5 71 2 4 3 63 2 5 1 8

Findatleastthree FDswhichareviolatedonthisinstance:

{ } à { }{ } à { }{ } à { }

Page 29: Lecture 6: Design Theory · FDs for Relational Schema Design •High-level idea: why do we care about FDs? 1. Start with some relational schema 2. Find out its functional dependencies

2.Findingfunctionaldependencies

29

Lecture6>Section2

Page 30: Lecture 6: Design Theory · FDs for Relational Schema Design •High-level idea: why do we care about FDs? 1. Start with some relational schema 2. Find out its functional dependencies

Whatyouwilllearnaboutinthissection

1. “Good”vs.“Bad”FDs:Intuition

2. FindingFDs

3. Closures

4. ACTIVITY:Computetheclosures

30

Lecture6>Section2

Page 31: Lecture 6: Design Theory · FDs for Relational Schema Design •High-level idea: why do we care about FDs? 1. Start with some relational schema 2. Find out its functional dependencies

31

“Good”vs.“Bad”FDs

Wecanstarttodevelopanotionofgoodvs.bad FDs:

EmpID Name Phone PositionE0045 Smith 1234 ClerkE3542 Mike 9876 SalesrepE1111 Smith 9876 SalesrepE9999 Mary 1234 Lawyer

Intuitively:

EmpID ->Name,Phone,Positionis“goodFD”• Minimalredundancy,

lesspossibilityofanomalies

Lecture6>Section2>Goodvs.BadFDs

Page 32: Lecture 6: Design Theory · FDs for Relational Schema Design •High-level idea: why do we care about FDs? 1. Start with some relational schema 2. Find out its functional dependencies

32

“Good”vs.“Bad”FDs

Wecanstarttodevelopanotionofgoodvs.bad FDs:

EmpID Name Phone PositionE0045 Smith 1234 ClerkE3542 Mike 9876 SalesrepE1111 Smith 9876 SalesrepE9999 Mary 1234 Lawyer

Intuitively:

EmpID ->Name,Phone,Positionis“goodFD”

ButPosition->Phoneisa“badFD”• Redundancy!

Possibilityofdataanomalies

Lecture6>Section2>Goodvs.BadFDs

Page 33: Lecture 6: Design Theory · FDs for Relational Schema Design •High-level idea: why do we care about FDs? 1. Start with some relational schema 2. Find out its functional dependencies

Student Course RoomMary CS564 B01Joe CS564 B01Sam CS564 B01.. .. ..

GivenasetofFDs(fromuser)ourgoalisto:1. FindallFDs,and2. Eliminatethe“BadOnes".

Returningtoouroriginalexample…canyouseehowthe“badFD”{Course}->{Room}couldleadtoan:• UpdateAnomaly• InsertAnomaly• DeleteAnomaly• …

“Good”vs.“Bad”FDs

Lecture6>Section2>Goodvs.BadFDs

Page 34: Lecture 6: Design Theory · FDs for Relational Schema Design •High-level idea: why do we care about FDs? 1. Start with some relational schema 2. Find out its functional dependencies

FDsforRelationalSchemaDesign

• High-levelidea:whydowecareaboutFDs?

1. Startwithsomerelationalschema

2. Findoutitsfunctionaldependencies(FDs)

3. Usethesetodesignabetterschema1. Onewhichminimizespossibilityofanomalies

Lecture6>Section2>FindingFDs

Thispartcanbetricky!

Page 35: Lecture 6: Design Theory · FDs for Relational Schema Design •High-level idea: why do we care about FDs? 1. Start with some relational schema 2. Find out its functional dependencies

FindingFunctionalDependencies

• Therecanbeaverylargenumber ofFDs…• Howtofindthemallefficiently?

• Wecan’tnecessarilyshowthatanyFDwillholdonallinstances…• Howtodothis?

Wewillstartwiththisproblem:GivenasetofFDs,F,whatotherFDsmusthold?

Lecture6>Section2>FindingFDs

Page 36: Lecture 6: Design Theory · FDs for Relational Schema Design •High-level idea: why do we care about FDs? 1. Start with some relational schema 2. Find out its functional dependencies

Equivalenttoasking:GivenasetofFDs,F={f1,…fn},doesanFDghold?

Inferenceproblem:Howdowedecide?

FindingFunctionalDependencies

Lecture6>Section2>FindingFDs

Page 37: Lecture 6: Design Theory · FDs for Relational Schema Design •High-level idea: why do we care about FDs? 1. Start with some relational schema 2. Find out its functional dependencies

FindingFunctionalDependencies

1.{Name}à {Color}2.{Category}à {Department}3.{Color,Category}à {Price}

Name Color Category Dep PriceGizmo Green Gadget Toys 49Widget Black Gadget Toys 59Gizmo Green Whatsit Garden 99

Which/howmanyotherFDsdo?!?

ProvidedFDs:Products

GiventheprovidedFDs,wecanseethat{Name,Category}à {Price}mustalsoholdonanyinstance…

Example:

Lecture6>Section2>FindingFDs

Page 38: Lecture 6: Design Theory · FDs for Relational Schema Design •High-level idea: why do we care about FDs? 1. Start with some relational schema 2. Find out its functional dependencies

Equivalenttoasking:GivenasetofFDs,F={f1,…fn},doesanFDghold?

Inferenceproblem:Howdowedecide?

Answer:ThreesimplerulescalledArmstrong’sRules.

1. Split/Combine,2. Reduction,and3. Transitivity…ideasbypicture

FindingFunctionalDependencies

Lecture6>Section2>FindingFDs

Page 39: Lecture 6: Design Theory · FDs for Relational Schema Design •High-level idea: why do we care about FDs? 1. Start with some relational schema 2. Find out its functional dependencies

1.Split/Combine

A1 … Am B1 … Bn

A1,…,Am à B1,…,Bn

Lecture6>Section2>FindingFDs

Page 40: Lecture 6: Design Theory · FDs for Relational Schema Design •High-level idea: why do we care about FDs? 1. Start with some relational schema 2. Find out its functional dependencies

1.Split/Combine

A1 … Am B1 … Bn

A1,…,Am à B1,…,Bn

…isequivalenttothefollowingn FDs…

A1,…,Am à Bi fori=1,…,n

Lecture6>Section2>FindingFDs

Page 41: Lecture 6: Design Theory · FDs for Relational Schema Design •High-level idea: why do we care about FDs? 1. Start with some relational schema 2. Find out its functional dependencies

1.Split/Combine

A1 … Am B1 … Bn

A1,…,Am à B1,…,Bn

…isequivalentto…

Andvice-versa,A1,…,Am à Bi fori=1,…,n

Lecture6>Section2>FindingFDs

Page 42: Lecture 6: Design Theory · FDs for Relational Schema Design •High-level idea: why do we care about FDs? 1. Start with some relational schema 2. Find out its functional dependencies

2.Reduction/TrivialA1 … Am

A1,…,Am à Aj foranyj=1,…,m

Lecture6>Section2>FindingFDs

Page 43: Lecture 6: Design Theory · FDs for Relational Schema Design •High-level idea: why do we care about FDs? 1. Start with some relational schema 2. Find out its functional dependencies

3.TransitiveClosure

A1 … Am B1 … Bn C1 … Ck

A1,…,Am à B1,…,Bn andB1,…,Bn à C1,…,Ck

Lecture6>Section2>FindingFDs

Page 44: Lecture 6: Design Theory · FDs for Relational Schema Design •High-level idea: why do we care about FDs? 1. Start with some relational schema 2. Find out its functional dependencies

3.TransitiveClosure

A1 … Am B1 … Bn C1 … Ck

A1,…,Am à B1,…,Bn andB1,…,Bn à C1,…,Ck

impliesA1,…,Am à C1,…,Ck

Lecture6>Section2>FindingFDs

Page 45: Lecture 6: Design Theory · FDs for Relational Schema Design •High-level idea: why do we care about FDs? 1. Start with some relational schema 2. Find out its functional dependencies

FindingFunctionalDependencies

1.{Name}à {Color}2.{Category}à {Department}3.{Color,Category}à {Price}

Name Color Category Dep PriceGizmo Green Gadget Toys 49Widget Black Gadget Toys 59Gizmo Green Whatsit Garden 99

Which/howmanyotherFDshold?

ProvidedFDs:Products

Example:

Lecture6>Section2>FindingFDs

Page 46: Lecture 6: Design Theory · FDs for Relational Schema Design •High-level idea: why do we care about FDs? 1. Start with some relational schema 2. Find out its functional dependencies

FindingFunctionalDependencies

1.{Name}à {Color}2.{Category}à {Dept.}3.{Color,Category}à{Price}

Which/howmanyotherFDshold?

ProvidedFDs:InferredFDs:

Example:

Inferred FD Ruleused

4. {Name,Category}->{Name} ?5.{Name,Category}->{Color} ?6.{Name,Category} ->{Category} ?7.{Name,Category ->{Color,Category} ?8.{Name,Category}->{Price} ?

Lecture6>Section2>FindingFDs

Page 47: Lecture 6: Design Theory · FDs for Relational Schema Design •High-level idea: why do we care about FDs? 1. Start with some relational schema 2. Find out its functional dependencies

FindingFunctionalDependencies

1.{Name}à {Color}2.{Category}à {Dept.}3.{Color,Category}à{Price}

Canwefindanalgorithmicwaytodothis?

ProvidedFDs:InferredFDs:

Example:

Inferred FD Ruleused

4. {Name,Category}->{Name} Trivial5.{Name,Category}->{Color} Transitive (4->1)6.{Name,Category} ->{Category} Trivial7.{Name,Category ->{Color,Category} Split/combine (5+6)8.{Name,Category}->{Price} Transitive(7-> 3)

Lecture6>Section2>FindingFDs

Page 48: Lecture 6: Design Theory · FDs for Relational Schema Design •High-level idea: why do we care about FDs? 1. Start with some relational schema 2. Find out its functional dependencies

Closures

Lecture6>Section2 >Closures

Page 49: Lecture 6: Design Theory · FDs for Relational Schema Design •High-level idea: why do we care about FDs? 1. Start with some relational schema 2. Find out its functional dependencies

49

ClosureofasetofAttributes

Given asetofattributesA1,…,An andasetofFDsF:Thentheclosure,{A1,…,An}+ isthesetofattributesB s.t. {A1,…,An}à B

{name} à {color}{category} à {department}{color, category} à {price}

Example: F=

ExampleClosures:

{name}+ = {name, color}{name, category}+ ={name, category, color, dept, price}{color}+ = {color}

Lecture6>Section2 >Closures

Page 50: Lecture 6: Design Theory · FDs for Relational Schema Design •High-level idea: why do we care about FDs? 1. Start with some relational schema 2. Find out its functional dependencies

50

ClosureAlgorithm

StartwithX={A1,…,An}andsetofFDsF.

Repeatuntil Xdoesn’tchange;do:

if {B1,…,Bn}à CisentailedbyF

and {B1,…,Bn}⊆ X

then addCtoX.

Return XasX+

Lecture6>Section2 >Closures

Page 51: Lecture 6: Design Theory · FDs for Relational Schema Design •High-level idea: why do we care about FDs? 1. Start with some relational schema 2. Find out its functional dependencies

51

ClosureAlgorithmStartwithX={A1,…,An},FDsF.Repeatuntil Xdoesn’tchange;do:if {B1,…,Bn}à CisinFand {B1,

…,Bn}⊆ X:then addCtoX.

Return XasX+

{name} à {color}

{category} à {dept}

{color, category} à{price}

F=

{name, category}+ ={name, category}

Lecture6>Section2 >Closures

Page 52: Lecture 6: Design Theory · FDs for Relational Schema Design •High-level idea: why do we care about FDs? 1. Start with some relational schema 2. Find out its functional dependencies

52

ClosureAlgorithmStartwithX={A1,…,An},FDsF.Repeatuntil Xdoesn’tchange;do:if {B1,…,Bn}à CisinFand {B1,

…,Bn}⊆ X:then addCtoX.

Return XasX+

{name} à {color}

{category} à {dept}

{color, category} à{price}

F=

{name, category}+ ={name, category}

{name, category}+ ={name, category, color}

Lecture6>Section2 >Closures

Page 53: Lecture 6: Design Theory · FDs for Relational Schema Design •High-level idea: why do we care about FDs? 1. Start with some relational schema 2. Find out its functional dependencies

53

ClosureAlgorithmStartwithX={A1,…,An},FDsF.Repeatuntil Xdoesn’tchange;do:if {B1,…,Bn}à CisinFand {B1,

…,Bn}⊆ X:then addCtoX.

Return XasX+

{name} à {color}

{category} à {dept}

{color, category} à{price}

F=

{name, category}+ ={name, category}

{name, category}+ ={name, category, color}

{name, category}+ ={name, category, color, dept}

Lecture6>Section2 >Closures

Page 54: Lecture 6: Design Theory · FDs for Relational Schema Design •High-level idea: why do we care about FDs? 1. Start with some relational schema 2. Find out its functional dependencies

54

ClosureAlgorithmStartwithX={A1,…,An},FDsF.Repeatuntil Xdoesn’tchange;do:if {B1,…,Bn}à CisinFand {B1,

…,Bn}⊆ X:then addCtoX.

Return XasX+

F=

{name, category}+ ={name, category}

{name, category}+ ={name, category, color, dept, price}

{name, category}+ ={name, category, color}

{name, category}+ ={name, category, color, dept}{name} à {color}

{category} à {dept}

{color, category} à{price}

Lecture6>Section2 >Closures

Page 55: Lecture 6: Design Theory · FDs for Relational Schema Design •High-level idea: why do we care about FDs? 1. Start with some relational schema 2. Find out its functional dependencies

Example

55

Compute{A,B}+ ={A,B,}

Compute{A,F}+ ={A,F,}

R(A,B,C,D,E,F) {A,B} à {C}{A,D} à {E}{B} à {D}{A,F} à {B}

Lecture6>Section2 >Closures

Page 56: Lecture 6: Design Theory · FDs for Relational Schema Design •High-level idea: why do we care about FDs? 1. Start with some relational schema 2. Find out its functional dependencies

Example

56

Compute{A,B}+ ={A,B,C,D}

Compute{A,F}+ ={A,F,B}

R(A,B,C,D,E,F) {A,B} à {C}{A,D} à {E}{B} à {D}{A,F} à {B}

Lecture6>Section2 >Closures

Page 57: Lecture 6: Design Theory · FDs for Relational Schema Design •High-level idea: why do we care about FDs? 1. Start with some relational schema 2. Find out its functional dependencies

Example

57

Compute{A,B}+ ={A,B,C,D,E}

Compute{A,F}+ ={A,B,C,D,E,F}

R(A,B,C,D,E,F) {A,B} à {C}{A,D} à {E}{B} à {D}{A,F} à {B}

Lecture6>Section2 >Closures

Page 58: Lecture 6: Design Theory · FDs for Relational Schema Design •High-level idea: why do we care about FDs? 1. Start with some relational schema 2. Find out its functional dependencies

3.Closures,Superkeys &Keys

58

Lecture6>Section3

Page 59: Lecture 6: Design Theory · FDs for Relational Schema Design •High-level idea: why do we care about FDs? 1. Start with some relational schema 2. Find out its functional dependencies

Whatyouwilllearnaboutinthissection

1. ClosuresPt.II

2. Superkeys &Keys

3. ACTIVITY:Thekeyorakey?

59

Lecture6>Section3

Page 60: Lecture 6: Design Theory · FDs for Relational Schema Design •High-level idea: why do we care about FDs? 1. Start with some relational schema 2. Find out its functional dependencies

60

WhyDoWeNeed theClosure?

• WithclosurewecanfindallFD’seasily

• TocheckifX® A

1. ComputeX+

2. CheckifAÎ X+

NoteherethatX isaset ofattributes,butA isasingleattribute.

RecalltheSplit/combine rule:Xà A1,…,Xà AnimpliesXà {A1,…,An}

Lecture6>Section3>ClosuresPt.II

Page 61: Lecture 6: Design Theory · FDs for Relational Schema Design •High-level idea: why do we care about FDs? 1. Start with some relational schema 2. Find out its functional dependencies

61

UsingClosuretoInferALLFDs{A,B} à C{A,D} à B{B} à D

Example:GivenF=Step1:ComputeX+,foreverysetofattributesX:

{A}+ = {A}{B}+ = {B,D}{C}+ = {C}{D}+ = {D}{A,B}+ = {A,B,C,D}{A,C}+ = {A,C}{A,D}+ = {A,B,C,D}{A,B,C}+ = {A,B,D}+ = {A,C,D}+ = {A,B,C,D} {B,C,D}+ = {B,C,D}{A,B,C,D}+ = {A,B,C,D}

Noneedtocomputethese-why?

Lecture6>Section3>ClosuresPt.II

Page 62: Lecture 6: Design Theory · FDs for Relational Schema Design •High-level idea: why do we care about FDs? 1. Start with some relational schema 2. Find out its functional dependencies

62

UsingClosuretoInferALLFDs{A,B} à C{A,D} à B{B} à D

Example:GivenF=Step1:ComputeX+,foreverysetofattributesX:

{A}+ = {A}, {B}+ = {B,D}, {C}+ = {C}, {D}+ = {D}, {A,B}+ = {A,B,C,D}, {A,C}+ = {A,C}, {A,D}+ = {A,B,C,D}, {A,B,C}+ = {A,B,D}+ = {A,C,D}+ = {A,B,C,D}, {B,C,D}+ = {B,C,D}, {A,B,C,D}+ = {A,B,C,D}

Step2:EnumerateallFDsXà Y,s.t. YÍ X+ andXÇ Y=Æ:

{A,B} à {C,D}, {A,D} à {B,C},{A,B,C} à {D}, {A,B,D} à {C},{A,C,D} à {B}

Lecture6>Section3>ClosuresPt.II

Page 63: Lecture 6: Design Theory · FDs for Relational Schema Design •High-level idea: why do we care about FDs? 1. Start with some relational schema 2. Find out its functional dependencies

63

UsingClosuretoInferALLFDs{A,B} à C{A,D} à B{B} à D

Example:GivenF=

{A}+ = {A}, {B}+ = {B,D}, {C}+ = {C}, {D}+ = {D}, {A,B}+ = {A,B,C,D}, {A,C}+ = {A,C}, {A,D}+ = {A,B,C,D}, {A,B,C}+ = {A,B,D}+ = {A,C,D}+ = {A,B,C,D}, {B,C,D}+ = {B,C,D}, {A,B,C,D}+ = {A,B,C,D}

Step2:EnumerateallFDsXà Y,s.t. YÍ X+ andXÇ Y=Æ:

{A,B} à {C,D}, {A,D} à {B,C},{A,B,C} à {D}, {A,B,D} à {C},{A,C,D} à {B}

“YisintheclosureofX”

Lecture6>Section3>ClosuresPt.II

Step1:ComputeX+,foreverysetofattributesX:

Page 64: Lecture 6: Design Theory · FDs for Relational Schema Design •High-level idea: why do we care about FDs? 1. Start with some relational schema 2. Find out its functional dependencies

64

UsingClosuretoInferALLFDs{A,B} à C{A,D} à B{B} à D

Example:GivenF=

{A}+ = {A}, {B}+ = {B,D}, {C}+ = {C}, {D}+ = {D}, {A,B}+ = {A,B,C,D}, {A,C}+ = {A,C}, {A,D}+ = {A,B,C,D}, {A,B,C}+ = {A,B,D}+ = {A,C,D}+ = {A,B,C,D}, {B,C,D}+ = {B,C,D}, {A,B,C,D}+ = {A,B,C,D}

Step2:EnumerateallFDsXà Y,s.t. YÍ X+ andXÇ Y=Æ:

{A,B} à {C,D}, {A,D} à {B,C},{A,B,C} à {D}, {A,B,D} à {C},{A,C,D} à {B}

TheFDXà Yisnon-trivial

Lecture6>Section3>ClosuresPt.II

Step1:ComputeX+,foreverysetofattributesX:

Page 65: Lecture 6: Design Theory · FDs for Relational Schema Design •High-level idea: why do we care about FDs? 1. Start with some relational schema 2. Find out its functional dependencies

Superkeys andKeys

Lecture6>Section3>Superkeys &Keys

Page 66: Lecture 6: Design Theory · FDs for Relational Schema Design •High-level idea: why do we care about FDs? 1. Start with some relational schema 2. Find out its functional dependencies

KeysandSuperkeys

Asuperkey isasetofattributesA1,…,An s.t.foranyother attributeB inR,wehave {A1,…,An}à B

Akey isaminimal superkey

I.e.allattributesarefunctionallydeterminedbyasuperkey

Meaningthatnosubsetofakeyisalsoasuperkey

Lecture6>Section3>Superkeys &Keys

Page 67: Lecture 6: Design Theory · FDs for Relational Schema Design •High-level idea: why do we care about FDs? 1. Start with some relational schema 2. Find out its functional dependencies

FindingKeysandSuperkeys

• ForeachsetofattributesX

1. ComputeX+

2. IfX+=setofallattributesthenXisasuperkey

3. IfXisminimal,thenitisakey

Lecture6>Section3>Superkeys &Keys

Page 68: Lecture 6: Design Theory · FDs for Relational Schema Design •High-level idea: why do we care about FDs? 1. Start with some relational schema 2. Find out its functional dependencies

ExampleofFindingKeysProduct(name, price, category, color)

{name, category} à price{category} à color

Whatisakey?

Lecture6>Section3>Superkeys &Keys

Page 69: Lecture 6: Design Theory · FDs for Relational Schema Design •High-level idea: why do we care about FDs? 1. Start with some relational schema 2. Find out its functional dependencies

ExampleofKeysProduct(name, price, category, color)

{name, category} à price{category} à color

{name, category}+ = {name, price, category, color}= thesetofallattributes⟹ thisisasuperkey⟹ thisisakey,sinceneithername norcategoryaloneisasuperkey

Lecture6>Section3>Superkeys &Keys

Page 70: Lecture 6: Design Theory · FDs for Relational Schema Design •High-level idea: why do we care about FDs? 1. Start with some relational schema 2. Find out its functional dependencies

Activity-6.ipynb

70

Lecture6>Section3 >ACTIVITY