Normalization is a process of organizing data in a relational database to reduce data redundancy and improve data integrity. There are several levels of normalization including 1NF, 2NF, and 3NF. Here are some examples to illustrate these concepts:
- First Normal Form (1NF)
To meet the requirements of 1NF, a table should have no repeating groups, and all the attributes in a table should be atomic. Here's an example to demonstrate this concept:
Consider a table called "CUSTOMER" having the following attributes: Customer_ID, Customer_Name, and Contact_Details. The Contact_Details column contains multiple values (repeating groups) like Phone_Number, Email, and Address. To conform to 1NF, we can split the Contact_Details column into individual columns as follows:
CUSTOMER(Customer_ID, Customer_Name, Phone_Number, Email, Address)
- Second Normal Form (2NF)
In order to achieve 2NF, a table needs to be in 1NF and have a primary key, which can be a composite key. In addition, any non-key attributes in the table should depend on the full primary key. Here's an example to demonstrate this concept:
Consider a table called "ORDER_DETAILS" having the following attributes: Order_ID, Product_ID, Product_Name, Quantity, and Price. The primary key is (Order_ID, Product_ID). However, Product_Name is dependent only on Product_ID and not on the full primary key. To conform to 2NF, we can split the table into two separate tables like this:
ORDERS(Order_ID, Product_ID, Quantity, Price)
PRODUCTS(Product_ID, Product_Name)
- Third Normal Form (3NF)
In order to achieve 3NF, a table needs to be in 2NF and should have no transitive dependencies. Transitive dependency means that a non-key attribute depends on another non-key attribute instead of depending on the primary key. Here's an example to demonstrate this concept:
Consider a table called "INVOICE_DETAILS" having the following attributes: Invoice_ID, Customer_Name, Product_Name, Product_Price, and Quantity. The primary key is Invoice_ID, and Customer_Name is dependent on the primary key. However, Product_Price is dependent on Product_Name and not the primary key. To conform to 3NF, we can split the table into three separate tables like this:
INVOICES(Invoice_ID, Customer_Name)
PRODUCTS(Product_ID, Product_Name)
PRODUCT_PRICES(Product_ID, Price)
In this normalized version, quantity depends on the invoice, product ID links the invoice to the product, and product name and price depend on the product ID.
These are just a few examples to help illustrate the basic concepts of normalization. There are additional levels of normalization beyond 3NF which may be appropriate for very large or complex databases, but 1NF, 2NF, and 3NF are the most commonly used normalization levels.