CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos -...

80
CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams

Transcript of CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos -...

Page 1: CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams.

CMU SCS

Carnegie Mellon Univ.Dept. of Computer Science

15-415/615 - DB Applications

C. Faloutsos - A. Pavlo

Lecture#2: E-R diagrams

Page 2: CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams.

CMU SCS

Problem

• Develop an application for U.G. admin:– Student info– Who-takes-what class– Class rosters– Transcripts

• How do you proceed?– (Which role(s) are you playing?)

Faloutsos - Pavlo CMU SCS 15-415/615 2

Page 3: CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams.

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 3

Database Design

• Requirements Analysis

• Conceptual Design

• Logical Design

• Schema Refinement

• Physical Design

• Security Design

Page 4: CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams.

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 4

Database Design

• Requirements Analysis

• Conceptual Design

• Logical Design

• Schema Refinement

• Physical Design

• Security Design

user’s needs

high level (ER)

Tables

Normalization

Indices etc

Access controls

Page 5: CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams.

CMU SCS

Problem’

• Develop an application for U.G. admin:– Student info– Who-takes-what class– Class rosters– Transcripts

• If you are the *new* DBA, what would you rather inherit:

Faloutsos - Pavlo CMU SCS 15-415/615 5

Maintain

Page 6: CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams.

CMU SCS

This or this ?

drop table if exists student;

create table student (ssn fixed, name char(20)); drop table if exists takes;create table takes (ssn fixed, cid char(10), grade fixed);

Faloutsos - Pavlo CMU SCS 15-415/615 6

Student

Course

Takes

Page 7: CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams.

CMU SCS

True story• Health insurance company

• Wants to catch (some of the abundant) fraud

• Schema: – patients, visit doctors, get medicine, – Doctors perform operations, …– Nurses monitor patients, …– etc etc

• Q: How many tables do you think it spans?

Faloutsos - Pavlo CMU SCS 15-415/615 7

Page 8: CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams.

CMU SCS

True story

• Schema: – patients, visit doctors, get medicine, – Doctors perform operations, …– Nurses monitor patients, …– etc etc

• Q: How many tables do you think it spans? 10? 20? 30?

Faloutsos - Pavlo CMU SCS 15-415/615 8

Page 9: CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams.

CMU SCS

True story

• Schema: – patients, visit doctors, get medicine, – Doctors perform operations, …– Nurses monitor patients, …– etc etc

• Q: How many tables do you think it spans? 10? 20? 30?

• A: 120 PAGES of schemaFaloutsos - Pavlo CMU SCS 15-415/615 9

Page 10: CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams.

CMU SCS

Motivation & upcoming conclusion:

• E-R diagrams are excellent documentation tools

Faloutsos - Pavlo CMU SCS 15-415/615 10

Student

Course

Takes

Page 11: CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams.

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 11

Overview

• concepts– Entities– Relationships– Attributes– Specialization/Generalization– Aggregation– ER modeling questions

Page 12: CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams.

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 12

Tools

Entities (‘entity sets’)

Relationships (‘rel. sets’)

and mapping constraints

attributes

N M

P

Student

Course

Takes

Page 13: CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams.

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 13

Example

Students, taking courses, offered by instructors; a course may have multiple sections; one instructor per section

nouns -> entity sets

verbs -> relationship sets

Page 14: CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams.

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 14

Example

Students, taking courses, offered by instructors; a course may have multiple sections; one instructor per section

nouns -> entity sets

verbs -> relationship sets

Page 15: CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams.

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 15

Example

Students, taking courses, offered by instructors; a course may have multiple sections; one instructor per section

nouns -> entity sets

verbs -> relationship sets

Page 16: CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams.

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 16

STUDENT

name

ssn

...

INSTRUCTORissn primary key =

unique identifier ->

underline

Page 17: CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams.

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 17

STUDENT

name

ssn

...

COURSE

c-id

c-name

but: sections of course (with different instructors)?

INSTRUCTORissn

Page 18: CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams.

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 18

COURSE

c-id

INSTRUCTORissn

STUDENT

ssn

SECTIONs-id

but: s-id is not unique... (see later)

Page 19: CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams.

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 19

COURSE

c-id

INSTRUCTORissn

STUDENT

ssn

SECTIONs-id

Q: how to record that students take courses?

Page 20: CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams.

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 20

COURSE

c-id

INSTRUCTORissn

STUDENT

ssn

SECTIONs-id

takes

N

M

Page 21: CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams.

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 21

COURSE

c-id

INSTRUCTOR

STUDENT

SECTIONs-id

takes

N

M

N

1

teaches

Page 22: CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams.

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 22

COURSE

c-id

INSTRUCTOR

STUDENT

SECTIONs-id

takes

N

M

N

1

hasN 1

teaches

Page 23: CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams.

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 23

Cardinalities

• 1 to 1 (example?)

• 1 to N

• N to M

1 1

1 N

N M

Page 24: CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams.

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 24

Cardinalities1 1

1 N

N M

COUNTRY CAPITAL

PERSON

STUDENT SECTION

has

CARowns

takes

Page 25: CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams.

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 25

Cardinalities

PERSON

STUDENT SECTION

CARowns

takes

Book’s notation:

COUNTRY has CAPITAL

Page 26: CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams.

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 26

Cardinalities1 1

1 N

N M

PERSON

STUDENT SECTION

CARowns

takes

Book’s notation

vs

1 to N notation

CAPITALCOUNTRY has

Page 27: CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams.

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 27

Cardinalities1 1

1 N

N M

PERSON

STUDENT SECTION

CARowns

takes

CAPITALCOUNTRY has

Page 28: CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams.

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 28

‘Total/partial’ participation1:1 1:1

?:1 ?:N

?:N ?:M

PERSON

STUDENT SECTION

CARowns

takes

total, total

??

??

COUNTRY has CAPITAL

Page 29: CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams.

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 29

‘Total/partial’ participation1:1 1:1

1:1 0:N

?:N ?:M

PERSON

STUDENT SECTION

CARowns

takes

total, total

??

partial, total

COUNTRY has CAPITAL

Page 30: CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams.

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 30

‘Total/partial’ participation1:1 1:1

1:1 0:N

1:N 0:M

PERSON

STUDENT SECTION

CARowns

takes

total, total

partial, total

partial, total

COUNTRY has CAPITAL

Page 31: CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams.

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 31

‘Total/partial’ participation

1:1 0:NPERSON CARowns

partial, total

Is it ‘legal’?

Page 32: CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams.

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 32

‘Total/partial’ participation

1:1 0:NPERSON CARowns

partial, total

Is it ‘legal’?

NO! why not?

Page 33: CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams.

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 33

Subtle concept: Weak entities• ‘section’ has no unique-id of its own!(?)

COURSE

c-id

SECTIONs-id hasN 1

Page 34: CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams.

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 34

Weak entities• ‘weak’ entities: if they need to borrow a unique id from a ‘strong entity - thick box.• ‘c-id’ + ‘s-id’: unique id for SECTION• partial key (eg., ‘s-id’) - dashed-underline• identifying relationship (eg., ‘has’)

COURSE

c-id

SECTIONs-id hasN 1

Page 35: CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams.

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 35

Weak entities• Other example(s) of weak entities?

???

??id

????? ??N 1

Page 36: CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams.

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 36

Weak entities• Other example(s) of weak entities?

Employee

e-id

Dependentname hasN 1

Page 37: CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams.

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 37

More details

• self-relationships - example?

??

????

??

Page 38: CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams.

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 38

More details

• self-relationships - example?

EMPLOYEE

manages1

N

Page 39: CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams.

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 39

More details

• self-relationships - example?

FB user

Has-friend??

??

Page 40: CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams.

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 40

More details

• 3-way and k-way relationships?

Page 41: CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams.

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 41

More details

• 3-way and k-way relationships? Rare, but possible:

EMPLOYEE uses

PROJECT

TOOLN M

P

Page 42: CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams.

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 42

More details

• 3-way and k-way relationships? Rare, but possible:

user reviews

app

keyword

N M

P

App-store/amazon

reviews

Page 43: CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams.

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 43

Overview

• concepts– Entities– Relationships– Attributes– Specialization/Generalization– Aggregation– ER modeling questions

Page 44: CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams.

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 44

More details - attributes

• key (or primary key): unique identifier

• underlined, in the ER diagram• [not in textbook - FYI:

– multivalued or set-valued attributes (eg., ‘dependents’ for EMPLOYEE)

– derived attributes (eg., 15% tip)

]

Page 45: CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams.

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 45

Overview

• concepts– Entities– Relationships– Attributes– Specialization/Generalization– Aggregation– ER modeling questions

Basic

Advanced/

rare

Page 46: CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams.

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 46

Specialization

• eg., students: part time (#credit-hours) and full time (major)

STUDENT

name

ssn

PT-STUDENT

#credits

FT-STUDENTmajor

IS-A

Page 47: CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams.

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 47

Observations

• Generalization: exact reverse of ‘specialization’

• attribute inheritance

• could have many levels of an IS-A hierarchy

Page 48: CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams.

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 48

More details

• Overlap constraints

• Covering constraints

A

B C

Page 49: CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams.

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 49

More details

• Overlap constraints– can an entity belong to both

‘B’ and ‘C’?

• Covering constraints– can an ‘A’ entity belong to

neither ‘B’ nor ‘C’?

A

B C

Page 50: CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams.

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 50

More details

• Overlap constraints - examples?

A

B CA

B C

No overlap

A

B C

with overlap

Page 51: CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams.

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 51

More details

• Covering constraints - examples?

A

B C

A

B C

Total coverage

A

B C

Partial coverage

Page 52: CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams.

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 52

Overview

• concepts– Entities– Relationships– Attributes– Specialization/Generalization– Aggregation– ER modeling questions

Page 53: CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams.

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 53

Aggregation

CPU HD

• computer model (w/ CPU and HD)• and Maker (eg., Dell, HP)

N MMAKER

?

Page 54: CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams.

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 54

Aggregation

• treat a relationship as an entity• used to express a relationship among relationships

CPU HDN M

MAKER

Page 55: CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams.

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 55

Overview

• concepts– Entities– Relationships– Attributes– Specialization/Generalization– Aggregation– ER modeling questions

Page 56: CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams.

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 56

Conceptual design

• Entity vs attribute

• Entity vs relationship

• Binary or ternary relationships?

• Aggregation?

Page 57: CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams.

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 57

Entity vs. attribute

• Entity EMPLOYEE (w/ emp#, name, job_code, ...)

• Q: How about ‘spouse’ - entity or attribute?

• Q: How about ‘dependents’?

Page 58: CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams.

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 58

Entity vs. attribute

• Entity EMPLOYEE (w/ emp#, name, job_code, ...)

• Q: How about ‘spouse’ - entity or attribute?

• A: probably, ‘attribute’ is enough

• Q: How about ‘dependents’?

• A: Entity - we may have many dependents

Page 59: CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams.

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 59

Entity vs. Relationship

STUDENT

SECTION

takes

N

M

STUDENT

SECTION

TAKES

1

N

1

N

OR

Page 60: CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams.

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 60

Binary vs Ternary Relationships

• usually, binary relationships are ‘cleaner’:

Page 61: CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams.

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 61

Binary vs. Ternary Relationships

If each policy is owned by just 1 employee: Policies

policyid cost

agepname

DependentsCovers

name

Employees

ssn lot

Page 62: CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams.

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 62

Binary vs. Ternary Relationships

If each policy is owned by just 1 employee:

Bad design Policies

policyid cost

agepname

DependentsCovers

name

Employees

ssn lot

Page 63: CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams.

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 63

Binary vs. Ternary Relationships

If each policy is owned by just 1 employee:

Bad design Policies

policyid cost

agepname

DependentsCovers

name

Employees

ssn lot

Key constraint on Policies would mean policy can only cover 1 dependent!

Page 64: CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams.

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 64

Binary vs. Ternary Relationships

If each policy is owned by just 1 employee:

Bad design

Beneficiary

agepname

Dependents

policyid cost

Policies

Purchaser

name

Employees

ssn lot

Better design

What are the additional constraints in the 2nd diagram?

Policies

policyid cost

agepname

DependentsCovers

name

Employees

ssn lot

Key constraint on Policies would mean policy can only cover 1 dependent!

Page 65: CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams.

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 65

Binary vs Ternary Rel.

• But sometimes ternary rel. can not be replaced by a set of binary rel’s:

Page 66: CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams.

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 66

Binary vs. Ternary Relationships (Contd.)

Suppliers

qty

DepartmentsContractParts

Suppliers

Departments

deals-with

Parts

can-supply

VS.

why is it bad?

Page 67: CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams.

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 67

Binary vs. Ternary Relationships (Contd.)

– S “can-supply” P, D “needs” P, and D “deals-with” S does not imply that D has agreed to buy P from S.

– How do we record qty?

Suppliers

qty

DepartmentsContractParts

Suppliers

Departments

deals-with

Parts

can-supply

VS.

Page 68: CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams.

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 68

Binary vs. Ternary Relationships (Contd.)

Suppliers

qty

DepartmentsContractParts

Suppliers

qty

DepartmentsContractParts

Not in textbook:

in practice, often:

Page 69: CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams.

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 69

Binary vs. Ternary Relationships (Contd.)

Suppliers

qty

DepartmentsContractParts

c-id

Not in textbook:

in practice, often:

Page 70: CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams.

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 70

Binary vs. Ternary Relationships (Contd.)

Suppliers

qty

DepartmentsContractParts

c-id

Not in textbook:

in practice, often:

NM

1

N 1N

Page 71: CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams.

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 71

Ternary vs. aggregation

• use aggregation, if we want to attach a relationship to a relationship

• (see book for example)

• (in practice, again we create a unique-id and resort to binary relationships)

Page 72: CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams.

CMU SCS

Ternary vs. aggregation

• How would you handle this case?

Faloutsos - Pavlo CMU SCS 15-415/615 72

CPU HDN M

MAKER

Page 73: CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams.

CMU SCS

Ternary vs. aggregation

• How would you handle this case?

Faloutsos - Pavlo CMU SCS 15-415/615 73

CPU HD MAKER

COMP. MODEL

Page 74: CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams.

CMU SCS

Ternary vs. aggregation

• How would you handle this case?

Faloutsos - Pavlo CMU SCS 15-415/615 74

CPU HD?

?

MAKER

COMP. MODEL

?

?

Page 75: CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams.

CMU SCS

Ternary vs. aggregation

• How would you handle this case?

Faloutsos - Pavlo CMU SCS 15-415/615 75

CPU HDN

M

MAKER

COMP. MODEL

N

MHAS_CPU

Page 76: CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams.

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 76

Summary

• E-R Diagrams: a powerful, user-friendly tool for data modeling:– Entities (strong, weak)– Attributes (primary keys, discriminators,

derived, multivalued)– Relationships (1:1, 1:N, N:M; multi-way)– Generalization/Specialization; Aggregation

Page 77: CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams.

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 77

Summary

• E-R Diagrams: a powerful, user-friendly tool for data modeling:– Entities (strong, weak)– Attributes (primary keys, discriminators,

derived, multivalued)– Relationships (1:1, 1:N, N:M; multi-way)– Generalization/Specialization; Aggregation

PO

PU

LA

R

Page 78: CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams.

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 78

Summary - cont’d

(strong) entity set

weak entity set

relationship set

identifying rel. set

for weak entity

attribute

primary keyA

partial keyA

Page 79: CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams.

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 79

Summary - cont’d

cardinalitiesN M

cardinalities

with limits

l:h l’:h’

cardinalities

partial/total

(not in textbook - FYI)

Page 80: CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams.

CMU SCS

Faloutsos - Pavlo CMU SCS 15-415/615 80

Summary - cont’d

IS-A

aggregation