State the business contract before choosing a constraint
A key identifies a row within a defined relation. A foreign key can enforce that a referenced identifier exists, but it does not establish that the relationship is economically correct, current or authorized. A valid customer identifier on an order can still identify the wrong customer. A required-value rule answers a different question from a uniqueness rule. Document entity identity, nullable relationships, effective dates and the intended unit of analysis before writing a query.
This chapter uses SQLite and the runnable laboratory uses only synthetic databases. In SQLite, enable and verify foreign-key enforcement for each relevant connection before beginning the transaction. Do not infer enforcement merely from a REFERENCES clause in a schema listing. The official documentation explains that changing the foreign-key setting inside an active transaction does not have the intended enabling effect. These examples explain SQL contracts; they do not migrate Academy data or substitute for the site’s actual storage system.
For a deliberately assigned text order identifier, use a declared non-null key such as order_id TEXT PRIMARY KEY NOT NULL. SQLite has historical exceptions to assuming that every ordinary PRIMARY KEY declaration implies NOT NULL; INTEGER PRIMARY KEY also has special rowid behavior. A meaningful schema review checks the actual engine and declaration rather than transplanting another database’s assumptions.
NULL is missing information, not a customer numbered zero
The fixture contains customers 1, 2 and 3. Orders O1 and O2 reference customers 1 and 2; draft order O3 has a NULL customer_id because assignment is still unresolved. The draft status and nullable relationship are explicit policy choices. Substituting customer zero would invent an entity unless such a real business record and meaning actually exist. Conversely, if every saved order must have a known customer, enforce NOT NULL rather than merely hoping the application supplies it.
Consider this anti-join:
SELECT c.customer_id
FROM customers AS c
WHERE c.customer_id NOT IN
(SELECT o.customer_id FROM orders AS o);
The subquery contains 1, 2 and NULL. In this record it returns no customers: known matches fail the NOT IN condition, while customer 3’s comparison with an unknown value cannot establish the needed true result. A correlated NOT EXISTS expresses the intended question without that particular NULL trap:
SELECT c.customer_id
FROM customers AS c
WHERE NOT EXISTS (
SELECT 1 FROM orders AS o
WHERE o.customer_id = c.customer_id
);
That query returns customer 3. It does not prove customer 3 is inactive in every system or time period; it answers the question about the orders relation and filters actually supplied.
Count the entity whose existence is at issue
A left join retains a customer with no matching order by producing an unmatched extended row. COUNT(*) counts that retained row; COUNT(o.order_id) counts non-null matched order identifiers. Grouping the fixture by customer therefore gives customer 3 a row count of one but an order count of zero. Neither result is an arithmetic error. They count different things. State whether the dashboard means customers, orders, line items, responses or joined rows.
SQLite permits multiple NULL values under an ordinary UNIQUE email constraint, while repeated equal non-null email strings are rejected under the applicable comparison rules. That can be appropriate for optional contact information, but it does not establish that an email address is a reliable person identifier. Empty text, unknown text, normalized text and a verified address are distinct states. Specify collation and normalization rather than silently merging records because their displayed strings look similar.
Build tests around counterexamples: an orphan reference, a missing required key, two unknown optional addresses, an unresolved draft order, a customer with no orders, and a customer with several tags. For each, identify the constraint or query that should accept, reject or retain the record. A passing query is one that preserves the intended population and exposes unresolved information, not one that merely returns a plausible total. Use the complete current on-site schema and query studios before extending a model; official links are optional provenance.
Worked example: An anti-join must match the intended population
For customers 1,2,3 and order references 1,2,NULL, the stated NOT IN query returns no rows while correlated NOT EXISTS returns 3. The difference is caused by the predicate’s treatment of unknown information, not by a change in stored customers.
Worked example: A customer with no orders still has a joined row
After a left join grouped by customer, customer 3 has COUNT(*) equal to one and COUNT(o.order_id) equal to zero. Replacing one with the other changes the business measure. A filter on the joined order in WHERE can also remove the unmatched customer entirely.