0% found this document useful (0 votes)
12 views

UNIT 03-P2 Logical Data Modeling Using The Relational Model

The document outlines the procedure for mapping an ER database schema to a relational schema. It discusses 7 steps for mapping different relationship types in ER diagrams to relations in a relational database, including: 1. Mapping regular/strong entity types to relations with their attributes and choosing a primary key. 2. Mapping weak entities by including the owner's primary key and partial key as the new relation's primary key. 3. Mapping 1:1 relationships using merged relations, foreign keys, or cross-reference relations. 4. Mapping 1:N relationships by including the parent's primary key as a foreign key in the child relation. It uses examples from an ER diagram for a company database to

Uploaded by

youmna7july
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

UNIT 03-P2 Logical Data Modeling Using The Relational Model

The document outlines the procedure for mapping an ER database schema to a relational schema. It discusses 7 steps for mapping different relationship types in ER diagrams to relations in a relational database, including: 1. Mapping regular/strong entity types to relations with their attributes and choosing a primary key. 2. Mapping weak entities by including the owner's primary key and partial key as the new relation's primary key. 3. Mapping 1:1 relationships using merged relations, foreign keys, or cross-reference relations. 4. Mapping 1:N relationships by including the parent's primary key as a foreign key in the child relation. It uses examples from an ER diagram for a company database to

Uploaded by

youmna7july
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 61

Dr. Suleiman H.

Mustafa Slide 3-2 - 1


Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe
UNIT 03 (Part-2)

Logical Data Modeling:

Mapping ER Diagrams into the


Relational Model

Reviewed and Edited by Prof. Suleiman H. Mustafa

Dr. Suleiman H. Mustafa Slide 3-2 - 2


Outline
 Procedure for Mapping ER Database Schema to a
Relational Schema

 Step 1: Mapping of Regular Entity Types


 Step 2: Mapping of Weak Entity Types
 Step 3: Mapping of Binary 1:1 Relationship 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.
 Step 7: Mapping of N-ary Relationship Types.
 Step 8: Mapping Specialization Subentities

Dr. Suleiman H. Mustafa Slide 3-2 - 3


GOALS during Mapping
1. Preserve all information (that includes all attributes)
2. Maintain the constraints to the extent possible
(Relational Model cannot preserve all constraints -
e.g., participation constraints (Min-Max) such as
1:10 in ER)
3. Minimize null values

Dr. Suleiman H. Mustafa Slide 3-2 - 4


The ER conceptual schema diagram for the
COMPANY database.

Dr. Suleiman H. Mustafa Slide 3-2 - 5


Reminder

Dr. Suleiman H. Mustafa Slide 3-2 - 6


Step 1: Mapping of Regular (Strong) Entities
 For each regular (strong) entity type E in the ER
schema,
 Create a relation R
 Include all the simple attributes and subattributes of E
in R.
 Choose one of the candidate keys of E in the ER
diagram as the primary key for 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.

Dr. Suleiman H. Mustafa Slide 3-2 - 7


Step 1: Mapping of Regular (Strong) Entities
 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.

Dr. Suleiman H. Mustafa Slide 3-2 - 8


Mapping of Regular Entities

Dr. Suleiman H. Mustafa Slide 3-2 - 9


Step 2: Mapping of Weak Entities

 For each weak entity W in the ER schema with


owner entity E,
 Create a relation R &
 Include all simple attributes and subattributes of W as
attributes of R.
 Include the primary key attribute(s) of the owner entity
as foreign key attributes of R.

 The primary key of R is the combination of the primary


key of the owner and the partial key of the weak entity
W, if any.

Dr. Suleiman H. Mustafa Slide 3-2 - 10


Step 2: Mapping of Weak Entities

 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.

Dr. Suleiman H. Mustafa Slide 3-2 - 11


Mapping of Weak Entities

Foreign Key
Primary Key

Dr. Suleiman H. Mustafa Slide 3-2 - 12


Step 3: Mapping of One-To-One (1:1)
Relationships

 For each binary 1:1 relationship type R in the ER


schema,
 Identify the relations S and T that correspond to the
entities participating in R.
 Use one of the following three possible approaches:

Possible
Approaches

Merged Relation Foreign Key Cross-Reference


Option Option Option
Total + Total Total + Partial Partial + Partial
Dr. Suleiman H. Mustafa Slide 3-2 - 13
Step 3: Mapping of One-To-One (1:1)
Relationships

 Three possible approaches:


1. 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.
2. Foreign Key approach: Choose one of the relations-
say S-and include a foreign key 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.
3. Cross-reference (or relationship relation) option:
The third alternative is to set up a third relation R for
the purpose of cross-referencing the primary keys of
the two relations S and T representing the entity types.

Dr. Suleiman H. Mustafa Slide 3-2 - 14


Mapping of One-To-One (1:1) Relationships
1. Merged Relation Approach
• Given a 1:1 relationship R between the entities T and S
with total participation on both sides as follows and
T R S
1 1
• Given the corresponding two relations T and S
T(PK, a1, a2, …, an)
S(PK, a1, a2, …, an)
• Merge the two relations and the 1:1 relationship R into
a single relation (say TS).

TS PK a1 a2 ………. an

Dr. Suleiman H. Mustafa Slide 3-2 - 15


Mapping of One-To-One (1:1) Relationships
Total participation with two different primary keys

Primary Key
Dr. Suleiman H. Mustafa Slide 3-2 - 16
Mapping of One-To-One (1:1) Relationships

Total participation with the same primary keys

CUST_SHIPPING (CUSTOMER_NO, CUST_NAME, SHIP_ADRESS)

CUST_SHIPPING Customer_No Cust_Name Ship_Address

Dr. Suleiman H. Mustafa Slide 3-2 - 17


Mapping of One-To-One (1:1) Relationships

2. Foreign Key Approach


 Given a 1:1 relationship R between the entities T and
S with total participation on one side and partial
participation on the other side as follows, and
T R S
1 1
 Given the corresponding two relations T and S
T(PK, a1, a2, …, an)
S(PK, a1, a2, …, an)
Choose the relation with total participation (Say S)
and include as a foreign key in S the primary key of T.

S PK FK a1 ……… an
Dr. Suleiman H. Mustafa Slide 3-2 - 18
Mapping of Binary 1:1 Relation Type

Add FK

Add the attribute


of the relationship
Dr. Suleiman H. Mustafa Slide 3-2 - 19
Mapping of One-To-One (1:1) Relationships
3. Cross-Reference Relation Approach
 Given a 1:1 relationship R between the entities T and
S with partial participation on both sides as follows,
and
T R S
1 1
 Given the corresponding two relations T and S
T(PK, a1, a2, …, an)
S(PK, a1, a2, …, an)
Create a third relation (say TS) and add to it the
primary keys of T and S as foreign keys and the
simple attributes of R. The primary key is either that of
T or S. T TS S
PK …. FK FK PK …..
Dr. Suleiman H. Mustafa Slide 3-2 - 20
Mapping of One-To-One (1:1) Relationships

MALE (Male_SSN, Name)


FEMALE (Female-SSN, Name)
MARRIAGE (Male-SSN, Female-SSN, Date, Duration)

MALE Male_SSN Name FEMALE Female_SSN Name

MARRIAGE Male_SSN Female_SSN Date Duration


Dr. Suleiman H. Mustafa Slide 3-2 - 21
Step 4: Mapping of One-To-Many (1:n)
Relationship

 Given a 1:n binary relationship R between the


entities T and S, and
Many side
T R S
1 n
 Given the corresponding two relations T and S
T(PK, a1, a2, …, an)
S(PK, a1, a2, …, an)

 Identify the relation that represents the


participating entity at the N-side of the relationship
(Say S, in this case).
Dr. Suleiman H. Mustafa Slide 3-2 - 22
Step 4: Mapping of One-To-Many (1:n)
Relationship

 Step 4: Continues
 Include as foreign key in S the primary key of the
relation T.
 Include any simple attributes of the 1:N relationship as
attributes of S.

 Examples: 1:N relationships in the Company


database ER
 WORKS_FOR,
 CONTROLS, and
 SUPERVISION

Dr. Suleiman H. Mustafa Slide 3-2 - 23


Mapping of One-To-Many (1:n) Relationships

Example-1: For WORKS_FOR we include the primary key


DNUMBER of the DEPARTMENT relation as foreign key in
the EMPLOYEE relation and call it DNO.

relation S relation T

EMPLOYEE WORKS_FOR DEPARTMENT


n 1
The many side

Dr. Suleiman H. Mustafa Slide 3-2 - 24


Mapping of Binary 1:N Relationship Types.

Add FK

Dr. Suleiman H. Mustafa Slide 3-2 - 25


Mapping of One-To-Many (1:n) Relationships

EMPLOYEE WORKS_FOR DEPARTMENT


n 1

supervisor supervisee
n
1
SUPERVISION

relation S relation T
supervisee
EMPLOYEE SUPERVISION EMPLOYEE
n 1
The many side supervisor
Dr. Suleiman H. Mustafa Slide 3-2 - 26
Mapping of Binary 1:N Relationship Types.

Example-2: For SUPERVISION we include the primary key


SSN of the EPLOYEE relation as foreign key in the same
relation and call it Super_SSN.

Add FK

Dr. Suleiman H. Mustafa Slide 3-2 - 27


Mapping of One-To-Many (1:n) Relationships

Example-3: For CONTROLS we include the primary key


DNUMBER of the DEPARTMENT relation as foreign key in
the PROJECT relation and call it Dnum.

relation T relation S
DEPARTMENT CONTROLS PROJECT
1 n
The many side

Dr. Suleiman H. Mustafa Slide 3-2 - 28


Mapping of Binary 1:N Relationship Types.

Add FK

Dr. Suleiman H. Mustafa Slide 3-2 - 29


Step 5: Mapping of Many-To-Many (n:m)
Relationship

 Given a m:n binary relationship R between the


entities T and S as follows, and

T R S
n m
 Given the two relations T and S
T(PK, a1, a2, …, an)
S(PK, a1, a2, …, an)

 Create a third relation (say, TS) to represent R.


TS …. …. …… ....
Dr. Suleiman H. Mustafa Slide 3-2 - 30
Step 5: Mapping of Many-To-Many (n:m)
Relationship
 Include as foreign key attributes in TS the primary keys
of the relations that represent the participating entities;
their combination will form the primary key of S.
 Also include any simple attributes of the M:N
relationship type (or simple components of composite
attributes) as attributes of TS.

T R S
n m

Create new relation TS representing R

TS(FK, FK, …) FK FK …….


Dr. Suleiman H. Mustafa Slide 3-2 - 31
Step 5: Mapping of Many-To-Many (m:n)
Relationships
 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.
 The primary keys of the PROJECT and EMPLOYEE
relations are included as foreign keys in WORKS_ON
and renamed PNO and ESSN, respectively.
 Attribute HOURS in WORKS_ON represents the HOURS
attribute of the relation type. The primary key of the
WORKS_ON relation is the combination of the foreign
key attributes {ESSN, PNO}.

Dr. Suleiman H. Mustafa Slide 3-2 - 32


Mapping of Many-To-Many (m:n) Relationships

Hours

EMPLOYEE WORKS_ON PROJECT


M N

Create new relation WORKS-ON representing R


WORKS_ON …. …. …… ....

Dr. Suleiman H. Mustafa Slide 3-2 - 33


Mapping of Binary M:N Relationship Types

Add FK

Dr. Suleiman H. Mustafa Slide 3-2 - 34


Step 6: Mapping of Multivalued Attributes

 Given an entity E with K as the primary key and


a multivalued attribute A,
 Create a new relation R for each multivalued
attribute A.
A
E

K
R FK a

 Include in R an attribute corresponding to A, plus


the primary key K as a foreign key in R.

Dr. Suleiman H. Mustafa Slide 3-2 - 35


Step 6: Mapping of 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

A
E

K
R FK a

Dr. Suleiman H. Mustafa Slide 3-2 - 36


Step 6: Mapping of Multivalued Attributes

 Example: The relation DEPT_LOCATIONS is


created.
 The attribute DLOCATION represents the multivalued
attribute LOCATIONS of DEPARTMENT, while
DNUMBER-as foreign key-represents the primary key of
the DEPARTMENT relation.
 The primary key of R is the combination of {DNUMBER,
DLOCATION}.

Dr. Suleiman H. Mustafa Slide 3-2 - 37


Step 6: Mapping of Multivalued attributes

Attribute A

Primary Key
Dr. Suleiman H. Mustafa Slide 3-2 - 38
The Final Relational Database Schema for the COMPANY Database

Note: Now it is ready to be


given to the DBA to create
the tables using any DBMS
such as Oracle. To create
the tables (Relations) the
DBA uses the CREATE
command in SQL.

Dr. Suleiman H. Mustafa Slide 3-2 - 39


Step 7: Mapping of N-ary Relationships

 For each n-ary relationship 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.
 Also include any simple attributes of the n-ary
relationship type (or simple components of
composite attributes) as attributes of S.

Dr. Suleiman H. Mustafa Slide 3-2 - 40


Step 7: Mapping of N-ary Relationships

 Example
 The relationship SUPPLY in the ER on the next slide.
 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}

Dr. Suleiman H. Mustafa Slide 3-2 - 41


Step 7: Mapping the N-ary Relationship SUPPLY

Dr. Suleiman H. Mustafa Slide 3-2 - 42


Step 8: Mapping Specialization Subentities

 Given a super-entity C and its m subentities {S1,


S2,….,Sm},
 where the attributes of C are {k,a1,…an} and
 k is the primary key, (see next slide)
 Covert C and its suentities into relation schemas
using one of the following options:
 Options:
 Option 8A: Single Relation - Collapsing subentities
with the super-entity into a single relation with a
defining (discriminating) attribute
 Option 8B: Multiple relations – Using separate
relations for the superentity and its subentities
Dr. Suleiman H. Mustafa Slide 3-2 - 43
Step 8: Mapping Specialization Subentities

a1 ….
…. an
K a2

S1 S2 …. Sm

…. …. ….
…. …. ….
a1 a1 a1

Dr. Suleiman H. Mustafa Slide 3-2 - 44


Option A: Single Relation

 Single relation with one defining attribute


 Create a single relation L with attributes A(L)

 A(L) = {k,a1,…an} U {attributes of S1} U…U

{attributes of Sm} U {t}


 PK(L) = k.

 The attribute t is called a defining or

discriminating attribute. It indicates the


subentity to which each tuple belongs.

Dr. Suleiman H. Mustafa Slide 3-2 - 45


a1 ….
…. an
K a2 DA

…. …. ….
…. …. ….
a1 a1 a1

Dr. Suleiman H. Mustafa Slide 3-2 - 46


FIGURE 4.4
EER diagram notation for an attribute-defined
specialization on JobType.

Discriminating Attribute

Dr. Suleiman H. Mustafa Slide 3-2 - 47


Mapping the EER schema in Figure 4.4 using option 8A

Discriminating Attribute

subclass Attributes

Dr. Suleiman H. Mustafa Slide 3-2 - 48


Option B: Multiple Relations
 Multiple relations (Super-entity and
subentities)
 Create a relation L for C with attributes A(L) =
{k,a1,…an} and PK(L) = k.
 Create a relation Li for each subentity Si,

 The attributes A(Li) = {k} U {attributes of Si} and


PK(Li)=k.
 This option works for any specialization
 total or partial,
 disjoint or over-lapping

Dr. Suleiman H. Mustafa Slide 3-2 - 49


a1 ….
…. an
K a2

S1 S2 …. Sm

…. …. ….
…. …. ….
a1 a1 a1

K K K

Dr. Suleiman H. Mustafa Slide 3-2 - 50


FIGURE 4.4
EER diagram notation for an attribute-defined
specialization on JobType.

Dr. Suleiman H. Mustafa Slide 3-2 - 51


Mapping the EER schema in Figure 4.4 using option 8B

Dr. Suleiman H. Mustafa Slide 3-2 - 52


END

Dr. Suleiman H. Mustafa Slide 3-2 - 53


Dr. Suleiman H. Mustafa Slide 3-2 - 54
Dr. Suleiman H. Mustafa Slide 3-2 - 55
Dr. Suleiman H. Mustafa Slide 3-2 - 56
Dr. Suleiman H. Mustafa Slide 3-2 - 57
Which of the following questions cannot be answered
using the given ER model?
(a) What is the total number of a particular product
purchased by a customer?
(b) What is the price of the product which was purchased
by a particular customer in a particular date?
(c) What is the total number of a particular product that is
ordered through a specific order number?
(d) What is the address of customer who placed a greater
number of orders to date?

Answer: Option (d)


Dr. Suleiman H. Mustafa Slide 3-2 - 58
Suppose that there are two entity sets A and B with
attributes a1, a2, a3 and b1, b2, b3 respectively. Here, a1
is the key for A and b2 is the key for B. If there exists a
many-to-many relationship R from A to B with an attribute
r1, what would be the schema for R after converting the
ER diagram into relational schemas?
a. R(a1, a1)
b. R(a1, b2)
c. R(a1, b1, b2)
d. R(a1, b2, r1)

Answer: Option (d)


Dr. Suleiman H. Mustafa Slide 3-2 - 59
Dr. Suleiman H. Mustafa Slide 3-2 - 60
Dr. Suleiman H. Mustafa Slide 3-2 - 61

You might also like