University Course Database
In plain English
A student and course records database that prevents duplicate enrolments, rejects invalid scores, and computes weighted grade averages accurately.
A Python 3.12 and SQLite 3 explorer testing relational constraints, credit-weighted grade aggregations, and query-plan index usage.
1. Why I built it
Built to regression-test composite B-tree covering index selection and exact weighted SQL aggregations inside automated tests.
2. How it works
Implemented `SCHEMA_SQL` and context-managed `QueryExplorer` in `src/sql_explorer/database.py` with tests in `tests/test_database.py`.
Unweighted `AVG(mark)` treats 10-credit seminars identically to 40-credit projects, while SQL integer division truncates decimals unless explicitly promoted to float (`* 1.0`).
University SQL Explorer defines `departments`, `students`, `modules`, and `enrolments` with composite primary keys (`student_id, module_code, academic_year`), credit and mark `CHECK` bounds, and cascading/restricted foreign keys. `QueryExplorer` guards `min_credits >= 0`, non-blank `module_code`, and `0 <= min_mark <= 100`.
Unit tests assert `EXPLAIN QUERY PLAN` output programmatically so range filters on `(module_code, mark)` remain covered by `idx_enrolments_module_mark`.
3. Output & Source Specimen
Schema & Query Planner Guarantees
| Object | Type | Constraint / Index Purpose |
|---|---|---|
| departments | Table | code TEXT PRIMARY KEY |
| modules | Table | CHECK(credits IN (10, 20, 40)), FK dept -> RESTRICT |
| enrolments | Table | PK(student_id, module_code, academic_year), CHECK(0..100) |
| idx_enrolments_module_mark | Covering Index | (module_code, mark) satisfies range scans without table reads |
Run checks: pytest -v tests/test_database.py4. Design Decisions & Challenges
Key Design Decisions
- 01.Set `(student_id, module_code, academic_year)` as the primary key on `enrolments` to forbid same-year duplicates while allowing retakes in later years.
- 02.Indexed `(module_code, mark)` via `idx_enrolments_module_mark` so mark threshold lookups resolve purely from the B-tree index.
- 03.Validated `min_credits`, blank `module_code`, and `min_mark` bounds cleanly before executing SQL.
Edge Cases Handled
- Ensuring modules with zero enrolments still surface in `module_statistics()` via `LEFT JOIN enrolments`.
5. Testing & Limits
Automated Checks
Verified with 7 pytest checks covering credit-weighted averages, UK degree-band mark distribution (`module_mark_distribution`), `--seed`/`--module` CLI controls, parameter bounds, `IntegrityError` rejections, and covering index plans.
Current Limits & Next Steps
- Stores one aggregate mark per enrolment rather than weighted sub-assessments.
6. Key Takeaways
What building University Course Database reinforced
Checking `EXPLAIN QUERY PLAN` inside unit tests catches accidental index regressions immediately after schema edits.
- — 7 pytest checks and Ruff lint pass cleanly.
- — Source hosted at github.com/16lv/university-sql-explorer.
Recent Activity
Latest repository updates
feat: gate custom DB seeding behind --seed flag and support --module selection
Kept custom SQLite files untouched unless --seed is requested and reported selected module mark distributions in JSON output.
feat: add UK degree-band mark distribution query and tests
Added QueryExplorer.module_mark_distribution(module_code) counting first-class through fail/unmarked bands in one SQL CASE pass.
Add python -m sql_explorer runner and verify editable install in CI
Committed directly to university-sql-explorer (main) · A student and course records database that prevents duplicate enrolments, rejects invalid scores, and computes weighted grade averages accurately.