Logo hdtoledo
hdtoledo.dev
Módulos_del_Sistema
MÓDULO_TEÓRICO

Ordering the Chaos (Normalization)

Ordering the Chaos

The Marie Kondo Method for Data (Normalization)

So far we have created tables and inserted data. But imagine Doña Pepa, to save time, decides to use a single giant notebook for everything. In the Sales table she starts writing down: The customer's name, their address, the product they bought, the name of the company that made the product, and that company's phone number.

🚨 The Problem (Update Anomaly) If the company "San Marcos Milk" changes its phone number, Doña Pepa would have to search for and cross out the old number in the 500 sales where that milk was sold. A nightmare! Also, if she makes a mistake on just one, the database becomes inconsistent.

To avoid this, we use Normalization. It's like the Marie Kondo method, but for databases: "Everything in its place, without repeating anything".


The 3 Golden Rules (Normal Forms)

Engineers created rules to ensure tables are clean. If you follow these three, your database will be perfect 99% of the time!

1. First Normal Form (1NF): No packed lists

Rule: Each cell must contain a single piece of data.

❌ Wrong: In the "products_bought" column Pepa writes: Bread, Milk, Eggs. ✅ Right: If they bought 3 things, those are 3 different records in the sales detail table. We do not put comma-separated lists in a single cell.

2. Second Normal Form (2NF): Everything depends on the Key

Rule: Take out information that has nothing to do with the transaction.

❌ Wrong: In the Sales table, writing down the customer_address. ✅ Right: The customer's address belongs to the customer, not the sale. Put that column in the Customers table. In the sales table, just use the "ID" (id_customer) as a bridge (Foreign Key).

3. Third Normal Form (3NF): Separate independent concepts

Rule: If a piece of data depends on another piece of data that is NOT the primary key, move it to another table.

❌ Wrong: In the Products table (e.g., Milk), writing down the supplier_phone of the dairy company. ✅ Right: The phone belongs to the supplier company, not the milk. Create a new Suppliers table, and in the Products table just put the id_supplier.


The Result: Chaos becomes Order

By applying normalization, Doña Pepa's giant, messy notebook transforms into a beautiful, fast, and efficient ecosystem of four separate boxes:

  1. Suppliers Table: (ID, Company Name, Phone)
  2. Customers Table: (ID, Juan's Name, His Address)
  3. Products Table: (ID, Product Name, Price, id_supplier)
  4. Sales Table: (ID, Date, id_customer, id_product)

If the milk company changes its phone number, Doña Pepa goes to the Suppliers table, changes it in a single cell, and automatically the whole store has the updated number. That is the power of relational SQL!

In the next lesson, we will see how to bring all these separated pieces of data together when Doña Pepa wants to read a complete report using JOINS.