3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
6 # May you do good and not evil.
7 # May you find forgiveness for yourself and forgive others.
8 # May you share freely, never taking more than you give.
10 #***********************************************************************
12 # This file implements tests to verify the "testable statements" in the
13 # foreignkeys.in document.
15 # The tests in this file are arranged to mirror the structure of
16 # foreignkey.in, with one exception: The statements in section 2, which
17 # deals with enabling/disabling foreign key support, is tested first,
18 # before section 1. This is because some statements in section 2 deal
19 # with builds that do not include complete foreign key support (because
20 # either SQLITE_OMIT_TRIGGER or SQLITE_OMIT_FOREIGN_KEY was defined
24 set testdir [file dirname $argv0]
25 source $testdir/tester.tcl
27 proc eqp {sql {db db}} { uplevel execsql [list "EXPLAIN QUERY PLAN $sql"] $db }
29 ###########################################################################
30 ### SECTION 2: Enabling Foreign Key Support
31 ###########################################################################
33 #-------------------------------------------------------------------------
34 # EVIDENCE-OF: R-33710-56344 In order to use foreign key constraints in
35 # SQLite, the library must be compiled with neither
36 # SQLITE_OMIT_FOREIGN_KEY or SQLITE_OMIT_TRIGGER defined.
38 ifcapable trigger&&foreignkey {
41 PRAGMA foreign_keys = ON;
42 CREATE TABLE p(i PRIMARY KEY);
43 CREATE TABLE c(j REFERENCES p ON UPDATE CASCADE);
44 INSERT INTO p VALUES('hello');
45 INSERT INTO c VALUES('hello');
46 UPDATE p SET i = 'world';
52 #-------------------------------------------------------------------------
53 # Test the effects of defining OMIT_TRIGGER but not OMIT_FOREIGN_KEY.
55 # EVIDENCE-OF: R-10109-20452 If SQLITE_OMIT_TRIGGER is defined but
56 # SQLITE_OMIT_FOREIGN_KEY is not, then SQLite behaves as it did prior to
57 # version 3.6.19 (2009-10-14) - foreign key definitions are parsed and
58 # may be queried using PRAGMA foreign_key_list, but foreign key
59 # constraints are not enforced.
61 # Specifically, test that "PRAGMA foreign_keys" is a no-op in this case.
62 # When using the pragma to query the current setting, 0 rows are returned.
64 # EVIDENCE-OF: R-22567-44039 The PRAGMA foreign_keys command is a no-op
65 # in this configuration.
67 # EVIDENCE-OF: R-41784-13339 Tip: If the command "PRAGMA foreign_keys"
68 # returns no data instead of a single row containing "0" or "1", then
69 # the version of SQLite you are using does not support foreign keys
70 # (either because it is older than 3.6.19 or because it was compiled
71 # with SQLITE_OMIT_FOREIGN_KEY or SQLITE_OMIT_TRIGGER defined).
74 ifcapable !trigger&&foreignkey {
77 PRAGMA foreign_keys = ON;
78 CREATE TABLE p(i PRIMARY KEY);
79 CREATE TABLE c(j REFERENCES p ON UPDATE CASCADE);
80 INSERT INTO p VALUES('hello');
81 INSERT INTO c VALUES('hello');
82 UPDATE p SET i = 'world';
87 execsql { PRAGMA foreign_key_list(c) }
88 } {0 0 p j {} CASCADE {NO ACTION} NONE}
90 execsql { PRAGMA foreign_keys }
95 #-------------------------------------------------------------------------
96 # Test the effects of defining OMIT_FOREIGN_KEY.
98 # EVIDENCE-OF: R-58428-36660 If OMIT_FOREIGN_KEY is defined, then
99 # foreign key definitions cannot even be parsed (attempting to specify a
100 # foreign key definition is a syntax error).
102 # Specifically, test that foreign key constraints cannot even be parsed
106 ifcapable !foreignkey {
108 execsql { CREATE TABLE p(i PRIMARY KEY) }
109 catchsql { CREATE TABLE c(j REFERENCES p ON UPDATE CASCADE) }
110 } {1 {near "ON": syntax error}}
112 # This is allowed, as in this build, "REFERENCES" is not a keyword.
113 # The declared datatype of column j is "REFERENCES p".
114 execsql { CREATE TABLE c(j REFERENCES p) }
117 execsql { PRAGMA table_info(c) }
118 } {0 j {REFERENCES p} 0 {} 0}
120 execsql { PRAGMA foreign_key_list(c) }
123 execsql { PRAGMA foreign_keys }
127 ifcapable !foreignkey||!trigger { finish_test ; return }
131 #-------------------------------------------------------------------------
132 # EVIDENCE-OF: R-07280-60510 Assuming the library is compiled with
133 # foreign key constraints enabled, it must still be enabled by the
134 # application at runtime, using the PRAGMA foreign_keys command.
136 # This also tests that foreign key constraints are disabled by default.
138 # EVIDENCE-OF: R-44261-39702 Foreign key constraints are disabled by
139 # default (for backwards compatibility), so must be enabled separately
140 # for each database connection.
145 CREATE TABLE p(i PRIMARY KEY);
146 CREATE TABLE c(j REFERENCES p ON UPDATE CASCADE);
147 INSERT INTO p VALUES('hello');
148 INSERT INTO c VALUES('hello');
149 UPDATE p SET i = 'world';
157 PRAGMA foreign_keys = ON;
158 INSERT INTO p VALUES('hello');
159 INSERT INTO c VALUES('hello');
160 UPDATE p SET i = 'world';
165 #-------------------------------------------------------------------------
166 # EVIDENCE-OF: R-08013-37737 The application can also use a PRAGMA
167 # foreign_keys statement to determine if foreign keys are currently
171 # This also tests the example code in section 2 of foreignkeys.in.
173 # EVIDENCE-OF: R-11255-19907
177 execsql { PRAGMA foreign_keys }
181 PRAGMA foreign_keys = ON;
187 PRAGMA foreign_keys = OFF;
192 #-------------------------------------------------------------------------
193 # Test that it is not possible to enable or disable foreign key support
194 # while not in auto-commit mode.
196 # EVIDENCE-OF: R-46649-58537 It is not possible to enable or disable
197 # foreign key constraints in the middle of a multi-statement transaction
198 # (when SQLite is not in autocommit mode). Attempting to do so does not
199 # return an error; it simply has no effect.
204 PRAGMA foreign_keys = ON;
205 CREATE TABLE t1(a UNIQUE, b);
206 CREATE TABLE t2(c, d REFERENCES t1(a));
207 INSERT INTO t1 VALUES(1, 2);
208 INSERT INTO t2 VALUES(2, 1);
210 PRAGMA foreign_keys = OFF;
215 } {1 {FOREIGN KEY constraint failed}}
217 execsql { PRAGMA foreign_keys }
222 PRAGMA foreign_keys = OFF;
224 PRAGMA foreign_keys = ON;
233 ###########################################################################
234 ### SECTION 1: Introduction to Foreign Key Constraints
235 ###########################################################################
236 execsql "PRAGMA foreign_keys = ON"
238 #-------------------------------------------------------------------------
239 # Verify that the syntax in the first example in section 1 is valid.
241 # EVIDENCE-OF: R-04042-24825 To do so, a foreign key definition may be
242 # added by modifying the declaration of the track table to the
243 # following: CREATE TABLE track( trackid INTEGER, trackname TEXT,
244 # trackartist INTEGER, FOREIGN KEY(trackartist) REFERENCES
245 # artist(artistid) );
250 artistid INTEGER PRIMARY KEY,
257 FOREIGN KEY(trackartist) REFERENCES artist(artistid)
262 #-------------------------------------------------------------------------
263 # EVIDENCE-OF: R-61362-32087 Attempting to insert a row into the track
264 # table that does not correspond to any row in the artist table will
268 catchsql { INSERT INTO track VALUES(1, 'track 1', 1) }
269 } {1 {FOREIGN KEY constraint failed}}
271 execsql { INSERT INTO artist VALUES(2, 'artist 1') }
272 catchsql { INSERT INTO track VALUES(1, 'track 1', 1) }
273 } {1 {FOREIGN KEY constraint failed}}
275 execsql { INSERT INTO track VALUES(1, 'track 1', 2) }
278 #-------------------------------------------------------------------------
279 # Attempting to delete a row from the 'artist' table while there are
280 # dependent rows in the track table also fails.
282 # EVIDENCE-OF: R-24401-52400 as will attempting to delete a row from the
283 # artist table when there exist dependent rows in the track table
286 catchsql { DELETE FROM artist WHERE artistid = 2 }
287 } {1 {FOREIGN KEY constraint failed}}
290 DELETE FROM track WHERE trackartist = 2;
291 DELETE FROM artist WHERE artistid = 2;
295 #-------------------------------------------------------------------------
296 # If the foreign key column (trackartist) in table 'track' is set to NULL,
297 # there is no requirement for a matching row in the 'artist' table.
299 # EVIDENCE-OF: R-23980-48859 There is one exception: if the foreign key
300 # column in the track table is NULL, then no corresponding entry in the
301 # artist table is required.
303 do_test e_fkey-10.1 {
305 INSERT INTO track VALUES(1, 'track 1', NULL);
306 INSERT INTO track VALUES(2, 'track 2', NULL);
309 do_test e_fkey-10.2 {
310 execsql { SELECT * FROM artist }
312 do_test e_fkey-10.3 {
313 # Setting the trackid to a non-NULL value fails, of course.
314 catchsql { UPDATE track SET trackartist = 5 WHERE trackid = 1 }
315 } {1 {FOREIGN KEY constraint failed}}
316 do_test e_fkey-10.4 {
318 INSERT INTO artist VALUES(5, 'artist 5');
319 UPDATE track SET trackartist = 5 WHERE trackid = 1;
321 catchsql { DELETE FROM artist WHERE artistid = 5}
322 } {1 {FOREIGN KEY constraint failed}}
323 do_test e_fkey-10.5 {
325 UPDATE track SET trackartist = NULL WHERE trackid = 1;
326 DELETE FROM artist WHERE artistid = 5;
330 #-------------------------------------------------------------------------
331 # Test that the following is true fo all rows in the track table:
333 # trackartist IS NULL OR
334 # EXISTS(SELECT 1 FROM artist WHERE artistid=trackartist)
336 # EVIDENCE-OF: R-52486-21352 Expressed in SQL, this means that for every
337 # row in the track table, the following expression evaluates to true:
338 # trackartist IS NULL OR EXISTS(SELECT 1 FROM artist WHERE
339 # artistid=trackartist)
341 # This procedure executes a test case to check that statement
342 # R-52486-21352 is true after executing the SQL statement passed.
343 # as the second argument.
344 proc test_r52486_21352 {tn sql} {
345 set res [catchsql $sql]
348 {1 {UNIQUE constraint failed: artist.artistid}}
349 {1 {FOREIGN KEY constraint failed}}
351 if {[lsearch $results $res]<0} {
355 do_test e_fkey-11.$tn {
357 SELECT count(*) FROM track WHERE NOT (
358 trackartist IS NULL OR
359 EXISTS(SELECT 1 FROM artist WHERE artistid=trackartist)
365 # Execute a series of random INSERT, UPDATE and DELETE operations
366 # (some of which may fail due to FK or PK constraint violations) on
367 # the two tables in the example schema. Test that R-52486-21352
368 # is true after executing each operation.
371 {INSERT INTO track VALUES($t, 'track $t', $a)}
372 {DELETE FROM track WHERE trackid = $t}
373 {UPDATE track SET trackartist = $a WHERE trackid = $t}
374 {INSERT INTO artist VALUES($a, 'artist $a')}
375 {DELETE FROM artist WHERE artistid = $a}
376 {UPDATE artist SET artistid = $a2 WHERE artistid = $a}
378 for {set i 0} {$i < 500} {incr i} {
379 set a [expr int(rand()*10)]
380 set a2 [expr int(rand()*10)]
381 set t [expr int(rand()*50)]
382 set sql [subst [lindex $Template [expr int(rand()*6)]]]
384 test_r52486_21352 $i $sql
387 #-------------------------------------------------------------------------
388 # Check that a NOT NULL constraint can be added to the example schema
389 # to prohibit NULL child keys from being inserted.
391 # EVIDENCE-OF: R-42412-59321 Tip: If the application requires a stricter
392 # relationship between artist and track, where NULL values are not
393 # permitted in the trackartist column, simply add the appropriate "NOT
394 # NULL" constraint to the schema.
397 do_test e_fkey-12.1 {
400 artistid INTEGER PRIMARY KEY,
406 trackartist INTEGER NOT NULL,
407 FOREIGN KEY(trackartist) REFERENCES artist(artistid)
411 do_test e_fkey-12.2 {
412 catchsql { INSERT INTO track VALUES(14, 'Mr. Bojangles', NULL) }
413 } {1 {NOT NULL constraint failed: track.trackartist}}
415 #-------------------------------------------------------------------------
416 # EVIDENCE-OF: R-16127-35442
418 # Test an example from foreignkeys.html.
421 do_test e_fkey-13.1 {
424 artistid INTEGER PRIMARY KEY,
431 FOREIGN KEY(trackartist) REFERENCES artist(artistid)
433 INSERT INTO artist VALUES(1, 'Dean Martin');
434 INSERT INTO artist VALUES(2, 'Frank Sinatra');
435 INSERT INTO track VALUES(11, 'That''s Amore', 1);
436 INSERT INTO track VALUES(12, 'Christmas Blues', 1);
437 INSERT INTO track VALUES(13, 'My Way', 2);
440 do_test e_fkey-13.2 {
441 catchsql { INSERT INTO track VALUES(14, 'Mr. Bojangles', 3) }
442 } {1 {FOREIGN KEY constraint failed}}
443 do_test e_fkey-13.3 {
444 execsql { INSERT INTO track VALUES(14, 'Mr. Bojangles', NULL) }
446 do_test e_fkey-13.4 {
448 UPDATE track SET trackartist = 3 WHERE trackname = 'Mr. Bojangles';
450 } {1 {FOREIGN KEY constraint failed}}
451 do_test e_fkey-13.5 {
453 INSERT INTO artist VALUES(3, 'Sammy Davis Jr.');
454 UPDATE track SET trackartist = 3 WHERE trackname = 'Mr. Bojangles';
455 INSERT INTO track VALUES(15, 'Boogie Woogie', 3);
459 #-------------------------------------------------------------------------
460 # EVIDENCE-OF: R-15958-50233
462 # Test the second example from the first section of foreignkeys.html.
464 do_test e_fkey-14.1 {
466 DELETE FROM artist WHERE artistname = 'Frank Sinatra';
468 } {1 {FOREIGN KEY constraint failed}}
469 do_test e_fkey-14.2 {
471 DELETE FROM track WHERE trackname = 'My Way';
472 DELETE FROM artist WHERE artistname = 'Frank Sinatra';
475 do_test e_fkey-14.3 {
477 UPDATE artist SET artistid=4 WHERE artistname = 'Dean Martin';
479 } {1 {FOREIGN KEY constraint failed}}
480 do_test e_fkey-14.4 {
482 DELETE FROM track WHERE trackname IN('That''s Amore', 'Christmas Blues');
483 UPDATE artist SET artistid=4 WHERE artistname = 'Dean Martin';
488 #-------------------------------------------------------------------------
489 # EVIDENCE-OF: R-56032-24923 The foreign key constraint is satisfied if
490 # for each row in the child table either one or more of the child key
491 # columns are NULL, or there exists a row in the parent table for which
492 # each parent key column contains a value equal to the value in its
493 # associated child key column.
495 # Test also that the usual comparison rules are used when testing if there
496 # is a matching row in the parent table of a foreign key constraint.
498 # EVIDENCE-OF: R-57765-12380 In the above paragraph, the term "equal"
499 # means equal when values are compared using the rules specified here.
502 do_test e_fkey-15.1 {
504 CREATE TABLE par(p PRIMARY KEY);
505 CREATE TABLE chi(c REFERENCES par);
507 INSERT INTO par VALUES(1);
508 INSERT INTO par VALUES('1');
509 INSERT INTO par VALUES(X'31');
510 SELECT typeof(p) FROM par;
512 } {integer text blob}
514 proc test_efkey_45 {tn isError sql} {
515 do_test e_fkey-15.$tn.1 "
517 " [lindex {{0 {}} {1 {FOREIGN KEY constraint failed}}} $isError]
519 do_test e_fkey-15.$tn.2 {
521 SELECT * FROM chi WHERE c IS NOT NULL AND c NOT IN (SELECT p FROM par)
526 test_efkey_45 1 0 "INSERT INTO chi VALUES(1)"
527 test_efkey_45 2 1 "INSERT INTO chi VALUES('1.0')"
528 test_efkey_45 3 0 "INSERT INTO chi VALUES('1')"
529 test_efkey_45 4 1 "DELETE FROM par WHERE p = '1'"
530 test_efkey_45 5 0 "DELETE FROM chi WHERE c = '1'"
531 test_efkey_45 6 0 "DELETE FROM par WHERE p = '1'"
532 test_efkey_45 7 1 "INSERT INTO chi VALUES('1')"
533 test_efkey_45 8 0 "INSERT INTO chi VALUES(X'31')"
534 test_efkey_45 9 1 "INSERT INTO chi VALUES(X'32')"
536 #-------------------------------------------------------------------------
537 # Specifically, test that when comparing child and parent key values the
538 # default collation sequence of the parent key column is used.
540 # EVIDENCE-OF: R-15796-47513 When comparing text values, the collating
541 # sequence associated with the parent key column is always used.
544 do_test e_fkey-16.1 {
546 CREATE TABLE t1(a COLLATE nocase PRIMARY KEY);
547 CREATE TABLE t2(b REFERENCES t1);
550 do_test e_fkey-16.2 {
552 INSERT INTO t1 VALUES('oNe');
553 INSERT INTO t2 VALUES('one');
554 INSERT INTO t2 VALUES('ONE');
555 UPDATE t2 SET b = 'OnE';
556 UPDATE t1 SET a = 'ONE';
559 do_test e_fkey-16.3 {
560 catchsql { UPDATE t2 SET b = 'two' WHERE rowid = 1 }
561 } {1 {FOREIGN KEY constraint failed}}
562 do_test e_fkey-16.4 {
563 catchsql { DELETE FROM t1 WHERE rowid = 1 }
564 } {1 {FOREIGN KEY constraint failed}}
566 #-------------------------------------------------------------------------
567 # Specifically, test that when comparing child and parent key values the
568 # affinity of the parent key column is applied to the child key value
569 # before the comparison takes place.
571 # EVIDENCE-OF: R-04240-13860 When comparing values, if the parent key
572 # column has an affinity, then that affinity is applied to the child key
573 # value before the comparison is performed.
576 do_test e_fkey-17.1 {
578 CREATE TABLE t1(a NUMERIC PRIMARY KEY);
579 CREATE TABLE t2(b TEXT REFERENCES t1);
582 do_test e_fkey-17.2 {
584 INSERT INTO t1 VALUES(1);
585 INSERT INTO t1 VALUES(2);
586 INSERT INTO t1 VALUES('three');
587 INSERT INTO t2 VALUES('2.0');
588 SELECT b, typeof(b) FROM t2;
591 do_test e_fkey-17.3 {
592 execsql { SELECT typeof(a) FROM t1 }
593 } {integer integer text}
594 do_test e_fkey-17.4 {
595 catchsql { DELETE FROM t1 WHERE rowid = 2 }
596 } {1 {FOREIGN KEY constraint failed}}
598 ###########################################################################
599 ### SECTION 3: Required and Suggested Database Indexes
600 ###########################################################################
602 #-------------------------------------------------------------------------
603 # A parent key must be either a PRIMARY KEY, subject to a UNIQUE
604 # constraint, or have a UNIQUE index created on it.
606 # EVIDENCE-OF: R-13435-26311 Usually, the parent key of a foreign key
607 # constraint is the primary key of the parent table. If they are not the
608 # primary key, then the parent key columns must be collectively subject
609 # to a UNIQUE constraint or have a UNIQUE index.
611 # Also test that if a parent key is not subject to a PRIMARY KEY or UNIQUE
612 # constraint, but does have a UNIQUE index created on it, then the UNIQUE index
613 # must use the default collation sequences associated with the parent key
616 # EVIDENCE-OF: R-00376-39212 If the parent key columns have a UNIQUE
617 # index, then that index must use the collation sequences that are
618 # specified in the CREATE TABLE statement for the parent table.
621 do_test e_fkey-18.1 {
623 CREATE TABLE t2(a REFERENCES t1(x));
626 proc test_efkey_57 {tn isError sql} {
627 catchsql { DROP TABLE t1 }
629 do_test e_fkey-18.$tn {
630 catchsql { INSERT INTO t2 VALUES(NULL) }
631 } [lindex {{0 {}} {/1 {foreign key mismatch - ".*" referencing ".*"}/}} \
634 test_efkey_57 2 0 { CREATE TABLE t1(x PRIMARY KEY) }
635 test_efkey_57 3 0 { CREATE TABLE t1(x UNIQUE) }
636 test_efkey_57 4 0 { CREATE TABLE t1(x); CREATE UNIQUE INDEX t1i ON t1(x) }
639 CREATE UNIQUE INDEX t1i ON t1(x COLLATE nocase);
641 test_efkey_57 6 1 { CREATE TABLE t1(x) }
642 test_efkey_57 7 1 { CREATE TABLE t1(x, y, PRIMARY KEY(x, y)) }
643 test_efkey_57 8 1 { CREATE TABLE t1(x, y, UNIQUE(x, y)) }
645 CREATE TABLE t1(x, y);
646 CREATE UNIQUE INDEX t1i ON t1(x, y);
650 #-------------------------------------------------------------------------
651 # This block tests an example in foreignkeys.html. Several testable
652 # statements refer to this example, as follows
654 # EVIDENCE-OF: R-27484-01467
656 # FK Constraints on child1, child2 and child3 are Ok.
658 # Problem with FK on child4:
660 # EVIDENCE-OF: R-51039-44840 The foreign key declared as part of table
661 # child4 is an error because even though the parent key column is
662 # indexed, the index is not UNIQUE.
664 # Problem with FK on child5:
666 # EVIDENCE-OF: R-01060-48788 The foreign key for table child5 is an
667 # error because even though the parent key column has a unique index,
668 # the index uses a different collating sequence.
670 # Problem with FK on child6 and child7:
672 # EVIDENCE-OF: R-63088-37469 Tables child6 and child7 are incorrect
673 # because while both have UNIQUE indices on their parent keys, the keys
674 # are not an exact match to the columns of a single UNIQUE index.
677 do_test e_fkey-19.1 {
679 CREATE TABLE parent(a PRIMARY KEY, b UNIQUE, c, d, e, f);
680 CREATE UNIQUE INDEX i1 ON parent(c, d);
681 CREATE INDEX i2 ON parent(e);
682 CREATE UNIQUE INDEX i3 ON parent(f COLLATE nocase);
684 CREATE TABLE child1(f, g REFERENCES parent(a)); -- Ok
685 CREATE TABLE child2(h, i REFERENCES parent(b)); -- Ok
686 CREATE TABLE child3(j, k, FOREIGN KEY(j, k) REFERENCES parent(c, d)); -- Ok
687 CREATE TABLE child4(l, m REFERENCES parent(e)); -- Err
688 CREATE TABLE child5(n, o REFERENCES parent(f)); -- Err
689 CREATE TABLE child6(p, q, FOREIGN KEY(p,q) REFERENCES parent(b, c)); -- Err
690 CREATE TABLE child7(r REFERENCES parent(c)); -- Err
693 do_test e_fkey-19.2 {
695 INSERT INTO parent VALUES(1, 2, 3, 4, 5, 6);
696 INSERT INTO child1 VALUES('xxx', 1);
697 INSERT INTO child2 VALUES('xxx', 2);
698 INSERT INTO child3 VALUES(3, 4);
701 do_test e_fkey-19.2 {
702 catchsql { INSERT INTO child4 VALUES('xxx', 5) }
703 } {1 {foreign key mismatch - "child4" referencing "parent"}}
704 do_test e_fkey-19.3 {
705 catchsql { INSERT INTO child5 VALUES('xxx', 6) }
706 } {1 {foreign key mismatch - "child5" referencing "parent"}}
707 do_test e_fkey-19.4 {
708 catchsql { INSERT INTO child6 VALUES(2, 3) }
709 } {1 {foreign key mismatch - "child6" referencing "parent"}}
710 do_test e_fkey-19.5 {
711 catchsql { INSERT INTO child7 VALUES(3) }
712 } {1 {foreign key mismatch - "child7" referencing "parent"}}
714 #-------------------------------------------------------------------------
715 # Test errors in the database schema that are detected while preparing
716 # DML statements. The error text for these messages always matches
717 # either "foreign key mismatch" or "no such table*" (using [string match]).
719 # EVIDENCE-OF: R-45488-08504 If the database schema contains foreign key
720 # errors that require looking at more than one table definition to
721 # identify, then those errors are not detected when the tables are
724 # EVIDENCE-OF: R-48391-38472 Instead, such errors prevent the
725 # application from preparing SQL statements that modify the content of
726 # the child or parent tables in ways that use the foreign keys.
728 # EVIDENCE-OF: R-03108-63659 The English language error message for
729 # foreign key DML errors is usually "foreign key mismatch" but can also
730 # be "no such table" if the parent table does not exist.
732 # EVIDENCE-OF: R-35763-48267 Foreign key DML errors are reported if: The
733 # parent table does not exist, or The parent key columns named in the
734 # foreign key constraint do not exist, or The parent key columns named
735 # in the foreign key constraint are not the primary key of the parent
736 # table and are not subject to a unique constraint using collating
737 # sequence specified in the CREATE TABLE, or The child table references
738 # the primary key of the parent without specifying the primary key
739 # columns and the number of primary key columns in the parent do not
740 # match the number of child key columns.
742 do_test e_fkey-20.1 {
744 CREATE TABLE c1(c REFERENCES nosuchtable, d);
746 CREATE TABLE p2(a, b, UNIQUE(a, b));
747 CREATE TABLE c2(c, d, FOREIGN KEY(c, d) REFERENCES p2(a, x));
749 CREATE TABLE p3(a PRIMARY KEY, b);
750 CREATE TABLE c3(c REFERENCES p3(b), d);
752 CREATE TABLE p4(a PRIMARY KEY, b);
753 CREATE UNIQUE INDEX p4i ON p4(b COLLATE nocase);
754 CREATE TABLE c4(c REFERENCES p4(b), d);
756 CREATE TABLE p5(a PRIMARY KEY, b COLLATE nocase);
757 CREATE UNIQUE INDEX p5i ON p5(b COLLATE binary);
758 CREATE TABLE c5(c REFERENCES p5(b), d);
760 CREATE TABLE p6(a PRIMARY KEY, b);
761 CREATE TABLE c6(c, d, FOREIGN KEY(c, d) REFERENCES p6);
763 CREATE TABLE p7(a, b, PRIMARY KEY(a, b));
764 CREATE TABLE c7(c, d REFERENCES p7);
768 foreach {tn tbl ptbl err} {
769 2 c1 {} "no such table: main.nosuchtable"
770 3 c2 p2 "foreign key mismatch - \"c2\" referencing \"p2\""
771 4 c3 p3 "foreign key mismatch - \"c3\" referencing \"p3\""
772 5 c4 p4 "foreign key mismatch - \"c4\" referencing \"p4\""
773 6 c5 p5 "foreign key mismatch - \"c5\" referencing \"p5\""
774 7 c6 p6 "foreign key mismatch - \"c6\" referencing \"p6\""
775 8 c7 p7 "foreign key mismatch - \"c7\" referencing \"p7\""
777 do_test e_fkey-20.$tn.1 {
778 catchsql "INSERT INTO $tbl VALUES('a', 'b')"
780 do_test e_fkey-20.$tn.2 {
781 catchsql "UPDATE $tbl SET c = ?, d = ?"
783 do_test e_fkey-20.$tn.3 {
784 catchsql "INSERT INTO $tbl SELECT ?, ?"
788 do_test e_fkey-20.$tn.4 {
789 catchsql "DELETE FROM $ptbl"
791 do_test e_fkey-20.$tn.5 {
792 catchsql "UPDATE $ptbl SET a = ?, b = ?"
794 do_test e_fkey-20.$tn.6 {
795 catchsql "INSERT INTO $ptbl SELECT ?, ?"
800 #-------------------------------------------------------------------------
801 # EVIDENCE-OF: R-19353-43643
803 # Test the example of foreign key mismatch errors caused by implicitly
804 # mapping a child key to the primary key of the parent table when the
805 # child key consists of a different number of columns to that primary key.
808 do_test e_fkey-21.1 {
810 CREATE TABLE parent2(a, b, PRIMARY KEY(a,b));
812 CREATE TABLE child8(x, y, FOREIGN KEY(x,y) REFERENCES parent2); -- Ok
813 CREATE TABLE child9(x REFERENCES parent2); -- Err
814 CREATE TABLE child10(x,y,z, FOREIGN KEY(x,y,z) REFERENCES parent2); -- Err
817 do_test e_fkey-21.2 {
819 INSERT INTO parent2 VALUES('I', 'II');
820 INSERT INTO child8 VALUES('I', 'II');
823 do_test e_fkey-21.3 {
824 catchsql { INSERT INTO child9 VALUES('I') }
825 } {1 {foreign key mismatch - "child9" referencing "parent2"}}
826 do_test e_fkey-21.4 {
827 catchsql { INSERT INTO child9 VALUES('II') }
828 } {1 {foreign key mismatch - "child9" referencing "parent2"}}
829 do_test e_fkey-21.5 {
830 catchsql { INSERT INTO child9 VALUES(NULL) }
831 } {1 {foreign key mismatch - "child9" referencing "parent2"}}
832 do_test e_fkey-21.6 {
833 catchsql { INSERT INTO child10 VALUES('I', 'II', 'III') }
834 } {1 {foreign key mismatch - "child10" referencing "parent2"}}
835 do_test e_fkey-21.7 {
836 catchsql { INSERT INTO child10 VALUES(1, 2, 3) }
837 } {1 {foreign key mismatch - "child10" referencing "parent2"}}
838 do_test e_fkey-21.8 {
839 catchsql { INSERT INTO child10 VALUES(NULL, NULL, NULL) }
840 } {1 {foreign key mismatch - "child10" referencing "parent2"}}
842 #-------------------------------------------------------------------------
843 # Test errors that are reported when creating the child table.
846 # * different number of child and parent key columns, and
847 # * child columns that do not exist.
849 # EVIDENCE-OF: R-23682-59820 By contrast, if foreign key errors can be
850 # recognized simply by looking at the definition of the child table and
851 # without having to consult the parent table definition, then the CREATE
852 # TABLE statement for the child table fails.
854 # These errors are reported whether or not FK support is enabled.
856 # EVIDENCE-OF: R-33883-28833 Foreign key DDL errors are reported
857 # regardless of whether or not foreign key constraints are enabled when
858 # the table is created.
861 foreach fk [list OFF ON] {
862 execsql "PRAGMA foreign_keys = $fk"
864 foreach {sql error} {
865 "CREATE TABLE child1(a, b, FOREIGN KEY(a, b) REFERENCES p(c))"
866 {number of columns in foreign key does not match the number of columns in the referenced table}
867 "CREATE TABLE child2(a, b, FOREIGN KEY(a, b) REFERENCES p(c, d, e))"
868 {number of columns in foreign key does not match the number of columns in the referenced table}
869 "CREATE TABLE child2(a, b, FOREIGN KEY(a, c) REFERENCES p(c, d))"
870 {unknown column "c" in foreign key definition}
871 "CREATE TABLE child2(a, b, FOREIGN KEY(c, b) REFERENCES p(c, d))"
872 {unknown column "c" in foreign key definition}
874 do_test e_fkey-22.$fk.[incr i] {
880 #-------------------------------------------------------------------------
881 # Test that a REFERENCING clause that does not specify parent key columns
882 # implicitly maps to the primary key of the parent table.
884 # EVIDENCE-OF: R-43879-08025 Attaching a "REFERENCES <parent-table>"
885 # clause to a column definition creates a foreign
886 # key constraint that maps the column to the primary key of
889 do_test e_fkey-23.1 {
891 CREATE TABLE p1(a, b, PRIMARY KEY(a, b));
892 CREATE TABLE p2(a, b PRIMARY KEY);
893 CREATE TABLE c1(c, d, FOREIGN KEY(c, d) REFERENCES p1);
894 CREATE TABLE c2(a, b REFERENCES p2);
897 proc test_efkey_60 {tn isError sql} {
898 do_test e_fkey-23.$tn "
900 " [lindex {{0 {}} {1 {FOREIGN KEY constraint failed}}} $isError]
903 test_efkey_60 2 1 "INSERT INTO c1 VALUES(239, 231)"
904 test_efkey_60 3 0 "INSERT INTO p1 VALUES(239, 231)"
905 test_efkey_60 4 0 "INSERT INTO c1 VALUES(239, 231)"
906 test_efkey_60 5 1 "INSERT INTO c2 VALUES(239, 231)"
907 test_efkey_60 6 0 "INSERT INTO p2 VALUES(239, 231)"
908 test_efkey_60 7 0 "INSERT INTO c2 VALUES(239, 231)"
910 #-------------------------------------------------------------------------
911 # Test that an index on on the child key columns of an FK constraint
914 # EVIDENCE-OF: R-15417-28014 Indices are not required for child key
917 # Also test that if an index is created on the child key columns, it does
918 # not make a difference whether or not it is a UNIQUE index.
920 # EVIDENCE-OF: R-15741-50893 The child key index does not have to be
921 # (and usually will not be) a UNIQUE index.
924 do_test e_fkey-24.1 {
926 CREATE TABLE parent(x, y, UNIQUE(y, x));
927 CREATE TABLE c1(a, b, FOREIGN KEY(a, b) REFERENCES parent(x, y));
928 CREATE TABLE c2(a, b, FOREIGN KEY(a, b) REFERENCES parent(x, y));
929 CREATE TABLE c3(a, b, FOREIGN KEY(a, b) REFERENCES parent(x, y));
930 CREATE INDEX c2i ON c2(a, b);
931 CREATE UNIQUE INDEX c3i ON c2(b, a);
934 proc test_efkey_61 {tn isError sql} {
935 do_test e_fkey-24.$tn "
937 " [lindex {{0 {}} {1 {FOREIGN KEY constraint failed}}} $isError]
939 foreach {tn c} [list 2 c1 3 c2 4 c3] {
940 test_efkey_61 $tn.1 1 "INSERT INTO $c VALUES(1, 2)"
941 test_efkey_61 $tn.2 0 "INSERT INTO parent VALUES(1, 2)"
942 test_efkey_61 $tn.3 0 "INSERT INTO $c VALUES(1, 2)"
944 execsql "DELETE FROM $c ; DELETE FROM parent"
947 #-------------------------------------------------------------------------
948 # EVIDENCE-OF: R-00279-52283
950 # Test an example showing that when a row is deleted from the parent
951 # table, the child table is queried for orphaned rows as follows:
953 # SELECT rowid FROM track WHERE trackartist = ?
955 # EVIDENCE-OF: R-23302-30956 If this SELECT returns any rows at all,
956 # then SQLite concludes that deleting the row from the parent table
957 # would violate the foreign key constraint and returns an error.
959 do_test e_fkey-25.1 {
962 artistid INTEGER PRIMARY KEY,
969 FOREIGN KEY(trackartist) REFERENCES artist(artistid)
973 do_execsql_test e_fkey-25.2 {
974 PRAGMA foreign_keys = OFF;
975 EXPLAIN QUERY PLAN DELETE FROM artist WHERE 1;
976 EXPLAIN QUERY PLAN SELECT rowid FROM track WHERE trackartist = ?;
978 0 0 0 {SCAN TABLE artist}
979 0 0 0 {SCAN TABLE track}
981 do_execsql_test e_fkey-25.3 {
982 PRAGMA foreign_keys = ON;
983 EXPLAIN QUERY PLAN DELETE FROM artist WHERE 1;
985 0 0 0 {SCAN TABLE artist}
986 0 0 0 {SCAN TABLE track}
988 do_test e_fkey-25.4 {
990 INSERT INTO artist VALUES(5, 'artist 5');
991 INSERT INTO artist VALUES(6, 'artist 6');
992 INSERT INTO artist VALUES(7, 'artist 7');
993 INSERT INTO track VALUES(1, 'track 1', 5);
994 INSERT INTO track VALUES(2, 'track 2', 6);
998 do_test e_fkey-25.5 {
1000 [execsql { SELECT rowid FROM track WHERE trackartist = 5 }] \
1001 [catchsql { DELETE FROM artist WHERE artistid = 5 }]
1002 } {1 1 {FOREIGN KEY constraint failed}}
1004 do_test e_fkey-25.6 {
1006 [execsql { SELECT rowid FROM track WHERE trackartist = 7 }] \
1007 [catchsql { DELETE FROM artist WHERE artistid = 7 }]
1010 do_test e_fkey-25.7 {
1012 [execsql { SELECT rowid FROM track WHERE trackartist = 6 }] \
1013 [catchsql { DELETE FROM artist WHERE artistid = 6 }]
1014 } {2 1 {FOREIGN KEY constraint failed}}
1016 #-------------------------------------------------------------------------
1017 # EVIDENCE-OF: R-47936-10044 Or, more generally:
1018 # SELECT rowid FROM <child-table> WHERE <child-key> = :parent_key_value
1020 # Test that when a row is deleted from the parent table of an FK
1021 # constraint, the child table is queried for orphaned rows. The
1022 # query is equivalent to:
1024 # SELECT rowid FROM <child-table> WHERE <child-key> = :parent_key_value
1026 # Also test that when a row is inserted into the parent table, or when the
1027 # parent key values of an existing row are modified, a query equivalent
1028 # to the following is planned. In some cases it is not executed, but it
1029 # is always planned.
1031 # SELECT rowid FROM <child-table> WHERE <child-key> = :parent_key_value
1033 # EVIDENCE-OF: R-61616-46700 Similar queries may be run if the content
1034 # of the parent key is modified or a new row is inserted into the parent
1039 do_test e_fkey-26.1 {
1040 execsql { CREATE TABLE parent(x, y, UNIQUE(y, x)) }
1044 CREATE TABLE child(a, b, FOREIGN KEY(a, b) REFERENCES parent(x, y))
1047 CREATE TABLE child(a, b, FOREIGN KEY(a, b) REFERENCES parent(x, y));
1048 CREATE INDEX childi ON child(a, b);
1051 CREATE TABLE child(a, b, FOREIGN KEY(a, b) REFERENCES parent(x, y));
1052 CREATE UNIQUE INDEX childi ON child(b, a);
1057 execsql {PRAGMA foreign_keys = OFF}
1058 set delete [concat \
1059 [eqp "DELETE FROM parent WHERE 1"] \
1060 [eqp "SELECT rowid FROM child WHERE a = ? AND b = ?"]
1062 set update [concat \
1063 [eqp "UPDATE parent SET x=?, y=?"] \
1064 [eqp "SELECT rowid FROM child WHERE a = ? AND b = ?"] \
1065 [eqp "SELECT rowid FROM child WHERE a = ? AND b = ?"]
1067 execsql {PRAGMA foreign_keys = ON}
1069 do_test e_fkey-26.$tn.1 { eqp "DELETE FROM parent WHERE 1" } $delete
1070 do_test e_fkey-26.$tn.2 { eqp "UPDATE parent set x=?, y=?" } $update
1072 execsql {DROP TABLE child}
1075 #-------------------------------------------------------------------------
1076 # EVIDENCE-OF: R-14553-34013
1078 # Test the example schema at the end of section 3. Also test that is
1079 # is "efficient". In this case "efficient" means that foreign key
1080 # related operations on the parent table do not provoke linear scans.
1083 do_test e_fkey-27.1 {
1085 CREATE TABLE artist(
1086 artistid INTEGER PRIMARY KEY,
1092 trackartist INTEGER REFERENCES artist
1094 CREATE INDEX trackindex ON track(trackartist);
1097 do_test e_fkey-27.2 {
1098 eqp { INSERT INTO artist VALUES(?, ?) }
1100 do_execsql_test e_fkey-27.3 {
1101 EXPLAIN QUERY PLAN UPDATE artist SET artistid = ?, artistname = ?
1103 0 0 0 {SCAN TABLE artist}
1104 0 0 0 {SEARCH TABLE track USING COVERING INDEX trackindex (trackartist=?)}
1105 0 0 0 {SEARCH TABLE track USING COVERING INDEX trackindex (trackartist=?)}
1107 do_execsql_test e_fkey-27.4 {
1108 EXPLAIN QUERY PLAN DELETE FROM artist
1110 0 0 0 {SCAN TABLE artist}
1111 0 0 0 {SEARCH TABLE track USING COVERING INDEX trackindex (trackartist=?)}
1115 ###########################################################################
1116 ### SECTION 4.1: Composite Foreign Key Constraints
1117 ###########################################################################
1119 #-------------------------------------------------------------------------
1120 # Check that parent and child keys must have the same number of columns.
1122 # EVIDENCE-OF: R-41062-34431 Parent and child keys must have the same
1125 foreach {tn sql err} {
1126 1 "CREATE TABLE c(jj REFERENCES p(x, y))"
1127 {foreign key on jj should reference only one column of table p}
1129 2 "CREATE TABLE c(jj REFERENCES p())" {near ")": syntax error}
1131 3 "CREATE TABLE c(jj, FOREIGN KEY(jj) REFERENCES p(x, y))"
1132 {number of columns in foreign key does not match the number of columns in the referenced table}
1134 4 "CREATE TABLE c(jj, FOREIGN KEY(jj) REFERENCES p())"
1135 {near ")": syntax error}
1137 5 "CREATE TABLE c(ii, jj, FOREIGN KEY(jj, ii) REFERENCES p())"
1138 {near ")": syntax error}
1140 6 "CREATE TABLE c(ii, jj, FOREIGN KEY(jj, ii) REFERENCES p(x))"
1141 {number of columns in foreign key does not match the number of columns in the referenced table}
1143 7 "CREATE TABLE c(ii, jj, FOREIGN KEY(jj, ii) REFERENCES p(x,y,z))"
1144 {number of columns in foreign key does not match the number of columns in the referenced table}
1147 do_test e_fkey-28.$tn [list catchsql $sql] [list 1 $err]
1149 do_test e_fkey-28.8 {
1152 CREATE TABLE p(x PRIMARY KEY);
1153 CREATE TABLE c(a, b, FOREIGN KEY(a,b) REFERENCES p);
1155 catchsql {DELETE FROM p}
1156 } {1 {foreign key mismatch - "c" referencing "p"}}
1157 do_test e_fkey-28.9 {
1160 CREATE TABLE p(x, y, PRIMARY KEY(x,y));
1161 CREATE TABLE c(a REFERENCES p);
1163 catchsql {DELETE FROM p}
1164 } {1 {foreign key mismatch - "c" referencing "p"}}
1167 #-------------------------------------------------------------------------
1168 # EVIDENCE-OF: R-24676-09859
1170 # Test the example schema in the "Composite Foreign Key Constraints"
1173 do_test e_fkey-29.1 {
1179 PRIMARY KEY(albumartist, albumname)
1186 FOREIGN KEY(songartist, songalbum) REFERENCES album(albumartist,albumname)
1191 do_test e_fkey-29.2 {
1193 INSERT INTO album VALUES('Elvis Presley', 'Elvis'' Christmas Album', NULL);
1194 INSERT INTO song VALUES(
1195 1, 'Elvis Presley', 'Elvis'' Christmas Album', 'Here Comes Santa Clause'
1199 do_test e_fkey-29.3 {
1201 INSERT INTO song VALUES(2, 'Elvis Presley', 'Elvis Is Back!', 'Fever');
1203 } {1 {FOREIGN KEY constraint failed}}
1206 #-------------------------------------------------------------------------
1207 # EVIDENCE-OF: R-33626-48418 In SQLite, if any of the child key columns
1208 # (in this case songartist and songalbum) are NULL, then there is no
1209 # requirement for a corresponding row in the parent table.
1211 do_test e_fkey-30.1 {
1213 INSERT INTO song VALUES(2, 'Elvis Presley', NULL, 'Fever');
1214 INSERT INTO song VALUES(3, NULL, 'Elvis Is Back', 'Soldier Boy');
1218 ###########################################################################
1219 ### SECTION 4.2: Deferred Foreign Key Constraints
1220 ###########################################################################
1222 #-------------------------------------------------------------------------
1223 # Test that if a statement violates an immediate FK constraint, and the
1224 # database does not satisfy the FK constraint once all effects of the
1225 # statement have been applied, an error is reported and the effects of
1226 # the statement rolled back.
1228 # EVIDENCE-OF: R-09323-30470 If a statement modifies the contents of the
1229 # database so that an immediate foreign key constraint is in violation
1230 # at the conclusion the statement, an exception is thrown and the
1231 # effects of the statement are reverted.
1234 do_test e_fkey-31.1 {
1236 CREATE TABLE king(a, b, PRIMARY KEY(a));
1237 CREATE TABLE prince(c REFERENCES king, d);
1241 do_test e_fkey-31.2 {
1242 # Execute a statement that violates the immediate FK constraint.
1243 catchsql { INSERT INTO prince VALUES(1, 2) }
1244 } {1 {FOREIGN KEY constraint failed}}
1246 do_test e_fkey-31.3 {
1247 # This time, use a trigger to fix the constraint violation before the
1248 # statement has finished executing. Then execute the same statement as
1249 # in the previous test case. This time, no error.
1251 CREATE TRIGGER kt AFTER INSERT ON prince WHEN
1252 NOT EXISTS (SELECT a FROM king WHERE a = new.c)
1254 INSERT INTO king VALUES(new.c, NULL);
1257 execsql { INSERT INTO prince VALUES(1, 2) }
1260 # Test that operating inside a transaction makes no difference to
1261 # immediate constraint violation handling.
1262 do_test e_fkey-31.4 {
1265 INSERT INTO prince VALUES(2, 3);
1268 catchsql { INSERT INTO prince VALUES(3, 4) }
1269 } {1 {FOREIGN KEY constraint failed}}
1270 do_test e_fkey-31.5 {
1277 #-------------------------------------------------------------------------
1278 # Test that if a deferred constraint is violated within a transaction,
1279 # nothing happens immediately and the database is allowed to persist
1280 # in a state that does not satisfy the FK constraint. However attempts
1281 # to COMMIT the transaction fail until the FK constraint is satisfied.
1283 # EVIDENCE-OF: R-49178-21358 By contrast, if a statement modifies the
1284 # contents of the database such that a deferred foreign key constraint
1285 # is violated, the violation is not reported immediately.
1287 # EVIDENCE-OF: R-39692-12488 Deferred foreign key constraints are not
1288 # checked until the transaction tries to COMMIT.
1290 # EVIDENCE-OF: R-55147-47664 For as long as the user has an open
1291 # transaction, the database is allowed to exist in a state that violates
1292 # any number of deferred foreign key constraints.
1294 # EVIDENCE-OF: R-29604-30395 However, COMMIT will fail as long as
1295 # foreign key constraints remain in violation.
1297 proc test_efkey_34 {tn isError sql} {
1298 do_test e_fkey-32.$tn "
1300 " [lindex {{0 {}} {1 {FOREIGN KEY constraint failed}}} $isError]
1305 CREATE TABLE ll(k PRIMARY KEY);
1306 CREATE TABLE kk(c REFERENCES ll DEFERRABLE INITIALLY DEFERRED);
1308 test_efkey_34 2 0 "BEGIN"
1309 test_efkey_34 3 0 "INSERT INTO kk VALUES(5)"
1310 test_efkey_34 4 0 "INSERT INTO kk VALUES(10)"
1311 test_efkey_34 5 1 "COMMIT"
1312 test_efkey_34 6 0 "INSERT INTO ll VALUES(10)"
1313 test_efkey_34 7 1 "COMMIT"
1314 test_efkey_34 8 0 "INSERT INTO ll VALUES(5)"
1315 test_efkey_34 9 0 "COMMIT"
1317 #-------------------------------------------------------------------------
1318 # When not running inside a transaction, a deferred constraint is similar
1319 # to an immediate constraint (violations are reported immediately).
1321 # EVIDENCE-OF: R-56844-61705 If the current statement is not inside an
1322 # explicit transaction (a BEGIN/COMMIT/ROLLBACK block), then an implicit
1323 # transaction is committed as soon as the statement has finished
1324 # executing. In this case deferred constraints behave the same as
1325 # immediate constraints.
1328 proc test_efkey_35 {tn isError sql} {
1329 do_test e_fkey-33.$tn "
1331 " [lindex {{0 {}} {1 {FOREIGN KEY constraint failed}}} $isError]
1333 do_test e_fkey-33.1 {
1335 CREATE TABLE parent(x, y);
1336 CREATE UNIQUE INDEX pi ON parent(x, y);
1337 CREATE TABLE child(a, b,
1338 FOREIGN KEY(a, b) REFERENCES parent(x, y) DEFERRABLE INITIALLY DEFERRED
1342 test_efkey_35 2 1 "INSERT INTO child VALUES('x', 'y')"
1343 test_efkey_35 3 0 "INSERT INTO parent VALUES('x', 'y')"
1344 test_efkey_35 4 0 "INSERT INTO child VALUES('x', 'y')"
1347 #-------------------------------------------------------------------------
1348 # EVIDENCE-OF: R-12782-61841
1350 # Test that an FK constraint is made deferred by adding the following
1351 # to the definition:
1353 # DEFERRABLE INITIALLY DEFERRED
1355 # EVIDENCE-OF: R-09005-28791
1357 # Also test that adding any of the following to a foreign key definition
1358 # makes the constraint IMMEDIATE:
1360 # NOT DEFERRABLE INITIALLY DEFERRED
1361 # NOT DEFERRABLE INITIALLY IMMEDIATE
1363 # DEFERRABLE INITIALLY IMMEDIATE
1366 # Foreign keys are IMMEDIATE by default (if there is no DEFERRABLE or NOT
1367 # DEFERRABLE clause).
1369 # EVIDENCE-OF: R-35290-16460 Foreign key constraints are immediate by
1372 # EVIDENCE-OF: R-30323-21917 Each foreign key constraint in SQLite is
1373 # classified as either immediate or deferred.
1376 do_test e_fkey-34.1 {
1378 CREATE TABLE parent(x, y, z, PRIMARY KEY(x,y,z));
1379 CREATE TABLE c1(a, b, c,
1380 FOREIGN KEY(a, b, c) REFERENCES parent NOT DEFERRABLE INITIALLY DEFERRED
1382 CREATE TABLE c2(a, b, c,
1383 FOREIGN KEY(a, b, c) REFERENCES parent NOT DEFERRABLE INITIALLY IMMEDIATE
1385 CREATE TABLE c3(a, b, c,
1386 FOREIGN KEY(a, b, c) REFERENCES parent NOT DEFERRABLE
1388 CREATE TABLE c4(a, b, c,
1389 FOREIGN KEY(a, b, c) REFERENCES parent DEFERRABLE INITIALLY IMMEDIATE
1391 CREATE TABLE c5(a, b, c,
1392 FOREIGN KEY(a, b, c) REFERENCES parent DEFERRABLE
1394 CREATE TABLE c6(a, b, c, FOREIGN KEY(a, b, c) REFERENCES parent);
1396 -- This FK constraint is the only deferrable one.
1397 CREATE TABLE c7(a, b, c,
1398 FOREIGN KEY(a, b, c) REFERENCES parent DEFERRABLE INITIALLY DEFERRED
1401 INSERT INTO parent VALUES('a', 'b', 'c');
1402 INSERT INTO parent VALUES('d', 'e', 'f');
1403 INSERT INTO parent VALUES('g', 'h', 'i');
1404 INSERT INTO parent VALUES('j', 'k', 'l');
1405 INSERT INTO parent VALUES('m', 'n', 'o');
1406 INSERT INTO parent VALUES('p', 'q', 'r');
1407 INSERT INTO parent VALUES('s', 't', 'u');
1409 INSERT INTO c1 VALUES('a', 'b', 'c');
1410 INSERT INTO c2 VALUES('d', 'e', 'f');
1411 INSERT INTO c3 VALUES('g', 'h', 'i');
1412 INSERT INTO c4 VALUES('j', 'k', 'l');
1413 INSERT INTO c5 VALUES('m', 'n', 'o');
1414 INSERT INTO c6 VALUES('p', 'q', 'r');
1415 INSERT INTO c7 VALUES('s', 't', 'u');
1419 proc test_efkey_29 {tn sql isError} {
1420 do_test e_fkey-34.$tn "catchsql {$sql}" [
1421 lindex {{0 {}} {1 {FOREIGN KEY constraint failed}}} $isError
1424 test_efkey_29 2 "BEGIN" 0
1425 test_efkey_29 3 "DELETE FROM parent WHERE x = 'a'" 1
1426 test_efkey_29 4 "DELETE FROM parent WHERE x = 'd'" 1
1427 test_efkey_29 5 "DELETE FROM parent WHERE x = 'g'" 1
1428 test_efkey_29 6 "DELETE FROM parent WHERE x = 'j'" 1
1429 test_efkey_29 7 "DELETE FROM parent WHERE x = 'm'" 1
1430 test_efkey_29 8 "DELETE FROM parent WHERE x = 'p'" 1
1431 test_efkey_29 9 "DELETE FROM parent WHERE x = 's'" 0
1432 test_efkey_29 10 "COMMIT" 1
1433 test_efkey_29 11 "ROLLBACK" 0
1435 test_efkey_29 9 "BEGIN" 0
1436 test_efkey_29 10 "UPDATE parent SET z = 'z' WHERE z = 'c'" 1
1437 test_efkey_29 11 "UPDATE parent SET z = 'z' WHERE z = 'f'" 1
1438 test_efkey_29 12 "UPDATE parent SET z = 'z' WHERE z = 'i'" 1
1439 test_efkey_29 13 "UPDATE parent SET z = 'z' WHERE z = 'l'" 1
1440 test_efkey_29 14 "UPDATE parent SET z = 'z' WHERE z = 'o'" 1
1441 test_efkey_29 15 "UPDATE parent SET z = 'z' WHERE z = 'r'" 1
1442 test_efkey_29 16 "UPDATE parent SET z = 'z' WHERE z = 'u'" 0
1443 test_efkey_29 17 "COMMIT" 1
1444 test_efkey_29 18 "ROLLBACK" 0
1446 test_efkey_29 17 "BEGIN" 0
1447 test_efkey_29 18 "INSERT INTO c1 VALUES(1, 2, 3)" 1
1448 test_efkey_29 19 "INSERT INTO c2 VALUES(1, 2, 3)" 1
1449 test_efkey_29 20 "INSERT INTO c3 VALUES(1, 2, 3)" 1
1450 test_efkey_29 21 "INSERT INTO c4 VALUES(1, 2, 3)" 1
1451 test_efkey_29 22 "INSERT INTO c5 VALUES(1, 2, 3)" 1
1452 test_efkey_29 22 "INSERT INTO c6 VALUES(1, 2, 3)" 1
1453 test_efkey_29 22 "INSERT INTO c7 VALUES(1, 2, 3)" 0
1454 test_efkey_29 23 "COMMIT" 1
1455 test_efkey_29 24 "INSERT INTO parent VALUES(1, 2, 3)" 0
1456 test_efkey_29 25 "COMMIT" 0
1458 test_efkey_29 26 "BEGIN" 0
1459 test_efkey_29 27 "UPDATE c1 SET a = 10" 1
1460 test_efkey_29 28 "UPDATE c2 SET a = 10" 1
1461 test_efkey_29 29 "UPDATE c3 SET a = 10" 1
1462 test_efkey_29 30 "UPDATE c4 SET a = 10" 1
1463 test_efkey_29 31 "UPDATE c5 SET a = 10" 1
1464 test_efkey_29 31 "UPDATE c6 SET a = 10" 1
1465 test_efkey_29 31 "UPDATE c7 SET a = 10" 0
1466 test_efkey_29 32 "COMMIT" 1
1467 test_efkey_29 33 "ROLLBACK" 0
1469 #-------------------------------------------------------------------------
1470 # EVIDENCE-OF: R-24499-57071
1472 # Test an example from foreignkeys.html dealing with a deferred foreign
1475 do_test e_fkey-35.1 {
1478 CREATE TABLE artist(
1479 artistid INTEGER PRIMARY KEY,
1485 trackartist INTEGER REFERENCES artist(artistid) DEFERRABLE INITIALLY DEFERRED
1489 do_test e_fkey-35.2 {
1492 INSERT INTO track VALUES(1, 'White Christmas', 5);
1495 } {1 {FOREIGN KEY constraint failed}}
1496 do_test e_fkey-35.3 {
1498 INSERT INTO artist VALUES(5, 'Bing Crosby');
1503 #-------------------------------------------------------------------------
1504 # Verify that a nested savepoint may be released without satisfying
1505 # deferred foreign key constraints.
1507 # EVIDENCE-OF: R-07223-48323 A nested savepoint transaction may be
1508 # RELEASEd while the database is in a state that does not satisfy a
1509 # deferred foreign key constraint.
1512 do_test e_fkey-36.1 {
1514 CREATE TABLE t1(a PRIMARY KEY,
1515 b REFERENCES t1 DEFERRABLE INITIALLY DEFERRED
1517 INSERT INTO t1 VALUES(1, 1);
1518 INSERT INTO t1 VALUES(2, 2);
1519 INSERT INTO t1 VALUES(3, 3);
1522 do_test e_fkey-36.2 {
1526 INSERT INTO t1 VALUES(4, 5);
1530 do_test e_fkey-36.3 {
1532 } {1 {FOREIGN KEY constraint failed}}
1533 do_test e_fkey-36.4 {
1535 UPDATE t1 SET a = 5 WHERE a = 4;
1541 #-------------------------------------------------------------------------
1542 # Check that a transaction savepoint (an outermost savepoint opened when
1543 # the database was in auto-commit mode) cannot be released without
1544 # satisfying deferred foreign key constraints. It may be rolled back.
1546 # EVIDENCE-OF: R-44295-13823 A transaction savepoint (a non-nested
1547 # savepoint that was opened while there was not currently an open
1548 # transaction), on the other hand, is subject to the same restrictions
1549 # as a COMMIT - attempting to RELEASE it while the database is in such a
1552 do_test e_fkey-37.1 {
1556 INSERT INTO t1 VALUES(6, 7);
1560 do_test e_fkey-37.2 {
1561 catchsql {RELEASE one}
1562 } {1 {FOREIGN KEY constraint failed}}
1563 do_test e_fkey-37.3 {
1565 UPDATE t1 SET a = 7 WHERE a = 6;
1569 do_test e_fkey-37.4 {
1573 INSERT INTO t1 VALUES(9, 10);
1577 do_test e_fkey-37.5 {
1578 catchsql {RELEASE one}
1579 } {1 {FOREIGN KEY constraint failed}}
1580 do_test e_fkey-37.6 {
1581 execsql {ROLLBACK TO one ; RELEASE one}
1584 #-------------------------------------------------------------------------
1585 # Test that if a COMMIT operation fails due to deferred foreign key
1586 # constraints, any nested savepoints remain open.
1588 # EVIDENCE-OF: R-37736-42616 If a COMMIT statement (or the RELEASE of a
1589 # transaction SAVEPOINT) fails because the database is currently in a
1590 # state that violates a deferred foreign key constraint and there are
1591 # currently nested savepoints, the nested savepoints remain open.
1593 do_test e_fkey-38.1 {
1595 DELETE FROM t1 WHERE a>3;
1599 do_test e_fkey-38.2 {
1602 INSERT INTO t1 VALUES(4, 4);
1604 INSERT INTO t1 VALUES(5, 6);
1607 } {1 1 2 2 3 3 4 4 5 6}
1608 do_test e_fkey-38.3 {
1610 } {1 {FOREIGN KEY constraint failed}}
1611 do_test e_fkey-38.4 {
1619 do_test e_fkey-38.5 {
1622 INSERT INTO t1 VALUES(5, 5);
1624 INSERT INTO t1 VALUES(6, 7);
1626 INSERT INTO t1 VALUES(7, 8);
1629 do_test e_fkey-38.6 {
1630 catchsql {RELEASE a}
1631 } {1 {FOREIGN KEY constraint failed}}
1632 do_test e_fkey-38.7 {
1633 execsql {ROLLBACK TO c}
1634 catchsql {RELEASE a}
1635 } {1 {FOREIGN KEY constraint failed}}
1636 do_test e_fkey-38.8 {
1642 } {1 1 2 2 3 3 4 4 5 5}
1644 ###########################################################################
1645 ### SECTION 4.3: ON DELETE and ON UPDATE Actions
1646 ###########################################################################
1648 #-------------------------------------------------------------------------
1649 # Test that configured ON DELETE and ON UPDATE actions take place when
1650 # deleting or modifying rows of the parent table, respectively.
1652 # EVIDENCE-OF: R-48270-44282 Foreign key ON DELETE and ON UPDATE clauses
1653 # are used to configure actions that take place when deleting rows from
1654 # the parent table (ON DELETE), or modifying the parent key values of
1655 # existing rows (ON UPDATE).
1657 # Test that a single FK constraint may have different actions configured
1658 # for ON DELETE and ON UPDATE.
1660 # EVIDENCE-OF: R-48124-63225 A single foreign key constraint may have
1661 # different actions configured for ON DELETE and ON UPDATE.
1663 do_test e_fkey-39.1 {
1665 CREATE TABLE p(a, b PRIMARY KEY, c);
1666 CREATE TABLE c1(d, e, f DEFAULT 'k0' REFERENCES p
1667 ON UPDATE SET DEFAULT
1671 INSERT INTO p VALUES(0, 'k0', '');
1672 INSERT INTO p VALUES(1, 'k1', 'I');
1673 INSERT INTO p VALUES(2, 'k2', 'II');
1674 INSERT INTO p VALUES(3, 'k3', 'III');
1676 INSERT INTO c1 VALUES(1, 'xx', 'k1');
1677 INSERT INTO c1 VALUES(2, 'xx', 'k2');
1678 INSERT INTO c1 VALUES(3, 'xx', 'k3');
1681 do_test e_fkey-39.2 {
1683 UPDATE p SET b = 'k4' WHERE a = 1;
1686 } {1 xx k0 2 xx k2 3 xx k3}
1687 do_test e_fkey-39.3 {
1689 DELETE FROM p WHERE a = 2;
1692 } {1 xx k0 2 xx {} 3 xx k3}
1693 do_test e_fkey-39.4 {
1695 CREATE UNIQUE INDEX pi ON p(c);
1696 REPLACE INTO p VALUES(5, 'k5', 'III');
1699 } {1 xx k0 2 xx {} 3 xx {}}
1701 #-------------------------------------------------------------------------
1702 # Each foreign key in the system has an ON UPDATE and ON DELETE action,
1703 # either "NO ACTION", "RESTRICT", "SET NULL", "SET DEFAULT" or "CASCADE".
1705 # EVIDENCE-OF: R-33326-45252 The ON DELETE and ON UPDATE action
1706 # associated with each foreign key in an SQLite database is one of "NO
1707 # ACTION", "RESTRICT", "SET NULL", "SET DEFAULT" or "CASCADE".
1709 # If none is specified explicitly, "NO ACTION" is the default.
1711 # EVIDENCE-OF: R-19803-45884 If an action is not explicitly specified,
1712 # it defaults to "NO ACTION".
1715 do_test e_fkey-40.1 {
1717 CREATE TABLE parent(x PRIMARY KEY, y);
1718 CREATE TABLE child1(a,
1719 b REFERENCES parent ON UPDATE NO ACTION ON DELETE RESTRICT
1721 CREATE TABLE child2(a,
1722 b REFERENCES parent ON UPDATE RESTRICT ON DELETE SET NULL
1724 CREATE TABLE child3(a,
1725 b REFERENCES parent ON UPDATE SET NULL ON DELETE SET DEFAULT
1727 CREATE TABLE child4(a,
1728 b REFERENCES parent ON UPDATE SET DEFAULT ON DELETE CASCADE
1731 -- Create some foreign keys that use the default action - "NO ACTION"
1732 CREATE TABLE child5(a, b REFERENCES parent ON UPDATE CASCADE);
1733 CREATE TABLE child6(a, b REFERENCES parent ON DELETE RESTRICT);
1734 CREATE TABLE child7(a, b REFERENCES parent ON DELETE NO ACTION);
1735 CREATE TABLE child8(a, b REFERENCES parent ON UPDATE NO ACTION);
1739 foreach {tn zTab lRes} {
1740 2 child1 {0 0 parent b {} {NO ACTION} RESTRICT NONE}
1741 3 child2 {0 0 parent b {} RESTRICT {SET NULL} NONE}
1742 4 child3 {0 0 parent b {} {SET NULL} {SET DEFAULT} NONE}
1743 5 child4 {0 0 parent b {} {SET DEFAULT} CASCADE NONE}
1744 6 child5 {0 0 parent b {} CASCADE {NO ACTION} NONE}
1745 7 child6 {0 0 parent b {} {NO ACTION} RESTRICT NONE}
1746 8 child7 {0 0 parent b {} {NO ACTION} {NO ACTION} NONE}
1747 9 child8 {0 0 parent b {} {NO ACTION} {NO ACTION} NONE}
1749 do_test e_fkey-40.$tn { execsql "PRAGMA foreign_key_list($zTab)" } $lRes
1752 #-------------------------------------------------------------------------
1753 # Test that "NO ACTION" means that nothing happens to a child row when
1754 # it's parent row is updated or deleted.
1756 # EVIDENCE-OF: R-19971-54976 Configuring "NO ACTION" means just that:
1757 # when a parent key is modified or deleted from the database, no special
1761 do_test e_fkey-41.1 {
1763 CREATE TABLE parent(p1, p2, PRIMARY KEY(p1, p2));
1764 CREATE TABLE child(c1, c2,
1765 FOREIGN KEY(c1, c2) REFERENCES parent
1768 DEFERRABLE INITIALLY DEFERRED
1770 INSERT INTO parent VALUES('j', 'k');
1771 INSERT INTO parent VALUES('l', 'm');
1772 INSERT INTO child VALUES('j', 'k');
1773 INSERT INTO child VALUES('l', 'm');
1776 do_test e_fkey-41.2 {
1779 UPDATE parent SET p1='k' WHERE p1='j';
1780 DELETE FROM parent WHERE p1='l';
1781 SELECT * FROM child;
1784 do_test e_fkey-41.3 {
1786 } {1 {FOREIGN KEY constraint failed}}
1787 do_test e_fkey-41.4 {
1791 #-------------------------------------------------------------------------
1792 # Test that "RESTRICT" means the application is prohibited from deleting
1793 # or updating a parent table row when there exists one or more child keys
1796 # EVIDENCE-OF: R-04272-38653 The "RESTRICT" action means that the
1797 # application is prohibited from deleting (for ON DELETE RESTRICT) or
1798 # modifying (for ON UPDATE RESTRICT) a parent key when there exists one
1799 # or more child keys mapped to it.
1802 do_test e_fkey-41.1 {
1804 CREATE TABLE parent(p1, p2);
1805 CREATE UNIQUE INDEX parent_i ON parent(p1, p2);
1806 CREATE TABLE child1(c1, c2,
1807 FOREIGN KEY(c2, c1) REFERENCES parent(p1, p2) ON DELETE RESTRICT
1809 CREATE TABLE child2(c1, c2,
1810 FOREIGN KEY(c2, c1) REFERENCES parent(p1, p2) ON UPDATE RESTRICT
1814 do_test e_fkey-41.2 {
1816 INSERT INTO parent VALUES('a', 'b');
1817 INSERT INTO parent VALUES('c', 'd');
1818 INSERT INTO child1 VALUES('b', 'a');
1819 INSERT INTO child2 VALUES('d', 'c');
1822 do_test e_fkey-41.3 {
1823 catchsql { DELETE FROM parent WHERE p1 = 'a' }
1824 } {1 {FOREIGN KEY constraint failed}}
1825 do_test e_fkey-41.4 {
1826 catchsql { UPDATE parent SET p2 = 'e' WHERE p1 = 'c' }
1827 } {1 {FOREIGN KEY constraint failed}}
1829 #-------------------------------------------------------------------------
1830 # Test that RESTRICT is slightly different from NO ACTION for IMMEDIATE
1831 # constraints, in that it is enforced immediately, not at the end of the
1834 # EVIDENCE-OF: R-37997-42187 The difference between the effect of a
1835 # RESTRICT action and normal foreign key constraint enforcement is that
1836 # the RESTRICT action processing happens as soon as the field is updated
1837 # - not at the end of the current statement as it would with an
1838 # immediate constraint, or at the end of the current transaction as it
1839 # would with a deferred constraint.
1842 do_test e_fkey-42.1 {
1844 CREATE TABLE parent(x PRIMARY KEY);
1845 CREATE TABLE child1(c REFERENCES parent ON UPDATE RESTRICT);
1846 CREATE TABLE child2(c REFERENCES parent ON UPDATE NO ACTION);
1848 INSERT INTO parent VALUES('key1');
1849 INSERT INTO parent VALUES('key2');
1850 INSERT INTO child1 VALUES('key1');
1851 INSERT INTO child2 VALUES('key2');
1853 CREATE TRIGGER parent_t AFTER UPDATE ON parent BEGIN
1854 UPDATE child1 set c = new.x WHERE c = old.x;
1855 UPDATE child2 set c = new.x WHERE c = old.x;
1859 do_test e_fkey-42.2 {
1860 catchsql { UPDATE parent SET x = 'key one' WHERE x = 'key1' }
1861 } {1 {FOREIGN KEY constraint failed}}
1862 do_test e_fkey-42.3 {
1864 UPDATE parent SET x = 'key two' WHERE x = 'key2';
1865 SELECT * FROM child2;
1870 do_test e_fkey-42.4 {
1872 CREATE TABLE parent(x PRIMARY KEY);
1873 CREATE TABLE child1(c REFERENCES parent ON DELETE RESTRICT);
1874 CREATE TABLE child2(c REFERENCES parent ON DELETE NO ACTION);
1876 INSERT INTO parent VALUES('key1');
1877 INSERT INTO parent VALUES('key2');
1878 INSERT INTO child1 VALUES('key1');
1879 INSERT INTO child2 VALUES('key2');
1881 CREATE TRIGGER parent_t AFTER DELETE ON parent BEGIN
1882 UPDATE child1 SET c = NULL WHERE c = old.x;
1883 UPDATE child2 SET c = NULL WHERE c = old.x;
1887 do_test e_fkey-42.5 {
1888 catchsql { DELETE FROM parent WHERE x = 'key1' }
1889 } {1 {FOREIGN KEY constraint failed}}
1890 do_test e_fkey-42.6 {
1892 DELETE FROM parent WHERE x = 'key2';
1893 SELECT * FROM child2;
1898 do_test e_fkey-42.7 {
1900 CREATE TABLE parent(x PRIMARY KEY);
1901 CREATE TABLE child1(c REFERENCES parent ON DELETE RESTRICT);
1902 CREATE TABLE child2(c REFERENCES parent ON DELETE NO ACTION);
1904 INSERT INTO parent VALUES('key1');
1905 INSERT INTO parent VALUES('key2');
1906 INSERT INTO child1 VALUES('key1');
1907 INSERT INTO child2 VALUES('key2');
1910 do_test e_fkey-42.8 {
1911 catchsql { REPLACE INTO parent VALUES('key1') }
1912 } {1 {FOREIGN KEY constraint failed}}
1913 do_test e_fkey-42.9 {
1915 REPLACE INTO parent VALUES('key2');
1916 SELECT * FROM child2;
1920 #-------------------------------------------------------------------------
1921 # Test that RESTRICT is enforced immediately, even for a DEFERRED constraint.
1923 # EVIDENCE-OF: R-24179-60523 Even if the foreign key constraint it is
1924 # attached to is deferred, configuring a RESTRICT action causes SQLite
1925 # to return an error immediately if a parent key with dependent child
1926 # keys is deleted or modified.
1929 do_test e_fkey-43.1 {
1931 CREATE TABLE parent(x PRIMARY KEY);
1932 CREATE TABLE child1(c REFERENCES parent ON UPDATE RESTRICT
1933 DEFERRABLE INITIALLY DEFERRED
1935 CREATE TABLE child2(c REFERENCES parent ON UPDATE NO ACTION
1936 DEFERRABLE INITIALLY DEFERRED
1939 INSERT INTO parent VALUES('key1');
1940 INSERT INTO parent VALUES('key2');
1941 INSERT INTO child1 VALUES('key1');
1942 INSERT INTO child2 VALUES('key2');
1946 do_test e_fkey-43.2 {
1947 catchsql { UPDATE parent SET x = 'key one' WHERE x = 'key1' }
1948 } {1 {FOREIGN KEY constraint failed}}
1949 do_test e_fkey-43.3 {
1950 execsql { UPDATE parent SET x = 'key two' WHERE x = 'key2' }
1952 do_test e_fkey-43.4 {
1954 } {1 {FOREIGN KEY constraint failed}}
1955 do_test e_fkey-43.5 {
1957 UPDATE child2 SET c = 'key two';
1963 do_test e_fkey-43.6 {
1965 CREATE TABLE parent(x PRIMARY KEY);
1966 CREATE TABLE child1(c REFERENCES parent ON DELETE RESTRICT
1967 DEFERRABLE INITIALLY DEFERRED
1969 CREATE TABLE child2(c REFERENCES parent ON DELETE NO ACTION
1970 DEFERRABLE INITIALLY DEFERRED
1973 INSERT INTO parent VALUES('key1');
1974 INSERT INTO parent VALUES('key2');
1975 INSERT INTO child1 VALUES('key1');
1976 INSERT INTO child2 VALUES('key2');
1980 do_test e_fkey-43.7 {
1981 catchsql { DELETE FROM parent WHERE x = 'key1' }
1982 } {1 {FOREIGN KEY constraint failed}}
1983 do_test e_fkey-43.8 {
1984 execsql { DELETE FROM parent WHERE x = 'key2' }
1986 do_test e_fkey-43.9 {
1988 } {1 {FOREIGN KEY constraint failed}}
1989 do_test e_fkey-43.10 {
1991 UPDATE child2 SET c = NULL;
1996 #-------------------------------------------------------------------------
1997 # Test SET NULL actions.
1999 # EVIDENCE-OF: R-03353-05327 If the configured action is "SET NULL",
2000 # then when a parent key is deleted (for ON DELETE SET NULL) or modified
2001 # (for ON UPDATE SET NULL), the child key columns of all rows in the
2002 # child table that mapped to the parent key are set to contain SQL NULL
2006 do_test e_fkey-44.1 {
2008 CREATE TABLE pA(x PRIMARY KEY);
2009 CREATE TABLE cA(c REFERENCES pA ON DELETE SET NULL);
2010 CREATE TABLE cB(c REFERENCES pA ON UPDATE SET NULL);
2012 INSERT INTO pA VALUES(X'ABCD');
2013 INSERT INTO pA VALUES(X'1234');
2014 INSERT INTO cA VALUES(X'ABCD');
2015 INSERT INTO cB VALUES(X'1234');
2018 do_test e_fkey-44.2 {
2020 DELETE FROM pA WHERE rowid = 1;
2021 SELECT quote(x) FROM pA;
2024 do_test e_fkey-44.3 {
2026 SELECT quote(c) FROM cA;
2029 do_test e_fkey-44.4 {
2031 UPDATE pA SET x = X'8765' WHERE rowid = 2;
2032 SELECT quote(x) FROM pA;
2035 do_test e_fkey-44.5 {
2036 execsql { SELECT quote(c) FROM cB }
2039 #-------------------------------------------------------------------------
2040 # Test SET DEFAULT actions.
2042 # EVIDENCE-OF: R-43054-54832 The "SET DEFAULT" actions are similar to
2043 # "SET NULL", except that each of the child key columns is set to
2044 # contain the columns default value instead of NULL.
2047 do_test e_fkey-45.1 {
2049 CREATE TABLE pA(x PRIMARY KEY);
2050 CREATE TABLE cA(c DEFAULT X'0000' REFERENCES pA ON DELETE SET DEFAULT);
2051 CREATE TABLE cB(c DEFAULT X'9999' REFERENCES pA ON UPDATE SET DEFAULT);
2053 INSERT INTO pA(rowid, x) VALUES(1, X'0000');
2054 INSERT INTO pA(rowid, x) VALUES(2, X'9999');
2055 INSERT INTO pA(rowid, x) VALUES(3, X'ABCD');
2056 INSERT INTO pA(rowid, x) VALUES(4, X'1234');
2058 INSERT INTO cA VALUES(X'ABCD');
2059 INSERT INTO cB VALUES(X'1234');
2062 do_test e_fkey-45.2 {
2064 DELETE FROM pA WHERE rowid = 3;
2065 SELECT quote(x) FROM pA ORDER BY rowid;
2067 } {X'0000' X'9999' X'1234'}
2068 do_test e_fkey-45.3 {
2069 execsql { SELECT quote(c) FROM cA }
2071 do_test e_fkey-45.4 {
2073 UPDATE pA SET x = X'8765' WHERE rowid = 4;
2074 SELECT quote(x) FROM pA ORDER BY rowid;
2076 } {X'0000' X'9999' X'8765'}
2077 do_test e_fkey-45.5 {
2078 execsql { SELECT quote(c) FROM cB }
2081 #-------------------------------------------------------------------------
2082 # Test ON DELETE CASCADE actions.
2084 # EVIDENCE-OF: R-61376-57267 A "CASCADE" action propagates the delete or
2085 # update operation on the parent key to each dependent child key.
2087 # EVIDENCE-OF: R-61809-62207 For an "ON DELETE CASCADE" action, this
2088 # means that each row in the child table that was associated with the
2089 # deleted parent row is also deleted.
2092 do_test e_fkey-46.1 {
2094 CREATE TABLE p1(a, b UNIQUE);
2095 CREATE TABLE c1(c REFERENCES p1(b) ON DELETE CASCADE, d);
2096 INSERT INTO p1 VALUES(NULL, NULL);
2097 INSERT INTO p1 VALUES(4, 4);
2098 INSERT INTO p1 VALUES(5, 5);
2099 INSERT INTO c1 VALUES(NULL, NULL);
2100 INSERT INTO c1 VALUES(4, 4);
2101 INSERT INTO c1 VALUES(5, 5);
2102 SELECT count(*) FROM c1;
2105 do_test e_fkey-46.2 {
2107 DELETE FROM p1 WHERE a = 4;
2108 SELECT d, c FROM c1;
2111 do_test e_fkey-46.3 {
2114 SELECT d, c FROM c1;
2117 do_test e_fkey-46.4 {
2118 execsql { SELECT * FROM p1 }
2122 #-------------------------------------------------------------------------
2123 # Test ON UPDATE CASCADE actions.
2125 # EVIDENCE-OF: R-13877-64542 For an "ON UPDATE CASCADE" action, it means
2126 # that the values stored in each dependent child key are modified to
2127 # match the new parent key values.
2129 # EVIDENCE-OF: R-61376-57267 A "CASCADE" action propagates the delete or
2130 # update operation on the parent key to each dependent child key.
2133 do_test e_fkey-47.1 {
2135 CREATE TABLE p1(a, b UNIQUE);
2136 CREATE TABLE c1(c REFERENCES p1(b) ON UPDATE CASCADE, d);
2137 INSERT INTO p1 VALUES(NULL, NULL);
2138 INSERT INTO p1 VALUES(4, 4);
2139 INSERT INTO p1 VALUES(5, 5);
2140 INSERT INTO c1 VALUES(NULL, NULL);
2141 INSERT INTO c1 VALUES(4, 4);
2142 INSERT INTO c1 VALUES(5, 5);
2143 SELECT count(*) FROM c1;
2146 do_test e_fkey-47.2 {
2148 UPDATE p1 SET b = 10 WHERE b = 5;
2149 SELECT d, c FROM c1;
2152 do_test e_fkey-47.3 {
2154 UPDATE p1 SET b = 11 WHERE b = 4;
2155 SELECT d, c FROM c1;
2158 do_test e_fkey-47.4 {
2160 UPDATE p1 SET b = 6 WHERE b IS NULL;
2161 SELECT d, c FROM c1;
2164 do_test e_fkey-46.5 {
2165 execsql { SELECT * FROM p1 }
2168 #-------------------------------------------------------------------------
2169 # EVIDENCE-OF: R-65058-57158
2171 # Test an example from the "ON DELETE and ON UPDATE Actions" section
2172 # of foreignkeys.html.
2175 do_test e_fkey-48.1 {
2177 CREATE TABLE artist(
2178 artistid INTEGER PRIMARY KEY,
2184 trackartist INTEGER REFERENCES artist(artistid) ON UPDATE CASCADE
2187 INSERT INTO artist VALUES(1, 'Dean Martin');
2188 INSERT INTO artist VALUES(2, 'Frank Sinatra');
2189 INSERT INTO track VALUES(11, 'That''s Amore', 1);
2190 INSERT INTO track VALUES(12, 'Christmas Blues', 1);
2191 INSERT INTO track VALUES(13, 'My Way', 2);
2194 do_test e_fkey-48.2 {
2196 UPDATE artist SET artistid = 100 WHERE artistname = 'Dean Martin';
2199 do_test e_fkey-48.3 {
2200 execsql { SELECT * FROM artist }
2201 } {2 {Frank Sinatra} 100 {Dean Martin}}
2202 do_test e_fkey-48.4 {
2203 execsql { SELECT * FROM track }
2204 } {11 {That's Amore} 100 12 {Christmas Blues} 100 13 {My Way} 2}
2207 #-------------------------------------------------------------------------
2208 # Verify that adding an FK action does not absolve the user of the
2209 # requirement not to violate the foreign key constraint.
2211 # EVIDENCE-OF: R-53968-51642 Configuring an ON UPDATE or ON DELETE
2212 # action does not mean that the foreign key constraint does not need to
2216 do_test e_fkey-49.1 {
2218 CREATE TABLE parent(a COLLATE nocase, b, c, PRIMARY KEY(c, a));
2219 CREATE TABLE child(d DEFAULT 'a', e, f DEFAULT 'c',
2220 FOREIGN KEY(f, d) REFERENCES parent ON UPDATE SET DEFAULT
2223 INSERT INTO parent VALUES('A', 'b', 'c');
2224 INSERT INTO parent VALUES('ONE', 'two', 'three');
2225 INSERT INTO child VALUES('one', 'two', 'three');
2228 do_test e_fkey-49.2 {
2231 UPDATE parent SET a = '' WHERE a = 'oNe';
2232 SELECT * FROM child;
2235 do_test e_fkey-49.3 {
2238 DELETE FROM parent WHERE a = 'A';
2239 SELECT * FROM parent;
2242 do_test e_fkey-49.4 {
2243 catchsql { UPDATE parent SET a = '' WHERE a = 'oNe' }
2244 } {1 {FOREIGN KEY constraint failed}}
2247 #-------------------------------------------------------------------------
2248 # EVIDENCE-OF: R-11856-19836
2250 # Test an example from the "ON DELETE and ON UPDATE Actions" section
2251 # of foreignkeys.html. This example shows that adding an "ON DELETE DEFAULT"
2252 # clause does not abrogate the need to satisfy the foreign key constraint
2255 # EVIDENCE-OF: R-28220-46694 For example, if an "ON DELETE SET DEFAULT"
2256 # action is configured, but there is no row in the parent table that
2257 # corresponds to the default values of the child key columns, deleting a
2258 # parent key while dependent child keys exist still causes a foreign key
2262 do_test e_fkey-50.1 {
2264 CREATE TABLE artist(
2265 artistid INTEGER PRIMARY KEY,
2271 trackartist INTEGER DEFAULT 0 REFERENCES artist(artistid) ON DELETE SET DEFAULT
2273 INSERT INTO artist VALUES(3, 'Sammy Davis Jr.');
2274 INSERT INTO track VALUES(14, 'Mr. Bojangles', 3);
2277 do_test e_fkey-50.2 {
2278 catchsql { DELETE FROM artist WHERE artistname = 'Sammy Davis Jr.' }
2279 } {1 {FOREIGN KEY constraint failed}}
2280 do_test e_fkey-50.3 {
2282 INSERT INTO artist VALUES(0, 'Unknown Artist');
2283 DELETE FROM artist WHERE artistname = 'Sammy Davis Jr.';
2286 do_test e_fkey-50.4 {
2287 execsql { SELECT * FROM artist }
2288 } {0 {Unknown Artist}}
2289 do_test e_fkey-50.5 {
2290 execsql { SELECT * FROM track }
2291 } {14 {Mr. Bojangles} 0}
2293 #-------------------------------------------------------------------------
2294 # EVIDENCE-OF: R-09564-22170
2296 # Check that the order of steps in an UPDATE or DELETE on a parent
2297 # table is as follows:
2299 # 1. Execute applicable BEFORE trigger programs,
2300 # 2. Check local (non foreign key) constraints,
2301 # 3. Update or delete the row in the parent table,
2302 # 4. Perform any required foreign key actions,
2303 # 5. Execute applicable AFTER trigger programs.
2306 do_test e_fkey-51.1 {
2307 proc maxparent {args} { db one {SELECT max(x) FROM parent} }
2308 db func maxparent maxparent
2311 CREATE TABLE parent(x PRIMARY KEY);
2313 CREATE TRIGGER bu BEFORE UPDATE ON parent BEGIN
2314 INSERT INTO parent VALUES(new.x-old.x);
2317 a DEFAULT (maxparent()) REFERENCES parent ON UPDATE SET DEFAULT
2319 CREATE TRIGGER au AFTER UPDATE ON parent BEGIN
2320 INSERT INTO parent VALUES(new.x+old.x);
2323 INSERT INTO parent VALUES(1);
2324 INSERT INTO child VALUES(1);
2327 do_test e_fkey-51.2 {
2329 UPDATE parent SET x = 22;
2330 SELECT * FROM parent ORDER BY rowid; SELECT 'xxx' ; SELECT a FROM child;
2333 do_test e_fkey-51.3 {
2337 INSERT INTO parent VALUES(-1);
2338 INSERT INTO child VALUES(-1);
2339 UPDATE parent SET x = 22;
2340 SELECT * FROM parent ORDER BY rowid; SELECT 'xxx' ; SELECT a FROM child;
2345 #-------------------------------------------------------------------------
2346 # Verify that ON UPDATE actions only actually take place if the parent key
2347 # is set to a new value that is distinct from the old value. The default
2348 # collation sequence and affinity are used to determine if the new value
2349 # is 'distinct' from the old or not.
2351 # EVIDENCE-OF: R-27383-10246 An ON UPDATE action is only taken if the
2352 # values of the parent key are modified so that the new parent key
2353 # values are not equal to the old.
2356 do_test e_fkey-52.1 {
2358 CREATE TABLE zeus(a INTEGER COLLATE NOCASE, b, PRIMARY KEY(a, b));
2359 CREATE TABLE apollo(c, d,
2360 FOREIGN KEY(c, d) REFERENCES zeus ON UPDATE CASCADE
2362 INSERT INTO zeus VALUES('abc', 'xyz');
2363 INSERT INTO apollo VALUES('ABC', 'xyz');
2366 UPDATE zeus SET a = 'aBc';
2367 SELECT * FROM apollo;
2370 do_test e_fkey-52.2 {
2372 UPDATE zeus SET a = 1, b = 1;
2373 SELECT * FROM apollo;
2376 do_test e_fkey-52.3 {
2378 UPDATE zeus SET a = 1, b = 1;
2379 SELECT typeof(c), c, typeof(d), d FROM apollo;
2381 } {integer 1 integer 1}
2382 do_test e_fkey-52.4 {
2384 UPDATE zeus SET a = '1';
2385 SELECT typeof(c), c, typeof(d), d FROM apollo;
2387 } {integer 1 integer 1}
2388 do_test e_fkey-52.5 {
2390 UPDATE zeus SET b = '1';
2391 SELECT typeof(c), c, typeof(d), d FROM apollo;
2393 } {integer 1 text 1}
2394 do_test e_fkey-52.6 {
2396 UPDATE zeus SET b = NULL;
2397 SELECT typeof(c), c, typeof(d), d FROM apollo;
2399 } {integer 1 null {}}
2401 #-------------------------------------------------------------------------
2402 # EVIDENCE-OF: R-35129-58141
2404 # Test an example from the "ON DELETE and ON UPDATE Actions" section
2405 # of foreignkeys.html. This example demonstrates that ON UPDATE actions
2406 # only take place if at least one parent key column is set to a value
2407 # that is distinct from its previous value.
2410 do_test e_fkey-53.1 {
2412 CREATE TABLE parent(x PRIMARY KEY);
2413 CREATE TABLE child(y REFERENCES parent ON UPDATE SET NULL);
2414 INSERT INTO parent VALUES('key');
2415 INSERT INTO child VALUES('key');
2418 do_test e_fkey-53.2 {
2420 UPDATE parent SET x = 'key';
2421 SELECT IFNULL(y, 'null') FROM child;
2424 do_test e_fkey-53.3 {
2426 UPDATE parent SET x = 'key2';
2427 SELECT IFNULL(y, 'null') FROM child;
2431 ###########################################################################
2432 ### SECTION 5: CREATE, ALTER and DROP TABLE commands
2433 ###########################################################################
2435 #-------------------------------------------------------------------------
2436 # Test that parent keys are not checked when tables are created.
2438 # EVIDENCE-OF: R-36018-21755 The parent key definitions of foreign key
2439 # constraints are not checked when a table is created.
2441 # EVIDENCE-OF: R-25384-39337 There is nothing stopping the user from
2442 # creating a foreign key definition that refers to a parent table that
2443 # does not exist, or to parent key columns that do not exist or are not
2444 # collectively bound by a PRIMARY KEY or UNIQUE constraint.
2446 # Child keys are checked to ensure all component columns exist. If parent
2447 # key columns are explicitly specified, SQLite checks to make sure there
2448 # are the same number of columns in the child and parent keys. (TODO: This
2449 # is tested but does not correspond to any testable statement.)
2451 # Also test that the above statements are true regardless of whether or not
2452 # foreign keys are enabled: "A CREATE TABLE command operates the same whether
2453 # or not foreign key constraints are enabled."
2455 # EVIDENCE-OF: R-08908-23439 A CREATE TABLE command operates the same
2456 # whether or not foreign key constraints are enabled.
2458 foreach {tn zCreateTbl lRes} {
2459 1 "CREATE TABLE t1(a, b REFERENCES t1)" {0 {}}
2460 2 "CREATE TABLE t1(a, b REFERENCES t2)" {0 {}}
2461 3 "CREATE TABLE t1(a, b, FOREIGN KEY(a,b) REFERENCES t1)" {0 {}}
2462 4 "CREATE TABLE t1(a, b, FOREIGN KEY(a,b) REFERENCES t2)" {0 {}}
2463 5 "CREATE TABLE t1(a, b, FOREIGN KEY(a,b) REFERENCES t2)" {0 {}}
2464 6 "CREATE TABLE t1(a, b, FOREIGN KEY(a,b) REFERENCES t2(n,d))" {0 {}}
2465 7 "CREATE TABLE t1(a, b, FOREIGN KEY(a,b) REFERENCES t1(a,b))" {0 {}}
2467 A "CREATE TABLE t1(a, b, FOREIGN KEY(c,b) REFERENCES t2)"
2468 {1 {unknown column "c" in foreign key definition}}
2469 B "CREATE TABLE t1(a, b, FOREIGN KEY(c,b) REFERENCES t2(d))"
2470 {1 {number of columns in foreign key does not match the number of columns in the referenced table}}
2472 do_test e_fkey-54.$tn.off {
2474 execsql {PRAGMA foreign_keys = OFF}
2475 catchsql $zCreateTbl
2477 do_test e_fkey-54.$tn.on {
2479 execsql {PRAGMA foreign_keys = ON}
2480 catchsql $zCreateTbl
2484 #-------------------------------------------------------------------------
2485 # EVIDENCE-OF: R-47952-62498 It is not possible to use the "ALTER TABLE
2486 # ... ADD COLUMN" syntax to add a column that includes a REFERENCES
2487 # clause, unless the default value of the new column is NULL. Attempting
2488 # to do so returns an error.
2490 proc test_efkey_6 {tn zAlter isError} {
2493 do_test e_fkey-56.$tn.1 "
2494 execsql { CREATE TABLE tbl(a, b) }
2495 [list catchsql $zAlter]
2496 " [lindex {{0 {}} {1 {Cannot add a REFERENCES column with non-NULL default value}}} $isError]
2500 test_efkey_6 1 "ALTER TABLE tbl ADD COLUMN c REFERENCES xx" 0
2501 test_efkey_6 2 "ALTER TABLE tbl ADD COLUMN c DEFAULT NULL REFERENCES xx" 0
2502 test_efkey_6 3 "ALTER TABLE tbl ADD COLUMN c DEFAULT 0 REFERENCES xx" 1
2504 #-------------------------------------------------------------------------
2505 # Test that ALTER TABLE adjusts REFERENCES clauses when the parent table
2508 # EVIDENCE-OF: R-47080-02069 If an "ALTER TABLE ... RENAME TO" command
2509 # is used to rename a table that is the parent table of one or more
2510 # foreign key constraints, the definitions of the foreign key
2511 # constraints are modified to refer to the parent table by its new name
2513 # Test that these adjustments are visible in the sqlite_master table.
2515 # EVIDENCE-OF: R-63827-54774 The text of the child CREATE TABLE
2516 # statement or statements stored in the sqlite_master table are modified
2517 # to reflect the new parent table name.
2519 do_test e_fkey-56.1 {
2522 CREATE TABLE 'p 1 "parent one"'(a REFERENCES 'p 1 "parent one"', b, PRIMARY KEY(b));
2524 CREATE TABLE c1(c, d REFERENCES 'p 1 "parent one"' ON UPDATE CASCADE);
2525 CREATE TABLE c2(e, f, FOREIGN KEY(f) REFERENCES 'p 1 "parent one"' ON UPDATE CASCADE);
2526 CREATE TABLE c3(e, 'f col 2', FOREIGN KEY('f col 2') REFERENCES 'p 1 "parent one"' ON UPDATE CASCADE);
2528 INSERT INTO 'p 1 "parent one"' VALUES(1, 1);
2529 INSERT INTO c1 VALUES(1, 1);
2530 INSERT INTO c2 VALUES(1, 1);
2531 INSERT INTO c3 VALUES(1, 1);
2533 -- CREATE TABLE q(a, b, PRIMARY KEY(b));
2536 do_test e_fkey-56.2 {
2537 execsql { ALTER TABLE 'p 1 "parent one"' RENAME TO p }
2539 do_test e_fkey-56.3 {
2541 UPDATE p SET a = 'xxx', b = 'xxx';
2547 } {xxx xxx 1 xxx 1 xxx 1 xxx}
2548 do_test e_fkey-56.4 {
2549 execsql { SELECT sql FROM sqlite_master WHERE type = 'table'}
2551 {CREATE TABLE "p"(a REFERENCES "p", b, PRIMARY KEY(b))} \
2552 {CREATE TABLE c1(c, d REFERENCES "p" ON UPDATE CASCADE)} \
2553 {CREATE TABLE c2(e, f, FOREIGN KEY(f) REFERENCES "p" ON UPDATE CASCADE)} \
2554 {CREATE TABLE c3(e, 'f col 2', FOREIGN KEY('f col 2') REFERENCES "p" ON UPDATE CASCADE)} \
2557 #-------------------------------------------------------------------------
2558 # Check that a DROP TABLE does an implicit DELETE FROM. Which does not
2559 # cause any triggers to fire, but does fire foreign key actions.
2561 # EVIDENCE-OF: R-14208-23986 If foreign key constraints are enabled when
2562 # it is prepared, the DROP TABLE command performs an implicit DELETE to
2563 # remove all rows from the table before dropping it.
2565 # EVIDENCE-OF: R-11078-03945 The implicit DELETE does not cause any SQL
2566 # triggers to fire, but may invoke foreign key actions or constraint
2569 do_test e_fkey-57.1 {
2572 CREATE TABLE p(a, b, PRIMARY KEY(a, b));
2574 CREATE TABLE c1(c, d, FOREIGN KEY(c, d) REFERENCES p ON DELETE SET NULL);
2575 CREATE TABLE c2(c, d, FOREIGN KEY(c, d) REFERENCES p ON DELETE SET DEFAULT);
2576 CREATE TABLE c3(c, d, FOREIGN KEY(c, d) REFERENCES p ON DELETE CASCADE);
2577 CREATE TABLE c4(c, d, FOREIGN KEY(c, d) REFERENCES p ON DELETE RESTRICT);
2578 CREATE TABLE c5(c, d, FOREIGN KEY(c, d) REFERENCES p ON DELETE NO ACTION);
2580 CREATE TABLE c6(c, d,
2581 FOREIGN KEY(c, d) REFERENCES p ON DELETE RESTRICT
2582 DEFERRABLE INITIALLY DEFERRED
2584 CREATE TABLE c7(c, d,
2585 FOREIGN KEY(c, d) REFERENCES p ON DELETE NO ACTION
2586 DEFERRABLE INITIALLY DEFERRED
2589 CREATE TABLE log(msg);
2590 CREATE TRIGGER tt AFTER DELETE ON p BEGIN
2591 INSERT INTO log VALUES('delete ' || old.rowid);
2596 do_test e_fkey-57.2 {
2598 INSERT INTO p VALUES('a', 'b');
2599 INSERT INTO c1 VALUES('a', 'b');
2600 INSERT INTO c2 VALUES('a', 'b');
2601 INSERT INTO c3 VALUES('a', 'b');
2607 do_test e_fkey-57.3 {
2608 execsql { SELECT * FROM c2 }
2610 do_test e_fkey-57.4 {
2611 execsql { SELECT * FROM c3 }
2613 do_test e_fkey-57.5 {
2614 execsql { SELECT * FROM log }
2616 do_test e_fkey-57.6 {
2619 do_test e_fkey-57.7 {
2628 #-------------------------------------------------------------------------
2629 # If an IMMEDIATE foreign key fails as a result of a DROP TABLE, the
2630 # DROP TABLE command fails.
2632 # EVIDENCE-OF: R-32768-47925 If an immediate foreign key constraint is
2633 # violated, the DROP TABLE statement fails and the table is not dropped.
2635 do_test e_fkey-58.1 {
2641 execsql { INSERT INTO c5 VALUES('a', 'b') }
2642 catchsql { DROP TABLE p }
2643 } {1 {FOREIGN KEY constraint failed}}
2644 do_test e_fkey-58.2 {
2645 execsql { SELECT * FROM p }
2647 do_test e_fkey-58.3 {
2652 } {1 {FOREIGN KEY constraint failed}}
2653 do_test e_fkey-58.4 {
2661 #-------------------------------------------------------------------------
2662 # If a DEFERRED foreign key fails as a result of a DROP TABLE, attempting
2663 # to commit the transaction fails unless the violation is fixed.
2665 # EVIDENCE-OF: R-05903-08460 If a deferred foreign key constraint is
2666 # violated, then an error is reported when the user attempts to commit
2667 # the transaction if the foreign key constraint violations still exist
2670 do_test e_fkey-59.1 {
2672 DELETE FROM c1 ; DELETE FROM c2 ; DELETE FROM c3 ;
2673 DELETE FROM c4 ; DELETE FROM c5 ; DELETE FROM c6 ;
2677 do_test e_fkey-59.2 {
2678 execsql { INSERT INTO c7 VALUES('a', 'b') }
2684 do_test e_fkey-59.3 {
2686 } {1 {FOREIGN KEY constraint failed}}
2687 do_test e_fkey-59.4 {
2688 execsql { CREATE TABLE p(a, b, PRIMARY KEY(a, b)) }
2690 } {1 {FOREIGN KEY constraint failed}}
2691 do_test e_fkey-59.5 {
2692 execsql { INSERT INTO p VALUES('a', 'b') }
2696 #-------------------------------------------------------------------------
2697 # Any "foreign key mismatch" errors encountered while running an implicit
2698 # "DELETE FROM tbl" are ignored.
2700 # EVIDENCE-OF: R-57242-37005 Any "foreign key mismatch" errors
2701 # encountered as part of an implicit DELETE are ignored.
2704 do_test e_fkey-60.1 {
2706 PRAGMA foreign_keys = OFF;
2708 CREATE TABLE p(a PRIMARY KEY, b REFERENCES nosuchtable);
2709 CREATE TABLE c1(c, d, FOREIGN KEY(c, d) REFERENCES a);
2710 CREATE TABLE c2(c REFERENCES p(b), d);
2711 CREATE TABLE c3(c REFERENCES p ON DELETE SET NULL, d);
2713 INSERT INTO p VALUES(1, 2);
2714 INSERT INTO c1 VALUES(1, 2);
2715 INSERT INTO c2 VALUES(1, 2);
2716 INSERT INTO c3 VALUES(1, 2);
2719 do_test e_fkey-60.2 {
2720 execsql { PRAGMA foreign_keys = ON }
2721 catchsql { DELETE FROM p }
2722 } {1 {no such table: main.nosuchtable}}
2723 do_test e_fkey-60.3 {
2731 do_test e_fkey-60.4 {
2732 execsql { CREATE TABLE nosuchtable(x PRIMARY KEY) }
2733 catchsql { DELETE FROM p }
2734 } {1 {foreign key mismatch - "c2" referencing "p"}}
2735 do_test e_fkey-60.5 {
2736 execsql { DROP TABLE c1 }
2737 catchsql { DELETE FROM p }
2738 } {1 {foreign key mismatch - "c2" referencing "p"}}
2739 do_test e_fkey-60.6 {
2740 execsql { DROP TABLE c2 }
2741 execsql { DELETE FROM p }
2744 #-------------------------------------------------------------------------
2745 # Test that the special behaviors of ALTER and DROP TABLE are only
2746 # activated when foreign keys are enabled. Special behaviors are:
2748 # 1. ADD COLUMN not allowing a REFERENCES clause with a non-NULL
2750 # 2. Modifying foreign key definitions when a parent table is RENAMEd.
2751 # 3. Running an implicit DELETE FROM command as part of DROP TABLE.
2753 # EVIDENCE-OF: R-54142-41346 The properties of the DROP TABLE and ALTER
2754 # TABLE commands described above only apply if foreign keys are enabled.
2756 do_test e_fkey-61.1.1 {
2758 execsql { CREATE TABLE t1(a, b) }
2759 catchsql { ALTER TABLE t1 ADD COLUMN c DEFAULT 'xxx' REFERENCES t2 }
2760 } {1 {Cannot add a REFERENCES column with non-NULL default value}}
2761 do_test e_fkey-61.1.2 {
2762 execsql { PRAGMA foreign_keys = OFF }
2763 execsql { ALTER TABLE t1 ADD COLUMN c DEFAULT 'xxx' REFERENCES t2 }
2764 execsql { SELECT sql FROM sqlite_master WHERE name = 't1' }
2765 } {{CREATE TABLE t1(a, b, c DEFAULT 'xxx' REFERENCES t2)}}
2766 do_test e_fkey-61.1.3 {
2767 execsql { PRAGMA foreign_keys = ON }
2770 do_test e_fkey-61.2.1 {
2773 CREATE TABLE p(a UNIQUE);
2774 CREATE TABLE c(b REFERENCES p(a));
2776 ALTER TABLE p RENAME TO parent;
2777 SELECT sql FROM sqlite_master WHERE name = 'c';
2780 } {{CREATE TABLE c(b REFERENCES "parent"(a))}}
2781 do_test e_fkey-61.2.2 {
2783 PRAGMA foreign_keys = OFF;
2784 ALTER TABLE p RENAME TO parent;
2785 SELECT sql FROM sqlite_master WHERE name = 'c';
2787 } {{CREATE TABLE c(b REFERENCES p(a))}}
2788 do_test e_fkey-61.2.3 {
2789 execsql { PRAGMA foreign_keys = ON }
2792 do_test e_fkey-61.3.1 {
2795 CREATE TABLE p(a UNIQUE);
2796 CREATE TABLE c(b REFERENCES p(a) ON DELETE SET NULL);
2797 INSERT INTO p VALUES('x');
2798 INSERT INTO c VALUES('x');
2805 do_test e_fkey-61.3.2 {
2807 PRAGMA foreign_keys = OFF;
2812 do_test e_fkey-61.3.3 {
2813 execsql { PRAGMA foreign_keys = ON }
2816 ###########################################################################
2817 ### SECTION 6: Limits and Unsupported Features
2818 ###########################################################################
2820 #-------------------------------------------------------------------------
2821 # Test that MATCH clauses are parsed, but SQLite treats every foreign key
2822 # constraint as if it were "MATCH SIMPLE".
2824 # EVIDENCE-OF: R-24728-13230 SQLite parses MATCH clauses (i.e. does not
2825 # report a syntax error if you specify one), but does not enforce them.
2827 # EVIDENCE-OF: R-24450-46174 All foreign key constraints in SQLite are
2828 # handled as if MATCH SIMPLE were specified.
2830 foreach zMatch [list SIMPLE PARTIAL FULL Simple parTIAL FuLL ] {
2832 do_test e_fkey-62.$zMatch.1 {
2834 CREATE TABLE p(a, b, c, PRIMARY KEY(b, c));
2835 CREATE TABLE c(d, e, f, FOREIGN KEY(e, f) REFERENCES p MATCH $zMatch);
2838 do_test e_fkey-62.$zMatch.2 {
2839 execsql { INSERT INTO p VALUES(1, 2, 3) }
2841 # MATCH SIMPLE behavior: Allow any child key that contains one or more
2842 # NULL value to be inserted. Non-NULL values do not have to map to any
2843 # parent key values, so long as at least one field of the child key is
2845 execsql { INSERT INTO c VALUES('w', 2, 3) }
2846 execsql { INSERT INTO c VALUES('x', 'x', NULL) }
2847 execsql { INSERT INTO c VALUES('y', NULL, 'x') }
2848 execsql { INSERT INTO c VALUES('z', NULL, NULL) }
2850 # Check that the FK is enforced properly if there are no NULL values
2851 # in the child key columns.
2852 catchsql { INSERT INTO c VALUES('a', 2, 4) }
2853 } {1 {FOREIGN KEY constraint failed}}
2856 #-------------------------------------------------------------------------
2857 # Test that SQLite does not support the SET CONSTRAINT statement. And
2858 # that it is possible to create both immediate and deferred constraints.
2860 # EVIDENCE-OF: R-21599-16038 In SQLite, a foreign key constraint is
2861 # permanently marked as deferred or immediate when it is created.
2864 do_test e_fkey-62.1 {
2865 catchsql { SET CONSTRAINTS ALL IMMEDIATE }
2866 } {1 {near "SET": syntax error}}
2867 do_test e_fkey-62.2 {
2868 catchsql { SET CONSTRAINTS ALL DEFERRED }
2869 } {1 {near "SET": syntax error}}
2871 do_test e_fkey-62.3 {
2873 CREATE TABLE p(a, b, PRIMARY KEY(a, b));
2874 CREATE TABLE cd(c, d,
2875 FOREIGN KEY(c, d) REFERENCES p DEFERRABLE INITIALLY DEFERRED);
2876 CREATE TABLE ci(c, d,
2877 FOREIGN KEY(c, d) REFERENCES p DEFERRABLE INITIALLY IMMEDIATE);
2881 do_test e_fkey-62.4 {
2882 catchsql { INSERT INTO ci VALUES('x', 'y') }
2883 } {1 {FOREIGN KEY constraint failed}}
2884 do_test e_fkey-62.5 {
2885 catchsql { INSERT INTO cd VALUES('x', 'y') }
2887 do_test e_fkey-62.6 {
2889 } {1 {FOREIGN KEY constraint failed}}
2890 do_test e_fkey-62.7 {
2897 #-------------------------------------------------------------------------
2898 # Test that the maximum recursion depth of foreign key action programs is
2899 # governed by the SQLITE_MAX_TRIGGER_DEPTH and SQLITE_LIMIT_TRIGGER_DEPTH
2902 # EVIDENCE-OF: R-42264-30503 The SQLITE_MAX_TRIGGER_DEPTH and
2903 # SQLITE_LIMIT_TRIGGER_DEPTH settings determine the maximum allowable
2904 # depth of trigger program recursion. For the purposes of these limits,
2905 # foreign key actions are considered trigger programs.
2907 proc test_on_delete_recursion {limit} {
2911 CREATE TABLE t0(a PRIMARY KEY, b);
2912 INSERT INTO t0 VALUES('x0', NULL);
2914 for {set i 1} {$i <= $limit} {incr i} {
2917 a PRIMARY KEY, b REFERENCES t[expr $i-1] ON DELETE CASCADE
2919 INSERT INTO t$i VALUES('x$i', 'x[expr $i-1]');
2925 SELECT count(*) FROM t$limit;
2928 proc test_on_update_recursion {limit} {
2932 CREATE TABLE t0(a PRIMARY KEY);
2933 INSERT INTO t0 VALUES('xxx');
2935 for {set i 1} {$i <= $limit} {incr i} {
2939 CREATE TABLE t$i (a PRIMARY KEY REFERENCES t$j ON UPDATE CASCADE);
2940 INSERT INTO t$i VALUES('xxx');
2945 UPDATE t0 SET a = 'yyy';
2946 SELECT NOT (a='yyy') FROM t$limit;
2950 # If the current build was created using clang with the -fsanitize=address
2951 # switch, then the library uses considerably more stack space than usual.
2952 # So much more, that some of the following tests cause stack overflows
2953 # if they are run under this configuration.
2955 if {[clang_sanitize_address]==0} {
2956 do_test e_fkey-63.1.1 {
2957 test_on_delete_recursion $SQLITE_MAX_TRIGGER_DEPTH
2959 do_test e_fkey-63.1.2 {
2960 test_on_delete_recursion [expr $SQLITE_MAX_TRIGGER_DEPTH+1]
2961 } {1 {too many levels of trigger recursion}}
2962 do_test e_fkey-63.1.3 {
2963 sqlite3_limit db SQLITE_LIMIT_TRIGGER_DEPTH 5
2964 test_on_delete_recursion 5
2966 do_test e_fkey-63.1.4 {
2967 test_on_delete_recursion 6
2968 } {1 {too many levels of trigger recursion}}
2969 do_test e_fkey-63.1.5 {
2970 sqlite3_limit db SQLITE_LIMIT_TRIGGER_DEPTH 1000000
2972 do_test e_fkey-63.2.1 {
2973 test_on_update_recursion $SQLITE_MAX_TRIGGER_DEPTH
2975 do_test e_fkey-63.2.2 {
2976 test_on_update_recursion [expr $SQLITE_MAX_TRIGGER_DEPTH+1]
2977 } {1 {too many levels of trigger recursion}}
2978 do_test e_fkey-63.2.3 {
2979 sqlite3_limit db SQLITE_LIMIT_TRIGGER_DEPTH 5
2980 test_on_update_recursion 5
2982 do_test e_fkey-63.2.4 {
2983 test_on_update_recursion 6
2984 } {1 {too many levels of trigger recursion}}
2985 do_test e_fkey-63.2.5 {
2986 sqlite3_limit db SQLITE_LIMIT_TRIGGER_DEPTH 1000000
2990 #-------------------------------------------------------------------------
2991 # The setting of the recursive_triggers pragma does not affect foreign
2994 # EVIDENCE-OF: R-44355-00270 The PRAGMA recursive_triggers setting does
2995 # not affect the operation of foreign key actions.
2997 foreach recursive_triggers_setting [list 0 1 ON OFF] {
2999 execsql "PRAGMA recursive_triggers = $recursive_triggers_setting"
3001 do_test e_fkey-64.$recursive_triggers_setting.1 {
3003 CREATE TABLE t1(a PRIMARY KEY, b REFERENCES t1 ON DELETE CASCADE);
3004 INSERT INTO t1 VALUES(1, NULL);
3005 INSERT INTO t1 VALUES(2, 1);
3006 INSERT INTO t1 VALUES(3, 2);
3007 INSERT INTO t1 VALUES(4, 3);
3008 INSERT INTO t1 VALUES(5, 4);
3009 SELECT count(*) FROM t1;
3012 do_test e_fkey-64.$recursive_triggers_setting.2 {
3013 execsql { SELECT count(*) FROM t1 WHERE a = 1 }
3015 do_test e_fkey-64.$recursive_triggers_setting.3 {
3017 DELETE FROM t1 WHERE a = 1;
3018 SELECT count(*) FROM t1;