Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
ER to Relational Mapping
Page 2
COSC 304 - Dr. Ramon Lawrence
Page 3
COSC 304 - Dr. Ramon Lawrence
ER Model Example
Supervises
0..1 bonus
Supervisor
0..* Employee Department
0..1 Manages 0..1
Supervisee number {PK} number {PK}
name name
address 0..* Has 0..1
state 0..1
Has
city
street
title
0..*
salary WorksOn Project
ER to Relational Mapping
Step #1: Convert Strong Entities
Step #1: Convert each strong entity to a relation.
Employee
number {PK}
name
address Employee (eno, ename, state, city, street, title, salary)
state
city
street
title
salary
Notes:
1) Attributes of the entity type become attributes of the relation.
2) Include only simple attributes in relation. For composite attributes,
only create attributes in the relation for their simple components.
3) Multi-valued attributes are handled separately (in step #6).
4) The primary key of the relation is the key attributes for the entity.
Page 5
COSC 304 - Dr. Ramon Lawrence
ER to Relational Mapping
Current Relational Schema - Step #1
Page 6
COSC 304 - Dr. Ramon Lawrence
ER to Relational Mapping
Step #2: Convert Weak Entities
Step #2: Convert each weak entity into a relation with foreign
keys to its identifying relations (entities).
Page 7
COSC 304 - Dr. Ramon Lawrence
ER to Relational Mapping
Step #2: Convert Weak Entities (2)
Employee
number {PK}
name
address Employee (eno, ename, state, city, street, title, salary)
state
city
street
title
salary
1..1
Has
0..*
Dependent
name {PPK} Dependent (eno, name, age)
age
Page 8
COSC 304 - Dr. Ramon Lawrence
ER to Relational Mapping
Current Relational Schema - Step #2
Dependent (eno, name, age)
Page 9
COSC 304 - Dr. Ramon Lawrence
ER to Relational Mapping
Steps #3-5: Convert Relationships
Steps 3 to 5 convert binary relationships of cardinality:
1:1 - Step #3
1:N - Step #4
M:N - Step #5
Note that M:N relationships are the most general case, and the
conversion algorithm for these relationships can be applied to
1:1 and 1:N as well.
However, for performance reasons, it is normally more efficient
to perform different conversions for each relationship type.
In general, each ER relationship can be mapped to a relation.
However, for 1:1 and 1:N relationships, it is more efficient to
combine the relationship with an existing relation instead of
creating a new one.
Relationships that are not binary are handled in step #7. Page 10
COSC 304 - Dr. Ramon Lawrence
ER to Relational Mapping
Step #3: Convert 1:1 Relationships
Step #3: Convert binary 1:1 relationships into a UNIQUE foreign
key reference from one relation to the other.
ER to Relational Mapping
Step #3: Convert 1:1 Relationships (2)
Employee
number {PK} bonus
name Department
address
state number {PK}
city
0..1 Manages 0..1 name
street
title
salary Manages (eno, dno, bonus) Department (dno, dname)
Employee (eno,
ename, state, city,
street, title, salary)
ER to Relational Mapping
Step #4: Convert 1:N Relationships
Step #4: Convert binary 1:N relationships between into a foreign
key reference from the N-side relation to the 1-side relation.
Notes:
Unlike 1:1 relationships, you must select the N-side of the
relationship as the relation containing the foreign key and
relationship attributes. Page 13
COSC 304 - Dr. Ramon Lawrence
ER to Relational Mapping
Step #4: Convert 1:N Relationships
Employee
number {PK}
name Department
address Has
state 0..* 0..1 number {PK}
city name
street
title
salary inDept (dno, eno) Department (dno, dname, mgreno, bonus)
Employee (eno,
ename, state, city,
street, title, salary)
Page 14
COSC 304 - Dr. Ramon Lawrence
ER to Relational Mapping
Step #4: Convert 1:N Relationships
Project
Has Department
number {PK}
name 0..* 0..1 number {PK}
budget name
location [1..3]
/totalEmp Department (dno, dname,
deptProj (dno, pno)
mgreno, bonus)
Project (pno, pname, budget)
Page 15
COSC 304 - Dr. Ramon Lawrence
ER to Relational Mapping
Step #4: Convert 1:N Relationships
Supervises
0..1 Supervises (supereno, eno)
Supervisor
0..* Employee Employee (eno, ename, state, city, street,
Supervisee number {PK} title, salary, dno)
name
address
state
city
street
title Employee (eno, ename, state, city, street,
salary
title, salary, dno, supereno)
Page 16
COSC 304 - Dr. Ramon Lawrence
ER to Relational Mapping
Current Relational Schema - Step #4
Dependent (eno, name, age)
Employee (eno, ename, state, city, street, title, salary, dno, supereno)
Page 17
COSC 304 - Dr. Ramon Lawrence
ER to Relational Mapping
Step #5: Convert M:N Relationships
Step #5: Convert binary M:N relationships into a new relation
with foreign keys to the two participating entities.
Page 18
COSC 304 - Dr. Ramon Lawrence
ER to Relational Mapping
Step #5: Convert M:N Relationships
Employee WorksOn Project
number {PK} 0..* 0..* number {PK}
name name
address budget
state location [1..3]
city
street responsibility
title hours Project (pno, pname, budget, dno)
salary
Employee (eno,
ename, state, city,
street, title, salary, WorksOn (eno, pno, resp, hours)
dno, supereno)
Page 19
COSC 304 - Dr. Ramon Lawrence
ER to Relational Mapping
Current Relational Schema - Step #5
Dependent (eno, name, age)
Employee (eno, ename, state, city, street, title, salary, dno, supereno)
Page 20
COSC 304 - Dr. Ramon Lawrence
ER to Relational Mapping
Step #6: Convert Multi-Valued Attributes
Step #6: Convert a multi-valued attribute into a relation with
composite primary key consisting of the attribute value plus the
primary key of the attribute's entity.
Page 21
COSC 304 - Dr. Ramon Lawrence
ER to Relational Mapping
Step #6: Convert Multi-Valued Attributes
Project
number {PK}
name
budget
location [1..3]
Page 22
COSC 304 - Dr. Ramon Lawrence
ER to Relational Mapping
Final Relational Schema
Dependent (eno, name, age)
Employee (eno, ename, state, city, street, title, salary, dno, supereno)
Page 23
COSC 304 - Dr. Ramon Lawrence
ER to Relational Mapping
Step #7: Convert n-ary Relationships
Step #7: Convert n-ary relationships by creating a new relation
to represent the relationship and creating foreign keys that
reference the related entities.
Page 24
COSC 304 - Dr. Ramon Lawrence
ER to Relational Mapping
Step #7: Convert n-ary Relationships
quanity
price
Part Project
number {PK} number {PK}
Provide
name name
description 0..* 0..* budget
Page 25
COSC 304 - Dr. Ramon Lawrence
ER to Relational Mapping
Step #8: Convert Subclasses
Step #8: Convert subclasses and superclasses by creating a
relation for each subclass and superclass. Link the subclasses
to the superclass using foreign key references.
Page 27
COSC 304 - Dr. Ramon Lawrence
ER to Relational Mapping
Step #8: Convert Subclasses
Supervisor Manager
Supervisor (eno) Manager (eno)
{Optional, AND}
Employee
number {PK}
name
address
state
city
street
title
salary
Summary of
ER to Relational Mapping
ER Model Relational Model
Entity Type Relation
1:1 or 1:N Relationship Type Foreign key (or "relationship" relation)
M:N Relationship Type "Relationship" relation and 2 foreign keys
n-ary Relationship Type "Relationship" relation and n foreign keys
Simple attribute Attribute
Composite attribute Set of simple component attributes
Multi-valued attribute Relation and foreign key
Key attribute Primary (or secondary) key
Page 29
COSC 304 - Dr. Ramon Lawrence
A) 0 B) 1 C) 2 D) 3 E) 4
Page 30
COSC 304 - Dr. Ramon Lawrence
ER to Relational Mapping
University Question
Page 31
COSC 304 - Dr. Ramon Lawrence
ER to Relational Mapping
TPC-H Standard Question
amount
price
Conclusion
There is a straightforward algorithm for converting ER models
to relational schemas.
Page 33
COSC 304 - Dr. Ramon Lawrence
Objectives
Given an ER/EER diagram, be able to convert it into a
relational schema using the seven/eight steps.
Page 34