A spreadsheet exported straight into a database table is not a data model. It is a photograph of one moment, with the reference data, the repeating columns and the duplicated facts all baked into a single flat sheet.
The moment you try to update it, query it or trust it, the shape fights you. The fix is normalisation: a mechanical, well-understood process for pulling a wide table apart into tables that each describe one thing. This walks a realistic order sheet through first, second and third normal form in PostgreSQL 17, ending with foreign keys and constraints in place. The point is not academic tidiness. It is that a normalised schema makes a whole class of data-quality bug structurally impossible.
The table we start with
Here is the table as it lands from the spreadsheet. One row per order, up to three line items smeared across repeating columns, and the customer and product details copied in wherever they are needed.
Free · 4 minutes
When two of your systems disagree, do you know which one to believe?
Fourteen questions on ownership, lineage, and quality — the difference between a number on a dashboard and a number you could defend. Banded finding on screen, full sheet by email.
CREATE TABLE orders_raw (
order_id integer,
order_date text,
customer_name text,
customer_email text,
item1_sku text,
item1_name text,
item1_qty integer,
item2_sku text,
item2_name text,
item2_qty integer,
item3_sku text,
item3_name text,
item3_qty integer
);
Everything wrong with this table follows from one fact: it stores several different kinds of thing at once. Orders, order lines, customers and products are all crammed into one row. An order with four items has nowhere to go. A customer who corrects their email has to be corrected in every row they appear in, and you will miss one. This is the same tension that shows up when you try to map REST resources onto a database — one table should describe one noun.
First normal form: one value per cell, no repeating groups
First normal form (1NF) requires that every cell holds a single value and that there are no repeating groups of columns. The item1_*, item2_*, item3_* triplets are a repeating group: three copies of the same idea, arbitrarily capped at three. The 1NF move is to turn those repeated columns into repeated rows in a child table.
CREATE TABLE orders (
order_id integer PRIMARY KEY,
order_date date NOT NULL,
customer_name text NOT NULL,
customer_email text NOT NULL
);
CREATE TABLE order_lines (
order_id integer NOT NULL,
sku text NOT NULL,
product_name text NOT NULL,
quantity integer NOT NULL,
unit_price numeric(10,2) NOT NULL,
PRIMARY KEY (order_id, sku)
);
An order can now have one line or fifty. Note the composite primary key on order_lines: a line is identified by the order it belongs to and the product on it. That composite key is what the next step turns on. Also note that order_date is now a real date, not free text — 1NF is a good moment to give each column an honest type.
Second normal form: nothing depends on part of the key
Second normal form (2NF) applies when you have a composite key. It requires that every non-key column depend on the whole key, not just part of it. Look at order_lines. The key is (order_id, sku). Does product_name depend on both? No — the name of a product is fixed by its sku alone. It is the same for every order that contains it. That is a partial dependency, and it is why the product name was duplicated across the sheet in the first place.
Pull the product facts into their own table, keyed by sku:
CREATE TABLE products (
sku text PRIMARY KEY,
product_name text NOT NULL,
category_name text NOT NULL,
list_price numeric(10,2) NOT NULL
);
CREATE TABLE order_lines (
order_id integer NOT NULL,
sku text NOT NULL,
quantity integer NOT NULL,
unit_price numeric(10,2) NOT NULL, -- price at time of sale
PRIMARY KEY (order_id, sku)
);
One judgement call worth making explicit: unit_price stays on the line, while list_price moves to the product. They are not the same fact. The list price is the current catalogue price and depends only on the product. The unit price is what this customer actually paid on this order, which depends on both the order and the product — so it genuinely belongs on the line. Getting this distinction wrong is how firms lose the ability to reconstruct historical invoices after a price change.
Third normal form: no attribute depends on another attribute
Third normal form (3NF) closes the last common gap: no non-key column should depend on another non-key column. These are transitive dependencies, and there are two hiding in what we have left.
In orders, customer_email does not really depend on order_id — it depends on the customer. Two orders from the same person carry the same email, copied. In products, category_name depends on the category, not on the individual sku. Both are the classic spreadsheet symptom: change a customer’s email or rename a category, and you have to hunt down every copy. Extract each into its own table and reference it by key.
CREATE TABLE customers (
customer_id integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
full_name text NOT NULL,
email text NOT NULL UNIQUE
);
CREATE TABLE categories (
category_id integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
category_name text NOT NULL UNIQUE
);
Whether a small, fixed set like category deserves its own table rather than a CHECK constraint or an enum is a real decision, not an automatic one — I have argued the trade-offs in enum, lookup table or check constraint. A lookup table wins when the set changes without a deployment or carries extra attributes of its own.
Foreign keys and constraints make it enforceable
Splitting the tables is only half the job. Without foreign keys, nothing stops an order_line pointing at a sku that does not exist, or an order referencing a deleted customer. The keys are what turn four related tables back into one coherent model. Here is the finished 3NF schema with every relationship declared and the obvious integrity checks in place.
CREATE TABLE products (
sku text PRIMARY KEY,
product_name text NOT NULL,
category_id integer NOT NULL REFERENCES categories (category_id),
list_price numeric(10,2) NOT NULL CHECK (list_price >= 0)
);
CREATE TABLE orders (
order_id integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
order_date date NOT NULL,
customer_id integer NOT NULL REFERENCES customers (customer_id)
);
CREATE TABLE order_lines (
order_id integer NOT NULL
REFERENCES orders (order_id) ON DELETE CASCADE,
sku text NOT NULL REFERENCES products (sku),
quantity integer NOT NULL CHECK (quantity > 0),
unit_price numeric(10,2) NOT NULL CHECK (unit_price >= 0),
PRIMARY KEY (order_id, sku)
);
Read the constraints as business rules the database now enforces for you. A line cannot exist without its order (ON DELETE CASCADE clears the lines when an order is deleted). A line cannot reference a product that was never catalogued. Quantities are positive; prices are not negative. None of these can be violated by a bad import, a race condition or a careless UPDATE, because the engine refuses the write. That is the return on normalisation: the correctness lives in the schema, not in the hope that every application touching the data remembers the rules.
When to denormalise on purpose
3NF is the right default for a transactional store, but it is a default, not a commandment. Deliberate denormalisation earns its keep in a few honest cases: a read-heavy reporting table where a nightly-refreshed category_name saves a join on every dashboard query; a materialised order total cached to avoid re-summing lines; a warehouse layer built for analysts rather than writers. The rule is that denormalisation is a considered optimisation with a named owner and a refresh strategy, never an accident you inherited from a spreadsheet. When the redundant copy exists on purpose and you know how it is kept in step, it is engineering. When it exists because nobody split the table, it is a bug waiting for its moment — often the same layer where role-based views over one table do the shaping instead.
Normalise first, prove the model holds every rule you care about, and denormalise later only where a measured query cost forces your hand. Do it the other way round and you spend the rest of the system’s life reconciling copies that were never meant to disagree.
Free interactive tool
Website compliance checklist
What your site has to do, based on what it actually does
Answer as much or as little as you like — the list builds as you go. Nothing is stored against your name and no email is required.
Everything that applies
Ordered by what to do first: legal requirements you can close quickly, then larger pieces of work, then what is expected rather than required. Not exhaustive, and not a legal audit.
Dated PDF, yours to keep or circulate.
Can you trust the architecture you have?
Architecture diagrams rarely show the reality of how systems actually operate. An independent review establishes what is really there.