comp283-lecture 5 applied database managementphil/teaching/comp283/lecture... · 2018. 4. 9. ·...
TRANSCRIPT
![Page 1: COMP283-Lecture 5 Applied Database Managementphil/Teaching/COMP283/lecture... · 2018. 4. 9. · Comp283-Lecture 5 Optimisation: DB Keys Foreign Key When a relationship exists between](https://reader036.vdocuments.site/reader036/viewer/2022070211/610025725ca02221227fa17e/html5/thumbnails/1.jpg)
COMP283-Lecture5
AppliedDatabaseManagement
Introduction
DatabaseAdministration Optimisation
Normalisation
�1
![Page 2: COMP283-Lecture 5 Applied Database Managementphil/Teaching/COMP283/lecture... · 2018. 4. 9. · Comp283-Lecture 5 Optimisation: DB Keys Foreign Key When a relationship exists between](https://reader036.vdocuments.site/reader036/viewer/2022070211/610025725ca02221227fa17e/html5/thumbnails/2.jpg)
Comp283-Lecture5Optimisation:DBKeys
● UniqueConstraints● Enforcesuniquenessonthesetofattributesdefinedinthem.
● Onlyonerowcancontainnull● PrimaryKeys● specialcaseofauniqueconstraint● oneormorecolumnsthattogetheruniquelydefinetherecord
● tablecanonlyhaveoneprimarykey● usually(andshouldbe)indexed● Everytableshouldhaveaprimarykey �2
![Page 3: COMP283-Lecture 5 Applied Database Managementphil/Teaching/COMP283/lecture... · 2018. 4. 9. · Comp283-Lecture 5 Optimisation: DB Keys Foreign Key When a relationship exists between](https://reader036.vdocuments.site/reader036/viewer/2022070211/610025725ca02221227fa17e/html5/thumbnails/3.jpg)
Comp283-Lecture5Optimisation:DBKeys● ForeignKey●Whenarelationshipexistsbetweentwotablesbasedonsomecommonsetofattributes,theintegrityofthedatacanbeenforcedbydefiningaForeignKey.
● Thereisoneprimarytableandonesecondary.● Setofattributesinsecondaryreferencingakeyintheprimarytable
● Attributesandstructureoflinked-to-keyinPrimarycannotbemodified(thoughdatacanofcourse)
● CompositeKey● auniqueconstraintmadeupofmultiplecolumns.
�3
![Page 4: COMP283-Lecture 5 Applied Database Managementphil/Teaching/COMP283/lecture... · 2018. 4. 9. · Comp283-Lecture 5 Optimisation: DB Keys Foreign Key When a relationship exists between](https://reader036.vdocuments.site/reader036/viewer/2022070211/610025725ca02221227fa17e/html5/thumbnails/4.jpg)
Comp283-Lecture5
Optimisation:DBIndexes● ClusteredIndexes● Eachtablecanhaveamaximumof1clusteredindex● Defineshowthedatabasewillsortthetable’sdata.● Aclusteredindexisastored(on-disk)structureassociatedwithaspecifictableorview,designedtohelpspeedupdataretrieval.
● Automaticallycreatedwhenaprimarykeyisdefined.Sinceeverytableshouldhaveaprimarykey,everytablewillhaveaclusteredindex.
● Un-ClusteredIndexes● Additionalindexes(ifyouchoosetomakethem)areunclustered.
�4
![Page 5: COMP283-Lecture 5 Applied Database Managementphil/Teaching/COMP283/lecture... · 2018. 4. 9. · Comp283-Lecture 5 Optimisation: DB Keys Foreign Key When a relationship exists between](https://reader036.vdocuments.site/reader036/viewer/2022070211/610025725ca02221227fa17e/html5/thumbnails/5.jpg)
Comp283-Lecture5
Optimisation:DBIndexes:ClusteredIndexes
�5
Record 04
Record 02
Record 06
Record 05
Record 01
Record 03
Indexed by Primary Key
Record 04
Record 02
Record 06
Record 05
Record 01
Record 03
![Page 6: COMP283-Lecture 5 Applied Database Managementphil/Teaching/COMP283/lecture... · 2018. 4. 9. · Comp283-Lecture 5 Optimisation: DB Keys Foreign Key When a relationship exists between](https://reader036.vdocuments.site/reader036/viewer/2022070211/610025725ca02221227fa17e/html5/thumbnails/6.jpg)
Comp283-Lecture5
Optimisation:DBIndexes:ClusteredIndexes
�6
R4
R2 R6 R5 R1 R3
1 2 3 4 5 6
Record 04
Record 02
Record 06
Record 05
Record 01
Record 03
Indexed by Primary Key
![Page 7: COMP283-Lecture 5 Applied Database Managementphil/Teaching/COMP283/lecture... · 2018. 4. 9. · Comp283-Lecture 5 Optimisation: DB Keys Foreign Key When a relationship exists between](https://reader036.vdocuments.site/reader036/viewer/2022070211/610025725ca02221227fa17e/html5/thumbnails/7.jpg)
Comp283-Lecture5
Optimisation:DBIndexes:Un-clusteredIndexes
�7
R4
R2 R6 R5 R1 R3
2 4 1 5 6 3Indexed by some other Key
Record 04
Record 06
Record 05
Record 03
Record 02
Record 01
![Page 8: COMP283-Lecture 5 Applied Database Managementphil/Teaching/COMP283/lecture... · 2018. 4. 9. · Comp283-Lecture 5 Optimisation: DB Keys Foreign Key When a relationship exists between](https://reader036.vdocuments.site/reader036/viewer/2022070211/610025725ca02221227fa17e/html5/thumbnails/8.jpg)
Comp283-Lecture5Optimisation:TablePartitioning● UsedforLargedatabases.● TablePartitioningcutsupthetableintosmallermoremanageablesizes.
● TheDBMSperformsPartitioninginthebackground.● Improvesperformanceforqueriesonsmallsubsetsofdata.● HardwarePartitioning● HorizontalPartitioning● VerticalPartitioning● Normalisation● RowSplitting
�8
![Page 9: COMP283-Lecture 5 Applied Database Managementphil/Teaching/COMP283/lecture... · 2018. 4. 9. · Comp283-Lecture 5 Optimisation: DB Keys Foreign Key When a relationship exists between](https://reader036.vdocuments.site/reader036/viewer/2022070211/610025725ca02221227fa17e/html5/thumbnails/9.jpg)
Comp283-Lecture5DBStorage:NormalisationForms● Normalisationistheprocessoftakingallofthedatathatistobestoredinadatabaseandseparatingitintodistincttablesfollowingstrictrulestoproducethemostefficientdatabasestoragestructure.
● Entityintegrity● Eachrowofatableisconsistent,generallyitshouldrepresentasetofuniqueattributes(thusitisaTuple)structuredsothateachattributerepresentsmeaningfortheobject
● Domainintegrity● Eachcolumnofatableisconsistent-alldatainthecolumnconformstothedefinitionoftheattribute.Aconstraintcanbeplacedonthecolumntoforcedomainintegrity.E.g.youcanallowordisallowNullvalues,definearangewithinwhichthedatavaluemustexist,theformatofthedata,aswellasspecifyingthedata-type.
�9
![Page 10: COMP283-Lecture 5 Applied Database Managementphil/Teaching/COMP283/lecture... · 2018. 4. 9. · Comp283-Lecture 5 Optimisation: DB Keys Foreign Key When a relationship exists between](https://reader036.vdocuments.site/reader036/viewer/2022070211/610025725ca02221227fa17e/html5/thumbnails/10.jpg)
Comp283-Lecture5DBStorage:NormalisationForms● Referentialintegrity● Maintainthedataconsistencybetweencolumnsinatableandalsobetweenrelatedtablesinthedatabase.Oftenenforcedusingaforeignkeywhichassociatesthedataattributeswithdataattributesinanothertable.
● User-definedintegrity● Maintaindataconsistencybystipulatingotherspecificrules.Usuallyenforcedusingstoredproceduresandtriggers.e.g.youmayrequirealogtabletobeupdatedwithareferencetoadatainsert,recordingtheuserthatmadetheinsert-Canbedoneusingatriggeronthedatatablethatcausessomecodetobeexecutedtoupdatethelogtable.
�10
![Page 11: COMP283-Lecture 5 Applied Database Managementphil/Teaching/COMP283/lecture... · 2018. 4. 9. · Comp283-Lecture 5 Optimisation: DB Keys Foreign Key When a relationship exists between](https://reader036.vdocuments.site/reader036/viewer/2022070211/610025725ca02221227fa17e/html5/thumbnails/11.jpg)
Comp283-Lecture5DBStorage:1NF● TheFirstNormalForm(1NF)● Thetablesmustnothaveanyduplicaterecords,creatingaprimarykeyonthetablewillsatisfythisrule.
● Notwocolumnsinthetableshoulddescribethesameattributetype.e.g.inabankingsystem;Current,Savings,Loancolumnsarealldescribingbankaccounttypes.
● Entriesinacolumnmustbeofthesamedata-type.● Eachrowmustcontainthesamenumberofcolumns(eachrecord/tuplehasthesamenumberandtypeofattributes–justdifferentvalues)
�11
![Page 12: COMP283-Lecture 5 Applied Database Managementphil/Teaching/COMP283/lecture... · 2018. 4. 9. · Comp283-Lecture 5 Optimisation: DB Keys Foreign Key When a relationship exists between](https://reader036.vdocuments.site/reader036/viewer/2022070211/610025725ca02221227fa17e/html5/thumbnails/12.jpg)
Comp283-Lecture5DBStorage:2NF● TheSecondNormalForm(2NF)● Mustbein1NF● Anynon-candidatekeyattributemustbedependantonthewholeofthePrimaryKey.
● e.g.● {Flight_No,Date,Safety_Check,Destination}Fails
2NF● But● {Flight_No,Date,Safety_Check}and {Flight_No,Destination} Passes2NF
�12
![Page 13: COMP283-Lecture 5 Applied Database Managementphil/Teaching/COMP283/lecture... · 2018. 4. 9. · Comp283-Lecture 5 Optimisation: DB Keys Foreign Key When a relationship exists between](https://reader036.vdocuments.site/reader036/viewer/2022070211/610025725ca02221227fa17e/html5/thumbnails/13.jpg)
Comp283-Lecture5DBStorage:2NF
�13
Flight_No Date Safety_Check DestinationBA32765 21/05/2017 Full-OK-03632 BelfastBA32765 22/05/2017 Full-OK-03784 BelfastLH93162 22/05/2017 Part-OK-73291 BerlinBA32765 23/05/2017 Full-OK-26534 Belfast
Flight_No Date Safety_CheckBA32765 21/05/2017 Full-OK-03632BA32765 22/05/2017 Full-OK-03784LH93162 22/05/2017 Part-OK-73291BA32765 23/05/2017 Full-OK-26534
Flight_No DestinationBA32765 BelfastLH93162 Berlin
![Page 14: COMP283-Lecture 5 Applied Database Managementphil/Teaching/COMP283/lecture... · 2018. 4. 9. · Comp283-Lecture 5 Optimisation: DB Keys Foreign Key When a relationship exists between](https://reader036.vdocuments.site/reader036/viewer/2022070211/610025725ca02221227fa17e/html5/thumbnails/14.jpg)
Comp283-Lecture5DBStorage:3NF● TheThirdNormalForm(3NF)● Mustbein1NF&in2NF.● Thetablesmustonlycontainnon-primarykeyattributesthatarefullydependantonthePrimaryKey.
● e.g.● {Flight_No,Date,Gate_No,Gate_Supervisor}Fails3NF,succeeds1NFand2NF
�14
● {Flight_No,Date,Gate_No}and {Gate_No,Date,Gate_Supervisor}arein3NF
![Page 15: COMP283-Lecture 5 Applied Database Managementphil/Teaching/COMP283/lecture... · 2018. 4. 9. · Comp283-Lecture 5 Optimisation: DB Keys Foreign Key When a relationship exists between](https://reader036.vdocuments.site/reader036/viewer/2022070211/610025725ca02221227fa17e/html5/thumbnails/15.jpg)
Comp283-Lecture5DBStorage:3NF
�15
Flight_No Date Gate_No Gate_Supervisor
BA32765 21/05/2017 12 Mr Jones
BA32765 22/05/2017 12 Ms Smith
LH93162 22/05/2017 8 Mr Jones
BA32765 23/05/2017 12 Ms Brown
![Page 16: COMP283-Lecture 5 Applied Database Managementphil/Teaching/COMP283/lecture... · 2018. 4. 9. · Comp283-Lecture 5 Optimisation: DB Keys Foreign Key When a relationship exists between](https://reader036.vdocuments.site/reader036/viewer/2022070211/610025725ca02221227fa17e/html5/thumbnails/16.jpg)
Comp283-Lecture5DBStorage:3NF
�16
Flight_No Date Gate_No
BA32765 21/05/2017 12
BA32765 22/05/2017 12
LH93162 22/05/2017 8
BA32765 23/05/2017 12
Date Gate_No Gate_Supervisor
21/05/2017 12 Mr Jones
22/05/2017 12 Ms Smith
22/05/2017 8 Mr Jones
23/05/2017 12 Ms Brown
● OurDateattributeswouldactuallybeDateandTimeofcourse-unlessweonlyhadoneflightperdaypergate!
![Page 17: COMP283-Lecture 5 Applied Database Managementphil/Teaching/COMP283/lecture... · 2018. 4. 9. · Comp283-Lecture 5 Optimisation: DB Keys Foreign Key When a relationship exists between](https://reader036.vdocuments.site/reader036/viewer/2022070211/610025725ca02221227fa17e/html5/thumbnails/17.jpg)
Comp283-Lecture5DBStorage:4NF● TheFourthNormalForm(4NF)● Mustbein1NFandin2NFandin3NF.● Thetablesmustcontainonlyoneindependentmulti-valuedfactabouttheentity.● e.g.{Person,CarReg,PetName} Fails4NFApersoncouldownmorethan1carandcouldownmorethan1pet,butcarsandpetsarenotdependentoneachother.
● Howdowerecordthis?
�17
![Page 18: COMP283-Lecture 5 Applied Database Managementphil/Teaching/COMP283/lecture... · 2018. 4. 9. · Comp283-Lecture 5 Optimisation: DB Keys Foreign Key When a relationship exists between](https://reader036.vdocuments.site/reader036/viewer/2022070211/610025725ca02221227fa17e/html5/thumbnails/18.jpg)
Comp283-Lecture5DBStorage:4NF-continued● Possiblesolutionsfor{Person,Cars,Pets}
Person Cars Pets
Fred LamborghiniGallardo
Fred PrinceRupert1(dog)
Fred Mollie(cat)
Fred Citroen2CVPerson Cars Pets
Fred LamborghiniGallardo PrinceRupert1(dog)
Fred Citroen2CV Mollie(cat)
Person Cars Pets
Fred LamborghiniGallardo PrinceRupert1(dog)
Fred Citroen2CV PrinceRupert1(dog)
Fred LamborghiniGallardo Mollie(cat)
Fred Citroen2CV Mollie(cat)
Orconformto4NF:
{Person,Cars}and {Person,Pets}
�18
![Page 19: COMP283-Lecture 5 Applied Database Managementphil/Teaching/COMP283/lecture... · 2018. 4. 9. · Comp283-Lecture 5 Optimisation: DB Keys Foreign Key When a relationship exists between](https://reader036.vdocuments.site/reader036/viewer/2022070211/610025725ca02221227fa17e/html5/thumbnails/19.jpg)
Comp283-Lecture5DBStorage:5NF● TheFifthNormalForm(5NF)● Mustbein1NF,in2NF,3NFandin4NF.● Aspecialisationof4NF.● Inthiscasesomeexternalrulegovernstherelationshipofthefactstotheentity● e.g.{Salesman,Manufacturer,Products} Rule:AsalespersonauthorisedtosellproductsoftypeAandalsoauthorisedtosellanyproductfromManufacturerB,mustbeabletosellanyproductoftypeAmadebymanufacturerB.
�19
![Page 20: COMP283-Lecture 5 Applied Database Managementphil/Teaching/COMP283/lecture... · 2018. 4. 9. · Comp283-Lecture 5 Optimisation: DB Keys Foreign Key When a relationship exists between](https://reader036.vdocuments.site/reader036/viewer/2022070211/610025725ca02221227fa17e/html5/thumbnails/20.jpg)
Comp283-Lecture5DBStorage:5NF-Continued
Salesman Manufacturer Product
Fred Phillips TV
Jane Phillips Amplifiers
Jane BOSE Speakers
Joe Sony TV
Joe Sony Amplifiers
{Salesman,Manufacturer,Products} Rule:AsalespersonauthorisedtosellproductsoftypeAandalsoauthorisedtosellanyproductfromManufacturerB,mustbeabletosellanyproductoftypeAmadebymanufacturerB.
Salesman Product
Fred TV
Jane Amplifiers
Jane Speakers
Joe TV
Joe Amplifiers
Salesman Manufacturer
Fred Phillips
Jane Phillips
Jane BOSE
Joe Sony
Manufacturer Product
Phillips TV
Phillips Amplifiers
BOSE Speakers
Sony TV
Sony Amplifiers
�20
![Page 21: COMP283-Lecture 5 Applied Database Managementphil/Teaching/COMP283/lecture... · 2018. 4. 9. · Comp283-Lecture 5 Optimisation: DB Keys Foreign Key When a relationship exists between](https://reader036.vdocuments.site/reader036/viewer/2022070211/610025725ca02221227fa17e/html5/thumbnails/21.jpg)
COMP283-Lecture5
Conclusion● DatabaseKeys● Normalisation
�21