You are on page 1of 18

10/24/2018

 ER-to-Relational Mapping Algorithm


Step 1: Mapping of Regular Entity Types
Chapter 9 Step 2: Mapping of Weak Entity Types
Step 3: Mapping of Binary 1:1 Relation Types
Mapping ER and EER Step 4: Mapping of Binary 1:N Relationship Types.
Step 5: Mapping of Binary M:N Relationship Types.
Model Constructs to Step 6: Mapping of Multivalued attributes.
Step 7: Mapping of N-ary Relationship Types.
Relations  Mapping EER Model Constructs to Relations
Step 8: Options for Mapping Specialization or Generalization.
Step 9: Mapping of Union Types (Categories).

ER-to-Relational Mapping Algorithm


 Step 1: Mapping of Regular Entity Types.
– For each regular (strong) entity type E in the ER schema, create FIGURE 7.1
a relation R that includes all the simple attributes of E. The ER
– Choose one of the key attributes of E as the primary key for R. conceptual
schema
If the chosen key of E is composite, the set of simple attributes diagram for
that form it will together form the primary key of R. the
COMPANY
database.
Example: We create the relations EMPLOYEE,
DEPARTMENT, and PROJECT in the relational schema
corresponding to the regular entities in the ER diagram. SSN,
DNUMBER, and PNUMBER are the primary keys for the
relations EMPLOYEE, DEPARTMENT, and PROJECT as
shown.

1
10/24/2018

5.5

FIGURE 7.2
Result of
mapping the
COMPANY
ER schema
into a
relational
schema.

ER-to-Relational Mapping Algorithm (cont)


 Step 2: Mapping of Weak Entity Types
– For each weak entity type W in the ER schema 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.
– In addition, include as foreign key attributes of R the primary key
attribute(s) of the relation(s) that correspond to the owner entity type(s).
– 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.

Example: Create the relation DEPENDENT in this step to correspond


to the weak entity type DEPENDENT. Include the primary key SSN of
the EMPLOYEE relation as a foreign key attribute of DEPENDENT
(renamed to ESSN).
The primary key of the DEPENDENT relation is the combination
{ESSN, DEPENDENT_NAME} because DEPENDENT_NAME is the
partial key of DEPENDENT.

2
10/24/2018

ER-to-Relational Mapping Algorithm (cont) ER-to-Relational Mapping Algorithm (cont)


 Step 3: Mapping of Binary 1:1 Relation Types  Step 4: Mapping of Binary 1:N Relationship Types.
For each binary 1:1 relationship type R in the ER schema, identify the relations S and T
that correspond to the entity types participating in R. There are three possible approaches: – For each regular binary 1:N relationship type R, identify the relation S
(1) Foreign Key approach: Choose one of the relations-S, say-and include a foreign key that represent the participating entity type at the N-side of the
in S the primary key of T. It is better to choose an entity type with total participation in
R in the role of S. relationship type.
Example: 1:1 relation MANAGES is mapped by choosing the participating entity type – Include as foreign key in S the primary key of the relation T that
DEPARTMENT to serve in the role of S, because its participation in the MANAGES
relationship type is total. represents the other entity type participating in R.
– Include any simple attributes of the 1:N relation type as attributes of S.
(2) Merged relation option: An alternate mapping of a 1:1 relationship type is possible
by merging the two entity types and the relationship into a single relation. This may be
appropriate when both participations are total. Example: 1:N relationship types WORKS_FOR, CONTROLS, and
SUPERVISION in the figure. For WORKS_FOR we include the
(3) Cross-reference or relationship relation option: The third alternative is to set up a primary key DNUMBER of the DEPARTMENT relation as foreign key
third relation R for the purpose of cross-referencing the primary keys of the two
relations S and T representing the entity types. in the EMPLOYEE relation and call it DNO.

ER-to-Relational Mapping Algorithm (cont) ER-to-Relational Mapping Algorithm (cont)


 Step 5: Mapping of Binary M:N Relationship Types.  Step 6: Mapping of Multivalued attributes.
– For each regular binary M:N relationship type R, create a new relation S – For each multivalued attribute A, create a new relation R. This relation R
to represent R.
will include an attribute corresponding to A, plus the primary key
– Include as foreign key attributes in S the primary keys of the relations that
represent the participating entity types; their combination will form the attribute K-as a foreign key in R-of the relation that represents the entity
primary key of S. type of relationship type that has A as an attribute.
– Also include any simple attributes of the M:N relationship type (or simple – The primary key of R is the combination of A and K. If the multivalued
components of composite attributes) as attributes of S. attribute is composite, we include its simple components.
Example: The M:N relationship type WORKS_ON from the ER diagram is
mapped by creating a relation WORKS_ON in the relational database schema. Example: The relation DEPT_LOCATIONS is created. The attribute
The primary keys of the PROJECT and EMPLOYEE relations are included as DLOCATION represents the multivalued attribute LOCATIONS of
foreign keys in WORKS_ON and renamed PNO and ESSN, respectively. DEPARTMENT, while DNUMBER-as foreign key-represents the primary key
Attribute HOURS in WORKS_ON represents the HOURS attribute of the of the DEPARTMENT relation. The primary key of R is the combination of
relation type. The primary key of the WORKS_ON relation is the combination
of the foreign key attributes {ESSN, PNO}. {DNUMBER, DLOCATION}.

3
10/24/2018

ER-to-Relational Mapping Algorithm (cont) FIGURE 4.11


 Step 7: Mapping of N-ary Relationship Types. Ternary relationship types. (a) The SUPPLY relationship.
– For each n-ary relationship type R, where n>2, create a new relationship S
to represent R.
– Include as foreign key attributes in S the primary keys of the relations that
represent the participating entity types.
– Also include any simple attributes of the n-ary relationship type (or simple
components of composite attributes) as attributes of S.

Example: The relationship type SUPPY in the ER below. This can be


mapped to the relation SUPPLY shown in the relational schema, whose
primary key is the combination of the three foreign keys {SNAME,
PARTNO, PROJNAME}

FIGURE 7.3 Summary of Mapping constructs and constraints


Mapping the n-ary relationship type SUPPLY from Figure 4.11a.
Table 7.1 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 key
Value set Domain
Key attribute Primary (or secondary) key

4
10/24/2018

Mapping EER Model Constructs to Relations Mapping EER Model to Relations


 Step8: Options for Mapping Specialization or Generalization. Step8: Options for Mapping Specialization or Generalization.
Convert each specialization with m subclasses {S1, S2,….,Sm} and generalized superclass
C, where the attributes of C are {k,a1,…an} and k is the (primary) key, into relational Option 8A: Multiple relations, Super class and subclasses.
schemas using one of the four following options:
– Create a relation for the super class, including the super class attributes.
Option 8A: Multiple relations-Superclass and subclasses. – Create a relation for each subclass, which includes the primary key of the
super class (which acts as the foreign key) and the attributes of the
Create a relation L for C with attributes Attrs(L) = {k,a1,…an} and PK(L) = k. Create a subclass specialization.
relation Li for each subclass Si, 1 < i < m, with the attributes Attrs(Li) = {k} U {attributes – This works for any specialization (partial, total, disjoint, overlapping)
of Si} and PK(Li)=k. This option works for any specialization (total or partial, disjoint
of over-lapping).
Option 8B: Multiple relations, Subclass relations only
Option 8B: Multiple relations-Subclass relations only – Create a relation for each subclass, with the attributes of both the super
Create a relation Li for each subclass Si, 1 < i < m, with the attributes Attr(Li) = class and the attributes of the subclass.
{attributes of Si} U {k,a1…,an} and PK(Li) = k. This option only works for a – This only works for total specializations, meaning that every entity in the
specialization whose subclasses are total (every entity in the superclass must belong super class must belong to at least one subclass. Otherwise members
to (at least) one of the subclasses). of the super class that don’t belong to a subclass will not be represented.

5
10/24/2018

• Options 8A and 8B are the multiple-relation options, whereas options 8C • In option 8B, the EQUIJOIN operation between each subclass
and 8D are the single-relation options. and the superclass is built into the schema and the superclass
• Option 8A creates a relation L for the superclass C and its attributes, plus a relation L is done away with, as illustrated in Figure 9.5(b) for the
relation Li for each subclass Si; each Li includes the specific (local) attributes
EER specialization in Figure 4.3(b).
of Si, plus the primary key of the superclass C, which is propagated to Li and
becomes its primary key. It also becomes a foreign key to the superclass • This option works well only when both the disjoint and total
relation. constraints hold.
• An EQUIJOIN operation on the primary key between any Li and L produces • If the specialization is not total, an entity that does not belong
all the specific and inherited attributes of the entities in Si.
to any of the subclasses Si is lost.
• This option is illustrated in Figure 9.5(a) for the EER schema in Figure
4.4. Option 8A works for any constraints on the specialization: disjoint or • If the specialization is not disjoint, an entity belonging to
overlapping, total or partial. more than one subclass will have its inherited attributes from
• Notice that the constraint must hold for each Li. This specifies a foreign key the superclass C stored redundantly in more than one table Li.
from each Li to L.

FIGURE 4.4
EER diagram
notation for an
attribute-defined FIGURE 7.4 –
specialization on Using Option 8A
JobType.

6
10/24/2018

FIGURE 4.3
Generalization. (b) Generalizing CAR and TRUCK into the superclass VEHICLE.

FIGURE 7.4 –
Using Option 8B

Mapping EER Model Constructs to Relations (cont)


Option 8C: Single relation with one type attribute.
Create a single relation L with attributes Attrs(L) = {k,a1,…an} U
{attributes of S1} U…U {attributes of Sm} U {t} and PK(L) = k. The
attribute t is called a type (or discriminating) attribute that indicates the
subclass to which each tuple belongs

Option 8D: Single relation with multiple type attributes.


Create a single relation schema L with attributes Attrs(L) = {k,a1,…an} U
{attributes of S1} U…U {attributes of Sm} U {t1, t2,…,tm} and PK(L) = k.
Each ti, 1 < I < m, is a Boolean type attribute indicating whether a tuple
belongs to the subclass Si.

7
10/24/2018

Mapping EER Model to Relations


Option 8C: Single relation with one type attribute.
• Options 8C and 8D create a single relation to represent the
– Create a single relation, with all the attributes of the super class and all the superclass C and all its subclasses.
attributes of a subclass.
– Include a ‘Type’ attribute, which is the discriminating attribute which indicates
• An entity that does not belong to some of the subclasses will have
which subclass the row belongs to. NULL values for the specific (local) attributes of these subclasses.
– This only works if the specialization is disjoint, meaning the super class
entity cannot be a member of more than one subclass. • These options are not recommended if many specific attributes are
defined for the subclasses.
Option 8D: Single relation with multiple type attributes.
• If few local subclass attributes exist, however, these mappings are
– Create a single relation with all the attributes of the super class and all the
attributes of the subclass. preferable to options 8A and 8B because they do away with the
– Include a Boolean “Type” attribute for each subclass, which indicates whether need to specify JOIN operations; therefore, they can yield a more
the row belongs to that subclass.
efficient implementation for queries.
– This works with overlapping specializations, to indicate if the super class
entity belongs to more than one subclass.

• Option 8C is used to handle disjoint subclasses by including a • Option 8D is designed to handle overlapping subclasses by
single type (or image or discriminating) attribute t to indicate including m Boolean type (or flag) fields, one for each subclass.
to which of the m subclasses each tuple belongs; hence, the It can also be used for disjoint subclasses.
domain of t could be {1, 2, … , m}. • Each type field ti can have a domain {yes, no}, where a value of
• If the specialization is partial, t can have NULL values in yes indicates that the tuple is a member of subclass Si.
tuples that do not belong to any subclass. • If we use this option for the EER specialization in Figure 4.4,
• If the specialization is attribute-defined, that attribute itself we would include three type attributes—Is_a_secretary,
serves the purpose of t and t is not needed; this option is Is_a_engineer, and Is_a_technician—instead of the Job_type
illustrated in Figure 9.5(c) for the EER specialization in Figure attribute in Figure 9.5(c).
4.4. • Figure 9.5(d) shows the mapping of the specialization from
Figure 4.5 using option 8D

8
10/24/2018

EngType

FIGURE 4.4
EER diagram
notation for an
attribute- FIGURE 7.4 –
defined Using Option 8C
specialization
on JobType.

FIGURE 4.5 FIGURE 7.4


EER diagram notation for an overlapping (nondisjoint) specialization. Options for mapping specialization or generalization.
(d) Mapping Figure 4.5 using option 8D with Boolean type fields Mflag and Pflag.

9
10/24/2018

Mapping EER Model Constructs to Relations (cont)


 Mapping of Shared Subclasses (Multiple
Inheritance)
FIGURE 4.7
A shared subclass, such as STUDENT_ASSISTANT, is a A specialization
subclass of several classes, indicating multiple inheritance. lattice with
multiple
These classes must all have the same key attribute; inheritance for a
UNIVERSITY
otherwise, the shared subclass would be modeled as a database.
category.
We can apply any of the options discussed in Step 8 to a
shared subclass, subject to the restriction discussed in Step
8 of the mapping algorithm. Below both 8C and 8D are
used for the shared class STUDENT_ASSISTANT.

FIGURE 7.5 Mapping EER Model Constructs to Relations (cont)


Mapping the EER specialization lattice in Figure 4.6 using multiple options.
Step 9: Mapping of Union Types (Categories).
– For mapping a category whose defining superclass have
different keys, it is customary to specify a new key
attribute, called a surrogate key, when creating a
relation to correspond to the category.
– In the example below we can create a relation OWNER
to correspond to the OWNER category and include any
attributes of the category in this relation. The primary
key of the OWNER relation is the surrogate key, which
we called OwnerId.

10
10/24/2018

Mapping EER Model to Relations


Step 9: Mapping of Union Types (Categories).
– For mapping a category whose defining super classes have
different keys, you can specify a new key attribute, called a
surrogate key, when creating a relation to correspond to the
FIGURE 4.8
category. Two categories (union
– Then create a relation for each category, which includes the types): OWNER and
REGISTERED_VEHICLE
attributes of the category, and the surrogate key, which acts as .
the foreign key.
Example:
 We can create a relation OWNER to correspond to the
OWNER category and include any attributes of the category in
this relation. The primary key of the OWNER relation is the
surrogate key, which we called OwnerId.

FIGURE 7.6
Mapping the
EER categories
(union types) in
Figure 4.7 to
relations.

11
10/24/2018

• Here we used option 8A for PERSON/{EMPLOYEE,


• For a multilevel specialization (or generalization)
ALUMNUS, STUDENT},
hierarchy or lattice, we do not have to follow the same
mapping option for all the specializations. Instead, we • And option 8C for EMPLOYEE/{STAFF, FACULTY,
can use one mapping option for part of the hierarchy or STUDENT_ASSISTANT} by including the type
lattice and other options for other parts. attribute Employee_type.
• Figure 9.6 shows one possible mapping into relations for • We then used the single-table option 8D for
the EER lattice in Figure 4.6. STUDENT_ASSISTANT/{RESEARCH_ASSISTANT,
TEACHING_ASSISTANT} by including the type
attributes Ta_flag and Ra_flag in EMPLOYEE.

12
10/24/2018

• We also used option 8D for


STUDENT/STUDENT_ASSISTANT by including the
type attributes Student_assist_flag in STUDENT,
• And for STUDENT/{GRADUATE_STUDENT,
UNDERGRADUATE_STUDENT} by including the type
attributes Grad_flag and Undergrad_flag in
STUDENT.
• In Figure 9.6, all attributes whose names end with type
or flag are type fields.

8A

8C
8A
8D

13
10/24/2018

9.2.2 Mapping of Shared Subclasses (Multiple Inheritance)


9.2.3 Mapping of Categories (Union Types)
• A shared subclass, such as ENGINEERING_MANAGER in Figure 4.6, is
a subclass of several superclasses, indicating multiple inheritance. • A category (or union type) is a subclass of the union of two or
• These classes must all have the same key attribute; otherwise, the more superclasses that can have different keys because they
shared subclass would be modeled as a category (union type)
• We can apply any of the options (step 8) to a shared subclass, subject can be of different entity types
to the restrictions discussed in step 8 of the mapping algorithm. • An example is the OWNER category shown in Figure 4.8, which
is a subset of the union of three entity types PERSON, BANK,
• In Figure 9.6, options 8C and 8D are used for the shared subclass
STUDENT_ASSISTANT.
and COMPANY.
• Option 8C is used in the EMPLOYEE relation (Employee_type • The other category in that figure, REGISTERED_VEHICLE,
attribute) has two superclasses that have the same key attribute.
• Option 8D is used in the STUDENT relation (Student_assist_flag
attribute)

Step 9: Mapping of Union Types (Categories). • The primary key of the OWNER relation is the surrogate key,
• For mapping a category whose defining superclasses have which we called Owner_id. We also include the surrogate key
different keys, it is customary to specify a new key attribute, attribute Owner_id as foreign key in each relation corresponding
called a surrogate key, when creating a relation to correspond to a superclass of the category, to specify the correspondence in
to the union type. values between the surrogate key and the original key of each
• The keys of the defining classes are different, so we cannot use superclass.
any one of them exclusively to identify all entities in the • Notice that if a particular PERSON (or BANK or COMPANY)
relation. entity is not a member of OWNER, it would have a NULL value
• In our example in Figure 4.8, we create a relation OWNER to for its Owner_id attribute in its corresponding tuple in the
correspond to the OWNER category, as illustrated in Figure PERSON (or BANK or COMPANY) relation, and it would not
have a tuple in the OWNER relation.
9.7, and include any attributes of the category in this relation.

14
10/24/2018

• It is also recommended to add a type attribute (not shown in


Figure 9.7) to the OWNER relation to indicate the particular
entity type to which each tuple belongs (PERSON or BANK or
COMPANY).
• For a category whose superclasses have the same key, such as
VEHICLE in Figure 4.8, there is no need for a surrogate key. The
mapping of the REGISTERED_VEHICLE category, which
illustrates this case, is also shown in Figure 9.7.

9.4. Figure 9.8 shows an ER schema for a database


that can be used to keep track of transport ships and
their locations for maritime authorities. Map this
schema into a relational schema and specify all
primary keys and foreign keys.

15
10/24/2018

9.6. Map the EER diagrams in Figures 4.9 and 4.12


into relational schemas. Justify your choice of
mapping options.

16
10/24/2018

9.8. Consider the EER diagram in Figure 9.9 for a car


dealer. Map the EER schema into a set of relations.
For the VEHICLE to CAR/TRUCK/SUV
generalization, consider the four options presented in
Section 9.2.1 and show the relational schema design
under each of those options.

9.9. Using the attributes you provided for the EER diagram
in Exercise 4.27, map the complete schema into a set of
relations. Choose an appropriate option out of 8A thru 8D
from Section 9.2.1 in doing the mapping of generalizations
and defend your choice.

(Exercise 4.27. Consider the following EER diagram that


describes the computer systems at a company. Provide your own
attributes and key for each entity type. Supply max cardinality
constraints justifying your choice. Write a complete narrative
description of what this EER diagram represents).

17
10/24/2018

Any Question?
Thank You!

18

You might also like