ER to Relational Mapping
1. Mapping Entity Set to Relation
• Concept: Each entity set in the ER model is mapped to a relation (table) in the relational model.
• Mapping Process:
o Each attribute of the entity set becomes an attribute of the table.
o The primary key attribute of the entity set becomes the primary key in the table.
• Example: For an ER diagram of an Employee entity:
CREATE TABLE Employee (
EmpID CHAR(11),
EName CHAR(30),
Salary INTEGER,
PRIMARY KEY(EmpID)
)
2. Mapping Relationship Sets (Without Constraints) to Tables
• Concept: When mapping relationships to tables, create a table that represents the relationship
between entities.
• Mapping Process:
o Include primary keys of the participating entities as foreign keys in the relationship table.
o Add any attributes specific to the relationship as fields in the table.
o Declare a primary key for the relationship table, which includes the primary keys of the
participating entities.
• Example: For a relationship between Employee and Department (Works In):
CREATE TABLE WorksIn (
EmpID CHAR(11),
DeptID CHAR(11),
EName CHAR(30),
Salary INTEGER,
DeptName CHAR(20),
Building CHAR(10),
PRIMARY KEY(EmpID, DeptID),
FOREIGN KEY(EmpID) REFERENCES Employee,
FOREIGN KEY(DeptID) REFERENCES Department
)
3. Mapping Relationship Sets (With Constraints) to Tables
• Concept: If the relationship set involves multiple entities with key constraints, there are two
approaches to mapping:
Approach 1: Separate Table for Relationship Set
o If there is a key constraint (e.g., each department has at most one manager), a separate table is
created for the relationship set.
Example: If a department can have only one manager:
CREATE TABLE Manages (
EmpID CHAR(11),
DeptID INTEGER,
Since DATE,
PRIMARY KEY(DeptID),
FOREIGN KEY(EmpID) REFERENCES Employees,
FOREIGN KEY(DeptID) REFERENCES Departments
)
Approach 2: Include Relationship Information in Entity Table
o Instead of creating a separate table, include the relationship information in the entity set's
table.
Example: A Dep_Mgr table that merges the Departments and Manages information:
CREATE TABLE Dep_Mgr (
DeptID INTEGER,
DName CHAR(20),
Budget REAL,
EmpID CHAR(11),
Since DATE,
PRIMARY KEY(DeptID),
FOREIGN KEY(EmpID) REFERENCES Employees
)
4. Mapping Weak Entity Sets to Relational Mapping
• Concept: A weak entity is one that cannot be uniquely identified without the help of another
(owner) entity.
• Mapping Process:
o Create a table for the weak entity set.
o Include the attributes of the weak entity.
o Add the primary key of the identifying owner entity.
o Use foreign key constraints on the owner's primary key.
• Example: Mapping a Department as a weak entity that depends on Buildings:
CREATE TABLE Department (
DeptID CHAR(11),
DeptName CHAR(20),
Bldg_No CHAR(5),
PRIMARY KEY(DeptID, Bldg_No),
FOREIGN KEY(Bldg_No) REFERENCES Buildings ON DELETE CASCADE
)
5. Mapping Specialization / Generalization (EER Constructs)
• Concept: Specialization/Generalization involves creating different types of entities from a general
entity, which may result in different mapping strategies.
Method 1: Map All Entities to Separate Tables
• Map each entity involved in the relationship to its own table.
Example:
CREATE TABLE InventoryItem(ID CHAR(10), Name CHAR(50));
CREATE TABLE Book(ID CHAR(10), Publisher CHAR(50));
CREATE TABLE DVD(ID CHAR(10), Manufacturer CHAR(50));
Method 2: Map Only Subclasses to Tables
• Only the subclasses are mapped to tables, inheriting attributes from the superclass.
Example:
CREATE TABLE Book(ID CHAR(10), Name CHAR(50), Publisher CHAR(50));
CREATE TABLE DVD(ID CHAR(10), Name CHAR(50), Manufacturer CHAR(50));
Method 3: Map Only the Superclass to a Table
• The superclass is mapped to a table, and its attributes are extended with attributes from its
subclasses.
• This method introduces null values when the subclass attributes are not applicable.
Example:
CREATE TABLE InventoryItem(ID CHAR(10), Name CHAR(50), Publisher CHAR(50),
Manufacturer CHAR(50));
Conclusion
The process of mapping ER models to relational models ensures that complex relationships between
entities are accurately captured in the relational schema. It is important to select the correct mapping
approach based on the constraints and specific needs of the system.
1. Employee Table
EmpID (PK) EName Salary
E001 John Doe 50000
E002 Jane Smith 60000
2. WorksIn Table
EmpID (PK, FK) DeptID (PK, FK) EName Salary DeptName Building
E001 D101 John Doe 50000 HR B1
E002 D102 Jane Smith 60000 Finance B2
3. Manages Table (Approach 1 for Key Constraint)
EmpID (FK) DeptID (PK) Since
E001 D101 2023-01-01
E002 D102 2023-02-15
4. Dep_Mgr Table (Approach 2 for Key Constraint)
DeptID (PK) DName Budget EmpID (FK) Since
D101 HR 500000 E001 2023-01-01
D102 Finance 600000 E002 2023-02-15
5. Department Table (Weak Entity)
DeptID (PK) DeptName Bldg_No (FK)
D101 HR B1
D102 Finance B2
6. InventoryItem Table (Method 1 - Separate Tables)
ID (PK) Name
I001 Book A
I002 DVD B
Book Table:
ID (PK, FK) Publisher
I001 Pearson
DVD Table:
ID (PK, FK) Manufacturer
I002 Sony
7. Book Table (Method 2 - Subclasses Only)
ID (PK) Name Publisher
I001 Book A Pearson
DVD Table:
ID (PK) Name Manufacturer
I002 DVD B Sony
8. InventoryItem Table (Method 3 - Single Table for All)
ID (PK) Name Publisher Manufacturer
I001 Book A Pearson NULL
I002 DVD B NULL Sony