From an App Idea to Your First Tables: Planning a Small Schema

Most SQL tutorials hand you a table that already exists and ask you to query it. Real projects start the other way round: you have an idea for an app and an empty database, and you have to decide what tables to create. That planning step, usually called designing a schema, is where many beginners freeze. This lesson walks through it with one small example, a personal recipe box, and explains each decision in plain words. By the end you will have a method you can reuse for your own project.

Step 1: describe the app in sentences

Before thinking about tables, write a few sentences about what the app does, as if explaining it to a friend. For the recipe box:

These sentences are your requirements. Everything in the schema should trace back to one of them, and if you find yourself adding tables that no sentence needs, pause and ask why.

Step 2: find the nouns

Read the sentences and underline the nouns, the things the app keeps track of: recipe, ingredient, amount, tag. Nouns that stand on their own, that you might want to list, search or reuse, are candidates for tables. Nouns that only describe something else are candidates for columns.

Here, recipe, ingredient and tag stand on their own. Amount only makes sense as "the amount of an ingredient in a recipe", so it will be a column somewhere, not its own table. Name, instructions and time describe a recipe, so they become columns of the recipes table.

Step 3: give each table a primary key

Every table needs a way to identify each row uniquely. For beginners, a simple whole-number id that the database assigns automatically is usually the easiest choice. It never changes, even if you rename a recipe. The lessons on picking a good primary key and natural versus surrogate keys explain the alternatives.

CREATE TABLE recipes (
  id INTEGER PRIMARY KEY,
  name TEXT NOT NULL,
  instructions TEXT,
  minutes INTEGER
);

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

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

The UNIQUE on ingredient and tag names stops you accidentally creating "flour" twice. Our lesson on constraints as safety rails explains why small rules like this save trouble later.

Step 4: work out how the tables relate

Now ask, for each pair of tables, how many of one can belong to how many of the other. This is the part that feels hardest at first, but there are only three common answers.

RelationshipExample questionRecipe box answer
One-to-manyCan one A have many B, but each B belong to one A?Not needed in this simple version
Many-to-manyCan one A have many B and one B have many A?Recipes and ingredients; recipes and tags
One-to-oneDoes each A have exactly one B?Not needed here

A recipe has many ingredients, and an ingredient such as flour appears in many recipes. That is many-to-many. The same is true of recipes and tags. The diagrams of one-to-many and many-to-many show the shapes visually.

Step 5: use a linking table for many-to-many

A column can hold only one value, so you cannot list many ingredients inside a single recipe row without making a mess. Instead, create a small linking table where each row says "this recipe uses this ingredient". It is also the natural home for the amount, which describes the pairing rather than either side alone.

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

CREATE TABLE recipe_tags (
  recipe_id INTEGER NOT NULL REFERENCES recipes(id),
  tag_id INTEGER NOT NULL REFERENCES tags(id),
  PRIMARY KEY (recipe_id, tag_id)
);

The REFERENCES parts are foreign keys: they promise that every recipe_id points to a real recipe. The two-column primary key stops the same ingredient being added to the same recipe twice. In SQLite, foreign key checks need to be switched on with PRAGMA foreign_keys = ON; for each connection, which surprises many beginners. See how to add a foreign key between two tables.

Step 6: test the design with your sentences

Go back to the requirements and check each one can be answered.

SELECT r.name
FROM recipes r
JOIN recipe_ingredients ri ON ri.recipe_id = r.id
JOIN ingredients i ON i.id = ri.ingredient_id
WHERE i.name = 'flour';

If a sentence cannot be answered, the schema is missing something. If you have a table no sentence needs, ask whether you need it yet. The step-by-step JOIN lesson explains the query above.

Common beginner mistakes, and why this design avoids them

Your turn

Pick your own small idea, a reading list, a plant-watering log, a gig diary, and follow the same six steps: sentences, nouns, keys, relationships, linking tables, test. Write it down before writing any SQL. The simple data dictionary template gives you a place to record each table and column. Your first schema does not need to be perfect; it needs to be clear enough that you can change it when you learn more. For the wider route through the classroom, see the five-step learning path or return to the Learn hub.