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.
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 answerHide 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
- Create relations for both participating entity types.
- Identify the entity on the many side.
- Add the primary key of the one-side entity as a foreign key into the many-side relation.
- 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_Nofrom 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
- Create relations for both participating entity types.
- Create a new relation for the relationship.
- Include the primary keys of both entities as foreign keys in the new relation.
- The composite primary key = PK of entity 1 + PK of entity 2.
- 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_IDandCourse_IDas a composite primary key, andGradeas a relationship attribute.
Summary Table
| Relationship | New Relation Created? | Foreign Key Placement |
|---|---|---|
| 1:N | No | PK of 1-side added as FK in N-side relation |
| N:M | Yes | PKs of both entities become composite PK in new relation |
0 · 5 marks
During ER-to-Relational Mapping, show how you map 1:N and N:M relationship into a relation? [5]
Show solved answerHide 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:
- Create a relation for each participating entity.
- Identify the entity on the 1-side and the entity on the N-side.
- Add the primary key of the 1-side entity as a foreign key in the N-side relation.
- 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_Nois 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:
- Create a relation for each participating entity.
- Create a new relation for the relationship itself.
- Include the primary keys of both participating entities as foreign keys in the new relation.
- The combination of these foreign keys usually forms the composite primary key of the new relation.
- 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_IDis a foreign key referencing STUDENTCourse_IDis a foreign key referencing COURSE- (Stu_ID, Course_ID) together form the composite primary key of ENROLLMENT
Enroll_Dateis a relationship attribute included in the new relation
Summary Table
| Relationship | Mapping Strategy | Where FK is Placed |
|---|---|---|
| 1:N | Foreign Key Method | FK added to the N-side relation |
| N:M | New Junction Relation | New relation with both PKs as FKs |
Make Unit 4 stick
Practice BIT202 with flashcards & quizzes