Logo
Published on

Exercise - Convert Employee-Project Table to 2NF in DBMS

πŸ“ Exercise: Convert Employee-Project Table to 2NF in DBMS

Understanding how to apply normalization rules in real-world scenarios is essential for designing clean and efficient databases. Let’s analyze the following table and determine whether it complies with Second Normal Form (2NF).

🧾 Problem Statement

A company maintains a table to track employee projects:

Employee_IDEmployee_NameProject_IDProject_NameProject_Manager
101AliceP1AlphaJohn
101AliceP2BetaSarah
102BobP1AlphaJohn
103CharlieP3GammaAlice

❓ Is the Table in 2NF?

Step 1: Identify the Primary Key

In this table, each row represents an assignment of an employee to a project. So, the composite primary key is:

{Employee_ID, Project_ID}

Step 2: Check for Partial Dependencies

Let’s examine each attribute:

  • Employee_Name depends only on Employee_ID, not on the full composite key β†’ ❌ Partial Dependency
  • Project_Name and Project_Manager depend only on Project_ID β†’ ❌ Partial Dependency

Since non-key attributes depend only on part of the primary key, this table violates 2NF.

βœ… Converting the Table to 2NF

To achieve 2NF, we must eliminate partial dependencies by splitting the table into three separate tables:

1. Employee Table

Stores employee details.

Employee_IDEmployee_Name
101Alice
102Bob
103Charlie

2. Project Table

Stores project details.

Project_IDProject_NameProject_Manager
P1AlphaJohn
P2BetaSarah
P3GammaAlice

3. Employee_Project Table

Tracks which employee is working on which project.

Employee_IDProject_ID
101P1
101P2
102P1
103P3

🎯 Result After 2NF

Now:

  • Each table has a single primary key.
  • All non-key attributes are fully dependent on their respective primary keys.
  • We’ve removed all partial dependencies and achieved Second Normal Form (2NF).

🧠 Solution to Exercise 1: Convert Employee-Project Table to 2NF

In the previous exercise, we were given a table tracking employee assignments to projects. Let's now analyze whether it complies with Second Normal Form (2NF) and learn how to convert it if it doesn't.

πŸ“‹ Original Table: Employee Projects

Employee_IDEmployee_NameProject_IDProject_NameProject_Manager
101AliceP1AlphaJohn
101AliceP2BetaSarah
102BobP1AlphaJohn
103CharlieP3GammaAlice

πŸ” Step-by-Step Analysis

βœ… Step 1: Identify the Primary Key

Since the table tracks which employee is assigned to which project, the composite primary key is:

{Employee_ID, Project_ID}

⚠️ Step 2: Check for Partial Dependencies

A partial dependency occurs when a non-key attribute depends on only part of the composite primary key.

Let’s look at the dependencies:

  • Employee_Name depends only on Employee_ID β†’ ❌ Partial dependency
  • Project_Name and Project_Manager depend only on Project_ID β†’ ❌ Partial dependency

Since these dependencies don't rely on the full key, the table violates 2NF.

πŸ› οΈ Converting to 2NF

To eliminate partial dependencies, we break the table into three:

πŸ“„ 1. Employee Table

Stores data related only to the employee.

Employee_ID (PK)Employee_Name
101Alice
102Bob
103Charlie

πŸ”Ž Explanation:

  • Employee_Name is fully functionally dependent on Employee_ID.
  • No partial dependency β†’ βœ… 2NF

πŸ“„ 2. Project Table

Stores data related only to the project.

Project_ID (PK)Project_NameProject_Manager
P1AlphaJohn
P2BetaSarah
P3GammaAlice

πŸ”Ž Explanation:

  • Project_Name and Project_Manager are functionally dependent on Project_ID.
  • No partial dependency β†’ βœ… 2NF

πŸ“„ 3. Employee_Project Table

Represents the many-to-many relationship between employees and projects.

Employee_ID (PK, FK)Project_ID (PK, FK)
101P1
101P2
102P1
103P3

πŸ”Ž Explanation:

  • Composite primary key {Employee_ID, Project_ID}
  • No non-key attributes β†’ βœ… 2NF

βœ… Final Result: Achieved 2NF

After decomposing:

  • βœ… All partial dependencies removed
  • βœ… Each table is in 1NF and 2NF
  • βœ… Data integrity is preserved
  • βœ… Redundancy reduced

🧠 Key Takeaways

  • Always identify the composite key in tables with many-to-many relationships.
  • Partial dependencies violate 2NFβ€”break them out into separate tables.
  • Use foreign keys to preserve relationships after decomposition.