Normalization in SQL to Reduce Data Redundancy

Problem

Various levels of normalization in SQL can be used to reduce data redundancy and have a better-structured relational data model. This tutorial looks at these various levels with explanations and examples in Microsoft SQL Server for beginners.

Solution

In this tutorial, we will talk about normalization, a process to help organize your relational database design.

What is Normalization in SQL?

It is a process that reduces redundant data, improves data integrity, simplifies table structure, and fixes dependency issues. Normalization has different levels which are covered below.

Database Normalization Levels in a DBMS

The following table summarizes the levels of normalization in a relational model:

Normalization LevelExplanation
First Normal Form (1NF)Ensures that the columns are atomic (indivisible) and all values in the column have the same data type. It means that the values cannot be divided into smaller units. For example, if we have several phone numbers inside the column, the value can be divided into smaller units.
Second Normal Form (2NF)Eliminates partial dependencies and makes sure that the non-key attributes are completely and functionally dependent on the primary key. You can have partial dependency when some of the columns partially depend on the primary key. For example, if you have a composed primary key of CustomerID and SalesID and you have the column SalesMonth which depends on the column SalesID, but not the CustomerID column.
Third Normal Form (3NF)Eliminates transitive dependencies. In other words, the non-primary keys do not depend on another non-primary key through a third attribute. The transitive dependency means that an attribute indirectly depends on another attribute.
Boyce-Codd Normal Form (BCNF)Ensures that all determinants of a table are candidate keys. It is also known as normalization 3.5. It ensures that the values determined by another value in the same table must be based on the complete key and not part of it.
Fourth Normal Form (4NF)Handles multi-valued dependencies by checking that there are no independent sets of multi-valued dependencies within a table. The multi-valued dependency exists between two columns when a value in the first column correspond to multiple values in the second one.
Fifth Normal Form (5NF)Verifies that the table information can be derived by joining two or more tables. All the information in a table should be able to be reconstructed joining smaller tables without data loss.

First Normal Form (1NF)

For a database table to comply with 1NF, it should meet the following criteria:

  • A cell cannot have more than a single value (must be indivisible).
  • The rows should contain a primary key to identify each row.
  • There is no top-to-bottom nor left-to-right order.
  • No duplicated rows are allowed.

1NF Example

The following example shows a table that is not in 1NF:

Player InformationTeam InfoGame StatsArena InfoSponsor
LeBron James, LakersFrank Vogel, Los Angeles25, 8, 7, 2023-01-15Staples Center, Los AngelesNike
LeBron James, LakersFrank Vogel, Los Angeles30, 9, 6, 2023-01-17Staples Center, Los AngelesNike
Stephen Curry, WarriorsSteve Kerr, San Francisco32, 5, 4, 2023-01-15Chase Center, San FranciscoUnder Armour
Stephen Curry, WarriorsSteve Kerr, San Francisco28, 7, 5, 2023-01-17Chase Center, San FranciscoUnder Armour

The game stats are not atomic, we can divide the data. The following example shows how to change this denormalized table to the 1NF:

Player NameTeam NameCoach NameTeam CityPointsAssistsReboundsGame DateArenaArena CitySponsor
LeBron JamesLakersFrank VogelLos Angeles258715/01/2023Staples CenterLos AngelesNike
LeBron JamesLakersFrank VogelLos Angeles309617/01/2023Staples CenterLos AngelesNike
Stephen CurryWarriorsSteve KerrSan Francisco325415/01/2023Chase CenterSan FranciscoUnder Armour
Stephen CurryWarriorsSteve KerrSan Francisco287517/01/2023Chase CenterSan FranciscoUnder Armour

We divided the game stats in points, assists, rebounds and the game date.

Second Normal Form (2NF)

For the 2NF, the database table must accomplish the 1NF and eliminate partial dependencies. The partial dependency occurs when the columns that are not primary keys are partially dependent on a primary key. In other words, if the non-primary keys are dependent on the primary key, it is in the 2NF.

2NF Example

Let’s say that we have the previous table with basketball statistics in the 1NF:

Player NameTeam NameCoach NameTeam CityPointsAssistsReboundsGame DateArenaArena CitySponsor
LeBron JamesLakersFrank VogelLos Angeles258715/01/2023Staples CenterLos AngelesNike
LeBron JamesLakersFrank VogelLos Angeles309617/01/2023Staples CenterLos AngelesNike
Stephen CurryWarriorsSteve KerrSan Francisco325415/01/2023Chase CenterSan FranciscoUnder Armour
Stephen CurryWarriorsSteve KerrSan Francisco287517/01/2023Chase CenterSan FranciscoUnder Armour

As you can see, there is some dependency between Team Name, Team City, and Coach Name. There are also dependencies between Arena Name and Arena City. Sponsor does not have a dependency with the other columns, so it can be in a separate table. Finally, the player statistics can also be separated in another table because they do have a dependency with other columns.

To achieve the 2NF, we will divide the table into the following tables:

Players

Player IDPlayer Name
1LeBron James
2Stephen Curry

Teams

Team IDTeam NameCoach NameTeam City
1LakersFrank VogelLos Angeles
2WarriorsSteve KerrSan Francisco

Arenas

Arena IDArena NameArena City
1Staples CenterLos Angeles
2Chase CenterSan Francisco

Sponsors

Sponsor IDSponsor Name
1Nike
2Under Armour

PlayerStats

Player IDTeam IDArena IDSponsor IDPointsAssistsReboundsGame Date
1111258715/01/2023
1111309617/01/2023
2222325415/01/2023
2222287517/01/2023

Third Second Normal Form (3NF)

The table is in the 3NF when two conditions are accomplished:

  • The table is in 2NF.
  • The transitive dependency should be removed.

The transitive dependency occurs when a non-key attribute depends on another with no key. It should depend on the primary key instead.

3NF Example

Let’s take a look at an example to understand the 3NF.

To convert from the 2NF to the 3NF, we need to remove the transitive dependencies first. In this example, the Coach Name and Team City depend on the Team Name. To fix this, we will convert to the 3NF by doing this:

Teams

Team IDTeam Name
1Lakers
2Warriors

TeamDetails

Team IDCoach NameTeam City
1Frank VogelLos Angeles
2Steve KerrSan Francisco

Dividing the table players into teams and team details will remove the transitive dependencies.

The Boyce-Codd Normal Form (BCNF) Normalization

The BCNF was created to fix some normalization problems that were not fixed in the 2NF. A table is in BCNF if all redundancy based on functional dependency is removed. The following example illustrates the BCNF.

BCNF Example

The following example shows how to convert the tables to the BCNF. We create a table PlayerSponsors to make sure that every determinant is a candidate key and we remove the Sponsor ID column from the PlayerStats table.

PlayerSponsors

Player IDSponsor ID
11
22

PlayerStats

Player IDTeam IDArena IDPointsAssistsReboundsGame Date
111258715/01/2023
111309617/01/2023
222325415/01/2023
222287517/01/2023

Fourth Normal Form (4NF)

The 4NF is related to the multivalued dependency. A multivalued dependency occurs when one attribute of the table uniquely determines another attribute, and both attributes are independent of each other.

4NF Example

The players can have several sponsors and arenas in different games. That can lead to a multivalued dependency. To fix that, we will modify the PlayerArenas table like this:

PlayerArenas

Player IDArena IDGame Date
1115/01/2023
1117/01/2023
2215/01/2023
2217/01/2023

We will also remove the Arena ID from the PlayerStats table.

PlayerStats

Player IDTeam IDPointsAssistsReboundsGame Date
11258715/01/2023
11309617/01/2023
22325415/01/2023
22287517/01/2023

Fifth Normal Form (5NF)

Finally, we have the 5NF. This normalization is also called the PJ AND NF (projection and join normal form). This form removes redundancy by recording multi-valued facts by the isolation of semantically-related multiple relationships. In other words, a table is in the 5NF if every non-trivial join dependency in the table is implied by the primary key. To understand this 5NF, we will use an example.

5NF Example

In this example, we have two tables and we will decompose the PlayerStats table into 2 tables:

PlayerPerformance

Player IDTeam IDArena IDGame DatePointsAssistsRebounds
11115/01/20232587
11117/01/20233096
22215/01/20233254
22217/01/20232875

PlayerTeams

Player IDTeam IDGame Date
1115/01/2023
1117/01/2023
2215/01/2023
2217/01/2023

The decomposition into two tables ensures that the information can be reconstructed and that the join dependencies are consequences of the candidate keys.

Next Steps

For more related information, refer to the following links:

One comment

  1. Under 1NF you remarked: “The rows should contain a primary key to identify each row” But there is no primary key. You did not put it. Why ?

Leave a Reply

Your email address will not be published. Required fields are marked *