Normalization

⭐ Interview Importance: HIGH
⏱️ Revision Time: 4 min

Concept

When inexperienced developers create a database, they often design it exactly like an Excel spreadsheet: one massive table containing every single piece of data they need.

OrderIDCustomerNameCustomerAddressProductPrice
1Alice123 Main StLaptop$1000
2Alice123 Main StMouse$50

This is called an Unnormalized Table. It contains massive duplication.
“Alice” and “123 Main St” are typed into the database twice.

Normalization is the formal, mathematical process of breaking this massive table down into multiple smaller, highly-focused tables, and linking them together using Foreign Keys.

Why Do We Normalize?

Normalization solves the three catastrophic Data Anomalies.

1. Update Anomaly (Data Inconsistency)

Alice moves to a new apartment (“456 Broadway”). You run an UPDATE statement to change her address. If you accidentally only update Order #1, and forget to update Order #2, the database now contains two different addresses for Alice. The data is logically corrupted. In a normalized database, Alice’s address exists in exactly one place (the users table). Updating it instantly updates it for all orders.

2. Insertion Anomaly

You want to add a brand new Product to your catalog (e.g., a “Keyboard”), but nobody has bought it yet. Because the table requires an OrderID and a CustomerName to create a row, it is mathematically impossible to insert the Keyboard into the database without faking a customer order.

3. Deletion Anomaly

Alice returns her Mouse, so you delete Order #2. However, Alice was the only person who ever bought a Mouse. By deleting the order, you accidentally completely deleted the concept of a “Mouse” and its Price from your entire database.

The Normal Forms

Normalization is executed in sequential stages called Normal Forms. A database must pass the rules of the 1st form before it can be evaluated for the 2nd form.
In modern software engineering, aiming for Third Normal Form (3NF) is the absolute industry standard.

  1. First Normal Form (1NF): Eliminate arrays and comma-separated lists. Every cell must hold exactly one value.
  2. Second Normal Form (2NF): Eliminate partial dependencies. (Requires a Primary Key).
  3. Third Normal Form (3NF): Eliminate transitive dependencies. (A column cannot rely on another non-key column).

Trade-Offs

  • Pros: Absolute data integrity. Zero duplication. Updates are extremely fast and safe because a piece of data only exists in exactly one place.
  • Cons: Read Performance (The JOIN Penalty). To answer the simple question “What is Alice’s address for Order #1?”, you can no longer just look at the orders table. You must force the database to perform CPU-intensive JOIN operations across the orders and users tables. If you normalize a database too much (e.g., splitting it into 50 tables), reconstructing a single web page could require 49 JOINs, bringing read performance to a crawl.

Interview Questions

Q: A developer argues that disk space is so cheap nowadays that we shouldn’t care about the duplicated strings in an unnormalized database. What is the fundamental flaw in their argument?
A: Normalization was originally invented in the 1970s to save expensive disk space. While disk space is cheap today, RAM is not.
Databases only perform fast if the active working set of data fits entirely in RAM (the Buffer Pool). If your tables are massively bloated with duplicated strings, fewer rows fit into RAM. The database is forced to constantly flush RAM and read from the slow physical hard drive, destroying performance. Furthermore, unnormalized data inevitably leads to Update Anomalies, which causes application bugs regardless of how cheap disk space is.

Q: What is the highest level of Normalization? Is it 3NF?
A: No, there are actually higher, stricter forms of normalization, such as Boyce-Codd Normal Form (BCNF), 4NF, and 5NF. However, these deal with extremely complex, theoretical edge cases involving multi-valued dependencies and overlapping composite keys. In standard web application development, reaching 3NF guarantees 99% of data integrity, and pursuing 4NF or 5NF often results in a database schema that is so abstracted it is completely unreadable and impossible to query efficiently.