Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Lecture #5
ER to Relational Mapping
Prepared by Mr. Shakil Ahmed
Department of Computer, SSUET
(Applicable to all sections)
Why?
• ER model is not directly supported by any
DBMS
• needs to be translated into a model that is
supported by DBMSs
• Transformation / mapping process
– Non-deterministic
• certain ER configurations can be expressed in more than
one way in the relational model
• certain aspects related to the relational model are not
specified in the ER model
1
3/8/2011
Procedure
• To convert an Entity-Relationship design to a relational
database schema, a procedure which includes the following
steps
p mayy be followed.
– Entity Conversion
• Regular Entity Conversion
• Weak Entity Conversion
– Attribute Conversion
• Multivalued Attributes
• Composite Attributes
• Derived Attributes
– Relationship Conversion
• Binary Relationship Conversion
– 1:1
– 1:Many
– Many: Many
• Non-Binary Relationship Conversion
2
3/8/2011
3
3/8/2011
Example
Student
Major
Telephone
p Student Num Student Name
St d t T l h
Student_TelephoneNo
N CREATE TABLE
N Student-Telephone-No(
StudentNum(FK) TelephoneNo Student Num CHAR(9),
TelephoneNo CHAR(20), PRIMARY
KEY (StudentNum, TelephoneNo),
FOREIGN KEY (StudentNum)
Composite pk REFERENCES Student(StudentNum) )
Student
DOB
Age Student Num Student Name
4
3/8/2011
Example
5
3/8/2011
6
3/8/2011
Relationship Conversion
• Mapping 1:1 Relationships
– In a 1:1 binary relationship between two entity types S and T, choose one of
them (say S) as the “base” relation.
– If either S or T has a total participation
participation, choose that one as the base.
base
– Include the primary key of the other entity type as a foreign key of the base.
– Include any relationship attributes as attributes of the base
– two partial participations → one foreign key from one base relation to another
– two total participations → as above or merge(Only appropriate if the individual
relations are not needed in other constructions.)
Secretary
Deptid EmpNo
p
Manager
Department 1 1 (EmpNo,
Managed by Manager Dept_id,
Secretary, …)
7
3/8/2011
Post BASE
1 1
Employee works on Machine
hours
Table required
1 1
Employee works on Machine
8
3/8/2011
1 1
Employee works on Machine
9
3/8/2011
Post
1 M
Employee works on Machine
t bl required
table i d
1 M
Employee works on Machine
10
3/8/2011
M:M Relationships
• In a M:M binary relationship, it is not possible to collapse
p into any
the relationship y one of the entity
y types,
yp since the
relationship does not identify either entity type uniquely.
• A separate relation is required to complete the mapping.
• Irrespective of the participation conditions, three tables
are always produced in these relationships
M M
Employee works on Machine
Example
Mapping M:M Relationships
EmpNo RecordNo Dept id
Dept_id
Employee(E1) DeputedTo(S)
M Department(E2)
M
• The new relation S has, as foreign keys, the primary key of each of E1
and E2.
• Include primary key pairs which “match” in the relationship Rel
• Also include any attributes directly associatedwith Rel.
• The Tables would be
Employee (EmpNo, …)
Department (Dept_id, …)
DeputedTo (EmpNo, Dept_id, RecordNo, …)
11
3/8/2011
Example II
12
3/8/2011
Example
Conversion of Company Database into Relational Schema
• We developed the following ERD of the Company database when studying ERD lecture of the
required design
• The next slide shows the equivalent relational schema of the following obtained ERD
EMPLOYEE
Fname Minit Lname SSN Bdate Address Gender Sal Superssn DNO
DEPARTMENT
Dname Dnumber Mgrssn Mgrstartdate
DEPT_LOCATIONS
Dnumber DLocation
PROJECT
Pname Pnumber Plocation Dnum
WORKS ON
WORKS_ON
Essn Pno Hours
DEPENDENT
ESSN Dependent_name Gender Bdate Relationship
13
3/8/2011
Explanation
• The relations (of strong entities) of Employee, Department and Project have
been shown with the Primary keys of SSN, Dnumber & Pnumber.
• The relation (of weak entity) of Dependent is shown with primary key SSN
(owner entity Employee) in combination of the partial primary key of
dependent which is dependent_name.
• To map the binary relationship (of 1:1) that is manages (here the
participation is total that is every department has a manager) therefore we
include the Primary key of the Employee relation as foreign key in the
department relation and rename it as Mgrssn
• To map the binary relationship (of 1:N) that is Works_for, Controls and
Supervision.
– For Works_for we include the Primary key Dnumber of the Department relation
as foreign key in the Employee relation and call it DNO
DNO.
– For Supervision we include the Primary key of the Employee relation as foreign
key in the employee relation itself – since the relationship is recursive- and call it
superssn
– The controls relationship is mapped to the foreign key attribute Dnum of Project,
which references the Primary key Dnumber of the Department relation
14