4 Er To Relational Mapping

Database Management System · Unit 4

ER to Relational Mapping

Exam-focused notes for ER to Relational Mapping (Database Management System, BIT202): what the TU syllabus asks and how it has actually been tested, with 2 solved past questions from this unit.

ER to Relational Mapping in short

After an ER diagram is drawn, it must be turned into tables. This chapter gives the standard rules for converting entities, relationships, weak entities, and specializations into relations.

ER to Relational Mapping: notes

Mapping entities to relations

Each strong entity set becomes one relation.

  • Each simple attribute becomes a column.
  • A composite attribute is split into its simple parts. Only the parts become columns.
  • The key attribute becomes the primary key.
  • A derived attribute is not stored, so it gets no column.
  • A multivalued attribute gets its own new relation. That relation holds the owner's key and the attribute. The two columns together form the primary key.

Example: Student(RollNo, Name(First, Last), DOB, Phone*) where Phone is multivalued.

CREATE TABLE Student (
    RollNo     INT PRIMARY KEY,
    FirstName  VARCHAR(30),
    LastName   VARCHAR(30),
    DOB        DATE
);

CREATE TABLE StudentPhone (
    RollNo INT,
    Phone  VARCHAR(15),
    PRIMARY KEY (RollNo, Phone),
    FOREIGN KEY (RollNo) REFERENCES Student(RollNo)
);

Mapping one-to-many relationships

For a 1:N relationship, put the primary key of the "one" side into the relation of the "many" side as a foreign key. No new table is needed. Any attribute of the relationship also moves to the "many" side.

Example: one Department has many Employee. The relationship is WorksFor.

CREATE TABLE Department (
    DeptID   INT PRIMARY KEY,
    DeptName VARCHAR(50)
);

CREATE TABLE Employee (
    EmpID   INT PRIMARY KEY,
    Name    VARCHAR(50),
    DeptID  INT,
    FOREIGN KEY (DeptID) REFERENCES Department(DeptID)
);

For a 1:1 relationship, put the foreign key on either side. It is better to choose the side with total participation. A separate table is used only in rare cases.

What this unit covers

  • Mapping entities to relations
  • Mapping one-to-many relationships
  • Mapping many-to-many relationships
  • Mapping weak entities
  • Mapping specialization and generalization

Mapping one-to-many relationships

2078 · 5 marks

During ER-to-Relational Mapping, explain with example to map 1: N and N:M relationship into a relation? [5]

Show solved answer

Mapping 1:N and N:M Relationships in ER-to-Relational Mapping

1. Mapping 1:N (One-to-Many) Relationship

Rule / Approach

In a 1:N relationship, the entity on the N-side (many-side) gets the primary key of the 1-side entity added as a foreign key in its relation. No new relation is created.

Steps

  1. Create relations for both participating entity types.
  2. Identify the entity on the many side.
  3. Add the primary key of the one-side entity as a foreign key into the many-side relation.
  4. Also include any attributes of the relationship in the many-side relation.

Example

ER Diagram: A DEPARTMENT employs many EMPLOYEES (1:N)

DEPARTMENT (1) ----manages---- EMPLOYEE (N)

Resulting Relations:

DEPARTMENT (Dept_No, Dept_Name, Location)
              PK

EMPLOYEE (Emp_ID, Emp_Name, Salary, Dept_No)
           PK                        FK → DEPARTMENT

The primary key Dept_No from DEPARTMENT is placed as a foreign key in the EMPLOYEE relation.


2. Mapping N:M (Many-to-Many) Relationship

Rule / Approach

In an N:M relationship, a new relation (cross/junction table) is created to represent the relationship. This new relation contains:

  • The primary key of the first entity (as FK)
  • The primary key of the second entity (as FK)
  • Any attributes of the relationship itself
  • The combination of both foreign keys forms the primary key of the new relation.

Steps

  1. Create relations for both participating entity types.
  2. Create a new relation for the relationship.
  3. Include the primary keys of both entities as foreign keys in the new relation.
  4. The composite primary key = PK of entity 1 + PK of entity 2.
  5. Add any relationship attributes to the new relation.

Example

ER Diagram: A STUDENT enrolls in many COURSES, and a COURSE has many STUDENTS (N:M), with an attribute Grade.

STUDENT (N) ----enrolls---- COURSE (M)
                  |
                Grade (relationship attribute)

Resulting Relations:

STUDENT (Student_ID, Student_Name, Address)
          PK

COURSE (Course_ID, Course_Name, Credits)
         PK

ENROLLMENT (Student_ID, Course_ID, Grade)
             FK→STUDENT  FK→COURSE
             |________________________|
                  Composite PK

A new relation ENROLLMENT is created with Student_ID and Course_ID as a composite primary key, and Grade as a relationship attribute.


Summary Table

RelationshipNew Relation Created?Foreign Key Placement
1:NNoPK of 1-side added as FK in N-side relation
N:MYesPKs of both entities become composite PK in new relation
Open this answer on its own page →

0 · 5 marks

During ER-to-Relational Mapping, show how you map 1:N and N:M relationship into a relation? [5]

Show solved answer

ER-to-Relational Mapping: 1:N and N:M Relationships

1. Mapping 1:N (One-to-Many) Relationship

Approach: Foreign Key Method

In a 1:N relationship between two entity types, the entity on the N-side (many-side) receives a foreign key that references the primary key of the entity on the 1-side (one-side).

Steps:

  1. Create a relation for each participating entity.
  2. Identify the entity on the 1-side and the entity on the N-side.
  3. Add the primary key of the 1-side entity as a foreign key in the N-side relation.
  4. Also include any attributes of the relationship in the N-side relation.

Example:

Consider: DEPARTMENT (1) ---- manages ---- (N) EMPLOYEE

DEPARTMENT (Dept_No, Dept_Name, Location)
EMPLOYEE   (Emp_ID, Emp_Name, Salary, Dept_No)
                                        ↑
                              Foreign Key referencing DEPARTMENT

The foreign key Dept_No is placed in the EMPLOYEE relation (N-side).


2. Mapping N:M (Many-to-Many) Relationship

Approach: New Relationship Relation (Cross/Junction Table)

In an N:M relationship, a new relation (junction table) is created to represent the relationship. A direct foreign key approach is not sufficient here.

Steps:

  1. Create a relation for each participating entity.
  2. Create a new relation for the relationship itself.
  3. Include the primary keys of both participating entities as foreign keys in the new relation.
  4. The combination of these foreign keys usually forms the composite primary key of the new relation.
  5. Include any attributes of the relationship in the new relation.

Example:

Consider: STUDENT (N) ---- enrolls ---- (M) COURSE

STUDENT    (Stu_ID, Stu_Name, Address)
COURSE     (Course_ID, Course_Name, Credits)
ENROLLMENT (Stu_ID, Course_ID, Enroll_Date)
              ↑         ↑
              FK        FK
  • Stu_ID is a foreign key referencing STUDENT
  • Course_ID is a foreign key referencing COURSE
  • (Stu_ID, Course_ID) together form the composite primary key of ENROLLMENT
  • Enroll_Date is a relationship attribute included in the new relation

Summary Table

RelationshipMapping StrategyWhere FK is Placed
1:NForeign Key MethodFK added to the N-side relation
N:MNew Junction RelationNew relation with both PKs as FKs
Open this answer on its own page →