Design and best practice · Designing databases

Database normalisation: from one table to 3NF

Normalising means separating data according to its dependencies, to avoid repeated information and contradictory changes. Each table should represent one clear entity or relationship.

Keys and dependencies

The problem: an order with lists inside its columns

Picture a spreadsheet with one row per order and the columns products, quantities and prices. Order 101 contains «Notebook, Pencil», «2, 3» and «350, 100». To find out how much of each product was sold, you would have to split three lists and trust that they all keep the same order.

On top of that, each order repeats the customer's name and the products' current names. That causes three specific problems:

  • Update: changing a customer's name means checking all their orders; if one is missed, there are two versions of the name.
  • Insertion: you cannot record a new product without inventing an order that contains it.
  • Deletion: if you delete a product's only order, you could also lose its catalogue data.

We will design separate tables based on the rules of this fictional business. The Database Design normalization chapter (opens in a new tab) explains the normal forms and the dependencies behind this process.

First, identify the keys and the functional dependencies

A functional dependency A → B means that a value of A determines a single value of B in any valid state of the model. It is not enough for that to happen by chance in a sample. In our shop, these are explicit rules:

  • Each order_id determines a customer and a date.
  • Each customer_id determines the customer's current name.
  • Each product_id determines the product's current name.
  • The pair (order_id, line_number) determines the product, the quantity and the price agreed on that line.

Line numbers start again within each order. That is why line 1 of order 101 and line 1 of order 102 are different. We allow the same product to appear on two lines of an order, so we do not use (order_id, product_id) as a key. If the business banned that repetition, it would have to be expressed with another constraint.

A candidate key is a minimal set of attributes that identifies a row. There can be more than one; the primary key is the one chosen to identify rows day to day. Normalisation considers dependencies on all candidate keys, not just the column marked as PRIMARY KEY.

From 1NF to 3NF

1NF: one row per line, with values from the chosen domain

In our case, reaching 1NF means removing the parallel lists and representing each line as a row. Values are treated as units of the defined domain: there is no need to split every piece of text into characters or words. We do need to separate the products, because we want to query and relate them individually.

Table in 1NF: it still repeats customer and product data
OrderLineCustomer (id / name)Product (id / name)QuantityPrice in cents
10117 / Amy20 / Notebook2350
10127 / Amy21 / Pencil3100
10218 / Liam20 / Notebook1400

The table would also hold the order date: 10 September for 101 and 11 September for 102. Every row now has a key, but we keep repeating information that does not belong to the line alone. Being in 1NF does not, on its own, solve update anomalies.

2NF: remove dependencies on part of the key

In 2NF, besides meeting 1NF, every non-prime attribute depends on the whole of each candidate key, not just part of it. An attribute is prime if it belongs to some candidate key. In our example we assume that the only candidate key of the lines table is (order_id, line_number).

The date and the customer depend only on order_id. They do not need the line number to be determined. We move them to an orders table. In the lines we keep the product, its name, the quantity and the agreed price; the next step will deal with the dependency of the product name.

Decomposition to reach 2NF in this example
TableKeyOther data
ordersorder_idcustomer_id, customer_name, date
linesorder_id + line_numberproduct_id, product_name, quantity, unit_price_cents

Adding an artificial id to the original table would not automatically fix those dependencies. You still have to analyse the rules and the other candidate keys. A technical key exists to identify; normalising exists to organise the facts that depend on that identification.

3NF: separate the unwanted transitive dependencies

In orders we have order_id → customer_id → customer_name. In lines we have (order_id, line_number) → product_id → product_name. The customer's current name belongs to the customer; the product's belongs to the catalogue. We move them to tables of their own.

Formally, in 3NF every non-trivial functional dependency X → A satisfies the condition that X is a superkey or A is a prime attribute. A superkey identifies a row, although it may contain extra attributes; a dependency is non-trivial when the attribute on the right is not part of the set on the left. For this model, with the stated keys and rules, separating those descriptive data is enough to remove the transitive dependencies of non-prime attributes.

Final model and what each table is responsible for
TableWhat it representsRelationship
customersA customer and their current nameA customer can have several orders.
productsA product and its current nameA product can appear on several lines.
ordersAn order, its customer and its dateEach order belongs to a customer.
linesAn order item with product, quantity and priceLinks orders to products.

The price of 350 for Notebook in order 101 and of 400 in order 102 are kept in the lines. They are prices agreed in different sales. There is no rule here that «product_id determines the selling price»: discounts and tariff changes would make that statement false.

SQL example

The following script is set up for SQLite on a fresh test connection. It turns on foreign keys before starting a transaction. The names ending in _normal_demo keep these tables apart from the course tables. Money amounts are stored as whole cents.

SQLite · tables and constraints
PRAGMA foreign_keys = ON;

CREATE TABLE customers_normal_demo (
  id INTEGER PRIMARY KEY,
  name TEXT NOT NULL
);

CREATE TABLE products_normal_demo (
  id INTEGER PRIMARY KEY,
  name TEXT NOT NULL
);

CREATE TABLE orders_normal_demo (
  id INTEGER PRIMARY KEY,
  customer_id INTEGER NOT NULL,
  date TEXT NOT NULL,
  FOREIGN KEY (customer_id) REFERENCES customers_normal_demo(id)
);

CREATE TABLE lines_normal_demo (
  order_id INTEGER NOT NULL,
  line_number INTEGER NOT NULL,
  product_id INTEGER NOT NULL,
  quantity INTEGER NOT NULL CHECK (quantity > 0),
  unit_price_cents INTEGER NOT NULL
    CHECK (unit_price_cents >= 0),
  PRIMARY KEY (order_id, line_number),
  FOREIGN KEY (order_id) REFERENCES orders_normal_demo(id),
  FOREIGN KEY (product_id) REFERENCES products_normal_demo(id)
);

Primary keys identify rows and foreign keys check references. NOT NULL enforces the mandatory relationships; CHECK rules out zero or negative quantities and negative prices. Constraints help enforce the design, but they cannot decide for you what the business dependencies are.

SQLite · example data
INSERT INTO customers_normal_demo (id, name)
VALUES (7, 'Amy'), (8, 'Liam');

INSERT INTO products_normal_demo (id, name)
VALUES (20, 'Notebook'), (21, 'Pencil');

INSERT INTO orders_normal_demo (id, customer_id, date)
VALUES (101, 7, '2026-09-10'), (102, 8, '2026-09-11');

INSERT INTO lines_normal_demo
  (order_id, line_number, product_id, quantity,
   unit_price_cents)
VALUES
  (101, 1, 20, 2, 350),
  (101, 2, 21, 3, 100),
  (102, 1, 20, 1, 400);

We can rebuild the report with JOINs without duplicating the current names on every line. This query includes orders that have at least one line, because it uses INNER JOIN.

SQLite · total for each order
SELECT pe.id AS order_id, c.name AS customer,
       SUM(l.quantity * l.unit_price_cents) AS total_cents
FROM orders_normal_demo AS pe
JOIN customers_normal_demo AS c ON c.id = pe.customer_id
JOIN lines_normal_demo AS l ON l.order_id = pe.id
GROUP BY pe.id, c.name
ORDER BY pe.id;
Checked result: what each order adds up to
order_idcustomertotal_cents
101Amy1000
102Liam400

Order 101 adds up to 2 × 350 + 3 × 100 = 1000 cents. Order 102 adds up to 1 × 400 = 400. The same product appears at different prices and each order keeps its correct total.

Download the normalised example

What changes on another engine

The dependencies and normal forms do not depend on SQLite. The DDL does: PRAGMA foreign_keys is specific to SQLite. PostgreSQL, MySQL and SQL Server have their own types, identity options and constraint rules. For production, choose a native date type where one exists, and an exact monetary type or a whole-number unit that suits the business. Check the data type guide before carrying the declarations over word for word.

Limits and checks

History, denormalisation and the limits of the rule

This model shows the customer's current name. If you need to keep the name or address that appeared on an invoice, store that historical version explicitly in the document or in a versioning model. «Current name» and «name when the invoice was issued» are different facts; do not remove a necessary historical value because it looks repeated.

Nor does it follow that normalising always makes every query faster. An analytical warehouse or a summary table may repeat data for a measured reason. If you denormalise, define which value is the source, when the copy is recalculated and how you detect divergence. Start with a correct design and justify each duplication with a specific need.

In our schema, it is still possible to record an order with no lines: the foreign keys require every line to have an order, but not every order to have lines. If your process requires at least one line to confirm a sale, enforce that rule in the confirmation step. A normalised database still needs business rules.

SQLite · units per product
SELECT p.id, p.name,
       SUM(l.quantity) AS units
FROM products_normal_demo AS p
JOIN lines_normal_demo AS l ON l.product_id = p.id
GROUP BY p.id, p.name
ORDER BY p.id;

You get 20, Notebook, 3 and 21, Pencil, 3. You group by the product's identity and name, and add up the quantities. The price belongs to each sale and is not part of the grouping you were asked for.