database design - unipi.itdidawiki.cli.di.unipi.it/lib/exe/fetch.php/bdd-infuma/b.conceptual... ·...
TRANSCRIPT
DatabaseDesign
AnnaMonreale
Needtodesign
• Databaseareo.enborndesignless,fromahugespreadsheet
• Anomaliesarise,becauseofredundancy
• Redundancygenerateerrors
• Designmustinvolvetheuser
Anomalies
PhasesforDBrealiza7on• Userrequirementsanalysis&specifica7on
– collec<nguserneedsandnormalizingthemaccordingtostandards
• Conceptualdesign– isthephaseinwhichrequirementsareformalizedandintegratedinto
aglobalconceptualschema– usingaDBMS-independentconceptuallanguage
• Logicaldesign– theconceptualschemaismappedintoalogicalschemausingthedata
modelsupportedbytheDBMSchosenfortheimplementa<on
• Physicaldesign– concernstheselec<onofthedatastructuresusedtostoreand
retrievethedata.
5
UserRequirementsAnalysis
• Hardac<vitybecausehardtostandardize• Sugges<ons
– Involvetheusersmany<mesforcon<nuouschecks– Considerthepointofviewoftheapplica<onsusers– Tobesurethatyouareusingacommonlanguage– Iden<fycasestudiesthatyoucandiscussindetailstoiden<fytheproper<estobecapturedbythemodel
Mod. Concettuale >> Concetti Fondamentali >> Introduzione
6
AnObjectOrientedLanguagefordatadesign
• Realiza<onofadiagramrepresen<ngtheconceptualmodelofthedatabase
• Components:– Classes(collec<ons)
– Rela7onshipsamongclasses
– Sub-collec7onslinks
7
ClassDiagram
• PhaseofAnalasys– Needtoadopttherightlevelofabstrac<on
• Inpar<cular– Wedon’tneedallaPributes– TypeofaPributesisnotnecessary
Mod. Concettuale >> Concetti Fondamentali >> Il Diagramma delle Classi
8
Example:UniversityDB• Weneedtodesignthedatabaseformanagingdataaboutcoursesof
computersciencedegreeattheUniversityofPisa
• Thesystemmustmanagedataaboutstudentsofthemasterprogramandbachelorprogram.Foreachoneweneedtomaintaindatarelatedtothestudentsexams.
• Weneedtorecorddataaboutcoursesandthestudentsexamsforeachcourse.
• Foreachcoursewewanttorecordteachers,whomaybemorethanone.Moreoverateachermaybeinternalorexternal.
• Foreachteacherwehaveoneormorephonenumbers.
• Foreachstudentweneedtorecordthesupervisor(ateacher).BachelorstudentsmayaskasupervisoronlywhentheyareaPendingthethirdyear.
• Lastlythesystemmustmaintaininforma<onaboutthetutoringac<vi<esofmasterstudents,thathelpbachelorstudents.
Mod. Concettuale >> Concetti Fondamentali >> Il Diagramma delle Classi
9
Classes
• “Concepts”oftherealitytobemodelled– facts,people,things,– examples:student,course,exam,teacher
• Instancesofaclass– en<<es,objectsoftherealitytobemodelled
• ClasseshaveaNributes– Proper<esrelevantfortheapplica<on
ClasswithaNributes
• Apersonclass,withaPributes:– Name– SSN(key)– Address
11
Classes
• Instancesoftheclasses
Students
studentID
surname
name
year
{77777, Bruno Pasquale, 1}
{444, Pinco, Palla, 3} {88888,
Pinco, Pietro, 1}
12
Rela7onship
• Rela7onshipbetweenclasses• Logiclinkrelevantfortheapplica<on• ex:teachingbetweenteacherandcourse• ex:studentpassesanexam
• Instanceofarela7onship• Asetofedgesbetweeninstancesbelongingtotheinvolvedclasses
Mod. Concettuale >> Concetti Fondamentali >> Il Diagramma delle Classi
13
Rela7onship:Instances
{Dino, Pedreschi, Full}
{77777, Bruno Pasquale, 1}
{444, Pinco, Palla, 3}
{88888, Pinco, Pietro, 1}
{Anna, Monreale, Associate}
Teachers
Students
{111, Rossi, Mario 1}
{Giannotti, Fosca}
14
Cardinality
• Constraintsonrela7onships– Constraintsonthenumberofedgesbetweeninstancesofclasses
• MinimalCardinality– 0or1
• MaximalCardinality– 1ormany
Students Exams
Students Teachers
15
Cardinality
{Pedreschi, Dino, ordinario}
{77777, Bruno Pasquale, 1}
{444, Pinco, Palla, 3}
{88888, Pinco, Pietro, 1}
{Monreale, Anna, associato}
Teachers
Students
{111, Rossi, Mario 1}
Giannotti Fosca}
Supervisor >
16
Cardinality(upperbound)
• Classifica<onoftherela<onshipswrtthecardinality– OnetoOne:maximalcardinalityequalto1forbothclasses
• Manages[Managers,Departments]– OnetoMany:maximalcardinalityequalto1foraclassandmany(N)fortheotherone
• Owns[Persons,Cars]– ManytoMany:maximalcardinalityequaltoNforbothclasses
• Teaching[Course,Teacher]
Cardinality(lowerbound)• Sixteencombin<ons:
– Onetomanytotal/par<al– Onetomanypar<al/par<al
– Onetomanypar<al/total
– Onetomanytotal/total
ExamsStudents
ExamsStudents
ExamsStudents
ExamsStudents
18
ClassHierarchy
• Asubclass:– asubsetofclasselements,forwhichweplantocollectmoreinforma<on:
– ex:StudentsissubclassofPersons– ex:Internalandexternalteachersaresubclassesofthegenericconcept“teacher”
Mod. Concettuale >> Concetti Fondamentali >> Il Diagramma delle Classi
19
ClassHierarchy
20
Notes
• Some<meitisnecesarytoaddnotesinthediagramtoexpresssomeconstraints– Ex:BachelorstudentsmayaskasupervisoronlywhentheyareaPendingthethirdyear.
Students Teachers
Bachelor students may have a supervisor only during the third year
Rela7onshpwithaNributes• Some<mesarela<onshipmayhavesomeproper<esthat
characterizeeachinstanceoftherela<onship• “Johnisoccupyingtheroom105atLeMeridien-Houston,ata$145rate”
• Thisisarela<onshipinstancebetweenpersonsandrooms,witharateaPribute
22
RecursiveRela7onships
{a01, Anna, Monreale}
Persons
{a02, Sofia, Giuliani}
{p01, Dino, Palla }
{p02, Paolo, Palla}
23
TernaryRela7onship
• Ternaryfactsexistalso• “JohnbookedflightFK354/13-6-2000withY2fare”
Keepitsimple
• Wheneveritmakessense,upgradearela7onshipwithaNributes,oraternaryone,toacollec<on
FromANributestoclasses
Fromternarytonewclass