Database normalization
Нормализация
Database normalization organizes relational tables so that a fact, such as a customer's current phone number, can be recorded once and referenced wherever it is needed. Normal forms are the rules used to check that design.
Also known as: нормальные формы
What problem does it solve?
Suppose a shop keeps an order and its customer's current phone number in the same row. A customer with ten orders now has ten copies of that number. Change nine of them and the database disagrees with itself: that's an update anomaly. If the shop can't record a new customer until they place an order, that's an insertion anomaly. If deleting the last order also erases the only record of the customer's phone number, that's a deletion anomaly.
How to reason about normal forms
Start with what the business guarantees. If each product ID has exactly one current product name, write product_id → product_name. This is a functional dependency: whenever two rows agree on the ID, they must agree on the name. A sample where all names happen to be unique does not prove that names determine IDs. Next, identify the candidate keys: the smallest sets of columns that uniquely identify a row, in the sense that no column can be removed from the set.
- First normal form (1NF): use one value per cell for the model at hand, without repeating groups. For example, represent items in an order as rows rather than adding
item_1,item_2, and so on. - Second normal form (2NF): meet 1NF, and ensure no non-key attribute depends on just part of a candidate key. If an order line is identified by
(order_id, line_no), the order date belongs with the order becauseorder_idalone determines it. - Third normal form (3NF): in a typical schema, remove dependencies from the key to one non-key attribute through another. The precise rule is that for every nontrivial dependency
X → Awith a single attribute on the right, eitherXis a superkey (it uniquely identifies a row), orAbelongs to at least one candidate key.
Example: a product name on an order line
An order line identifies the product, but the product ID, not the line's key, determines its current name. Store the name in products, and let order_lines.product_id reference that table:
SELECT l.order_id, l.line_no, p.name
FROM order_lines AS l
JOIN products AS p ON p.id = l.product_id;
When splitting a table, check that joining the pieces back recovers the original rows without dropping any or inventing combinations. In this example, a non-null foreign key to products.id ensures each line matches exactly one product. Don't confuse the current catalog price with the price actually charged on a line: the latter is a different fact and belongs on the line.
Production and interview trade-offs
3NF is a common starting point for transactional schemas. For reporting, a measured read bottleneck can justify storing derived data, such as a daily sales total. That's denormalization: it can make reads cheaper, but the team must decide how to refresh the total and how stale it can be. In an interview, expect to explain the assumptions behind your keys, show how you would split a table, and name the anomaly your change prevents.