Sobes.tech
Middle

What is normalization?

sobes.tech AI

Answer from AI

Data normalization in the context of databases is the process of organizing data to reduce redundancy and improve data integrity. It is achieved by dividing large tables into smaller ones and linking their relationships.

Main goals:

  • Eliminating data duplication.
  • Improving data integrity (avoiding insertion, update, and deletion anomalies).
  • Enhancing storage and query efficiency (in some cases).

The normalization process is based on the principles of normal forms (NF). The most common are:

  • First Normal Form (1NF): Atomicity of data in cells, absence of repeating groups of columns.
  • Second Normal Form (2NF): Meets 1NF and each non-key attribute is fully functionally dependent on the entire primary key.
  • Third Normal Form (3NF): Meets 2NF and there are no transitive functional dependencies of non-key attributes on the primary key through other non-key attributes.

Example:

Original table "Orders_Products":

Order ID Order Date Product ID Product Name Product Price Quantity Customer Name Customer Address
1 2023-10-27 101 Phone 500 1 Ivan Petrov Pushkin St., 10
1 2023-10-27 102 Headphones 100 2 Ivan Petrov Pushkin St., 10
2 2023-10-27 101 Phone 500 1 Maria Ivanova Lermontov St., 5

After normalization (up to 3NF):

"Orders" table:

Order ID Order Date Customer ID
1 2023-10-27 1
2 2023-10-27 2

"Order Items" table:

Item ID Order ID Product ID Quantity
1 1 101 1
2 1 102 2
3 2 101 1

"Products" table:

Product ID Product Name Product Price
101 Phone 500
102 Headphones 100

"Customers" table:

Customer ID Customer Name Customer Address
1 Ivan Petrov Pushkin St., 10
2 Maria Ivanova Lermontov St., 5

However, excessive normalization can lead to an increased number of joins when executing queries, which may reduce performance. Therefore, database design often seeks a balance between normalization level and performance. Denormalization is the process of intentionally violating normal forms to improve performance, usually by adding redundancy.