Difficulty: Intermediate
Explain the first three normal forms (1NF, 2NF, 3NF). How would you normalize a table that violates them?
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.
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:
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").
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 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.
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".
| Form | Rule | Typical violation | Fix |
|---|---|---|---|
| 1NF | Atomic values, no repeating groups | tags = 'a,b,c' | Child table, one row per value |
| 2NF | No partial dependency on part of a composite key | product_name in order_items | Move to products |
| 3NF | No non-key → non-key dependency | pincode → city | Lookup table |
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.
-- 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.
1NF, 2NF, 3NF, Functional Dependency, Transitive Dependency, Update Anomalies