DEV Community

Harsh Mange
Harsh Mange

Posted on • Originally published at harshmange.hashnode.dev on

What is Database Normalization?

Database normalization is a process of organizing the data in a relational database in a structured and efficient way, by minimizing data redundancy and dependency. It involves breaking up large tables into smaller, more manageable tables, and establishing relationships between them.

Normalization is achieved by following a set of rules, called Normal Forms (NF), which are designed to ensure that each table has a well-defined purpose, and that data is stored in a logical and consistent way.

There are several normal forms, including first normal form (1NF), second normal form (2NF), and third normal form (3NF). Let's take a look at each of these normal forms, along with an example:

  1. First Normal Form (1NF): A table is in 1NF if it contains only atomic values (values that cannot be further decomposed). For example, a customer table that stores a customer's address as a single field, like "123 Main St, Anytown, USA," violates 1NF because the address can be broken down into separate fields (street address, city, state, and zip code).

  2. Second Normal Form (2NF): A table is in 2NF if it is in 1NF and all non-key attributes are dependent on the primary key. For example, consider a table that stores orders and order items. The order table has a composite primary key consisting of order ID and customer ID, while the order items table has a composite primary key consisting of order ID and product ID. To achieve 2NF, we could split the order items table into two tables: one that stores order details (order ID, customer ID, date, etc.), and one that stores order item details (order ID, product ID, quantity, price, etc.).

  3. Third Normal Form (3NF): A table is in 3NF if it is in 2NF and all non-key attributes are independent of each other. For example, consider a table that stores employee data, including employee ID, name, department, and department manager. The department manager is not directly dependent on the employee ID, but rather on the department ID. To achieve 3NF, we could split the employee table into two tables: one that stores employee details (employee ID, name, department ID, etc.), and one that stores department details (department ID, department name, department manager, etc.).

By following the rules of normalization, we can create a well-organized and efficient database structure that is easy to manage and maintain.

Top comments (0)