How to Put a Database in Second Normal Form (2NF)
Over the past month, we've looked at several aspects of normalizing a database table. First, we discussed the basic principles of database normalization. Last time, we explored the basic requirements laid down by the first normal form (1NF). Now, let's continue our journey and cover the principles of second normal form (2NF).
Recall the general requirements of 2NF:
- Remove subsets of data that apply to multiple rows of a table and place them in separate tables.
- Create relationships between these new tables and their predecessors through the use of foreign keys.
Let's look at an example. Imagine an online store that maintains customer information in a database. They might have a single table called Customers with the following elements:
- CustNum
- FirstName
- LastName
- Address
- City
- State
- ZIP
In a 2NF-compliant database structure, this redundant information is extracted and stored in a separate table.
Our new table (let's call it ZIPs) might have the following fields:
- ZIP
- City
- State
Now that we've removed the duplicative data from the Customers table, we've satisfied the first rule of second normal form. We still need to use a foreign key to tie the two tables together. We'll use the ZIP code (the primary key from the ZIPs table) to create that relationship. Here's our new Customers table:
- CustNum
- FirstName
- LastName
- Address
- ZIP
If you'd like to ensure your database is normalized, explore our other articles in this series:
- Database Normalization Basics
- Putting your Database in First Normal Form
- Putting your Database in Second Normal Form
- Putting your Database in Third Normal Form
Source...