2078 10

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]

Database Management SystemChapter 4: ER to Relational MappingMapping one-to-many relationships

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

Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.