L09: ER modeling 2 - Northeastern University · 73 1./2. Cardinality + participation constraints...

Preview:

Citation preview

64

L09:ERmodeling2

CS3200 Databasedesign(sp18 s2)https://course.ccs.neu.edu/cs3200sp18s2/2/8/2018

65

Announcements!

• Avoidsittinginthelastrow• Slidesarenowsplitupperlecture• Classattendance:Remainingnametags• P1 tobereleasedtoday• HW collaborationpolicies• Exam1nextMonday.Practiceexam0today!- BB:filesavailableunder"assignments"at11:55- Nextweek:60min,SQLonly,1cheatsheet,tobehandedinafterclass,signhonorcode

• Outline- Practiceexam0- ERmodeling

66

Customer/Purchase

Purchaseoiddatecidamount

Customercidnameageaddresssalary

391

Q1: Find the number of purchases for each customer. Return customer name and number of purchases.

67

Back to ERDs:Relationships

68

Overview:3importantconceptsforrelationships

• Cardinality ("arity"):numberofentityinstancesthatparticipate(~mainlymax)

• Participationconstraints:mandatoryoroptional(equivalenttominimumcardinality0or1)

• Degree:numberofentitytypesthatparticipate

Randomquestion:Why"relationalmodel"?

69

1.Cardinality(multiplicity)ofrelationships• Definesthenumberofentityinstances thatparticipateinit.Alsocalledtypeofrelationship

(or"connectivity"attimes).

• One-to-One- Eachentityintherelationshipwillhaveexactlyonerelatedentity

• One-to-Many- Anentityononesideoftherelationshipcanhavemanyrelatedentities,butanentityontheotherside

willhaveamaximumofonerelatedentity

• Many-to-Many- Entitiesonbothsidesofthe

relationshipcanhavemanyrelatedentitiesontheotherside

Thisnotationisalsocalled"crow'sfootnotation"

70

1.Cardinality(multiplicity)ofE/Rrelationships

123

abcd

One-to-one:

123

abcd

One-to-many:

123

abcd

Many-to-many:

X->YmeansthereexistsafunctionmappingfromXtoY(recallthedefinitionofafunction)

Stanford'sArrownotation Crow'sFeet

71

address name ssn

Person

buys

makes

employs

CompanyProduct

name category

stockprice

name

price

Whatdoesthissay?

72

2.Participationconstraints

Whetherarelationismandatoryoroptionalisalsosometimescalled"participationconstraints"

• Thenumberofinstancesofoneentitythatcanormustbeassociatedwitheachinstanceofanotherentity

• Minimum:ifzero,thenoptional,ifoneormore,thenmandatory

• Maximum

- Mandatory one

- Mandatorymany

- Optional one

- Optionalmany

73

1./2.Cardinality+participationconstraints

• Definestherulesoftheassociationbetweenentities

Customer makes Order

Thisisaone-to-manyrelationship:Onecustomercanhavemanyorders.ThesymbolsclosesttotheOrderentityrepresents"one,ormany",whereasanorderhas"oneandonlyone"Customer.

at least – oneat most - many

at least – oneat most - one

Source:MIS5101,DataManagementandWarehousing,Temple.

74

Ex:cardinality+participationconstraints(1/2)

Source:Fig.2.17:Hoffer,Ramesh,Topi,"Moderndatabasemanagement,"10th ed,2010.

PATIENT PATIENTHISTORY

PleaseinterprettheconstraintsinthefollowingERdiagram:

Cardinalities+Participation

75

Apatientmusthaverecor-dedatleastonehistory,andcanhavemany

Apatienthistoryisrecordedforoneandonlyonepatient

Ex:cardinality+participationconstraints(1/2)

Source:Fig.2.17:Hoffer,Ramesh,Topi,"Moderndatabasemanagement,"10th ed,2010.

PATIENT PATIENTHISTORY

Alice

Bob

Charlie

Visit1Visit2Visit3Visit4

Cardinalities+Participation

PleaseinterprettheconstraintsinthefollowingERdiagram:

76

Ex:cardinality+participationconstraints(2/2)

Source:Fig.2.17:Hoffer,Ramesh,Topi,"Moderndatabasemanagement,"10th ed,2010.

EMPLOYEE PROJECT

PleaseinterprettheconstraintsinthefollowingERdiagram:

Cardinalities+Participation

77

Anemployeecanbeassignedtoanynumberofprojects,ormaynotbeassignedtoanyatall

Aprojectmustbeassignedtoatleastoneemployee,andmaybeassignedtomany

Ex:cardinality+participationconstraints(2/2)

Source:Fig.2.17:Hoffer,Ramesh,Topi,"Moderndatabasemanagement,"10th ed,2010.

EMPLOYEE PROJECT

Alice

Bob

Charlie

Project1

Project2

Project3

Project4Dorothy

PleaseinterprettheconstraintsinthefollowingERdiagram:

Cardinalities+Participation

78

ParticipationconstraintsinChennotation

Each student must be guided by one professor.A professor professor can (but does not have to) guide one or more students

Total participation means that every entity in the set is involved in the relationship.

Source:http://www.vertabelo.com/blog/technical-articles/chen-erd-notation

79

3.DegreeofRelationship

• Definesthenumberofentitytypes thatparticipateinit

UnaryRelationship- Oneentitytyperelatedtoitself

BinaryRelationship- Twoentitytypesrelatedtoeachother

TernaryRelationship- Threeentitytypesrelatedtoeachother

• Itispossible,butunusual,tohaverelationshiptypesoflargerthan3entities

80

Multi-wayRelationships

Howdowemodelapurchaserelationshipbetweenbuyers,productsandstores?

Purchase

Product

Person

Store

81

ArrowsinMultiwayRelationshipsQ:Whatdoesthearrowmean?

Purchase

Product

Person

Store

82

ArrowsinMultiwayRelationshipsQ:Whatdoesthearrowmean?

Purchase

Product

Person

Store

83

ArrowsinMultiwayRelationshipsQ:Howdowesaythateverypersonshopsinatmostonestore?

A: Cannot. This is the best approximation.(Why only approximation ?)

Product

Person

StorePurchase

84

ConvertingMulti-wayRelationshipstoBinary

Fromwhatwehadonpreviousslidetothis- whatdidwedo?

Purchase

Person

Store

Product

StoreOf

ProductOf

BuyerOf

date

85

ConvertingMulti-wayRelationshipstoNewEntity+BinaryRelationships

Purchase

Person

Store

Product

StoreOf

ProductOf

BuyerOf

date

Sidenote:Whatarrowsshouldbeaddedhere?Arethesecorrect?

86

Decision:Multi-wayorNewEntity+Binary?

Shouldweuseasinglemulti-wayrelationshiporanewentitywithbinaryrelations?

Purchase

Person

Store

Product

StoreOf

ProductOf

BuyerOf

date

Purchase

Product

Person

Store

Multi-wayRelationship Entity+Binary

87

Decision:Multi-wayorNewEntity+Binary?

(A)Multi-wayRelationship (B)Entity+Binary

• Coveredearlier:(B)isusefulifwewanttohavemultipleinstancesofthe“relationship”perentitycombination

Multiplepurchasesper(product,store,person)combopossiblehere!

Purchase

Person

Store

Product

StoreOf

ProductOf

BuyerOf

date

Purchase

Product

Person

Store

• (A)isusefulwhenarelationshipreallyisbetweenmultipleentities- Ex: A three-party legal contract

88

Weak (or dependent)

Entities

89

Strongvs.Weak(Dependent)Entities• Strongentities

- Canbeidentified("exist")independently ofothertypesofentities- Havetheirownuniqueidentifier

• Weakentities- Dependentonastrongentity,

cannotexistontheirown(better:cannotbeidentifiedindependently)

- Donothaveuniqueidentifiers:PKoverlapswithparent'sPK

- (representedwithdouble-linerectangle)

• Identifyingrelationship- Linksstrongentitiestoweakentities- Representedwithdoublelinerelationship

Entitysetsareweakwhenpartoftheiridentifiercomesfromclassestowhichtheyarerelated

90

Example:StrongandWeakEntities

• Employeecariesonedependent- Employee:ID,name- Dependent:name,Date_of_Birth

Source:Fig2.5.Hoffer,Ramesh,Topi,"Moderndatabasemanagement,"10th ed,2010.

Identifying relationship

Strong entity= identifying owner

Weak entity= dependent entity

Partial identifier

Note we need both EMPLOYEE_ID and DEPENDENT_NAME to uniquely identify a dependent

91

AlternativenotationsforsamescenarioEMPLOYEE

*EMPLOYEE_ID

EMPLOYEE_NAME *DEPENDENT_NAME

DATE_OF_BIRTH

DEPENDENT

has

Dependentpolicy

DOB name

Employee

namedeptssn

92

Participationconstraintsandweakentities

Source:http://www.vertabelo.com/blog/technical-articles/chen-erd-notation

93

AssociativeEntities

94

AssociativeEntities

• It seemslikearelationship:itlinksentitiestogether• Yetitseemslikeanentity:ithasattributes• Whenshouldarelationshipwithattributesinsteadbeanassociativeentity?- Allrelationshipsfortheassociativeentityshouldbemany- Theassociativeentitycouldhavemeaningindependentoftheotherentities- Theassociativeentitypreferablyhasauniqueidentifier(butnotnecessarily),and

shouldalsohaveotherattributes- Ternaryrelationships shouldbeconvertedtoassociativeentities- (Theassociativeentitymayparticipateinotherrelationshipsotherthantheentitiesof

theassociatedrelationship)

95Source:Hoffer,Ramesh,Topi,"Moderndatabasemanagement,"10th ed,2010.

Example:CertificateAssociativeEntity(1/3)• Associativeentitycanberepresentedasrectanglewithroundedcorners(Hoffernotation)• Notethatthemany-to-manycardinalitysymbolsfacetoward

theassociativeentityandnottowardtheotherentities

96Source:Hoffer,Ramesh,Topi,"Moderndatabasemanagement,"10th ed,2010.

Example:CertificateAssociativeEntity(1/3)• Associativeentitycanberepresentedasrectanglewithroundedcorners(Hoffernotation)• Notethatthemany-to-manycardinalitysymbolsfacetoward

theassociativeentityandnottowardtheotherentities

97

Example:CertificateAssociativeEntity(2/3)

*CERTIFICATE_NUMBER

DATE_COMPLETED

EMPLOYEE

*EMPLOYEE_ID

EMPLOYEE_NAME

COURSE

*COURSE_ID

COURSE_TITLE

EMPLOYEE*EMPLOYEE_ID

EMPLOYEE_NAME

COURSE*COURSE_ID

COURSE_TITLEGets Certified

CERTIFICATE

Relationship

AssociativeEntity

DATE_COMPLETED

98

Example:CertificateAssociativeEntity(3/3)

Source:Hoffer,Ramesh,Topi,"Moderndatabasemanagement,"10th ed,2010.

NotationusedbyMicrosoftVisio

Important!NoticethatERDsdonotshowforeignkeys!

99

CASTinourIMDBmoviedatabase

100Source:Fig2-21,Hoffer,Ramesh,Topi,"Moderndatabasemanagement,"10th ed,2010.

Multiplerelationships

101

Here,mincardinalityconstraintis2:Atleasttwoprofessorsmustbequalifiedtoteacheachcourse

Source:Fig2-21,Hoffer,Ramesh,Topi,"Moderndatabasemanagement,"10th ed,2010.

Multiplerelationships