Expand description
Foreign keys, as the triggers they are.
Invariant: a foreign key is enforced by exactly the machinery a written
trigger is enforced by. The clause is turned into CREATE TRIGGER text,
parsed by the same parser, bound by the same binder and inlined by the same
compiler - so ON DELETE CASCADE and the DELETE somebody wrote by hand
cannot disagree about what a conflict clause does, what OLD means, or what
order things happen in. SQLite makes the same choice for the same reason.
Generating text rather than building bound structures is deliberate. The text is printable, so a diagnostic can show what a constraint actually does, and it is the same shape a person would have written - which means every test that covers written triggers covers this too.
Four kinds of trigger come out of one clause:
- the child’s check, on
INSERTand onUPDATE OFits own key columns, which refuses a row whose parent is not there; - the parent’s check, on
DELETEand onUPDATE OFits key, which refuses to strand a child - this isNO ACTIONandRESTRICT; - the parent’s
CASCADE, which deletes or updates the children with it; - the parent’s
SET NULLandSET DEFAULT, which keep the children and let go of the key.
Reference: https://sqlite.org/foreignkeys.html.
Enums§
- Foreign
KeyEvent - Which write a synthesised trigger is generated for.
Constants§
- VIOLATION_
MESSAGE - The message SQLite reports for every foreign-key violation.
Functions§
- parent_
columns - The parent columns a key refers to.
- parent_
key_ is_ unique - Reports whether a key’s parent columns are a key of the parent.
- plan_
schema - Builds the triggers every table’s writes fire because of a foreign key.
- sweep_
statement - Returns the statement that repairs one cyclic key, or
Nonewhen the key has nothing to repair. - trigger_
for - Builds the trigger that enforces one key for one event, if there is one.
- violation_
query - Returns the
SELECTthat finds every row of a child table whose key has no parent, which is whatPRAGMA foreign_key_checkreports and what a deferred constraint is tested with at commit.