ER- Relational Mapping (Based on Chapter 9 in Fundamentals of Database Systems by Elmasri and...

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

description

3 ER-to-Relational Mapping Algorithm STEP 1Strong Entity Types STEP 2Weak Entity Types STEP 3Binary N to 1 Relationship Types STEP 4Binary 1 to 1 Relationship Types STEP 5Binary M to N Relationship Types STEP 6Multi-valued Attributes STEP 7n-ary Relationship Types

Transcript of ER- Relational Mapping (Based on Chapter 9 in Fundamentals of Database Systems by Elmasri and...

Page 1: ER- Relational Mapping (Based on Chapter 9 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 3)

ER- Relational Mapping

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

Page 2: ER- Relational Mapping (Based on Chapter 9 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 3)

2

General GuidelinesEntity - RelationN:1 Relationship - Foreign KeyM:N Relationship - Relation

Page 3: ER- Relational Mapping (Based on Chapter 9 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 3)

3

ER-to-Relational Mapping Algorithm

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

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

4

STEP 1Strong Entity TypesFor 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.

Page 5: ER- Relational Mapping (Based on Chapter 9 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 3)

5

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.

Page 6: ER- Relational Mapping (Based on Chapter 9 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 3)

6

Entity Types Mapping

ET AET

BA

B

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

7

Composite Attributes Mapping

ETBA D EC

E

D

ETA B

Page 8: ER- Relational Mapping (Based on Chapter 9 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 3)

8

STEP 2Weak Entity TypesFor 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.

Page 9: ER- Relational Mapping (Based on Chapter 9 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 3)

9

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.

Page 10: ER- Relational Mapping (Based on Chapter 9 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 3)

10

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.

Page 11: ER- Relational Mapping (Based on Chapter 9 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 3)

11

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.

Page 12: ER- Relational Mapping (Based on Chapter 9 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 3)

12

Weak entities

ET2 B

R

ET1

A

ET2A

ET1A

B

Page 13: ER- Relational Mapping (Based on Chapter 9 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 3)

13

STEP 3 Binary N:1Relationship TypesFor each regular binary N:1

relationship type R, identify the relations S that represents the N-side of the relationship type.

Page 14: ER- Relational Mapping (Based on Chapter 9 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 3)

14

Include as foreign key in S the primary key of the 1-side of 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.

Page 15: ER- Relational Mapping (Based on Chapter 9 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 3)

15

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

Page 16: ER- Relational Mapping (Based on Chapter 9 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 3)

16

1 to N Relationship Mapping

R

1

ET2

ET1

N

ET1A

ET2B A

ET2B

ET1A

NOA

B

Page 17: ER- Relational Mapping (Based on Chapter 9 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 3)

17

STEP 4 Binary 1:1 Relationship TypesFor each binary 1:1 relationship type

R in the ER schema, and S and T, the relations that participate in RChoose one relation, for example S,

and include as foreign key in S the primary

key of T.

Page 18: ER- Relational Mapping (Based on Chapter 9 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 3)

18

1 to 1 Relationship Mapping

R

1

ET2

ET1

1

ET2B

ET1A B

ET1A

ET2B A

- or -

A

B

Page 19: ER- Relational Mapping (Based on Chapter 9 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 3)

19

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.

Page 20: ER- Relational Mapping (Based on Chapter 9 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 3)

20

1 to 1 relationship

ET1A

ET2B AR

1

ET2

ET1

1

A

B

Page 21: ER- Relational Mapping (Based on Chapter 9 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 3)

21

STEP 5 Binary M to N Relationship TypesFor 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.

Page 22: ER- Relational Mapping (Based on Chapter 9 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 3)

22

STEP 5 Binary M to N Relationship TypesAlso include any simple attributes of

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

Page 23: ER- Relational Mapping (Based on Chapter 9 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 3)

23

M to N Relationship Mapping

R

n

ET2

ET1

n

ET2B

ET1A

NO

ET2B

ET1A

NOET2

B

ET1A

RA B

Page 24: ER- Relational Mapping (Based on Chapter 9 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 3)

24

STEP 5 Binary M to N Relationship TypesNotice 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.

Page 25: ER- Relational Mapping (Based on Chapter 9 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 3)

25

STEP 5 Binary M to N Relationship TypesNotice that we can always map 1:1

or 1:N relationships in a manner similar to M:N relationships.

Page 26: ER- Relational Mapping (Based on Chapter 9 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 3)

26

STEP 6 Multivalued AttributesFor 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.

Page 27: ER- Relational Mapping (Based on Chapter 9 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 3)

27

STEP 6 Multi-valued AttributesThe primary key of R is the combination of A and K.If the multivalued attribute is composite, we include its simple components.

Page 28: ER- Relational Mapping (Based on Chapter 9 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 3)

28

Multivalued Attributes Mapping

F

ET-FA FET A

Page 29: ER- Relational Mapping (Based on Chapter 9 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 3)

29

STEP 7 n-ary Relationship TypesFor 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.

Page 30: ER- Relational Mapping (Based on Chapter 9 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 3)

30

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.

Page 31: ER- Relational Mapping (Based on Chapter 9 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 3)

31

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.

Page 32: ER- Relational Mapping (Based on Chapter 9 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 3)

32

Page 33: ER- Relational Mapping (Based on Chapter 9 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 3)

33

Page 34: ER- Relational Mapping (Based on Chapter 9 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 3)

34

Page 35: ER- Relational Mapping (Based on Chapter 9 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 3)

35

Notes on ER-to-Relational MappingRelationships in ER are mapped to foreign key attributes.

A single foreign key needed for 1:1 or 1:N relationshipsA extra relation with two foreign keys needed for binary M:N relationship.For n-ary relationship, n>2, we need an extra relation with n foreign keys.

Page 36: ER- Relational Mapping (Based on Chapter 9 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 3)

36

Notes on ER-to-Relational MappingEQUIJOIN 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 binary M:N relationshipN EQUIJOINS needed to materialize the full n-ary relationship.

Page 37: ER- Relational Mapping (Based on Chapter 9 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 3)

37

Notes on ER-to-Relational MappingPrimary key of weak entity type includes key of owner relation.Primary key of relation representing an n-ary relationship determined by the structural participation constraints.

Page 38: ER- Relational Mapping (Based on Chapter 9 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 3)

38

Notes on ER-to-Relational MappingWe 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.

Page 39: ER- Relational Mapping (Based on Chapter 9 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 3)

39

Correspondence Between ER and Relational ModelsER Model Relational ModelEntity type “Entity” relation1:1 or 1:N relationship type Foreign key (or “relationship” relation)M:N relationship type “Relationship” relation and two foreign

keysN-ary relationship type “Relationship” relation and n foreign keysSimple attribute Attribute Composite attribute Set of simple component attributesMultivalued attribute Relation and foreign keysValue set DomainKey attribute Primary (or secondary) key