Anda di halaman 1dari 87

Chapter7:RelationalDatabaseDesign

DatabaseSystemConcepts,5thEd.
Silberschatz,KorthandSudarshan Seewww.dbbook.comforconditionsonreuse

Chapter7:RelationalDatabaseDesign
s FeaturesofGoodRelationalDesign s AtomicDomainsandFirstNormalForm s DecompositionUsingFunctionalDependencies s FunctionalDependencyTheory s AlgorithmsforFunctionalDependencies s DecompositionUsingMultivaluedDependencies s MoreNormalForm s DatabaseDesignProcess s ModelingTemporalData

DatabaseSystemConcepts5thEdition,July28,2005.

7.<number>

Silberschatz,KorthandSudarshan

TheBankingSchema
s s s s s s s s s s s s s s s

branch=(branch_name,branch_city,assets) customer=(customer_id,customer_name,customer_street,customer_city) loan=(loan_number,amount) account=(account_number,balance) employee=(employee_id.employee_name,telephone_number,start_date) dependent_name=(employee_id,dname) account_branch=(account_number,branch_name) loan_branch=(loan_number,branch_name) borrower=(customer_id,loan_number) depositor=(customer_id,account_number) cust_banker=(customer_id,employee_id,type) works_for=(worker_employee_id,manager_employee_id) payment=(loan_number,payment_number,payment_date,payment_amount) savings_account=(account_number,interest_rate) checking_account=(account_number,overdraft_amount)

DatabaseSystemConcepts5thEdition,July28,2005.

7.<number>

Silberschatz,KorthandSudarshan

CombineSchemas?
s Supposewecombineborrowerandloantoget

bor_loan=(customer_id,loan_number,amount)
s Resultispossiblerepetitionofinformation(L100inexamplebelow)

DatabaseSystemConcepts5thEdition,July28,2005.

7.<number>

Silberschatz,KorthandSudarshan

ACombinedSchemaWithoutRepetition
s Considercombiningloan_branchandloan

loan_amt_br=(loan_number,amount,branch_name)
s Norepetition(assuggestedbyexamplebelow)

DatabaseSystemConcepts5thEdition,July28,2005.

7.<number>

Silberschatz,KorthandSudarshan

WhatAboutSmallerSchemas?
s s s

Supposewehadstartedwithbor_loan.Howwouldweknowtosplitup (decompose)itintoborrowerandloan? Writearuleiftherewereaschema(loan_number,amount),thenloan_number wouldbeacandidatekey Denoteasafunctionaldependency: loan_numberamount Inbor_loan,becauseloan_numberisnotacandidatekey,theamountofaloan mayhavetoberepeated.Thisindicatestheneedtodecomposebor_loan. Notalldecompositionsaregood.Supposewedecomposeemployeeinto employee1=(employee_id,employee_name) employee2=(employee_name,telephone_number,start_date)

s s

Thenextslideshowshowweloseinformationwecannotreconstructthe originalemployeerelationandso,thisisalossydecomposition.

DatabaseSystemConcepts5thEdition,July28,2005.

7.<number>

Silberschatz,KorthandSudarshan

ALossyDecomposition

DatabaseSystemConcepts5thEdition,July28,2005.

7.<number>

Silberschatz,KorthandSudarshan

FirstNormalForm
s Domainisatomicifitselementsareconsideredtobeindivisibleunits
q

Examplesofnonatomicdomains:

Setofnames,compositeattributes IdentificationnumberslikeCS101thatcanbebrokenupinto parts

s ArelationalschemaRisinfirstnormalformifthedomainsofall

attributesofRareatomic

s Nonatomicvaluescomplicatestorageandencourageredundant

(repeated)storageofdata
q

Example:Setofaccountsstoredwitheachcustomer,andsetof ownersstoredwitheachaccount Weassumeallrelationsareinfirstnormalform(andrevisitthisin Chapter9)

DatabaseSystemConcepts5thEdition,July28,2005.

7.<number>

Silberschatz,KorthandSudarshan

FirstNormalForm(Contd)
s Atomicityisactuallyapropertyofhowtheelementsofthedomainare

used.
q q

Example:Stringswouldnormallybeconsideredindivisible Supposethatstudentsaregivenrollnumberswhicharestringsof theformCS0012orEE1127 Ifthefirsttwocharactersareextractedtofindthedepartment,the domainofrollnumbersisnotatomic. Doingsoisabadidea:leadstoencodingofinformationin applicationprogramratherthaninthedatabase.

DatabaseSystemConcepts5thEdition,July28,2005.

7.<number>

Silberschatz,KorthandSudarshan

GoalDeviseaTheoryfortheFollowing
s DecidewhetheraparticularrelationRisingoodform. s InthecasethatarelationRisnotingoodform,decomposeitintoa

setofrelations{R1,R2,...,Rn}suchthat
q q

eachrelationisingoodform thedecompositionisalosslessjoindecomposition functionaldependencies multivalueddependencies

s Ourtheoryisbasedon:
q q

DatabaseSystemConcepts5thEdition,July28,2005.

7.<number>

Silberschatz,KorthandSudarshan

FunctionalDependencies
s Constraintsonthesetoflegalrelations. s Requirethatthevalueforacertainsetofattributesdetermines

uniquelythevalueforanothersetofattributes.

s Afunctionaldependencyisageneralizationofthenotionofakey.

DatabaseSystemConcepts5thEdition,July28,2005.

7.<number>

Silberschatz,KorthandSudarshan

FunctionalDependencies(Cont.)
s LetRbearelationschema

RandR
s Thefunctionaldependency

holdsonRifandonlyifforanylegalrelationsr(R),wheneverany twotuplest1andt2ofragreeontheattributes,theyalsoagree ontheattributes.Thatis, t1[]=t2[]t1[]=t2[]


s Example:Considerr(A,B)withthefollowinginstanceofr.

1 4 15 3 7

s Onthisinstance,ABdoesNOThold,butBAdoeshold.

DatabaseSystemConcepts5thEdition,July28,2005.

7.<number>

Silberschatz,KorthandSudarshan

FunctionalDependencies(Cont.)
s KisasuperkeyforrelationschemaRifandonlyifKR s KisacandidatekeyforRifandonlyif
q q

KR,and fornoK,R

s Functionaldependenciesallowustoexpressconstraintsthatcannot

beexpressedusingsuperkeys.Considertheschema: Weexpectthisfunctionaldependencytohold: loan_numberamount butwouldnotexpectthefollowingtohold: amountcustomer_name

bor_loan=(customer_id,loan_number,amount).

DatabaseSystemConcepts5thEdition,July28,2005.

7.<number>

Silberschatz,KorthandSudarshan

UseofFunctionalDependencies
s Weusefunctionaldependenciesto:
q

testrelationstoseeiftheyarelegalunderagivensetoffunctional dependencies.

IfarelationrislegalunderasetFoffunctionaldependencies,we saythatrsatisfiesF. WesaythatFholdsonRifalllegalrelationsonRsatisfythesetof functionaldependenciesF.

specifyconstraintsonthesetoflegalrelations

s Note:Aspecificinstanceofarelationschemamaysatisfyafunctional

dependencyevenifthefunctionaldependencydoesnotholdonalllegal instances.
q

Forexample,aspecificinstanceofloanmay,bychance,satisfy amountcustomer_name.

DatabaseSystemConcepts5thEdition,July28,2005.

7.<number>

Silberschatz,KorthandSudarshan

FunctionalDependencies(Cont.)
s Afunctionaldependencyistrivialifitissatisfiedbyallinstancesofa

relation
q

Example: customer_name,loan_numbercustomer_name customer_namecustomer_name

Ingeneral,istrivialif

DatabaseSystemConcepts5thEdition,July28,2005.

7.<number>

Silberschatz,KorthandSudarshan

ClosureofaSetofFunctional Dependencies
s GivenasetFoffunctionaldependencies,therearecertainother

functionaldependenciesthatarelogicallyimpliedbyF.
q

Forexample:IfABandBC,thenwecaninferthatAC

s ThesetofallfunctionaldependencieslogicallyimpliedbyFistheclosure

ofF.

s WedenotetheclosureofFbyF+. s F+isasupersetofF.

DatabaseSystemConcepts5thEdition,July28,2005.

7.<number>

Silberschatz,KorthandSudarshan

BoyceCoddNormalForm
ArelationschemaRisinBCNFwithrespecttoasetFof functionaldependenciesifforallfunctionaldependenciesinF+of theform whereRandR,atleastoneofthefollowingholds:
s istrivial(i.e.,) s isasuperkeyforR
ExampleschemanotinBCNF: bor_loan=(customer_id,loan_number,amount) becauseloan_numberamountholdsonbor_loanbutloan_numberis

notasuperkey

DatabaseSystemConcepts5thEdition,July28,2005.

7.<number>

Silberschatz,KorthandSudarshan

DecomposingaSchemaintoBCNF
s SupposewehaveaschemaRandanontrivialdependency

causesaviolationofBCNF. WedecomposeRinto: ( U) (R())

s Inourexample,
q q

=loan_number

=amount

andbor_loanisreplacedby q ( U)=(loan_number,amount) q (R())=(customer_id,loan_number)

DatabaseSystemConcepts5thEdition,July28,2005.

7.<number>

Silberschatz,KorthandSudarshan

BCNFandDependencyPreservation
s Constraints,includingfunctionaldependencies,arecostlytocheckin

practiceunlesstheypertaintoonlyonerelation

s Ifitissufficienttotestonlythosedependenciesoneachindividual

relationofadecompositioninordertoensurethatallfunctional dependencieshold,thenthatdecompositionisdependency preserving.

s BecauseitisnotalwayspossibletoachievebothBCNFand

dependencypreservation,weconsideraweakernormalform,known asthirdnormalform.

DatabaseSystemConcepts5thEdition,July28,2005.

7.<number>

Silberschatz,KorthandSudarshan

ThirdNormalForm
s ArelationschemaRisinthirdnormalform(3NF)ifforall:

inF+ atleastoneofthefollowingholds:
q q q

istrivial(i.e.,) isasuperkeyforR EachattributeAiniscontainedinacandidatekeyforR.

(NOTE:eachattributemaybeinadifferentcandidatekey)
s IfarelationisinBCNFitisin3NF(sinceinBCNFoneofthefirsttwo

conditionsabovemusthold).

s ThirdconditionisaminimalrelaxationofBCNFtoensuredependency

preservation(willseewhylater).

DatabaseSystemConcepts5thEdition,July28,2005.

7.<number>

Silberschatz,KorthandSudarshan

GoalsofNormalization
s LetRbearelationschemewithasetFoffunctional

dependencies.

s DecidewhetherarelationschemeRisingoodform. s InthecasethatarelationschemeRisnotingoodform,

decomposeitintoasetofrelationscheme{R1,R2,...,Rn}such that
q q q

eachrelationschemeisingoodform thedecompositionisalosslessjoindecomposition Preferably,thedecompositionshouldbedependency preserving.

DatabaseSystemConcepts5thEdition,July28,2005.

7.<number>

Silberschatz,KorthandSudarshan

HowgoodisBCNF?
s TherearedatabaseschemasinBCNFthatdonotseemtobe

sufficientlynormalized

s Consideradatabase

classes(course,teacher,book) suchthat(c,t,b)classesmeansthattisqualifiedtoteachc,andb isarequiredtextbookforc


s Thedatabaseissupposedtolistforeachcoursethesetofteachers

anyoneofwhichcanbethecoursesinstructor,andthesetofbooks, allofwhicharerequiredforthecourse(nomatterwhoteachesit).

DatabaseSystemConcepts5thEdition,July28,2005.

7.<number>

Silberschatz,KorthandSudarshan

HowgoodisBCNF?(Cont.)
course database database database database database database operatingsystems operatingsystems operatingsystems operatingsystems teacher Avi Avi Hank Hank Sudarshan Sudarshan Avi Avi Pete Pete classes
s Therearenonontrivialfunctionaldependenciesandthereforethe

book DBConcepts Ullman DBConcepts Ullman DBConcepts Ullman OSConcepts Stallings OSConcepts Stallings

relationisinBCNF

s Insertionanomaliesi.e.,ifMarilynisanewteacherthatcanteach

database,twotuplesneedtobeinserted

(database,Marilyn,DBConcepts) (database,Marilyn,Ullman)
DatabaseSystemConcepts5thEdition,July28,2005. 7.<number> Silberschatz,KorthandSudarshan

HowgoodisBCNF?(Cont.)
s Therefore,itisbettertodecomposeclassesinto:

course database database database operatingsystems operatingsystems course database database operatingsystems operatingsystems text

teacher Avi Hank Sudarshan Avi Jim book DBConcepts Ullman OSConcepts Shaw

teaches

Thissuggeststheneedforhighernormalforms,suchasFourth NormalForm(4NF),whichweshallseelater.
DatabaseSystemConcepts5thEdition,July28,2005. 7.<number> Silberschatz,KorthandSudarshan

FunctionalDependencyTheory
s Wenowconsidertheformaltheorythattellsuswhichfunctional

dependenciesareimpliedlogicallybyagivensetoffunctional dependencies. BCNFand3NF preserving

s Wethendevelopalgorithmstogeneratelosslessdecompositionsinto s Wethendevelopalgorithmstotestifadecompositionisdependency

DatabaseSystemConcepts5thEdition,July28,2005.

7.<number>

Silberschatz,KorthandSudarshan

ClosureofaSetofFunctional Dependencies
s GivenasetFsetoffunctionaldependencies,therearecertainother

functionaldependenciesthatarelogicallyimpliedbyF.
q

Forexample:IfABandBC,thenwecaninferthatAC

s ThesetofallfunctionaldependencieslogicallyimpliedbyFistheclosure

ofF.

s WedenotetheclosureofFbyF+. s WecanfindallofF+byapplyingArmstrongsAxioms:
q q q

if,then(reflexivity) if,then(augmentation) if,and,then(transitivity) sound(generateonlyfunctionaldependenciesthatactuallyhold)and complete(generateallfunctionaldependenciesthathold).

s Theserulesare
q q

DatabaseSystemConcepts5thEdition,July28,2005.

7.<number>

Silberschatz,KorthandSudarshan

Example
s R=(A,B,C,G,H,I)

F={AB AC CGH CGI BH}


q

s somemembersofF+

AH

bytransitivityfromABandBH byaugmentingACwithG,togetAGCG andthentransitivitywithCGI byaugmentingCGItoinferCGCGI,

AGI

CGHI

andaugmentingofCGHtoinferCGIHI, andthentransitivity
DatabaseSystemConcepts5thEdition,July28,2005. 7.<number> Silberschatz,KorthandSudarshan

ProcedureforComputingF+
s TocomputetheclosureofasetoffunctionaldependenciesF:

F+=F repeat foreachfunctionaldependencyfinF+ applyreflexivityandaugmentationrulesonf addtheresultingfunctionaldependenciestoF+ foreachpairoffunctionaldependenciesf1andf2inF+ iff1andf2canbecombinedusingtransitivity thenaddtheresultingfunctionaldependencytoF+ untilF+doesnotchangeanyfurther NOTE:Weshallseeanalternativeprocedureforthistasklater

DatabaseSystemConcepts5thEdition,July28,2005.

7.<number>

Silberschatz,KorthandSudarshan

ClosureofFunctionalDependencies (Cont.)
s WecanfurthersimplifymanualcomputationofF+byusingthe

followingadditionalrules.
q q

Ifholdsandholds,thenholds(union) Ifholds,thenholdsandholds (decomposition) Ifholdsandholds,thenholds (pseudotransitivity)

TheaboverulescanbeinferredfromArmstrongsaxioms.

DatabaseSystemConcepts5thEdition,July28,2005.

7.<number>

Silberschatz,KorthandSudarshan

ClosureofAttributeSets
s Givenasetofattributes,definetheclosureofunderF(denotedby

+)asthesetofattributesthatarefunctionallydeterminedbyunder F

s Algorithmtocompute+,theclosureofunderF

result:=; while(changestoresult)do foreachinFdo begin ifresultthenresult:=result end

DatabaseSystemConcepts5thEdition,July28,2005.

7.<number>

Silberschatz,KorthandSudarshan

ExampleofAttributeSetClosure
s R=(A,B,C,G,H,I) s F={AB

AC CGH CGI BH}

s (AG)+

1. result=AG 2. result=ABCG 3. result=ABCGH (ACandAB) (CGHandCGAGBC)

4. result=ABCGHI (CGIandCGAGBCH)
s IsAGacandidatekey?
1. 2.

IsAGasuperkey?
1.

DoesAGR?==Is(AG)+R DoesAR?==Is(A)+R DoesGR?==Is(G)+R


7.<number> Silberschatz,KorthandSudarshan

IsanysubsetofAGasuperkey?
1. 2.

DatabaseSystemConcepts5thEdition,July28,2005.

UsesofAttributeClosure
Thereareseveralusesoftheattributeclosurealgorithm:
s Testingforsuperkey:
q

Totestifisasuperkey,wecompute+,andcheckif+contains allattributesofR. Tocheckifafunctionaldependencyholds(or,inother words,isinF+),justcheckif+. Thatis,wecompute+byusingattributeclosure,andthencheck ifitcontains. Isasimpleandcheaptest,andveryuseful ForeachR,wefindtheclosure+,andforeachS+,we outputafunctionaldependencyS.

s Testingfunctionaldependencies
q

s ComputingclosureofF
q

DatabaseSystemConcepts5thEdition,July28,2005.

7.<number>

Silberschatz,KorthandSudarshan

CanonicalCover
s Setsoffunctionaldependenciesmayhaveredundantdependencies

thatcanbeinferredfromtheothers
q q

Forexample:ACisredundantin:{AB,BC} Partsofafunctionaldependencymayberedundant

E.g.:onRHS:{AB,BC,ACD}canbesimplified to {AB,BC,AD} E.g.:onLHS:{AB,BC,ACD}canbesimplified to {AB,BC,AD}

s Intuitively,acanonicalcoverofFisaminimalsetoffunctional

dependenciesequivalenttoF,havingnoredundantdependenciesor redundantpartsofdependencies

DatabaseSystemConcepts5thEdition,July28,2005.

7.<number>

Silberschatz,KorthandSudarshan

ExtraneousAttributes
s ConsiderasetFoffunctionaldependenciesandthefunctional

dependencyinF.
q

AttributeAisextraneousinifA andFlogicallyimplies(F{}){(A)}. AttributeAisextraneousinifA andthesetoffunctionaldependencies (F{}){(A)}logicallyimpliesF.

s Note:implicationintheoppositedirectionistrivialineachofthe

casesabove,sinceastrongerfunctionaldependencyalwaysimplies aweakerone
q

s Example:GivenF={AC,ABC}

BisextraneousinABCbecause{AC,ABC}logically impliesAC(I.e.theresultofdroppingBfromABC). CisextraneousinABCDsinceABCcanbeinferredeven afterdeletingC


7.<number> Silberschatz,KorthandSudarshan

s Example:GivenF={AC,ABCD}
q

DatabaseSystemConcepts5thEdition,July28,2005.

TestingifanAttributeisExtraneous
s s

ConsiderasetFoffunctionaldependenciesandthefunctional dependencyinF. TotestifattributeAisextraneousin


1. 2.

compute({}A)+usingthedependenciesinF checkthat({}A)+contains;ifitdoes,Aisextraneousin compute+usingonlythedependenciesin F=(F{}){(A)}, checkthat+containsA;ifitdoes,Aisextraneousin

TotestifattributeAisextraneousin
1. 2.

DatabaseSystemConcepts5thEdition,July28,2005.

7.<number>

Silberschatz,KorthandSudarshan

CanonicalCover
s AcanonicalcoverforFisasetofdependenciesFcsuchthat
q q q q

FlogicallyimpliesalldependenciesinFc,and FclogicallyimpliesalldependenciesinF,and NofunctionaldependencyinFccontainsanextraneousattribute,and EachleftsideoffunctionaldependencyinFcisunique.

s TocomputeacanonicalcoverforF:

repeat UsetheunionruletoreplaceanydependenciesinF 11and12with112 Findafunctionaldependencywithan extraneousattributeeitherinorin Ifanextraneousattributeisfound,deleteitfrom untilFdoesnotchange havebeendeleted,soithastobereapplied

s Note:Unionrulemaybecomeapplicableaftersomeextraneousattributes

DatabaseSystemConcepts5thEdition,July28,2005.

7.<number>

Silberschatz,KorthandSudarshan

ComputingaCanonicalCover
s

R=(A,B,C) F={ABC BC AB ABC} CombineABCandABintoABC


q

Setisnow{ABC,BC,ABC} CheckiftheresultofdeletingAfromABCisimpliedbytheother dependencies

AisextraneousinABC
q

Yes:infact,BCisalreadypresent!

Setisnow{ABC,BC} CheckifACislogicallyimpliedbyABandtheotherdependencies

CisextraneousinABC
q

Yes:usingtransitivityonABandBC. CanuseattributeclosureofAinmorecomplexcases AB BC
7.<number> Silberschatz,KorthandSudarshan

Thecanonicalcoveris:

DatabaseSystemConcepts5thEdition,July28,2005.

LosslessjoinDecomposition
s ForthecaseofR=(R1,R2),werequirethatforallpossible

relationsronschemaR

r=R1(r)R2(r)
s AdecompositionofRintoR1andR2islosslessjoinifand

onlyifatleastoneofthefollowingdependenciesisinF+:
q q

R1R2R1 R1R2R2

DatabaseSystemConcepts5thEdition,July28,2005.

7.<number>

Silberschatz,KorthandSudarshan

Example
s R=(A,B,C)
q

F={AB,BC) Canbedecomposedintwodifferentways Losslessjoindecomposition: R1R2={B}andBBC


q

s R1=(A,B),R2=(B,C)
q

Dependencypreserving Losslessjoindecomposition: R1R2={A}andAAB Notdependencypreserving (cannotcheckBCwithoutcomputingR1R2)

s R1=(A,B),R2=(A,C)
q

DatabaseSystemConcepts5thEdition,July28,2005.

7.<number>

Silberschatz,KorthandSudarshan

DependencyPreservation
s LetFibethesetofdependenciesF+thatincludeonlyattributesin

Ri.

Adecompositionisdependencypreserving,if Ifitisnot,thencheckingupdatesforviolationoffunctional dependenciesmayrequirecomputingjoins,whichis expensive.

(F1F2Fn)+=F+

DatabaseSystemConcepts5thEdition,July28,2005.

7.<number>

Silberschatz,KorthandSudarshan

TestingforDependencyPreservation
s TocheckifadependencyispreservedinadecompositionofRinto

R1,R2,,Rnweapplythefollowingtest(withattributeclosuredonewith respecttoF)
q

result= while(changestoresult)do foreachRiinthedecomposition t=(resultRi)+Ri result=resultt Ifresultcontainsallattributesin,thenthefunctionaldependency ispreserved.

s WeapplythetestonalldependenciesinFtocheckifadecompositionis

dependencypreserving

s Thisproceduretakespolynomialtime,insteadoftheexponentialtime requiredtocomputeF+and(F1F2Fn)+

DatabaseSystemConcepts5thEdition,July28,2005.

7.<number>

Silberschatz,KorthandSudarshan

Example
s R=(A,B,C)

F={AB BC} Key={A}

s RisnotinBCNF s DecompositionR1=(A,B),R2=(B,C)
q q q

R1andR2inBCNF Losslessjoindecomposition Dependencypreserving

DatabaseSystemConcepts5thEdition,July28,2005.

7.<number>

Silberschatz,KorthandSudarshan

TestingforBCNF
s Tocheckifanontrivialdependency causesaviolationofBCNF

1.compute+(theattributeclosureof),and 2.verifythatitincludesallattributesofR,thatis,itisasuperkeyofR.
s Simplifiedtest:TocheckifarelationschemaRisinBCNF,itsufficesto

checkonlythedependenciesinthegivensetFforviolationofBCNF, ratherthancheckingalldependenciesinF+.
q

IfnoneofthedependenciesinFcausesaviolationofBCNF,then noneofthedependenciesinF+willcauseaviolationofBCNFeither.

s However,usingonlyFisincorrectwhentestingarelationina

decompositionofR
q

ConsiderR=(A,B,C,D,E),withF={AB,BCD} DecomposeRintoR = (A,B)andR = (A,C,D,E) 1 2

NeitherofthedependenciesinFcontainonlyattributesfrom (A,C,D,E)sowemightbemisleadintothinkingR2satisfiesBCNF. Infact,dependencyACDinF+showsR2isnotinBCNF.


7.<number> Silberschatz,KorthandSudarshan

DatabaseSystemConcepts5thEdition,July28,2005.

TestingDecompositionforBCNF
s TocheckifarelationRiinadecompositionofRisinBCNF,
q

EithertestRiforBCNFwithrespecttotherestrictionofFtoRi(that is,allFDsinF+thatcontainonlyattributesfromRi) orusetheoriginalsetofdependenciesFthatholdonR,butwiththe followingtest: foreverysetofattributesRi,checkthat+(theattribute closureof)eitherincludesnoattributeofRi,orincludesall attributesofRi.

Iftheconditionisviolatedbysome inF,thedependency (+ )Ri canbeshowntoholdonRi,andRiviolatesBCNF. WeuseabovedependencytodecomposeRi

DatabaseSystemConcepts5thEdition,July28,2005.

7.<number>

Silberschatz,KorthandSudarshan

BCNFDecompositionAlgorithm
result:={R}; done:=false; computeF+; while(notdone)do if(thereisaschemaRiinresultthatisnotinBCNF) thenbegin let beanontrivialfunctionaldependencythatholdsonRi suchthat RiisnotinF+, and=;

result:=(resultRi)(Ri)(,); end elsedone:=true; Note:eachRiisinBCNF,anddecompositionislosslessjoin.

DatabaseSystemConcepts5thEdition,July28,2005.

7.<number>

Silberschatz,KorthandSudarshan

ExampleofBCNFDecomposition
s R=(A,B,C)

F={AB BC} Key={A}

s RisnotinBCNF(BCbutBisnotsuperkey) s Decomposition
q q

R1=(B,C) R2=(A,B)

DatabaseSystemConcepts5thEdition,July28,2005.

7.<number>

Silberschatz,KorthandSudarshan

ExampleofBCNFDecomposition
s OriginalrelationRandfunctionaldependencyF

R=(branch_name,branch_city,assets, customer_name,loan_number,amount) F={branch_nameassetsbranch_city loan_numberamountbranch_name} Key={loan_number,customer_name}


s Decomposition
q q q q

R1=(branch_name,branch_city,assets) R2=(branch_name,customer_name,loan_number,amount) R3=(branch_name,loan_number,amount) R4=(customer_name,loan_number) R1,R3,R4

s Finaldecomposition

DatabaseSystemConcepts5thEdition,July28,2005.

7.<number>

Silberschatz,KorthandSudarshan

BCNFandDependencyPreservation
ItisnotalwayspossibletogetaBCNFdecompositionthatis dependencypreserving
s R=(J,K,L)

F={JKL LK} Twocandidatekeys=JKandJL

s RisnotinBCNF s AnydecompositionofRwillfailtopreserve

JKL ThisimpliesthattestingforJKLrequiresajoin

DatabaseSystemConcepts5thEdition,July28,2005.

7.<number>

Silberschatz,KorthandSudarshan

ThirdNormalForm:Motivation
s Therearesomesituationswhere
q q

BCNFisnotdependencypreserving,and efficientcheckingforFDviolationonupdatesisimportant

s Solution:defineaweakernormalform,calledThird

NormalForm(3NF)
q

Allowssomeredundancy(withresultantproblems;wewill seeexampleslater) Butfunctionaldependenciescanbecheckedonindividual relationswithoutcomputingajoin. Thereisalwaysalosslessjoin,dependencypreserving decompositioninto3NF.

DatabaseSystemConcepts5thEdition,July28,2005.

7.<number>

Silberschatz,KorthandSudarshan

3NFExample
s RelationR:
q

R=(J,K,L) F={JKL,LK} Twocandidatekeys:JKandJL Risin3NF JKL LK JKisasuperkey Kiscontainedinacandidatekey

q q

DatabaseSystemConcepts5thEdition,July28,2005.

7.<number>

Silberschatz,KorthandSudarshan

Redundancyin3NF
s Thereissomeredundancyinthisschema s Exampleofproblemsduetoredundancyin3NF
q

R=(J,K,L) F={JKL,LK} J j1 j2 j3 null L l1 l1 l1 l2 K k1 k1 k1 k2

s repetitionofinformation(e.g.,therelationshipl1,k1) s needtousenullvalues(e.g.,torepresenttherelationship l2,k2wherethereisnocorrespondingvalueforJ).

DatabaseSystemConcepts5thEdition,July28,2005.

7.<number>

Silberschatz,KorthandSudarshan

Testingfor3NF
s Optimization:NeedtocheckonlyFDsinF,neednotcheckallFDsin

F+ .

s Useattributeclosuretocheckforeachdependency,ifisa

superkey.

s Ifisnotasuperkey,wehavetoverifyifeachattributeinis

containedinacandidatekeyofR
q

thistestisrathermoreexpensive,sinceitinvolvefinding candidatekeys testingfor3NFhasbeenshowntobeNPhard Interestingly,decompositionintothirdnormalform(described shortly)canbedoneinpolynomialtime

q q

DatabaseSystemConcepts5thEdition,July28,2005.

7.<number>

Silberschatz,KorthandSudarshan

3NFDecompositionAlgorithm
LetFcbeacanonicalcoverforF; i:=0; foreachfunctionaldependencyinFcdo ifnoneoftheschemasRj,1jicontains thenbegin i:=i+1; Ri:= end ifnoneoftheschemasRj,1jicontainsacandidatekeyforR thenbegin i:=i+1; Ri:=anycandidatekeyforR; end return(R1,R2,...,Ri)

DatabaseSystemConcepts5thEdition,July28,2005.

7.<number>

Silberschatz,KorthandSudarshan

3NFDecompositionAlgorithm(Cont.)
s Abovealgorithmensures:
q q q

eachrelationschemaRiisin3NF decompositionisdependencypreservingandlosslessjoin Proofofcorrectnessisatendofthispresentation(clickhere)

DatabaseSystemConcepts5thEdition,July28,2005.

7.<number>

Silberschatz,KorthandSudarshan

3NFDecomposition:AnExample
s Relationschema:

cust_banker_branch=(customer_id,employee_id,branch_name,type)
s Thefunctionaldependenciesforthisrelationschemaare:
q q q

customer_id,employee_idbranch_name,type employee_idbranch_name customer_id,branch_nameemployee_id branch_nameisextraneousinther.h.s.ofthe1stdependency Nootherattributeisextraneous,sowegetFC=

s Wefirstcomputeacanonicalcover
q q

customer_id,employee_idtype employee_idbranch_name customer_id,branch_nameemployee_id

DatabaseSystemConcepts5thEdition,July28,2005.

7.<number>

Silberschatz,KorthandSudarshan

3NFDecompsitionExample(Cont.)
s

Theforloopgeneratesfollowing3NFschema:

(customer_id,employee_id,type) (employee_id,branch_name) (customer_id,branch_name,employee_id)


q

Observethat(customer_id,employee_id,type)containsacandidatekeyof theoriginalschema,sonofurtherrelationschemaneedsbeadded

IftheFDswereconsideredinadifferentorder,withthe2ndoneconsideredafter the3rd, (employee_id,branch_name) wouldnotbeincludedinthedecompositionbecauseitisasubsetof (customer_id,branch_name,employee_id) Minorextensionofthe3NFdecompositionalgorithm:atendofforloop,detect anddeleteschemas,suchas(employee_id,branch_name),whicharesubsets ofotherschemas


q

resultwillnotdependontheorderinwhichFDsareconsidered

Theresultantsimplified3NFschemais:

(customer_id,employee_id,type)

(customer_id,branch_name,employee_id)
DatabaseSystemConcepts5thEdition,July28,2005. 7.<number> Silberschatz,KorthandSudarshan

ComparisonofBCNFand3NF
s Itisalwayspossibletodecomposearelationintoasetofrelations

thatarein3NFsuchthat:
q q

thedecompositionislossless thedependenciesarepreserved

s Itisalwayspossibletodecomposearelationintoasetofrelationsthat

areinBCNFsuchthat:
q q

thedecompositionislossless itmaynotbepossibletopreservedependencies.

DatabaseSystemConcepts5thEdition,July28,2005.

7.<number>

Silberschatz,KorthandSudarshan

DesignGoals
s Goalforarelationaldatabasedesignis:
q q q

BCNF. Losslessjoin. Dependencypreservation. Lackofdependencypreservation Redundancyduetouseof3NF

s Ifwecannotachievethis,weacceptoneof
q q

s Interestingly,SQLdoesnotprovideadirectwayofspecifying

functionaldependenciesotherthansuperkeys.

CanspecifyFDsusingassertions,buttheyareexpensivetotest
s Evenifwehadadependencypreservingdecomposition,usingSQL

wewouldnotbeabletoefficientlytestafunctionaldependencywhose lefthandsideisnotakey.

DatabaseSystemConcepts5thEdition,July28,2005.

7.<number>

Silberschatz,KorthandSudarshan

MultivaluedDependencies(MVDs)
s LetRbearelationschemaandletRandR.The

multivalueddependency

holdsonRifinanylegalrelationr(R),forallpairsfortuplest1 andt2inrsuchthatt1[]=t2[],thereexisttuplest3andt4inr suchthat: t1[]=t2[]=t3[]=t4[] t3[]=t1[] t3[R]=t2[R] t4[]=t2[] t4[R]=t1[R]

DatabaseSystemConcepts5thEdition,July28,2005.

7.<number>

Silberschatz,KorthandSudarshan

MVD(Cont.)
s Tabularrepresentationof

DatabaseSystemConcepts5thEdition,July28,2005.

7.<number>

Silberschatz,KorthandSudarshan

Example
s LetRbearelationschemawithasetofattributesthatarepartitioned

into3nonemptysubsets.

Y,Z,W
s WesaythatYZ(YmultideterminesZ)

ifandonlyifforallpossiblerelationsr(R) then

<y1,z1,w1>rand<y1,z2,w2>r <y1,z1,w2>rand<y1,z2,w1>r
s NotethatsincethebehaviorofZandWareidenticalitfollowsthat

YZifYW

DatabaseSystemConcepts5thEdition,July28,2005.

7.<number>

Silberschatz,KorthandSudarshan

Example(Cont.)
s Inourexample:

courseteacher coursebook
s Theaboveformaldefinitionissupposedtoformalizethe

notionthatgivenaparticularvalueofY(course)ithas associatedwithitasetofvaluesofZ(teacher)andasetof valuesofW(book),andthesetwosetsareinsomesense independentofeachother.


q q

s Note:

IfYZthenYZ Indeedwehave(inabovenotation)Z1=Z2 Theclaimfollows.

DatabaseSystemConcepts5thEdition,July28,2005.

7.<number>

Silberschatz,KorthandSudarshan

UseofMultivaluedDependencies
s Weusemultivalueddependenciesintwoways:

1. Totestrelationstodeterminewhethertheyarelegalundera givensetoffunctionalandmultivalueddependencies 2. Tospecifyconstraintsonthesetoflegalrelations.Weshall thusconcernourselvesonlywithrelationsthatsatisfya givensetoffunctionalandmultivalueddependencies.


s Ifarelationrfailstosatisfyagivenmultivalueddependency,we

canconstructarelationsrthatdoessatisfythemultivalued dependencybyaddingtuplestor.

DatabaseSystemConcepts5thEdition,July28,2005.

7.<number>

Silberschatz,KorthandSudarshan

TheoryofMVDs
s Fromthedefinitionofmultivalueddependency,wecanderivethe

followingrule:
q

If,then

Thatis,everyfunctionaldependencyisalsoamultivalueddependency
s TheclosureD+ofDisthesetofallfunctionalandmultivalued

dependencieslogicallyimpliedbyD.
q

WecancomputeD+fromD,usingtheformaldefinitionsof functionaldependenciesandmultivalueddependencies. Wecanmanagewithsuchreasoningforverysimplemultivalued dependencies,whichseemtobemostcommoninpractice Forcomplexdependencies,itisbettertoreasonaboutsetsof dependenciesusingasystemofinferencerules(seeAppendixC).

DatabaseSystemConcepts5thEdition,July28,2005.

7.<number>

Silberschatz,KorthandSudarshan

FourthNormalForm
s ArelationschemaRisin4NFwithrespecttoasetDoffunctionaland

multivalueddependenciesifforallmultivalueddependenciesinD+ofthe form,whereRandR,atleastoneofthefollowinghold:
q q

istrivial(i.e.,or=R) isasuperkeyforschemaR

s Ifarelationisin4NFitisinBCNF

DatabaseSystemConcepts5thEdition,July28,2005.

7.<number>

Silberschatz,KorthandSudarshan

RestrictionofMultivaluedDependencies
s TherestrictionofDtoRiisthesetDiconsistingof
q q

AllfunctionaldependenciesinD+thatincludeonlyattributesofRi Allmultivalueddependenciesoftheform (Ri)

whereRiandisinD+

DatabaseSystemConcepts5thEdition,July28,2005.

7.<number>

Silberschatz,KorthandSudarshan

4NFDecompositionAlgorithm
result:={R}; done:=false; computeD+; LetDidenotetherestrictionofD+toRi while(notdone) if(thereisaschemaRiinresultthatisnotin4NF)then begin letbeanontrivialmultivalueddependencythatholds onRisuchthatRiisnotinDi,and=; result:=(resultRi)(Ri)(,); end elsedone:=true; Note:eachRiisin4NF,anddecompositionislosslessjoin

DatabaseSystemConcepts5thEdition,July28,2005.

7.<number>

Silberschatz,KorthandSudarshan

Example
s R=(A,B,C,G,H,I)

F={AB BHI CGH}


s Risnotin4NFsinceABandAisnotasuperkeyforR s Decomposition

a)R1=(A,B) b)R2=(A,C,G,H,I) c)R3=(C,G,H) d)R4=(A,C,G,I) e)R5=(A,I) f)R6=(A,C,G)


DatabaseSystemConcepts5thEdition,July28,2005.

(R1isin4NF) (R2isnotin4NF) (R3isin4NF) (R4isnotin4NF) (R5isin4NF) (R6isin4NF)


7.<number> Silberschatz,KorthandSudarshan

s SinceABandBHI,AHI,AI

FurtherNormalForms
s Joindependenciesgeneralizemultivalueddependencies
q

leadtoprojectjoinnormalform(PJNF)(alsocalledfifthnormal form)

s Aclassofevenmoregeneralconstraints,leadstoanormalform

calleddomainkeynormalform.

s Problemwiththesegeneralizedconstraints:arehardtoreasonwith,

andnosetofsoundandcompletesetofinferencerulesexists.

s Hencerarelyused

DatabaseSystemConcepts5thEdition,July28,2005.

7.<number>

Silberschatz,KorthandSudarshan

OverallDatabaseDesignProcess
s WehaveassumedschemaRisgiven
q

RcouldhavebeengeneratedwhenconvertingERdiagramtoasetof tables. Rcouldhavebeenasinglerelationcontainingallattributesthatareof interest(calleduniversalrelation). NormalizationbreaksRintosmallerrelations. Rcouldhavebeentheresultofsomeadhocdesignofrelations,which wethentest/converttonormalform.

q q

DatabaseSystemConcepts5thEdition,July28,2005.

7.<number>

Silberschatz,KorthandSudarshan

ERModelandNormalization
s WhenanERdiagramiscarefullydesigned,identifyingallentities

correctly,thetablesgeneratedfromtheERdiagramshouldnotneed furthernormalization. dependenciesfromnonkeyattributesofanentitytootherattributesof theentity


q

s However,inareal(imperfect)design,therecanbefunctional

Example:anemployeeentitywithattributesdepartment_number anddepartment_address,andafunctionaldependency department_numberdepartment_address Gooddesignwouldhavemadedepartmentanentity

s Functionaldependenciesfromnonkeyattributesofarelationshipset

possible,butraremostrelationshipsarebinary

DatabaseSystemConcepts5thEdition,July28,2005.

7.<number>

Silberschatz,KorthandSudarshan

DenormalizationforPerformance
s Maywanttousenonnormalizedschemaforperformance s Forexample,displayingcustomer_namealongwithaccount_numberand

balancerequiresjoinofaccountwithdepositor aswellasdepositorwithallaboveattributes
q q q

s Alternative1:Usedenormalizedrelationcontainingattributesofaccount

fasterlookup extraspaceandextraexecutiontimeforupdates extracodingworkforprogrammerandpossibilityoferrorinextracode

s Alternative2:useamaterializedviewdefinedas

accountdepositor
q

Benefitsanddrawbackssameasabove,exceptnoextracodingwork forprogrammerandavoidspossibleerrors

DatabaseSystemConcepts5thEdition,July28,2005.

7.<number>

Silberschatz,KorthandSudarshan

OtherDesignIssues
s Someaspectsofdatabasedesignarenotcaughtbynormalization s Examplesofbaddatabasedesign,tobeavoided:

Insteadofearnings(company_id,year,amount),use
q

earnings_2004,earnings_2005,earnings_2006,etc.,allonthe schema(company_id,earnings).

AboveareinBCNF,butmakequeryingacrossyearsdifficult andneedsnewtableeachyear

company_year(company_id,earnings_2004,earnings_2005, earnings_2006)

AlsoinBCNF,butalsomakesqueryingacrossyearsdifficult andrequiresnewattributeeachyear. Isanexampleofacrosstab,wherevaluesforoneattribute becomecolumnnames Usedinspreadsheets,andindataanalysistools

DatabaseSystemConcepts5thEdition,July28,2005.

7.<number>

Silberschatz,KorthandSudarshan

ModelingTemporalData
s Temporaldatahaveanassociationtimeintervalduringwhichthedata

arevalid.

s Asnapshotisthevalueofthedataataparticularpointintime s SeveralproposalstoextendERmodelbyaddingvalidtimeto
q q q

attributes,e.g.addressofacustomeratdifferentpointsintime entities,e.g.timedurationwhenanaccountexists relationships,e.g.timeduringwhichacustomerownedanaccount

s Butnoacceptedstandard s Addingatemporalcomponentresultsinfunctionaldependencieslike

customer_idcustomer_street,customer_city nottohold,becausetheaddressvariesovertime
s AtemporalfunctionaldependencyXYholdsonschemaRifthe

functionaldependencyXYholdsonallsnapshotsforalllegal instancesr(R)

DatabaseSystemConcepts5thEdition,July28,2005.

7.<number>

Silberschatz,KorthandSudarshan

ModelingTemporalData(Cont.)
s Inpractice,databasedesignersmayaddstartandendtimeattributes

torelations
q

E.g.course(course_id,course_title) course(course_id,course_title,start,end)

Constraint:notwotuplescanhaveoverlappingvalidtimes Hardtoenforceefficiently

s Foreignkeyreferencesmaybetocurrentversionofdata,ortodataat

apointintime
q

E.g.studenttranscriptshouldrefertocourseinformationatthe timethecoursewastaken

DatabaseSystemConcepts5thEdition,July28,2005.

7.<number>

Silberschatz,KorthandSudarshan

EndofChapter

DatabaseSystemConcepts,5thEd.
Silberschatz,KorthandSudarshan Seewww.dbbook.comforconditionsonreuse

ProofofCorrectnessof3NF DecompositionAlgorithm

DatabaseSystemConcepts,5thEd.
Silberschatz,KorthandSudarshan Seewww.dbbook.comforconditionsonreuse

s 3NFdecompositionalgorithmisdependencypreserving(sincethereisa

Correctnessof3NFDecomposition Algorithm
relationforeveryFDinFc)
q q q

s Decompositionislossless

Acandidatekey(C)isinoneoftherelationsRiindecomposition ClosureofcandidatekeyunderFcmustcontainallattributesinR. Followthestepsofattributeclosurealgorithmtoshowthereisonly onetupleinthejoinresultforeachtupleinRi

DatabaseSystemConcepts5thEdition,July28,2005.

7.<number>

Silberschatz,KorthandSudarshan

Correctnessof3NFDecomposition Algorithm(Contd.)
Claim:ifarelationRiisinthedecompositiongeneratedbythe abovealgorithm,thenRisatisfies3NF.
s LetRibegeneratedfromthedependency s LetBbeanynontrivialfunctionaldependencyonRi.(Weneedonly

considerFDswhoserighthandsideisasingleattribute.) separately.

s Now,Bcanbeineitherorbutnotinboth.Considereachcase

DatabaseSystemConcepts5thEdition,July28,2005.

7.<number>

Silberschatz,KorthandSudarshan

s Case1:IfBin:
q q q

Correctnessof3NFDecomposition (Contd.)
Ifisasuperkey,the2ndconditionof3NFissatisfied Otherwisemustcontainsomeattributenotin SinceBisinF+itmustbederivablefromFc,byusingattribute closureon. Attributeclosurenothaveused.Ifithadbeenused,must becontainedintheattributeclosureof,whichisnotpossible,since weassumedisnotasuperkey. Now,using({B})andB,wecanderiveB (since,andBsinceBisnontrivial)
q

Then,Bisextraneousintherighthandsideof;whichisnot possiblesinceisinFc. Thus,ifBisinthenmustbeasuperkey,andthesecond conditionof3NFmustbesatisfied.

DatabaseSystemConcepts5thEdition,July28,2005.

7.<number>

Silberschatz,KorthandSudarshan

Correctnessof3NFDecomposition (Contd.)
s Case2:Bisin.
q

Sinceisacandidatekey,thethirdalternativeinthedefinitionof 3NFistriviallysatisfied. Infact,wecannotshowthatisasuperkey. Thisshowsexactlywhythethirdalternativeispresentinthe definitionof3NF.

q q

Q.E.D.

DatabaseSystemConcepts5thEdition,July28,2005.

7.<number>

Silberschatz,KorthandSudarshan

Figure7.5:SampleRelationr

DatabaseSystemConcepts5thEdition,July28,2005.

7.<number>

Silberschatz,KorthandSudarshan

Figure7.6

DatabaseSystemConcepts5thEdition,July28,2005.

7.<number>

Silberschatz,KorthandSudarshan

Figure7.7

DatabaseSystemConcepts5thEdition,July28,2005.

7.<number>

Silberschatz,KorthandSudarshan

Figure7.15:AnExampleof RedundancyinaBCNFRelation

DatabaseSystemConcepts5thEdition,July28,2005.

7.<number>

Silberschatz,KorthandSudarshan

Figure7.16:AnIllegalR2Relation

DatabaseSystemConcepts5thEdition,July28,2005.

7.<number>

Silberschatz,KorthandSudarshan

Figure7.18:RelationofPractice Exercise7.2

DatabaseSystemConcepts5thEdition,July28,2005.

7.<number>

Silberschatz,KorthandSudarshan