Data Librarian — Database

Organize a data library: 10 levels of normalization, indexes, constraints and transactions. A database design game for moving from arrays to tables.

What you learn in this game

Welcome, Data Librarian. Your job is to design the right database schema. Each level gives you a scenario, and you pick the correct columns for each table. Careful: some columns are distractors that don't belong.

A Foreign Key (FK) links two tables. In a one-to-many relationship, the FK always sits on the "many" side. Example: one author → many books, so penulis_id (author id) lives in the books table.

A many-to-many relation can't be represented directly in SQL. You need a junction table (a go-between) that stores a pair of FKs from both tables. A junction table can also hold extra data about the relation (a grade, say).

1NF (First Normal Form): every column must contain an atomic value, one value per cell. Storing "A, B, C" in one column breaks 1NF. The fix: a separate table with one row per item.

An index speeds up lookups, like the index of a book. Index the columns you use often in WHERE, ORDER BY, and JOIN. But don't index every column: each index slows down INSERT/UPDATE.

Composite Primary Key = a PK made of 2+ columns. Good for a junction table where the FK pair already guarantees uniqueness. Alternative: a surrogate id + UNIQUE(user_id, role_id), a bit more flexible but it costs an extra column.

What the levels ask

  1. Pick the right columns for the library's book table.
  2. Build two tables that link to each other.
  3. Decide which column makes a good Primary Key.
  4. Design the tables for an e-commerce system: categories and products.
  5. Set the right NOT NULL and UNIQUE constraints.
  6. A student can take many courses, and one course can be taken by many students.
  7. This table breaks 1NF. Pick the right columns to fix it.
  8. This query is slow. Pick the columns that need an index.
  9. Design 4 tables for a simple online shop.
  10. Design a blog database with tags (M:N).
  11. A junction table doesn't need an id of its own. Use a composite PK.
  12. The employee table has bosses who are employees too.

And more, 20 levels in all.