SQL (SQLite engine)

Test referential integrity on delete

Hard120 pts~45 min
  • Referential integrity
  • ON DELETE behaviour
Practice app · Acme Commerce DB

A real SQL database (SQLite engine) seeded with an e-commerce and HR dataset, with schema browser and query runner.

Test URL
/lab/database-testing-test-referential-integrity-on-delete

Your starter code already declares TEST_URL — never hardcode a host.

Objective

Verify a department with employees cannot be deleted, an empty department can, and no employee is left pointing at a missing department.

Your task

  1. 1Try DELETE FROM departments WHERE name = 'QA'; employees reference it, so it must fail.
  2. 2Delete the empty 'Support' department; it must succeed.
  3. 3Verify: SELECT COUNT(*) AS orphaned_employees FROM employees e LEFT JOIN departments d ON d.id = e.department_id WHERE e.department_id IS NOT NULL AND d.id IS NULL.

Acceptance criteria

  • Deleting QA fails with FOREIGN KEY constraint failed
  • Only Support was removed from departments
  • The last result set shows 0 orphaned employees

Constraints

  • Every run starts from a freshly seeded database, so your script must include its own setup statements.
  • A failing statement is recorded and the script keeps running, so you can trigger an error and verify afterwards.
  • Only the last result set of your script is compared, so finish with the verification query.

SQL (SQLite engine) · Database Testing · Constraints & keys