Skip to content

Schema From Scratch

You'll design tables for something you actually track, with keys, constraints and a join that holds, then feed them real rows for two weeks and change your mind once.

About

Loading someone else's CSV gives you their shape. Designing your own means deciding what a row is, which is harder than it sounds and is the real skill in this badge. Pick something you already track or wish you did: workouts and the sets inside them, a coffee log with beans and brews, a club's members and dues, a reading list with books and sittings. You need at least two tables with a foreign key between them, constraints that refuse bad rows, and two weeks of real entries so the schema meets reality. Reality always wins, and you'll find a column you got wrong, so the last step is changing the schema without losing the data. What counts is a schema you drew before you typed it, constraints you watched reject a row, and fourteen days of rows.

At a glance

Per requirement
25 XP
Completion bonus
150 XP
Requirements
6
Takes
A weekend, then two weeks of use
Skill level
Beginner (done it a few times)
Cost
Free
Supplies
Nothing special
Where
At home
Other people
On your own
Locked for now
Earn two more Common badges in SQL & Databases to open this one. Log in to see how far along you are.

Requirements

  1. 1
    Draw the tables on paper first, with boxes, columns, lines for the keys, and one sentence per table saying what one row is. 'One row is one workout' and 'one row is one set of one exercise within one workout' are two tables, not one. If the sentence for a table needs the word 'and' in it, that's usually two tables. Take a photo of the drawing with the sentences on it. Photo
  2. 2
    Create the tables with primary keys, NOT NULL where it matters, a UNIQUE constraint, a CHECK and a foreign key. In SQLite run PRAGMA foreign_keys = ON first, every time you open the file, or the foreign key does nothing at all, which surprises everyone. A CHECK like CHECK (reps > 0) and a UNIQUE on a pair like (workout_id, exercise_id, set_number) are the lines that save you from your own typos. Take a screenshot of the CREATE statements. Photo
  3. 3
    Try to insert three bad rows and show the database refusing each one. A reps of -5, a workout_id that doesn't exist, and a duplicate of a UNIQUE pair. The error message is the point. If the foreign key one goes in without complaint, the pragma is off. Take a screenshot of the three errors. Photo
  4. 4
    Enter real rows for fourteen days and write the join that gives you a per-day summary. Entry can be DB Browser's edit grid, a .sql file you append to, or a tiny form; typing is fine. The join is something like SELECT w.date, COUNT(s.id), SUM(s.reps * s.weight) FROM workouts w JOIN sets s ON s.workout_id = w.id GROUP BY w.date. Take a screenshot of the summary with 14 or more days in it. Photo
  5. 5
    Change the schema once, without losing a row, because reality disagreed with your drawing. Add a column with ALTER TABLE ADD COLUMN and backfill it with an UPDATE, or split a column out into a new table with INSERT INTO ... SELECT. Do it inside a transaction, with a COUNT(*) before and after that matches, and write down what the drawing got wrong. Take a screenshot of the statements and the matching counts. Photo
  6. 6
    Redraw the schema as it is now and post it next to the first drawing. Two weeks in, the difference between the two drawings is what you learned. Take a photo of them side by side. Photo