Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/sqlite/blob/master/test/fkey08.sql # sqlite [Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `sqlite / test / fkey08.sql` 399 lines ยท 11229 bytes Raw Blame History 1 #!sqlite3 2 # 3 # 2026-10-21 4 # 5 # The author disclaims copyright to this source code. In place of 6 # a legal notice, here is a blessing: 7 # 8 # May you do good and not evil. 9 # May you find forgiveness for yourself and forgive others. 10 # May you share freely, never taking more than you give. 11 # 12 #*********************************************************************** 13 # 14 # Test cases for comparisons between parent and child keys in foreign 15 # key processing. Derived from test cases 8.0 and following of 16 # test/fkey8.test. To run these tests: 17 # 18 # ./sqlite3 test/fkey08.sql 19 # 20 # 21 .open :memory: 22 .mode list 23 .if NOT capable('fkey') 24 .exit 25 .endif 26 27 #------------------------------------------------------------------------- 28 # Bug report: https://sqlite.org/bugs/forumpost/`89bb27c659` 29 # 30 .open :memory: 31 PRAGMA foreign_keys=ON; 32 CREATE TABLE parent(id INTEGER PRIMARY KEY, code TEXT UNIQUE); 33 CREATE TABLE child( 34 id INTEGER PRIMARY KEY, 35 k REFERENCES parent(code) ON DELETE CASCADE 36 ); 37 INSERT INTO parent VALUES(1, '1001'), (2, '1002'); 38 INSERT INTO child VALUES(10, 1001), (11, 1002); 39 40 .testcase 8.1 41 INSERT INTO child VALUES(12, 1003); 42 .check --glob "*: FOREIGN KEY constraint failed" 43 44 .testcase 8.2 45 DELETE FROM parent WHERE code = '1001'; 46 SELECT id, quote(k) FROM child; 47 .check 11|1002 48 49 .testcase 8.3 50 PRAGMA foreign_key_check; 51 .check '' 52 53 # Parent columns with numeric affinity, child columns with BLOB affinity. 54 # The child keys are text values that only match the parent keys once 55 # the parent column affinity has been applied. 56 # 57 CREATE TABLE p2(id INTEGER PRIMARY KEY, n INTEGER UNIQUE, r REAL UNIQUE); 58 CREATE TABLE c1(k REFERENCES p2(n) ON DELETE CASCADE); 59 CREATE TABLE c2(k REFERENCES p2(n) ON UPDATE CASCADE); 60 CREATE TABLE c3(k REFERENCES p2(r) ON DELETE SET NULL); 61 CREATE TABLE c4(k REFERENCES p2(id) ON DELETE CASCADE); 62 INSERT INTO p2 VALUES(1, 5, 1.5), (2, 6, 2.5), (3, 7, 3.5), (4, 8, 4.5); 63 INSERT INTO c1 VALUES('5'); 64 INSERT INTO c2 VALUES('6'); 65 INSERT INTO c3 VALUES('3.5'); 66 INSERT INTO c4 VALUES('4'); 67 68 .testcase 8.5 69 DELETE FROM p2 WHERE n=5; 70 SELECT count(*) FROM c1; 71 .check 0 72 73 .testcase 8.6 74 UPDATE p2 SET n=60 WHERE n=6; 75 SELECT quote(k) FROM c2; 76 .check 60 77 78 .testcase 8.7 79 DELETE FROM p2 WHERE n=7; 80 SELECT quote(k) FROM c3; 81 .check NULL 82 83 .testcase 8.8 84 DELETE FROM p2 WHERE n=8; 85 SELECT count(*) FROM c4; 86 .check 0 87 88 .testcase 8.9 89 PRAGMA foreign_key_check; 90 .check '' 91 92 #------------------------------------------------------------------------- 93 # More cases where (parent_key = child_key) does not naturally use the 94 # affinity of the parent key. Tests 9.1.* are without an index on the 95 # child key and 9.2.* are the same tests with an index on the child key. 96 # 97 .open :memory: 98 PRAGMA foreign_keys=ON; 99 100 # TEXT parent, INTEGER child. Child value 1001 matches '1001' but 101 # not '01001'. 102 CREATE TABLE p1(code TEXT PRIMARY KEY); 103 CREATE TABLE c1(k INTEGER REFERENCES p1 ON DELETE CASCADE); 104 INSERT INTO p1 VALUES('01001'), ('1001'); 105 INSERT INTO c1 VALUES(1001); 106 107 .testcase 9.1.2 108 DELETE FROM p1 WHERE code='01001'; 109 SELECT quote(k) FROM c1; 110 .check 1001 111 112 .testcase 9.1.3 113 DELETE FROM p1 WHERE code='1001'; 114 SELECT quote(k) FROM c1; 115 .check '' 116 117 # TEXT parent, REAL child. Child value 1001.0 matches '1001.0' but 118 # not '1001'. 119 CREATE TABLE p2(code TEXT PRIMARY KEY); 120 CREATE TABLE c2(k REAL REFERENCES p2 ON DELETE SET NULL); 121 INSERT INTO p2 VALUES('1001'), ('1001.0'); 122 INSERT INTO c2 VALUES(1001); 123 124 .testcase 9.1.5 125 DELETE FROM p2 WHERE code='1001'; 126 SELECT quote(k) FROM c2; 127 .check 1001.0 128 129 .testcase 9.1.6 130 DELETE FROM p2 WHERE code='1001.0'; 131 SELECT quote(k) FROM c2; 132 .check NULL 133 134 # BLOB parent, INTEGER child. Child value 5 matches 5 but not '5'. 135 CREATE TABLE p3(code UNIQUE); 136 CREATE TABLE c3(k INTEGER REFERENCES p3(code) ON DELETE CASCADE); 137 INSERT INTO p3 VALUES('5'), (5); 138 INSERT INTO c3 VALUES(5); 139 140 .testcase 9.1.8 141 DELETE FROM p3 WHERE code='5'; 142 SELECT quote(k) FROM c3; 143 .check 5 144 145 .testcase 9.1.9 146 DELETE FROM p3 WHERE code=5; 147 SELECT quote(k) FROM c3; 148 .check '' 149 150 # TEXT parent, BLOB child, ON UPDATE CASCADE. 151 CREATE TABLE p4(code TEXT PRIMARY KEY); 152 CREATE TABLE c4(k REFERENCES p4 ON UPDATE CASCADE); 153 INSERT INTO p4 VALUES('7'), ('07'); 154 INSERT INTO c4 VALUES(7), ('07'); 155 156 .testcase 9.1.11 157 UPDATE p4 SET code='8' WHERE code='7'; 158 SELECT quote(k) FROM c4 ORDER BY 1; 159 .check <0 FROM bytecode('DELETE FROM parent WHERE id=1') 354 WHERE opcode='OpenEphemeral'; 355 .check 0 356 357 # An index with the wrong collation sequence is not suitable. 358 CREATE INDEX c1k1 ON c1(k COLLATE nocase); 359 .testcase 11.3 360 SELECT count(*)>0 FROM bytecode('DELETE FROM parent WHERE id=1') 361 WHERE opcode='OpenEphemeral'; 362 .check 0 363 364 # Nor is a partial index, or one on which k is not the left-most column. 365 CREATE INDEX c1k2 ON c1(k) WHERE id>10; 366 CREATE INDEX c1k3 ON c1(id, k); 367 .testcase 11.5 368 SELECT count(*)>0 FROM bytecode('DELETE FROM parent WHERE id=1') 369 WHERE opcode='OpenEphemeral'; 370 .check 0 371 372 CREATE INDEX c1k4 ON c1(k); 373 .testcase 11.7 374 SELECT count(*)>0 FROM bytecode('DELETE FROM parent WHERE id=1') 375 WHERE opcode='OpenEphemeral'; 376 .check 1 377 .endif /* capable('bytecode') */ 378 379 #------------------------------------------------------------------------- 380 # Check that an index on a composite child key can be used for all 381 # columns of the key, not just the left-most, when the comparisons do 382 # not naturally use the affinity of the parent key. 383 # 384 .open :memory: 385 PRAGMA foreign_keys=ON; 386 CREATE TABLE parent(id INTEGER PRIMARY KEY, x TEXT, y TEXT, UNIQUE(x, y)); 387 CREATE TABLE c1(a, b, FOREIGN KEY(a, b) REFERENCES parent(x, y)); 388 CREATE INDEX c1ab ON c1(a, b); 389 CREATE TABLE c2(a, b, FOREIGN KEY(a, b) REFERENCES parent(x, y)); 390 CREATE INDEX c2ba ON c2(b, a); 391 392 .testcase 12.1 393 EXPLAIN QUERY PLAN DELETE FROM parent WHERE id=1; 394 .check <