e/r modelskirlinp/courses/db/f16/lectures/er-modeling.pdf§ in an entity-relationship diagram, each...

64
E/R Models (Chapter 4)

Upload: others

Post on 01-Apr-2021

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: E/R Modelskirlinp/courses/db/f16/lectures/er-modeling.pdf§ In an entity-relationship diagram, each entity set is represented by a rectangle. § Each attribute of an entity set is

E/RModels

(Chapter4)

Page 2: E/R Modelskirlinp/courses/db/f16/lectures/er-modeling.pdf§ In an entity-relationship diagram, each entity set is represented by a rectangle. § Each attribute of an entity set is

ThreePiecesofCourse

§ Databasedesign–Modelingdata

§ Databaseprogramming– SQL(otherlanguages)– Constructingapplications

§ Databaseimplementation– Learninghowthegutswork

Page 3: E/R Modelskirlinp/courses/db/f16/lectures/er-modeling.pdf§ In an entity-relationship diagram, each entity set is represented by a rectangle. § Each attribute of an entity set is

WhyLearnAboutDatabaseModeling?

§ ThewayinwhichdataisstoredisveryimportantforsubsequentaccessandmanipulationbySQL.

§ Propertiesofagooddatamodel:– Itiseasytowritecorrectandeasytounderstandqueries.– Minorchangesintheproblemdomaindonotchangethe

schema.– Majorchangesintheproblemdomain canbehandledwithout

toomuchdifficulty.– Cansupportefficientdatabaseaccess.

Page 4: E/R Modelskirlinp/courses/db/f16/lectures/er-modeling.pdf§ In an entity-relationship diagram, each entity set is represented by a rectangle. § Each attribute of an entity set is

PurposeoftheE/RModel

§ TheE/Rmodelallowsustosketchthedesignofadatabaseinformally.– Representdifferenttypesofdataandhowtheyrelatetoeachother

§ Designsaredrawingscalledentity-relationship diagrams.§ FairlymechanicalwaystoconvertE/Rdiagramstoreal

implementationslikerelationaldatabasesexist.

Page 5: E/R Modelskirlinp/courses/db/f16/lectures/er-modeling.pdf§ In an entity-relationship diagram, each entity set is represented by a rectangle. § Each attribute of an entity set is

PurposeofE/RModel

§ WhendesigningE/Rdiagrams,– forgetaboutrelations/tables!– onlyconsiderhowtomodeltheinformationyouneedtorepresentinyourdatabase.

Page 6: E/R Modelskirlinp/courses/db/f16/lectures/er-modeling.pdf§ In an entity-relationship diagram, each entity set is represented by a rectangle. § Each attribute of an entity set is

Tools

§ Entities(‘entitysets’)

§ Relationships(‘rel.sets’)andmappingconstraints

§ Attributes

N M

P

Page 7: E/R Modelskirlinp/courses/db/f16/lectures/er-modeling.pdf§ In an entity-relationship diagram, each entity set is represented by a rectangle. § Each attribute of an entity set is

EntitySets

§ Entity ="thing" or"objectinstance"or"noun"§ Entityset =collectionofsimilarentities.– Similartoaclassinobject-orientedlanguages.

§ Attribute =propertyofanentityset.– Generally,allentitiesinasethavethesamesetofproperties.

– Attributescanonlybe“primitive”types,likestrings,ints,floats.No“collection”typesorobjects.

Page 8: E/R Modelskirlinp/courses/db/f16/lectures/er-modeling.pdf§ In an entity-relationship diagram, each entity set is represented by a rectangle. § Each attribute of an entity set is

E/RDiagrams

§ Inanentity-relationshipdiagram,eachentitysetisrepresentedbyarectangle.

§ Eachattributeofanentitysetisrepresentedbyanoval,withalinetotherectanglerepresentingitsentityset.

Page 9: E/R Modelskirlinp/courses/db/f16/lectures/er-modeling.pdf§ In an entity-relationship diagram, each entity set is represented by a rectangle. § Each attribute of an entity set is

Example:EntitySets

Page 10: E/R Modelskirlinp/courses/db/f16/lectures/er-modeling.pdf§ In an entity-relationship diagram, each entity set is represented by a rectangle. § Each attribute of an entity set is

Relationships

§ Arelationshipconnectstwoormoreentitysets.

§ Itisrepresentedbyadiamond,withlinestoeachoftheentitysetsinvolved.

§ Don’tconfuse‘relationships’with‘relations’!

Page 11: E/R Modelskirlinp/courses/db/f16/lectures/er-modeling.pdf§ In an entity-relationship diagram, each entity set is represented by a rectangle. § Each attribute of an entity set is

InstanceofanE/RDiagram

§ E/Rdiagramdescribesaschema,nottheDBcontentitself.

§ However,wecanvisualizewhattheDBtuplesmightlooklikebythinkingofaninstance oftheE/Rdiagram:– containsinstancesof entitysetsand– instancesof relationshipsets.

Page 12: E/R Modelskirlinp/courses/db/f16/lectures/er-modeling.pdf§ In an entity-relationship diagram, each entity set is represented by a rectangle. § Each attribute of an entity set is

InstanceofanEntitySet

§ Foreachentityset,aninstancestoresaspecificsetofentities

§ Eachentityisatuplecontainingspecificvaluesforeachattribute

§ Whataretheexamplesofentitysetsforourrelationssofar?

Page 13: E/R Modelskirlinp/courses/db/f16/lectures/er-modeling.pdf§ In an entity-relationship diagram, each entity set is represented by a rectangle. § Each attribute of an entity set is

Instancesof(binary)relationshipsets

§ BinaryrelationwithentitiesE andF:§ Instanceisasetofpairs{(e, f):e isinE andf isinF}– InstanceneednotrelateeverytupleinE witheverytupleinF.Dependsonwhattherelationshipmeans.

§ (Atthemoment)Hardtovisualizeaninstanceofrelationshipsetasatable(orrelation)becauseeandf areentities,notsimplescalarvalues.

Page 14: E/R Modelskirlinp/courses/db/f16/lectures/er-modeling.pdf§ In an entity-relationship diagram, each entity set is represented by a rectangle. § Each attribute of an entity set is

Multiplicityofbinaryrelationships

§ Many-onefromAtoB:wheneachentityinAisconnectedtoatmostoneentityinB.– IfIgiveyouaparticularinstanceofentityA,youcangivemebackatmostoneentityinB.

– But,eachinstanceofBmayhavemultipleAs.§ One-one:whenarelationshipismany-onefromAtoBandfromBtoA.

§ Many-many: everythingelse.

Page 15: E/R Modelskirlinp/courses/db/f16/lectures/er-modeling.pdf§ In an entity-relationship diagram, each entity set is represented by a rectangle. § Each attribute of an entity set is

Many-ManyRelationships

§ Inamany-many relationship,anentityofeithersetcanbeconnectedtomanyentitiesoftheotherset.

Page 16: E/R Modelskirlinp/courses/db/f16/lectures/er-modeling.pdf§ In an entity-relationship diagram, each entity set is represented by a rectangle. § Each attribute of an entity set is

Many-OneRelationships

§ Somebinaryrelationshipsaremany-one fromoneentitysettoanother.

§ Eachentityofthefirstsetisconnectedtoatmost oneentityofthesecondset.

§ Butanentityofthesecondsetcanbeconnectedtozero,one,ormanyentitiesofthefirstset.

Page 17: E/R Modelskirlinp/courses/db/f16/lectures/er-modeling.pdf§ In an entity-relationship diagram, each entity set is represented by a rectangle. § Each attribute of an entity set is

One-OneRelationships

§ Inaone-onerelationship,eachentityofeitherentitysetisrelatedtoatmostoneentityoftheotherset.

Page 18: E/R Modelskirlinp/courses/db/f16/lectures/er-modeling.pdf§ In an entity-relationship diagram, each entity set is represented by a rectangle. § Each attribute of an entity set is

RepresentingMultiplicity

§ Showamany-onerelationshipby anarrowenteringthe"one" side.

§ Showaone-onerelationshipbyarrowsenteringbothentitysets.

Page 19: E/R Modelskirlinp/courses/db/f16/lectures/er-modeling.pdf§ In an entity-relationship diagram, each entity set is represented by a rectangle. § Each attribute of an entity set is

Differentkindsofrelationships

many-many

Profs. Advise Students

Take CoursesStudents Office RoomsProfs.

many-one one-one

Page 20: E/R Modelskirlinp/courses/db/f16/lectures/er-modeling.pdf§ In an entity-relationship diagram, each entity set is represented by a rectangle. § Each attribute of an entity set is

Exactlyone

§ Insomesituations,wecanalsoassert�exactlyone,� i.e.,eachentityofonesetmustberelatedtoexactlyoneentityoftheotherset.Todoso,weusearoundedarrow.

Page 21: E/R Modelskirlinp/courses/db/f16/lectures/er-modeling.pdf§ In an entity-relationship diagram, each entity set is represented by a rectangle. § Each attribute of an entity set is

Example:ExactlyOne

§ Considerfavorite-course betweenStudentsandCourses.

§ Somecoursesarenotthefavorite-courseofanystudent,soaroundedarrowtoStudentswouldbeinappropriate.

§ Butastudenthastohaveafavorite-course

Students CoursesFavoritecourse

Page 22: E/R Modelskirlinp/courses/db/f16/lectures/er-modeling.pdf§ In an entity-relationship diagram, each entity set is represented by a rectangle. § Each attribute of an entity set is

Attributesonrelationships

§ Attributescanalsobeplacedonarelationship,aswellasonanentityset.

§ Onlynecessaryiftheattributecannotbedeterminedfromasingleentityinstance.

§ Example:– StudentsandCourses:wheredowestoregrades?

Page 23: E/R Modelskirlinp/courses/db/f16/lectures/er-modeling.pdf§ In an entity-relationship diagram, each entity set is represented by a rectangle. § Each attribute of an entity set is

E/RDiagramsDay2:Review

§ Entitysets(rectangles)§ Attributes(ovals)§ Relationships(diamondsconnectingentitysets)

§ Multiplicityofrelationships(arrows)§ Runningexamples:BannerWeb-styleDB,bookstoreDB

Page 24: E/R Modelskirlinp/courses/db/f16/lectures/er-modeling.pdf§ In an entity-relationship diagram, each entity set is represented by a rectangle. § Each attribute of an entity set is

Multiway relationships

§ Rare§ AnarrowpointingtoentitysetEmeansifweselectoneentityfromeachoftheotherentitysetsintherelationship,thoseentitiesarerelatedto(atmost/exactly) oneentityinE.

§ Multiwayrelationships canoftenbeconvertedintomultiplebinaryrelationships. (later)

Page 25: E/R Modelskirlinp/courses/db/f16/lectures/er-modeling.pdf§ In an entity-relationship diagram, each entity set is represented by a rectangle. § Each attribute of an entity set is

RolesinRelationships

§ Canthesameentitysetappearmorethanonceinthesamerelationship?

§ PrerequisiterelationshipbetweentwoCourses

§ Butwhichcourseisthepre-req?

Page 26: E/R Modelskirlinp/courses/db/f16/lectures/er-modeling.pdf§ In an entity-relationship diagram, each entity set is represented by a rectangle. § Each attribute of an entity set is

RolesinRelationships

§ Labeltheconnectinglineswiththerole oftheentity

Page 27: E/R Modelskirlinp/courses/db/f16/lectures/er-modeling.pdf§ In an entity-relationship diagram, each entity set is represented by a rectangle. § Each attribute of an entity set is

ParallelRelationships

§ Cantherebemorethanonerelationshipbetweenthesamepairofentities?

§ TAandTakerelationshipbetweenStudentsandClasses

Page 28: E/R Modelskirlinp/courses/db/f16/lectures/er-modeling.pdf§ In an entity-relationship diagram, each entity set is represented by a rectangle. § Each attribute of an entity set is

ConvertingMultiway toBinary

§ Itiseasytoconvertamultiway relationshiptomultiplebinaryrelationships– Createanewconnectingentityset.Thinkofitsentitiesasthetuplesintherelationshipsetforthemultiway relationship

– Introducemany-onerelationshipsfromtheconnectingentitysettoeachoftheentitiesintheoriginalrelationship

– Ifanentitysetplays>1role,createarelationshipforeachrole

Page 29: E/R Modelskirlinp/courses/db/f16/lectures/er-modeling.pdf§ In an entity-relationship diagram, each entity set is represented by a rectangle. § Each attribute of an entity set is

Trythis

§ Partnersortriples.§ DesignanE/Rdiagramforabank,includinginfoaboutcustomersandaccounts.

§ Customerinfo:name,addr,phone,SSN.§ Accountinfo:type(checking/savings),balance.

§ Accountsmayhavemultiplecustomers;customersmayhavemultipleaccounts.

Page 30: E/R Modelskirlinp/courses/db/f16/lectures/er-modeling.pdf§ In an entity-relationship diagram, each entity set is represented by a rectangle. § Each attribute of an entity set is

Trythis

§ Whatifanaccountcanhaveonlyonecustomer?

§ Whatifacustomercanhaveonlyoneaccount?

§ Whatifacustomercanhavemultipleaddressesandmultiplephones?

§ (Thinkpre-cell-phones)Whatifwewanttoassociatephoneswithaddresses?

Page 31: E/R Modelskirlinp/courses/db/f16/lectures/er-modeling.pdf§ In an entity-relationship diagram, each entity set is represented by a rectangle. § Each attribute of an entity set is

Is-AHierarchies(Subclasses)

§ Certainentitiesmightneedtostorespecialpropertiesthatnotallentitiespossess.

§ Createtwoentitysets:a“super-entity”anda“sub-entity”andconnectthemwithaIs-Arelationship(triangleinsteadofdiamond).

Page 32: E/R Modelskirlinp/courses/db/f16/lectures/er-modeling.pdf§ In an entity-relationship diagram, each entity set is represented by a rectangle. § Each attribute of an entity set is

Gooddesignprinciples

§ Faithfulness– Entitysets&attributesshouldreflectrealityinchoiceofattributesandmultiplicityofrelationships.

– Thereal-worldsituationcandictatewhatfaithfulnessmeans.

– E/Rdiagramcannotconveyalltheinformation.– ConsiderStudents/Courses/Profs&multiplicity–canbedifferentwaystodothisdiagram.

Page 33: E/R Modelskirlinp/courses/db/f16/lectures/er-modeling.pdf§ In an entity-relationship diagram, each entity set is represented by a rectangle. § Each attribute of an entity set is

Gooddesignprinciples

§ Avoidredundancy–Watchoutforanattributeduplicatingarelationship.

§ Choosingtherightrelationships– Doeseveryrelationshipexpressalltheinformationyouneedittoexpress?

Page 34: E/R Modelskirlinp/courses/db/f16/lectures/er-modeling.pdf§ In an entity-relationship diagram, each entity set is represented by a rectangle. § Each attribute of an entity set is

Gooddesignprinciples

§ Pickinganattributeorentityset§ ReplaceEbyanattributewhen– AllrelationshipsinvolvingEmusthavearrowsenteringE.

– IfEhas>1attribute,thennoattributedependsonanyotherattribute.

– NorelationshipinvolvesEmorethanonce.

Page 35: E/R Modelskirlinp/courses/db/f16/lectures/er-modeling.pdf§ In an entity-relationship diagram, each entity set is represented by a rectangle. § Each attribute of an entity set is

KeysinE/Rdiagrams

§ Entitysetswillhaveoneormorekeys.– Customarytochooseaprimarykeyandunderlinetheattributes.

§ Possibleforanentityset'skeyattributestobelongtoanotherentitysetincertainsituations.– Is-ahierarchies– weakentitysets(later)

Page 36: E/R Modelskirlinp/courses/db/f16/lectures/er-modeling.pdf§ In an entity-relationship diagram, each entity set is represented by a rectangle. § Each attribute of an entity set is

Oneperspectiveonreal-worldkeys§ Multi-attributeand/orstringkeys…§ …canbetimeconsumingandsometimesmaynotguaranteealack

ofduplicates.– movie(title, year,date-released, etc)– title+year=lotstotypetoidentifyamovieinSQL.– integerkeymovieID saves typing!

§ …breakencapsulation– patient(first, last,DOB,etc)– Arethesekeysbeingtransmitted inaninsecuremanner? Isthisa

security/privacy risk?– integerkeypatientID fixes this.

§ …arebrittle– Namechange?Twomovieswiththesamename/year?– Unique integerIDalwaysexists,neverchanges.

Page 37: E/R Modelskirlinp/courses/db/f16/lectures/er-modeling.pdf§ In an entity-relationship diagram, each entity set is represented by a rectangle. § Each attribute of an entity set is

ReferentialintegrityinE/R

§ Referentialintegrity:requireseveryvalueofanattributeinonerelationtoappearasthevalueofanattributeinanother(orthesame)relation.

§ Enforcedthroughmultiplicityarrows§ Degreeconstraintscanbeaddedtofurtherrestrictmultiplicity.

Page 38: E/R Modelskirlinp/courses/db/f16/lectures/er-modeling.pdf§ In an entity-relationship diagram, each entity set is represented by a rectangle. § Each attribute of an entity set is

TryUSCongresshandout

§ TheUSCongressiscomposedoftheHouseofRepresentativesandtheSenate.

Page 39: E/R Modelskirlinp/courses/db/f16/lectures/er-modeling.pdf§ In an entity-relationship diagram, each entity set is represented by a rectangle. § Each attribute of an entity set is

Weakentitysets

§ Aweakentitysetisanentitysetwhose(primary)keycontainsattributesfromoneormoreotherentitysets.

§ Inotherwords,anentitysetEisweakifinordertoidentifyentitiesofEuniquely,weneedtofollowoneormoremany-onerelationshipsfromEandincludethekeyoftherelatedentitysetsinE'skey.

§ Possiblethatallattributesinaweakentityset'skeycomefromotherentitysets.

Page 40: E/R Modelskirlinp/courses/db/f16/lectures/er-modeling.pdf§ In an entity-relationship diagram, each entity set is represented by a rectangle. § Each attribute of an entity set is

Example§ Considerplayersinasportsleague:– Nameisnotakey(mightbeduplicatenames)– Numberiscertainlynotakey(numberswillbeduplicatedacrossteams)

– Butnumber+teamshouldbeakey

§ Usedoubleborderforweakentitysetsandtheirsupportingmany-onerelationships.

Players TeamsPlays-on

name namenumber

Page 41: E/R Modelskirlinp/courses/db/f16/lectures/er-modeling.pdf§ In an entity-relationship diagram, each entity set is represented by a rectangle. § Each attribute of an entity set is

Howaboutcoursesanddepartments?

Page 42: E/R Modelskirlinp/courses/db/f16/lectures/er-modeling.pdf§ In an entity-relationship diagram, each entity set is represented by a rectangle. § Each attribute of an entity set is

Keysforaweakentityset

§ ArelationshipRfromaweakentitysetEtoFissupporting if– Risabinary,many-onerelationshipfromEtoF.– RhasreferentialintegrityfromEtoF.

§ FsuppliesitskeyattributestodefineE'skey.§ IfFitselfisaweakentityset,thenwemustfindF’ssupportingrelationshipsandalsousethekeysfromthosesupportingentitysets.

Page 43: E/R Modelskirlinp/courses/db/f16/lectures/er-modeling.pdf§ In an entity-relationship diagram, each entity set is represented by a rectangle. § Each attribute of an entity set is

Wheredoweakentitysetscomefrom?

§ Cause1:Implicithierarchiesnotfroman"is-a"relationship.– Aplayer“belongsto”ateam,oraflight“isflownby”anairline.

– Happenswhenapieceofakeyisrepresentedasanentitysetratherthananattribute.• Can(technically) besolvedbyputtingauniqueIDonanentityset,butsometimes thiscausesmore troublethanit’sworth.

– "is-a"hierarchiesseemtoleadtoweakentitysets(subclasses),butwedon'tnotatethemwithdoublebordersbecausetheirhierarchicalrelationshipsarealwaysone-one.

Page 44: E/R Modelskirlinp/courses/db/f16/lectures/er-modeling.pdf§ In an entity-relationship diagram, each entity set is represented by a rectangle. § Each attribute of an entity set is

Wheredoweakentitysetscomefrom?

§ Cause2:Connectingentitysetscreatedbyeliminatingamulti-wayrelationship.– Often,connectingentitysetshavenoattributesoftheirown;theymustpickuptheirkeyattributesfromtheentitysetstheyconnect.

– Example:ACUSTOMERrentsaCARfromaSALESPERSON.

Page 45: E/R Modelskirlinp/courses/db/f16/lectures/er-modeling.pdf§ In an entity-relationship diagram, each entity set is represented by a rectangle. § Each attribute of an entity set is

ConvertingE/Rdiagramstorelationaldesigns

§ Entityset->Relation– Attributeofentityset->attributeofrelation– Keyofentityset->primarykeyofrelation

§ Relationship->Relation– Attributeofrelationship->attributeofrelation– Keyattributeofconnectingentityset->keyattributeofrelation

§ Specialcases:weakentitysets,"is-a"hierarchies,combiningrelations.

Page 46: E/R Modelskirlinp/courses/db/f16/lectures/er-modeling.pdf§ In an entity-relationship diagram, each entity set is represented by a rectangle. § Each attribute of an entity set is
Page 47: E/R Modelskirlinp/courses/db/f16/lectures/er-modeling.pdf§ In an entity-relationship diagram, each entity set is represented by a rectangle. § Each attribute of an entity set is

Handlingmultipleroles

Friend-of

Person

name

email

Requester

Recipient

IfanentitysetEappearsk>1timesinarelationshipR,thenthekeyattributesforEappearktimesintherelationforR,appropriatelyrenamed.

Page 48: E/R Modelskirlinp/courses/db/f16/lectures/er-modeling.pdf§ In an entity-relationship diagram, each entity set is represented by a rectangle. § Each attribute of an entity set is

Handlingweakentitysets

§ ForeachweakentitysetW,createarelationwithattributes:– attributesofW– attributesofsupportingrelationshipsforW– key attributesofsupportingentitysetsforW

Page 49: E/R Modelskirlinp/courses/db/f16/lectures/er-modeling.pdf§ In an entity-relationship diagram, each entity set is represented by a rectangle. § Each attribute of an entity set is

SupportingRelationships

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

§ WhatistheschemaforOffer?

Page 50: E/R Modelskirlinp/courses/db/f16/lectures/er-modeling.pdf§ In an entity-relationship diagram, each entity set is represented by a rectangle. § Each attribute of an entity set is

SupportingRelationships

§ Whatistheschemaforoffer?– Offer(Name,Number,DeptName)– ButNameandDeptName areidentical,sotheschemaforOfferisOffer(Number,DeptName)

– TheschemaforOfferisasubsetoftheschemafortheweakentityset,sowecandispensewiththerelationforOffer.

– Keypoint:Don'tmakearelationforsupportingrelationships.

Page 51: E/R Modelskirlinp/courses/db/f16/lectures/er-modeling.pdf§ In an entity-relationship diagram, each entity set is represented by a rectangle. § Each attribute of an entity set is

SummaryofWeakEntitySets

§ IfWisaweakentityset,therelationforWhasaschemawhoseattributesare– allattributesofW– allattributesofsupportingrelationships forW– foreachsupporting relationship forWtoanentitysetE

• thekeyattributes ofE

§ ThereisnorelationforanysupportingrelationshipforW

Page 52: E/R Modelskirlinp/courses/db/f16/lectures/er-modeling.pdf§ In an entity-relationship diagram, each entity set is represented by a rectangle. § Each attribute of an entity set is

CombiningRelations

§ Considermany-oneTeachrelationshipfromCoursestoProfessors

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

Page 53: E/R Modelskirlinp/courses/db/f16/lectures/er-modeling.pdf§ In an entity-relationship diagram, each entity set is represented by a rectangle. § Each attribute of an entity set is

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

§ ThekeyforCoursesuniquelydeterminesallattributesofTeach

§ WecancombinetherelationsforCoursesandTeachintoasinglerelationwhoseattributesare– AlltheattributesforCourses,– AnyattributesofTeach,and– ThekeyattributesofProfessors

Page 54: E/R Modelskirlinp/courses/db/f16/lectures/er-modeling.pdf§ In an entity-relationship diagram, each entity set is represented by a rectangle. § Each attribute of an entity set is

RulesforCombiningRelations§ WecancombineintoonerelationQ– TherelationforanentitysetE– allmany-to-onerelationshipsR1,R2,…,Rk fromEtootherentitysetsE1,E2,…,Ek respectively

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

§ Combiningamany-many relationshipwithoneofitsentitysetsoftenleadstoredundancy.Youprobablyneverwanttodothis!

Page 55: E/R Modelskirlinp/courses/db/f16/lectures/er-modeling.pdf§ In an entity-relationship diagram, each entity set is represented by a rectangle. § Each attribute of an entity set is

Is-atoRelational

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

Page 56: E/R Modelskirlinp/courses/db/f16/lectures/er-modeling.pdf§ In an entity-relationship diagram, each entity set is represented by a rectangle. § Each attribute of an entity set is

RulesSatisfiedbyanIs-aHierarchy

§ Thehierarchyhasarootentityset.§ Therootentitysethasakeythatidentifieseveryentityrepresentedbythehierarchy.

§ Aparticularentitycanhavecomponentsthatbelongtoentitysetsofanysubtree ofthehierarchy,aslongasthatsubtree includestheroot.

Page 57: E/R Modelskirlinp/courses/db/f16/lectures/er-modeling.pdf§ In an entity-relationship diagram, each entity set is represented by a rectangle. § Each attribute of an entity set is

ExampleISAhierarchy

Page 58: E/R Modelskirlinp/courses/db/f16/lectures/er-modeling.pdf§ In an entity-relationship diagram, each entity set is represented by a rectangle. § Each attribute of an entity set is

Is-atoRelationalMethodI:E/RApproach

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

– anyattributesofEitself§ Anentitywithcomponentsinmultipleentitysetshastuplesinalltherelationscorrespondingtotheseentitysets

§ Donotcreatearelationforanyis-arelationship§ Createarelationforeveryotherrelationship

Page 59: E/R Modelskirlinp/courses/db/f16/lectures/er-modeling.pdf§ In an entity-relationship diagram, each entity set is represented by a rectangle. § Each attribute of an entity set is

Is-atoRelationalMethodII:ObjectOrientedApproach

§ Treatentitiesasobjectsthataremembersofaparticularsubtreeinthetree.– Subtreesmustcontaintheroot.– Subtreesmaycontainmorethanoneentityset.

§ Whatareallthelogically-possibleclassesforbooksinourhierarchy?

Page 60: E/R Modelskirlinp/courses/db/f16/lectures/er-modeling.pdf§ In an entity-relationship diagram, each entity set is represented by a rectangle. § Each attribute of an entity set is

Is-atoRelationalMethodII:ObjectOrientedApproach

§ Enumerateallsubtrees ofthehierarchythatcontaintheroot.

§ Foreachsuchsubtree,– Createarelationthatrepresentsentitiesthathavecomponentsinexactlythatsubtree.

– Theschemaforthisrelationhasalltheattributesofalltheentitysetsinthatsubtree.

Page 61: E/R Modelskirlinp/courses/db/f16/lectures/er-modeling.pdf§ In an entity-relationship diagram, each entity set is represented by a rectangle. § Each attribute of an entity set is

Is-atoRelationalMethodIII:“Flatten”Approach(or"NULLs")

§ Makeonerelationforthewholehierarchicalstructure.

§ UseNULLforanyattributethatisnotdefinedforaparticularentity.

Page 62: E/R Modelskirlinp/courses/db/f16/lectures/er-modeling.pdf§ In an entity-relationship diagram, each entity set is represented by a rectangle. § Each attribute of an entity set is

ComparisonoftheThreeApproaches

§ Trade-offs– Ingeneral,wewanttominimizejoins(takestime)andalsominimizeduplicatedorredundantinformation(takesspace[memory]).

– Itisexpensivetoanswerqueriesinvolvingseveralrelations(advantage:flatten)

– E/Rapproachworkswellforsomequerieswhereinfoisduplicatedamongrelations.

– E/Rapproachishardforotherqueriesbecausewemayneedjoins.

Page 63: E/R Modelskirlinp/courses/db/f16/lectures/er-modeling.pdf§ In an entity-relationship diagram, each entity set is represented by a rectangle. § Each attribute of an entity set is

ComparisonoftheThreeApproaches

§ Numberofrelationsfornrelationsinthehierarchy–Weliketohaveasmallnumberofrelations– Flatten• 1

– E/R• n

– OO• Canbe2^n

Page 64: E/R Modelskirlinp/courses/db/f16/lectures/er-modeling.pdf§ In an entity-relationship diagram, each entity set is represented by a rectangle. § Each attribute of an entity set is

ComparisonoftheThreeApproaches

§ Redundancyandspaceusage– Flatten• MayhavealargenumberofNULLs• (alsopreventsyoufromusingNULLtodenotesomethingbesidesclassmembership)

– E/R• Severaltuplesperentity,butonlykeyattributesarerepeated

– OO• Onlyonetupleperentity