SQL · Module 10 · Lesson 32 min read

Normalization: 1NF, 2NF, 3NF & BCNF

Why normalize?

A badly designed table causes anomalies:

  • Update anomaly: a user's city is stored on 50 expense rows; you update 49 → contradictions
  • Insert anomaly: you can't add a category until someone spends money in it
  • Delete anomaly: deleting a user's last expense also deletes the only record of their city

First Normal Form (1NF)

  • Each cell holds a single (atomic) value — no lists like 'food, travel' in one text column
  • No repeating groups (phone1, phone2, phone3)
  • Each row is unique (has a primary key) ➡️ Move multi-valued data to its own table (or use a proper array/JSONB type deliberately)

Second Normal Form (2NF)

  • 1NF, and every non-key column depends on the whole primary key
  • Only matters with composite keys: in order_items(order_id, product_id, product_name), product_name depends on product_id alone → move it to products

Third Normal Form (3NF) ⭐ (the usual target)

  • 2NF, and no non-key column depends on another non-key column (no transitive dependencies)
  • In expenses(…, user_id, user_city), city depends on user_id, not on the expense → move it to users
  • Memory aid: "every non-key column depends on the key, the whole key, and nothing but the key"

BCNF (Boyce–Codd Normal Form)

A slightly stricter 3NF: every determinant (a column set that decides another column) must be a candidate key. Most 3NF designs already satisfy it.

In practice

  • Design to 3NF by default
  • Derived values (totals, counts) normally aren't stored — compute them with queries or views
  • Denormalize deliberately for performance or history (next lesson)
  • Lookup tables (categories, statuses) keep values consistent

Quick smell test

  • The same value typed in many rows? → probably needs its own table
  • Columns with numbers in their names (tag1, tag2)? → a separate table
  • Commas inside a column? → not atomic

Example

SQL
-- ❌ UNNORMALIZED: one wide table with repeated and multi-valued data
CREATE TABLE bad_expenses (
  id          INT PRIMARY KEY,
  user_name   TEXT,
  user_email  TEXT,
  user_city   TEXT,          -- depends on the user, not the expense (3NF violation)
  category    TEXT,          -- free text: 'Food', 'food', 'FOOD'...
  title       TEXT,
  amount      NUMERIC,
  tags        TEXT           -- 'essential, monthly' (1NF violation)
);
INSERT INTO bad_expenses VALUES
  (1, 'Asha Sharma', 'asha@example.com', 'Kathmandu', 'Food', 'Groceries', 2450, 'essential'),
  (2, 'Asha Sharma', 'asha@example.com', 'Kathmandu', 'food', 'Vegetables', 820, 'essential, weekly'),
  (3, 'Asha Sharma', 'asha@example.com', 'Lalitpur',  'Bills', 'Electricity', 1350, 'monthly');
--                                        ↑ update anomaly: which city is right?

-- The anomalies in action
SELECT DISTINCT user_city FROM bad_expenses WHERE user_email = 'asha@example.com';  -- 2 cities!
SELECT DISTINCT category FROM bad_expenses;                                         -- Food vs food

-- ✅ NORMALIZED (3NF): each fact stored once
--   users(id, name, email UNIQUE, city)          ← user facts
--   categories(id, name UNIQUE)                  ← lookup table
--   expenses(id, user_id FK, category_id FK, title, amount)
--   expense_tags(expense_id FK, tag)  or  tags TEXT[]   ← atomic values

-- Splitting a comma list into atomic rows (fixing 1NF)
SELECT id, trim(tag) AS tag
FROM bad_expenses, unnest(string_to_array(tags, ',')) AS tag
ORDER BY id;
-- 1 essential | 2 essential | 2 weekly | 3 monthly

-- In a normalized design, a user's city lives in ONE place
UPDATE users SET city = 'Lalitpur' WHERE email = 'asha@example.com';   -- one row, done

DROP TABLE bad_expenses;