Upload
others
View
4
Download
0
Embed Size (px)
Citation preview
Lecture6:DesignTheory
Lectures6
Announcements
• SolutionstoPS1arepostedonline.Gradescomingsoon!
• Projectpart1isout.• Checkyourgroupsandletusknowifyouhaveanyissues.• Wehaveassignedpeopletogroupsthathadonlytwomembers.
• ActivitiesandNotebooksarethereforyourbenefit!
Lecture6
Lecture6:DesignTheoryI
Lecture6
Today’sLecture
1. Normalforms&functionaldependencies• ACTIVITY:FindingFDs
2. Findingfunctionaldependencies
3. Closures,superkeys &keys• ACTIVITY:Thekeyorakey?
4
Lecture6
1.Normalforms&functionaldependencies
5
Lecture6>Section1
Whatyouwilllearnaboutinthissection
1. Overviewofdesigntheory&normalforms
2. Dataanomalies&constraints
3. Functionaldependencies
4. ACTIVITY:FindingFDs
6
Lecture6>Section1
DesignTheory
• Designtheoryisabouthowtorepresentyourdatatoavoidanomalies.
• Itisamostlymechanicalprocess• Toolscancarryoutroutineportions
• Wehaveanotebookimplementingallalgorithms!• We’llplaywithitintheactivities!
Lecture6>Section1>Overview
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
1st NormalForm(1NF)
Student CoursesMary {CS564,CS368}Joe {CS564,CS552}… …
Violates1NF.
1NFConstraint:Typesmustbeatomic!
Student CoursesMary CS564Mary CS368Joe CS564Joe CS552
In1st NF
Lecture6>Section1>Overview
DataAnomalies&Constraints
Lecture6>Section1>Dataanomalies&constraints
ConstraintsPrevent(some)AnomaliesintheData
Student Course RoomMary CS564 B01Joe CS564 B01Sam CS564 B01.. .. ..
Ifeverycourseisinonlyoneroom,containsredundantinformation!
Apoorlydesigneddatabasecausesanomalies:
Lecture6>Section1>Dataanomalies&constraints
ConstraintsPrevent(some)AnomaliesintheData
Student Course RoomMary CS564 B01Joe CS564 C12Sam CS564 B01.. .. ..
Ifweupdatetheroomnumberforonetuple,wegetinconsistentdata=anupdate anomaly
Apoorlydesigneddatabasecausesanomalies:
Lecture6>Section1>Dataanomalies&constraints
ConstraintsPrevent(some)AnomaliesintheData
Student Course Room.. .. ..
Ifeveryonedropstheclass,welosewhatroomtheclassisin!=adelete anomaly
Apoorlydesigneddatabasecausesanomalies:
Lecture6>Section1>Dataanomalies&constraints
ConstraintsPrevent(some)AnomaliesintheData
Student Course RoomMary CS564 B01Joe CS564 B01Sam CS564 B01.. .. ..
Similarly,wecan’treservearoomwithoutstudents=aninsertanomaly
Apoorlydesigneddatabasecausesanomalies:
… CS368 C12
Lecture6>Section1>Dataanomalies&constraints
ConstraintsPrevent(some)AnomaliesintheData
Student CourseMary CS564Joe CS564Sam CS564.. ..
Course RoomCS564 B01CS368 C12
Today:developtheorytounderstandwhythisdesignmaybebetterand howtofindthisdecomposition…
Isthisformbetter?
• Redundancy?• Updateanomaly?• Deleteanomaly?• Insertanomaly?
Lecture6>Section1>Dataanomalies&constraints
FunctionalDependencies
Lecture6>Section1>Functionaldependencies
FunctionalDependency
A->Bmeansthat“whenevertwotuplesagreeonAthentheyagreeonB.”
Def:LetA,Bbesets ofattributesWewriteAà BorsayAfunctionallydeterminesBif,foranytuplest1 andt2:
t1[A]=t2[A]impliest1[B]=t2[B]
andwecallAà Bafunctionaldependency
Lecture6>Section1>Functionaldependencies
APictureOfFDs
A1 … Am B1 … Bn
Defn (again):GivenattributesetsA={A1,…,Am} andB={B1,…Bn}inR,
Lecture6>Section1>Functionaldependencies
A1 … Am B1 … Bn
APictureOfFDs
ti
tj
Defn (again):GivenattributesetsA={A1,…,Am} andB={B1,…Bn}inR,
Thefunctionaldependency Aà BonRholdsifforanyti,tj inR:
Lecture6>Section1>Functionaldependencies
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
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
FDsforRelationalSchemaDesign
• High-levelidea:whydowecareaboutFDs?
1. Startwithsomerelationalschema
2. Modelitsfunctionaldependencies(FDs)
3. Usethesetodesignabetterschema1. Onewhichminimizesthepossibilityofanomalies
Lecture6>Section1>Functionaldependencies
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
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
25
MoreExamplesAnFDisaconstraintwhichholds,ordoesnothold onaninstance:
EmpID Name Phone PositionE0045 Smith 1234 ClerkE3542 Mike 9876 SalesrepE1111 Smith 9876 SalesrepE9999 Mary 1234 Lawyer
Lecture6>Section1>Functionaldependencies
26
{Position}à {Phone}
EmpID Name Phone PositionE0045 Smith 1234 ClerkE3542 Mike 9876 ¬ SalesrepE1111 Smith 9876 ¬ SalesrepE9999 Mary 1234 Lawyer
MoreExamples
Lecture6>Section1>Functionaldependencies
27
EmpID Name Phone PositionE0045 Smith 1234 ® ClerkE3542 Mike 9876 SalesrepE1111 Smith 9876 SalesrepE9999 Mary 1234 ® Lawyer
butnot {Phone}à {Position}
MoreExamples
Lecture6>Section1>Functionaldependencies
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:
{ } à { }{ } à { }{ } à { }
2.Findingfunctionaldependencies
29
Lecture6>Section2
Whatyouwilllearnaboutinthissection
1. “Good”vs.“Bad”FDs:Intuition
2. FindingFDs
3. Closures
4. ACTIVITY:Computetheclosures
30
Lecture6>Section2
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
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
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
FDsforRelationalSchemaDesign
• High-levelidea:whydowecareaboutFDs?
1. Startwithsomerelationalschema
2. Findoutitsfunctionaldependencies(FDs)
3. Usethesetodesignabetterschema1. Onewhichminimizespossibilityofanomalies
Lecture6>Section2>FindingFDs
Thispartcanbetricky!
FindingFunctionalDependencies
• Therecanbeaverylargenumber ofFDs…• Howtofindthemallefficiently?
• Wecan’tnecessarilyshowthatanyFDwillholdonallinstances…• Howtodothis?
Wewillstartwiththisproblem:GivenasetofFDs,F,whatotherFDsmusthold?
Lecture6>Section2>FindingFDs
Equivalenttoasking:GivenasetofFDs,F={f1,…fn},doesanFDghold?
Inferenceproblem:Howdowedecide?
FindingFunctionalDependencies
Lecture6>Section2>FindingFDs
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
Equivalenttoasking:GivenasetofFDs,F={f1,…fn},doesanFDghold?
Inferenceproblem:Howdowedecide?
Answer:ThreesimplerulescalledArmstrong’sRules.
1. Split/Combine,2. Reduction,and3. Transitivity…ideasbypicture
FindingFunctionalDependencies
Lecture6>Section2>FindingFDs
1.Split/Combine
A1 … Am B1 … Bn
A1,…,Am à B1,…,Bn
Lecture6>Section2>FindingFDs
1.Split/Combine
A1 … Am B1 … Bn
A1,…,Am à B1,…,Bn
…isequivalenttothefollowingn FDs…
A1,…,Am à Bi fori=1,…,n
Lecture6>Section2>FindingFDs
1.Split/Combine
A1 … Am B1 … Bn
A1,…,Am à B1,…,Bn
…isequivalentto…
Andvice-versa,A1,…,Am à Bi fori=1,…,n
Lecture6>Section2>FindingFDs
2.Reduction/TrivialA1 … Am
A1,…,Am à Aj foranyj=1,…,m
Lecture6>Section2>FindingFDs
3.TransitiveClosure
A1 … Am B1 … Bn C1 … Ck
A1,…,Am à B1,…,Bn andB1,…,Bn à C1,…,Ck
Lecture6>Section2>FindingFDs
3.TransitiveClosure
A1 … Am B1 … Bn C1 … Ck
A1,…,Am à B1,…,Bn andB1,…,Bn à C1,…,Ck
impliesA1,…,Am à C1,…,Ck
Lecture6>Section2>FindingFDs
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
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
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
Closures
Lecture6>Section2 >Closures
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
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
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
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
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
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
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
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
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
3.Closures,Superkeys &Keys
58
Lecture6>Section3
Whatyouwilllearnaboutinthissection
1. ClosuresPt.II
2. Superkeys &Keys
3. ACTIVITY:Thekeyorakey?
59
Lecture6>Section3
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
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
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
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:
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:
Superkeys andKeys
Lecture6>Section3>Superkeys &Keys
KeysandSuperkeys
Asuperkey isasetofattributesA1,…,An s.t.foranyother attributeB inR,wehave {A1,…,An}à B
Akey isaminimal superkey
I.e.allattributesarefunctionallydeterminedbyasuperkey
Meaningthatnosubsetofakeyisalsoasuperkey
Lecture6>Section3>Superkeys &Keys
FindingKeysandSuperkeys
• ForeachsetofattributesX
1. ComputeX+
2. IfX+=setofallattributesthenXisasuperkey
3. IfXisminimal,thenitisakey
Lecture6>Section3>Superkeys &Keys
ExampleofFindingKeysProduct(name, price, category, color)
{name, category} à price{category} à color
Whatisakey?
Lecture6>Section3>Superkeys &Keys
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
Activity-6.ipynb
70
Lecture6>Section3 >ACTIVITY