Sub topic 6
Converting an ER Diagram to Tables
Assume you know basic set notation, tables, rows, columns, and introductory SQL. This chapter maps entities, attributes, relationships, and constraints from an entity-relationship (ER) diagram into relational tables.
Map Strong Entities
A strong entity has its own identifying key. Create one table for each entity set:
- Convert each simple attribute into a column.
- Split composite attributes into their components. For example,
Addresscan becomestreet,city, andpostal_code. - Choose the entity's key as the table's primary key.
- Do not store derived attributes when they can be calculated reliably.
For example, an Employee entity becomes an employee table with emp_id as its primary key.
Map Relationships
The relationship's cardinality determines where foreign keys belong.
| ER relationship | Relational mapping |
|---|---|
| One-to-many | Put the one-side key as a foreign key in the many-side table |
| One-to-one | Put a foreign key in one table, usually the total-participation side |
| Many-to-many | Create a junction table containing both entity keys |
A foreign key is a column whose values must match a key in another table. A junction table represents relationship instances. Its composite primary key often prevents duplicate relationships.
CREATE TABLE department (
dept_id INT PRIMARY KEY,
dept_name VARCHAR(50) NOT NULL UNIQUE
);
CREATE TABLE employee (
emp_id INT PRIMARY KEY,
emp_name VARCHAR(80) NOT NULL,
dept_id INT,
FOREIGN KEY (dept_id) REFERENCES department(dept_id)
);
CREATE TABLE project (
project_id INT PRIMARY KEY,
project_name VARCHAR(100) NOT NULL
);
CREATE TABLE assignment (
emp_id INT,
project_id INT,
hours_per_week INT CHECK (hours_per_week > 0),
PRIMARY KEY (emp_id, project_id),
FOREIGN KEY (emp_id) REFERENCES employee(emp_id),
FOREIGN KEY (project_id) REFERENCES project(project_id)
);Here, each employee can belong to at most one department, while employees and projects have a many-to-many relationship. assignment stores the relationship attribute hours_per_week.
Map Multivalued and Weak Entities
A multivalued attribute needs a separate table. For example, an employee with several phone numbers can use:
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
);A weak entity depends on an owner entity for identification. Combine the owner's primary key with the weak entity's partial key. The owner key is also a foreign key.
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
);The ON DELETE CASCADE action removes dependents when their employee is deleted. This matches the weak entity's existence dependency.
Exercise
Where should the foreign key go for a one-to-many relationship between Department and Employee?
Show answer
Put Department's primary key in Employee, because many employees can refer to one department.