CS 4604: Introducon to Database Management...
Transcript of CS 4604: Introducon to Database Management...
CS4604:Introduc0ontoDatabaseManagementSystems
B.AdityaPrakashLecture#7:En-ty/Rela-onal
Model---Part3
2
PurposeofE/RModel
§ TheE/Rmodelallowsustosketchthedesignofadatabaseinformally.– Representdifferenttypesofdataandhowtheyrelatetoeachother
§ Designsaredrawingscalleden1ty-rela1onshipdiagrams.§ FairlymechanicalwaystoconvertE/Rdiagramstoreal
implementa-onslikerela-onaldatabasesexist.
Prakash2016 VTCS4604
Recap:Rela0onalModel
§ Builtaroundasingleconceptformodellingdata:therela-onortable.
§ Supportshigh-levelprogramminglanguage(SQL).
§ Hasanelegantmathema-caldesigntheory.§ MostcurrentDBMSarerela-onal.
Prakash2016 VTCS4604 3
Recap:TheRela0on
§ Arela-onisatwo-dimensionaltable:– Rela-onßàtable.– AXributeßàcolumnname.– Tupleßàrow(nottheheaderrow).– Databaseßàcollec-onofrela-ons.
Prakash2016 VTCS4604 4
Recap:TheSchema
§ Theschemaofarela-onisthenameoftherela-onfollowedbyaparanthe-sedlistofaXributes– CoursesTaken(Student,Course,Grade)
§ Adesigninarela-onalmodelconsistsofasetofschemas.– Suchasetofschemasiscalledarela-onaldatabaseschema
Prakash2016 VTCS4604 5
Conver0ngE/RDiagramstoRela0onalDesigns
§ En-tySetàRela-on– AXributeofEn-tySetàAXributeofaRela-on
§ Rela-onshipàrela-onwhoseaXributesare– AXributeoftherela-onshipitself– KeyaXributesoftheconnecteden-tysets
§ Severalspecialcases:– Weaken-tysets.– Combiningrela-ons(especiallyformany-onerela-onships)
– ISArela-onshipsandsubclasses
Prakash2016 VTCS4604 6
SchemasforNon-WeakEn0tySets
§ Foreachen-tyset,createarela-onwiththesamenameandwiththesamesetofaXributes
Students(Name,Address)Professors(Name,Office,Age)Departments(Name)
Prakash2016 VTCS4604 8
SchemasforWeakEn0tySets
§ Foreachweaken-tysetW,createarela-onwiththesamenamewhoseaXributesare:– AXributesofW– KeyaXributesofotheren-tysetsthathelpformthekeyforW
Courses(Number,DepartmentName,CourseName,Classroom,Enrollment)
Prakash2016 VTCS4604 9
SchemasforNon-Suppor0ngRela0onships
§ Foreachrela-onship,createarela-onwiththesamenamewhoseaXributesare– AXributesoftherela-onshipitself.– KeyaXributesoftheconnecteden-tysets(eveniftheyareweak)
Prakash2016 VTCS4604 10
SchemasforNon-Suppor0ngRela0onships
§ Take(StudentName,Address,Number,DepartmentName)§ Teach
Prakash2016 VTCS4604 13
SchemasforNon-Suppor0ngRela0onships
§ Take(StudentName,Address,Number,DepartmentName)§ Teach(ProfessorName,Office,Number,DepartmentName)§ Evalua-on
Prakash2016 VTCS4604 14
SchemasforNon-Suppor0ngRela0onships
§ Take(StudentName,Address,Number,DepartmentName)§ Teach(ProfessorName,Office,Number,DepartmentName)§ Evalua-on(StudentName,Address,ProfessorName,Office,
Number,DepartmentName,Grade)
Prakash2016 VTCS4604 15
RolesinRela0onships
§ Ifanen-tysetEappearsk>1-mesinarela-onshipR(indifferentroles),thekeyaXributesforEappeark-mesintherela-onforR,appropriatelyrenamed
PreReq(RequirerNumber,RequirerDeptName,RequirementNumber,RequirementDeptName)
Prakash2016 VTCS4604 16
CombiningRela0ons
§ Considermany-oneTeachrela-onshipfromCoursestoProfessors
§ Schemasare:Courses(Number,DepartmentName,CourseName,Classroom,Enrollment)Professors(Name,Office,Age)Teach(Number,DepartmentName,ProfessorName,Office)
Prakash2016 VTCS4604 17
CombiningRela0onsCourses(Number,DepartmentName,CourseName,Classroom,Enrollment)Professors(Name,Office,Age)Teach(Number,DepartmentName,ProfessorName,Office)
§ ThekeyforCoursesuniquelydeterminesallaXributesofTeach
§ Wecancombinetherela-onsforCoursesandTeachintoasinglerela-onwhoseaXributesare– AlltheaXributesforCourses,– AnyaXributesofTeach,and– ThekeyaXributesofProfessors
Prakash2016 VTCS4604 18
RulesforCombiningRela0ons
§ Wecancombineintoonerela-onQ– Therela-onforanen-tysetE– allmany-to-onerela-onshipsR1,R2,…,RkfromEtootheren-tysetsE1,E2,…,Ekrespec-vely
§ TheaXributesofQare– AlltheaXributesofE– AnyaXributesofR1,R2,…,Rk– ThekeyaXributesofE1,E2,…,Ek
§ WhatifRisamany-manyrela-onshipfromEtoF?
Prakash2016 VTCS4604 19
Suppor0ngRela0onships
§ SchemaforDepartmentsisDepartments(Name)§ SchemaforCoursesisCourses(Number,DepartmentName,CourseName,Classroom,Enrollment)
§ Whatistheschemaforoffer?
Prakash2016 VTCS4604 20
Suppor0ngRela0onships
§ Whatistheschemaforoffer?– Offer(Name,Number,DepartmentName)– ButNameandDepartmentNameareiden-cal,sotheschemaforOfferisOffer(Number,DepartmentName)
– TheschemaforOfferisasubsetoftheschemafortheweaken-tyset,sowecandispensewiththerela-onforOffer
Prakash2016 VTCS4604 21
SummaryofWeakEn0tySets
§ IfWisaweaken-tyset,therela-onforWhasaschemawhoseaXributesare– allaXributesofW– allaXributesofsuppor-ngrela-onshipsforW– foreachsuppor-ngrela-onshipforWtoanen-tysetE– thekeyaXributesofE
§ Thereisnorela-onforanysuppor-ngrela-onshipforWPrakash2016 VTCS4604 22
ISAtoRela0onal
§ Threeapproaches:– E/Rviewpoint– Object-orientedviewpoint– “FlaXen”viewpoint
Prakash2016 VTCS4604 23
RulesSa0sfiedbyanISAHierarchy
§ Thehierarchyhasarooten-tyset§ Therooten-tysethasakeythatiden-fieseveryen-tyrepresentedbythehierarchy
§ Apar-cularen-tycanhavecomponentsthatbelongtoen-tysetsofanysubtreeofthehierarchy,aslongasthatsubtreeincludestheroot
Prakash2016 VTCS4604 24
ISAtoRela0onalMethodI:E/RApproach
§ Createarela-onforeachen-tyset§ TheaXributesoftherela-onforanon-rooten-tysetEare– theaXributesformingthekey(obtainedfromtheroot)and
– anyaXributesofEitself§ Anen-tywithcomponentsinmul-pleen-tysetshastuplesinalltherela-onscorrespondingtotheseen-tysets
§ Donotcreatearela-onforanyisarela-onship§ Createarela-onforeveryotherrela-onshipPrakash2016 VTCS4604 26
Example:ISAtoRela0onalMethodI:E/RApproach
Students(ID,Name)Undergraduates(ID,Major)Graduates(ID,Major)Masters(ID,Thesis_-tle_MS)PhDs(ID,Thesis_-tle_PhD)UTA_for(ID,CourseNum,
DeptName)GTA_for(ID,CourseNum,
DeptName)
Prakash2016 VTCS4604 27
ISAtoRela0onalMethodII:“FlaOen”Approach
§ Createasinglerela-onfortheen-rehierarchy
§ AXributesare– thekeyaXributesoftherootand– theaXributesofeachen-tysetinthehierarchy
§ Handlerela-onshipsasbeforeStudents(ID,Name,UGMajor,GMajor,Thesis_-tle_MS,Thesis_-tle_PhD).
Prakash2016 VTCS4604 28
ISAtoRela0onalMethodIII:ObjectOrientedApproach
§ Treaten--esasobjectsbelongingtoasingleclass.§ “Class”==subtreeofthehierarchythatincludestheroot.
§ Enumerateallsubtreesofthehierarchythatcontaintheroot.
§ Foreachsuchsubtree,– Createarela-onthatrepresentsen--esthathavecomponentsinexactlythatsubtree.
– Theschemaforthisrela-onhasalltheaXributesofalltheen-tysetsinthatsubtree.
§ Schemaoftherela-onforarela-onshiphaskeyaXributesoftheconnecteden-tysets.
Prakash2016 VTCS4604 29
Example:ISAtoRela0onalMethodIII:ObjectOrientedApproach
Subtreesare:Students(ID,Name)
Prakash2016 VTCS4604 31
Example:ISAtoRela0onalMethodIII:ObjectOrientedApproach
Subtreesare:Students(ID)StudentsUGs(ID,Major)
Prakash2016 VTCS4604 32
Example:ISAtoRela0onalMethodIII:ObjectOrientedApproach
Subtreesare:Students(ID)StudentsUGs(ID,Major)StudentGs(ID,Major)
Prakash2016 VTCS4604 33
Example:ISAtoRela0onalMethodIII:ObjectOrientedApproach
Subtreesare:Students(ID)StudentsUGs(ID,Major)StudentGs(ID,Major)StudentGsMasters(ID,Major,Thesis_-tle_MS)
Prakash2016 VTCS4604 34
Example:ISAtoRela0onalMethodIII:ObjectOrientedApproach
Subtreesare:Students(ID)StudentsUGs(ID,Major)StudentGs(ID,Major)StudentGsMasters(ID,Major,Thesis_-tle_MS)StudentsGsPhDs(ID,Major,Thesis_-tle_PhD)
Prakash2016 VTCS4604 35
Example:ISAtoRela0onalMethodIII:ObjectOrientedApproach
Subtreesare:Students(ID)StudentsUGs(ID,Major)StudentGs(ID,Major)StudentGsMasters(ID,Major,Thesis_-tle_MS)StudentsGsPhDs(ID,Major,Thesis_-tle_PhD)StudentsUGsGsMasters(ID,UGMajor,GradMajor,Thesis_-tle_MS)
Prakash2016 VTCS4604 36
Example:ISAtoRela0onalMethodIII:ObjectOrientedApproach
Subtreesare:Students(ID)StudentsUGs(ID,Major)StudentGs(ID,Major)StudentGsMasters(ID,Major,Thesis_-tle_MS)StudentsGsPhDs(ID,Major,Thesis_-tle_PhD)StudentsUGsGsMasters(ID,UGMajor,GradMajor,Thesis_-tle_MS)
Prakash2016 VTCS4604 37
Whatothersubtreesexist?
ComparisonoftheThreeApproaches
§ Answeringqueries– Itisexpensivetoanswerqueriesinvolvingseveralrela-ons
– QueriesaboutStudentsingeneral– Queriesaboutapar-cularsubclassofStudents
Prakash2016 VTCS4604 38
ComparisonoftheThreeApproaches
§ Numberofrela-onsfornrela-onsinthehierarchy– Weliketohaveasmallnumberofrela-ons– FlaXen
• 1– E/R
• n– OO
• Canbe2^n
Prakash2016 VTCS4604 39
ComparisonoftheThreeApproaches
§ Redundancyandspaceusage– FlaXen
• MayhavealargenumberofNULLS
– E/R• Severaltuplesperen-ty,butonlykeyaXributesarerepeated
– OO• Onlyonetupleperen-ty
Prakash2016 VTCS4604 40
Inshort,goodenoughruleofthumb:usetheE/Rstyle
E/RExample1:USCongress(Handout2)
§ TheUSCongressisbicameralmeaningthatitiscomposedoftwohouses:theHouseofRepresenta-vesandtheSenate.EverystatehasexactlytwoSenators(ajuniorandaseniormember),butavariablenumberofRepresenta-ves(exactlyoneperdistrict).Nosenatorcanrepresentmorethanonestateata-me.Likewise,noRepresenta-vecanservemorethanonedistrictata-me.Everystatehasavariablenumberofdistricts(dependentonpopula-on),buteverystatehasatleastonedistrict(inastatelikeDelawarethedistrictboundariesarethestate'sborders).Districtshavenumbers(e.g.,district1).AgivenCongressperson(SenatororRepresenta-ve)cannotserveinbothhousesatagiven-me.Congresspeoplehavenamesande-mailaddresses.EveryCongresspersonisamemberofexactlyonepoli-calparty.ExactlyonememberoftheHouseisdesignatedasSpeakeroftheHouse.Lastly,CongresspeoplebelongtoCongressionalcommiXeeswhichhavenamesandsponsorbills,whichalsohavenames."
Prakash2016 VTCS4604 42
E/RExample2:CompanyDB§ A company database needs to store informa-on about
employees (iden-fied by ssn, with salary and phone asa X r i b u t e s ) , d e p a r tmen t s ( i d e n -fi e d b y d n o ,with dname and budget as aXributes), and children ofemployees (with name and age as aXr ibutes) .Employees work in departments; each departmentis managed by an employee; a child must be iden-fieduniquely by name when the parent (who is an employee;assume that only one parent works for the company) isknown.
Prakash2016 VTCS4604 51
§ A company database needs to store informa-on about employees(iden-fied by ssn, with salary and phone as aXributes), departments(iden-fiedbydno,withdnameandbudgetasaXributes),andchildrenofemployees(withnameandageasaXributes).
Note:Childisaweaken-ty
Prakash2016 VTCS4604 52