Sub topic 8
Converting an ER Diagram to Tables
This chapter assumes you can read an ER diagram (entities, attribute types, cardinality ratios, participation, weak entities) and know what superkeys, candidate keys, primary keys, and foreign keys are. It gives a fixed procedure for turning a diagram into relational tables. GATE questions often ask for the number of tables, or for which key a table gets.
The Mapping Procedure
Apply these rules in order. Each rule handles one construct, so the order keeps foreign keys pointing at tables that already exist.
- Map each strong entity set to a table.
- Map each weak entity set to a table.
- Map each binary 1:1 relationship.
- Map each binary 1:N relationship.
- Map each binary M:N relationship.
- Map each multivalued attribute.
- Map higher-degree (ternary and above) relationships.
Strong Entities and Attribute Types
A strong entity set becomes one table. Each simple attribute becomes a column. The key attribute becomes the primary key.
- Composite attributes: keep only the leaf components. An attribute
namemade offirst_nameandlast_namebecomes two columns. The composite itself gets no column. - Derived attributes: do not store them. A derived
ageis computed frombirth_datewhen needed. - Multivalued attributes: they cannot be a single column, because that would break 1NF. They get their own table (see below).
Weak Entities
A weak entity set has no key of its own. Its table contains:
- all of its own attributes,
- the primary key of the owner (identifying) entity as a foreign key,
- a primary key made of the owner's key plus the weak entity's partial key (discriminator).
The identifying relationship adds no table of its own. The foreign key is the relationship. The foreign key uses ON DELETE CASCADE, because a weak entity cannot exist without its owner.
CREATE TABLE employee (
emp_id INT PRIMARY KEY,
emp_name VARCHAR(50) NOT NULL
);
CREATE TABLE dependent (
emp_id INT,
dependent_name VARCHAR(50),
relationship VARCHAR(20),
PRIMARY KEY (emp_id, dependent_name),
FOREIGN KEY (emp_id) REFERENCES employee(emp_id)
ON DELETE CASCADE
);Binary Relationships
The cardinality ratio decides where the relationship goes.
One-to-One (1:1)
There are three options.
- Foreign key approach (preferred): put the primary key of one side into the other side's table as a foreign key. Choose the side with total participation. This avoids NULLs. Mark the foreign key
UNIQUEso it enforces the 1:1 ratio. - Merged table: if both sides have total participation, merge the two entities into a single table.
- Relationship table: a separate table with both keys. This is rarely needed. It makes sense when few instances participate and a foreign key column would be mostly NULL.
One-to-Many (1:N)
Put the primary key of the "1" side into the table of the "N" side as a foreign key. Any attributes of the relationship also move to the N side. No extra table is needed. If the N side has total participation, make the foreign key NOT NULL.
CREATE TABLE department (
dept_id INT PRIMARY KEY,
dept_name VARCHAR(50) NOT NULL UNIQUE
);
CREATE TABLE employee_works (
emp_id INT PRIMARY KEY,
emp_name VARCHAR(50) NOT NULL,
dept_id INT NOT NULL,
FOREIGN KEY (dept_id) REFERENCES department(dept_id)
);Many-to-Many (M:N)
An M:N relationship always needs a separate table. A foreign key column in either entity cannot hold many values. The table contains:
- the primary keys of both participating entities, as foreign keys,
- any attributes of the relationship,
- a primary key that is the combination of both foreign keys.
CREATE TABLE student (
student_id INT PRIMARY KEY,
student_name VARCHAR(50) NOT NULL
);
CREATE TABLE course (
course_id INT PRIMARY KEY,
title VARCHAR(80) NOT NULL
);
CREATE TABLE enrolls (
student_id INT,
course_id INT,
grade CHAR(2),
PRIMARY KEY (student_id, course_id),
FOREIGN KEY (student_id) REFERENCES student(student_id),
FOREIGN KEY (course_id) REFERENCES course(course_id)
);Multivalued Attributes
Create a new table for each multivalued attribute. It has the owner's primary key as a foreign key plus a column for the attribute value. The primary key is the pair of both columns.
CREATE TABLE employee_phone (
emp_id INT,
phone VARCHAR(20),
PRIMARY KEY (emp_id, phone),
FOREIGN KEY (emp_id) REFERENCES employee(emp_id)
ON DELETE CASCADE
);Higher-Degree Relationships
A ternary relationship becomes a table with the primary keys of all participating entities as foreign keys, plus any relationship attributes. The primary key depends on the cardinalities. If all three sides are "many", the primary key is all three foreign keys. If one side has a "1" (for example, each pair of A and B relates to at most one C), that side's key can be dropped from the primary key.
Specialization and Generalization
The extended ER constructs have several mappings. For a superclass Person and subclasses Student and Teacher:
| Approach | Tables | Best when |
|---|---|---|
| One table per entity | Person, Student, Teacher. Each subclass table holds the superclass key plus its own attributes. | Any case. Works for overlapping and partial specialization. |
| Subclass tables only | Student and Teacher. Each holds all inherited attributes. | The specialization is total and disjoint. |
| Single table | One table with a type column and all attributes. | Few subclass attributes. Gives many NULLs. |
Counting Tables: A Worked Example
Suppose a diagram has these parts:
- Strong entities:
Employee(with multivaluedphone),Department,Project. - Weak entity:
Dependent, owned byEmployee. Works_For: Employee to Department, N:1.Works_On: Employee to Project, M:N.
The tables are:
Employee, which holds the foreign keydept_idforWorks_For.Department.Project.Dependent.Employee_Phone.Works_On.
That is 6 tables. Works_For and the identifying relationship add none.
Common Errors
- Creating a separate table for every relationship. Only M:N and higher-degree relationships need one, plus 1:1 when the table approach is chosen.
- Giving a weak entity a primary key of only its partial key.
- Forgetting the table for a multivalued attribute.
- Storing a derived attribute.
- Putting the foreign key on the "1" side of a 1:N relationship.
- Counting the identifying relationship of a weak entity as a table.
Exercise
A diagram has entities Student and Club with an M:N relationship MemberOf that carries the attribute joindate. Student has a multivalued attribute email. How many tables result, and what is the primary key of the Member_Of table?
Show answer
Four tables: Student, Club, StudentEmail, and MemberOf. The primary key of MemberOf is (studentid, clubid). Both columns are foreign keys, and joindate is an ordinary column.
Exercise
Each Employee works in exactly one Department (total participation on the Employee side), and a Department has many employees. Where does the foreign key go, and should it allow NULL?
Show answer
The foreign key dept_id goes in the Employee table, the N side. It should be NOT NULL because participation is total.