cs 4604: introduction to database management...

Post on 28-Aug-2020

2 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

CS4604:IntroductiontoDatabaseManagementSystems

B.AdityaPrakashLecture#7:Entity/Relational

Model---Part3

2

PurposeofE/RModel

§  TheE/Rmodelallowsustosketchthedesignofadatabaseinformally.–  Representdifferenttypesofdataandhowtheyrelatetoeachother

§  Designsaredrawingscalledentity-relationshipdiagrams.§  FairlymechanicalwaystoconvertE/Rdiagramstoreal

implementationslikerelationaldatabasesexist.

Prakash2018 VTCS4604

Recap:RelationalModel

§  Builtaroundasingleconceptformodellingdata:therelationortable.

§  Supportshigh-levelprogramminglanguage(SQL).

§  Hasanelegantmathematicaldesigntheory.§ MostcurrentDBMSarerelational.

Prakash2018 VTCS4604 3

Recap:TheRelation

§  Arelationisatwo-dimensionaltable:–  Relationßàtable.–  Attributeßàcolumnname.–  Tupleßàrow(nottheheaderrow).–  Databaseßàcollectionofrelations.

Prakash2018 VTCS4604 4

Recap:TheSchema

§  Theschemaofarelationisthenameoftherelationfollowedbyaparanthetisedlistofattributes–  CoursesTaken(Student,Course,Grade)

§  Adesigninarelationalmodelconsistsofasetofschemas.–  Suchasetofschemasiscalledarelationaldatabaseschema

Prakash2018 VTCS4604 5

ConvertingE/RDiagramstoRelationalDesigns

§  EntitySetàRelation– AttributeofEntitySetàAttributeofaRelation

§  Relationshipàrelationwhoseattributesare– Attributeoftherelationshipitself–  Keyattributesoftheconnectedentitysets

§  Severalspecialcases:– Weakentitysets.–  Combiningrelations(especiallyformany-onerelationships)

–  ISArelationshipsandsubclasses

Prakash2018 VTCS4604 6

ExampleforConversion

Prakash2018 VTCS4604 7

SchemasforNon-WeakEntitySets

§  Foreachentityset,createarelationwiththesamenameandwiththesamesetofattributes

Students(Name,Address)Professors(Name,Office,Age)Departments(Name)

Prakash2018 VTCS4604 8

SchemasforWeakEntitySets

§  ForeachweakentitysetW,createarelationwiththesamenamewhoseattributesare:–  AttributesofW–  KeyattributesofotherentitysetsthathelpformthekeyforW

Courses(Number,DepartmentName,CourseName,Classroom,Enrollment)

Prakash2018 VTCS4604 9

SchemasforNon-SupportingRelationships

§  Foreachrelationship,createarelationwiththesamenamewhoseattributesare–  Attributesoftherelationshipitself.–  Keyattributesoftheconnectedentitysets(eveniftheyareweak)

Prakash2018 VTCS4604 10

SchemasforNon-SupportingRelationships

Prakash2018 VTCS4604 11

SchemasforNon-SupportingRelationships

§  Take

Prakash2018 VTCS4604 12

SchemasforNon-SupportingRelationships

§  Take(StudentName,Address,Number,DepartmentName)§  Teach

Prakash2018 VTCS4604 13

SchemasforNon-SupportingRelationships

§  Take(StudentName,Address,Number,DepartmentName)§  Teach(ProfessorName,Office,Number,DepartmentName)§  Evaluation

Prakash2018 VTCS4604 14

SchemasforNon-SupportingRelationships

§  Take(StudentName,Address,Number,DepartmentName)§  Teach(ProfessorName,Office,Number,DepartmentName)§  Evaluation(StudentName,Address,ProfessorName,Office,

Number,DepartmentName,Grade)

Prakash2018 VTCS4604 15

RolesinRelationships

§  IfanentitysetEappearsk>1timesinarelationshipR(indifferentroles),thekeyattributesforEappearktimesintherelationforR,appropriatelyrenamed

PreReq(RequirerNumber,RequirerDeptName,RequirementNumber,RequirementDeptName)

Prakash2018 VTCS4604 16

CombiningRelations

§  Considermany-oneTeachrelationshipfromCoursestoProfessors

§  Schemasare:Courses(Number,DepartmentName,CourseName,Classroom,Enrollment)Professors(Name,Office,Age)Teach(Number,DepartmentName,ProfessorName,Office)

Prakash2018 VTCS4604 17

CombiningRelationsCourses(Number,DepartmentName,CourseName,Classroom,Enrollment)Professors(Name,Office,Age)Teach(Number,DepartmentName,ProfessorName,Office)

§  ThekeyforCoursesuniquelydeterminesallattributesofTeach

§  WecancombinetherelationsforCoursesandTeachintoasinglerelationwhoseattributesare–  AlltheattributesforCourses,–  AnyattributesofTeach,and–  ThekeyattributesofProfessors

Prakash2018 VTCS4604 18

RulesforCombiningRelations

§  WecancombineintoonerelationQ–  TherelationforanentitysetE–  allmany-to-onerelationshipsR1,R2,…,RkfromEtootherentitysetsE1,E2,…,Ekrespectively

§  TheattributesofQare– AlltheattributesofE– AnyattributesofR1,R2,…,Rk–  ThekeyattributesofE1,E2,…,Ek

§  WhatifRisamany-manyrelationshipfromEtoF?

Prakash2018 VTCS4604 19

SupportingRelationships

§  SchemaforDepartmentsisDepartments(Name)§  SchemaforCoursesisCourses(Number,DepartmentName,CourseName,Classroom,Enrollment)

§ Whatistheschemaforoffer?

Prakash2018 VTCS4604 20

SupportingRelationships

§  Whatistheschemaforoffer?– Offer(Name,Number,DepartmentName)–  ButNameandDepartmentNameareidentical,sotheschemaforOfferisOffer(Number,DepartmentName)

–  TheschemaforOfferisasubsetoftheschemafortheweakentityset,sowecandispensewiththerelationforOffer

Prakash2018 VTCS4604 21

SummaryofWeakEntitySets

§  IfWisaweakentityset,therelationforWhasaschemawhoseattributesare–  allattributesofW–  allattributesofsupportingrelationshipsforW–  foreachsupportingrelationshipforWtoanentitysetE–  thekeyattributesofE

§  ThereisnorelationforanysupportingrelationshipforWPrakash2018 VTCS4604 22

ISAtoRelational

§  Threeapproaches:– E/Rviewpoint– Object-orientedviewpoint– “Flatten”viewpoint

Prakash2018 VTCS4604 23

RulesSatisfiedbyanISAHierarchy

§  Thehierarchyhasarootentityset§  Therootentitysethasakeythatidentifieseveryentityrepresentedbythehierarchy

§  Aparticularentitycanhavecomponentsthatbelongtoentitysetsofanysubtreeofthehierarchy,aslongasthatsubtreeincludestheroot

Prakash2018 VTCS4604 24

ExampleISAhierarchy

Prakash2018 VTCS4604 25

ISAtoRelationalMethodI:E/RApproach

§  Createarelationforeachentityset§  Theattributesoftherelationforanon-rootentitysetEare–  theattributesformingthekey(obtainedfromtheroot)and

–  anyattributesofEitself§  Anentitywithcomponentsinmultipleentitysetshastuplesinalltherelationscorrespondingtotheseentitysets

§  Donotcreatearelationforanyisarelationship§  CreatearelationforeveryotherrelationshipPrakash2018 VTCS4604 26

Example:ISAtoRelationalMethodI:E/RApproach

Students(ID,Name)Undergraduates(ID,Major)Graduates(ID,Major)Masters(ID,Thesis_title_MS)PhDs(ID,Thesis_title_PhD)UTA_for(ID,CourseNum,

DeptName)GTA_for(ID,CourseNum,

DeptName)

Prakash2018 VTCS4604 27

ISAtoRelationalMethodII:“Flatten”Approach

§  Createasinglerelationfortheentirehierarchy

§  Attributesare–  thekeyattributesoftherootand–  theattributesofeachentitysetinthehierarchy

§  HandlerelationshipsasbeforeStudents(ID,Name,UGMajor,GMajor,Thesis_title_MS,Thesis_title_PhD).

Prakash2018 VTCS4604 28

ISAtoRelationalMethodIII:ObjectOrientedApproach

§  Treatentitiesasobjectsbelongingtoasingleclass.§  “Class”==subtreeofthehierarchythatincludestheroot.

§  Enumerateallsubtreesofthehierarchythatcontaintheroot.

§  Foreachsuchsubtree,–  Createarelationthatrepresentsentitiesthathavecomponentsinexactlythatsubtree.

–  Theschemaforthisrelationhasalltheattributesofalltheentitysetsinthatsubtree.

§  Schemaoftherelationforarelationshiphaskeyattributesoftheconnectedentitysets.

Prakash2018 VTCS4604 29

Example:ISAtoRelationalMethodIII:ObjectOrientedApproach

Subtreesare:

Prakash2018 VTCS4604 30

Example:ISAtoRelationalMethodIII:ObjectOrientedApproach

Subtreesare:Students(ID,Name)

Prakash2018 VTCS4604 31

Example:ISAtoRelationalMethodIII:ObjectOrientedApproach

Subtreesare:Students(ID)StudentsUGs(ID,Major)

Prakash2018 VTCS4604 32

Example:ISAtoRelationalMethodIII:ObjectOrientedApproach

Subtreesare:Students(ID)StudentsUGs(ID,Major)StudentGs(ID,Major)

Prakash2018 VTCS4604 33

Example:ISAtoRelationalMethodIII:ObjectOrientedApproach

Subtreesare:Students(ID)StudentsUGs(ID,Major)StudentGs(ID,Major)StudentGsMasters(ID,Major,Thesis_title_MS)

Prakash2018 VTCS4604 34

Example:ISAtoRelationalMethodIII:ObjectOrientedApproach

Subtreesare:Students(ID)StudentsUGs(ID,Major)StudentGs(ID,Major)StudentGsMasters(ID,Major,Thesis_title_MS)StudentsGsPhDs(ID,Major,Thesis_title_PhD)

Prakash2018 VTCS4604 35

Example:ISAtoRelationalMethodIII:ObjectOrientedApproach

Subtreesare:Students(ID)StudentsUGs(ID,Major)StudentGs(ID,Major)StudentGsMasters(ID,Major,Thesis_title_MS)StudentsGsPhDs(ID,Major,Thesis_title_PhD)StudentsUGsGsMasters(ID,UGMajor,GradMajor,Thesis_title_MS)

Prakash2018 VTCS4604 36

Example:ISAtoRelationalMethodIII:ObjectOrientedApproach

Subtreesare:Students(ID)StudentsUGs(ID,Major)StudentGs(ID,Major)StudentGsMasters(ID,Major,Thesis_title_MS)StudentsGsPhDs(ID,Major,Thesis_title_PhD)StudentsUGsGsMasters(ID,UGMajor,GradMajor,Thesis_title_MS)

Prakash2018 VTCS4604 37

Whatothersubtreesexist?

ComparisonoftheThreeApproaches

§  Answeringqueries–  Itisexpensivetoanswerqueriesinvolvingseveralrelations

– QueriesaboutStudentsingeneral– QueriesaboutaparticularsubclassofStudents

Prakash2018 VTCS4604 38

ComparisonoftheThreeApproaches

§  Numberofrelationsfornrelationsinthehierarchy– Weliketohaveasmallnumberofrelations– Flatten

•  1– E/R

•  n– OO

•  Canbe2^n

Prakash2018 VTCS4604 39

ComparisonoftheThreeApproaches

§  Redundancyandspaceusage– Flatten

•  MayhavealargenumberofNULLS

– E/R•  Severaltuplesperentity,butonlykeyattributesarerepeated

– OO•  Onlyonetupleperentity

Prakash2018 VTCS4604 40

Inshort,goodenoughruleofthumb:usetheE/Rstyle

EXAMPLES

Prakash2018 VTCS4604 41

E/RExample1:USCongress(Handout2)

§  TheUSCongressisbicameralmeaningthatitiscomposedoftwohouses:theHouseofRepresentativesandtheSenate.EverystatehasexactlytwoSenators(ajuniorandaseniormember),butavariablenumberofRepresentatives(exactlyoneperdistrict).Nosenatorcanrepresentmorethanonestateatatime.Likewise,noRepresentativecanservemorethanonedistrictatatime.Everystatehasavariablenumberofdistricts(dependentonpopulation),buteverystatehasatleastonedistrict(inastatelikeDelawarethedistrictboundariesarethestate'sborders).Districtshavenumbers(e.g.,district1).AgivenCongressperson(SenatororRepresentative)cannotserveinbothhousesatagiventime.Congresspeoplehavenamesande-mailaddresses.EveryCongresspersonisamemberofexactlyonepoliticalparty.ExactlyonememberoftheHouseisdesignatedasSpeakeroftheHouse.Lastly,CongresspeoplebelongtoCongressionalcommitteeswhichhavenamesandsponsorbills,whichalsohavenames."

Prakash2018 VTCS4604 42

Prakash2018 VTCS4604 43

Prakash2018 VTCS4604 44

Prakash2018 VTCS4604 45

Prakash2018 VTCS4604 46

Prakash2018 VTCS4604 47

Prakash2018 VTCS4604 48

Prakash2018 VTCS4604 49

Prakash2018 VTCS4604 50

Notes:1.  SpeakerisNOTNULL

E/RExample2:CompanyDB§  A company database needs to store information about

employees (identified by ssn, with salary and phone asa t t r i b u t e s ) , d epa r tmen t s ( i d en t i f i e d b y dno ,with dname and budget as attributes), and children ofemployees (with name and age as attr ibutes) .Employees work in departments; each departmentis managed by an employee; a child must be identifieduniquely by name when the parent (who is an employee;assume that only one parent works for the company) isknown.

Prakash2018 VTCS4604 51

§  A company database needs to store information about employees(identified by ssn, with salary and phone as attributes), departments(identifiedbydno,withdnameandbudgetasattributes),andchildrenofemployees(withnameandageasattributes).

Note:Childisaweakentity

Prakash2018 VTCS4604 52

§  Employeesworkindepartments;§  eachdepartmentismanagedbyanemployee;

Prakash2018 VTCS4604 53

§  achildmustbe identifieduniquelybynamewhentheparent (who isanemployee;assumethatonlyoneparentworksforthecompany)isknown.

Prakash2018 VTCS4604 54

top related