CSE 480: Database Systems

28
1 CSE 480: Database Systems Lecture 6: ER to Relational Mapping Reference: Read Chapter 9 of the textbook

description

CSE 480: Database Systems. Lecture 6: ER to Relational Mapping. Reference: Read Chapter 9 of the textbook. The ER diagram for COMPANY database. Mapping ER diagram into relational schema. Mapping ER diagram into relational schema. Map everything as separate tables. - PowerPoint PPT Presentation

Transcript of CSE 480: Database Systems

Page 1: CSE 480: Database Systems

1

CSE 480: Database Systems

Lecture 6: ER to Relational Mapping

Reference:

Read Chapter 9 of the textbook

Page 2: CSE 480: Database Systems

2

The ER diagram for COMPANY database

Page 3: CSE 480: Database Systems

3

Mapping ER diagram into relational schema

Page 4: CSE 480: Database Systems

4

Mapping ER diagram into relational schema

Map everything as separate tables

ProjNumber ProjName

ProjNumber ProjLocation

Query processing becomes expensive

E.g.: Find me the names of all projects located in Michigan

Need to perform many join operations

Page 5: CSE 480: Database Systems

5

Mapping ER diagram into relational schema

Map everything into 1 table

1 1EMPLOYEE DEPARTMENT

MANAGES

WorksFor

1N

Too much redundancy

Page 6: CSE 480: Database Systems

6

Outline

Mapping ER Constructs to Relations– Step 1: Mapping of Regular (Strong) Entity Types

– Step 2: Mapping of Weak Entity Types

– Step 3: Mapping of Binary 1:1 Relation Types

– Step 4: Mapping of Binary 1:N Relationship Types.

– Step 5: Mapping of Binary M:N Relationship Types.

– Step 6: Mapping of Multivalued attributes.

– Step 7: Mapping of N-ary Relationship Types.

The procedure– Avoids generating too many unnecessary tables

– Avoids too much redundancy in tables

– More details in Chapter 10 (normal forms)

Page 7: CSE 480: Database Systems

7

Step 1: Mapping of Regular (Strong) Entity Types– For each strong entity type E, create a relation R

Include all the simple attributes of E as columns in R Include component attributes of a composite attribute as columns in R Ignore the derived attributes Choose one of the key attributes of E as the primary key for R.

– If the chosen key attribute of E is composite, the set of simple attributes that form it will together form the primary key of R.

ER-to-Relational Mapping Algorithm

E

ID B

C D

Entity type, E

ID C D

R

Relation, R

Page 8: CSE 480: Database Systems

8

Example

PRIMARY KEY (Ssn)

Page 9: CSE 480: Database Systems

9

Example

PRIMARY KEY (Dnumber)UNIQUE(Dname)

PRIMARY KEY (Pnumber)UNIQUE(Pname)

secondary keys(can be null)

Page 10: CSE 480: Database Systems

10

ER-to-Relational Mapping Algorithm

Step 2: Mapping of Weak Entity Types– For each weak entity type W with owner entity type O, create a

relation R

Include all simple attributes of W as columns in R Include components of a composite attribute as columns in R Include primary key attribute(s) of the the owner entity type(s) O as

foreign key attributes of R Primary key of R is the combination of the primary key(s) of the

owner(s) and the partial key of the weak entity type W

O W1 N

Id

A

P

QR

S

Page 11: CSE 480: Database Systems

11

ER-to-Relational Mapping Algorithm

Step 2: Mapping of Weak Entity Types

O W1 N

Id

A

P

QR

S

Oid P R

W

S

PRIMARY KEY (Oid, P)W(Oid) REFERENCES O(Id)

ON DELETE CASCADE

Id A

O

Page 12: CSE 480: Database Systems

12

Example

EMPLOYEESsn

1

PRIMARY KEY(Essn, Dependent_name)

DEPENDENT(Essn) REFERENCES EMPLOYEE(Ssn)ON DELETE CASCADE

Page 13: CSE 480: Database Systems

13

ER-to-Relational Mapping Algorithm

Step 3: Mapping of Binary 1-to-1 Relationship types

1 1A B

R

1 1A B

R

1 1A

R

Cross-reference approach: 3 Tables

Foreign key approach: 2 Tables

Merged relation approach: 1 Table

Page 14: CSE 480: Database Systems

14

Cross-Reference Approach

Cross-reference approach:

1 1EMPLOYEE DEPARTMENT

MANAGES

StartDate

MANAGES

MANAGES(Dnumber) REFERENCES DEPARTMENT(Dnumber)MANAGES(Mgr_ssn) REFERENCES EMPLOYEE(Ssn)

Page 15: CSE 480: Database Systems

15

Foreign Key Approach

1 1EMPLOYEE DEPARTMENT

MANAGES

StartDate

Foreign key approach:

DEPARTMENT(Mgr_ssn) REFERENCES EMPLOYEE(Ssn)

Mgr_ssn is NOTNULL

Total participation

Page 16: CSE 480: Database Systems

16

Merged Relation Approach

Merged relation approach:

MANAGER

1 1MANAGER DEPARTMENT

MANAGES

StartDate

Total participation

DEPARTMENT_W_MANAGER

Page 17: CSE 480: Database Systems

17

Summary (Binary 1-1 Relationships)

1 1S T

R

1 1S T

R

1 1S T

R

S

SID

R

SIDTIDP

T

TID

Cross-reference:

S

SID

T_R

TIDSIDP

Foreign key:

S_R_T

SIDTIDP

Merged relation:

P

P

P

Page 18: CSE 480: Database Systems

18

ER-to-Relational Mapping Algorithm

Step 4: Mapping of Binary 1:N Relationship Types.

1 NS T

R

P

S

SID

T_R

TIDSIDP

T_R(SID) references S(SID)

Page 19: CSE 480: Database Systems

19

Example

N 1EMPLOYEE DEPARTMENT

WORKS_FOR

SUPERVISES

Supervisor 1

Subordinate N

Page 20: CSE 480: Database Systems

20

– For each binary 1:N relationship type R Identify the relation S that represents the participating entity type at

the N-side of the relationship type. Include as foreign key in S the primary key of the T Include any simple attributes of R as attributes of S.

Summary (Binary 1-N Relationships)

N 1S T

R

S

SIDTIDP

T

TID

P

Page 21: CSE 480: Database Systems

21

ER-to-Relational Mapping Algorithm

Step 5: Mapping of Binary M:N Relationship Types.

M NS T

R

P

S

SID

R

SIDTIDP

T

TID

R(SID) references S(SID)R(TID) references T(TID)Primary key(SID, TID)

Page 22: CSE 480: Database Systems

22

Example

M NEMPLOYEE PROJECT

WORKS_ON

Hours

Primary key(Essn, Pno)

Page 23: CSE 480: Database Systems

23

Summary (Binary M-N Relationships)

– For each binary M:N relationship type R Create a new relation R Include as foreign key attributes in R the primary keys of the

relations that represent the participating entity types; their combination will form the primary key of R.

Include simple attributes of the relationship type R as attributes of the relation R

M NS T

R

Page 24: CSE 480: Database Systems

24

ER-to-Relational Mapping Algorithm

Step 6: Mapping of Multivalued attributes.

– For each multivalued attribute A, create a new relation T. Include attribute corresponding to A Include primary key attribute K of relation S – as a foreign key in T Primary key of T is the combination of A and K

SA

K

S

KB

T

KA

B

Page 25: CSE 480: Database Systems

25

Example

Page 26: CSE 480: Database Systems

26

ER-to-Relational Mapping Algorithm

Step 7: Mapping of N-ary Relationship Types.

– For each n-ary relationship type R, create a new relation R Include primary keys of the relations participating in the relationship

type as foreign key attributes in R Include any simple attributes of the n-ary relationship type R as

attributes of R

S T

R

P

Page 27: CSE 480: Database Systems

27

Ternary relationship types

Page 28: CSE 480: Database Systems

28

Exercise