Documentos de Académico
Documentos de Profesional
Documentos de Cultura
Ramon Lawrence
COSC 304
Introduction to Database Systems
ER to Relational Mapping
Page 2
ER Model Example
Supervises
0..1
bonus
Supervisor
0..*
Supervisee
Employee
number {PK}
name
address
state
city
street
title
salary
1..1
0..*
Has
Dependent
name {PPK}
age
Has
0..1
Department
number {PK}
name
0..1
0..*
WorksOn
0..*
Project
0..*
number {PK}
name
budget
location [1..3]
responsibility
hours
ER to Relational Mapping
Step #1: Convert Strong Entities
Has
Page 4
ER to Relational Mapping
Current Relational Schema - Step #1
Notes:
Page 5
ER to Relational Mapping
Step #2: Convert Weak Entities
ER to Relational Mapping
Step #2: Convert Weak Entities (2)
Step #2: Convert each weak entity into a relation with foreign
keys to its identifying relations (entities).
For each weak entity W with identifying owners E1, E2, , En
create a relation R:
Identify relations R1, R2, , Rn for entity types E1, E2, , En.
The primary key of R consists of the primary keys of R1, R2, ,
Rn plus the partial key of the weak entity.
Create a foreign key in R to the primary key of each relation R1,
R2, , Rn.
Attributes are converted the same as strong entities.
Employee
number {PK}
name
address
state
city
street
title
salary
1..1
0..*
Has
Dependent
name {PPK}
age
Page 7
Page 8
ER to Relational Mapping
Current Relational Schema - Step #2
ER to Relational Mapping
Steps #3-5: Convert Relationships
ER to Relational Mapping
Step #3: Convert 1:1 Relationships
ER to Relational Mapping
Step #3: Convert 1:1 Relationships (2)
You can select either Ri or Rj. Typically, it is best to select the relation that
is guaranteed to always participate in the relationship or the one that will
participate the most in the relationship.
Page 11
Employee
number {PK}
name
address
state
city
street
title
salary
bonus
Department
0..1
Manages
0..1
number {PK}
name
Employee (eno,
ename, state, city,
street, title, salary)
ER to Relational Mapping
Step #4: Convert 1:N Relationships
ER to Relational Mapping
Step #4: Convert 1:N Relationships
Employee
number {PK}
name
address
state
city
street
title
salary
0..1
Employee (eno,
ename, state, city,
street, title, salary)
ER to Relational Mapping
Step #4: Convert 1:N Relationships
Supervises
0..1
Supervisor
Project
number {PK}
name
budget
location [1..3]
/totalEmp
number {PK}
name
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
ER to Relational Mapping
Step #4: Convert 1:N Relationships
Department
Has
0..*
0..1
deptProj (dno, pno)
0..*
Department
Has
0..*
number {PK}
name
Supervisee
Employee
number {PK}
name
address
state
city
street
title
salary
Page 15
ER to Relational Mapping
Current Relational Schema - Step #4
Page 16
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
ER to Relational Mapping
Step #5: Convert M:N Relationships
WorksOn
Employee
number {PK}
name
address
state
city
street
title
salary
Employee (eno,
ename, state, city,
street, title, salary,
dno, supereno)
0..*
responsibility
hours
Project
0..*
ER to Relational Mapping
Current Relational Schema - Step #5
number {PK}
name
budget
location [1..3]
Employee (eno, ename, state, city, street, title, salary, dno, supereno)
Page 20
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.
Given a multi-valued attribute A of entity Ei:
Identify the corresponding relation Ri.
Create a new relation R representing the attribute where:
ER to Relational Mapping
Step #6: Convert Multi-Valued Attributes
Project
number {PK}
name
budget
location [1..3]
Page 21
ER to Relational Mapping
Final Relational Schema
Page 22
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
ER to Relational Mapping
Step #7: Convert n-ary Relationships
Part
Project
number {PK}
name
description
number {PK}
name
budget
Provide
0..*
0..*
0..*
Supplier
quanity
price
number {PK}
name
address
4)
ER to Relational Mapping
Step #8: Convert Subclasses
ER to Relational Mapping
Step #8: Convert Subclasses
Supervisor (eno)
Supervisor
Manager
Manager (eno)
{Optional, AND}
Employee
number {PK}
name
address
state
city
street
title
salary
Page 28
Summary of
ER to Relational Mapping
ER to Relational Mapping
University Question
ER Model
Relational Model
Entity Type
1:1 or 1:N Relationship Type
M:N Relationship Type
n-ary Relationship Type
Simple attribute
Composite attribute
Multi-valued attribute
Key attribute
Relation
Foreign key (or "relationship" relation)
"Relationship" relation and 2 foreign keys
"Relationship" relation and n foreign keys
Attribute
Set of simple component attributes
Relation and foreign key
Primary (or secondary) key
Page 29
Page 30
ER to Relational Mapping
TPC-H Standard Question
Conclusion
There is a straightforward algorithm for converting ER models
to relational schemas.
amount
price
Order
0..*
number {PK}
date
totalAmount
Supplier
number {PK}
name
Places
0..*
1..1
1..1
Customer
number {PK}
name
In
0..*
1..1
Part
0..*
InOrder
number {PK}
name
price
0..*
0..*
0..*
0..*
Supplies
In
In
Nation
name {PK}
0..*
1..1
Region
name {PK}
Page 31
Objectives
Given an ER/EER diagram, be able to convert it into a
relational schema using the seven/eight steps.
Be able to discuss the different ways of converting
subclasses/superclasses into relational schemas.
Page 33
Page 32