Convert ER Diagram Into Tables
Convert ER Diagram Into Tables
Introduction
As the database grows, the ER diagram representation becomes more complex
and crowded. It is creates a difficult situation to understand the requirement and
their structure as a whole. Similarly, if ER diagram is represented at very high
level, it again creates a difficulty in understanding the system. But representation
at high level and till the minute levels is very necessary to understand the system
well. These concepts are well defined by generalization and specialization.
Sometimes, we would have divided the entities into two or more entities to be
more accurate in design. But when compared to the whole database or user, it
can be combined to one entity. Such a process is called as aggregation.
Once designing ER diagram is complete, we need to put it into logical structure.
But how it can be done? Let us discuss this in the last section.
Generalization
In our Employee example, we have seen different types of employees like
Engineer, Accountant, Salesperson, Clerk etc. Similarly each employee belongs
to different departments. We can represent it in an ER diagram as below. When
you see this diagram for the first time, you will not understand it quickly. One will
take time to understand it or he might misunderstand some requirement.
What if we group all the sub departments into one department and different
employees into one employee? However sub departments and different
employee types have same features in their own domain. So if we merge the
child entities into their parent, it makes the diagram simpler, hence easy to
understand. This method of merging the branches into one is called
generalization. We can see the generalized structure of requirement to
understand it quickly. So above ER diagram will be changed to as below:
Isn’t it simpler? Generalization is the bottom up approach which helps to design
the requirement at high level. Thus making one to understand quickly.
Specialization
It is opposite approach of generalization. Here, each entity is further divided into
sub levels to understand it deeper. In the above example, Department entity is
further divided into sub departments to understand how they are scattered. This
method of representation helps the developer to code correctly and quickly. It is a
top down approach of breaking higher level entity to low level entity. Once the
entities are understood at higher level, it makes easy to understand the
requirement at low level.
Aggregation
Look at below ER diagram of STUDENT, COURSE and SUBJECTS. What does
it infer? Student attends the Course, and he has some subjects to study. At the
same time, Course offers some subjects. Here a relation is defined on a relation.
But ER diagram does not entertain such a relation. It supports mapping between
entities, not between relations. So what can we do in this case?
If we look at STUDENT and COURSE from SUBJECT’s point of view, it does not
differentiate both of them. It offers it’s subject to both of them. So what can we do
here is, merge STUDENT and COURSE as one entity. This process of merging is
called aggregation. It is completely different from generalization. In
generalization, we merge entities of same domain into one entity. In this case
we merge related entities into one entity.
Key attribute in the ER diagram becomes the Primary key of the table.
In diagram above, STUDENT_ID, LECTURER_ID, COURSE_ID and SUB_ID are
the key attributes of the entities. Hence we consider them as the primary keys of
respective table.
One can ignore derived attribute, since it can be calculated at any time.
In the STUDENT table, Age can be derived at any point of time by calculating the
difference between DateOfBirth and current date. Hence we need not create a
column for this attribute. It reduces the duplicity in the database.
These are the very basic rules of converting ER diagram into tables and columns,
and assigning the mapping between the tables. Table structure at this would be
as below:
In our example above, SUBJECTS is the weak entity. Hence, we create a table
for it. Its attributes SUBJECT_ID and SUBJECT_NAME forms the column of this
table. Although SUBJECT_ID is represented as key attribute in the diagram, it
cannot be considered as primary key. In order to add primary key to the column,
we have to find the foreign key first. COURSE is the strong entity related to
SUBJECT. Hence the primary key COURSE_ID of COURSE is added to
SUBJECT table as foreign key. Now we can create a composite primary key out
of COURSE_ID and SUBJECT_ID.
1. Create table for both LECTURER and SUBJECT. Add the primary key of
LECTURER in SUBJECT table as foreign key. It implies the lecturer name
for that particular subject.
2. Create table for both LECTURER and SUBJECT. Add the primary key of
SUBJECT in LECTURER table as foreign key. It implies the subject taught
by the lecturer.
In both the case, meaning is same. Foreign key column can be added in either of
the table, depending on the developer’s choice.
That is, in this case both the participating entities are converted into tables, and a
new table is created for the relation between them. Primary keys of entity tables
are added into new table to form the composite primary key. We can add any
additional columns, if present as attribute of the relation in ER diagram.