Long Scenario: Designing Tables for a Recipe App

This long scenario is invented for teaching. It follows Learner D through designing the database for a personal recipe app, with the decision points where a different choice would lead somewhere else. The aim is to show how normalisation decisions feel in practice.

Learning how databases work illustration

Version 1: one big table

Learner D starts with a single table, mirroring their old spreadsheet:

idrecipeingredientscook_minutes
1Pancakesflour, eggs, milk20
2Omeletteeggs, cheese10

It works for listing recipes. Then they want to answer "what can I cook with eggs?".

Decision point 1: how to search ingredients

Option A: search inside the text with WHERE ingredients LIKE '%egg%'.

Option B: give ingredients their own table.

Learner D tries Option A first. It finds "eggs", but it would also match "eggplant", and it misses "Egg" written with a capital letter in a recipe typed later. Searching inside text is fragile.

What if they stayed with Option A? For a tiny personal list, it might be acceptable. But every new question about ingredients would get harder.

They choose Option B.

Version 2: recipes and ingredients

CREATE TABLE recipes (id INTEGER PRIMARY KEY, name TEXT NOT NULL, cook_minutes INTEGER);
CREATE TABLE ingredients (id INTEGER PRIMARY KEY, name TEXT NOT NULL UNIQUE);

But how does a recipe know its ingredients?

Decision point 2: linking recipes and ingredients

Option A: add ingredient_id to recipes. That only allows one ingredient per recipe. Ruled out.

Option B: add recipe_id to ingredients. That only allows each ingredient in one recipe, but eggs appear in both. Ruled out.

Option C: a linking table between them, because this is a many-to-many relationship.

CREATE TABLE recipe_ingredients (
  recipe_id INTEGER REFERENCES recipes(id),
  ingredient_id INTEGER REFERENCES ingredients(id),
  quantity TEXT,
  PRIMARY KEY (recipe_id, ingredient_id)
);

The linking table also gives a natural home for the quantity, such as "2" or "200 g".

Version 3: answering questions

"What can I cook with eggs?" becomes:

SELECT recipes.name
FROM recipes
JOIN recipe_ingredients ON recipe_ingredients.recipe_id = recipes.id
JOIN ingredients ON ingredients.id = recipe_ingredients.ingredient_id
WHERE ingredients.name = 'eggs';

Exact, reliable and unaffected by "eggplant".

Decision point 3: units

Learner D now wants to scale recipes. "200 g" stored as text is hard to multiply.

Option A: keep text. Simple, but no maths.

Option B: split into amount (a number) and unit (text, such as g or ml).

They choose Option B for new recipes and leave a note to convert old ones later. A reasonable compromise for a personal project.

Decision point 4: categories

Each recipe can be "breakfast", "dinner" or both. Learner D recognises another many-to-many and adds categories and recipe_categories tables, following the same pattern as ingredients.

The final design

TableHolds
recipesOne row per recipe
ingredientsOne row per ingredient name
recipe_ingredientsWhich ingredient is in which recipe, with amount and unit
categoriesOne row per category name
recipe_categoriesWhich recipe is in which category

What the scenario teaches

For more on the reasoning behind each step, read Inside Normalisation and the recipe box example schema.

A free learning page for beginners. Practise on a test database, never on real customer data, and check the official documentation for your database version.

Illustrative example: the organisations, people, projects and results described on this page are hypothetical. They do not describe a real client or engagement, and no consulting services are offered here.

Related pages on this site