Second Normal Form (2NF)
2NF takes this concept a bit further. This rule states that any data that is in multiple rows of the same table should be moved to a new table and the two tables should be joined via a foreign key. Basically, the idea is to reduce data redundancy by extracting redundant data and moving it elsewhere. Let's imagine you have a customers table that has the following columns
Customers
Customers
- CustomerID
- FirstName
- LastName
- Address
- City
- State
- PostalCode
- Country
Sure, you will have more than one customer from the same city, even more from the same state and definitely more from the same country. You may possibly have many customers from the same postal (zip) code. As you enter customers in this table, you are duplicating all this data i.e. City, State, PostalCode, Country etc. This table is not in 2NF. To make it 2NF compliant, create a new table and store City, State, PostalCode, Country. Let's call this table Addresses. This table has the following columns
Addresses
Addresses
- City
- State
- PostalCode
- Country
What columns do you think could be redundant here? Sure PostalCode won't be, but what about City? A city could have multiple postal codes? Definitely a State can have multiple cities and Country will have multiple states. A good schema for all the tables may be the following...
Customers
- CustomerID
- StreetAddress
- CityID
CityAddress
- CityID
- City
- PostalCode
- StateID
- CountryID
States
- StateID
- State
Countries
- CountryID
- Country
Notice, one table resulted in 4 different tables. It is definitely more complex but more flexible. You can even pre-populate States and Countries in advance.
This schema also satisfies second rule of 2NF i.e. the related tables should be related by foreign key. CityID is a foreign key in Customers, StateID, CountryID are foreign keys in CityAddress table.
We will discuss 3NF in future post.
Thank you.
No comments:
Post a Comment
As always, your comments are welcome and appreciated!