Upload
others
View
2
Download
0
Embed Size (px)
Citation preview
367
L14:Normalization
CS3200 Databasedesign(sp18 s2)https://course.ccs.neu.edu/cs3200sp18s2/3/1/2018
368
Announcements!
• KeepbringingyournameplatesJ• Outlinetoday- MoreNormalization- Project1discussion&TransactionsafterSpringBreak
369
Closures, Superkeys,and (Candidate) Keys
370
Whatwewillseenext
• ClosuresPart2
• Superkeys &Keys
• Practice:Thekeyorakey?
371
WhyDoWeNeedtheClosure?
• WithclosurewecanfindallFD’seasily
• TocheckifX® A
- ComputeX+
- CheckifAÎ X+
NoteherethatX isaset ofattributes,butA isasingleattribute.WhydoesconsideringFDsofthisformsuffice?
RecalltheSplit/combine rule:Xà A1,…,Xà AnimpliesXà {A1,…,An}
372
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}
Noneedtocomputeallofthese- why?
373
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}
374
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”
Step1:ComputeX+,foreverysetofattributesX:
375
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
Step1:ComputeX+,foreverysetofattributesX:
376
KeysandSuperkeys
Asuperkey isasetofattributesA1,…,An s.t.foranyother attributeB inR,wehave {A1,…,An}à B
Akey isaminimal superkey(alsocalled"candidatekey")
I.e.allattributesarefunctionallydeterminedbyasuperkey
Thismeansthatnosubsetofakeyisalsoasuperkey (i.e.,droppinganyattributefromthekeymakesitnolongerasuperkey)
377
FindingKeysandSuperkeys
• ForeachsetofattributesX
- ComputeX+
- IfX+ =setofallattributesthenXisasuperkey
- IfXisminimal,thenitisakey
378
ExampleofFindingKeys
Product(name, price, category, color)
{name, category} à price{category} à color
Whatisakey?
379
ExampleofFindingKeys
Product(name, price, category, color)
{name, category} à price{category} à color
{name, category}+ = {name, price, category, color}= thesetofallattributes⟹ thisisasuperkey⟹ thisisakey,sinceneithername norcategoryaloneisasuperkey
380
Practice
• Activity-21.ipynb
381
CompleteNormalization Practice!
382
ParkingTickets:OriginalList
383
ParkingTickets:OriginalList
ST ID L Name F Name Phone No St Lic Lic No Ticket # Date Code Fine
38249 Brown Thomas 111-7804 FL BRY 123 15634 10/17/10 2 $25
38249 Brown Thomas 111-7804 FL BRY 123 16017 11/13/10 1 $15
82453 Green Sally 391-1689 AL TRE 141 14987 10/05/10 3 $100
82453 Green Sally 391-1689 AL TRE 141 16293 11/18/10 1 $15
82453 Green Sally 391-1689 AL TRE-141 17892 12/13/10 2 $25
384
ParkingTickets:OriginalList
ST ID L Name F Name Phone No St Lic Lic No Ticket # Date Code Fine
38249 Brown Thomas 111-7804 FL BRY 123 15634 10/17/10 2 $25
38249 Brown Thomas 111-7804 FL BRY 123 16017 11/13/10 1 $15
82453 Green Sally 391-1689 AL TRE 141 14987 10/05/10 3 $100
82453 Green Sally 391-1689 AL TRE 141 16293 11/18/10 1 $15
82453 Green Sally 391-1689 AL TRE-141 17892 12/13/10 2 $25
385
ParkingTickets:Relationin1NF
STID LName FName PhoneNo StLic LicNo Ticketnr Date Code Fine
38249 Brown Thomas 111-7804 FL BRY 123 15634 10/17/10 2 $25
38249 Brown Thomas 111-7804 FL BRY 123 16017 11/13/10 1 $15
82453 Green Sally 391-1689 AL TRE 141 14987 10/05/10 3 $100
82453 Green Sally 391-1689 AL TRE 141 16293 11/18/10 1 $15
82453 Green Sally 391-1689 AL TRE-141 17892 12/13/10 2 $25
ParkingTickets
386
ParkingTickets:Relationin1NF
STID LName FName PhoneNo StLic LicNo Ticketnr Date Code Fine
38249 Brown Thomas 111-7804 FL BRY 123 15634 10/17/10 2 $25
38249 Brown Thomas 111-7804 FL BRY 123 16017 11/13/10 1 $15
82453 Green Sally 391-1689 AL TRE 141 14987 10/05/10 3 $100
82453 Green Sally 391-1689 AL TRE 141 16293 11/18/10 1 $15
82453 Green Sally 391-1689 AL TRE-141 17892 12/13/10 2 $25
ParkingTickets
387
ParkingTickets:Dependencydiagram
STID LName FName PhoneNo StLic LicNo Ticketnr Date Code Fine
38249 Brown Thomas 111-7804 FL BRY 123 15634 10/17/10 2 $25
38249 Brown Thomas 111-7804 FL BRY 123 16017 11/13/10 1 $15
82453 Green Sally 391-1689 AL TRE 141 14987 10/05/10 3 $100
82453 Green Sally 391-1689 AL TRE 141 16293 11/18/10 1 $15
82453 Green Sally 391-1689 AL TRE-141 17892 12/13/10 2 $25
STID LName FName PhoneNo StLic LicNo Ticketnr Date Code Fine
Assume,eachstudentcanhavemaximalonecar:
ParkingTickets
388
ParkingTickets:Dependencydiagram
STID LName FName PhoneNo StLic LicNo Ticketnr Date Code Fine
38249 Brown Thomas 111-7804 FL BRY 123 15634 10/17/10 2 $25
38249 Brown Thomas 111-7804 FL BRY 123 16017 11/13/10 1 $15
82453 Green Sally 391-1689 AL TRE 141 14987 10/05/10 3 $100
82453 Green Sally 391-1689 AL TRE 141 16293 11/18/10 1 $15
82453 Green Sally 391-1689 AL TRE-141 17892 12/13/10 2 $25
STID LName FName PhoneNo StLic LicNo Ticketnr Date Code Fine
Assume,eachstudentcanhavemaximalonecar:
ParkingTickets
389
ParkingTickets:Dependencydiagram
STID LName FName PhoneNo StLic LicNo Ticketnr Date Code Fine
38249 Brown Thomas 111-7804 FL BRY 123 15634 10/17/10 2 $25
38249 Brown Thomas 111-7804 FL BRY 123 16017 11/13/10 1 $15
82453 Green Sally 391-1689 AL TRE 141 14987 10/05/10 3 $100
82453 Green Sally 391-1689 AL TRE 141 16293 11/18/10 1 $15
82453 Green Sally 391-1689 AL TRE-141 17892 12/13/10 2 $25
STID LName FName PhoneNo StLic LicNo Ticketnr Date Code Fine
Nextassume,studentscanhavemorethanonecar:
Assume,eachstudentcanhavemaximalonecar:
ParkingTickets
390
ParkingTickets:Dependencydiagram
STID LName FName PhoneNo StLic LicNo Ticketnr Date Code Fine
38249 Brown Thomas 111-7804 FL BRY 123 15634 10/17/10 2 $25
38249 Brown Thomas 111-7804 FL BRY 123 16017 11/13/10 1 $15
82453 Green Sally 391-1689 AL TRE 141 14987 10/05/10 3 $100
82453 Green Sally 391-1689 AL TRE 141 16293 11/18/10 1 $15
82453 Green Sally 391-1689 AL TRE-141 17892 12/13/10 2 $25
STID LName FName PhoneNo StLic LicNo Ticketnr Date Code Fine
Nextassume,studentscanhavemorethanonecar:
Assume,eachstudentcanhavemaximalonecar:
ParkingTickets
391
ParkingTickets:Relationsin3NF
STID LName FName PhoneNo StLic LicNo Ticketnr Date Code Fine
Assume,eachstudentcanhavemaximalonecar:
392
ParkingTickets:Relationsin3NF
STID LName FName PhoneNo StLic LicNo Ticketnr Date Code Fine
Assume,eachstudentcanhavemaximalonecar:
STID LName FName PhoneNo StLic LicNo
Code Fine
Ticketnr Date Code@ STID@
Student
TicketCode
Ticket
393
ParkingTickets:Relationsin3NF
STID LName FName PhoneNo StLic LicNo Ticketnr Date Code Fine
Nextassume,studentscanhavemorethanonecar:
394
ParkingTickets:Relationsin3NF
STID LName FName PhoneNo StLic LicNo Ticketnr Date Code Fine
Nextassume,studentscanhavemorethanonecar:
STID LName FName PhoneNo
Code Fine
Ticketnr Date Code@ StLic@ LicNo@
Student
TicketCode
Ticket
StLic LicNo STID@Car
395
ParkingTickets:AddingViolationAssume,eachstudentcanhavemaximalonecar:
STID LName FName PhoneNo StLic LicNo
Code Fine
Ticketnr Date Code@ STID@
Student
TicketCode
Ticket
396
ParkingTickets:AddingViolationAssume,eachstudentcanhavemaximalonecar:
STID LName FName PhoneNo StLic LicNo
Code Fine
Ticketnr Date Code@ STID@
Student
TicketCode
Ticket
Violation
397
ParkingTickets:ERDiagram
STID LName FName PhoneNo StLic LicNo
Code Fine
Ticketnr Date Code@ STID@
Student
TicketCode
Ticket
Violation
ERdiagramsdonotshowforeignkeys!
398
CompleteNormalization Practice!
399
Example:DreamHomeRental
Source:Connolly,Begg:Databasesystems,4thed,p.423,2005.
MembersofDreamHomeinspectproperties• Whenstaffarerequiredtoundertaketheseinspections,theyareallocateda
companycarforuseonthedayoftheinspections.(Onecarperperson&day)• However,acarmaybeallocatedtoseveralmembersofstaffasrequired
throughouttheworkingday.• Amemberofstaffmayinspectseveralpropertiesonagivendate,buta
propertyisonlyinspectedonceonagivendate.
StaffPropertyInspectionpropertyNo pAddress iDate iTime comments staffNo sName carReg
PG4 6LawrenceSt,Glasgow
18-Oct-03 10:00 needtoreplacecrockery
SG37 AnnBeech M231JGR
22-Apr-04 09:00 ingoodorder SG14 DavidFord M533HDR
1-Oct-04 12:00 damprotinbathroom
SG14 DavidFord N721HFR
PG16 5NovarDr,Glasgow
22-Apr-04 13:00 replacelivingroomcarpet
SG14 DavidFord M533HDR
24-Oct-04 14:00 goodcondition SG37 AnnBeech N721HFR
Canadatabasestorethisinformation?Isitin1NF?
400
Example:DreamHomeRental
Source:Connolly,Begg:Databasesystems,4thed,p.423,2005.
MembersofDreamHomeinspectproperties• Whenstaffarerequiredtoundertaketheseinspections,theyareallocateda
companycarforuseonthedayoftheinspections.(Onecarperperson&day)• However,acarmaybeallocatedtoseveralmembersofstaffasrequired
throughouttheworkingday.• Amemberofstaffmayinspectseveralpropertiesonagivendate,buta
propertyisonlyinspectedonceonagivendate.
StaffPropertyInspectionpropertyNo iDate iTime pAddress comments staffNo sName carReg
PG4 18-Oct-03 10:00 6LawrenceSt,Glasgow
needtoreplacecrockery
SG37 AnnBeech M231JGR
PG4 22-Apr-04 09:00 6LawrenceSt,Glasgow
ingoodorder SG14 DavidFord M533HDR
PG4 1-Oct-04 12:00 6LawrenceSt,Glasgow
damprotinbathroom
SG14 DavidFord N721HFR
PG16 22-Apr-04 13:00 5NovarDr,Glasgow replacelivingroomcarpet
SG14 DavidFord M533HDR
PG16 24-Oct-04 14:00 5NovarDr,Glasgow goodcondition SG37 AnnBeech N721HFR
No!Onlynowadatabasecanstoretheinformation:1NFButwestillneedaprimarykey
401
Example:DreamHomeRental
Source:Connolly,Begg:Databasesystems,4thed,p.423,2005.
MembersofDreamHomeinspectproperties• Whenstaffarerequiredtoundertaketheseinspections,theyareallocateda
companycarforuseonthedayoftheinspections.(Onecarperperson&day)• However,acarmaybeallocatedtoseveralmembersofstaffasrequired
throughouttheworkingday.• Amemberofstaffmayinspectseveralpropertiesonagivendate,buta
propertyisonlyinspectedonceonagivendate.
StaffPropertyInspectionpropertyNo iDate iTime pAddress comments staffNo sName carReg
PG4 18-Oct-03 10:00 6LawrenceSt,Glasgow
needtoreplacecrockery
SG37 AnnBeech M231JGR
PG4 22-Apr-04 09:00 6LawrenceSt,Glasgow
ingoodorder SG14 DavidFord M533HDR
PG4 1-Oct-04 12:00 6LawrenceSt,Glasgow
damprotinbathroom
SG14 DavidFord N721HFR
PG16 22-Apr-04 13:00 5NovarDr,Glasgow replacelivingroomcarpet
SG14 DavidFord M533HDR
PG16 24-Oct-04 14:00 5NovarDr,Glasgow goodcondition SG37 AnnBeech N721HFR
Now1NF+PK
402
Example:DreamHomeRental
Source:Connolly,Begg:Databasesystems,4thed,p.423,2005.
propertyNo iDate iTime pAddress comments staffNo sName carReg
MembersofDreamHomeinspectproperties• Whenstaffarerequiredtoundertaketheseinspections,theyareallocateda
companycarforuseonthedayoftheinspections.(Onecarperperson&day)• However,acarmaybeallocatedtoseveralmembersofstaffasrequired
throughouttheworkingday.• Amemberofstaffmayinspectseveralpropertiesonagivendate,buta
propertyisonlyinspectedonceonagivendate.
StaffPropertyInspection
DrawallFDs
403
Example:DreamHomeRental
Source:Connolly,Begg:Databasesystems,4thed,p.423,2005.
propertyNo iDate iTime pAddress comments staffNo sName carReg
(full,PK)
MembersofDreamHomeinspectproperties• Whenstaffarerequiredtoundertaketheseinspections,theyareallocateda
companycarforuseonthedayoftheinspections.(Onecarperperson&day)• However,acarmaybeallocatedtoseveralmembersofstaffasrequired
throughouttheworkingday.• Amemberofstaffmayinspectseveralpropertiesonagivendate,buta
propertyisonlyinspectedonceonagivendate.
StaffPropertyInspection
404
Example:DreamHomeRental
Source:Connolly,Begg:Databasesystems,4thed,p.423,2005.
propertyNo iDate iTime pAddress comments staffNo sName carReg
(partial)
(full,PK)
(transitive)
MembersofDreamHomeinspectproperties• Whenstaffarerequiredtoundertaketheseinspections,theyareallocateda
companycarforuseonthedayoftheinspections.(Onecarperperson&day)• However,acarmaybeallocatedtoseveralmembersofstaffasrequired
throughouttheworkingday.• Amemberofstaffmayinspectseveralpropertiesonagivendate,buta
propertyisonlyinspectedonceonagivendate.
StaffPropertyInspection
405
Example:DreamHomeRental
Source:Connolly,Begg:Databasesystems,4thed,p.423,2005.
propertyNo iDate iTime pAddress comments staffNo sName carReg
(partial)
(full,PK)
(transitive)
MembersofDreamHomeinspectproperties• Whenstaffarerequiredtoundertaketheseinspections,theyareallocateda
companycarforuseonthedayoftheinspections.(Onecarperperson&day)• However,acarmaybeallocatedtoseveralmembersofstaffasrequired
throughouttheworkingday.• Amemberofstaffmayinspectseveralpropertiesonagivendate,buta
propertyisonlyinspectedonceonagivendate.
StaffPropertyInspection
(other)
406
Example:DreamHomeRental
Source:Connolly,Begg:Databasesystems,4thed,p.423,2005.
propertyNo iDate iTime pAddress comments staffNo sName carReg
(partial)
(full,PK)
(transitive)
(CandidateK)
(CandidateK)
MembersofDreamHomeinspectproperties• Whenstaffarerequiredtoundertaketheseinspections,theyareallocateda
companycarforuseonthedayoftheinspections.(Onecarperperson&day)• However,acarmaybeallocatedtoseveralmembersofstaffasrequired
throughouttheworkingday.• Amemberofstaffmayinspectseveralpropertiesonagivendate,buta
propertyisonlyinspectedonceonagivendate.
StaffPropertyInspection
(other)
407
Example:DreamHomeRental
Source:Connolly,Begg:Databasesystems,4thed,p.423,2005.
propertyNo pAddress
(PK,nowfull,formerpartial)
staffNo sName
propertyNo@
iDate iTime comments staffNo@ carReg
(PK,nowfull,formertransitive)
Property
Staff
Inspection
(other)
(PK)
408
Example:DreamHomeRental
Source:Connolly,Begg:Databasesystems,4thed,p.423,2005.
propertyNo pAddressProperty
propertyNo@
iDate@ iTime comments staffNo@Inspection
iDate staffNo@ carRegStaffCar
staffNo sNameStaff
(PK)
Wealsoneedtokeeptrackofthefactthat"staffno@"wasalreadyaforeignkeybeforeweputitintoanothertable
Extraquestion:WenowhaveacompositeFK(idate,staffno)fromINSPECTIONtoSTAFFCAR.Thus(idate,staffno)isacompositePKinSTAFFCAR.Assumeweliketoreplaceitwithasurrogatekey.Howwouldtheresultingcompletelynormalizedtableslooklike?
409
scid iDate staffNo@ carReg
Example:DreamHomeRental
Source:Connolly,Begg:Databasesystems,4thed,p.423,2005.
propertyNo pAddressProperty
propertyNo@
scid@ iTime commentsInspection
StaffCar
staffNo sNameStaff
(PK)
Thisisnowfullynormalized.Downside:weneedtojoinINSPECTIONwithSTAFFCAReverytimeweliketofindoutaboutwhenaproperty(by"properyNo")waslastinspected
410
Boyce-Codd Normal Form
411
QuickrecapFDs• FunctionalDependency:Thevalueofonesetofattributes(thedeterminant)
uniquelydeterminesthevalueofanothersetofattributes(thedependents)• Asuperkey isasasetofattributesofarelationschemauponwhichallattributes
oftheschemaarefunctionallydependent.• Acandidatekey(CK) isanattributeornon-redundantcombinationofattributes
thatuniquelyidentifiesarowinarelation• 2NF:noPartialFD:AFDinwhichoneormorenonkeyattributesarefunctionally
dependentonpart(butnotall)ofthePK• 3NF:noTransitivedependency:AnFDbetweentwo(ormore)nonkeyattributes
412
Boyce-CoddNormalForm(BCNF)
• Boyce-Coddnormalform(BCNF)- ArelationisinBCNF,ifandonlyif,everydeterminantisacandidatekey.
• Thedifferencebetween3NFandBCNFisthatforafunctionaldependencyAàB,- 3NFallowsthisdependencyinarelationifBisaprimary-keyattribute andAisnotacandidatekey,
- whereasBCNFinsiststhatforthisdependencytoremaininarelation,Amustbeacandidatekey.
413
3NFtoBCNF
Source:Hoffer,Ramesh,Topi,Moderndatabasemanagement,10th ed,AppendixB,2010.
414
3NFtoBCNF
415
3NFtoBCNF
Source:Hoffer,Ramesh,Topi,Moderndatabasemanagement,10th ed,AppendixB,2010.
416
BCNFvs3NF• BCNF:ForeveryfunctionaldependencyX->YinasetF offunctionaldependenciesoverrelationR,either:- XisasuperkeyofR- (orYisasubsetofX)
• 3NF:ForeveryfunctionaldependencyX->YinasetFoffunctionaldependenciesoverrelationR,either:- XisasuperkeyofR,or- YisasubsetofKforsomekeyKofR
• N.b., nosubsetofakeyisakey- (orYisasubsetofX)
417
BacktoConceptualDesign
• NowthatweknowhowtofindFDs,it’sastraight-forwardprocess:
- Searchfor“bad”FDs
- Ifthereareany,thenkeepdecomposingthetableintosub-tablesuntilnomorebadFDs
- Whendone,thedatabaseschemaisnormalized
Recall:thereareseveralnormalforms…
418
Boyce-CoddNormalForm(BCNF)
• Mainideaisthatwedefine“good”and“bad”FDsasfollows:
- Xà Aisa“goodFD”ifXisa(super)key• Inotherwords,ifAisthesetofallattributes
- Xà Aisa“badFD”otherwise
• Wewilltrytoeliminatethe“bad”FDs!
419
Boyce-CoddNormalForm(BCNF)
• Whydoesthisdefinitionof“good”and“bad”FDsmakesense?
• IfXisnota(super)key,itfunctionallydeterminessomeoftheattributes;therefore,thoseotherattributescanbeduplicated
- Recall:thismeansthereisredundancy- Andredundancylikethiscanleadtodataanomalies!
EmpID Name Phone Position
E0045 Smith 1234 Clerk
E3542 Mike 9876 Salesrep
E1111 Smith 9876 Salesrep
E9999 Mary 1234 Lawyer
420
Boyce-CoddNormalForm
BCNFisasimpleconditionforremovinganomaliesfromrelations:
Inotherwords:thereareno“bad”FDs
ArelationRisinBCNF if:
if{A1,...,An}à B isanon-trivial FDinR
then{A1,...,An}isasuperkey forR
Equivalently: ∀ setsofattributesX,either(X+ =X)or(X+ =allattributes)
421
Example
Whatisthekey?{SSN,PhoneNumber}
Name SSN PhoneNumber CityFred 123-45-6789 206-555-1234 SeattleFred 123-45-6789 206-555-6543 SeattleJoe 987-65-4321 908-555-2121 WestfieldJoe 987-65-4321 908-555-1234 Westfield
{SSN} à {Name,City}
⟹Not inBCNF
ThisFDisbadbecauseitisnot asuperkey
422
Example
Name SSN CityFred 123-45-6789 SeattleJoe 987-65-4321 Madison
SSN PhoneNumber123-45-6789 206-555-1234123-45-6789 206-555-6543987-65-4321 908-555-2121987-65-4321 908-555-1234
Let’scheckanomalies:• Redundancy?• Update?• Delete?
{SSN} à {Name,City}
NowinBCNF!
ThisFDisnowgoodbecauseitisthekey
423
BCNFDecompositionAlgorithm
BCNFDecomp(R):FindXs.t.:X+ ≠XandX+≠[allattributes]
if (notfound)then Return R
let Y=X+ - X,Z=(X+)Cdecompose R intoR1(XÈ Y)andR2(XÈ Z)
Return BCNFDecomp(R1),BCNFDecomp(R2)
424
BCNFDecompositionAlgorithm
BCNFDecomp(R):Findasetofattributes Xs.t.:X+ ≠XandX+≠
[allattributes]
if (notfound)then Return R
let Y=X+ - X,Z=(X+)Cdecompose R intoR1(XÈ Y)andR2(XÈ Z)
Return BCNFDecomp(R1),BCNFDecomp(R2)
FindasetofattributesXwhichhasnon-trivial“bad”FDs,i.e.isnotasuperkey,usingclosures
425
BCNFDecompositionAlgorithm
BCNFDecomp(R):Finda setofattributesXs.t.:X+ ≠XandX+≠
[allattributes]
if (notfound)then Return R
let Y=X+ - X,Z=(X+)Cdecompose R intoR1(XÈ Y)andR2(XÈ Z)
Return BCNFDecomp(R1),BCNFDecomp(R2)
Ifno“bad”FDsfound,inBCNF!
426
BCNFDecompositionAlgorithm
BCNFDecomp(R):FindasetofattributesXs.t.:X+ ≠XandX+≠
[allattributes]
if (notfound)then Return R
let Y=X+ - X,Z=(X+)Cdecompose R intoR1(XÈ Y)andR2(XÈ Z)
Return BCNFDecomp(R1),BCNFDecomp(R2)
LetYbetheattributesthatXfunctionallydetermines(+thatarenotinX)
AndletZbethecomplement,theotherattributesthatitdoesn’t
427
BCNFDecompositionAlgorithm
BCNFDecomp(R):FindasetofattributesXs.t.:X+ ≠XandX+≠
[allattributes]
if (notfound)then Return R
let Y=X+ - X,Z=(X+)Cdecompose R intoR1(XÈ Y)andR2(XÈ Z)
Return BCNFDecomp(R1),BCNFDecomp(R2)
X ZY
R1 R2
Splitintoonerelation(table)withXplustheattributesthatXdetermines(Y)…
428
BCNFDecompositionAlgorithm
BCNFDecomp(R):FindasetofattributesXs.t.:X+ ≠XandX+≠
[allattributes]
if (notfound)then Return R
let Y=X+ - X,Z=(X+)Cdecompose R intoR1(XÈ Y)andR2(XÈ Z)
Return BCNFDecomp(R1),BCNFDecomp(R2)
X ZY
R1 R2
AndonerelationwithXplustheattributesitdoesnotdetermine(Z)
429
BCNFDecompositionAlgorithm
BCNFDecomp(R):FindasetofattributesXs.t.:X+ ≠XandX+≠
[allattributes]
if (notfound)then Return R
let Y=X+ - X,Z=(X+)Cdecompose R intoR1(XÈ Y)andR2(XÈ Z)
Return BCNFDecomp(R1),BCNFDecomp(R2)
Proceedrecursivelyuntilnomore“bad”FDs!
430
R(A,B,C,D,E)BCNFDecomp(R):FindasetofattributesXs.t.:X+ ≠XandX+≠
[allattributes]
if (notfound)then Return R
let Y=X+ - X,Z=(X+)Cdecompose R intoR1(XÈ Y)andR2(XÈ Z)
Return BCNFDecomp(R1),BCNFDecomp(R2)
Example
{A} à {B,C}{C} à {D}
431
Example
R(A,B,C,D,E){A}+ ={A,B,C,D}≠{A,B,C,D,E}
R1(A,B,C,D){C}+ ={C,D}≠{A,B,C,D}
R2(A,E)R11(C,D) R12(A,B,C)
R(A,B,C,D,E)
{A} à {B,C}{C} à {D}
432
Practice
• Activity-22.ipynb