Free

Retail Database Design (SQL)

Normalisation is not bureaucracy — it is the prevention of lies, and the error message is the feature.

  • SQL
  • MySQL

About this project

A properly normalised database for a small retail shop — customers, products, categories, orders, order lines and payments — with a seed dataset and a file of worked queries you run and read.

The idea it is built around is that normalisation prevents lies. The README starts with one wide spreadsheet-shaped table holding an order and shows the three things that go wrong with it: a customer whose phone number is stored in forty rows and now disagrees with itself, a product that cannot exist until somebody orders it, and deleting the last order of a product erasing the product. Then it takes the same data to 3NF and those three become impossible rather than merely discouraged.

The second theme is that the constraint is the guarantee. The queries file deliberately attempts thirteen bad writes and shows the exact error the database returns each time — because that error, not a comment in your application code, is what actually stops bad data. The demonstrations run inside transactions that roll back, with an assertion at the end proving the row counts are unchanged.

What you will learn

  • Take one wide spreadsheet-shaped table to 3NF, and see each anomaly become impossible
  • Use FOREIGN KEY, UNIQUE, CHECK and NOT NULL as guarantees the application cannot bypass
  • Read EXPLAIN, add an index, and read it again to see what changed
  • Store money as DECIMAL and know exactly what FLOAT would cost you
  • Understand why an order line records the price at the time of sale, not a join to today’s price

Before you start

  • Basic SQL — SELECT, INSERT, and what a table is
  • MySQL 8.0.18 or newer. Not MariaDB: CHECK constraints are only enforced from 8.0.16.

New to SQL? The free SQL course covers everything this project assumes.

Academic integrity notice

These projects are provided for learning, experimentation and reference. Understand the code, modify it for your own requirements, and acknowledge external work according to your institution’s academic policies. Priodemy does not supply work to be submitted as your own.