Professional Documents
Culture Documents
BITWeek6 - L8 - ITE2422 V1
BITWeek6 - L8 - ITE2422 V1
Introduction
During this 8th lesson you will learn how to map an ER diagram to a relational model.
In the previous lesson, you have learnt about the main concepts in a relational data
model that can be directly implemented by any relational database management
system (RDBMS). Also, you have learnt about creating a proper ER diagram to a given
scenario.
Learning outcomes
Let’s try to map ER diagrams to relational models using the set of steps mentioned
below.
So, how to convert a regular (strong) entity? It is important to consider the following
facts.
• Each entity type becomes a table in the relational model.
• Each single-valued attribute becomes a column.
• Derived attributes are ignored.
• Composite attributes are represented by components.
• The key attribute of the entity type becomes the primary key of the table.
FName
MName
e
ENo LName
FullName
DateOfBirth
NIC
Employee Age
ENo DateOfBirth
Name
Employee SkillSet
From the ER Diagram in Figure 8.2, the following relational schema can be created.
If there is another multivalued attribute, then another relation will be created from
that.
…
ENo Name DependentId DependentName
Address
From the ER Diagram in Figure 8.3, the following relational schema can be created.
As can be seen in Figure 8.3, Dependent is a weak entity. A relation will be created as
Dependent. However, to identify the dependents of each employee, a foreign key will
be created from the Dependent relation to Employee relation.
Here, Employee entity and Manges relationship has a partial participation and
Department and Manges has a total participation (double lines to connect) as
illustrated in Figure 8.4. The primary key of the partial participant (Employee) will
become the foreign key of the total participant (Department).
StartDate
As can be seen in the Figure 8.4
1 1
Employee Manages Department
From the ER Diagram in Figure 8.4, the following relational schema can be created.
Both sides are having same type of participation. Employee and Desktop computer are
having total participation with the Has relationship as shown in Figure 8.5. The
primary key of either of the participants can become a foreign key in the other.
1 1
Employee Has Desktop
From the ER Diagram in Figure 8.5, the following relational schema can be created.
or
Thus, Desktop can have a foreign key called UserENo referring to ENo in Employee or
Employee can have a foreign key called DesktopId referring to DNo in Desktop. Either
one of these options can be used when converting to a relational schema.
Here, the two entity types and the relationship can be merged into a single relation.
If not, case 2 could be used.
From the Figure 8.5, the following relational schema can be created.
All the attributes of Employee and Desktop are placed in the same relation. Thus,
foreign keys are not used as in case 1 and case 2.
N 1
Employee Works_in Department
From the Figure 8.6, the following relational schema can be created.
Foreign key will be created in the Employee relation as it is the “N” side. DNo in
Employee refers DNum in Department.
N M
Employee Works_in Project
From the ER Diagram in Figure 8.7, the following relational schema can be created.
As can be seen in Figure 8.7, a new relation is created as Works_in. It has ENo as a
foreign key referring to Employee and PNo as a foreign key referring to Project. In
Works_in relation, both ENo and PNo are a composite primary key. If there are any
attributes in the Works_in relationship, then they will be included in the Works_in
relation (Hours_Worked).
Up to now we have discussed about binary relationships. So, what happens if there
are N-ary relationships?
Unary Relationships:
Self-Referencing 1: 1 Relationship
As shown in Figure 8.8, a couple works in the same company. Both husband and wife
are employees of the company. Thus, two relations will not be created to represent
husband and wife. They will be considered as roles of the Employee. Therefore, the
primary key field itself will become a foreign key in the same relation. This is called
as self-referencing.
Wife
Married_to
1
Employee
1
Husband
From the ER Diagram in Figure 8.8, the following relational schema can be created.
The ENo of the spouse will be mentioned in the Spouse attribute of Employee
relation. Spouse is a foreign key referring to the ENo in Employee.
Self-Referencing 1: N Relationship
This will be handled as same as self-referencing 1:1 relationship. That is, the primary
key field itself will become a foreign key in the same relation (Figure 8.9).
Manager
Manager_of
1
Employee
N
Subordinate
From the ER Diagram in Figure 8.9, the following relational schema can be created.
Self-Referencing M: N Relationship
As illustrated in Figure 8.10, an employee can be a guarantor for many employees and
an employee can be beneficiary from many employees (as guarantors). Even though,
only Employee entity is used in the ER diagram, there will be two resulting relations
in the relational model. One relation represents the entity and the other relation
represents the M:N relationship.
M Guarantor_of
Employee
N
From the ER Diagram in Figure 8.10, the following relational schema can be created.
Here, from M:N relationship a relation is created as Guaranty. Then there will be two
primary keys in the Guaranty relation to represent the two roles (Guarantor and
Beneficiary) of the employee in the unary relationship. Guarantor and Beneficiary in
Guaranty relation are foreign keys referring to Employee relation.
N-ary Relationships:
Here, we will be considering both ternary and n-ary relationships. There are three
entities participating with the prescription relationship in Figure 8.11. A new relation
will be created from the Prescription relationship. This new relation contains three
foreign keys - one from each of the participating Entities. Thus, the primary key of
the new relation is the combination of all three foreign keys.
Medicine
From the ER diagram in Figure 8.11, the following relational schema can be created.
Activity 8.1
Summary
Now we have completed Lesson 8. In this lesson, we discussed various concepts in
mapping an ER diagram to a relational model.
Based on the type of the entity, relationships and their attributes there are
variations in converting an ER diagram to a relation model. By following the steps
mentioned in this lesson, you will be able to create a relational database for an ER
diagram.
In the lesson 9, you will learn more details about converting an EER diagram to a
relational model. Thus, you will be able to start creating a relational database.
Before you go to the next lesson, complete the activities to check what you
have learnt in the Lesson 8.