39
ER- Relational Mapping (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

ER- Relational Mapping (Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

Embed Size (px)

Citation preview

ER- Relational Mapping

(Based on Chapter 7 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 4)

Mapping from ER Model to Relational Model

2

Content

Relational Database Design Using ER-to-Relational Mapping ER-to-Relational Mapping Algorithm Summary of Mapping for Model

Constructs and Constraints

Mapping from ER Model to Relational Model

3

ER-to-Relational Mapping Algorithm

STEP 1 Strong Entity TypesSTEP 2 Weak Entity TypesSTEP 3 Binary 1 to 1 Relationship TypesSTEP 4 Binary 1 to N Relationship TypesSTEP 5 Binary M to N Relationship TypesSTEP 6 Multivalued AttributesSTEP 7 n-ary Relationship Types

Mapping from ER Model to Relational Model

4

STEP 1 Strong Entity Types Mapping

For each regular (strong) entity type E in the ER schema, create a relation R that includes all the simple attributes.

Include only the simple component attributes of a composite attribute.

Mapping from ER Model to Relational Model

5

STEP 1 Continued

Choose one of key attributes of E as primary key of R.

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

Mapping from ER Model to Relational Model

6

Entity Types Mapping

ETET

ET BET

B

ET AET

BA

B

Mapping from ER Model to Relational Model

7

Composite Attributes Mapping

ET

BA D EC

E

D

ETA B

Mapping from ER Model to Relational Model

8

STEP 2 Weak Entity Types Mapping

For each weak entity type W in the ER schema with owner entity type E, create a relation R, and include all simple attributes (simple

component of composite attributes) of W as attributes of R.

Mapping from ER Model to Relational Model

9

STEP 2 Continued

In addition, include as foreign key attributes of R the primary key attribute(s) of the owner entity type(s);

This takes care of the identifying relationship type of W.

Mapping from ER Model to Relational Model

10

STEP 2 Continued

The 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, if any.

Mapping from ER Model to Relational Model

11

STEP 2 Continued

It is common to choose the propagate (CASCADE) option for the referential triggered action on the foreign key in the identifying entity relation of the weak entity type, since a weak entity has an existence dependency on its owner entity.

This can be used for both ON UPDATE and ON DELETE.

Mapping from ER Model to Relational Model

12

Weak entities

ET2 B

R

ET1

A

ET2A

ET1

A

B

Mapping from ER Model to Relational Model

13

STEP 3 Binary 1:1 Relationship Types Mapping

For each binary 1:1 relationship type R in the ER schema, and S and T, the relations that participate in R Choose one relation, for example S,

and include as foreign key in S the primary

key of T.

Mapping from ER Model to Relational Model

14

1 to 1 Relationship Mapping

R

1

ET2

ET1

1

ET2

B

ET1

A B

ET1

A

ET2

B A

- or -

Mapping from ER Model to Relational Model

15

STEP 3 Continued

It is better to choose an entity type with total participating in R as the role of S.

Include all simple attributes ( or simple component of composite attributes) of the 1:1 relationship type R as attributes of S.

Mapping from ER Model to Relational Model

16

1 to 1 relationship

R

1

ET2

ET1

1

ET1

A

ET2

B A

Mapping from ER Model to Relational Model

17

STEP 4 Binary 1:N Relationship Types Mapping

For each regular binary 1:N relationship type R, identify the relations S that represents the participating entity type at the N-side of the relationship type.

Mapping from ER Model to Relational Model

18

STEP 4 Continued

Include as foreign key in S the primary key of T that represents the other entity type (the 1-side) participating in R;

this is because each entity instance on the N-side is related to at most one entity instance on the 1-side of the relationship type.

Mapping from ER Model to Relational Model

19

STEP 4 Continued

Include any simple attributes (or simple component of composite attributes) of the 1:N relationship type as attributes of S.

Mapping from ER Model to Relational Model

20

1 to n Relationship Mapping

R

1

ET2

ET1

n

ET1

A

ET2

B A

ET2

B

ET1

A

NO

Mapping from ER Model to Relational Model

21

STEP 5 Binary M to N Relationship Types

For each binary M:N relationship type R, create a new relation S to represent R.

Include as a foreign key attributes in S the primary keys of the relations that participates in R; their combination will form the primary key of S.

Mapping from ER Model to Relational Model

22

STEP 5 Binary M to N Relationship Types

Also include any simple attributes of the M:N relationship type (or simple component of composite attributes) as attributes of S.

Mapping from ER Model to Relational Model

23

M to N Relationship Mapping

R

n

ET2

ET1

n

ET2

B

ET1

A

NO

ET2

B

ET1

A

NOET2

B

ET1

A

RA B

Mapping from ER Model to Relational Model

24

STEP 5 Binary M to N Relationship Types

Notice that we cannot represent an M:N relationship type by a single foreign key attribute in one of the participating relations – as we did for 1:1 or 1:N relationship types – because of the M:N cardinality ratio.

Mapping from ER Model to Relational Model

25

STEP 5 Binary M to N Relationship Types

Notice that we can always map 1:1 or 1:N relationships in a manner similar to M:N relationships.

Mapping from ER Model to Relational Model

26

STEP 6 Multivalued Attributes

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

The relation R will include an attribute corresponding to A, plus the primary key attribute K – as a foreign key in R – of the relation that represents the entity type or relationship type that has A as an attribute.

Mapping from ER Model to Relational Model

27

STEP 6 Multivalued Attributes

The primary key of R is the combination of A and K.

If the multivalued attribute is composite, we include its simple components.

Mapping from ER Model to Relational Model

28

Multivalued Attributes Mapping

F

ET-FA FET A

Mapping from ER Model to Relational Model

29

STEP 7 n-ary Relationship Types Mapping

For each n-ary relationship type R, where n>2, create a new relation S to represent R.

Include as foreign key attributes in S the primary keys of the relations that represent the participating entity types.

Mapping from ER Model to Relational Model

30

STEP 7 Continued

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

The primary of S is usually a combination of all the foreign keys that reference the relations representing the participating entity types.

Mapping from ER Model to Relational Model

31

STEP 7 Continued

However, if the cardinality constraints on any of entity type E participating in R is 1, then the primary key of S should not include the foreign key attribute that references relation E.

Mapping from ER Model to Relational Model

32

Mapping from ER Model to Relational Model

33

Mapping from ER Model to Relational Model

34

Mapping from ER Model to Relational Model

35

Notes on ER-to-Relational Mapping

Relationships in ER are mapped to foreign key attributes. A single foreign key needed for 1:1 or 1:N rel

ationships A extra relation with two foreign keys neede

d for binary M:N relationship. For n-ary relationship, n>2, we need an extr

a relation with n foreign keys.

Mapping from ER Model to Relational Model

36

Notes on ER-to-Relational Mapping

EQUIJOIN operations are needed to materialize the relationships by combining related tuples: A single EQUIJOIN needed to materialize 1:1

or 1:N relationships. Two EQUIJOINS needed to materialize bina

ry M:N relationship N EQUIJOINS needed to materialize the full

n-ary relationship.

Mapping from ER Model to Relational Model

37

Notes on ER-to-Relational Mapping

Primary key of weak entity type includes key of owner relation.

Primary key of relation representing an n-ary relationship determined from the structural participation constraints.

Mapping from ER Model to Relational Model

38

Notes on ER-to-Relational Mapping

We create a separate relation for each multivalued attribute.

This is because the relational model does not allow multiple values (a list, or a set of values) for an attribute in a single table.

Mapping from ER Model to Relational Model

39

Correspondence Between ER and Relational Models

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

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 keys

Value set Domain

Key attribute Primary (or secondary) key