Database constraints: rules the data must obey
A constraint is a rule the database enforces, such as not empty, unique or linked. Learn to test the rule at the database as well as in the app.
L1–L4 are part of the Pro pass
L0 Explain simply
Analogy: a locked gate with rules. The gate lets a person in only with a ticket, a name, and a valid date. If the front desk forgets , the gate still checks.
Daily List is a to-do list app. Tom, the tester, tests the gate in the database as well as the checks on the screen.
What it is: A is a rule the database enforces on its own. A are two of those rules. The primary key names one row. The foreign key says a task's list must be a list that exists. A check constraint limits a column to allowed values. SQL (Structured Query Language) is the language used to create these rules and to try to break them. A table is a grid, a row is one item, and a column is one fact about that item. A constraint is easy to mix up with a check that lives only in the app. The app check can be skipped by an import. The database rule cannot.
Why it matters: A screen that says Saved can still have stored a bad row, if the database rule was never created. Tom tries the bad row in . The error, or the stored row, is the finding.
(SYNTHETIC / TRAINING ONLY: Daily List and every person named in this lesson are invented.)
PLAIN WORDS · 2 in this section
- constraint
- a limit or rule that restricts what can be done
- enforces
- makes sure a rule is actually followed
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 Testing the rules at the last gate, not only at the front desk.
- Desk
- Gate
- Both
HOW THE PICTURE MAPS
| In the picture | In the software |
|---|---|
| The gate | The database |
| The ticket check | A not-null rule |
| Names on a list | Unique |
| A valid date | A check rule |
WHERE THIS PICTURE STOPS
- A gate stops people, but a database stops rows.
- A desk can be skipped, but not the gate.
- A gate is physical, but rules can be added later.
HOOK
Desk, gate, both.
TELL A FRIEND
A gate inside the data: it checks even if the desk forgets.
OFTEN MIXED UP WITH Databases for testers. That topic explains what a database is. This one tests the rules inside it.
PRACTISE AND KEEP
Sources for this topic — 4 sources
- Market practice
PostgreSQL documentation: 5.5 Constraints ↗ — PostgreSQL Global Development Group · PostgreSQL documentation: constraints include check, not-null, unique, primary key and foreign key constraints, and by default two null values are not considered equal in a unique constraint (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
PostgreSQL foreign key (Devart blog) ↗ — Devart · Devart blog: a foreign key links a column to a key in another table, so a row cannot point to something that does not exist (search excerpt, page not opened)
Found through a web search; the page was not opened (search excerpt, page not opened). Version: Web page, no edition number. 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: ddl-constraints §5.5 plus SQLite foreignkeys and CREATE TABLE. 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.