Normalization and Relational Design
Design clean relational schemas with keys, constraints and normal forms, and know when to deliberately denormalize.
Intermediate · about 5 hours · difficulty 3/5
Learn first: OLTP vs OLAP Workloads
Normalization is the discipline of storing each fact once. It is essential for source systems and still shapes how you design staging layers and reason about warehouse tables.
Keys and relationships
- Primary keys uniquely identify rows; natural keys come from the business (PAN, GSTIN, order number), surrogate keys are generated integers or hashes.
- Foreign keys link tables; know one-to-one, one-to-many and many-to-many (via a bridge table).
- Grain: what one row represents. State it for every table you build.
Normal forms in practice
- 1NF: atomic values, no repeating groups (no comma-separated phone numbers in one column).
- 2NF: every non-key column depends on the whole key.
- 3NF: non-key columns depend only on the key, not on other non-key columns (store city in an address table, not state and city repeated in orders).
Beyond 3NF (BCNF and higher) rarely matters day to day.
Why and when to denormalize
Normalized schemas avoid update anomalies but need many joins. Analytical models deliberately denormalize (copying customer city onto each order row) to make queries fast and simple, accepting duplication because data is loaded in batches, not updated by users.
Constraints
NOT NULL, UNIQUE, CHECK and foreign keys catch bad data at write time. Many warehouses do not enforce them, so you test them instead (dbt tests, later).
Pitfalls
- Not knowing a table's grain, leading to double counting in joins.
- Using natural keys that change (phone numbers, emails) as primary keys.
You are ready to move on when you can take a flat spreadsheet of college admissions and split it into a 3NF schema with keys, then explain which denormalized table you would build for reporting.
Key concepts
- Primary, foreign and surrogate keys
- Table grain
- 1NF, 2NF, 3NF
- Many-to-many bridge tables
- Constraints
- Deliberate denormalization
- ER diagrams
Mini project
Take a denormalized CSV of student course registrations and design a 3NF schema in PostgreSQL with keys and constraints. Draw an ER diagram (dbdiagram.io or draw.io) and commit it to your repo.
Check yourself
What is the grain of an order_items table, and what goes wrong if you sum order-level delivery fees after joining to it?
Free resources
- PostgreSQL: Constraints (PostgreSQL)
- Database normalization (Wikipedia)
- PostgreSQL DBA roadmap (roadmap.sh)