erd relational mapping
TRANSCRIPT
© Louis D. Nel 1996-2006 1Mapping ER->Tables -COMP3005 Databases
Module 4
Mapping E-R Diagrams toRelational Model
© Louis D. Nel 1996-2006 2Mapping ER->Tables -COMP3005 Databases
95.305
• Learn a direct mapping from E-R diagram to relational model
• Reference: Example is fromElmasri & Navathe, Chapter 6
Objectives
© Louis D. Nel 1996-2006 3Mapping ER->Tables -COMP3005 Databases
95.305
Topics
• Mapping E-R Diagram to relational Model
© Louis D. Nel 1996-2006 4Mapping ER->Tables -COMP3005 Databases
E-R diagram for Company Data (fig 3.2)
Employee
Dependent
1
1
11
N
N
NM1 N
1
N
Supervisor Supervisee
Bdate
Sin
Name
Sex AddressSalary
Name
NoLocation
StartDate
Supervision
Dependentsof
ManagesDepartment
Location
WorksFor
Project
Controls
Name
No
WorksOn
BdateName Sex Relationship
hours
© Louis D. Nel 1996-2006 5Mapping ER->Tables -COMP3005 Databases
Entity-Relationship Notation
Manages
Employee Address
Name
Location
Name
Entity
Dependent Weak Entity
Relationship
IdentifyingRelationship(for weak entities)
Attribute
Key Attribute
Discriminator “weak key”
Total participation of E2in R. (E2 can only exist ifit participates in R)
Cardinality 1:N for E1:E2
1 N (min,max)
E must participatein at least min and at most max associations in R
Address Derived Attribute
Multi-valued Attribute
Dependentsof
E1 R E2 E1 R E2 R E
© Louis D. Nel 1996-2006 6Mapping ER->Tables -COMP3005 Databases
Company Relational Database Schema
EMPLOYEEfname minit lname ssn bdate address sex salary superssn dno
DEPT_LOCATIONSdnumber dlocation
PROJECTpname pnumber plocation dnum
WORKS_ONessn pno hours
DEPENDENTessn dpendent_name sex bdate relationship
DEPARTMENTdname dnumber mgrssn mgrstartdate Note:
Attributes refering to thesame thing can have differentname(pnumber vs. pno)
Attributes refering to differentthings can have the same name( name of employee or
name of department )
© Louis D. Nel 1996-2006 7Mapping ER->Tables -COMP3005 Databases
Step 1 -Create Entity Relations from Entities
Employee
Bdate
Sin
Name
Sex AddressSalary
Name
No
Department
Location
Project
Name
No
Step 1:Create relation for each table usingrectangular entities and their simpleattributes -not composite attributes
Include only the simple component attributes from compound attributes
Select a key attribute to serve as therelation key
© Louis D. Nel 1996-2006 8Mapping ER->Tables -COMP3005 Databases
Entity -Relations
EMPLOYEEfname minit lname ssn bdate address sex salary superssn dno
PROJECTpname pnumber plocation dnum
DEPARTMENTdname dnumber mgrssn mgrstartdate
© Louis D. Nel 1996-2006 9Mapping ER->Tables -COMP3005 Databases
Step 2 -Create Weak Entity Relations
Employee
Bdate
Sin
Name
Sex AddressSalary
Name
No
Department
Location
Project
Name
NoDependent
BdateName Sex Relationship
1
N
Dependentsof
© Louis D. Nel 1996-2006 10Mapping ER->Tables -COMP3005 Databases
Weak Entity -Relations
EMPLOYEEfname minit lname ssn bdate address sex salary superssn dno
PROJECTpname pnumber plocation dnum
DEPARTMENTdname dnumber mgrssn mgrstartdate
DEPENDENTessn dpendent_name sex bdate relationship
© Louis D. Nel 1996-2006 11Mapping ER->Tables -COMP3005 Databases
Step 3 -1:1 Relationships
Employee
Bdate
Sin
Name
Sex AddressSalary
Name
No
Department
Location
Project
Name
NoDependent
BdateName Sex Relationship
11
StartDate
Manages
1
N
Dependentsof
© Louis D. Nel 1996-2006 12Mapping ER->Tables -COMP3005 Databases
Taking Care of “Manages” Relationship
EMPLOYEEfname minit lname ssn bdate address sex salary superssn dno
PROJECTpname pnumber plocation dnum
DEPARTMENTdname dnumber mgrssn mgrstartdate
DEPENDENTessn dpendent_name sex bdate relationship
© Louis D. Nel 1996-2006 13Mapping ER->Tables -COMP3005 Databases
Step 4 -1:N Relationships
Employee
Bdate
Sin
Name
Sex AddressSalary
Name
No
Department
Location
Project
Name
NoDependent
BdateName Sex Relationship
11
StartDate
Manages
1N
1 N
1
N
Supervisor Supervisee
WorksForControls
Supervision1
N
Dependentsof
© Louis D. Nel 1996-2006 14Mapping ER->Tables -COMP3005 Databases
Taking Care of 1:N Relationships
EMPLOYEEfname minit lname ssn bdate address sex salary superssn dno
PROJECTpname pnumber plocation dnum
DEPARTMENTdname dnumber mgrssn mgrstartdate
DEPENDENTessn dpendent_name sex bdate relationship
© Louis D. Nel 1996-2006 15Mapping ER->Tables -COMP3005 Databases
Step 5 -N:M Relationships
Employee
Bdate
Sin
Name
Sex AddressSalary
Name
No
Department
Location
Project
Name
NoDependent
BdateName Sex Relationship
11
StartDate
Manages
1N
1 N
1
N
Supervisor Supervisee
WorksForControls
Supervision1
N
Dependentsof
NMWorksOn
hours
© Louis D. Nel 1996-2006 16Mapping ER->Tables -COMP3005 Databases
Taking Care of M:N Relationships
EMPLOYEEfname minit lname ssn bdate address sex salary superssn dno
PROJECTpname pnumber plocation dnum
DEPARTMENTdname dnumber mgrssn mgrstartdate
DEPENDENTessn dpendent_name sex bdate relationship
WORKS_ONessn pno hours
Note the key here
© Louis D. Nel 1996-2006 17Mapping ER->Tables -COMP3005 Databases
Step 6 - Multi-valued Attributes
Employee
Bdate
Sin
Name
Sex AddressSalary
Name
No
Department
Location
Project
Name
NoDependent
BdateName Sex Relationship
11
StartDate
Manages
1N
1 N
1
N
Supervisor Supervisee
WorksForControls
Supervision1
N
Dependentsof
NMWorksOn
hours
Location
© Louis D. Nel 1996-2006 18Mapping ER->Tables -COMP3005 Databases
Taking Care of Multi-valued Attributes
EMPLOYEEfname minit lname ssn bdate address sex salary superssn dno
PROJECTpname pnumber plocation dnum
DEPARTMENTdname dnumber mgrssn mgrstartdate
DEPENDENTessn dpendent_name sex bdate relationship
WORKS_ONessn pno hours
DEPT_LOCATIONSdnumber dlocation
© Louis D. Nel 1996-2006 19Mapping ER->Tables -COMP3005 Databases
Step 7 N-ary Relations
Supplydnumber pjnumber ptnumber ...
Supplierdnumber ...
Partptnumber ...
Projectpjnumber ...
© Louis D. Nel 1996-2006 20Mapping ER->Tables -COMP3005 Databases
E-R diagram for Company Data (fig 3.2)
Employee
Dependent
1
1
11
N
N
NM1 N
1
N
Supervisor Supervisee
Bdate
Sin
Name
Sex AddressSalary
Name
NoLocation
StartDate
Supervision
Dependentsof
ManagesDepartment
Location
WorksFor
Project
Controls
Name
No
WorksOn
BdateName Sex Relationship
hours
© Louis D. Nel 1996-2006 21Mapping ER->Tables -COMP3005 Databases
Mapping Inheritance to tables (option1)
Make Serial Number
Vehicle
FrameSize
Bicycle
#gearsEngineSize
Car
#doors
transmission
Model
Vehicle: serialNumber, Make, Model
Car: serialNumber, #Doors, EngineSize, Transmission
Bicycle: serialNumber, #Gears, FrameSize
© Louis D. Nel 1996-2006 22Mapping ER->Tables -COMP3005 Databases
Mapping Inheritance to tables (option2)
Make Serial Number
Vehicle
FrameSize
Bicycle
#gearsEngineSize
Car
#doors
transmission
Model
Car: serialNumber, Make, Model #Doors, EngineSize, Transmission
Bicycle: serialNumber, Make, Model, #Gears, FrameSize
© Louis D. Nel 1996-2006 23Mapping ER->Tables -COMP3005 Databases
Mapping Inheritance to tables (option3)
Make Serial Number
Vehicle
FrameSize
Bicycle
#gearsEngineSize
Car
#doors
transmission
Model
Vehicle: serialNumber, Make, Model, #Doors, Engine Size, Transmission, #Gears, FrameSize
© Louis D. Nel 1996-2006 24Mapping ER->Tables -COMP3005 Databases
Mapping Inheritance to tables (option4)
Make Serial Number
Vehicle
FrameSize
Bicycle
#gearsEngineSize
Car
#doors
transmission
Model
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 25Mapping ER->Tables -COMP3005 Databases
Mapping Multiple Inheritance
© Louis D. Nel 1996-2006 26Mapping ER->Tables -COMP3005 Databases
…Mapping Multiple Inheritance
© Louis D. Nel 1996-2006 27Mapping ER->Tables -COMP3005 Databases
Mapping Categories
© Louis D. Nel 1996-2006 28Mapping ER->Tables -COMP3005 Databases
…Mapping Categories