← Back to projects

movie-review-db — a relational model with reproducible query results

A SQLite schema for a movie-rating service with constraints that reject bad data, 17 queries whose outputs are regenerated by a script and checked in CI, and an index benchmark that records a case where the index made a query slower.

Verification

python -m unittest discover -s tests -t .
15 tests OK · Python 3.12, Windows 10 · checked on

python scripts/run_all.py --check
33 result files match fresh SQL output · Python 3.12, Windows 10 · checked on

Overview

This coursework project models a small movie-rating service — members, movies, genres and reviews — and answers a fixed list of questions in SQL. Beyond the required queries, I made the results reproducible: a script rebuilds the database and rewrites every result file, and CI fails if a committed result no longer matches what the SQL produces.

Context and problem

The assignment asked for a schema, sample data, and 15 queries across basic selection, joins, aggregation, subqueries, updates and an index. The usual failure mode for this kind of work is result files pasted by hand that silently drift from the SQL. I wanted the repository itself to prove that each result came from the query next to it.

Architecture

sql/01_schema.sql ─▶ sql/02_seed.sql ─▶ sql/03_queries.sql (Q01–Q17)
        │                                   │
        └──── scripts/run_all.py ───────────┴─▶ results/*.txt
                     └── --check: run in a temp DB, compare with committed results (CI)

Four tables: genre, member, movie, review. review connects members and movies (an N:M relationship through two 1:N relations). Seed data: 10 genres, 15 members, 22 movies, 78 reviews, loaded in one transaction, parents before children.

Implementation and trade-offs

Constraints instead of trust. Primary keys, three foreign keys, NOT NULL, UNIQUE and CHECK constraints carry the rules: a rating is 1–5, a member status is one of three values, and UNIQUE(member_id, movie_id) allows one review per member per movie.

Delete policies chosen per relation. Deleting a genre that movies still use is rejected (RESTRICT); deleting a member or a movie removes their reviews (CASCADE). Q16 demonstrates the cascade.

Real dates in a database without a DATE type. SQLite stores dates as text, so a typo like 2024-02-30 would be accepted. Each date column has CHECK (date(x, '+0 days') IS x), which only passes for valid ISO-8601 dates.

Foreign keys are off by default in SQLite. Every SQL file starts with PRAGMA foreign_keys = ON;, and integrity scenarios (B07–B14) show each constraint rejecting bad input.

Query coverage. Joins (inner and left, including members with no reviews), HAVING for a top-5 with a minimum review count, a monthly trend with strftime, and a CTE with a correlated subquery for “the top-rated movie in each genre”. Two queries beyond the required fifteen are labeled as extras, and a test checks that the required set matches the requested categories exactly.

My contribution

Everything in the repository: schema, seed data, queries, the result script, tests, the benchmark script, CI and documentation.

Validation and results

  • Tests. python -m unittest discover -s tests -t . → 15 tests OK (re-run 2026-11-25, Python 3.12, Windows).
  • Result reproducibility. python scripts/run_all.py --check → 33 result files match a fresh run. Three files containing query plans are excluded because plan text varies across SQLite versions.
  • CI. GitHub Actions runs the tests and the result check on Ubuntu and Windows with Python 3.10, 3.13 and 3.14.
  • Index benchmark (scripts/bench_index.py, author’s environment: Python 3.14.4, SQLite 3.46.1). With 200,078 reviews:
Query Without index With idx_review_movie_id
Reviews for one movie (WHERE movie_id = ?) 9.93 ms 0.58 ms (17× faster)
Average per movie (JOIN + GROUP BY) 113.72 ms 231.93 ms (about 2× slower)

The second row is the most useful result in the project. With the index available, the planner switched to scanning movie and probing review through the index once per movie, which was slower than one sequential scan of review plus a temporary B-tree for grouping. An index helps selective lookups; it is not a free speed-up for every query that touches the column. The timings depend on the machine; the query plans are recorded next to them.

Reproduction

Only Python 3.10+ is needed (the sqlite3 module is in the standard library).

git clone https://github.com/maebsy/movie-review-db.git
cd movie-review-db
python scripts/run_all.py          # builds movie_review.db and rewrites results/
python scripts/run_all.py --check  # compares without writing

Limitations and next steps

  • The dataset is small and synthetic; it demonstrates modeling and SQL, not data volume.
  • It is a single-file SQLite database with no ingestion step. A natural next step is loading an external dataset, adding data-quality checks at load time, and comparing the same queries on a columnar engine.