Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

CHAPTER 9

Part01

Relational Database Design by ER- and


EERR-to-Relational Mapping

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 9- 1


GOALS during Mapping
◼ Preserve all information
◼ Entities, Attributes, Relationships

◼ Maintain the constraints to the extent possible


◼ Relational Model cannot preserve or represent all
constraints
◼ max cardinality ratio such as 1:10 in ER

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 9- 2


Figure 9.1 The ER conceptual schema
diagram for the COMPANY database.

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 9- 6


ER-to-Relational Mapping Algorithm
◼ Step 1: Mapping of Regular Entity Types.
◼ For each strong entity type E, create a relation R that
includes all the non-multivalued attributes of E.
◼ Choose one of the key attributes as the primary key for R

◼ Create relations: EMPLOYEE, DEPARTMENT, and


PROJECT
◼ SSN, DNUMBER, and PNUMBER are the PKs

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 9- 4


ER-to-Relational Mapping Algorithm
(contd.)
◼ Step 2: Mapping of Weak Entity Types
◼ For each weak entity type W with owner entity type
E, create a relation R & include all attributes of W
as attributes of R.
◼ Also, include to R as FK, the PK attribute(s) of the
relation(s) that correspond to the E.
◼ The PK of R is the combination of the PK of E and
the partial key of the W, if any.

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 9- 5


ER-to-Relational Mapping Algorithm
(contd.)
◼ Step 2: Example
◼ Create the relation DEPENDENT
◼ Include SSN, the PK of the EMPLOYEE as an FK
attribute of DEPENDENT (renamed to ESSN).
◼ The PK of the DEPENDENT relation is the
combination {ESSN, DEPENDENT_NAME}.

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 9- 6


ER-to-Relational Mapping Algorithm
(contd.)
◼ Step 3: Mapping of Binary 1:1 Relationship Types
◼ R is a relationship type in the ER schema, and
relations S and T correspond to the entity types
participating in R.
◼ There are three possible approaches:
1. Foreign Key (2 relations) approach: In relation S,
include FK for the PK of T, or vice versa
◼ Better choose entity type with total participation in R

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 9- 7


ER-to-Relational Mapping Algorithm
(contd.)
◼ Step 3: Mapping of Binary 1:1 Relationship Types
2. Merged relation option: Merge the two entity
types and the relationship into a single relation.
◼ Suitable when both participations are total

3. Relationship relation (3 relations) option: Set up


a third relation R for the purpose of cross-
referencing the PKs of S and T

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 9- 8


ER-to-Relational Mapping Algorithm
(contd.)
◼ Step 4: Mapping of Binary 1:N Relationship
Types.
◼ S represents the entity type at the N-side of the
relationship type R and T at the 1-side
◼ Add PK of T as attribute to S and create FK
constraint in S
◼ Add attributes of the relationship type to S, if any

◼ Alternative approach is to use a Relationship


relation

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 9- 9


ER-to-Relational Mapping Algorithm
(contd.)
◼ Step 4: Example
◼ EMPLOYEE--WORKS_FOR--DEPARTMENT
◼ Dnumber, the PK of DEPARTMENT is added to
EMPLOYEE as Dno and FK constraint is created

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 9- 10


ER-to-Relational Mapping Algorithm
(contd.)
◼ Step 5: Mapping of Binary M:N Relationship
Types
◼ Use Relationship-Relation approach
◼ For each M:N relationship type R, create a new
relation S
◼ Include as FK attributes in S, the PKs of the
relations for the participating entity types
◼ Also include any attributes of the relationship type

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 9- 11


ER-to-Relational Mapping Algorithm
(contd.)
◼ Step 5: EMPLOYEE--WORKS_ON—PROJECT
◼ Create relation WORKS_ON
◼ The PKs of the PROJECT and EMPLOYEE
relations are included as foreign keys in
WORKS_ON and renamed PNO and ESSN
◼ HOURS represents the HOURS attribute of the
relation type.
◼ PK is the
combination
of the FKs.

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 9- 12


ER-to-Relational Mapping Algorithm
(contd.)
◼ Step 6: Mapping of Multivalued attributes.
◼ For each multivalued attribute A of entity type E,
create a new relation R.
◼ R will include attributes of A and the PK of the
relation for E as FK.
◼ The PK of R is the combination of A and K.

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 9- 13


ER-to-Relational Mapping Algorithm
(contd.)
◼ Step 6: Example
◼ The relation DEPT_LOCATIONS is created.
◼ DLOCATION is the multivalued attribute
LOCATIONS of DEPARTMENT
◼ DNUMBER is the PK of the DEPARTMENT
◼ PK of R is {DNUMBER, DLOCATION}.

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 9- 14


Figure 9.2 Result of mapping the
COMPANY ER schema into a relational
database schema.

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 9- 13

You might also like