Sie sind auf Seite 1von 28

Module 4

Mapping E-R Diagrams to


Relational Model

© Louis D. Nel 1996-2006 COMP3005 Databases Mapping ER->Tables - 1


95.305

Objectives

• Learn a direct mapping from E-R diagram to


relational model
• Reference:
Example is from
Elmasri & Navathe, Chapter 6

© Louis D. Nel 1996-2006 COMP3005 Databases Mapping ER->Tables - 2


95.305

Topics
• Mapping E-R Diagram to relational Model

© Louis D. Nel 1996-2006 COMP3005 Databases Mapping ER->Tables - 3


E-R diagram for Company Data (fig 3.2)
Sex Address StartDate No
Salary
Location
Name
Name 1 1
Manages
Sin Employee Department

Bdate N 1 1
WorksFor
Supervisee Controls
Supervisor
1 N M N
Supervision N
1
WorksOn

Project
Dependents hours
of Name Location
N
No
Dependent

Name Sex Bdate Relationship

© Louis D. Nel 1996-2006 COMP3005 Databases Mapping ER->Tables - 4


Entity-Relationship Notation

Employee Entity Address Attribute

Dependent Weak Entity Name Key Attribute

Location Multi-valued Attribute


Manages Relationship

Name Discriminator
“weak key”
Identifying
Dependents Address
Relationship Derived Attribute
of
(for weak entities)

1 N (min,max)
E1 R E2 E1 R E2 R E

Total participation of E2 Cardinality E must participate


in R. (E2 can only exist if 1:N for E1:E2 in at least min and at most
it participates in R) max associations in R

© Louis D. Nel 1996-2006 COMP3005 Databases Mapping ER->Tables - 5


Company Relational Database Schema
EMPLOYEE
fname minit lname ssn bdate address sex salary superssn dno

DEPARTMENT
dname dnumber mgrssn mgrstartdate Note:
Attributes refering to the
DEPT_LOCATIONS same thing can have different
dnumber dlocation name
(pnumber vs. pno)
PROJECT
Attributes refering to different
pname pnumber plocation dnum things can have the same name
( name of employee or
WORKS_ON name of department )
essn pno hours

DEPENDENT
essn dpendent_name sex bdate relationship

© Louis D. Nel 1996-2006 COMP3005 Databases Mapping ER->Tables - 6


Step 1 -Create Entity Relations from Entities
Sex Address No
Salary
Name
Name

Sin Employee Department

Bdate

Step 1:
Create relation for each table using
rectangular entities and their simple
attributes -not composite attributes Project

Include only the simple component Name Location


attributes from compound attributes
No
Select a key attribute to serve as the
relation key

© Louis D. Nel 1996-2006 COMP3005 Databases Mapping ER->Tables - 7


Entity -Relations
EMPLOYEE
fname minit lname ssn bdate address sex salary superssn dno

DEPARTMENT
dname dnumber mgrssn mgrstartdate

PROJECT
pname pnumber plocation dnum

© Louis D. Nel 1996-2006 COMP3005 Databases Mapping ER->Tables - 8


Step 2 -Create Weak Entity Relations
Sex Address No
Salary
Name
Name

Sin Employee Department

Bdate

Project
Dependents
of Name Location
N
No
Dependent

Name Sex Bdate Relationship

© Louis D. Nel 1996-2006 COMP3005 Databases Mapping ER->Tables - 9


Weak Entity -Relations
EMPLOYEE
fname minit lname ssn bdate address sex salary superssn dno

DEPARTMENT
dname dnumber mgrssn mgrstartdate

PROJECT
pname pnumber plocation dnum

DEPENDENT
essn dpendent_name sex bdate relationship

© Louis D. Nel 1996-2006 COMP3005 Databases Mapping ER->Tables - 10


Step 3 -1:1 Relationships
Sex Address StartDate No
Salary
Name
Name 1 1
Manages
Sin Employee Department

Bdate

Project
Dependents
of Name Location
N
No
Dependent

Name Sex Bdate Relationship

© Louis D. Nel 1996-2006 COMP3005 Databases Mapping ER->Tables - 11


Taking Care of “Manages” Relationship
EMPLOYEE
fname minit lname ssn bdate address sex salary superssn dno

DEPARTMENT
dname dnumber mgrssn mgrstartdate

PROJECT
pname pnumber plocation dnum

DEPENDENT
essn dpendent_name sex bdate relationship

© Louis D. Nel 1996-2006 COMP3005 Databases Mapping ER->Tables - 12


Step 4 -1:N Relationships
Sex Address StartDate No
Salary
Name
Name 1 1
Manages
Sin Employee Department

Bdate N 1 1
WorksFor
Supervisee Controls
Supervisor
1 N N
Supervision
1

Project
Dependents
of Name Location
N
No
Dependent

Name Sex Bdate Relationship

© Louis D. Nel 1996-2006 COMP3005 Databases Mapping ER->Tables - 13


Taking Care of 1:N Relationships
EMPLOYEE
fname minit lname ssn bdate address sex salary superssn dno

DEPARTMENT
dname dnumber mgrssn mgrstartdate

PROJECT
pname pnumber plocation dnum

DEPENDENT
essn dpendent_name sex bdate relationship

© Louis D. Nel 1996-2006 COMP3005 Databases Mapping ER->Tables - 14


Step 5 -N:M Relationships
Sex Address StartDate No
Salary
Name
Name 1 1
Manages
Sin Employee Department

Bdate N 1 1
WorksFor
Supervisee Controls
Supervisor
1 N M N
Supervision N
1
WorksOn

Project
Dependents hours
of Name Location
N
No
Dependent

Name Sex Bdate Relationship

© Louis D. Nel 1996-2006 COMP3005 Databases Mapping ER->Tables - 15


Taking Care of M:N Relationships
EMPLOYEE
fname minit lname ssn bdate address sex salary superssn dno

DEPARTMENT
dname dnumber mgrssn mgrstartdate

PROJECT
pname pnumber plocation dnum

WORKS_ON Note the key here


essn pno hours

DEPENDENT
essn dpendent_name sex bdate relationship

© Louis D. Nel 1996-2006 COMP3005 Databases Mapping ER->Tables - 16


Step 6 - Multi-valued Attributes
Sex Address StartDate No
Salary
Location
Name
Name 1 1
Manages
Sin Employee Department

Bdate N 1 1
WorksFor
Supervisee Controls
Supervisor
1 N M N
Supervision N
1
WorksOn

Project
Dependents hours
of Name Location
N
No
Dependent

Name Sex Bdate Relationship

© Louis D. Nel 1996-2006 COMP3005 Databases Mapping ER->Tables - 17


Taking Care of Multi-valued Attributes
EMPLOYEE
fname minit lname ssn bdate address sex salary superssn dno

DEPARTMENT
dname dnumber mgrssn mgrstartdate

DEPT_LOCATIONS
dnumber dlocation

PROJECT
pname pnumber plocation dnum

WORKS_ON
essn pno hours

DEPENDENT
essn dpendent_name sex bdate relationship

© Louis D. Nel 1996-2006 COMP3005 Databases Mapping ER->Tables - 18


Step 7 N-ary Relations

Supplier
Part
dnumber ...
ptnumber ...
Project
pjnumber ...

Supply
dnumber pjnumber ptnumber ...

© Louis D. Nel 1996-2006 COMP3005 Databases Mapping ER->Tables - 19


E-R diagram for Company Data (fig 3.2)
Sex Address StartDate No
Salary
Location
Name
Name 1 1
Manages
Sin Employee Department

Bdate N 1 1
WorksFor
Supervisee Controls
Supervisor
1 N M N
Supervision N
1
WorksOn

Project
Dependents hours
of Name Location
N
No
Dependent

Name Sex Bdate Relationship

© Louis D. Nel 1996-2006 COMP3005 Databases Mapping ER->Tables - 20


Mapping Inheritance to tables (option1)
Model
Make Serial Number

Vehicle

Car
Bicycle
#doors EngineSize
#gears FrameSize
transmission

Vehicle: serialNumber, Make, Model


Car: serialNumber, #Doors, EngineSize, Transmission

Bicycle: serialNumber, #Gears, FrameSize

© Louis D. Nel 1996-2006 COMP3005 Databases Mapping ER->Tables - 21


Mapping Inheritance to tables (option2)
Model
Make Serial Number

Vehicle

Car
Bicycle
#doors EngineSize
#gears FrameSize
transmission

Car: serialNumber, Make, Model #Doors, EngineSize, Transmission

Bicycle: serialNumber, Make, Model, #Gears, FrameSize

© Louis D. Nel 1996-2006 COMP3005 Databases Mapping ER->Tables - 22


Mapping Inheritance to tables (option3)
Model
Make Serial Number

Vehicle

Car
Bicycle
#doors EngineSize
#gears FrameSize
transmission

Vehicle: serialNumber, Make, Model, #Doors, Engine Size,


Transmission, #Gears, FrameSize

© Louis D. Nel 1996-2006 COMP3005 Databases Mapping ER->Tables - 23


Mapping Inheritance to tables (option4)
Model
Make Serial Number

Vehicle

Car
Bicycle
#doors EngineSize
#gears FrameSize
transmission

Vehicle: serialNumber, isCar, Make, Model, #Doors, Engine Size,


Transmission, isBicyle, #Gears, FrameSize

i.e. use a boolean field to be able to test which subclass is being represented

© Louis D. Nel 1996-2006 COMP3005 Databases Mapping ER->Tables - 24


Mapping Multiple Inheritance

© Louis D. Nel 1996-2006 COMP3005 Databases Mapping ER->Tables - 25


…Mapping Multiple Inheritance

© Louis D. Nel 1996-2006 COMP3005 Databases Mapping ER->Tables - 26


Mapping Categories

© Louis D. Nel 1996-2006 COMP3005 Databases Mapping ER->Tables - 27


…Mapping Categories

© Louis D. Nel 1996-2006 COMP3005 Databases Mapping ER->Tables - 28