Schema Design and Normalization
Structure tables to avoid anomalies, and know when deliberate denormalization pays off.
Intermediate · about 5 hours · difficulty 3/5
Learn first: The Relational Model, Writing SQL Queries
Normalization is the practice of organizing tables so each fact is stored in exactly one place. It prevents anomalies: updating a customer's address in one order row but not another, or being unable to store a product until someone orders it.
The normal forms, practically
- 1NF: each column holds one atomic value; no repeating groups or lists in a cell.
- 2NF: in tables with composite keys, every non-key column depends on the whole key.
- 3NF: non-key columns depend only on the key, not on other non-key columns. (If
zipdeterminescity,citydoes not belong in the orders table.)
For most applications, aiming for 3NF and then making deliberate exceptions is the right default.
Denormalization
Sometimes you intentionally duplicate data for read speed or simplicity: storing a comment_count on posts, or copying the price onto an order line (which is actually correct, because the order must remember the price at purchase time). Denormalize with intent and keep the copies in sync through transactions, triggers or events.
Practical design habits
- Name consistently (
snake_case, singular or plural, pick one). - Add
created_atandupdated_atto most tables. - Decide on soft deletes (
deleted_at) versus hard deletes up front.
You are ready to move on when you can take a messy spreadsheet-style table and split it into a normalized schema, then justify one deliberate denormalization.
Key concepts
- Update, insert and delete anomalies
- First, second and third normal form
- Deliberate denormalization
- Naming conventions
- Soft deletes vs hard deletes
- BCNF and higher forms
Mini project
Take a flat CSV of orders (customer name, address, product, price, quantity in each row) and normalize it into customers, products, orders and order_items with a migration script that moves the data.
Check yourself
Why is copying a product's price into order_items not a normalization violation in practice?
Free resources
- Database normalization (Wikipedia) (Wikipedia)
- PostgreSQL: Data Definition (PostgreSQL)