Setting up and cleaning test data with SQL (Structured Query Language)
Tests need known data to start from and a clean state afterwards. Learn to insert, update and remove test data safely.
L1–L4 are part of the Pro pass
L0 Explain simply
Analogy: setting a stage. Before a scene, the crew sets the stage the same way each time. After the scene they put the props back. If they forget, the next scene starts with the wrong props.
Daily List is a to-do list app. Tom, the tester, sets the rows before a test and clears them after.
What it is: is a small known set of rows loaded before a test. are the three statements that set those rows, change them, and remove them. Rollback and cleanup (SQL) are the two ways to put the database back. A rollback undoes a transaction. A cleanup deletes rows that carry a marker. SQL (Structured Query Language) is the language these statements are written in. A table is a grid, a row is one item, and a column is one fact about that item. DELETE is easy to mix up with TRUNCATE and DROP. DELETE removes chosen rows. DROP removes the table. TRUNCATE is a different statement, and this SQLite does not have it.
Why it matters: A test that leaves ZZ- rows behind will fail the next run for a reason that is not the bug. A cleanup with no WHERE can delete Buy milk as well as the test rows.
(SYNTHETIC / TRAINING ONLY: Daily List and every person named in this lesson are invented.)
PLAIN WORDS · 2 in this section
- rollback
- undoing a change to return to the earlier state
- sqlite
- a small free database tool
PICTURE IT
The picture is the one the lesson above opens with. The table below shows how each part of it matches the software.
THE REAL JOB Setting the same stage before each scene and clearing it after.
- Seed
- Test
- Clean
HOW THE PICTURE MAPS
| In the picture | In the software |
|---|---|
| The crew | The seed script |
| The props | Seed rows |
| Putting back | Cleanup |
| An undo | Rollback |
WHERE THIS PICTURE STOPS
- A stage is physical, but data can be shared.
- A crew can see, but a script runs blind.
- Props are few, but tables can be large.
HOOK
Set, test, clear.
TELL A FRIEND
A stage crew: set it, play, put it back.
OFTEN MIXED UP WITH Test data and masking. That topic is about where comes from. This one loads and removes it with (Structured Query Language).
PRACTISE AND KEEP
Sources for this topic — 5 sources
- Market practice
SQL interview questions: DELETE, TRUNCATE, DROP (SQL Academy) ↗ — SQL Academy · SQL Academy interview page: DELETE, TRUNCATE and DROP differ in what they remove and whether they can be undone (search excerpt, page not opened)
Found through a web search; the page was not opened (search excerpt, page not opened). Licence terms were not read, so put it in your own words and do not quote it. Open the page and confirm what it says before relying on any detail.
- Market practice
How to write unit tests for MySQL stored procedures (OneUptime) ↗ — OneUptime · OneUptime article: wrap tests that change data in a transaction and roll back (search excerpt, page not opened)
Found through a web search; the page was not opened (search excerpt, page not opened). Licence terms were not read, so put it in your own words and do not quote it. Open the page and confirm what it says before relying on any detail.
- Market practice
Statements That Cause an Implicit Commit (MySQL 8.0 Reference Manual) ↗ — Oracle / MySQL · MySQL reference manual: some statements, such as ones that change the table structure, cause an implicit commit (search excerpt, page not opened)
Found through a web search; the page was not opened (search excerpt, page not opened). Licence terms were not read, so put it in your own words and do not quote it. Open the page and confirm what it says before relying on any detail.
- Implementation-specific
PostgreSQL documentation ↗ — The PostgreSQL Global Development Group · PostgreSQL 18 official manual, opened 2026-11-06: tutorial-transactions and MySQL8.4 implicit-commit. Supports only PostgreSQL behavior; earlier execution evidence remains separately versioned.
An implementation, not the standard — which is the point. ISO/IEC 9075-2 is paywalled, and this documents the same constructs in text anybody can open. Its isolation-level chapter attributes to the standard explicitly, so it can carry a standard-level claim as long as the citation says which document it is quoting.
Cited as market practice rather than an official standard by kind: an engine's manual is authoritative about that engine and is not the obligation. Where a question turns on behaviour PostgreSQL chose rather than behaviour SQL requires, the answer has to say so.
One correction carried here because it circulates widely: the sentence "The fundamental difference between WHERE and HAVING…", quoted all over the internet as PostgreSQL documentation, is not in the current documentation. It was searched for in the source tree and is not there.
Read through a mirror, because the publisher's own domain could not be fetched (a reachability gap, not a statement about the publisher). A 403 there is a policy refusal and says nothing about whether the document exists or is reachable elsewhere.
Anyone on an unrestricted network should open the canonical URL above and confirm the section and page numbers cited against it. Every claim drawn from this entry is written so that it can be checked against a numbered page.
Read from the
postgres/postgresdocumentation source tree (doc/src/sgml), which the project itself maintains and which carries the section ids the published URLs are derived from — so a cited section id can be checked against the rendered page. - Implementation-specific
SQLite official documentation — constraints, types and transactions ↗ — SQLite · SQLite rolling official documentation, opened 2026-11-06: foreignkeys.html, datatype3.html, lang_createtable.html, lang_transaction.html, isolation.html, cli.html, series.html and lang_createtrigger.html as relevant. Historical SQLite 3.40.1 results remain distinct from in-memory audit witnesses on 3.53.1.
Relevant primary pages opened 6 November2026; rolling documentation and CLI examples (3.54.0) are distinct from historical lesson runs3.40.1 and audit witnesses3.53.1. No file/connected database audit execution.
Deepest material on this page: L4 — Standards & sources. Where a topic stops short of implementation depth, that is a deliberate coverage decision, not an oversight — see coverage.