Lawfare Group → Knowledge Institute → Lawfare Academy

← Course syllabus · Related lesson

A valid key is not a complete definition of the reporting population

Current complete on-site edition 6

Required teaching and complete optional models are supplied on LawfareGroup.com. Publisher and primary links are optional provenance or further reading. Florida is the contemporary state-law default; federal rules and explicitly comparative accounting frameworks are distinguished. Independent subject review and observed human learner testing remain pending.

Cumulative data-system dossier, transfer models and 30-point rubric

Preserved original teaching integrated with complete current on-site preparation. Independent human subject review and observed learner testing remain pending.

Learning objectives

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.

Progressive optional practice

Attempt each problem before opening its model. Then revise the reasoning under the changed conditions.

Guided

Which constraint rejects an order referring to a nonexistent customer?

Compare the model reasoning

An actually enforced foreign key rejects the non-null orphan reference; enable and verify enforcement in the stated SQLite connection.

Changed conditions

The reference is NULL and the column permits it.

Compare the revised analysis

A foreign key alone does not express a required relationship. Use the appropriate NOT NULL rule if that is the business contract.

Independent

Explain why NOT EXISTS returns customer 3 in the fixture.

Compare the model reasoning

There is no order row with customer_id equal to 3. The unresolved draft’s NULL does not create such a match.

Changed conditions

An order is assigned to customer 3.

Compare the revised analysis

The same predicate now excludes 3; preserve the changed data and query scope in the explanation.

Transfer

Does unique email prove one row per real person?

Compare the model reasoning

No. Identity, optional missing values, shared addresses and comparison rules require separate business decisions.

Changed conditions

An email normalization change merges two existing accounts.

Compare the revised analysis

Review collisions, authority, preservation and rollback before applying it; a convenient normalized string does not authorize loss of record identity.

Preserved historical development snapshot · Course content review

Source readings and scope

SQLite: foreign-key support — Read enforcement, connection settings and nullable relationship behavior. Primary source checked October 27, 2026. The business records and numerical models are original synthetic exercises.

SQLite: CREATE TABLE constraints — Read PRIMARY KEY, UNIQUE, NOT NULL and constraint-enforcement qualifications. Primary source checked October 27, 2026. The business records and numerical models are original synthetic exercises.

SQLite: SELECT — Read join processing, grouping and aggregate expression behavior. Primary source checked October 27, 2026. The business records and numerical models are original synthetic exercises.

Reproduce the worked examples in Python 3. Standard library only; synthetic data; no production records or network access.