Lawfare Academy

Data Management and SQL for Business · Chapter 2 of 6

Keys, constraints and what relational integrity cannot prove

Return to the linked course unit →

DEVELOPED COURSE TEXT

Keys, constraints and what relational integrity cannot prove

Identity and reference are separate rules

A primary key identifies a row within its table; a foreign key constrains a reference to another table under the database’s rules. Names are often unsuitable identities because they can change or collide. A valid reference does not prove that the referenced customer is the correct real-world person. Database integrity and business truth overlap but are not identical. Reconciliation and authorized correction processes remain necessary.

Enable and verify the enforcement you rely on

SQLite foreign-key enforcement must be enabled for the connection when required, and its active setting should be checked. Do not assume that writing REFERENCES into a schema guarantees that every application connection enforces it. NOT NULL, CHECK and UNIQUE also address different problems. A positive quantity check does not prove that the delivered amount matches the record; a permitted status does not prove that cash was collected. Cross-row financial conditions need suitable controls beyond a simple row constraint.

Parameter binding preserves the value/query boundary

User-provided values should be bound through placeholders rather than interpolated into SQL text. This addresses a particular injection route; it does not grant authorization or validate the business meaning of the request. Parameters represent values, not arbitrary column names or SQL clauses. If an application permits selecting a sort column, choose from an explicit allowed set rather than concatenate unrestricted text. This educational fixture has no account system and should not be mistaken for a production access-control design.

rows = conn.execute(
    'SELECT customer_id, name FROM customers WHERE region = ? ORDER BY customer_id',
    ('North',)
).fetchall()
assert rows == [(1, 'Alpha'), (3, 'Gamma')]

Worked records

Record 1

A new order references customer 999, which does not exist. A second attempted insert uses an existing order primary key. A query is asked to look up a region whose literal text contains SQL-looking characters.

Develop the reasoning

With the fixture’s enforcement enabled, the nonexistent customer reference fails a foreign-key constraint, and the duplicate order identifier fails uniqueness. These protect particular database relationships; they do not verify every business fact. A parameter-bound region lookup treats the supplied string as a value, not executable SQL, so a value such as North followed by SQL-looking text does not turn into an all-region condition. Appropriate permissions, logging and validation are separate controls. The reproducibility script checks the constraint failures and confirms that the existing rows remain intact.

Progressive practice — optional

1. Foundation

Does an accepted foreign key prove the order belongs to the correct customer?

Compare the worked reasoning

No. It proves a permitted reference exists, not that the real-world association is accurate.

Change the record

A user selected the wrong valid customer ID.

Compare the changed-record reasoning

The constraint cannot detect that semantic mistake by itself; authorized correction and audit information are needed.

2. Application

Does a permitted paid status prove that payment rows sum to the order amount?

Compare the worked reasoning

No. Status validity and financial reconciliation are separate checks.

Change the record

A payment is missing.

Compare the changed-record reasoning

A query or transaction-level business control must identify the mismatch rather than assume the status guarantees it.

3. Transfer

Can a placeholder safely stand for any requested SQL column name?

Compare the worked reasoning

Not as a general identifier-substitution mechanism; bound parameters are values.

Change the record

The user supplies a sort-field choice.

Compare the changed-record reasoning

Map the choice to a reviewed identifier from an allowed set and keep ordinary values parameter-bound.

Sources and scope