FkCheck
Halt with an SQLITE_CONSTRAINT error if there are any unresolved foreign key constraint violations.
An example
1 of the 178 statements we compiled against the sample database use FkCheck. This is the one with the shortest program; the rows that use FkCheck are highlighted.
INSERT INTO artists VALUES (NULL, 'New', 'US') RETURNING id
| addr | opcode | p1 | p2 | p3 | p4 | p5 |
|---|---|---|---|---|---|---|
| 0 | Init | 0 | 40 | 0 | 0 | |
| 1 | OpenWrite | 0 | 2 | 0 | 3 | 0 |
| 2 | OpenWrite | 1 | 7 | 0 | k(2,,) | 0 |
| 3 | OpenWrite | 2 | 6 | 0 | k(2,,) | 0 |
| 4 | SoftNull | 2 | 0 | 0 | 0 | |
| 5 | String8 | 0 | 3 | 0 | New | 0 |
| 6 | String8 | 0 | 4 | 0 | US | 0 |
| 7 | NewRowid | 0 | 1 | 0 | 0 | |
| 8 | HaltIfNull | 1299 | 2 | 3 | artists.name | 1 |
| 9 | NotExists | 0 | 11 | 1 | 0 | |
| 10 | Halt | 1555 | 2 | 0 | artists.id | 2 |
| 11 | Affinity | 2 | 3 | 0 | DBB | 0 |
| 12 | SCopy | 3 | 12 | 0 | 0 | |
| 13 | PureFunc | 0 | 12 | 6 | lower(1) | 32 |
| 14 | IntCopy | 1 | 7 | 0 | 0 | |
| 15 | MakeRecord | 6 | 2 | 5 | 0 | |
| 16 | SCopy | 3 | 9 | 0 | 0 | |
| 17 | IntCopy | 1 | 10 | 0 | 0 | |
| 18 | MakeRecord | 9 | 2 | 8 | 0 | |
| 19 | MakeRecord | 2 | 3 | 11 | 0 | |
| 20 | FkIfZero | 0 | 27 | 0 | 0 | |
| 21 | OpenRead | 3 | 3 | 0 | 2 | 0 |
| 22 | Rewind | 3 | 27 | 0 | 0 | |
| 23 | Column | 3 | 1 | 12 | 0 | |
| 24 | Ne | 1 | 26 | 12 | BINARY-8 | 83 |
| 25 | FkCounter | 0 | -1 | 0 | 0 | |
| 26 | Next | 3 | 23 | 0 | 1 | |
| 27 | IdxInsert | 1 | 5 | 6 | 2 | 16 |
| 28 | IdxInsert | 2 | 8 | 9 | 2 | 16 |
| 29 | Insert | 0 | 11 | 1 | artists | 57 |
| 30 | Copy | 1 | 14 | 0 | 0 | |
| 31 | MakeRecord | 14 | 1 | 15 | 0 | |
| 32 | NewRowid | 5 | 16 | 0 | 0 | |
| 33 | Insert | 5 | 15 | 16 | 0 | |
| 34 | FkCheck | 0 | 0 | 0 | 0 | |
| 35 | Rewind | 5 | 39 | 0 | 0 | |
| 36 | Column | 5 | 0 | 14 | 0 | |
| 37 | ResultRow | 14 | 1 | 0 | 0 | |
| 38 | Next | 5 | 36 | 0 | 0 | |
| 39 | Halt | 0 | 0 | 0 | 0 | |
| 40 | Transaction | 0 | 1 | 1 | 0 | 1 |
| 41 | OpenEphemeral | 5 | 1 | 0 | 0 | |
| 42 | Goto | 0 | 1 | 0 | 0 |
Open it in the EXPLAIN visualizer →
Also in this program: Affinity, Column, Copy, FkCounter, FkIfZero, Goto, Halt, HaltIfNull, IdxInsert, Init, Insert, IntCopy, MakeRecord, Ne, NewRowid, Next, NotExists, OpenEphemeral, OpenRead, OpenWrite, PureFunc, ResultRow, Rewind, SCopy, SoftNull, String8, Transaction.
What the SQLite documentation says
Halt with an SQLITE_CONSTRAINT error if there are any unresolved foreign key constraint violations. If there are no foreign key constraint violations, this is a no-op.
FK constraint violations are also checked when the prepared statement exits. This opcode is used to raise foreign key constraint errors prior to returning results such as a row change count or the result of a RETURNING clause.
From sqlite.org's list of opcodes for SQLite 3.54.1, in the public domain. Opcodes are not a stable interface and change between releases.
← FinishSeek · FkCounter →