Trusted answers to developer questions

What is the second normal form (2NF)?

Get Started With Machine Learning

Learn the fundamentals of Machine Learning with this free course. Future-proof your career by adding ML skills to your toolkit — or prepare to land a job in AI or Data Science.

Normalization is the process of reducing redundancy from a set of relations. In database tables, we can use normal forms to do this. The four main normal forms are:

As the figure below shows, these normal forms build upon one another progressively. In this shot, we will focus on the second normal form.

Second normal form (2NF)

For a table to be in second normal form, it needs to satisfy the following conditions:

  • It should be in the first normal form.
  • It should not have any partial dependencies. This means that all non-key attributes are fully functional, dependent on the primary key.

Partial dependency is when a non-prime attribute is dependent on any proper subset of any candidate key of the table.

Now, let’s look at a concrete example of a table that is currently not in second normal form:

Person_ID Source Destination
1 Lahore Karachi
1 Multan Islamabad
2 Quetta Peshawar
3 Lahore Karachi
4 Multan Islamabad

In the example, the table has a composite primary key [Person_ID, Source] while Destination is the non-key attribute. However, since Destination is only dependent on Source, which is only part of the primary key, a partial dependency arises. Therefore, this table does not satisfy the second normal form.

We can decompose it to 2NF by removing this partial dependency. To do so, let’s break the table into two like this:

ID Source
1 Lahore
1 Multan
2 Quetta
3 Lahore
4 Multan
Source Destination
Lahore Karachi
Multan Islamabad
Quetta Peshawar

RELATED TAGS

general

CONTRIBUTOR

Anusheh Zohair Mustafeez
Did you find this helpful?