DB - ER - 180301b · 2018. 3. 3. · 82453 Green Sally 391-1689 AL TRE141 14987 10/05/10 3 $100...

66
367 L14: Normalization CS3200 Database design (sp18 s2) https://course.ccs.neu.edu/cs3200sp18s2/ 3/1/2018

Transcript of DB - ER - 180301b · 2018. 3. 3. · 82453 Green Sally 391-1689 AL TRE141 14987 10/05/10 3 $100...

Page 1: DB - ER - 180301b · 2018. 3. 3. · 82453 Green Sally 391-1689 AL TRE141 14987 10/05/10 3 $100 82453 Green Sally 391-1689 AL TRE141 16293 11/18/10 1 $15 82453 Green Sally 391-1689

367

L14:Normalization

CS3200 Databasedesign(sp18 s2)https://course.ccs.neu.edu/cs3200sp18s2/3/1/2018

Page 2: DB - ER - 180301b · 2018. 3. 3. · 82453 Green Sally 391-1689 AL TRE141 14987 10/05/10 3 $100 82453 Green Sally 391-1689 AL TRE141 16293 11/18/10 1 $15 82453 Green Sally 391-1689

368

Announcements!

• KeepbringingyournameplatesJ• Outlinetoday- MoreNormalization- Project1discussion&TransactionsafterSpringBreak

Page 3: DB - ER - 180301b · 2018. 3. 3. · 82453 Green Sally 391-1689 AL TRE141 14987 10/05/10 3 $100 82453 Green Sally 391-1689 AL TRE141 16293 11/18/10 1 $15 82453 Green Sally 391-1689

369

Closures, Superkeys,and (Candidate) Keys

Page 4: DB - ER - 180301b · 2018. 3. 3. · 82453 Green Sally 391-1689 AL TRE141 14987 10/05/10 3 $100 82453 Green Sally 391-1689 AL TRE141 16293 11/18/10 1 $15 82453 Green Sally 391-1689

370

Whatwewillseenext

• ClosuresPart2

• Superkeys &Keys

• Practice:Thekeyorakey?

Page 5: DB - ER - 180301b · 2018. 3. 3. · 82453 Green Sally 391-1689 AL TRE141 14987 10/05/10 3 $100 82453 Green Sally 391-1689 AL TRE141 16293 11/18/10 1 $15 82453 Green Sally 391-1689

371

WhyDoWeNeedtheClosure?

• WithclosurewecanfindallFD’seasily

• TocheckifX® A

- ComputeX+

- CheckifAÎ X+

NoteherethatX isaset ofattributes,butA isasingleattribute.WhydoesconsideringFDsofthisformsuffice?

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

Page 6: DB - ER - 180301b · 2018. 3. 3. · 82453 Green Sally 391-1689 AL TRE141 14987 10/05/10 3 $100 82453 Green Sally 391-1689 AL TRE141 16293 11/18/10 1 $15 82453 Green Sally 391-1689

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?

Page 7: DB - ER - 180301b · 2018. 3. 3. · 82453 Green Sally 391-1689 AL TRE141 14987 10/05/10 3 $100 82453 Green Sally 391-1689 AL TRE141 16293 11/18/10 1 $15 82453 Green Sally 391-1689

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}

Page 8: DB - ER - 180301b · 2018. 3. 3. · 82453 Green Sally 391-1689 AL TRE141 14987 10/05/10 3 $100 82453 Green Sally 391-1689 AL TRE141 16293 11/18/10 1 $15 82453 Green Sally 391-1689

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:

Page 9: DB - ER - 180301b · 2018. 3. 3. · 82453 Green Sally 391-1689 AL TRE141 14987 10/05/10 3 $100 82453 Green Sally 391-1689 AL TRE141 16293 11/18/10 1 $15 82453 Green Sally 391-1689

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:

Page 10: DB - ER - 180301b · 2018. 3. 3. · 82453 Green Sally 391-1689 AL TRE141 14987 10/05/10 3 $100 82453 Green Sally 391-1689 AL TRE141 16293 11/18/10 1 $15 82453 Green Sally 391-1689

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)

Page 11: DB - ER - 180301b · 2018. 3. 3. · 82453 Green Sally 391-1689 AL TRE141 14987 10/05/10 3 $100 82453 Green Sally 391-1689 AL TRE141 16293 11/18/10 1 $15 82453 Green Sally 391-1689

377

FindingKeysandSuperkeys

• ForeachsetofattributesX

- ComputeX+

- IfX+ =setofallattributesthenXisasuperkey

- IfXisminimal,thenitisakey

Page 12: DB - ER - 180301b · 2018. 3. 3. · 82453 Green Sally 391-1689 AL TRE141 14987 10/05/10 3 $100 82453 Green Sally 391-1689 AL TRE141 16293 11/18/10 1 $15 82453 Green Sally 391-1689

378

ExampleofFindingKeys

Product(name, price, category, color)

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

Whatisakey?

Page 13: DB - ER - 180301b · 2018. 3. 3. · 82453 Green Sally 391-1689 AL TRE141 14987 10/05/10 3 $100 82453 Green Sally 391-1689 AL TRE141 16293 11/18/10 1 $15 82453 Green Sally 391-1689

379

ExampleofFindingKeys

Product(name, price, category, color)

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

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

Page 14: DB - ER - 180301b · 2018. 3. 3. · 82453 Green Sally 391-1689 AL TRE141 14987 10/05/10 3 $100 82453 Green Sally 391-1689 AL TRE141 16293 11/18/10 1 $15 82453 Green Sally 391-1689

380

Practice

• Activity-21.ipynb

Page 15: DB - ER - 180301b · 2018. 3. 3. · 82453 Green Sally 391-1689 AL TRE141 14987 10/05/10 3 $100 82453 Green Sally 391-1689 AL TRE141 16293 11/18/10 1 $15 82453 Green Sally 391-1689

381

CompleteNormalization Practice!

Page 16: DB - ER - 180301b · 2018. 3. 3. · 82453 Green Sally 391-1689 AL TRE141 14987 10/05/10 3 $100 82453 Green Sally 391-1689 AL TRE141 16293 11/18/10 1 $15 82453 Green Sally 391-1689

382

ParkingTickets:OriginalList

Page 17: DB - ER - 180301b · 2018. 3. 3. · 82453 Green Sally 391-1689 AL TRE141 14987 10/05/10 3 $100 82453 Green Sally 391-1689 AL TRE141 16293 11/18/10 1 $15 82453 Green Sally 391-1689

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

Page 18: DB - ER - 180301b · 2018. 3. 3. · 82453 Green Sally 391-1689 AL TRE141 14987 10/05/10 3 $100 82453 Green Sally 391-1689 AL TRE141 16293 11/18/10 1 $15 82453 Green Sally 391-1689

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

Page 19: DB - ER - 180301b · 2018. 3. 3. · 82453 Green Sally 391-1689 AL TRE141 14987 10/05/10 3 $100 82453 Green Sally 391-1689 AL TRE141 16293 11/18/10 1 $15 82453 Green Sally 391-1689

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

Page 20: DB - ER - 180301b · 2018. 3. 3. · 82453 Green Sally 391-1689 AL TRE141 14987 10/05/10 3 $100 82453 Green Sally 391-1689 AL TRE141 16293 11/18/10 1 $15 82453 Green Sally 391-1689

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

Page 21: DB - ER - 180301b · 2018. 3. 3. · 82453 Green Sally 391-1689 AL TRE141 14987 10/05/10 3 $100 82453 Green Sally 391-1689 AL TRE141 16293 11/18/10 1 $15 82453 Green Sally 391-1689

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

Page 22: DB - ER - 180301b · 2018. 3. 3. · 82453 Green Sally 391-1689 AL TRE141 14987 10/05/10 3 $100 82453 Green Sally 391-1689 AL TRE141 16293 11/18/10 1 $15 82453 Green Sally 391-1689

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

Page 23: DB - ER - 180301b · 2018. 3. 3. · 82453 Green Sally 391-1689 AL TRE141 14987 10/05/10 3 $100 82453 Green Sally 391-1689 AL TRE141 16293 11/18/10 1 $15 82453 Green Sally 391-1689

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

Page 24: DB - ER - 180301b · 2018. 3. 3. · 82453 Green Sally 391-1689 AL TRE141 14987 10/05/10 3 $100 82453 Green Sally 391-1689 AL TRE141 16293 11/18/10 1 $15 82453 Green Sally 391-1689

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

Page 25: DB - ER - 180301b · 2018. 3. 3. · 82453 Green Sally 391-1689 AL TRE141 14987 10/05/10 3 $100 82453 Green Sally 391-1689 AL TRE141 16293 11/18/10 1 $15 82453 Green Sally 391-1689

391

ParkingTickets:Relationsin3NF

STID LName FName PhoneNo StLic LicNo Ticketnr Date Code Fine

Assume,eachstudentcanhavemaximalonecar:

Page 26: DB - ER - 180301b · 2018. 3. 3. · 82453 Green Sally 391-1689 AL TRE141 14987 10/05/10 3 $100 82453 Green Sally 391-1689 AL TRE141 16293 11/18/10 1 $15 82453 Green Sally 391-1689

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

Page 27: DB - ER - 180301b · 2018. 3. 3. · 82453 Green Sally 391-1689 AL TRE141 14987 10/05/10 3 $100 82453 Green Sally 391-1689 AL TRE141 16293 11/18/10 1 $15 82453 Green Sally 391-1689

393

ParkingTickets:Relationsin3NF

STID LName FName PhoneNo StLic LicNo Ticketnr Date Code Fine

Nextassume,studentscanhavemorethanonecar:

Page 28: DB - ER - 180301b · 2018. 3. 3. · 82453 Green Sally 391-1689 AL TRE141 14987 10/05/10 3 $100 82453 Green Sally 391-1689 AL TRE141 16293 11/18/10 1 $15 82453 Green Sally 391-1689

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

Page 29: DB - ER - 180301b · 2018. 3. 3. · 82453 Green Sally 391-1689 AL TRE141 14987 10/05/10 3 $100 82453 Green Sally 391-1689 AL TRE141 16293 11/18/10 1 $15 82453 Green Sally 391-1689

395

ParkingTickets:AddingViolationAssume,eachstudentcanhavemaximalonecar:

STID LName FName PhoneNo StLic LicNo

Code Fine

Ticketnr Date Code@ STID@

Student

TicketCode

Ticket

Page 30: DB - ER - 180301b · 2018. 3. 3. · 82453 Green Sally 391-1689 AL TRE141 14987 10/05/10 3 $100 82453 Green Sally 391-1689 AL TRE141 16293 11/18/10 1 $15 82453 Green Sally 391-1689

396

ParkingTickets:AddingViolationAssume,eachstudentcanhavemaximalonecar:

STID LName FName PhoneNo StLic LicNo

Code Fine

Ticketnr Date Code@ STID@

Student

TicketCode

Ticket

Violation

Page 31: DB - ER - 180301b · 2018. 3. 3. · 82453 Green Sally 391-1689 AL TRE141 14987 10/05/10 3 $100 82453 Green Sally 391-1689 AL TRE141 16293 11/18/10 1 $15 82453 Green Sally 391-1689

397

ParkingTickets:ERDiagram

STID LName FName PhoneNo StLic LicNo

Code Fine

Ticketnr Date Code@ STID@

Student

TicketCode

Ticket

Violation

ERdiagramsdonotshowforeignkeys!

Page 32: DB - ER - 180301b · 2018. 3. 3. · 82453 Green Sally 391-1689 AL TRE141 14987 10/05/10 3 $100 82453 Green Sally 391-1689 AL TRE141 16293 11/18/10 1 $15 82453 Green Sally 391-1689

398

CompleteNormalization Practice!

Page 33: DB - ER - 180301b · 2018. 3. 3. · 82453 Green Sally 391-1689 AL TRE141 14987 10/05/10 3 $100 82453 Green Sally 391-1689 AL TRE141 16293 11/18/10 1 $15 82453 Green Sally 391-1689

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?

Page 34: DB - ER - 180301b · 2018. 3. 3. · 82453 Green Sally 391-1689 AL TRE141 14987 10/05/10 3 $100 82453 Green Sally 391-1689 AL TRE141 16293 11/18/10 1 $15 82453 Green Sally 391-1689

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

Page 35: DB - ER - 180301b · 2018. 3. 3. · 82453 Green Sally 391-1689 AL TRE141 14987 10/05/10 3 $100 82453 Green Sally 391-1689 AL TRE141 16293 11/18/10 1 $15 82453 Green Sally 391-1689

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

Page 36: DB - ER - 180301b · 2018. 3. 3. · 82453 Green Sally 391-1689 AL TRE141 14987 10/05/10 3 $100 82453 Green Sally 391-1689 AL TRE141 16293 11/18/10 1 $15 82453 Green Sally 391-1689

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

Page 37: DB - ER - 180301b · 2018. 3. 3. · 82453 Green Sally 391-1689 AL TRE141 14987 10/05/10 3 $100 82453 Green Sally 391-1689 AL TRE141 16293 11/18/10 1 $15 82453 Green Sally 391-1689

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

Page 38: DB - ER - 180301b · 2018. 3. 3. · 82453 Green Sally 391-1689 AL TRE141 14987 10/05/10 3 $100 82453 Green Sally 391-1689 AL TRE141 16293 11/18/10 1 $15 82453 Green Sally 391-1689

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

Page 39: DB - ER - 180301b · 2018. 3. 3. · 82453 Green Sally 391-1689 AL TRE141 14987 10/05/10 3 $100 82453 Green Sally 391-1689 AL TRE141 16293 11/18/10 1 $15 82453 Green Sally 391-1689

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)

Page 40: DB - ER - 180301b · 2018. 3. 3. · 82453 Green Sally 391-1689 AL TRE141 14987 10/05/10 3 $100 82453 Green Sally 391-1689 AL TRE141 16293 11/18/10 1 $15 82453 Green Sally 391-1689

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)

Page 41: DB - ER - 180301b · 2018. 3. 3. · 82453 Green Sally 391-1689 AL TRE141 14987 10/05/10 3 $100 82453 Green Sally 391-1689 AL TRE141 16293 11/18/10 1 $15 82453 Green Sally 391-1689

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)

Page 42: DB - ER - 180301b · 2018. 3. 3. · 82453 Green Sally 391-1689 AL TRE141 14987 10/05/10 3 $100 82453 Green Sally 391-1689 AL TRE141 16293 11/18/10 1 $15 82453 Green Sally 391-1689

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?

Page 43: DB - ER - 180301b · 2018. 3. 3. · 82453 Green Sally 391-1689 AL TRE141 14987 10/05/10 3 $100 82453 Green Sally 391-1689 AL TRE141 16293 11/18/10 1 $15 82453 Green Sally 391-1689

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

Page 44: DB - ER - 180301b · 2018. 3. 3. · 82453 Green Sally 391-1689 AL TRE141 14987 10/05/10 3 $100 82453 Green Sally 391-1689 AL TRE141 16293 11/18/10 1 $15 82453 Green Sally 391-1689

410

Boyce-Codd Normal Form

Page 45: DB - ER - 180301b · 2018. 3. 3. · 82453 Green Sally 391-1689 AL TRE141 14987 10/05/10 3 $100 82453 Green Sally 391-1689 AL TRE141 16293 11/18/10 1 $15 82453 Green Sally 391-1689

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

Page 46: DB - ER - 180301b · 2018. 3. 3. · 82453 Green Sally 391-1689 AL TRE141 14987 10/05/10 3 $100 82453 Green Sally 391-1689 AL TRE141 16293 11/18/10 1 $15 82453 Green Sally 391-1689

412

Boyce-CoddNormalForm(BCNF)

• Boyce-Coddnormalform(BCNF)- ArelationisinBCNF,ifandonlyif,everydeterminantisacandidatekey.

• Thedifferencebetween3NFandBCNFisthatforafunctionaldependencyAàB,- 3NFallowsthisdependencyinarelationifBisaprimary-keyattribute andAisnotacandidatekey,

- whereasBCNFinsiststhatforthisdependencytoremaininarelation,Amustbeacandidatekey.

Page 47: DB - ER - 180301b · 2018. 3. 3. · 82453 Green Sally 391-1689 AL TRE141 14987 10/05/10 3 $100 82453 Green Sally 391-1689 AL TRE141 16293 11/18/10 1 $15 82453 Green Sally 391-1689

413

3NFtoBCNF

Source:Hoffer,Ramesh,Topi,Moderndatabasemanagement,10th ed,AppendixB,2010.

Page 48: DB - ER - 180301b · 2018. 3. 3. · 82453 Green Sally 391-1689 AL TRE141 14987 10/05/10 3 $100 82453 Green Sally 391-1689 AL TRE141 16293 11/18/10 1 $15 82453 Green Sally 391-1689

414

3NFtoBCNF

Page 49: DB - ER - 180301b · 2018. 3. 3. · 82453 Green Sally 391-1689 AL TRE141 14987 10/05/10 3 $100 82453 Green Sally 391-1689 AL TRE141 16293 11/18/10 1 $15 82453 Green Sally 391-1689

415

3NFtoBCNF

Source:Hoffer,Ramesh,Topi,Moderndatabasemanagement,10th ed,AppendixB,2010.

Page 50: DB - ER - 180301b · 2018. 3. 3. · 82453 Green Sally 391-1689 AL TRE141 14987 10/05/10 3 $100 82453 Green Sally 391-1689 AL TRE141 16293 11/18/10 1 $15 82453 Green Sally 391-1689

416

BCNFvs3NF• BCNF:ForeveryfunctionaldependencyX->YinasetF offunctionaldependenciesoverrelationR,either:- XisasuperkeyofR- (orYisasubsetofX)

• 3NF:ForeveryfunctionaldependencyX->YinasetFoffunctionaldependenciesoverrelationR,either:- XisasuperkeyofR,or- YisasubsetofKforsomekeyKofR

• N.b., nosubsetofakeyisakey- (orYisasubsetofX)

Page 51: DB - ER - 180301b · 2018. 3. 3. · 82453 Green Sally 391-1689 AL TRE141 14987 10/05/10 3 $100 82453 Green Sally 391-1689 AL TRE141 16293 11/18/10 1 $15 82453 Green Sally 391-1689

417

BacktoConceptualDesign

• NowthatweknowhowtofindFDs,it’sastraight-forwardprocess:

- Searchfor“bad”FDs

- Ifthereareany,thenkeepdecomposingthetableintosub-tablesuntilnomorebadFDs

- Whendone,thedatabaseschemaisnormalized

Recall:thereareseveralnormalforms…

Page 52: DB - ER - 180301b · 2018. 3. 3. · 82453 Green Sally 391-1689 AL TRE141 14987 10/05/10 3 $100 82453 Green Sally 391-1689 AL TRE141 16293 11/18/10 1 $15 82453 Green Sally 391-1689

418

Boyce-CoddNormalForm(BCNF)

• Mainideaisthatwedefine“good”and“bad”FDsasfollows:

- Xà Aisa“goodFD”ifXisa(super)key• Inotherwords,ifAisthesetofallattributes

- Xà Aisa“badFD”otherwise

• Wewilltrytoeliminatethe“bad”FDs!

Page 53: DB - ER - 180301b · 2018. 3. 3. · 82453 Green Sally 391-1689 AL TRE141 14987 10/05/10 3 $100 82453 Green Sally 391-1689 AL TRE141 16293 11/18/10 1 $15 82453 Green Sally 391-1689

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

Page 54: DB - ER - 180301b · 2018. 3. 3. · 82453 Green Sally 391-1689 AL TRE141 14987 10/05/10 3 $100 82453 Green Sally 391-1689 AL TRE141 16293 11/18/10 1 $15 82453 Green Sally 391-1689

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)

Page 55: DB - ER - 180301b · 2018. 3. 3. · 82453 Green Sally 391-1689 AL TRE141 14987 10/05/10 3 $100 82453 Green Sally 391-1689 AL TRE141 16293 11/18/10 1 $15 82453 Green Sally 391-1689

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

Page 56: DB - ER - 180301b · 2018. 3. 3. · 82453 Green Sally 391-1689 AL TRE141 14987 10/05/10 3 $100 82453 Green Sally 391-1689 AL TRE141 16293 11/18/10 1 $15 82453 Green Sally 391-1689

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

Page 57: DB - ER - 180301b · 2018. 3. 3. · 82453 Green Sally 391-1689 AL TRE141 14987 10/05/10 3 $100 82453 Green Sally 391-1689 AL TRE141 16293 11/18/10 1 $15 82453 Green Sally 391-1689

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)

Page 58: DB - ER - 180301b · 2018. 3. 3. · 82453 Green Sally 391-1689 AL TRE141 14987 10/05/10 3 $100 82453 Green Sally 391-1689 AL TRE141 16293 11/18/10 1 $15 82453 Green Sally 391-1689

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

Page 59: DB - ER - 180301b · 2018. 3. 3. · 82453 Green Sally 391-1689 AL TRE141 14987 10/05/10 3 $100 82453 Green Sally 391-1689 AL TRE141 16293 11/18/10 1 $15 82453 Green Sally 391-1689

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!

Page 60: DB - ER - 180301b · 2018. 3. 3. · 82453 Green Sally 391-1689 AL TRE141 14987 10/05/10 3 $100 82453 Green Sally 391-1689 AL TRE141 16293 11/18/10 1 $15 82453 Green Sally 391-1689

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

Page 61: DB - ER - 180301b · 2018. 3. 3. · 82453 Green Sally 391-1689 AL TRE141 14987 10/05/10 3 $100 82453 Green Sally 391-1689 AL TRE141 16293 11/18/10 1 $15 82453 Green Sally 391-1689

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)…

Page 62: DB - ER - 180301b · 2018. 3. 3. · 82453 Green Sally 391-1689 AL TRE141 14987 10/05/10 3 $100 82453 Green Sally 391-1689 AL TRE141 16293 11/18/10 1 $15 82453 Green Sally 391-1689

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)

Page 63: DB - ER - 180301b · 2018. 3. 3. · 82453 Green Sally 391-1689 AL TRE141 14987 10/05/10 3 $100 82453 Green Sally 391-1689 AL TRE141 16293 11/18/10 1 $15 82453 Green Sally 391-1689

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!

Page 64: DB - ER - 180301b · 2018. 3. 3. · 82453 Green Sally 391-1689 AL TRE141 14987 10/05/10 3 $100 82453 Green Sally 391-1689 AL TRE141 16293 11/18/10 1 $15 82453 Green Sally 391-1689

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}

Page 65: DB - ER - 180301b · 2018. 3. 3. · 82453 Green Sally 391-1689 AL TRE141 14987 10/05/10 3 $100 82453 Green Sally 391-1689 AL TRE141 16293 11/18/10 1 $15 82453 Green Sally 391-1689

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}

Page 66: DB - ER - 180301b · 2018. 3. 3. · 82453 Green Sally 391-1689 AL TRE141 14987 10/05/10 3 $100 82453 Green Sally 391-1689 AL TRE141 16293 11/18/10 1 $15 82453 Green Sally 391-1689

432

Practice

• Activity-22.ipynb