Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 45

Functional Dependencies and Second Normal Form (2NF)

Recommended resources

Hello! Welcome to your next lesson on database design.

In our last session, we took the first step in normalization by applying First Normal Form (1NF). We learned to eliminate repeating groups and ensure every value is atomic. However, we also saw that even a table in 1NF can still have data redundancy. For example, in a Customer_Orders table, the customer's name might be repeated for every single item they order.

Today, we'll address this exact issue. Our learning outcome is to define functional dependency and apply Second Normal Form (2NF) to remove partial dependencies. This is the next level of ensuring data integrity, building directly on the foundation of 1NF.

What is a Functional Dependency?

Before we can understand 2NF, we must first grasp a core concept in database theory: functional dependency. This is a fundamental relationship between attributes (columns) within a table. Your background in programming and logical thinking will be helpful here, as this concept is about defining deterministic relationships in your data.

In simple terms, a functional dependency exists when the value of one attribute (or a set of attributes) uniquely determines the value of another attribute. We write this as , which is read as "A determines B".

For a deep dive into this concept, the following video provides an excellent, clear explanation.

Functional Dependency in DBMS

This video from the channel 'making IT simple' explains functional dependency from the ground up, using a helpful analogy to mathematical functions. It clearly distinguishes between valid and invalid dependencies.

Please watch from the beginning at 00:37 until 07:19. Pay close attention to: The definition of a 'determinant' and a 'dependent'. The examples showing why Role number -> Student name is a valid dependency. The examples showing why Student name -> Department name is an invalid dependency in that specific table.

As the video showed, the key rule for a functional dependency is: for any given value of in the table, there can only be one corresponding value of .

For instance, in a Users table:

  • UserID -> Email is a valid functional dependency because a single user ID will correspond to exactly one email address.
  • City -> UserID is likely not a valid functional dependency, because many users can live in the same city. Knowing the city does not uniquely determine the user.

The Problem: Partial Dependencies and Their Anomalies

Now we can talk about Second Normal Form (2NF). The problem that 2NF solves is called a partial dependency. This issue can only arise when a table has a composite primary key (a primary key made up of two or more columns).

A partial dependency occurs when a non-key attribute is functionally dependent on only part of the composite primary key, not the whole thing.

Let's see what problems this causes in practice.

Learn Database Normalization - 1NF, 2NF, 3NF, 4NF, 5NF

In our last lesson, we used a video from the Decomplexify channel. Let's return to it, as it has a great section explaining the problems caused by tables that violate 2NF.

Watch the segment from 10:25 to 15:13. The video introduces a Player_Rating column to an inventory table. Notice how this one change leads to insertion, update, and deletion anomalies. The video then clearly defines 2NF and partial dependency.

The video demonstrates that when Player_Rating is added to the Player_Inventory table, we create redundancy. The primary key is {Player, Item_Type}, but the Player_Rating depends only on {Player}. This is a partial dependency, and it leads to:

  • Insertion Anomaly: You can't add a new player's rating until they have an item in their inventory.
  • Update Anomaly: To change a player's rating, you must update multiple rows, risking inconsistent data.
  • Deletion Anomaly: If a player loses their last inventory item, their rating information is also deleted from the database.

This image provides a sharp, visual definition of a partial dependency.

2NF - Partial Dependency Example
This diagram shows a table with a composite primary key `{ssn, pnumber}`. The `hours` attribute is **fully dependent** on the entire key—you need to know both the employee (`ssn`) and the project (`pnumber`) to know the hours worked. In contrast, the `ename` (employee name) attribute is **partially dependent** because it is determined by `ssn` alone, which is only part of the primary key.

The Solution: Applying Second Normal Form (2NF)

The rule for 2NF is straightforward:

  1. The table must be in 1NF.
  2. The table must have no partial dependencies. Every non-key attribute must be fully functionally dependent on the entire primary key.

To fix a 2NF violation, we decompose the table. We move the partially dependent attributes, along with the part of the key they depend on, into a new table.

Let's see this in action using a couple of examples.

How to Normalize a Database Into 2NF and 3NF

This article from Red-Gate provides a detailed, step-by-step walkthrough of normalizing a table that violates 2NF. It's a great practical application of the theory.

Read the section 'Normalization to 2NF', stopping before the subsection titled 'The Algorithm for Normalizing to 2NF'. Focus on how the Courses1 table is split into two new tables, Courses2 and CourseTextbooks, to resolve the partial dependency.

The article shows that the original table had a composite key {course, textbook}. However, the lecturer and department only depended on course. By splitting the table, we create:

  1. A Courses2 table containing course, department, and lecturer.
  2. A CourseTextbooks table containing just course and textbook.

This resolves the anomalies. You can now add a course without a textbook, and changing a lecturer only requires a single-row update.

This next image shows the same decomposition process for the EMP_PROJ table we saw earlier.

Second Normal Form Normalization Example
This diagram illustrates the process of bringing the `EMP_PROJ` table into 2NF. The original table is broken down into three smaller tables to eliminate partial dependencies. `EP2` stores employee information (dependent on `Ssn`), `EP3` stores project information (dependent on `Pnumber`), and `EP1` links them, storing the attribute (`Hours`) that was fully dependent on the composite key.
Test your understanding!

You are given the following table, which tracks student exam enrollments. The primary key is {StudentID, ExamID}.

StudentID StudentName ExamID ExamSubject ExamDate
101 Sarah Chen E1 Math 101 2023-12-15
101 Sarah Chen E2 Physics 201 2023-12-18
102 David Lee E1 Math 101 2023-12-15
103 Emily Jones E3 Chem 101 2023-12-16

This table is in 1NF, but it violates 2NF.

  1. Identify the functional dependencies in this table.
  2. Which dependencies are partial dependencies?
  3. How would you decompose this table to achieve 2NF? Describe the new tables and their columns.
Show answer
  1. Functional Dependencies:

    • StudentID -> StudentName
    • ExamID -> {ExamSubject, ExamDate}
  2. Partial Dependencies: Both dependencies listed above are partial. StudentName depends only on StudentID (part of the key), and ExamSubject and ExamDate depend only on ExamID (the other part of the key).

  3. Decomposition to 2NF: We would create three tables:

    • Students table: To hold student-specific information.
      • StudentID (Primary Key)
      • StudentName
    • Exams table: To hold exam-specific information.
      • ExamID (Primary Key)
      • ExamSubject
      • ExamDate
    • Enrollments table: This is the remaining link table, containing only the composite primary key.
      • StudentID (Foreign Key, part of PK)
      • ExamID (Foreign Key, part of PK)

This new design eliminates all redundancy and the associated anomalies.


The 2NF Algorithm

Since you appreciate abstract and logical frameworks, here is the generalized algorithm for normalizing a 1NF table to 2NF, as described in the Red-Gate article.

Given a table R that is not in 2NF, find a functional dependency that violates the rule (where A is a part of the primary key).

  1. Let A be the attributes on the left side of the dependency (the determinant).
  2. Let B be the attributes on the right side of the dependency (the dependent attributes).
  3. Create a new table, R2, with the attributes from A and B. The primary key of this new table is A.
  4. Remove the attributes in B from the original table, R. The original table is now R1.
  5. Repeat this process for all partial dependencies.

This is the formal procedure that we followed in the examples above.


Conclusion

In this lesson, we took a crucial step toward creating a robust database schema. By understanding and resolving partial dependencies, we eliminate a significant source of data redundancy and protect our database from modification anomalies.

Key Takeaways:

  • A Functional Dependency () means the value of attribute(s) A uniquely determines the value of attribute(s) B.
  • A Partial Dependency occurs when a non-key attribute depends on only a part of a composite primary key.
  • A table is in Second Normal Form (2NF) if it is in 1NF and contains no partial dependencies.
  • To achieve 2NF, you decompose the table, moving partially dependent attributes into a new table where the determinant part of the key becomes the primary key.

Preview of the Next Lesson:
We've solved partial dependencies, but our work isn't done. Look again at the Courses2 table from the Red-Gate example:

course department lecturer
Relational Databases Computer Science Jeremy Brown
Cloud Databases Computer Science Jeremy Brown

Here, course -> lecturer and lecturer -> department. This means department is indirectly dependent on the primary key, course. This is called a transitive dependency, and it introduces its own set of anomalies. In our next lesson, we will learn how to resolve this by applying Third Normal Form (3NF).