coderz.py

Keep Coding Keep Cheering!

Second Normal Form(2NF)

SQL Views
What is 2NF?

The second step in Normalization is 2NF. A table must meet two requirements to be in the second normal form:

  • The relation must be in the 1NF.
  • In the second normal form, all non-key attributes are entirely functionally dependent on the primary key

Let’s see, what we mean by dependency.

What are Database Dependencies, and Why Do They Matter? 

A constraint that outlines the connection between attributes is a database dependency. This occurs when data from one database table uniquely determines data from another stored in a different database table. Since database dependencies serve as the fundamental building blocks for database normalization, it is crucial to understand what they are.

The process of effectively organizing data in a database is called normalization. Database normalization aims to achieve two main goals. First, redundant data must be removed, and second, data dependencies must be made logically. By doing this, you can ensure that the data is stored logically and decrease the space a database uses.

What is Partial Dependency?

A table’s attribute may be functionally dependent on only a portion of a composite primary key rather than the entire key, which is known as partial dependency. It happens when a non-key attribute is dependent only on a portion of the columns for the primary key.

Note: If the proper subset of the candidate key determines non-prime attribute, it is called partial dependency.

For example, let’s consider a table of customer orders:

Order_IDCustomer_IDCustomer_NameOrder_DateOrder_Total
1A0110Ranbeer Singh12-03-222300
1A0281Niharika Yadav01-01-23499
1B2110Ranbeer Singh13-02-221599
Order Table

In this table, the Order_ID and Customer_ID combination serves as the primary key. But the Customer_Name attribute only needs the Customer ID; it doesn’t need the entire primary key. This is an example of partial dependency.

We must normalize the table to make sure that every non-key attribute depends on the entire primary key to eliminate partial dependency. To achieve this, we could divide the table into two tables:

Order_IDCustomer_IDOrder_DateOrder_Total
1A011012-03-222300
1A028101-01-23499
Order Table
Customer_IDCustomer_Name
10Ranbeer Singh
81Niharika Yadav
Customer Table

The Customer Name attribute now only depends on the Customer ID and does not depend on any other elements of the primary key. By doing so, the table will be in Second Normal Form (2NF).

Note: also read about Normalization of Database

Follow Me

Please follow me to read my latest post on programming and technology if you like my post.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Leave a Comment

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

Advertisement