BIT202 ยท 2078 ยท 5 marks
During ER-to-Relational Mapping, explain with example to map 1: N and N:M relationship into a relation?
During ER-to-Relational Mapping, explain with example to map 1: N and N:M relationship into a relation? [5]
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 |
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.