Tuesday 3 July 2012

Second Normal Form (2NF)

A table is said to be in its Second Normal Form if it satisfied the following conditions:-
1) It satisfies the condition for the First Normal Form (1NF),
2) It does not include any partial dependencies where a column is dependent only on a part of a primary key.
3) It should remove subsets of data that apply to multiple rows of a table and place them in separate tables.
4) It create relationships between these new tables and their predecessors through the use of foreign keys. 



For example suppose we have a table CustomerOrderDetail , which contains the customer details and its order details

Table Name:-CustomerOrderDetail
Primary Key :- CustomerId + OrderId

CustomerId
OrderId
CustomerName
OrderName
Price
1
1
Lily
Laptop
35000
2
2
Salony
Mouse
150

In this table, the primary key is composition of two columns CustomerId + OrderId. Now this table is in 1NF but it is not in the 2NF since the column CustomerName is dependent on the column CustomerId and the column OrderName is dependent on the column OrderId which violates the second condition for the 2NF.

We can break this table into three different tables to convert it into the 2NF. These tables are given below:-

Table name:- Customer
Primary Key: - CustomerId

CustomerId
CustomerName
1
Lily
2
Salony

Table name:- Order
Primary Key: - OrderId

ProjectId
ProjectName
1
Laptop
2
Mouse



Table name:- CustomerOrderDetails
Primary Key: - CustomerId + OrderId

CustomerId
OrderId
Price
1
1
35000
2
2
150

Now all the three tables are in 1NF and all the columns of these tables are fully depended on their respective primary keys.

No comments:

Post a Comment