COMP283-Lecture 5 Applied Database Managementphil/Teaching/COMP283/lecture... · 2018. 4. 9. ·...
Transcript of COMP283-Lecture 5 Applied Database Managementphil/Teaching/COMP283/lecture... · 2018. 4. 9. ·...
COMP283-Lecture5
AppliedDatabaseManagement
Introduction
DatabaseAdministration Optimisation
Normalisation
�1
Comp283-Lecture5Optimisation:DBKeys
● UniqueConstraints● Enforcesuniquenessonthesetofattributesdefinedinthem.
● Onlyonerowcancontainnull● PrimaryKeys● specialcaseofauniqueconstraint● oneormorecolumnsthattogetheruniquelydefinetherecord
● tablecanonlyhaveoneprimarykey● usually(andshouldbe)indexed● Everytableshouldhaveaprimarykey �2
Comp283-Lecture5Optimisation:DBKeys● ForeignKey●Whenarelationshipexistsbetweentwotablesbasedonsomecommonsetofattributes,theintegrityofthedatacanbeenforcedbydefiningaForeignKey.
● Thereisoneprimarytableandonesecondary.● Setofattributesinsecondaryreferencingakeyintheprimarytable
● Attributesandstructureoflinked-to-keyinPrimarycannotbemodified(thoughdatacanofcourse)
● CompositeKey● auniqueconstraintmadeupofmultiplecolumns.
�3
Comp283-Lecture5
Optimisation:DBIndexes● ClusteredIndexes● Eachtablecanhaveamaximumof1clusteredindex● Defineshowthedatabasewillsortthetable’sdata.● Aclusteredindexisastored(on-disk)structureassociatedwithaspecifictableorview,designedtohelpspeedupdataretrieval.
● Automaticallycreatedwhenaprimarykeyisdefined.Sinceeverytableshouldhaveaprimarykey,everytablewillhaveaclusteredindex.
● Un-ClusteredIndexes● Additionalindexes(ifyouchoosetomakethem)areunclustered.
�4
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
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
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
Comp283-Lecture5Optimisation:TablePartitioning● UsedforLargedatabases.● TablePartitioningcutsupthetableintosmallermoremanageablesizes.
● TheDBMSperformsPartitioninginthebackground.● Improvesperformanceforqueriesonsmallsubsetsofdata.● HardwarePartitioning● HorizontalPartitioning● VerticalPartitioning● Normalisation● RowSplitting
�8
Comp283-Lecture5DBStorage:NormalisationForms● Normalisationistheprocessoftakingallofthedatathatistobestoredinadatabaseandseparatingitintodistincttablesfollowingstrictrulestoproducethemostefficientdatabasestoragestructure.
● Entityintegrity● Eachrowofatableisconsistent,generallyitshouldrepresentasetofuniqueattributes(thusitisaTuple)structuredsothateachattributerepresentsmeaningfortheobject
● Domainintegrity● Eachcolumnofatableisconsistent-alldatainthecolumnconformstothedefinitionoftheattribute.Aconstraintcanbeplacedonthecolumntoforcedomainintegrity.E.g.youcanallowordisallowNullvalues,definearangewithinwhichthedatavaluemustexist,theformatofthedata,aswellasspecifyingthedata-type.
�9
Comp283-Lecture5DBStorage:NormalisationForms● Referentialintegrity● Maintainthedataconsistencybetweencolumnsinatableandalsobetweenrelatedtablesinthedatabase.Oftenenforcedusingaforeignkeywhichassociatesthedataattributeswithdataattributesinanothertable.
● User-definedintegrity● Maintaindataconsistencybystipulatingotherspecificrules.Usuallyenforcedusingstoredproceduresandtriggers.e.g.youmayrequirealogtabletobeupdatedwithareferencetoadatainsert,recordingtheuserthatmadetheinsert-Canbedoneusingatriggeronthedatatablethatcausessomecodetobeexecutedtoupdatethelogtable.
�10
Comp283-Lecture5DBStorage:1NF● TheFirstNormalForm(1NF)● Thetablesmustnothaveanyduplicaterecords,creatingaprimarykeyonthetablewillsatisfythisrule.
● Notwocolumnsinthetableshoulddescribethesameattributetype.e.g.inabankingsystem;Current,Savings,Loancolumnsarealldescribingbankaccounttypes.
● Entriesinacolumnmustbeofthesamedata-type.● Eachrowmustcontainthesamenumberofcolumns(eachrecord/tuplehasthesamenumberandtypeofattributes–justdifferentvalues)
�11
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
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
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
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
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!
Comp283-Lecture5DBStorage:4NF● TheFourthNormalForm(4NF)● Mustbein1NFandin2NFandin3NF.● Thetablesmustcontainonlyoneindependentmulti-valuedfactabouttheentity.● e.g.{Person,CarReg,PetName} Fails4NFApersoncouldownmorethan1carandcouldownmorethan1pet,butcarsandpetsarenotdependentoneachother.
● Howdowerecordthis?
�17
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
Comp283-Lecture5DBStorage:5NF● TheFifthNormalForm(5NF)● Mustbein1NF,in2NF,3NFandin4NF.● Aspecialisationof4NF.● Inthiscasesomeexternalrulegovernstherelationshipofthefactstotheentity● e.g.{Salesman,Manufacturer,Products} Rule:AsalespersonauthorisedtosellproductsoftypeAandalsoauthorisedtosellanyproductfromManufacturerB,mustbeabletosellanyproductoftypeAmadebymanufacturerB.
�19
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
COMP283-Lecture5
Conclusion● DatabaseKeys● Normalisation
�21