cs 377 database systems - emory universitylxiong/cs377_f11/share/slides/05_ermapping.pdf · for...

15
CS 377 Database Systems 1 ER-to-Relational Mapping Li Xiong Department of Mathematics and Computer Science Emory University

Upload: others

Post on 27-Mar-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS 377 Database Systems - Emory Universitylxiong/cs377_f11/share/slides/05_ERmapping.pdf · For each n-aryrelationship type R (n>2), create a new relation S Include as foreign key

CS 377

Database Systems

1

ER-to-Relational Mapping

Li Xiong

Department of Mathematics and Computer Science

Emory University

Page 2: CS 377 Database Systems - Emory Universitylxiong/cs377_f11/share/slides/05_ERmapping.pdf · For each n-aryrelationship type R (n>2), create a new relation S Include as foreign key

Requirement analysis

Requirement specification

Conceptual design

Conceptual data model (ER Model)

Logical design

2

Logical design

Representational data model (Relational Model)

Physical design

Physical data model

Database design implementation

Page 3: CS 377 Database Systems - Emory Universitylxiong/cs377_f11/share/slides/05_ERmapping.pdf · For each n-aryrelationship type R (n>2), create a new relation S Include as foreign key

ER Diagram of Company Database

3

Page 4: CS 377 Database Systems - Emory Universitylxiong/cs377_f11/share/slides/05_ERmapping.pdf · For each n-aryrelationship type R (n>2), create a new relation S Include as foreign key

4

Page 5: CS 377 Database Systems - Emory Universitylxiong/cs377_f11/share/slides/05_ERmapping.pdf · For each n-aryrelationship type R (n>2), create a new relation S Include as foreign key

ER-to-Relational Mapping

Step 1: Mapping of Regular 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.

5

Step 6: Mapping of Multivalued attributes.

Step 7: Mapping of N-ary Relationship Types.

Chen, Peter P. (1976). "The Entity-Relationship Model Toward a Unified View of Data". ACM Transactions on Database Systems 1 (1): 9-36.

Page 6: CS 377 Database Systems - Emory Universitylxiong/cs377_f11/share/slides/05_ERmapping.pdf · For each n-aryrelationship type R (n>2), create a new relation S Include as foreign key

Step 1: Mapping of Regular Entity Types.� For each regular (strong) entity type, create a relation R that

includes all the simple attributes (or simple component attributes of a composite attribute) of E.

� Choose one of the key attributes of E as the primary key for R.

6

Page 7: CS 377 Database Systems - Emory Universitylxiong/cs377_f11/share/slides/05_ERmapping.pdf · For each n-aryrelationship type R (n>2), create a new relation S Include as foreign key

Step 2: Mapping of Weak Entity Types

� For each weak entity type W with owner entity type E, create a relation R and

include all simple attributes (or simple components of composite attributes) of

W as attributes of R.

� Include primary key of E as foreign key of R, the primary key of R is the

combination of the primary key(s) of E and the partial key of W, if any.

7

Page 8: CS 377 Database Systems - Emory Universitylxiong/cs377_f11/share/slides/05_ERmapping.pdf · For each n-aryrelationship type R (n>2), create a new relation S Include as foreign key

Step 3: Mapping of 1:1 Binary Relationship Types� Foreign key approach (expand a relation)

� choose a relation S and include as foreign key the primary key of T; include simple relationship attributes - which relation to choose as S?

� Merge two relations - when is it appropriate?

� Create a new relation� Create R and include as foreign keys the primary keys of S and T; primary key of R is one of the foreign keys – drawback? when is it better?

8

Page 9: CS 377 Database Systems - Emory Universitylxiong/cs377_f11/share/slides/05_ERmapping.pdf · For each n-aryrelationship type R (n>2), create a new relation S Include as foreign key

Step 4: Mapping of 1:N Binary Relationship Types

� Foreign key approach (expand a relation)� choose a relation S and include as foreign key the primary key of T; include simple relationship attributes - which relation to choose as S?

� Create a new relation� Create R and include as foreign keys the primary keys of S and T; primary key of R is one of the foreign keys – when is it better than the foreign key approach?

� Why is merged relation approach not appropriate?

9

Page 10: CS 377 Database Systems - Emory Universitylxiong/cs377_f11/share/slides/05_ERmapping.pdf · For each n-aryrelationship type R (n>2), create a new relation S Include as foreign key

Step 5: Mapping of M:N Binary Relationship Types

� Create a new relation� Create R and include as foreign keys the primary keys of S and T; primary key of R is one of the foreign keys

� Why is foreign key approach and merged relation approach not appropriate?

10

Page 11: CS 377 Database Systems - Emory Universitylxiong/cs377_f11/share/slides/05_ERmapping.pdf · For each n-aryrelationship type R (n>2), create a new relation S Include as foreign key

Step 6: Mapping of Multivalued attributes.

� For each multivalued attribute A, create a new relation R. Include the attribute

A and the primary key attribute as a foreign key in R

� What’s the primary key of R?

11

Page 12: CS 377 Database Systems - Emory Universitylxiong/cs377_f11/share/slides/05_ERmapping.pdf · For each n-aryrelationship type R (n>2), create a new relation S Include as foreign key

� For each n-ary relationship type R (n>2), create a new relation S

� Include as foreign key the primary keys of the participating entity types

� include any simple attributes of the n-ary relationship type (or simple components of composite attributes) as attributes of S

Step 7: Mapping of N-ary Relationship Types

12

Page 13: CS 377 Database Systems - Emory Universitylxiong/cs377_f11/share/slides/05_ERmapping.pdf · For each n-aryrelationship type R (n>2), create a new relation S Include as foreign key

13

Page 14: CS 377 Database Systems - Emory Universitylxiong/cs377_f11/share/slides/05_ERmapping.pdf · For each n-aryrelationship type R (n>2), create a new relation S Include as foreign key

Summary of Mapping

ER Model Relational Model

Entity type “Entity” relation

1:1 or 1:N relationship type Foreign key (or “relationship” relation)

M:N relationship type “Relationship” relation and two foreign keys

14

M:N relationship type “Relationship” relation and two foreign keys

n-ary relationship type “Relationship” relation and n foreign keys

Simple attribute Attribute

Composite attribute Set of simple component attributes

Multivalued attribute Relation and foreign key

Key attribute Primary (or secondary) key

Page 15: CS 377 Database Systems - Emory Universitylxiong/cs377_f11/share/slides/05_ERmapping.pdf · For each n-aryrelationship type R (n>2), create a new relation S Include as foreign key

In-Class Exercise

15

Exercise 9.4. Map the ER schema for a SHIP_TRACKING database into a relational schema and

specify all primary keys and foreign keys.