- 1.21 MB
- 2022-04-29 14:31:59 发布
- 1、本文档共5页,可阅读全部内容。
- 2、本文档内容版权归属内容提供方,所产生的收益全部归内容提供方所有。如果您对本文有版权争议,可选择认领,认领后既往收益都归您。
- 3、本文档由用户上传,本站不保证质量和数量令人满意,可能有诸多瑕疵,付费之前,请仔细先通过免费阅读内容等途径辨别内容交易风险。如存在严重挂羊头卖狗肉之情形,可联系本站下载客服投诉处理。
- 文档侵权举报电话:19940600175。
'Chapter8:RelationalDatabaseDesign
Chapter8:RelationalDatabaseDesignFeaturesofGoodRelationalDesignAtomicDomainsandFirstNormalFormDecompositionUsingFunctionalDependenciesFunctionalDependencyTheoryAlgorithmsforFunctionalDependenciesDecompositionUsingMultivaluedDependenciesMoreNormalFormDatabase-DesignProcessModelingTemporalData
CombineSchemas?Supposewecombineinstructoranddepartmentintoinst_dept(Noconnectiontorelationshipsetinst_dept)Resultispossiblerepetitionofinformation
ACombinedSchemaWithoutRepetitionConsidercombiningrelationssec_class(sec_id,building,room_number)andsection(course_id,sec_id,semester,year)intoonerelationsection(course_id,sec_id,semester,year,building,room_number)Norepetitioninthiscase
WhatAboutSmallerSchemas?Supposewehadstartedwithinst_dept.Howwouldweknowtosplitup(decompose)itintoinstructoranddepartment?Writearule“iftherewereaschema(dept_name,building,budget),thendept_namewouldbeacandidatekey”Denoteasafunctionaldependency:dept_namebuilding,budgetIninst_dept,becausedept_nameisnotacandidatekey,thebuildingandbudgetofadepartmentmayhavetoberepeated.Thisindicatestheneedtodecomposeinst_deptNotalldecompositionsaregood.Supposewedecomposeemployee(ID,name,street,city,salary)intoemployee1(ID,name)employee2(name,street,city,salary)Thenextslideshowshowweloseinformation--wecannotreconstructtheoriginalemployeerelation--andso,thisisalossydecomposition.
ALossyDecomposition
ExampleofLossless-JoinDecompositionLosslessjoindecompositionDecompositionofR=(A,B,C)R1=(A,B)R2=(B,C)AB12AB12rB,C(r)A(r)B(r)AB12CABB12CABCABA,B(r)
FirstNormalFormDomainisatomicifitselementsareconsideredtobeindivisibleunitsExamplesofnon-atomicdomains:Setofnames,compositeattributesIdentificationnumberslikeCS101thatcanbebrokenupintopartsArelationalschemaRisinfirstnormalformifthedomainsofallattributesofRareatomicNon-atomicvaluescomplicatestorageandencourageredundant(repeated)storageofdataExample:Setofaccountsstoredwitheachcustomer,andsetofownersstoredwitheachaccountWeassumeallrelationsareinfirstnormalform(andrevisitthisinChapter22:ObjectBasedDatabases)
FirstNormalForm(Cont’d)Atomicityisactuallyapropertyofhowtheelementsofthedomainareused.Example:StringswouldnormallybeconsideredindivisibleSupposethatstudentsaregivenrollnumberswhicharestringsoftheformCS0012orEE1127Ifthefirsttwocharactersareextractedtofindthedepartment,thedomainofrollnumbersisnotatomic.Doingsoisabadidea:leadstoencodingofinformationinapplicationprogramratherthaninthedatabase.
Goal—DeviseaTheoryfortheFollowingDecidewhetheraparticularrelationRisin“good”form.InthecasethatarelationRisnotin“good”form,decomposeitintoasetofrelations{R1,R2,...,Rn}suchthateachrelationisingoodformthedecompositionisalossless-joindecompositionOurtheoryisbasedon:functionaldependenciesmultivalueddependencies
FunctionalDependenciesConstraintsonthesetoflegalrelations.Requirethatthevalueforacertainsetofattributesdeterminesuniquelythevalueforanothersetofattributes.Afunctionaldependencyisageneralizationofthenotionofakey.
FunctionalDependencies(Cont.)LetRbearelationschemaRandRThefunctionaldependencyholdsonRifandonlyifforanylegalrelationsr(R),wheneveranytwotuplest1andt2ofragreeontheattributes,theyalsoagreeontheattributes.Thatis,t1[]=t2[]t1[]=t2[]Example:Considerr(A,B)withthefollowinginstanceofr.Onthisinstance,ABdoesNOThold,butBAdoeshold.41537
FunctionalDependencies(Cont.)KisasuperkeyforrelationschemaRifandonlyifKRKisacandidatekeyforRifandonlyifKR,andfornoK,RFunctionaldependenciesallowustoexpressconstraintsthatcannotbeexpressedusingsuperkeys.Considertheschema:inst_dept(ID,name,salary,dept_name,building,budget).Weexpectthesefunctionaldependenciestohold:dept_namebuildingandIDbuildingbutwouldnotexpectthefollowingtohold:dept_namesalary
UseofFunctionalDependenciesWeusefunctionaldependenciesto:testrelationstoseeiftheyarelegalunderagivensetoffunctionaldependencies.IfarelationrislegalunderasetFoffunctionaldependencies,wesaythatrsatisfiesF.specifyconstraintsonthesetoflegalrelationsWesaythatFholdsonRifalllegalrelationsonRsatisfythesetoffunctionaldependenciesF.Note:Aspecificinstanceofarelationschemamaysatisfyafunctionaldependencyevenifthefunctionaldependencydoesnotholdonalllegalinstances.Forexample,aspecificinstanceofinstructormay,bychance,satisfynameID.
FunctionalDependencies(Cont.)AfunctionaldependencyistrivialifitissatisfiedbyallinstancesofarelationExample:ID,nameIDnamenameIngeneral,istrivialif
ClosureofaSetofFunctionalDependenciesGivenasetFoffunctionaldependencies,therearecertainotherfunctionaldependenciesthatarelogicallyimpliedbyF.Forexample:IfABandBC,thenwecaninferthatACThesetofallfunctionaldependencieslogicallyimpliedbyFistheclosureofF.WedenotetheclosureofFbyF+.F+isasupersetofF.
Boyce-CoddNormalFormistrivial(i.e.,)isasuperkeyforRArelationschemaRisinBCNFwithrespecttoasetFoffunctionaldependenciesifforallfunctionaldependenciesinF+oftheformwhereRandR,atleastoneofthefollowingholds:ExampleschemanotinBCNF:instr_dept(ID,name,salary,dept_name,building,budget)becausedept_namebuilding,budgetholdsoninstr_dept,butdept_nameisnotasuperkey
DecomposingaSchemaintoBCNFSupposewehaveaschemaRandanon-trivialdependencycausesaviolationofBCNF.WedecomposeRinto:(U)(R-(-))Inourexample,=dept_name=building,budgetandinst_deptisreplacedby(U)=(dept_name,building,budget)(R-(-))=(ID,name,salary,dept_name)
BCNFandDependencyPreservationConstraints,includingfunctionaldependencies,arecostlytocheckinpracticeunlesstheypertaintoonlyonerelationIfitissufficienttotestonlythosedependenciesoneachindividualrelationofadecompositioninordertoensurethatallfunctionaldependencieshold,thenthatdecompositionisdependencypreserving.BecauseitisnotalwayspossibletoachievebothBCNFanddependencypreservation,weconsideraweakernormalform,knownasthirdnormalform.
ThirdNormalFormArelationschemaRisinthirdnormalform(3NF)ifforall:inF+atleastoneofthefollowingholds:istrivial(i.e.,)isasuperkeyforREachattributeAin–iscontainedinacandidatekeyforR.(NOTE:eachattributemaybeinadifferentcandidatekey)IfarelationisinBCNFitisin3NF(sinceinBCNFoneofthefirsttwoconditionsabovemusthold).ThirdconditionisaminimalrelaxationofBCNFtoensuredependencypreservation(willseewhylater).
GoalsofNormalizationLetRbearelationschemewithasetFoffunctionaldependencies.DecidewhetherarelationschemeRisin“good”form.InthecasethatarelationschemeRisnotin“good”form,decomposeitintoasetofrelationscheme{R1,R2,...,Rn}suchthateachrelationschemeisingoodformthedecompositionisalossless-joindecompositionPreferably,thedecompositionshouldbedependencypreserving.
HowgoodisBCNF?TherearedatabaseschemasinBCNFthatdonotseemtobesufficientlynormalizedConsiderarelationinst_info(ID,child_name,phone)whereaninstructormayhavemorethanonephoneandcanhavemultiplechildrenIDchild_namephone99999999999999999999DavidDavidWilliamWillian512-555-1234512-555-4321512-555-1234512-555-4321inst_info
Therearenonon-trivialfunctionaldependenciesandthereforetherelationisinBCNFInsertionanomalies–i.e.,ifweaddaphone981-992-3443to99999,weneedtoaddtwotuples(99999,David,981-992-3443)(99999,William,981-992-3443)HowgoodisBCNF?(Cont.)
Therefore,itisbettertodecomposeinst_infointo:Thissuggeststheneedforhighernormalforms,suchasFourthNormalForm(4NF),whichweshallseelater.HowgoodisBCNF?(Cont.)IDchild_name99999999999999999999DavidDavidWilliamWillianinst_childIDphone99999999999999999999512-555-1234512-555-4321512-555-1234512-555-4321inst_phone
Functional-DependencyTheoryWenowconsidertheformaltheorythattellsuswhichfunctionaldependenciesareimpliedlogicallybyagivensetoffunctionaldependencies.WethendevelopalgorithmstogeneratelosslessdecompositionsintoBCNFand3NFWethendevelopalgorithmstotestifadecompositionisdependency-preserving
ClosureofaSetofFunctionalDependenciesGivenasetFsetoffunctionaldependencies,therearecertainotherfunctionaldependenciesthatarelogicallyimpliedbyF.Fore.g.:IfABandBC,thenwecaninferthatACThesetofallfunctionaldependencieslogicallyimpliedbyFistheclosureofF.WedenotetheclosureofFbyF+.
ClosureofaSetofFunctionalDependenciesWecanfindF+,theclosureofF,byrepeatedlyapplyingArmstrong’sAxioms:if,then(reflexivity)if,then(augmentation)if,and,then(transitivity)Theserulesaresound(generateonlyfunctionaldependenciesthatactuallyhold),andcomplete(generateallfunctionaldependenciesthathold).
ExampleR=(A,B,C,G,H,I)F={ABACCGHCGIBH}somemembersofF+AHbytransitivityfromABandBHAGIbyaugmentingACwithG,togetAGCGandthentransitivitywithCGICGHIbyaugmentingCGItoinferCGCGI,andaugmentingofCGHtoinferCGIHI,andthentransitivity
ProcedureforComputingF+TocomputetheclosureofasetoffunctionaldependenciesF:F+=FrepeatforeachfunctionaldependencyfinF+applyreflexivityandaugmentationrulesonfaddtheresultingfunctionaldependenciestoF+foreachpairoffunctionaldependenciesf1andf2inF+iff1andf2canbecombinedusingtransitivitythenaddtheresultingfunctionaldependencytoF+untilF+doesnotchangeanyfurtherNOTE:Weshallseeanalternativeprocedureforthistasklater
ClosureofFunctionalDependencies(Cont.)Additionalrules:Ifholdsandholds,thenholds(union)Ifholds,thenholdsandholds(decomposition)Ifholdsandholds,thenholds(pseudotransitivity)TheaboverulescanbeinferredfromArmstrong’saxioms.
ClosureofAttributeSetsGivenasetofattributesa,definetheclosureofaunderF(denotedbya+)asthesetofattributesthatarefunctionallydeterminedbyaunderFAlgorithmtocomputea+,theclosureofaunderFresult:=a;while(changestoresult)doforeachinFdobeginifresultthenresult:=resultend
ExampleofAttributeSetClosureR=(A,B,C,G,H,I)F={ABACCGHCGIBH}(AG)+1.result=AG2.result=ABCG(ACandAB)3.result=ABCGH(CGHandCGAGBC)4.result=ABCGHI(CGIandCGAGBCH)IsAGacandidatekey?IsAGasuperkey?DoesAGR?==Is(AG)+RIsanysubsetofAGasuperkey?DoesAR?==Is(A)+RDoesGR?==Is(G)+R
UsesofAttributeClosureThereareseveralusesoftheattributeclosurealgorithm:Testingforsuperkey:Totestifisasuperkey,wecompute+,andcheckif+containsallattributesofR.TestingfunctionaldependenciesTocheckifafunctionaldependencyholds(or,inotherwords,isinF+),justcheckif+.Thatis,wecompute+byusingattributeclosure,andthencheckifitcontains.Isasimpleandcheaptest,andveryusefulComputingclosureofFForeachR,wefindtheclosure+,andforeachS+,weoutputafunctionaldependencyS.
CanonicalCoverSetsoffunctionaldependenciesmayhaveredundantdependenciesthatcanbeinferredfromtheothersForexample:ACisredundantin:{AB,BC,AC}PartsofafunctionaldependencymayberedundantE.g.:onRHS:{AB,BC,ACD}canbesimplifiedto{AB,BC,AD}E.g.:onLHS:{AB,BC,ACD}canbesimplifiedto{AB,BC,AD}Intuitively,acanonicalcoverofFisa“minimal”setoffunctionaldependenciesequivalenttoF,havingnoredundantdependenciesorredundantpartsofdependencies
ExtraneousAttributesConsiderasetFoffunctionaldependenciesandthefunctionaldependencyinF.AttributeAisextraneousinifAandFlogicallyimplies(F–{}){(–A)}.AttributeAisextraneousinifAandthesetoffunctionaldependencies(F–{}){(–A)}logicallyimpliesF.Note:implicationintheoppositedirectionistrivialineachofthecasesabove,sincea“stronger”functionaldependencyalwaysimpliesaweakeroneExample:GivenF={AC,ABC}BisextraneousinABCbecause{AC,ABC}logicallyimpliesAC(I.e.theresultofdroppingBfromABC).Example:GivenF={AC,ABCD}CisextraneousinABCDsinceABCcanbeinferredevenafterdeletingC
TestingifanAttributeisExtraneousConsiderasetFoffunctionaldependenciesandthefunctionaldependencyinF.TotestifattributeAisextraneousincompute({}–A)+usingthedependenciesinFcheckthat({}–A)+contains;ifitdoes,AisextraneousinTotestifattributeAisextraneousincompute+usingonlythedependenciesinF’=(F–{}){(–A)},checkthat+containsA;ifitdoes,Aisextraneousin
CanonicalCoverAcanonicalcoverforFisasetofdependenciesFcsuchthatFlogicallyimpliesalldependenciesinFc,andFclogicallyimpliesalldependenciesinF,andNofunctionaldependencyinFccontainsanextraneousattribute,andEachleftsideoffunctionaldependencyinFcisunique.TocomputeacanonicalcoverforF:repeatUsetheunionruletoreplaceanydependenciesinF11and12with112Findafunctionaldependencywithanextraneousattributeeitherinorin/*Note:testforextraneousattributesdoneusingFc,notF*/Ifanextraneousattributeisfound,deleteitfromuntilFdoesnotchangeNote:Unionrulemaybecomeapplicableaftersomeextraneousattributeshavebeendeleted,soithastobere-applied
ComputingaCanonicalCoverR=(A,B,C)F={ABCBCABABC}CombineABCandABintoABCSetisnow{ABC,BC,ABC}AisextraneousinABCCheckiftheresultofdeletingAfromABCisimpliedbytheotherdependenciesYes:infact,BCisalreadypresent!Setisnow{ABC,BC}CisextraneousinABCCheckifACislogicallyimpliedbyABandtheotherdependenciesYes:usingtransitivityonABandBC.CanuseattributeclosureofAinmorecomplexcasesThecanonicalcoveris:ABBC
Lossless-joinDecompositionForthecaseofR=(R1,R2),werequirethatforallpossiblerelationsronschemaRr=R1(r)R2(r)AdecompositionofRintoR1andR2islosslessjoinifatleastoneofthefollowingdependenciesisinF+:R1R2R1R1R2R2Theabovefunctionaldependenciesareasufficientconditionforlosslessjoindecomposition;thedependenciesareanecessaryconditiononlyifallconstraintsarefunctionaldependencies
ExampleR=(A,B,C)F={AB,BC)CanbedecomposedintwodifferentwaysR1=(A,B),R2=(B,C)Lossless-joindecomposition:R1R2={B}andBBCDependencypreservingR1=(A,B),R2=(A,C)Lossless-joindecomposition:R1R2={A}andAABNotdependencypreserving(cannotcheckBCwithoutcomputingR1R2)
DependencyPreservationLetFibethesetofdependenciesF+thatincludeonlyattributesinRi.Adecompositionisdependencypreserving,if(F1F2…Fn)+=F+Ifitisnot,thencheckingupdatesforviolationoffunctionaldependenciesmayrequirecomputingjoins,whichisexpensive.
TestingforDependencyPreservationTocheckifadependencyispreservedinadecompositionofRintoR1,R2,…,Rnweapplythefollowingtest(withattributeclosuredonewithrespecttoF)result=while(changestoresult)doforeachRiinthedecompositiont=(resultRi)+Riresult=resulttIfresultcontainsallattributesin,thenthefunctionaldependencyispreserved.WeapplythetestonalldependenciesinFtocheckifadecompositionisdependencypreservingThisproceduretakespolynomialtime,insteadoftheexponentialtimerequiredtocomputeF+and(F1F2…Fn)+
ExampleR=(A,B,C)F={ABBC}Key={A}RisnotinBCNFDecompositionR1=(A,B),R2=(B,C)R1andR2inBCNFLossless-joindecompositionDependencypreserving
TestingforBCNFTocheckifanon-trivialdependencycausesaviolationofBCNF1.compute+(theattributeclosureof),and2.verifythatitincludesallattributesofR,thatis,itisasuperkeyofR.Simplifiedtest:TocheckifarelationschemaRisinBCNF,itsufficestocheckonlythedependenciesinthegivensetFforviolationofBCNF,ratherthancheckingalldependenciesinF+.IfnoneofthedependenciesinFcausesaviolationofBCNF,thennoneofthedependenciesinF+willcauseaviolationofBCNFeither.However,simplifiedtestusingonlyFisincorrectwhentestingarelationinadecompositionofRConsiderR=(A,B,C,D,E),withF={AB,BCD}DecomposeRintoR1=(A,B)andR2=(A,C,D,E)NeitherofthedependenciesinFcontainonlyattributesfrom(A,C,D,E)sowemightbemisleadintothinkingR2satisfiesBCNF.Infact,dependencyACDinF+showsR2isnotinBCNF.
TestingDecompositionforBCNFTocheckifarelationRiinadecompositionofRisinBCNF,EithertestRiforBCNFwithrespecttotherestrictionofFtoRi(thatis,allFDsinF+thatcontainonlyattributesfromRi)orusetheoriginalsetofdependenciesFthatholdonR,butwiththefollowingtest:foreverysetofattributesRi,checkthat+(theattributeclosureof)eitherincludesnoattributeofRi-,orincludesallattributesofRi.IftheconditionisviolatedbysomeinF,thedependency(+-)RicanbeshowntoholdonRi,andRiviolatesBCNF.WeuseabovedependencytodecomposeRi
BCNFDecompositionAlgorithmresult:={R};done:=false;computeF+;while(notdone)doif(thereisaschemaRiinresultthatisnotinBCNF)thenbeginletbeanontrivialfunctionaldependencythatholdsonRisuchthatRiisnotinF+,and=;result:=(result–Ri)(Ri–)(,);endelsedone:=true;Note:eachRiisinBCNF,anddecompositionislossless-join.
ExampleofBCNFDecompositionR=(A,B,C)F={ABBC}Key={A}RisnotinBCNF(BCbutBisnotsuperkey)DecompositionR1=(B,C)R2=(A,B)
ExampleofBCNFDecompositionclass(course_id,title,dept_name,credits,sec_id,semester,year,building,room_number,capacity,time_slot_id)Functionaldependencies:course_id→title,dept_name,creditsbuilding,room_number→capacitycourse_id,sec_id,semester,year→building,room_number,time_slot_idAcandidatekey{course_id,sec_id,semester,year}.BCNFDecomposition:course_id→title,dept_name,creditsholdsbutcourse_idisnotasuperkey.Wereplaceclassby:course(course_id,title,dept_name,credits)class-1(course_id,sec_id,semester,year,building,room_number,capacity,time_slot_id)
BCNFDecomposition(Cont.)courseisinBCNFHowdoweknowthis?building,room_number→capacityholdsonclass-1but{building,room_number}isnotasuperkeyforclass-1.Wereplaceclass-1by:classroom(building,room_number,capacity)section(course_id,sec_id,semester,year,building,room_number,time_slot_id)classroomandsectionareinBCNF.
BCNFandDependencyPreservationR=(J,K,L)F={JKLLK}Twocandidatekeys=JKandJLRisnotinBCNFAnydecompositionofRwillfailtopreserveJKLThisimpliesthattestingforJKLrequiresajoinItisnotalwayspossibletogetaBCNFdecompositionthatisdependencypreserving
ThirdNormalForm:MotivationTherearesomesituationswhereBCNFisnotdependencypreserving,andefficientcheckingforFDviolationonupdatesisimportantSolution:defineaweakernormalform,calledThirdNormalForm(3NF)Allowssomeredundancy(withresultantproblems;wewillseeexampleslater)Butfunctionaldependenciescanbecheckedonindividualrelationswithoutcomputingajoin.Thereisalwaysalossless-join,dependency-preservingdecompositioninto3NF.
3NFExampleRelationdept_advisor:dept_advisor(s_ID,i_ID,dept_name)F={s_ID,dept_namei_ID,i_IDdept_name}Twocandidatekeys:s_ID,dept_name,andi_ID,s_IDRisin3NFs_ID,dept_namei_IDs_IDdept_nameisasuperkeyi_IDdept_namedept_nameiscontainedinacandidatekey
Redundancyin3NFJj1j2j3nullLl1l1l1l2Kk1k1k1k2repetitionofinformation(e.g.,therelationshipl1,k1)(i_ID,dept_name)needtousenullvalues(e.g.,torepresenttherelationshipl2,k2wherethereisnocorrespondingvalueforJ).(i_ID,dept_nameI)ifthereisnoseparaterelationmappinginstructorstodepartmentsThereissomeredundancyinthisschemaExampleofproblemsduetoredundancyin3NFR=(J,K,L)F={JKL,LK}
Testingfor3NFOptimization:NeedtocheckonlyFDsinF,neednotcheckallFDsinF+.Useattributeclosuretocheckforeachdependency,ifisasuperkey.Ifisnotasuperkey,wehavetoverifyifeachattributeiniscontainedinacandidatekeyofRthistestisrathermoreexpensive,sinceitinvolvefindingcandidatekeystestingfor3NFhasbeenshowntobeNP-hardInterestingly,decompositionintothirdnormalform(describedshortly)canbedoneinpolynomialtime
3NFDecompositionAlgorithmLetFcbeacanonicalcoverforF;i:=0;foreachfunctionaldependencyinFcdoifnoneoftheschemasRj,1jicontainsthenbegini:=i+1;Ri:=endifnoneoftheschemasRj,1jicontainsacandidatekeyforRthenbegini:=i+1;Ri:=anycandidatekeyforR;end/*Optionally,removeredundantrelations*/repeatifanyschemaRjiscontainedinanotherschemaRkthen/*deleteRj*/Rj=R;;i=i-1;return(R1,R2,...,Ri)
3NFDecompositionAlgorithm(Cont.)Abovealgorithmensures:eachrelationschemaRiisin3NFdecompositionisdependencypreservingandlossless-joinProofofcorrectnessisatendofthispresentation(clickhere)
3NFDecomposition:AnExampleRelationschema:cust_banker_branch=(customer_id,employee_id,branch_name,type)Thefunctionaldependenciesforthisrelationschemaare:customer_id,employee_idbranch_name,typeemployee_idbranch_namecustomer_id,branch_nameemployee_idWefirstcomputeacanonicalcoverbranch_nameisextraneousinther.h.s.ofthe1stdependencyNootherattributeisextraneous,sowegetFC=customer_id,employee_idtypeemployee_idbranch_namecustomer_id,branch_nameemployee_id
3NFDecompsitionExample(Cont.)Theforloopgeneratesfollowing3NFschema:(customer_id,employee_id,type)(employee_id,branch_name)(customer_id,branch_name,employee_id)Observethat(customer_id,employee_id,type)containsacandidatekeyoftheoriginalschema,sonofurtherrelationschemaneedsbeaddedAtendofforloop,detectanddeleteschemas,suchas(employee_id,branch_name),whicharesubsetsofotherschemasresultwillnotdependontheorderinwhichFDsareconsideredTheresultantsimplified3NFschemais:(customer_id,employee_id,type)(customer_id,branch_name,employee_id)
ComparisonofBCNFand3NFItisalwayspossibletodecomposearelationintoasetofrelationsthatarein3NFsuchthat:thedecompositionislosslessthedependenciesarepreservedItisalwayspossibletodecomposearelationintoasetofrelationsthatareinBCNFsuchthat:thedecompositionislosslessitmaynotbepossibletopreservedependencies.
DesignGoalsGoalforarelationaldatabasedesignis:BCNF.Losslessjoin.Dependencypreservation.Ifwecannotachievethis,weacceptoneofLackofdependencypreservationRedundancyduetouseof3NFInterestingly,SQLdoesnotprovideadirectwayofspecifyingfunctionaldependenciesotherthansuperkeys.CanspecifyFDsusingassertions,buttheyareexpensivetotest,(andcurrentlynotsupportedbyanyofthewidelyuseddatabases!)Evenifwehadadependencypreservingdecomposition,usingSQLwewouldnotbeabletoefficientlytestafunctionaldependencywhoselefthandsideisnotakey.
MultivaluedDependenciesSupposewerecordnamesofchildren,andphonenumbersforinstructors:inst_child(ID,child_name)inst_phone(ID,phone_number)Ifweweretocombinetheseschemastogetinst_info(ID,child_name,phone_number)Exampledata:(99999,David,512-555-1234)(99999,David,512-555-4321)(99999,William,512-555-1234)(99999,William,512-555-4321)ThisrelationisinBCNFWhy?
MultivaluedDependencies(MVDs)LetRbearelationschemaandletRandR.ThemultivalueddependencyholdsonRifinanylegalrelationr(R),forallpairsfortuplest1andt2inrsuchthatt1[]=t2[],thereexisttuplest3andt4inrsuchthat:t1[]=t2[]=t3[]=t4[]t3[]=t1[]t3[R–]=t2[R–]t4[]=t2[]t4[R–]=t1[R–]
MVD(Cont.)Tabularrepresentationof
ExampleLetRbearelationschemawithasetofattributesthatarepartitionedinto3nonemptysubsets.Y,Z,WWesaythatYZ(YmultideterminesZ)ifandonlyifforallpossiblerelationsr(R)randrthenrandrNotethatsincethebehaviorofZandWareidenticalitfollowsthatYZifYW
Example(Cont.)Inourexample:IDchild_nameIDphone_numberTheaboveformaldefinitionissupposedtoformalizethenotionthatgivenaparticularvalueofY(ID)ithasassociatedwithitasetofvaluesofZ(child_name)andasetofvaluesofW(phone_number),andthesetwosetsareinsomesenseindependentofeachother.Note:IfYZthenYZIndeedwehave(inabovenotation)Z1=Z2Theclaimfollows.
UseofMultivaluedDependenciesWeusemultivalueddependenciesintwoways:1.Totestrelationstodeterminewhethertheyarelegalunderagivensetoffunctionalandmultivalueddependencies2.Tospecifyconstraintsonthesetoflegalrelations.Weshallthusconcernourselvesonlywithrelationsthatsatisfyagivensetoffunctionalandmultivalueddependencies.Ifarelationrfailstosatisfyagivenmultivalueddependency,wecanconstructarelationsrthatdoessatisfythemultivalueddependencybyaddingtuplestor.
TheoryofMVDsFromthedefinitionofmultivalueddependency,wecanderivethefollowingrule:If,thenThatis,everyfunctionaldependencyisalsoamultivalueddependencyTheclosureD+ofDisthesetofallfunctionalandmultivalueddependencieslogicallyimpliedbyD.WecancomputeD+fromD,usingtheformaldefinitionsoffunctionaldependenciesandmultivalueddependencies.Wecanmanagewithsuchreasoningforverysimplemultivalueddependencies,whichseemtobemostcommoninpracticeForcomplexdependencies,itisbettertoreasonaboutsetsofdependenciesusingasystemofinferencerules(seeAppendixC).
FourthNormalFormArelationschemaRisin4NFwithrespecttoasetDoffunctionalandmultivalueddependenciesifforallmultivalueddependenciesinD+oftheform,whereRandR,atleastoneofthefollowinghold:istrivial(i.e.,or=R)isasuperkeyforschemaRIfarelationisin4NFitisinBCNF
RestrictionofMultivaluedDependenciesTherestrictionofDtoRiisthesetDiconsistingofAllfunctionaldependenciesinD+thatincludeonlyattributesofRiAllmultivalueddependenciesoftheform(Ri)whereRiandisinD+
4NFDecompositionAlgorithmresult:={R};done:=false;computeD+;LetDidenotetherestrictionofD+toRiwhile(notdone)if(thereisaschemaRiinresultthatisnotin4NF)thenbeginletbeanontrivialmultivalueddependencythatholdsonRisuchthatRiisnotinDi,and;result:=(result-Ri)(Ri-)(,);endelsedone:=true;Note:eachRiisin4NF,anddecompositionislossless-join
ExampleR=(A,B,C,G,H,I)F={ABBHICGH}Risnotin4NFsinceABandAisnotasuperkeyforRDecompositiona)R1=(A,B)(R1isin4NF)b)R2=(A,C,G,H,I)(R2isnotin4NF,decomposeintoR3andR4)c)R3=(C,G,H)(R3isin4NF)d)R4=(A,C,G,I)(R4isnotin4NF,decomposeintoR5andR6)ABandBHIAHI,(MVDtransitivity),andandhenceAI(MVDrestrictiontoR4)e)R5=(A,I)(R5isin4NF)f)R6=(A,C,G)(R6isin4NF)
FurtherNormalFormsJoindependenciesgeneralizemultivalueddependenciesleadtoproject-joinnormalform(PJNF)(alsocalledfifthnormalform)Aclassofevenmoregeneralconstraints,leadstoanormalformcalleddomain-keynormalform.Problemwiththesegeneralizedconstraints:arehardtoreasonwith,andnosetofsoundandcompletesetofinferencerulesexists.Hencerarelyused
OverallDatabaseDesignProcessWehaveassumedschemaRisgivenRcouldhavebeengeneratedwhenconvertingE-Rdiagramtoasetoftables.Rcouldhavebeenasinglerelationcontainingallattributesthatareofinterest(calleduniversalrelation).NormalizationbreaksRintosmallerrelations.Rcouldhavebeentheresultofsomeadhocdesignofrelations,whichwethentest/converttonormalform.
ERModelandNormalizationWhenanE-Rdiagramiscarefullydesigned,identifyingallentitiescorrectly,thetablesgeneratedfromtheE-Rdiagramshouldnotneedfurthernormalization.However,inareal(imperfect)design,therecanbefunctionaldependenciesfromnon-keyattributesofanentitytootherattributesoftheentityExample:anemployeeentitywithattributesdepartment_nameandbuilding,andafunctionaldependencydepartment_namebuildingGooddesignwouldhavemadedepartmentanentityFunctionaldependenciesfromnon-keyattributesofarelationshipsetpossible,butrare---mostrelationshipsarebinary
DenormalizationforPerformanceMaywanttousenon-normalizedschemaforperformanceForexample,displayingprereqsalongwithcourse_id,andtitlerequiresjoinofcoursewithprereqAlternative1:UsedenormalizedrelationcontainingattributesofcourseaswellasprereqwithallaboveattributesfasterlookupextraspaceandextraexecutiontimeforupdatesextracodingworkforprogrammerandpossibilityoferrorinextracodeAlternative2:useamaterializedviewdefinedascourseprereqBenefitsanddrawbackssameasabove,exceptnoextracodingworkforprogrammerandavoidspossibleerrors
OtherDesignIssuesSomeaspectsofdatabasedesignarenotcaughtbynormalizationExamplesofbaddatabasedesign,tobeavoided:Insteadofearnings(company_id,year,amount),useearnings_2004,earnings_2005,earnings_2006,etc.,allontheschema(company_id,earnings).AboveareinBCNF,butmakequeryingacrossyearsdifficultandneedsnewtableeachyearcompany_year(company_id,earnings_2004,earnings_2005,earnings_2006)AlsoinBCNF,butalsomakesqueryingacrossyearsdifficultandrequiresnewattributeeachyear.Isanexampleofacrosstab,wherevaluesforoneattributebecomecolumnnamesUsedinspreadsheets,andindataanalysistools
ModelingTemporalDataTemporaldatahaveanassociationtimeintervalduringwhichthedataarevalid.AsnapshotisthevalueofthedataataparticularpointintimeSeveralproposalstoextendERmodelbyaddingvalidtimetoattributes,e.g.,addressofaninstructoratdifferentpointsintimeentities,e.g.,timedurationwhenastudententityexistsrelationships,e.g.,timeduringwhichaninstructorwasassociatedwithastudentasanadvisor.ButnoacceptedstandardAddingatemporalcomponentresultsinfunctionaldependencieslikeIDstreet,citynottohold,becausetheaddressvariesovertimeAtemporalfunctionaldependencyXYholdsonschemaRifthefunctionaldependencyXYholdsonallsnapshotsforalllegalinstancesr(R).t
ModelingTemporalData(Cont.)Inpractice,databasedesignersmayaddstartandendtimeattributestorelationsE.g.,course(course_id,course_title)isreplacedbycourse(course_id,course_title,start,end)Constraint:notwotuplescanhaveoverlappingvalidtimesHardtoenforceefficientlyForeignkeyreferencesmaybetocurrentversionofdata,ortodataatapointintimeE.g.,studenttranscriptshouldrefertocourseinformationatthetimethecoursewastaken
EndofChapter
ProofofCorrectnessof3NFDecompositionAlgorithm
Correctnessof3NFDecompositionAlgorithm3NFdecompositionalgorithmisdependencypreserving(sincethereisarelationforeveryFDinFc)DecompositionislosslessAcandidatekey(C)isinoneoftherelationsRiindecompositionClosureofcandidatekeyunderFcmustcontainallattributesinR.FollowthestepsofattributeclosurealgorithmtoshowthereisonlyonetupleinthejoinresultforeachtupleinRi
Correctnessof3NFDecompositionAlgorithm(Cont’d.)Claim:ifarelationRiisinthedecompositiongeneratedbytheabovealgorithm,thenRisatisfies3NF.LetRibegeneratedfromthedependencyLetBbeanynon-trivialfunctionaldependencyonRi.(WeneedonlyconsiderFDswhoseright-handsideisasingleattribute.)Now,Bcanbeineitherorbutnotinboth.Considereachcaseseparately.
Correctnessof3NFDecomposition(Cont’d.)Case1:IfBin:Ifisasuperkey,the2ndconditionof3NFissatisfiedOtherwisemustcontainsomeattributenotinSinceBisinF+itmustbederivablefromFc,byusingattributeclosureon.Attributeclosurenothaveused.Ifithadbeenused,mustbecontainedintheattributeclosureof,whichisnotpossible,sinceweassumedisnotasuperkey.Now,using(-{B})andB,wecanderiveB(since,andBsinceBisnon-trivial)Then,Bisextraneousintheright-handsideof;whichisnotpossiblesinceisinFc.Thus,ifBisinthenmustbeasuperkey,andthesecondconditionof3NFmustbesatisfied.
Correctnessof3NFDecomposition(Cont’d.)Case2:Bisin.Sinceisacandidatekey,thethirdalternativeinthedefinitionof3NFistriviallysatisfied.Infact,wecannotshowthatisasuperkey.Thisshowsexactlywhythethirdalternativeispresentinthedefinitionof3NF.Q.E.D.
Figure8.02
Figure8.03
Figure8.04
Figure8.05
Figure8.06
Figure8.14
Figure8.15
Figure8.17'
您可能关注的文档
- 数据库系统概念全套配套课件PPT ch1.ppt
- 数据库系统概念全套配套课件PPT ch25.ppt
- 数据库系统概念全套配套课件PPT ch24.ppt
- 数据库系统概念全套配套课件PPT ch17.ppt
- 数据库系统概念全套配套课件PPT ch20.ppt
- 数据库系统概念全套配套课件PPT ch19.ppt
- 数据库系统概念全套配套课件PPT ch11.ppt
- 数据库系统概念全套配套课件PPT ch14.ppt
- 数据库系统概念全套配套课件PPT ch10.ppt
- 数据库系统概念全套配套课件PPT appC.ppt
- 土木工程材料第2版柯国军配套教学课件PPT 03土木工程材料.ppt
- 土木工程材料第2版柯国军配套教学课件PPT 02土木工程材料.ppt
- 土木工程材料第2版柯国军配套教学课件PPT 15土木工程材料.ppt
- 新编经济法高职财经大类专业基础课 配套教学课件PPT 第四章.ppt
- 新编经济法高职财经大类专业基础课 配套教学课件PPT 第三章.ppt
- 新编经济法高职财经大类专业基础课 配套教学课件PPT 第二章.ppt
- 新能源汽车概论崔胜民韩家军配套教学课件PPT 新能源汽车概论4.ppt
- 新能源汽车概论崔胜民韩家军配套教学课件PPT 新能源汽车概论2.ppt