Database Normalization

Difficulty: Intermediate

Question

Explain the first three normal forms (1NF, 2NF, 3NF). How would you normalize a table that violates them?

Answer

Normalization is the process of organising tables so that each fact is stored exactly once. 1NF requires atomic column values and no repeating groups, 2NF requires that every non-key column depends on the whole primary key (no partial dependency), and 3NF requires that non-key columns depend only on the key and not on other non-key columns (no transitive dependency). The goal is to remove redundancy so that inserts, updates and deletes cannot leave the data contradicting itself.

The problem normalization solves: anomalies

Picture a single orders sheet at a small Flipkart seller: order_id, product_id, product_name, product_price, customer_id, customer_name, customer_city, qty. It works until it does not:

  • Update anomaly: a customer moves from Pune to Bengaluru and you update 3 of their 40 rows. Now the database disagrees with itself.
  • Insert anomaly: you cannot record a new product until someone orders it, because there is no row to put it in.
  • Delete anomaly: delete the only order for a product and you lose the product's name and price too.

Each normal form removes one class of these problems by splitting the table along its functional dependencies (A → B means "if you know A, B is determined").

1NF: atomic values, no repeating groups

A table is in 1NF when every cell holds a single value and there are no repeating column groups like phone1, phone2, phone3 or a comma-separated tags string. The fix is to move the repeating data into its own table with one row per value (customer_phones(customer_id, phone)). Note that "atomic" is about how you query the data: storing a full address in one column is fine if you never filter by city, and modern types like Postgres jsonb or arrays are a deliberate, accepted bend of the rule.

2NF: no partial dependency on a composite key

2NF only matters when the primary key has more than one column. In order_items(order_id, product_id, qty, product_name) the key is (order_id, product_id). qty depends on the whole key, but product_name depends only on product_id. That is a partial dependency. Move it out: products(product_id, product_name, price) and keep order_items(order_id, product_id, qty). A table with a single-column key that is already in 1NF is automatically in 2NF.

3NF: no transitive dependency

In customers(customer_id, name, pincode, city), pincode → city, so city depends on the key only through pincode. That is a transitive dependency. Split it into pincodes(pincode, city) and keep pincode in customers. The memorable summary: every non-key attribute must depend on "the key, the whole key, and nothing but the key".

FormRuleTypical violationFix
1NFAtomic values, no repeating groupstags = 'a,b,c'Child table, one row per value
2NFNo partial dependency on part of a composite keyproduct_name in order_itemsMove to products
3NFNo non-key → non-key dependencypincode → cityLookup table

Beyond 3NF and when to stop

BCNF tightens 3NF: every determinant must be a candidate key. It matters in tables with overlapping candidate keys, which are rare in practice. Most OLTP schemas aim for 3NF or BCNF and then denormalize deliberately where read performance demands it, for example storing price_at_purchase on an order line. That is not a violation: the price at purchase time is a different fact from the product's current price, and it must not change when the catalogue does.

Normalizing an orders sheet step by step. Each normal form removes one kind of dependency that causes anomalies.
  1. Start: one wide orders table: Customer, product and order facts repeated on every row.
  2. 1NF: make values atomic: Split comma-separated phones or tags into child rows.
  3. 2NF: remove partial dependencies: product_name depends only on product_id, so move it to products.
  4. 3NF: remove transitive dependencies: pincode determines city, so move city to a pincodes table.
  5. Result: each fact stored once: Updates touch one row and cannot contradict each other.

Code examples

Normalized schema for orders (3NF)

-- Each fact lives in exactly one place
CREATE TABLE customers (
  customer_id BIGINT PRIMARY KEY,
  name        TEXT NOT NULL,
  pincode     CHAR(6) NOT NULL REFERENCES pincodes(pincode)
);

CREATE TABLE products (
  product_id  BIGINT PRIMARY KEY,
  name        TEXT NOT NULL,
  price_paise BIGINT NOT NULL          -- current catalogue price
);

CREATE TABLE orders (
  order_id    BIGINT PRIMARY KEY,
  customer_id BIGINT NOT NULL REFERENCES customers(customer_id),
  created_at  TIMESTAMPTZ NOT NULL DEFAULT now()
);

CREATE TABLE order_items (
  order_id    BIGINT REFERENCES orders(order_id),
  product_id  BIGINT REFERENCES products(product_id),
  qty         INT NOT NULL CHECK (qty > 0),
  price_paise_at_purchase BIGINT NOT NULL, -- historical fact, not redundancy
  PRIMARY KEY (order_id, product_id)
);

Customer, product and order data each have their own table. The price at purchase is stored on the line item because it is a separate fact that must not change when the catalogue price changes.

Key points

  • 1NF: atomic values, no repeating groups or comma-separated lists.
  • 2NF: no non-key column depends on only part of a composite key.
  • 3NF: no non-key column depends on another non-key column.
  • Normalization prevents update, insert and delete anomalies.
  • BCNF is a stricter 3NF; most production OLTP schemas target 3NF and denormalize selectively.

Common follow-up questions

  • What is BCNF and give an example of a table in 3NF but not BCNF?
  • What is a functional dependency and how do you identify them?
  • When would you intentionally denormalize?
  • Is storing JSON in a column a violation of 1NF?

Interview tips

  • Use one running example (orders, customers, products) and normalize it live through each form.
  • Name the anomalies explicitly; interviewers want to hear why normalization matters, not just definitions.
  • Mention that 2NF is only relevant with composite keys, which shows real understanding.

Concepts covered

1NF, 2NF, 3NF, Functional Dependency, Transitive Dependency, Update Anomalies

More Database Design questions