FkIfZero
This opcode tests if a foreign key constraint-counter is currently zero.
Jumps: P2 is an instruction address. In the listings here, a jump is drawn as an arc and the address is a link.
An example
6 of the 178 statements we compiled against the sample database use FkIfZero. This is the one with the shortest program; the rows that use FkIfZero are highlighted.
INSERT INTO artists (name) SELECT title FROM albums
| addr | opcode | p1 | p2 | p3 | p4 | p5 |
|---|---|---|---|---|---|---|
| 0 | Init | 0 | 37 | 0 | 0 | |
| 1 | InitCoroutine | 5 | 8 | 2 | 0 | |
| 2 | OpenRead | 0 | 3 | 0 | 3 | 0 |
| 3 | Rewind | 0 | 7 | 0 | 0 | |
| 4 | Column | 0 | 2 | 6 | 0 | |
| 5 | Yield | 5 | 0 | 0 | 0 | |
| 6 | Next | 0 | 4 | 0 | 1 | |
| 7 | EndCoroutine | 5 | 0 | 0 | 0 | |
| 8 | OpenWrite | 1 | 2 | 0 | 3 | 0 |
| 9 | OpenWrite | 2 | 7 | 0 | k(2,,) | 0 |
| 10 | OpenWrite | 3 | 6 | 0 | k(2,,) | 0 |
| 11 | Yield | 5 | 36 | 0 | 0 | |
| 12 | SoftNull | 2 | 0 | 0 | 0 | |
| 13 | SCopy | 6 | 3 | 0 | 0 | |
| 14 | NewRowid | 1 | 1 | 0 | 0 | |
| 15 | HaltIfNull | 1299 | 2 | 3 | artists.name | 1 |
| 16 | Affinity | 2 | 3 | 0 | DBB | 0 |
| 17 | SCopy | 3 | 14 | 0 | 0 | |
| 18 | PureFunc | 0 | 14 | 8 | lower(1) | 32 |
| 19 | IntCopy | 1 | 9 | 0 | 0 | |
| 20 | MakeRecord | 8 | 2 | 7 | 0 | |
| 21 | SCopy | 3 | 11 | 0 | 0 | |
| 22 | IntCopy | 1 | 12 | 0 | 0 | |
| 23 | MakeRecord | 11 | 2 | 10 | 0 | |
| 24 | MakeRecord | 2 | 3 | 13 | 0 | |
| 25 | FkIfZero | 0 | 32 | 0 | 0 | |
| 26 | OpenRead | 4 | 3 | 0 | 2 | 0 |
| 27 | Rewind | 4 | 32 | 0 | 0 | |
| 28 | Column | 4 | 1 | 14 | 0 | |
| 29 | Ne | 1 | 31 | 14 | BINARY-8 | 83 |
| 30 | FkCounter | 0 | -1 | 0 | 0 | |
| 31 | Next | 4 | 28 | 0 | 1 | |
| 32 | IdxInsert | 2 | 7 | 8 | 2 | 16 |
| 33 | IdxInsert | 3 | 10 | 11 | 2 | 16 |
| 34 | Insert | 1 | 13 | 1 | artists | 57 |
| 35 | Goto | 0 | 11 | 0 | 0 | |
| 36 | Halt | 0 | 0 | 0 | 0 | |
| 37 | Transaction | 0 | 1 | 1 | 0 | 1 |
| 38 | Null | 0 | 4 | 0 | 0 | |
| 39 | Goto | 0 | 1 | 0 | 0 |
Open it in the EXPLAIN visualizer →
Also in this program: Affinity, Column, EndCoroutine, FkCounter, Goto, Halt, HaltIfNull, IdxInsert, Init, InitCoroutine, Insert, IntCopy, MakeRecord, Ne, NewRowid, Next, Null, OpenRead, OpenWrite, PureFunc, Rewind, SCopy, SoftNull, Transaction, Yield.
What the SQLite documentation says
This opcode tests if a foreign key constraint-counter is currently zero. If so, jump to instruction P2. Otherwise, fall through to the next instruction.
If P1 is non-zero, then the jump is taken if the database constraint-counter is zero (the one that counts deferred constraint violations). If P1 is zero, the jump is taken if the statement constraint-counter is zero (immediate foreign key constraint violations).
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.