13
FCT-NOVA - Bases de Dados 2016/2017 Ficha2 –Solutions Functional Dependencies and Normalization – Exercises Group 1. Considere a seguinte instância de relação no esquema R=(A, B, C, D, E): Consider the following instance of the relation schema R=(A, B, C, D, E): A B C D E a1 b1 c1 d1 e1 a1 b1 c1 d2 e1 a3 b2 c1 d2 e2 a2 b2 c3 d3 e2 Para cada uma das seguintes dependências funcionais explique porque é que ela é (ou não) satisfeita pela instância referida. For each of the following functional dependencies explain why it is (or it is not) satisfied by the same instance. 1. DC é satisfeita porque para quaisquer dois tuplos onde D tenha o mesmo valor, C não varia o seu valor, isto é is satisfied because for any two tuples where D has the same value, C does not change its values, that is 2. CD não é satisfeita porque para diferentes tuplos onde C=c1, D=d1 ou D=d2. is not satisfied because for different tuples where C=c1, D=d1 or D=d2. 3. EC não é satisfeita porque para diferentes tuplos onde E=e2, C=c1 ou C=c3. is not satisfied because for different tuples where E=e2, C=c1 or C=c3. 4. BA não é satisfeita porque para diferentes tuplos onde B=b2, A=a2 ou A=a3. is not satisfied because for different tuples where B=b2, A=a2 or A=a3. 5. AE é satisfeita porque para quaisquer dois tuplos onde A tenha o mesmo valor, E não varia o seu valor. is satisfied because for any two tuples where A has the same value, E does not change its values. 6. BCA é satisfeita porque para quaisquer dois tuplos onde BC tenha o mesmo valor, A não varia o seu valor. is satisfied because for any two tuples where BC has the same value, A does not change its values. 7. ABD não é satisfeita porque para diferentes tuplos onde AB=a1b1, D=d1 ou D=d2. is not satisfied because for different tuples where AB=a1b1, D=d1 or D=d2. 8. ABDE não é satisfeita porque para diferentes tuplos onde AB=a1b1, DE=d1e1 ou DE=d2e1. is not satisfied because for different tuples where AB=a1b1, DE=d1e1 or DE=d2e1 9. BDB é satisfeita porque para diferentes tuplos onde BD tem o mesmo valor, B não varia o seu valor. De facto, para diferentes tuplos, BD apresenta valores diferentes. is satisfied because for any two tuples where BD has the same value, B does not change its values. In fact, for different tuples, BD presents different values.

FCT-NOVA - Bases de Dados 2016/2017 Ficha2 –Solutions …bdu.ssdi.di.fct.unl.pt/resources/ProgFile/bd16_17Ficha2... · 2020-05-11 · a3 b2 c1 d2 e2 a2 b2 c3 d3 e2 Para cada uma

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: FCT-NOVA - Bases de Dados 2016/2017 Ficha2 –Solutions …bdu.ssdi.di.fct.unl.pt/resources/ProgFile/bd16_17Ficha2... · 2020-05-11 · a3 b2 c1 d2 e2 a2 b2 c3 d3 e2 Para cada uma

FCT-NOVA-BasesdeDados

2016/2017

Ficha2–Solutions

FunctionalDependenciesandNormalization–Exercises

Group1.

ConsidereaseguinteinstânciaderelaçãonoesquemaR=(A,B,C,D,E):

ConsiderthefollowinginstanceoftherelationschemaR=(A,B,C,D,E):

A B C D Ea1 b1 c1 d1 e1a1 b1 c1 d2 e1a3 b2 c1 d2 e2a2 b2 c3 d3 e2

Paracadaumadasseguintesdependênciasfuncionaisexpliqueporqueéqueelaé(ounão)satisfeitapelainstânciareferida.

Foreachofthefollowingfunctionaldependenciesexplainwhyitis(oritisnot)satisfiedbythesameinstance.

1. D→C ésatisfeitaporqueparaquaisquerdoistuplosondeDtenhaomesmovalor,Cnãovariaoseuvalor,istoé issatisfiedbecauseforanytwotupleswhereDhasthesamevalue,Cdoesnotchangeitsvalues,thatis

2. C→D nãoésatisfeitaporqueparadiferentestuplosondeC=c1,D=d1ouD=d2.isnotsatisfiedbecausefordifferenttupleswhereC=c1,D=d1orD=d2.

3. E→C nãoésatisfeitaporqueparadiferentestuplosondeE=e2,C=c1ouC=c3.isnotsatisfiedbecausefordifferenttupleswhereE=e2,C=c1orC=c3.

4. B→A nãoésatisfeitaporqueparadiferentestuplosondeB=b2,A=a2ouA=a3.isnotsatisfiedbecausefordifferenttupleswhereB=b2,A=a2orA=a3.

5. A→E ésatisfeitaporqueparaquaisquerdoistuplosondeAtenhaomesmovalor,Enãovariaoseuvalor.

issatisfiedbecauseforanytwotupleswhereAhasthesamevalue,Edoesnotchangeitsvalues.

6. BC→A ésatisfeitaporqueparaquaisquerdoistuplosondeBCtenhaomesmovalor,Anãovariaoseuvalor.

issatisfiedbecauseforanytwotupleswhereBChasthesamevalue,Adoesnotchangeitsvalues.

7. AB→D nãoésatisfeitaporqueparadiferentestuplosondeAB=a1b1,D=d1ouD=d2.isnotsatisfiedbecausefordifferenttupleswhereAB=a1b1,D=d1orD=d2.

8. AB→DEnãoésatisfeitaporqueparadiferentestuplosondeAB=a1b1,DE=d1e1ouDE=d2e1.

isnotsatisfiedbecausefordifferenttupleswhereAB=a1b1,DE=d1e1orDE=d2e19. BD→B ésatisfeitaporqueparadiferentestuplosondeBDtemomesmovalor,Bnão

variaoseuvalor.Defacto,paradiferentestuplos,BDapresentavaloresdiferentes. issatisfiedbecauseforanytwotupleswhereBDhasthesamevalue,Bdoesnotchangeitsvalues.Infact,fordifferenttuples,BDpresentsdifferentvalues.

Page 2: FCT-NOVA - Bases de Dados 2016/2017 Ficha2 –Solutions …bdu.ssdi.di.fct.unl.pt/resources/ProgFile/bd16_17Ficha2... · 2020-05-11 · a3 b2 c1 d2 e2 a2 b2 c3 d3 e2 Para cada uma

10. C→E nãoéatisfeitaporqueparadiferentestuplosondeCtemomesmovalor,Enãovariaoseuvalor:quandoC=c1,Epodesere1oue2.ItisnotsatisfiedbecauseforsometwotupleswhereChasthesamevalue,Echangesitsvalues:whenC=c1,Ecanbee1oue2.

Group2.

ConsidereoesquemaR=(A,B,C,D,E)eoseguinteconjuntodedependênciasfuncionaisF,verdadeirasemR:

ConsidertheschemaR=(A,B,C,D,E)andthefollowingsetoffunctionaldependenciesFwhicharetrueinR:

UsandoosaxiaomasdeArmstrong(Reflexividade,TransitividadeeAumento)bemcomoasregrasderivadasUnião,DecomposiçãoePseudotransitividade,mostrequeF+,ofechodeF,contemcadaumadasseguintesdependênciasfuncionais.Paracadacaso,mostrequalaregrausadaemcadapasso.

ByusingtheArmstrong’saxioms(Reflexivity,TransitivityandAugmentation)andalsoderivedrulesUnion,DecompositionandPseudotransitivity,showthatF+,theclosureofF,containseachofthefollowingfunctionaldependencies.Foreachcase,showwhatwastheruleusedineachstep.

1. E→A porque/because

2. D→C porque/because

3. B→A porque/because

4. EB→C porque/because

5. ABC→A porque/because

Group3.

UsandooconjuntoFdedependênciasfuncionaisdoexercícioanterior,encontreofechodosseguintesconjuntosdeatributos.Faça-opasso-a-passoindicandoqualadependênciafunctionalusadaemcadacaso.

UsingthefunctionaldependenciesFofthepreviousgroup,findtheclosureofthefollowingsetsofattributes.Doitstep-by-stepexplainingwhatwasthefunctionaldependencyusedineachstep.

1. B solução/solution: porque/becasue sendoque/andporquenenhumoutrosubconjutode/nosubsetofBAéparte

esquerdadeumadep.Func.de/isleftpartofaFunc.Dep.ofFdeformaa

Page 3: FCT-NOVA - Bases de Dados 2016/2017 Ficha2 –Solutions …bdu.ssdi.di.fct.unl.pt/resources/ProgFile/bd16_17Ficha2... · 2020-05-11 · a3 b2 c1 d2 e2 a2 b2 c3 d3 e2 Para cada uma

queasuapartedireitaacrescentenovosatributosaofechode/soitsrightpartcanaddnewattributestotheclosureofB.

2. A solução/solution: porque/because (portransitividade/bytransitivity)enãoháDep.Func.em/andthereisnoFuncDepinFtalqueAsejasuaparteesquerda/suchthatAisitsleftpart.

3. EB solução/solution: porRefl./byrefl.; porque/because ; porque/because ;

porque/because ;porfim/finallyporquenenhumoutrosubconjutode/nosubsetof

EBDACéparteesquerdadeumadep.Func.de/isleftpartofaFunc.Dep.ofFdeformaaqueasuapartedireitaacrescentenovosatributosaofechode/soitsrightpartcanaddnewattributestotheclosureofEB.

Group4.

ConsidereoesquemaR=(A,B,C,D,E)eoseguinteconjuntodedependênciasfuncionaisFverdadeiroemR:

ConsideraschemaR=(A,B,C,D,E)andthefollowingsetoffunctionaldependenciesFwhichistrueinR:

Paracadaumadasseguintesdependênciasfuncionais,verifiquesepertenceaF+.

Foreachofthefollowingfunctionaldependencies,checkifitbelongstoF+.

1. ED→B yesbecause umavezque/since

porque/because ; porque/because ; porque/because ;porfim/finally porquenenhumoutrosubconjutode/nosubsetofEDCABéparteesquerdadeumadep.Func.de/isleftpartofaFunc.Dep.ofFdeformaaqueasuapartedireitaacrescentenovosatributosaofechode/soitsrightpartcanaddnewattributestotheclosureofED.

2. EC→B yesbecause

umavezque/since porque/because ;porque/because ;porfim/finallyporquenenhumoutrosubconjutode/nosubsetofECAB

éparteesquerdadeumadep.Func.de/isleftpartofaFunc.Dep.ofFdeformaaqueasuapartedireitaacrescentenovosatributosaofechode/soitsrightpartcanaddnewattributestotheclosureofEC.

3. E→C nobecause

umavezque/since porque/because ;porfim/finallyporquenenhumoutrosubconjutode/nosubsetofEAéparte

esquerdadeumadep.Func.de/isleftpartofaFunc.Dep.ofFdeformaa

Page 4: FCT-NOVA - Bases de Dados 2016/2017 Ficha2 –Solutions …bdu.ssdi.di.fct.unl.pt/resources/ProgFile/bd16_17Ficha2... · 2020-05-11 · a3 b2 c1 d2 e2 a2 b2 c3 d3 e2 Para cada uma

queasuapartedireitaacrescentenovosatributosaofechode/soitsrightpartcanaddnewattributestotheclosureofE.

4. E→D nobecause umavezque/sinceporque/because ;porfim/finally porquenenhumoutrosubconjutode/nosubsetofEAéparteesquerdadeumadep.Func.de/isleftpartofaFunc.Dep.ofFdeformaaqueasuapartedireitaacrescentenovosatributosaofechode/soitsrightpartcanaddnewattributestotheclosureofE.

5. DA→B nobecause porquenenhumsubconjutode/nosubsetofDAéparteesquerdadeumadep.Func.de/isleftpartofaFunc.Dep.ofFdeformaaqueasuapartedireitaacrescentenovosatributosaofechode/soitsrightpartcanaddnewattributestotheclosureofDA.

Group5.

EncontreumacoberturacanónicaFcparacadaesquemaReconjuntodedependênciasfuncionaisF:

FindacanonicalcoverFcforeachschemaRandsetoffunctionaldependenciesF:

1. R=(A,B,C,D,E) and Solução:umavezqueoatributoGéextraemαseG αeFimplicalogicamente(F–{α→β}) {(α-G)→β}eoattributoGéextraemβseG βeoconjuntodedependências(F–{α→β}) (α→(β-G)}implicalogicamenteF

Solution:sinceattributeGisextraneousinαifG αandFlogicallyimplies(F–{α→β})

{(α-G)→β}andattributeGisextraneousinβifG βandthesetofdependencies(F–{α→β})(α→(β-G)}logicallyimpliesFentão/then:

DéextraemED→CBporque

DisextraneousinED→CBbecause

DonderesultaqueResultingin MasBéextraemDA→BEporqueButBisextraneousinDA→BEbecause

Page 5: FCT-NOVA - Bases de Dados 2016/2017 Ficha2 –Solutions …bdu.ssdi.di.fct.unl.pt/resources/ProgFile/bd16_17Ficha2... · 2020-05-11 · a3 b2 c1 d2 e2 a2 b2 c3 d3 e2 Para cada uma

DonderesultaqueSo,resultingin MasBéextraemE→CBporqueButBisextraneousinE→CBbecause

DonderesultaqueSo,resultingin .Nãohámaisatributosextra.Agora,umavezquecadaparteesquerdadumadependênciafuncionaldeveserúnicanumacoberturacanónica(Fc),entãoTherearenomoreextraneousattributes.Now,sinceeachleftsideoffunctionaldependencymustbeuniqueinacanonicalcover(Fc),then

2. R=(A,B,C,D,E,G) and

DéextraemEDB→CAporque

DisextraneousinEDB→CAbecause

DonderesultaqueSo,resultingin .MasAéextraemEB→CAporqueButAisextraneousinEB→CAbecause

DonderesultaqueSo,resultingin

3. R=(A,B,C,D,E,G,H)and

Aotornarúnicocadaladoesquerdonoconjuntodasdependênciasfuncionais,Bymakinguniqueeachleftsideofthefunctionaldependencies,

MasGéextraemEC→GDporqueButGisextraneousinEC→GDbecause

Page 6: FCT-NOVA - Bases de Dados 2016/2017 Ficha2 –Solutions …bdu.ssdi.di.fct.unl.pt/resources/ProgFile/bd16_17Ficha2... · 2020-05-11 · a3 b2 c1 d2 e2 a2 b2 c3 d3 e2 Para cada uma

Donderesultaque

So,resultingin

MasDéextraemCG→DBporqueButDisextraneousinCG→DBbecause

DonderesultaqueSo,resultingin

MasGéextraemCA→EGporque

ButGisextraneousinCA→EGbecause

Donderesultaque

So,resultingin

Group6.

Considerthefollowingschemawhichcontainsinformationaboutalibrary:

Library=(Book,Title,ISBN,Copy,Branch,Time,Reader,DeliveryDate)

1. Assumindoquecadaumadasseguintesdependênciasfuncionaissãoverdadeirasnoesquema,dêumainterpretaçãoemlingualnaturalparacadauma.Giventhateachofthefollowingfunctionaldependenciesaretrueintheschema,giveaninterpretationinnaturallanguageforeachone.

Page 7: FCT-NOVA - Bases de Dados 2016/2017 Ficha2 –Solutions …bdu.ssdi.di.fct.unl.pt/resources/ProgFile/bd16_17Ficha2... · 2020-05-11 · a3 b2 c1 d2 e2 a2 b2 c3 d3 e2 Para cada uma

a. Book→Title Cadalivrotemapenasumtítulo/eachbookhasjustonetitle

b. Book→ISBN CadalivrotemapenasumISBN/eachbookhasjustoneISBN

c. ISBN→Book AcadaISBNcorrespondeapenasumlivro/ToeachISBNthereisjustacorrespondingbook

d. Copy→Book Capacópiatempretenceapenasaumlivro/eachcopybelongstojustonebook

e. Copy→Branch Cadacópiaestáapenasnumasucursal/Eachcopyisinjustonebranch

f. Branch,Book→Copy Emcadasucursalháapenasumacópiadecadalivro/Ineachbranchthereisonlyacopyforeachbook

g. Copy,Time→Reader Numdadomomento,cadacópiapodeapenasserrequeridaporumsócio/Inagivenmoment,eachcopycanonlyberequestedbyareader

h. Branch,Book→DeliveryDate Paraumlivronumasucursal,háapenasumadatadeentrega/Forabookinabranch,thereisonlyadeliverydate

2. EncontreaschavescandidatasdoesquemaLibrary.FindthecandidateKeysofLibrary.

Umachaveéumconjuntominimaldeattributoscujofechocontémtodososatributosdoesquema.Assim,comoTimenuncaocorreàdireitaemqualquerdasDFsdalista,elatemdefazerpartedachave.Aschavessão{Time,copy},{Time,Book,Branch}e{Time,ISBN,Branch}.

Akeyisaminimalsetofattributeswhoseclosurecontainsallattributesoftheschema.So,asTimeneveroccursontherightsideofanyfunctionaldependencyofthelist,ithastobepartofanykey.Thekeysare{Time,copy},{Time,Book,Branch}and{Time,ISBN,Branch}.

3. SehouveralgumasdependênciafunctionalqueviolemaBCNFconsiderandooesquemaLibrary,encontre-as.IfthereareanyfunctionaldependenciesthatviolateBCNFconsideringLibraryschema,findthem.

TodasasDfsexcepto(Copy,Time)→ReaderviolamaBCNF,umavezque(Copy,Time)éumadaschaves.Everyfunctionaldependencyexcept(Copy,Time)→ReaderviolatestheBCNF,since(Copy,Time)isoneofthekeys.

4. Sehouveralgumasdependênciafunctionalqueviolema3FNconsiderandooesquemaLibrary,encontre-as.Iftherearefunctionaldependenciesthatviolates3FNconsideringLibraryschema,findthem.

ApenasBook→Titlee(Branch,Book)→DeliveryDateviolamumavezquenestasDfs,ambasaspartesesquerdasnãosãochaveseassuapartesdireitas(Title)e(DeliveryDate)nãosãopartedenenhumachavecandidata.OnlyBook→Titleand(Branch,Book)→DeliveryDatesinceintheseDfs,bothleftpartsarenotkeysandtheirrightparts(Title)and(DeliveryDate)arenotpartofanykey.

5. SeLibrarynãoestánaBCNF,decomponha-anumconjuntodeesquemasqueestejamnessaforma(BCNF).Econtreoutradecomposiçãonamesmaformaemostrequaldelaspreserva(ounão)asdependênciasfuncionais.IfLibraryisnotinBCNF,decomposeitinasetofschemasthatareinBCNF.FindanotherBCNFdecompositionandshowwhichofthempreserve(ornot)thefunctionaldependencies.

Page 8: FCT-NOVA - Bases de Dados 2016/2017 Ficha2 –Solutions …bdu.ssdi.di.fct.unl.pt/resources/ProgFile/bd16_17Ficha2... · 2020-05-11 · a3 b2 c1 d2 e2 a2 b2 c3 d3 e2 Para cada uma

PorsimplificaçãodoconjuntodeDfs,podemosobterBook→(Title,ISBN)união(Un)deBook→TitleeBook→ISBN.ECopy→(Branch,Book)poruniãodeCopy→BrancheCopy→Book,e(Brunch,Book)→(Copy,DeliveryDate)tambémporunião(verDfs).Assim,F2={Book→(Title,ISBN),ISBN→Book,Copy→(Branch,Book),(Branch,Book)→(Copy,DeliveryDate),(Copy,Time)→Reader}BysimplifyingthesetofDfs,wecangetBook→(Title,ISBN)byunion(Un)ofBook→TitleandBook→ISBN.AndCopy→(Branch,Book)byunionofCopy→BranchandCopy→Book,and(Brunch,Book)→(Copy,DeliveryDate)alsobyunion(seeDfs).SoF2={Book→(Title,ISBN),ISBN→Book,Copy→(Branch,Book),(Branch,Book)→(Copy,DeliveryDate),(Copy,Time)→Reader}E,peloalgoritmodedecomposiçãoemBCNF(slide7.16),usandoBook→(Title,ISBN)obtem-se,R1=(Book,Title,ISBN)eR2’=(Book,Copy,Branch,DeliveryDate,Time,Reader).Agora,umavezqueCopy→(Branch,Book)impedeR2’deestarnaBCNF,seestaDffôrusada,obte-seR2=(Copy,Branch,Book)eR3’=(Copy,DeliveryDate,Time,Reader),masR3’nãoestánaBCNFporqueCopy→DeliveryDate,quepertenceaF+,nãoopermite---repareque

And,bytheBCNFalgorithm(slide7.16),usingBook→(Title,ISBN)weget,R1=(Book,Title,ISBN)andR2’=(Book,Copy,Branch,DeliveryDate,Time,Reader).Now,sinceCopy→(Branch,Book)preventsR2’ofbeinginBCNF,ifthisDfisused,wegetR2=(Copy,Branch,Book)andR3’=(Copy,DeliveryDate,Time,Reader),butR3’isnotinBCNFbecauseCopy→DeliveryDate,whichbelongstoF+,preventsit---noticethat

.Agora,usandoCopy→DeliveryDate,juntamosR3eR4aR1eR2,ondeR3=(Copy,DeliveryDate)eR4=(Copy,Time,Reader),estandoR1,R2,R3eR4inBCNF.

.NowusingCopy→DeliveryDate,weaddR3andR4toR1andR2,whereR3=(Copy,DeliveryDate)andR4=(Copy,Time,Reader),beingR1,R2,R3andR4inBCNF.UmaoutradecomposiçãoBCNFpodeserencontradaconsiderando,porexemplo,umconjuntoconjuntocanónicodeDfs,Fc={Book→(Title,ISBN),ISBN→Book,Copy→(Branch,Book,DeliveryDate),(Branch,Book)→Copy,(Copy,Time)→Reader}.Assim,usandoBook→(Title,ISBN)obtem-seR1=(Book,Title,ISBN)eR2’={Book,Copy,Branch,DeliveryDate,Time,Reader}.MasCopy→(Branch,Book,DeliveryDate)impedeR2’deestarnaBCNFBCNFporqueofechodeCopy(Copy+)nãoincluiReader.Assim,usandoestaDfobtem-seR2=(Copy,Branch,Book,DeliveryDate)eosrestantesatributosformamR3={Copy,Time,Reader},estandoR1,R2eR3naBCNF,formandooutradecomposiçãoBCNF---reparequeumavezque(Branch,Book)+contemtodososatributosdeR2,estaDfnãoviolaaBCNFemR2---.Lembre-sequeumesquemaRestánaBCNFseofechodecadasubconjuntodosatributosdeRsatisfazumadasseguintes

Page 9: FCT-NOVA - Bases de Dados 2016/2017 Ficha2 –Solutions …bdu.ssdi.di.fct.unl.pt/resources/ProgFile/bd16_17Ficha2... · 2020-05-11 · a3 b2 c1 d2 e2 a2 b2 c3 d3 e2 Para cada uma

condições:a)contémtodososatributosdeR;b)contemapenasosatributosqueocompõem.

AnotherBCNFdecompositioncanbefindbyconsideringanotherequivalentsetofDfs,forexampleacanonicalsetFc={Book→(Title,ISBN),ISBN→Book,Copy→(Branch,Book,DeliveryDate),(Branch,Book)→Copy,(Copy,Time)→Reader}.Then,byusingBook→(Title,ISBN)wegetR1=(Book,Title,ISBN)andR2’={Book,Copy,Branch,DeliveryDate,Time,Reader}.ButCopy→(Branch,Book,DeliveryDate)preventsR2’frombeinginBCNFbecausetheclosureofCopy(Copy+)doesnotincludeReader.So,byusingthisDfwegetR2=(Copy,Branch,Book,DeliveryDate)andtheremainingattributesformR3={Copy,Time,Reader},beingR1,R2andR3inBCNF,forminganotherBCNFdecomposition---noticethat,since(Branch,Book)+containsallattributesofR2,thisfunctionaldependencydoesnotviolatesBCNFonR2---.RememberthataschemaRisinBCNFiftheclosureofeachsubsetofattributesofRsatisfiesoneofthefollowingconditions:a)itcontainsallattributesofR;b)itcontainsonlythatsubset.Apreservaçãodasdependênciasverifica-sese sendoFioconjuntodeDfsdeF+queincluemapenasattributosemRi.Contudo,háumaabordagemalternativaqueevitaacomputaçãodeF+.Assim,eanalisandoaprimeiracomposiçãoencontrada(R1,R2,R3,R4),umavezque(Branch,Book)→(Copy,DeliveryDate)pretenceaF2enemtodososseus4atributosaparecemnumdosesquemas,háqueverificarseestaDfépreservada,comvistaaumacompletapreservaçãodaDfsnestadecomposição.Assim,usandooalgoritmoparatestarapreservaçãodumaDf(slide71.2),temos: result=(Branch,Book)Ri=R1=(Book,Title,ISBN)t=((Branch,Book) R1)+ R1%t={Book,Title,ISBN)result=result t %result=(Branch,Book,Title,ISBN)Ri=R2=(Copy,Branch,Book)t=((Branch,Book,Title,ISBN) R2)+ R2 %t=(Copy,Branch,Book)

result=result t %result=(Branch,Book,Title,ISBN,Copy) Ri=R3=(Copy,DeliveryDate) t=(Branch,Book,Title,ISBN,Copy) R3)+ R3%t=(Copy,DeliveryDate) result=result t%result=(Branch,Book,Title,ISBN,Copy,Deliverydate) Ri=R4=(Copy,Time,Reader) t=((Branch,Book,Title,ISBN,Copy,DeliveryDate) R3)+ R3%t=Copy result=result t%result=(Branch,Book,Title,ISBN,Copy,Deliverydate)

Assim,tendoemcontaqueresultcontemtodososatributosdoladodireitodaDf,istoé,CopyeDeliveryDate,então(Branch,Book)→(Copy,DeliveryDate)épreservada.UmexercíciosemelhanteparaasoutrasDfsemF2mostrariaqueestadecomposiçãopreservaessasdependências.Damesmaforma,umexercíciosemelhanteparaasDfsnaFcmostrariaqueasegundadecomposiçãotambémpreservaasdependências.Contudo,nestecasoestatarefaseriafacilitadajáquetodososatributosdecadaDfdaFcestãorepresentadosempeloumdosesquemasdessadecomposição,oquenospermiteconcluirquetodasasdependênciassãopreservadas.Dependencypreservationispreservedif beingFithesetof

dependenciesfromF+thatincludeonlyattributesinschemaRi.However,thereisanalternativeapproachwhichavoidscomputingF+.So,andanalyzingthefirstdecomposition(R1,R2,R3,R4),since(Branch,Book)→(Copy,DeliveryDate)belongstoF2andnotalltheirfourattributesappearinasingleschema,wemustverifyifthisDfispreservedforacompletedependencypreservationofthewholedecomposition.Thus,bythealgorithmfortestingaDfpreservation(slide71.2),letuscheckit:

Page 10: FCT-NOVA - Bases de Dados 2016/2017 Ficha2 –Solutions …bdu.ssdi.di.fct.unl.pt/resources/ProgFile/bd16_17Ficha2... · 2020-05-11 · a3 b2 c1 d2 e2 a2 b2 c3 d3 e2 Para cada uma

result=(Branch,Book)Ri=R1=(Book,Title,ISBN)t=((Branch,Book) R1)+ R1%t={Book,Title,ISBN)result=result t %result=(Branch,Book,Title,ISBN)Ri=R2=(Copy,Branch,Book)t=((Branch,Book,Title,ISBN) R2)+ R2 %t=(Copy,Branch,Book)

result=result t %result=(Branch,Book,Title,ISBN,Copy) Ri=R3=(Copy,DeliveryDate) t=(Branch,Book,Title,ISBN,Copy) R3)+ R3%t=(Copy,DeliveryDate) result=result t%result=(Branch,Book,Title,ISBN,Copy,Deliverydate) Ri=R4=(Copy,Time,Reader) t=((Branch,Book,Title,ISBN,Copy,DeliveryDate) R3)+ R3%t=Copy result=result t%result=(Branch,Book,Title,ISBN,Copy,Deliverydate)

So,sinceresultcontainsallright-sideattributesoftheDf,thatis,CopyandDeliveryDate,then(Branch,Book)→(Copy,DeliveryDate)ispreserved.AsimilarexercisefortheotherDfsinF2wouldshowthatthisdecompositionpreservesthedependencies.Also,asimilarexercisefortheDfsinFcwouldshowthattheseconddecompositionpreservesthedependenciestoo.Though,inthiscasethistaskwouldbefacilitatedsinceallattributesofeachDfinFcarerepresentedinatleastoneoftheschemasofthatdecomposition,whichenableustoconcludethatalldependenciesarepreserved.

6. Mesmosenãofôrnecessário,decomponhaLibraryna3FN.Evenifitisnotnecessary,decomposeLibraryin3FN.UmavezqueasdependênciasfuncionaissãopreservadasempelomenosumadasdecomposiçõesBCNF,adecomposição3FNnãooferecevantagem.Noentanto,comoexercício,eisumadecomposição3NFdeLibrary:Assim,peloalgoritmodedecomposiçãopara3NF(slide7.24),podemosusaracoberturacanónicaFcsuadanasegundadecomposiçãoparaBCN,istoé:Fc={Book→(Title,ISBN),ISBN→Book,Copy→(Branch,Book,DeliveryDate),(Branch,Book)→Copy,(Copy,Time)→Reader}Apartirdessealgoritmo,podemosobteraseguintedecomposição3FN:R1=(Book,Title,ISBN),R2=(Copy,Book,Branch,DeliveryDate)andR3={Copy,Time,Reader}.Repare-seque,umavezqueR3containsoneumadaschavescandidatasdoesquemaLibrary,ouseja,(Copy,Time),entãonãohánecessidadedecriarmasinenhumesquema(verslide7.24).SincedependencypreservationwaskeptforatleastoneoftheBCNFdecompositions,3FNoffersnoadvantage.Though,asanexercise,hereisa3NFdecompositionofLibrary:So,bythe3NFdecompositionalgorithm(slide7.24),wemaytakethecanonicalcoverFcusedinthesecondBNCFdecomposition,thatisFc={Book→(Title,ISBN),ISBN→Book,Copy→(Branch,Book,DeliveryDate),(Branch,Book)→Copy,(Copy,Time)→Reader}Bythatalgorithm,wegetthefollowingdecomposition:R1=(Book,Title,ISBN),R2=(Copy,Book,Branch,DeliveryDate)andR3={Copy,Time,Reader}.Noticethat,sinceR3containsoneofthecandidatekeysosLibraryschema,thatis,(Copy,Time),sothereisnoneedofcreatinganotherschema(seeslide7.24).

Page 11: FCT-NOVA - Bases de Dados 2016/2017 Ficha2 –Solutions …bdu.ssdi.di.fct.unl.pt/resources/ProgFile/bd16_17Ficha2... · 2020-05-11 · a3 b2 c1 d2 e2 a2 b2 c3 d3 e2 Para cada uma

Group7.

Deformaaacrescentarinformaçãoacercadalistadecandidatos,foicriadaumesquemacomosseguintesatributos:

Inordertoincreasetheinformationaboutthelistsofcandidates,aschemawascreatedwiththefollowingattributes:

OndeCandidatedenotaonúmerodecidadãodocandidato,PartyeConstituency(círculoeleitoral)significaopartidoearegiãoqueocandidatodesejarepresentarnumalegislatura.OrderNuméonúmerodeordemdocandidatonalistadopartido,andNumDeputieséonúmerodecandidatosaelegernocírculoeleitoral.Otermo“List”significaoconjuntodetodososcandidatosdopartidonumcírculoeleitoral.Alémdisso,tenhaemcontaasseguintesrestrições:

WhereCandidatedenotestheIdentityCardnumberofacandidate,thePartyandtheConstituency(círculoeleitoral)meansthepartyandtheregiontheCandidatewishestorepresentinalegislativebody.OrderNumistheordernumberofCandidateinthelistofParty,andNumDeputiesisthenumberofcandidatestobeelectedinConstituency.Term“list”meansthesetofallcandidatesofapartyinaconstituency.Also,takeintoaccountthefollowingrestrictions:

a) Paracadacírculoeleitoralháumnúmeroúnicodedeputadosaelegerforeachconstituencythereisauniquenumberofdeputiestoelect

b) UmcandidatonãopodeconcorrerpormaisdeumpartidoAcandidadecannotrunformorethanoneparty

c) UmcandidatonãopodeconcorreremmaisdoqueumalistanomesmopartidoAcandidadecannotbelongtomorethanonelistofthesameparty

d) Numadadalista,umcandidatotemapenasumnúmerodeordemInagivenlist,acandidatehasauniqueordernumber

e) NãoépossívelhaverdoiscandidatoscomomesmonúmerodeordemnamesmalistaItisnotpossibletohavetwocandidateswiththesameordernumberinthesamelist

1. Paracadarestriçãoacimaindicada,apresenteumadependênciafunctionalqueaimponha.Foreachoftheaboverestrictionspresentafunctionaldependencythatimposesit.Constituency→NumDeputiesCandidate→Party(Candidate,Party)→Constituency(Candidate,Constituency)→OnderNum(Constituency,OrderNum)→Candidate

2. Tendoemcontaasdependênciasfuncionaisimpostas,encontretodasaschavescandidatas.TakingintoaccountalltheimposedfunctionaldependenciesfindallcandidatekeysofList.Aschavessãoosconjuntosminimaisdeatributoscujofechocontemtodososatributos.Assim,hádoisconjuntos:Keysareminimalsetsofattributeswhoseclosurecontainallattributes.So,therearetwosets:{Candidate}e/and{Constituency,OrderNum}

Page 12: FCT-NOVA - Bases de Dados 2016/2017 Ficha2 –Solutions …bdu.ssdi.di.fct.unl.pt/resources/ProgFile/bd16_17Ficha2... · 2020-05-11 · a3 b2 c1 d2 e2 a2 b2 c3 d3 e2 Para cada uma

3. DêumexemploquemostrequeoesquemaListnãoevitaredundâncias.

Byanexample,showthatschemaListdoesnotavoidredundancies.Osdoisseguintestuplosmostramquearedundâncianãoéevitada:Thefollowingtwotuplesshowthatredundancyisnotavoided:(João,PS,Lisboa,1,30)(Maria,PSD,Lisboa,4,30)Repare-sequesemprequeLisboaestánumtuplo,sabemosque30seráovalordeNumDeputiesdevidoàdependênciaConstituency→NumDeputies.NoticethateverytimeLisboaisinatupleweknowthat30willbethevalueofNumDeputiesduetotheConstituency→NumDeputiesdependency.

4. ObtenhaumadecomposiçãosemperdasdeListnumconjuntodeesquemasqueestejamnaBCNF.ObtainalosslessdecompositionofListinasetofschemaswhichareinBCNF.Umconjuntosimplificadodedependênciasfuncionaisé,porexemplo:Asimplifiedsetoffunctionaldependenciesare,forexample:F2={Candidate→(Constituency,OnderNum,Party),(Constituency,OnderNum)→Candidate,Constituency→NumDeputies}Apartirdoqual,aseguintedecomposiçãopodeserobtidausandooalgoritmodedecomposiçãoem(slide7.16):Fromwhich,thefollowingdecompositioncanbeobtainedbyusingtheBCNFdecompositionalgorithm(slide7.16)R1=(Candidate,Constituency,OnderNum,Party)R2=(Constituency,NumDeputies)E(R1 R2)→R2,queécondiçãosuficienteparaqueadecomposiçãosejasemperdas.And(R1 R2)→R2,whichisasufficientconditionforalosslessdecomposition.

Group8.

(Contemsoluçõesapenasparaaalíneaa)

(Containsonlythesolutionsfora)

ConsidereoesquemaR=(A,B,C,D,E).Paracadaumadasseguintesdependênciasfuncionais:

ConsideraschemaR=(A,B,C,D,E).Foreachofthefollowingsetsoffunctionaldependencies:

a) F= b) c) d) e)

1. EncontreaschavescandidatasFindthecandidatekeys.{ED}

Page 13: FCT-NOVA - Bases de Dados 2016/2017 Ficha2 –Solutions …bdu.ssdi.di.fct.unl.pt/resources/ProgFile/bd16_17Ficha2... · 2020-05-11 · a3 b2 c1 d2 e2 a2 b2 c3 d3 e2 Para cada uma

2. SehouveralgumasdependênciasfuncionaisqueviolemaBCNF,encontre-asIftherearefunctionaldependenciesthatviolatesBCNF,findthem.TodasasDfsem/EveryDfinF= violamaBCNF/violatesBCNF

3. Sehouveralgumasdependênciasfuncionaisqueviolema3NF,encontre-asIftherearefunctionaldependenciesthatviolates3NF,findthem.TodasasDfsem/EveryDfinF= violama3NF/violates3NF

4. SeRnãoestivernaBCNF,decomponha-anumconjuntodeesquemasqueestejamnessaforma.VerifiquequaisasdependênciasfuncionaisquesãopreservadasIfRisnotinBCNF,decomposeitinasetofschemaswhichareinBCNF.DecomposeitinanotherBCNFdecomposition.Checkwhicharethefunctionaldependenciesthatarepreserved.SimplificandoF,temosporexemploF2={E→ACB,D→CB},quenospodelevaràseguintedecomposiçãoBCNF,R1=(E,A,C,B),R2=(E,D),quanãopreservaD→CB.ApartirdomesmoconjuntoF2,podeserobtidaoutradecomposiçãoBCNF:R1=(D,C,B),R2=(E,A),R3=(E,D),queéumadecomposiçãoquenãopreservaE→ACB.SimplifyingF,wegetforexampleF2={E→ACB,D→CB},whichcanleadustogetthefollowingBCNFdecomposition,R1=(E,A,C,B),R2=(E,D),whichdoesnotpreserveD→CBdependency.FromthesameF2setofdependencies,anotherBCNFdecompositioncanbeobtained:R1=(D,C,B),R2=(E,A),R3=(E,D),whichisadecompositionthatdoesnotpreserveE→ACB.

5. Decomponha-ana3FN,mesmosenãofornecessário.Decomposeitinthe3NF,evenifitisnotnecessary.SendoF2umacoberturacanónicadeF,podeserusadaparaobteraseguintedecomposiçãona3FN:R1=(E,A,C,B),R2=(D,C,B)andR3=(ED).SinceF2isacanonicalcoverofF,itcanbeusedtoobtainthefollowing3NFdecomposition:R1=(E,A,C,B),R2=(D,C,B)andR3=(ED).

6. MostreseadecomposiçãodeRemR1=(A,B,C)eR2=(C,D,E)ésemperdas.IndicateifadecompositionofRinR1=(A,B,C)andR2=(C,D,E)isalosslessone.DadoqueR1 R2=Cequer quer ,entãonãoéumadecomposiçãosemperdas.SinceR1 R2=Candboth and ,thenitisnotalesslossdecomposition.