/ Hex Artifact Content
Login

Artifact 5106b8fb0832c9b6d90541c98577494b3057254f:


0000: 23 20 32 30 30 39 20 4f 63 74 6f 62 65 72 20 37  # 2009 October 7
0010: 0a 23 0a 23 20 54 68 65 20 61 75 74 68 6f 72 20  .#.# The author 
0020: 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
0030: 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
0040: 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
0050: 63 65 20 6f 66 0a 23 20 61 20 6c 65 67 61 6c 20  ce of.# a legal 
0060: 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
0070: 61 20 62 6c 65 73 73 69 6e 67 3a 0a 23 0a 23 20  a blessing:.#.# 
0080: 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
0090: 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
00a0: 0a 23 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  .#    May you fi
00b0: 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
00c0: 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
00d0: 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
00e0: 23 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  #    May you sha
00f0: 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
0100: 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
0110: 6e 20 79 6f 75 20 67 69 76 65 2e 0a 23 0a 23 2a  n you give..#.#*
0120: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0130: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0140: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0150: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0160: 2a 2a 2a 2a 2a 2a 0a 23 0a 23 20 54 68 69 73 20  ******.#.# This 
0170: 66 69 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20  file implements 
0180: 74 65 73 74 73 20 74 6f 20 76 65 72 69 66 79 20  tests to verify 
0190: 74 68 65 20 22 74 65 73 74 61 62 6c 65 20 73 74  the "testable st
01a0: 61 74 65 6d 65 6e 74 73 22 20 69 6e 20 74 68 65  atements" in the
01b0: 0a 23 20 66 6f 72 65 69 67 6e 6b 65 79 73 2e 69  .# foreignkeys.i
01c0: 6e 20 64 6f 63 75 6d 65 6e 74 2e 0a 23 0a 0a 73  n document..#..s
01d0: 65 74 20 74 65 73 74 64 69 72 20 5b 66 69 6c 65  et testdir [file
01e0: 20 64 69 72 6e 61 6d 65 20 24 61 72 67 76 30 5d   dirname $argv0]
01f0: 0a 73 6f 75 72 63 65 20 24 74 65 73 74 64 69 72  .source $testdir
0200: 2f 74 65 73 74 65 72 2e 74 63 6c 0a 69 66 63 61  /tester.tcl.ifca
0210: 70 61 62 6c 65 20 7b 21 66 6f 72 65 69 67 6e 6b  pable {!foreignk
0220: 65 79 7d 20 7b 20 66 69 6e 69 73 68 5f 74 65 73  ey} { finish_tes
0230: 74 20 3b 20 72 65 74 75 72 6e 20 7d 0a 0a 65 78  t ; return }..ex
0240: 65 63 73 71 6c 20 22 50 52 41 47 4d 41 20 66 6f  ecsql "PRAGMA fo
0250: 72 65 69 67 6e 5f 6b 65 79 73 20 3d 20 4f 4e 22  reign_keys = ON"
0260: 0a 0a 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ..##############
0270: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
0280: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
0290: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
02a0: 23 23 23 23 23 23 23 23 23 23 23 23 23 0a 23 23  #############.##
02b0: 23 20 53 45 43 54 49 4f 4e 20 34 2e 33 3a 20 4f  # SECTION 4.3: O
02c0: 4e 20 44 45 4c 45 54 45 20 61 6e 64 20 4f 4e 20  N DELETE and ON 
02d0: 55 50 44 41 54 45 20 41 63 74 69 6f 6e 73 0a 23  UPDATE Actions.#
02e0: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
02f0: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
0300: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
0310: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
0320: 23 23 23 23 23 23 23 23 23 23 0a 0a 23 2d 2d 2d  ##########..#---
0330: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0340: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0350: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0360: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0370: 2d 2d 2d 2d 2d 2d 0a 23 20 2f 2a 20 45 56 3a 20  ------.# /* EV: 
0380: 52 2d 34 38 32 37 30 2d 34 34 32 38 32 20 2a 2f  R-48270-44282 */
0390: 0a 23 0a 23 20 54 65 73 74 20 74 68 61 74 20 63  .#.# Test that c
03a0: 6f 6e 66 69 67 75 72 65 64 20 4f 4e 20 44 45 4c  onfigured ON DEL
03b0: 45 54 45 20 61 6e 64 20 4f 4e 20 55 50 44 41 54  ETE and ON UPDAT
03c0: 45 20 61 63 74 69 6f 6e 73 20 74 61 6b 65 20 70  E actions take p
03d0: 6c 61 63 65 20 77 68 65 6e 0a 23 20 64 65 6c 65  lace when.# dele
03e0: 74 69 6e 67 20 6f 72 20 6d 6f 64 69 66 79 69 6e  ting or modifyin
03f0: 67 20 72 6f 77 73 20 6f 66 20 74 68 65 20 70 61  g rows of the pa
0400: 72 65 6e 74 20 74 61 62 6c 65 2c 20 72 65 73 70  rent table, resp
0410: 65 63 74 69 76 65 6c 79 2e 0a 23 0a 23 20 2f 2a  ectively..#.# /*
0420: 20 45 56 3a 20 52 2d 34 38 31 32 34 2d 36 33 32   EV: R-48124-632
0430: 32 35 20 2a 2f 0a 23 0a 23 20 54 65 73 74 20 74  25 */.#.# Test t
0440: 68 61 74 20 61 20 73 69 6e 67 6c 65 20 46 4b 20  hat a single FK 
0450: 63 6f 6e 73 74 72 61 69 6e 74 20 6d 61 79 20 68  constraint may h
0460: 61 76 65 20 64 69 66 66 65 72 65 6e 74 20 61 63  ave different ac
0470: 74 69 6f 6e 73 20 63 6f 6e 66 69 67 75 72 65 64  tions configured
0480: 0a 23 20 66 6f 72 20 4f 4e 20 44 45 4c 45 54 45  .# for ON DELETE
0490: 20 61 6e 64 20 4f 4e 20 55 50 44 41 54 45 2e 0a   and ON UPDATE..
04a0: 23 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79  #.do_test e_fkey
04b0: 2d 31 36 2e 31 20 7b 0a 20 20 65 78 65 63 73 71  -16.1 {.  execsq
04c0: 6c 20 7b 0a 20 20 20 20 43 52 45 41 54 45 20 54  l {.    CREATE T
04d0: 41 42 4c 45 20 70 28 61 2c 20 62 20 50 52 49 4d  ABLE p(a, b PRIM
04e0: 41 52 59 20 4b 45 59 2c 20 63 29 3b 0a 20 20 20  ARY KEY, c);.   
04f0: 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 63 31   CREATE TABLE c1
0500: 28 64 2c 20 65 2c 20 66 20 44 45 46 41 55 4c 54  (d, e, f DEFAULT
0510: 20 27 6b 30 27 20 52 45 46 45 52 45 4e 43 45 53   'k0' REFERENCES
0520: 20 70 20 0a 20 20 20 20 20 20 4f 4e 20 55 50 44   p .      ON UPD
0530: 41 54 45 20 53 45 54 20 44 45 46 41 55 4c 54 0a  ATE SET DEFAULT.
0540: 20 20 20 20 20 20 4f 4e 20 44 45 4c 45 54 45 20        ON DELETE 
0550: 53 45 54 20 4e 55 4c 4c 0a 20 20 20 20 29 3b 0a  SET NULL.    );.
0560: 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f  .    INSERT INTO
0570: 20 70 20 56 41 4c 55 45 53 28 30 2c 20 27 6b 30   p VALUES(0, 'k0
0580: 27 2c 20 27 27 29 3b 0a 20 20 20 20 49 4e 53 45  ', '');.    INSE
0590: 52 54 20 49 4e 54 4f 20 70 20 56 41 4c 55 45 53  RT INTO p VALUES
05a0: 28 31 2c 20 27 6b 31 27 2c 20 27 49 27 29 3b 0a  (1, 'k1', 'I');.
05b0: 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20      INSERT INTO 
05c0: 70 20 56 41 4c 55 45 53 28 32 2c 20 27 6b 32 27  p VALUES(2, 'k2'
05d0: 2c 20 27 49 49 27 29 3b 0a 20 20 20 20 49 4e 53  , 'II');.    INS
05e0: 45 52 54 20 49 4e 54 4f 20 70 20 56 41 4c 55 45  ERT INTO p VALUE
05f0: 53 28 33 2c 20 27 6b 33 27 2c 20 27 49 49 49 27  S(3, 'k3', 'III'
0600: 29 3b 0a 0a 20 20 20 20 49 4e 53 45 52 54 20 49  );..    INSERT I
0610: 4e 54 4f 20 63 31 20 56 41 4c 55 45 53 28 31 2c  NTO c1 VALUES(1,
0620: 20 27 78 78 27 2c 20 27 6b 31 27 29 3b 0a 20 20   'xx', 'k1');.  
0630: 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 63 31    INSERT INTO c1
0640: 20 56 41 4c 55 45 53 28 32 2c 20 27 78 78 27 2c   VALUES(2, 'xx',
0650: 20 27 6b 32 27 29 3b 0a 20 20 20 20 49 4e 53 45   'k2');.    INSE
0660: 52 54 20 49 4e 54 4f 20 63 31 20 56 41 4c 55 45  RT INTO c1 VALUE
0670: 53 28 33 2c 20 27 78 78 27 2c 20 27 6b 33 27 29  S(3, 'xx', 'k3')
0680: 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a 64 6f 5f 74 65  ;.  }.} {}.do_te
0690: 73 74 20 65 5f 66 6b 65 79 2d 31 36 2e 32 20 7b  st e_fkey-16.2 {
06a0: 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20  .  execsql {.   
06b0: 20 55 50 44 41 54 45 20 70 20 53 45 54 20 62 20   UPDATE p SET b 
06c0: 3d 20 27 6b 34 27 20 57 48 45 52 45 20 61 20 3d  = 'k4' WHERE a =
06d0: 20 31 3b 0a 20 20 20 20 53 45 4c 45 43 54 20 2a   1;.    SELECT *
06e0: 20 46 52 4f 4d 20 63 31 3b 0a 20 20 7d 0a 7d 20   FROM c1;.  }.} 
06f0: 7b 31 20 78 78 20 6b 30 20 32 20 78 78 20 6b 32  {1 xx k0 2 xx k2
0700: 20 33 20 78 78 20 6b 33 7d 0a 64 6f 5f 74 65 73   3 xx k3}.do_tes
0710: 74 20 65 5f 66 6b 65 79 2d 31 36 2e 33 20 7b 0a  t e_fkey-16.3 {.
0720: 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20    execsql {.    
0730: 44 45 4c 45 54 45 20 46 52 4f 4d 20 70 20 57 48  DELETE FROM p WH
0740: 45 52 45 20 61 20 3d 20 32 3b 0a 20 20 20 20 53  ERE a = 2;.    S
0750: 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 63 31 3b  ELECT * FROM c1;
0760: 0a 20 20 7d 0a 7d 20 7b 31 20 78 78 20 6b 30 20  .  }.} {1 xx k0 
0770: 32 20 78 78 20 7b 7d 20 33 20 78 78 20 6b 33 7d  2 xx {} 3 xx k3}
0780: 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d  .do_test e_fkey-
0790: 31 36 2e 34 20 7b 0a 20 20 65 78 65 63 73 71 6c  16.4 {.  execsql
07a0: 20 7b 0a 20 20 20 20 43 52 45 41 54 45 20 55 4e   {.    CREATE UN
07b0: 49 51 55 45 20 49 4e 44 45 58 20 70 69 20 4f 4e  IQUE INDEX pi ON
07c0: 20 70 28 63 29 3b 0a 20 20 20 20 52 45 50 4c 41   p(c);.    REPLA
07d0: 43 45 20 49 4e 54 4f 20 70 20 56 41 4c 55 45 53  CE INTO p VALUES
07e0: 28 35 2c 20 27 6b 35 27 2c 20 27 49 49 49 27 29  (5, 'k5', 'III')
07f0: 3b 0a 20 20 20 20 53 45 4c 45 43 54 20 2a 20 46  ;.    SELECT * F
0800: 52 4f 4d 20 63 31 3b 0a 20 20 7d 0a 7d 20 7b 31  ROM c1;.  }.} {1
0810: 20 78 78 20 6b 30 20 32 20 78 78 20 7b 7d 20 33   xx k0 2 xx {} 3
0820: 20 78 78 20 7b 7d 7d 0a 0a 23 2d 2d 2d 2d 2d 2d   xx {}}..#------
0830: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0840: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0850: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0860: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0870: 2d 2d 2d 0a 23 20 2f 2a 20 45 56 3a 20 52 2d 33  ---.# /* EV: R-3
0880: 33 33 32 36 2d 34 35 32 35 32 20 2a 2f 0a 23 0a  3326-45252 */.#.
0890: 23 20 45 61 63 68 20 66 6f 72 65 69 67 6e 20 6b  # Each foreign k
08a0: 65 79 20 69 6e 20 74 68 65 20 73 79 73 74 65 6d  ey in the system
08b0: 20 68 61 73 20 61 6e 20 4f 4e 20 55 50 44 41 54   has an ON UPDAT
08c0: 45 20 61 6e 64 20 4f 4e 20 44 45 4c 45 54 45 20  E and ON DELETE 
08d0: 61 63 74 69 6f 6e 2c 0a 23 20 65 69 74 68 65 72  action,.# either
08e0: 20 22 4e 4f 20 41 43 54 49 4f 4e 22 2c 20 22 52   "NO ACTION", "R
08f0: 45 53 54 52 49 43 54 22 2c 20 22 53 45 54 20 4e  ESTRICT", "SET N
0900: 55 4c 4c 22 2c 20 22 53 45 54 20 44 45 46 41 55  ULL", "SET DEFAU
0910: 4c 54 22 20 6f 72 20 22 43 41 53 43 41 44 45 22  LT" or "CASCADE"
0920: 2e 0a 23 0a 23 20 2f 2a 20 45 56 3a 20 52 2d 31  ..#.# /* EV: R-1
0930: 39 38 30 33 2d 34 35 38 38 34 20 2a 2f 0a 23 0a  9803-45884 */.#.
0940: 23 20 49 66 20 6e 6f 6e 65 20 69 73 20 73 70 65  # If none is spe
0950: 63 69 66 69 65 64 20 65 78 70 6c 69 63 69 74 6c  cified explicitl
0960: 79 2c 20 22 4e 4f 20 41 43 54 49 4f 4e 22 20 69  y, "NO ACTION" i
0970: 73 20 74 68 65 20 64 65 66 61 75 6c 74 2e 0a 23  s the default..#
0980: 20 0a 64 72 6f 70 5f 61 6c 6c 5f 74 61 62 6c 65   .drop_all_table
0990: 73 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79  s.do_test e_fkey
09a0: 2d 31 37 2e 31 20 7b 0a 20 20 65 78 65 63 73 71  -17.1 {.  execsq
09b0: 6c 20 7b 0a 20 20 20 20 43 52 45 41 54 45 20 54  l {.    CREATE T
09c0: 41 42 4c 45 20 70 61 72 65 6e 74 28 78 20 50 52  ABLE parent(x PR
09d0: 49 4d 41 52 59 20 4b 45 59 2c 20 79 29 3b 0a 20  IMARY KEY, y);. 
09e0: 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20     CREATE TABLE 
09f0: 63 68 69 6c 64 31 28 61 2c 20 0a 20 20 20 20 20  child1(a, .     
0a00: 20 62 20 52 45 46 45 52 45 4e 43 45 53 20 70 61   b REFERENCES pa
0a10: 72 65 6e 74 20 4f 4e 20 55 50 44 41 54 45 20 4e  rent ON UPDATE N
0a20: 4f 20 41 43 54 49 4f 4e 20 4f 4e 20 44 45 4c 45  O ACTION ON DELE
0a30: 54 45 20 52 45 53 54 52 49 43 54 0a 20 20 20 20  TE RESTRICT.    
0a40: 29 3b 0a 20 20 20 20 43 52 45 41 54 45 20 54 41  );.    CREATE TA
0a50: 42 4c 45 20 63 68 69 6c 64 32 28 61 2c 20 0a 20  BLE child2(a, . 
0a60: 20 20 20 20 20 62 20 52 45 46 45 52 45 4e 43 45       b REFERENCE
0a70: 53 20 70 61 72 65 6e 74 20 4f 4e 20 55 50 44 41  S parent ON UPDA
0a80: 54 45 20 52 45 53 54 52 49 43 54 20 4f 4e 20 44  TE RESTRICT ON D
0a90: 45 4c 45 54 45 20 53 45 54 20 4e 55 4c 4c 0a 20  ELETE SET NULL. 
0aa0: 20 20 20 29 3b 0a 20 20 20 20 43 52 45 41 54 45     );.    CREATE
0ab0: 20 54 41 42 4c 45 20 63 68 69 6c 64 33 28 61 2c   TABLE child3(a,
0ac0: 20 0a 20 20 20 20 20 20 62 20 52 45 46 45 52 45   .      b REFERE
0ad0: 4e 43 45 53 20 70 61 72 65 6e 74 20 4f 4e 20 55  NCES parent ON U
0ae0: 50 44 41 54 45 20 53 45 54 20 4e 55 4c 4c 20 4f  PDATE SET NULL O
0af0: 4e 20 44 45 4c 45 54 45 20 53 45 54 20 44 45 46  N DELETE SET DEF
0b00: 41 55 4c 54 0a 20 20 20 20 29 3b 0a 20 20 20 20  AULT.    );.    
0b10: 43 52 45 41 54 45 20 54 41 42 4c 45 20 63 68 69  CREATE TABLE chi
0b20: 6c 64 34 28 61 2c 20 0a 20 20 20 20 20 20 62 20  ld4(a, .      b 
0b30: 52 45 46 45 52 45 4e 43 45 53 20 70 61 72 65 6e  REFERENCES paren
0b40: 74 20 4f 4e 20 55 50 44 41 54 45 20 53 45 54 20  t ON UPDATE SET 
0b50: 44 45 46 41 55 4c 54 20 4f 4e 20 44 45 4c 45 54  DEFAULT ON DELET
0b60: 45 20 43 41 53 43 41 44 45 0a 20 20 20 20 29 3b  E CASCADE.    );
0b70: 0a 0a 20 20 20 20 2d 2d 20 43 72 65 61 74 65 20  ..    -- Create 
0b80: 73 6f 6d 65 20 66 6f 72 65 69 67 6e 20 6b 65 79  some foreign key
0b90: 73 20 74 68 61 74 20 75 73 65 20 74 68 65 20 64  s that use the d
0ba0: 65 66 61 75 6c 74 20 61 63 74 69 6f 6e 20 2d 20  efault action - 
0bb0: 22 4e 4f 20 41 43 54 49 4f 4e 22 0a 20 20 20 20  "NO ACTION".    
0bc0: 43 52 45 41 54 45 20 54 41 42 4c 45 20 63 68 69  CREATE TABLE chi
0bd0: 6c 64 35 28 61 2c 20 62 20 52 45 46 45 52 45 4e  ld5(a, b REFEREN
0be0: 43 45 53 20 70 61 72 65 6e 74 20 4f 4e 20 55 50  CES parent ON UP
0bf0: 44 41 54 45 20 43 41 53 43 41 44 45 29 3b 0a 20  DATE CASCADE);. 
0c00: 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20     CREATE TABLE 
0c10: 63 68 69 6c 64 36 28 61 2c 20 62 20 52 45 46 45  child6(a, b REFE
0c20: 52 45 4e 43 45 53 20 70 61 72 65 6e 74 20 4f 4e  RENCES parent ON
0c30: 20 44 45 4c 45 54 45 20 52 45 53 54 52 49 43 54   DELETE RESTRICT
0c40: 29 3b 0a 20 20 20 20 43 52 45 41 54 45 20 54 41  );.    CREATE TA
0c50: 42 4c 45 20 63 68 69 6c 64 37 28 61 2c 20 62 20  BLE child7(a, b 
0c60: 52 45 46 45 52 45 4e 43 45 53 20 70 61 72 65 6e  REFERENCES paren
0c70: 74 20 4f 4e 20 44 45 4c 45 54 45 20 4e 4f 20 41  t ON DELETE NO A
0c80: 43 54 49 4f 4e 29 3b 0a 20 20 20 20 43 52 45 41  CTION);.    CREA
0c90: 54 45 20 54 41 42 4c 45 20 63 68 69 6c 64 38 28  TE TABLE child8(
0ca0: 61 2c 20 62 20 52 45 46 45 52 45 4e 43 45 53 20  a, b REFERENCES 
0cb0: 70 61 72 65 6e 74 20 4f 4e 20 55 50 44 41 54 45  parent ON UPDATE
0cc0: 20 4e 4f 20 41 43 54 49 4f 4e 29 3b 0a 20 20 7d   NO ACTION);.  }
0cd0: 0a 7d 20 7b 7d 0a 0a 66 6f 72 65 61 63 68 20 7b  .} {}..foreach {
0ce0: 74 6e 20 7a 54 61 62 20 6c 52 65 73 7d 20 7b 0a  tn zTab lRes} {.
0cf0: 20 20 32 20 63 68 69 6c 64 31 20 7b 30 20 30 20    2 child1 {0 0 
0d00: 70 61 72 65 6e 74 20 62 20 7b 7d 20 7b 4e 4f 20  parent b {} {NO 
0d10: 41 43 54 49 4f 4e 7d 20 52 45 53 54 52 49 43 54  ACTION} RESTRICT
0d20: 20 4e 4f 4e 45 7d 0a 20 20 33 20 63 68 69 6c 64   NONE}.  3 child
0d30: 32 20 7b 30 20 30 20 70 61 72 65 6e 74 20 62 20  2 {0 0 parent b 
0d40: 7b 7d 20 52 45 53 54 52 49 43 54 20 7b 53 45 54  {} RESTRICT {SET
0d50: 20 4e 55 4c 4c 7d 20 4e 4f 4e 45 7d 0a 20 20 34   NULL} NONE}.  4
0d60: 20 63 68 69 6c 64 33 20 7b 30 20 30 20 70 61 72   child3 {0 0 par
0d70: 65 6e 74 20 62 20 7b 7d 20 7b 53 45 54 20 4e 55  ent b {} {SET NU
0d80: 4c 4c 7d 20 7b 53 45 54 20 44 45 46 41 55 4c 54  LL} {SET DEFAULT
0d90: 7d 20 4e 4f 4e 45 7d 0a 20 20 35 20 63 68 69 6c  } NONE}.  5 chil
0da0: 64 34 20 7b 30 20 30 20 70 61 72 65 6e 74 20 62  d4 {0 0 parent b
0db0: 20 7b 7d 20 7b 53 45 54 20 44 45 46 41 55 4c 54   {} {SET DEFAULT
0dc0: 7d 20 43 41 53 43 41 44 45 20 4e 4f 4e 45 7d 0a  } CASCADE NONE}.
0dd0: 20 20 36 20 63 68 69 6c 64 35 20 7b 30 20 30 20    6 child5 {0 0 
0de0: 70 61 72 65 6e 74 20 62 20 7b 7d 20 43 41 53 43  parent b {} CASC
0df0: 41 44 45 20 7b 4e 4f 20 41 43 54 49 4f 4e 7d 20  ADE {NO ACTION} 
0e00: 4e 4f 4e 45 7d 0a 20 20 37 20 63 68 69 6c 64 36  NONE}.  7 child6
0e10: 20 7b 30 20 30 20 70 61 72 65 6e 74 20 62 20 7b   {0 0 parent b {
0e20: 7d 20 7b 4e 4f 20 41 43 54 49 4f 4e 7d 20 52 45  } {NO ACTION} RE
0e30: 53 54 52 49 43 54 20 4e 4f 4e 45 7d 0a 20 20 38  STRICT NONE}.  8
0e40: 20 63 68 69 6c 64 37 20 7b 30 20 30 20 70 61 72   child7 {0 0 par
0e50: 65 6e 74 20 62 20 7b 7d 20 7b 4e 4f 20 41 43 54  ent b {} {NO ACT
0e60: 49 4f 4e 7d 20 7b 4e 4f 20 41 43 54 49 4f 4e 7d  ION} {NO ACTION}
0e70: 20 4e 4f 4e 45 7d 0a 20 20 39 20 63 68 69 6c 64   NONE}.  9 child
0e80: 38 20 7b 30 20 30 20 70 61 72 65 6e 74 20 62 20  8 {0 0 parent b 
0e90: 7b 7d 20 7b 4e 4f 20 41 43 54 49 4f 4e 7d 20 7b  {} {NO ACTION} {
0ea0: 4e 4f 20 41 43 54 49 4f 4e 7d 20 4e 4f 4e 45 7d  NO ACTION} NONE}
0eb0: 0a 7d 20 7b 0a 20 20 64 6f 5f 74 65 73 74 20 65  .} {.  do_test e
0ec0: 5f 66 6b 65 79 2d 31 37 2e 24 74 6e 20 7b 20 65  _fkey-17.$tn { e
0ed0: 78 65 63 73 71 6c 20 22 50 52 41 47 4d 41 20 66  xecsql "PRAGMA f
0ee0: 6f 72 65 69 67 6e 5f 6b 65 79 5f 6c 69 73 74 28  oreign_key_list(
0ef0: 24 7a 54 61 62 29 22 20 7d 20 24 6c 52 65 73 0a  $zTab)" } $lRes.
0f00: 7d 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  }..#------------
0f10: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0f20: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0f30: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0f40: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20  -------------.# 
0f50: 2f 2a 20 45 56 3a 20 52 2d 31 39 39 37 31 2d 35  /* EV: R-19971-5
0f60: 34 39 37 36 20 2a 2f 0a 23 0a 23 20 54 65 73 74  4976 */.#.# Test
0f70: 20 74 68 61 74 20 22 4e 4f 20 41 43 54 49 4f 4e   that "NO ACTION
0f80: 22 20 6d 65 61 6e 73 20 74 68 61 74 20 6e 6f 74  " means that not
0f90: 68 69 6e 67 20 68 61 70 70 65 6e 73 20 74 6f 20  hing happens to 
0fa0: 61 20 63 68 69 6c 64 20 72 6f 77 20 77 68 65 6e  a child row when
0fb0: 0a 23 20 69 74 27 73 20 70 61 72 65 6e 74 20 72  .# it's parent r
0fc0: 6f 77 20 69 73 20 75 70 64 61 74 65 64 20 6f 72  ow is updated or
0fd0: 20 64 65 6c 65 74 65 64 2e 0a 23 0a 64 72 6f 70   deleted..#.drop
0fe0: 5f 61 6c 6c 5f 74 61 62 6c 65 73 0a 64 6f 5f 74  _all_tables.do_t
0ff0: 65 73 74 20 65 5f 66 6b 65 79 2d 31 38 2e 31 20  est e_fkey-18.1 
1000: 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20  {.  execsql {.  
1010: 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 70    CREATE TABLE p
1020: 61 72 65 6e 74 28 70 31 2c 20 70 32 2c 20 50 52  arent(p1, p2, PR
1030: 49 4d 41 52 59 20 4b 45 59 28 70 31 2c 20 70 32  IMARY KEY(p1, p2
1040: 29 29 3b 0a 20 20 20 20 43 52 45 41 54 45 20 54  ));.    CREATE T
1050: 41 42 4c 45 20 63 68 69 6c 64 28 63 31 2c 20 63  ABLE child(c1, c
1060: 32 2c 20 0a 20 20 20 20 20 20 46 4f 52 45 49 47  2, .      FOREIG
1070: 4e 20 4b 45 59 28 63 31 2c 20 63 32 29 20 52 45  N KEY(c1, c2) RE
1080: 46 45 52 45 4e 43 45 53 20 70 61 72 65 6e 74 0a  FERENCES parent.
1090: 20 20 20 20 20 20 4f 4e 20 55 50 44 41 54 45 20        ON UPDATE 
10a0: 4e 4f 20 41 43 54 49 4f 4e 0a 20 20 20 20 20 20  NO ACTION.      
10b0: 4f 4e 20 44 45 4c 45 54 45 20 4e 4f 20 41 43 54  ON DELETE NO ACT
10c0: 49 4f 4e 0a 20 20 20 20 20 20 44 45 46 45 52 52  ION.      DEFERR
10d0: 41 42 4c 45 20 49 4e 49 54 49 41 4c 4c 59 20 44  ABLE INITIALLY D
10e0: 45 46 45 52 52 45 44 0a 20 20 20 20 29 3b 0a 20  EFERRED.    );. 
10f0: 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 70     INSERT INTO p
1100: 61 72 65 6e 74 20 56 41 4c 55 45 53 28 27 6a 27  arent VALUES('j'
1110: 2c 20 27 6b 27 29 3b 0a 20 20 20 20 49 4e 53 45  , 'k');.    INSE
1120: 52 54 20 49 4e 54 4f 20 70 61 72 65 6e 74 20 56  RT INTO parent V
1130: 41 4c 55 45 53 28 27 6c 27 2c 20 27 6d 27 29 3b  ALUES('l', 'm');
1140: 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f  .    INSERT INTO
1150: 20 63 68 69 6c 64 20 56 41 4c 55 45 53 28 27 6a   child VALUES('j
1160: 27 2c 20 27 6b 27 29 3b 0a 20 20 20 20 49 4e 53  ', 'k');.    INS
1170: 45 52 54 20 49 4e 54 4f 20 63 68 69 6c 64 20 56  ERT INTO child V
1180: 41 4c 55 45 53 28 27 6c 27 2c 20 27 6d 27 29 3b  ALUES('l', 'm');
1190: 0a 20 20 7d 0a 7d 20 7b 7d 0a 64 6f 5f 74 65 73  .  }.} {}.do_tes
11a0: 74 20 65 5f 66 6b 65 79 2d 31 38 2e 32 20 7b 0a  t e_fkey-18.2 {.
11b0: 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20    execsql {.    
11c0: 42 45 47 49 4e 3b 0a 20 20 20 20 20 20 55 50 44  BEGIN;.      UPD
11d0: 41 54 45 20 70 61 72 65 6e 74 20 53 45 54 20 70  ATE parent SET p
11e0: 31 3d 27 6b 27 20 57 48 45 52 45 20 70 31 3d 27  1='k' WHERE p1='
11f0: 6a 27 3b 0a 20 20 20 20 20 20 44 45 4c 45 54 45  j';.      DELETE
1200: 20 46 52 4f 4d 20 70 61 72 65 6e 74 20 57 48 45   FROM parent WHE
1210: 52 45 20 70 31 3d 27 6c 27 3b 0a 20 20 20 20 20  RE p1='l';.     
1220: 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 63   SELECT * FROM c
1230: 68 69 6c 64 3b 0a 20 20 7d 0a 7d 20 7b 6a 20 6b  hild;.  }.} {j k
1240: 20 6c 20 6d 7d 0a 64 6f 5f 74 65 73 74 20 65 5f   l m}.do_test e_
1250: 66 6b 65 79 2d 31 38 2e 33 20 7b 0a 20 20 63 61  fkey-18.3 {.  ca
1260: 74 63 68 73 71 6c 20 43 4f 4d 4d 49 54 0a 7d 20  tchsql COMMIT.} 
1270: 7b 31 20 7b 66 6f 72 65 69 67 6e 20 6b 65 79 20  {1 {foreign key 
1280: 63 6f 6e 73 74 72 61 69 6e 74 20 66 61 69 6c 65  constraint faile
1290: 64 7d 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b  d}}.do_test e_fk
12a0: 65 79 2d 31 38 2e 34 20 7b 0a 20 20 65 78 65 63  ey-18.4 {.  exec
12b0: 73 71 6c 20 52 4f 4c 4c 42 41 43 4b 0a 7d 20 7b  sql ROLLBACK.} {
12c0: 7d 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  }..#------------
12d0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
12e0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
12f0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1300: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20  -------------.# 
1310: 2f 2a 20 45 56 3a 20 52 2d 30 34 32 37 32 2d 33  /* EV: R-04272-3
1320: 38 36 35 33 20 2a 2f 0a 23 0a 23 20 54 65 73 74  8653 */.#.# Test
1330: 20 74 68 61 74 20 22 52 45 53 54 52 49 43 54 22   that "RESTRICT"
1340: 20 6d 65 61 6e 73 20 74 68 65 20 61 70 70 6c 69   means the appli
1350: 63 61 74 69 6f 6e 20 69 73 20 70 72 6f 68 69 62  cation is prohib
1360: 69 74 65 64 20 66 72 6f 6d 20 64 65 6c 65 74 69  ited from deleti
1370: 6e 67 0a 23 20 6f 72 20 75 70 64 61 74 69 6e 67  ng.# or updating
1380: 20 61 20 70 61 72 65 6e 74 20 74 61 62 6c 65 20   a parent table 
1390: 72 6f 77 20 77 68 65 6e 20 74 68 65 72 65 20 65  row when there e
13a0: 78 69 73 74 73 20 6f 6e 65 20 6f 72 20 6d 6f 72  xists one or mor
13b0: 65 20 63 68 69 6c 64 20 6b 65 79 73 0a 23 20 6d  e child keys.# m
13c0: 61 70 70 65 64 20 74 6f 20 69 74 2e 0a 23 0a 64  apped to it..#.d
13d0: 72 6f 70 5f 61 6c 6c 5f 74 61 62 6c 65 73 0a 64  rop_all_tables.d
13e0: 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 31 38  o_test e_fkey-18
13f0: 2e 31 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b  .1 {.  execsql {
1400: 0a 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c  .    CREATE TABL
1410: 45 20 70 61 72 65 6e 74 28 70 31 2c 20 70 32 29  E parent(p1, p2)
1420: 3b 0a 20 20 20 20 43 52 45 41 54 45 20 55 4e 49  ;.    CREATE UNI
1430: 51 55 45 20 49 4e 44 45 58 20 70 61 72 65 6e 74  QUE INDEX parent
1440: 5f 69 20 4f 4e 20 70 61 72 65 6e 74 28 70 31 2c  _i ON parent(p1,
1450: 20 70 32 29 3b 0a 20 20 20 20 43 52 45 41 54 45   p2);.    CREATE
1460: 20 54 41 42 4c 45 20 63 68 69 6c 64 31 28 63 31   TABLE child1(c1
1470: 2c 20 63 32 2c 20 0a 20 20 20 20 20 20 46 4f 52  , c2, .      FOR
1480: 45 49 47 4e 20 4b 45 59 28 63 32 2c 20 63 31 29  EIGN KEY(c2, c1)
1490: 20 52 45 46 45 52 45 4e 43 45 53 20 70 61 72 65   REFERENCES pare
14a0: 6e 74 28 70 31 2c 20 70 32 29 20 4f 4e 20 44 45  nt(p1, p2) ON DE
14b0: 4c 45 54 45 20 52 45 53 54 52 49 43 54 0a 20 20  LETE RESTRICT.  
14c0: 20 20 29 3b 0a 20 20 20 20 43 52 45 41 54 45 20    );.    CREATE 
14d0: 54 41 42 4c 45 20 63 68 69 6c 64 32 28 63 31 2c  TABLE child2(c1,
14e0: 20 63 32 2c 20 0a 20 20 20 20 20 20 46 4f 52 45   c2, .      FORE
14f0: 49 47 4e 20 4b 45 59 28 63 32 2c 20 63 31 29 20  IGN KEY(c2, c1) 
1500: 52 45 46 45 52 45 4e 43 45 53 20 70 61 72 65 6e  REFERENCES paren
1510: 74 28 70 31 2c 20 70 32 29 20 4f 4e 20 55 50 44  t(p1, p2) ON UPD
1520: 41 54 45 20 52 45 53 54 52 49 43 54 0a 20 20 20  ATE RESTRICT.   
1530: 20 29 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a 64 6f 5f   );.  }.} {}.do_
1540: 74 65 73 74 20 65 5f 66 6b 65 79 2d 31 38 2e 32  test e_fkey-18.2
1550: 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20   {.  execsql {. 
1560: 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 70     INSERT INTO p
1570: 61 72 65 6e 74 20 56 41 4c 55 45 53 28 27 61 27  arent VALUES('a'
1580: 2c 20 27 62 27 29 3b 0a 20 20 20 20 49 4e 53 45  , 'b');.    INSE
1590: 52 54 20 49 4e 54 4f 20 70 61 72 65 6e 74 20 56  RT INTO parent V
15a0: 41 4c 55 45 53 28 27 63 27 2c 20 27 64 27 29 3b  ALUES('c', 'd');
15b0: 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f  .    INSERT INTO
15c0: 20 63 68 69 6c 64 31 20 56 41 4c 55 45 53 28 27   child1 VALUES('
15d0: 62 27 2c 20 27 61 27 29 3b 0a 20 20 20 20 49 4e  b', 'a');.    IN
15e0: 53 45 52 54 20 49 4e 54 4f 20 63 68 69 6c 64 32  SERT INTO child2
15f0: 20 56 41 4c 55 45 53 28 27 64 27 2c 20 27 63 27   VALUES('d', 'c'
1600: 29 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a 64 6f 5f 74  );.  }.} {}.do_t
1610: 65 73 74 20 65 5f 66 6b 65 79 2d 31 38 2e 33 20  est e_fkey-18.3 
1620: 7b 0a 20 20 63 61 74 63 68 73 71 6c 20 7b 20 44  {.  catchsql { D
1630: 45 4c 45 54 45 20 46 52 4f 4d 20 70 61 72 65 6e  ELETE FROM paren
1640: 74 20 57 48 45 52 45 20 70 31 20 3d 20 27 61 27  t WHERE p1 = 'a'
1650: 20 7d 0a 7d 20 7b 31 20 7b 66 6f 72 65 69 67 6e   }.} {1 {foreign
1660: 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 20   key constraint 
1670: 66 61 69 6c 65 64 7d 7d 0a 64 6f 5f 74 65 73 74  failed}}.do_test
1680: 20 65 5f 66 6b 65 79 2d 31 38 2e 34 20 7b 0a 20   e_fkey-18.4 {. 
1690: 20 63 61 74 63 68 73 71 6c 20 7b 20 55 50 44 41   catchsql { UPDA
16a0: 54 45 20 70 61 72 65 6e 74 20 53 45 54 20 70 32  TE parent SET p2
16b0: 20 3d 20 27 65 27 20 57 48 45 52 45 20 70 31 20   = 'e' WHERE p1 
16c0: 3d 20 27 63 27 20 7d 0a 7d 20 7b 31 20 7b 66 6f  = 'c' }.} {1 {fo
16d0: 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72  reign key constr
16e0: 61 69 6e 74 20 66 61 69 6c 65 64 7d 7d 0a 0a 23  aint failed}}..#
16f0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1700: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1710: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1720: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1730: 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20 2f 2a 20 45  ---------.# /* E
1740: 56 3a 20 52 2d 33 37 39 39 37 2d 34 32 31 38 37  V: R-37997-42187
1750: 20 2a 2f 0a 23 20 0a 23 20 54 65 73 74 20 74 68   */.# .# Test th
1760: 61 74 20 52 45 53 54 52 49 43 54 20 69 73 20 73  at RESTRICT is s
1770: 6c 69 67 68 74 6c 79 20 64 69 66 66 65 72 65 6e  lightly differen
1780: 74 20 66 72 6f 6d 20 4e 4f 20 41 43 54 49 4f 4e  t from NO ACTION
1790: 20 66 6f 72 20 49 4d 4d 45 44 49 41 54 45 0a 23   for IMMEDIATE.#
17a0: 20 63 6f 6e 73 74 72 61 69 6e 74 73 2c 20 69 6e   constraints, in
17b0: 20 74 68 61 74 20 69 74 20 69 73 20 65 6e 66 6f   that it is enfo
17c0: 72 63 65 64 20 69 6d 6d 65 64 69 61 74 65 6c 79  rced immediately
17d0: 2c 20 6e 6f 74 20 61 74 20 74 68 65 20 65 6e 64  , not at the end
17e0: 20 6f 66 20 74 68 65 20 0a 23 20 73 74 61 74 65   of the .# state
17f0: 6d 65 6e 74 2e 0a 23 0a 64 72 6f 70 5f 61 6c 6c  ment..#.drop_all
1800: 5f 74 61 62 6c 65 73 0a 64 6f 5f 74 65 73 74 20  _tables.do_test 
1810: 65 5f 66 6b 65 79 2d 31 39 2e 31 20 7b 0a 20 20  e_fkey-19.1 {.  
1820: 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 43 52  execsql {.    CR
1830: 45 41 54 45 20 54 41 42 4c 45 20 70 61 72 65 6e  EATE TABLE paren
1840: 74 28 78 20 50 52 49 4d 41 52 59 20 4b 45 59 29  t(x PRIMARY KEY)
1850: 3b 0a 20 20 20 20 43 52 45 41 54 45 20 54 41 42  ;.    CREATE TAB
1860: 4c 45 20 63 68 69 6c 64 31 28 63 20 52 45 46 45  LE child1(c REFE
1870: 52 45 4e 43 45 53 20 70 61 72 65 6e 74 20 4f 4e  RENCES parent ON
1880: 20 55 50 44 41 54 45 20 52 45 53 54 52 49 43 54   UPDATE RESTRICT
1890: 29 3b 0a 20 20 20 20 43 52 45 41 54 45 20 54 41  );.    CREATE TA
18a0: 42 4c 45 20 63 68 69 6c 64 32 28 63 20 52 45 46  BLE child2(c REF
18b0: 45 52 45 4e 43 45 53 20 70 61 72 65 6e 74 20 4f  ERENCES parent O
18c0: 4e 20 55 50 44 41 54 45 20 4e 4f 20 41 43 54 49  N UPDATE NO ACTI
18d0: 4f 4e 29 3b 0a 0a 20 20 20 20 49 4e 53 45 52 54  ON);..    INSERT
18e0: 20 49 4e 54 4f 20 70 61 72 65 6e 74 20 56 41 4c   INTO parent VAL
18f0: 55 45 53 28 27 6b 65 79 31 27 29 3b 0a 20 20 20  UES('key1');.   
1900: 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 70 61 72   INSERT INTO par
1910: 65 6e 74 20 56 41 4c 55 45 53 28 27 6b 65 79 32  ent VALUES('key2
1920: 27 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49  ');.    INSERT I
1930: 4e 54 4f 20 63 68 69 6c 64 31 20 56 41 4c 55 45  NTO child1 VALUE
1940: 53 28 27 6b 65 79 31 27 29 3b 0a 20 20 20 20 49  S('key1');.    I
1950: 4e 53 45 52 54 20 49 4e 54 4f 20 63 68 69 6c 64  NSERT INTO child
1960: 32 20 56 41 4c 55 45 53 28 27 6b 65 79 32 27 29  2 VALUES('key2')
1970: 3b 0a 0a 20 20 20 20 43 52 45 41 54 45 20 54 52  ;..    CREATE TR
1980: 49 47 47 45 52 20 70 61 72 65 6e 74 5f 74 20 41  IGGER parent_t A
1990: 46 54 45 52 20 55 50 44 41 54 45 20 4f 4e 20 70  FTER UPDATE ON p
19a0: 61 72 65 6e 74 20 42 45 47 49 4e 0a 20 20 20 20  arent BEGIN.    
19b0: 20 20 55 50 44 41 54 45 20 63 68 69 6c 64 31 20    UPDATE child1 
19c0: 73 65 74 20 63 20 3d 20 6e 65 77 2e 78 20 57 48  set c = new.x WH
19d0: 45 52 45 20 63 20 3d 20 6f 6c 64 2e 78 3b 0a 20  ERE c = old.x;. 
19e0: 20 20 20 20 20 55 50 44 41 54 45 20 63 68 69 6c       UPDATE chil
19f0: 64 32 20 73 65 74 20 63 20 3d 20 6e 65 77 2e 78  d2 set c = new.x
1a00: 20 57 48 45 52 45 20 63 20 3d 20 6f 6c 64 2e 78   WHERE c = old.x
1a10: 3b 0a 20 20 20 20 45 4e 44 3b 0a 20 20 7d 0a 7d  ;.    END;.  }.}
1a20: 20 7b 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b   {}.do_test e_fk
1a30: 65 79 2d 31 39 2e 32 20 7b 0a 20 20 63 61 74 63  ey-19.2 {.  catc
1a40: 68 73 71 6c 20 7b 20 55 50 44 41 54 45 20 70 61  hsql { UPDATE pa
1a50: 72 65 6e 74 20 53 45 54 20 78 20 3d 20 27 6b 65  rent SET x = 'ke
1a60: 79 20 6f 6e 65 27 20 57 48 45 52 45 20 78 20 3d  y one' WHERE x =
1a70: 20 27 6b 65 79 31 27 20 7d 0a 7d 20 7b 31 20 7b   'key1' }.} {1 {
1a80: 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73  foreign key cons
1a90: 74 72 61 69 6e 74 20 66 61 69 6c 65 64 7d 7d 0a  traint failed}}.
1aa0: 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 31  do_test e_fkey-1
1ab0: 39 2e 33 20 7b 0a 20 20 65 78 65 63 73 71 6c 20  9.3 {.  execsql 
1ac0: 7b 20 0a 20 20 20 20 55 50 44 41 54 45 20 70 61  { .    UPDATE pa
1ad0: 72 65 6e 74 20 53 45 54 20 78 20 3d 20 27 6b 65  rent SET x = 'ke
1ae0: 79 20 74 77 6f 27 20 57 48 45 52 45 20 78 20 3d  y two' WHERE x =
1af0: 20 27 6b 65 79 32 27 3b 0a 20 20 20 20 53 45 4c   'key2';.    SEL
1b00: 45 43 54 20 2a 20 46 52 4f 4d 20 63 68 69 6c 64  ECT * FROM child
1b10: 32 3b 0a 20 20 7d 0a 7d 20 7b 7b 6b 65 79 20 74  2;.  }.} {{key t
1b20: 77 6f 7d 7d 0a 0a 64 72 6f 70 5f 61 6c 6c 5f 74  wo}}..drop_all_t
1b30: 61 62 6c 65 73 0a 64 6f 5f 74 65 73 74 20 65 5f  ables.do_test e_
1b40: 66 6b 65 79 2d 31 39 2e 34 20 7b 0a 20 20 65 78  fkey-19.4 {.  ex
1b50: 65 63 73 71 6c 20 7b 0a 20 20 20 20 43 52 45 41  ecsql {.    CREA
1b60: 54 45 20 54 41 42 4c 45 20 70 61 72 65 6e 74 28  TE TABLE parent(
1b70: 78 20 50 52 49 4d 41 52 59 20 4b 45 59 29 3b 0a  x PRIMARY KEY);.
1b80: 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45      CREATE TABLE
1b90: 20 63 68 69 6c 64 31 28 63 20 52 45 46 45 52 45   child1(c REFERE
1ba0: 4e 43 45 53 20 70 61 72 65 6e 74 20 4f 4e 20 44  NCES parent ON D
1bb0: 45 4c 45 54 45 20 52 45 53 54 52 49 43 54 29 3b  ELETE RESTRICT);
1bc0: 0a 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c  .    CREATE TABL
1bd0: 45 20 63 68 69 6c 64 32 28 63 20 52 45 46 45 52  E child2(c REFER
1be0: 45 4e 43 45 53 20 70 61 72 65 6e 74 20 4f 4e 20  ENCES parent ON 
1bf0: 44 45 4c 45 54 45 20 4e 4f 20 41 43 54 49 4f 4e  DELETE NO ACTION
1c00: 29 3b 0a 0a 20 20 20 20 49 4e 53 45 52 54 20 49  );..    INSERT I
1c10: 4e 54 4f 20 70 61 72 65 6e 74 20 56 41 4c 55 45  NTO parent VALUE
1c20: 53 28 27 6b 65 79 31 27 29 3b 0a 20 20 20 20 49  S('key1');.    I
1c30: 4e 53 45 52 54 20 49 4e 54 4f 20 70 61 72 65 6e  NSERT INTO paren
1c40: 74 20 56 41 4c 55 45 53 28 27 6b 65 79 32 27 29  t VALUES('key2')
1c50: 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54  ;.    INSERT INT
1c60: 4f 20 63 68 69 6c 64 31 20 56 41 4c 55 45 53 28  O child1 VALUES(
1c70: 27 6b 65 79 31 27 29 3b 0a 20 20 20 20 49 4e 53  'key1');.    INS
1c80: 45 52 54 20 49 4e 54 4f 20 63 68 69 6c 64 32 20  ERT INTO child2 
1c90: 56 41 4c 55 45 53 28 27 6b 65 79 32 27 29 3b 0a  VALUES('key2');.
1ca0: 0a 20 20 20 20 43 52 45 41 54 45 20 54 52 49 47  .    CREATE TRIG
1cb0: 47 45 52 20 70 61 72 65 6e 74 5f 74 20 41 46 54  GER parent_t AFT
1cc0: 45 52 20 44 45 4c 45 54 45 20 4f 4e 20 70 61 72  ER DELETE ON par
1cd0: 65 6e 74 20 42 45 47 49 4e 0a 20 20 20 20 20 20  ent BEGIN.      
1ce0: 55 50 44 41 54 45 20 63 68 69 6c 64 31 20 53 45  UPDATE child1 SE
1cf0: 54 20 63 20 3d 20 4e 55 4c 4c 20 57 48 45 52 45  T c = NULL WHERE
1d00: 20 63 20 3d 20 6f 6c 64 2e 78 3b 0a 20 20 20 20   c = old.x;.    
1d10: 20 20 55 50 44 41 54 45 20 63 68 69 6c 64 32 20    UPDATE child2 
1d20: 53 45 54 20 63 20 3d 20 4e 55 4c 4c 20 57 48 45  SET c = NULL WHE
1d30: 52 45 20 63 20 3d 20 6f 6c 64 2e 78 3b 0a 20 20  RE c = old.x;.  
1d40: 20 20 45 4e 44 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a    END;.  }.} {}.
1d50: 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 31  do_test e_fkey-1
1d60: 39 2e 35 20 7b 0a 20 20 63 61 74 63 68 73 71 6c  9.5 {.  catchsql
1d70: 20 7b 20 44 45 4c 45 54 45 20 46 52 4f 4d 20 70   { DELETE FROM p
1d80: 61 72 65 6e 74 20 57 48 45 52 45 20 78 20 3d 20  arent WHERE x = 
1d90: 27 6b 65 79 31 27 20 7d 0a 7d 20 7b 31 20 7b 66  'key1' }.} {1 {f
1da0: 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74  oreign key const
1db0: 72 61 69 6e 74 20 66 61 69 6c 65 64 7d 7d 0a 64  raint failed}}.d
1dc0: 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 31 39  o_test e_fkey-19
1dd0: 2e 36 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b  .6 {.  execsql {
1de0: 20 0a 20 20 20 20 44 45 4c 45 54 45 20 46 52 4f   .    DELETE FRO
1df0: 4d 20 70 61 72 65 6e 74 20 57 48 45 52 45 20 78  M parent WHERE x
1e00: 20 3d 20 27 6b 65 79 32 27 3b 0a 20 20 20 20 53   = 'key2';.    S
1e10: 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 63 68 69  ELECT * FROM chi
1e20: 6c 64 32 3b 0a 20 20 7d 0a 7d 20 7b 7b 7d 7d 0a  ld2;.  }.} {{}}.
1e30: 0a 64 72 6f 70 5f 61 6c 6c 5f 74 61 62 6c 65 73  .drop_all_tables
1e40: 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d  .do_test e_fkey-
1e50: 31 39 2e 37 20 7b 0a 20 20 65 78 65 63 73 71 6c  19.7 {.  execsql
1e60: 20 7b 0a 20 20 20 20 43 52 45 41 54 45 20 54 41   {.    CREATE TA
1e70: 42 4c 45 20 70 61 72 65 6e 74 28 78 20 50 52 49  BLE parent(x PRI
1e80: 4d 41 52 59 20 4b 45 59 29 3b 0a 20 20 20 20 43  MARY KEY);.    C
1e90: 52 45 41 54 45 20 54 41 42 4c 45 20 63 68 69 6c  REATE TABLE chil
1ea0: 64 31 28 63 20 52 45 46 45 52 45 4e 43 45 53 20  d1(c REFERENCES 
1eb0: 70 61 72 65 6e 74 20 4f 4e 20 44 45 4c 45 54 45  parent ON DELETE
1ec0: 20 52 45 53 54 52 49 43 54 29 3b 0a 20 20 20 20   RESTRICT);.    
1ed0: 43 52 45 41 54 45 20 54 41 42 4c 45 20 63 68 69  CREATE TABLE chi
1ee0: 6c 64 32 28 63 20 52 45 46 45 52 45 4e 43 45 53  ld2(c REFERENCES
1ef0: 20 70 61 72 65 6e 74 20 4f 4e 20 44 45 4c 45 54   parent ON DELET
1f00: 45 20 4e 4f 20 41 43 54 49 4f 4e 29 3b 0a 0a 20  E NO ACTION);.. 
1f10: 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 70     INSERT INTO p
1f20: 61 72 65 6e 74 20 56 41 4c 55 45 53 28 27 6b 65  arent VALUES('ke
1f30: 79 31 27 29 3b 0a 20 20 20 20 49 4e 53 45 52 54  y1');.    INSERT
1f40: 20 49 4e 54 4f 20 70 61 72 65 6e 74 20 56 41 4c   INTO parent VAL
1f50: 55 45 53 28 27 6b 65 79 32 27 29 3b 0a 20 20 20  UES('key2');.   
1f60: 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 63 68 69   INSERT INTO chi
1f70: 6c 64 31 20 56 41 4c 55 45 53 28 27 6b 65 79 31  ld1 VALUES('key1
1f80: 27 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49  ');.    INSERT I
1f90: 4e 54 4f 20 63 68 69 6c 64 32 20 56 41 4c 55 45  NTO child2 VALUE
1fa0: 53 28 27 6b 65 79 32 27 29 3b 0a 20 20 7d 0a 7d  S('key2');.  }.}
1fb0: 20 7b 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b   {}.do_test e_fk
1fc0: 65 79 2d 31 39 2e 38 20 7b 0a 20 20 63 61 74 63  ey-19.8 {.  catc
1fd0: 68 73 71 6c 20 7b 20 52 45 50 4c 41 43 45 20 49  hsql { REPLACE I
1fe0: 4e 54 4f 20 70 61 72 65 6e 74 20 56 41 4c 55 45  NTO parent VALUE
1ff0: 53 28 27 6b 65 79 31 27 29 20 7d 0a 7d 20 7b 31  S('key1') }.} {1
2000: 20 7b 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f   {foreign key co
2010: 6e 73 74 72 61 69 6e 74 20 66 61 69 6c 65 64 7d  nstraint failed}
2020: 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79  }.do_test e_fkey
2030: 2d 31 39 2e 39 20 7b 0a 20 20 65 78 65 63 73 71  -19.9 {.  execsq
2040: 6c 20 7b 20 0a 20 20 20 20 52 45 50 4c 41 43 45  l { .    REPLACE
2050: 20 49 4e 54 4f 20 70 61 72 65 6e 74 20 56 41 4c   INTO parent VAL
2060: 55 45 53 28 27 6b 65 79 32 27 29 3b 0a 20 20 20  UES('key2');.   
2070: 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 63   SELECT * FROM c
2080: 68 69 6c 64 32 3b 0a 20 20 7d 0a 7d 20 7b 6b 65  hild2;.  }.} {ke
2090: 79 32 7d 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  y2}..#----------
20a0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
20b0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
20c0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
20d0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a  ---------------.
20e0: 23 20 2f 2a 20 45 56 3a 20 52 2d 32 34 31 37 39  # /* EV: R-24179
20f0: 2d 36 30 35 32 33 20 2a 2f 0a 23 20 0a 23 20 54  -60523 */.# .# T
2100: 65 73 74 20 74 68 61 74 20 52 45 53 54 52 49 43  est that RESTRIC
2110: 54 20 69 73 20 65 6e 66 6f 72 63 65 64 20 69 6d  T is enforced im
2120: 6d 65 64 69 61 74 65 6c 79 2c 20 65 76 65 6e 20  mediately, even 
2130: 66 6f 72 20 61 20 44 45 46 45 52 52 45 44 20 63  for a DEFERRED c
2140: 6f 6e 73 74 72 61 69 6e 74 2e 0a 23 0a 64 72 6f  onstraint..#.dro
2150: 70 5f 61 6c 6c 5f 74 61 62 6c 65 73 0a 64 6f 5f  p_all_tables.do_
2160: 74 65 73 74 20 65 5f 66 6b 65 79 2d 32 30 2e 31  test e_fkey-20.1
2170: 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20   {.  execsql {. 
2180: 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20     CREATE TABLE 
2190: 70 61 72 65 6e 74 28 78 20 50 52 49 4d 41 52 59  parent(x PRIMARY
21a0: 20 4b 45 59 29 3b 0a 20 20 20 20 43 52 45 41 54   KEY);.    CREAT
21b0: 45 20 54 41 42 4c 45 20 63 68 69 6c 64 31 28 63  E TABLE child1(c
21c0: 20 52 45 46 45 52 45 4e 43 45 53 20 70 61 72 65   REFERENCES pare
21d0: 6e 74 20 4f 4e 20 55 50 44 41 54 45 20 52 45 53  nt ON UPDATE RES
21e0: 54 52 49 43 54 0a 20 20 20 20 20 20 44 45 46 45  TRICT.      DEFE
21f0: 52 52 41 42 4c 45 20 49 4e 49 54 49 41 4c 4c 59  RRABLE INITIALLY
2200: 20 44 45 46 45 52 52 45 44 0a 20 20 20 20 29 3b   DEFERRED.    );
2210: 0a 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c  .    CREATE TABL
2220: 45 20 63 68 69 6c 64 32 28 63 20 52 45 46 45 52  E child2(c REFER
2230: 45 4e 43 45 53 20 70 61 72 65 6e 74 20 4f 4e 20  ENCES parent ON 
2240: 55 50 44 41 54 45 20 4e 4f 20 41 43 54 49 4f 4e  UPDATE NO ACTION
2250: 0a 20 20 20 20 20 20 44 45 46 45 52 52 41 42 4c  .      DEFERRABL
2260: 45 20 49 4e 49 54 49 41 4c 4c 59 20 44 45 46 45  E INITIALLY DEFE
2270: 52 52 45 44 0a 20 20 20 20 29 3b 0a 0a 20 20 20  RRED.    );..   
2280: 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 70 61 72   INSERT INTO par
2290: 65 6e 74 20 56 41 4c 55 45 53 28 27 6b 65 79 31  ent VALUES('key1
22a0: 27 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49  ');.    INSERT I
22b0: 4e 54 4f 20 70 61 72 65 6e 74 20 56 41 4c 55 45  NTO parent VALUE
22c0: 53 28 27 6b 65 79 32 27 29 3b 0a 20 20 20 20 49  S('key2');.    I
22d0: 4e 53 45 52 54 20 49 4e 54 4f 20 63 68 69 6c 64  NSERT INTO child
22e0: 31 20 56 41 4c 55 45 53 28 27 6b 65 79 31 27 29  1 VALUES('key1')
22f0: 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54  ;.    INSERT INT
2300: 4f 20 63 68 69 6c 64 32 20 56 41 4c 55 45 53 28  O child2 VALUES(
2310: 27 6b 65 79 32 27 29 3b 0a 20 20 20 20 42 45 47  'key2');.    BEG
2320: 49 4e 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a 64 6f 5f  IN;.  }.} {}.do_
2330: 74 65 73 74 20 65 5f 66 6b 65 79 2d 32 30 2e 32  test e_fkey-20.2
2340: 20 7b 0a 20 20 63 61 74 63 68 73 71 6c 20 7b 20   {.  catchsql { 
2350: 55 50 44 41 54 45 20 70 61 72 65 6e 74 20 53 45  UPDATE parent SE
2360: 54 20 78 20 3d 20 27 6b 65 79 20 6f 6e 65 27 20  T x = 'key one' 
2370: 57 48 45 52 45 20 78 20 3d 20 27 6b 65 79 31 27  WHERE x = 'key1'
2380: 20 7d 0a 7d 20 7b 31 20 7b 66 6f 72 65 69 67 6e   }.} {1 {foreign
2390: 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 20   key constraint 
23a0: 66 61 69 6c 65 64 7d 7d 0a 64 6f 5f 74 65 73 74  failed}}.do_test
23b0: 20 65 5f 66 6b 65 79 2d 32 30 2e 33 20 7b 0a 20   e_fkey-20.3 {. 
23c0: 20 65 78 65 63 73 71 6c 20 7b 20 55 50 44 41 54   execsql { UPDAT
23d0: 45 20 70 61 72 65 6e 74 20 53 45 54 20 78 20 3d  E parent SET x =
23e0: 20 27 6b 65 79 20 74 77 6f 27 20 57 48 45 52 45   'key two' WHERE
23f0: 20 78 20 3d 20 27 6b 65 79 32 27 20 7d 0a 7d 20   x = 'key2' }.} 
2400: 7b 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65  {}.do_test e_fke
2410: 79 2d 32 30 2e 34 20 7b 0a 20 20 63 61 74 63 68  y-20.4 {.  catch
2420: 73 71 6c 20 43 4f 4d 4d 49 54 0a 7d 20 7b 31 20  sql COMMIT.} {1 
2430: 7b 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e  {foreign key con
2440: 73 74 72 61 69 6e 74 20 66 61 69 6c 65 64 7d 7d  straint failed}}
2450: 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d  .do_test e_fkey-
2460: 32 30 2e 35 20 7b 0a 20 20 65 78 65 63 73 71 6c  20.5 {.  execsql
2470: 20 7b 0a 20 20 20 20 55 50 44 41 54 45 20 63 68   {.    UPDATE ch
2480: 69 6c 64 32 20 53 45 54 20 63 20 3d 20 27 6b 65  ild2 SET c = 'ke
2490: 79 20 74 77 6f 27 3b 0a 20 20 20 20 43 4f 4d 4d  y two';.    COMM
24a0: 49 54 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a 0a 64 72  IT;.  }.} {}..dr
24b0: 6f 70 5f 61 6c 6c 5f 74 61 62 6c 65 73 0a 64 6f  op_all_tables.do
24c0: 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 32 30 2e  _test e_fkey-20.
24d0: 36 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a  6 {.  execsql {.
24e0: 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45      CREATE TABLE
24f0: 20 70 61 72 65 6e 74 28 78 20 50 52 49 4d 41 52   parent(x PRIMAR
2500: 59 20 4b 45 59 29 3b 0a 20 20 20 20 43 52 45 41  Y KEY);.    CREA
2510: 54 45 20 54 41 42 4c 45 20 63 68 69 6c 64 31 28  TE TABLE child1(
2520: 63 20 52 45 46 45 52 45 4e 43 45 53 20 70 61 72  c REFERENCES par
2530: 65 6e 74 20 4f 4e 20 44 45 4c 45 54 45 20 52 45  ent ON DELETE RE
2540: 53 54 52 49 43 54 0a 20 20 20 20 20 20 44 45 46  STRICT.      DEF
2550: 45 52 52 41 42 4c 45 20 49 4e 49 54 49 41 4c 4c  ERRABLE INITIALL
2560: 59 20 44 45 46 45 52 52 45 44 0a 20 20 20 20 29  Y DEFERRED.    )
2570: 3b 0a 20 20 20 20 43 52 45 41 54 45 20 54 41 42  ;.    CREATE TAB
2580: 4c 45 20 63 68 69 6c 64 32 28 63 20 52 45 46 45  LE child2(c REFE
2590: 52 45 4e 43 45 53 20 70 61 72 65 6e 74 20 4f 4e  RENCES parent ON
25a0: 20 44 45 4c 45 54 45 20 4e 4f 20 41 43 54 49 4f   DELETE NO ACTIO
25b0: 4e 0a 20 20 20 20 20 20 44 45 46 45 52 52 41 42  N.      DEFERRAB
25c0: 4c 45 20 49 4e 49 54 49 41 4c 4c 59 20 44 45 46  LE INITIALLY DEF
25d0: 45 52 52 45 44 0a 20 20 20 20 29 3b 0a 0a 20 20  ERRED.    );..  
25e0: 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 70 61    INSERT INTO pa
25f0: 72 65 6e 74 20 56 41 4c 55 45 53 28 27 6b 65 79  rent VALUES('key
2600: 31 27 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20  1');.    INSERT 
2610: 49 4e 54 4f 20 70 61 72 65 6e 74 20 56 41 4c 55  INTO parent VALU
2620: 45 53 28 27 6b 65 79 32 27 29 3b 0a 20 20 20 20  ES('key2');.    
2630: 49 4e 53 45 52 54 20 49 4e 54 4f 20 63 68 69 6c  INSERT INTO chil
2640: 64 31 20 56 41 4c 55 45 53 28 27 6b 65 79 31 27  d1 VALUES('key1'
2650: 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e  );.    INSERT IN
2660: 54 4f 20 63 68 69 6c 64 32 20 56 41 4c 55 45 53  TO child2 VALUES
2670: 28 27 6b 65 79 32 27 29 3b 0a 20 20 20 20 42 45  ('key2');.    BE
2680: 47 49 4e 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a 64 6f  GIN;.  }.} {}.do
2690: 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 32 30 2e  _test e_fkey-20.
26a0: 37 20 7b 0a 20 20 63 61 74 63 68 73 71 6c 20 7b  7 {.  catchsql {
26b0: 20 44 45 4c 45 54 45 20 46 52 4f 4d 20 70 61 72   DELETE FROM par
26c0: 65 6e 74 20 57 48 45 52 45 20 78 20 3d 20 27 6b  ent WHERE x = 'k
26d0: 65 79 31 27 20 7d 0a 7d 20 7b 31 20 7b 66 6f 72  ey1' }.} {1 {for
26e0: 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61  eign key constra
26f0: 69 6e 74 20 66 61 69 6c 65 64 7d 7d 0a 64 6f 5f  int failed}}.do_
2700: 74 65 73 74 20 65 5f 66 6b 65 79 2d 32 30 2e 38  test e_fkey-20.8
2710: 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 20 44   {.  execsql { D
2720: 45 4c 45 54 45 20 46 52 4f 4d 20 70 61 72 65 6e  ELETE FROM paren
2730: 74 20 57 48 45 52 45 20 78 20 3d 20 27 6b 65 79  t WHERE x = 'key
2740: 32 27 20 7d 0a 7d 20 7b 7d 0a 64 6f 5f 74 65 73  2' }.} {}.do_tes
2750: 74 20 65 5f 66 6b 65 79 2d 32 30 2e 39 20 7b 0a  t e_fkey-20.9 {.
2760: 20 20 63 61 74 63 68 73 71 6c 20 43 4f 4d 4d 49    catchsql COMMI
2770: 54 0a 7d 20 7b 31 20 7b 66 6f 72 65 69 67 6e 20  T.} {1 {foreign 
2780: 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 20 66  key constraint f
2790: 61 69 6c 65 64 7d 7d 0a 64 6f 5f 74 65 73 74 20  ailed}}.do_test 
27a0: 65 5f 66 6b 65 79 2d 32 30 2e 31 30 20 7b 0a 20  e_fkey-20.10 {. 
27b0: 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 55   execsql {.    U
27c0: 50 44 41 54 45 20 63 68 69 6c 64 32 20 53 45 54  PDATE child2 SET
27d0: 20 63 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 43   c = NULL;.    C
27e0: 4f 4d 4d 49 54 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a  OMMIT;.  }.} {}.
27f0: 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  .#--------------
2800: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2810: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2820: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2830: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20 2f 2a  -----------.# /*
2840: 20 45 56 3a 20 52 2d 30 33 33 35 33 2d 30 35 33   EV: R-03353-053
2850: 32 37 20 2a 2f 0a 23 0a 23 20 54 65 73 74 20 53  27 */.#.# Test S
2860: 45 54 20 4e 55 4c 4c 20 61 63 74 69 6f 6e 73 2e  ET NULL actions.
2870: 0a 23 0a 64 72 6f 70 5f 61 6c 6c 5f 74 61 62 6c  .#.drop_all_tabl
2880: 65 73 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65  es.do_test e_fke
2890: 79 2d 32 31 2e 31 20 7b 0a 20 20 65 78 65 63 73  y-21.1 {.  execs
28a0: 71 6c 20 7b 0a 20 20 20 20 43 52 45 41 54 45 20  ql {.    CREATE 
28b0: 54 41 42 4c 45 20 70 41 28 78 20 50 52 49 4d 41  TABLE pA(x PRIMA
28c0: 52 59 20 4b 45 59 29 3b 0a 20 20 20 20 43 52 45  RY KEY);.    CRE
28d0: 41 54 45 20 54 41 42 4c 45 20 63 41 28 63 20 52  ATE TABLE cA(c R
28e0: 45 46 45 52 45 4e 43 45 53 20 70 41 20 4f 4e 20  EFERENCES pA ON 
28f0: 44 45 4c 45 54 45 20 53 45 54 20 4e 55 4c 4c 29  DELETE SET NULL)
2900: 3b 0a 20 20 20 20 43 52 45 41 54 45 20 54 41 42  ;.    CREATE TAB
2910: 4c 45 20 63 42 28 63 20 52 45 46 45 52 45 4e 43  LE cB(c REFERENC
2920: 45 53 20 70 41 20 4f 4e 20 55 50 44 41 54 45 20  ES pA ON UPDATE 
2930: 53 45 54 20 4e 55 4c 4c 29 3b 0a 0a 20 20 20 20  SET NULL);..    
2940: 49 4e 53 45 52 54 20 49 4e 54 4f 20 70 41 20 56  INSERT INTO pA V
2950: 41 4c 55 45 53 28 58 27 41 42 43 44 27 29 3b 0a  ALUES(X'ABCD');.
2960: 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20      INSERT INTO 
2970: 70 41 20 56 41 4c 55 45 53 28 58 27 31 32 33 34  pA VALUES(X'1234
2980: 27 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49  ');.    INSERT I
2990: 4e 54 4f 20 63 41 20 56 41 4c 55 45 53 28 58 27  NTO cA VALUES(X'
29a0: 41 42 43 44 27 29 3b 0a 20 20 20 20 49 4e 53 45  ABCD');.    INSE
29b0: 52 54 20 49 4e 54 4f 20 63 42 20 56 41 4c 55 45  RT INTO cB VALUE
29c0: 53 28 58 27 31 32 33 34 27 29 3b 0a 20 20 7d 0a  S(X'1234');.  }.
29d0: 7d 20 7b 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66  } {}.do_test e_f
29e0: 6b 65 79 2d 32 31 2e 32 20 7b 0a 20 20 65 78 65  key-21.2 {.  exe
29f0: 63 73 71 6c 20 7b 0a 20 20 20 20 44 45 4c 45 54  csql {.    DELET
2a00: 45 20 46 52 4f 4d 20 70 41 20 57 48 45 52 45 20  E FROM pA WHERE 
2a10: 72 6f 77 69 64 20 3d 20 31 3b 0a 20 20 20 20 53  rowid = 1;.    S
2a20: 45 4c 45 43 54 20 71 75 6f 74 65 28 78 29 20 46  ELECT quote(x) F
2a30: 52 4f 4d 20 70 41 3b 0a 20 20 7d 0a 7d 20 7b 58  ROM pA;.  }.} {X
2a40: 27 31 32 33 34 27 7d 0a 64 6f 5f 74 65 73 74 20  '1234'}.do_test 
2a50: 65 5f 66 6b 65 79 2d 32 31 2e 33 20 7b 0a 20 20  e_fkey-21.3 {.  
2a60: 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 53 45  execsql {.    SE
2a70: 4c 45 43 54 20 71 75 6f 74 65 28 63 29 20 46 52  LECT quote(c) FR
2a80: 4f 4d 20 63 41 3b 0a 20 20 7d 0a 7d 20 7b 4e 55  OM cA;.  }.} {NU
2a90: 4c 4c 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b  LL}.do_test e_fk
2aa0: 65 79 2d 32 31 2e 34 20 7b 0a 20 20 65 78 65 63  ey-21.4 {.  exec
2ab0: 73 71 6c 20 7b 0a 20 20 20 20 55 50 44 41 54 45  sql {.    UPDATE
2ac0: 20 70 41 20 53 45 54 20 78 20 3d 20 58 27 38 37   pA SET x = X'87
2ad0: 36 35 27 20 57 48 45 52 45 20 72 6f 77 69 64 20  65' WHERE rowid 
2ae0: 3d 20 32 3b 0a 20 20 20 20 53 45 4c 45 43 54 20  = 2;.    SELECT 
2af0: 71 75 6f 74 65 28 78 29 20 46 52 4f 4d 20 70 41  quote(x) FROM pA
2b00: 3b 0a 20 20 7d 0a 7d 20 7b 58 27 38 37 36 35 27  ;.  }.} {X'8765'
2b10: 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79  }.do_test e_fkey
2b20: 2d 32 31 2e 35 20 7b 0a 20 20 65 78 65 63 73 71  -21.5 {.  execsq
2b30: 6c 20 7b 20 53 45 4c 45 43 54 20 71 75 6f 74 65  l { SELECT quote
2b40: 28 63 29 20 46 52 4f 4d 20 63 42 20 7d 0a 7d 20  (c) FROM cB }.} 
2b50: 7b 4e 55 4c 4c 7d 0a 0a 23 2d 2d 2d 2d 2d 2d 2d  {NULL}..#-------
2b60: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2b70: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2b80: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2b90: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2ba0: 2d 2d 0a 23 20 2f 2a 20 45 56 3a 20 52 2d 34 33  --.# /* EV: R-43
2bb0: 30 35 34 2d 35 34 38 33 32 20 2a 2f 0a 23 0a 23  054-54832 */.#.#
2bc0: 20 54 65 73 74 20 53 45 54 20 44 45 46 41 55 4c   Test SET DEFAUL
2bd0: 54 20 61 63 74 69 6f 6e 73 2e 0a 23 0a 64 72 6f  T actions..#.dro
2be0: 70 5f 61 6c 6c 5f 74 61 62 6c 65 73 0a 64 6f 5f  p_all_tables.do_
2bf0: 74 65 73 74 20 65 5f 66 6b 65 79 2d 32 32 2e 31  test e_fkey-22.1
2c00: 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20   {.  execsql {. 
2c10: 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20     CREATE TABLE 
2c20: 70 41 28 78 20 50 52 49 4d 41 52 59 20 4b 45 59  pA(x PRIMARY KEY
2c30: 29 3b 0a 20 20 20 20 43 52 45 41 54 45 20 54 41  );.    CREATE TA
2c40: 42 4c 45 20 63 41 28 63 20 44 45 46 41 55 4c 54  BLE cA(c DEFAULT
2c50: 20 58 27 30 30 30 30 27 20 52 45 46 45 52 45 4e   X'0000' REFEREN
2c60: 43 45 53 20 70 41 20 4f 4e 20 44 45 4c 45 54 45  CES pA ON DELETE
2c70: 20 53 45 54 20 44 45 46 41 55 4c 54 29 3b 0a 20   SET DEFAULT);. 
2c80: 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20     CREATE TABLE 
2c90: 63 42 28 63 20 44 45 46 41 55 4c 54 20 58 27 39  cB(c DEFAULT X'9
2ca0: 39 39 39 27 20 52 45 46 45 52 45 4e 43 45 53 20  999' REFERENCES 
2cb0: 70 41 20 4f 4e 20 55 50 44 41 54 45 20 53 45 54  pA ON UPDATE SET
2cc0: 20 44 45 46 41 55 4c 54 29 3b 0a 0a 20 20 20 20   DEFAULT);..    
2cd0: 49 4e 53 45 52 54 20 49 4e 54 4f 20 70 41 28 72  INSERT INTO pA(r
2ce0: 6f 77 69 64 2c 20 78 29 20 56 41 4c 55 45 53 28  owid, x) VALUES(
2cf0: 31 2c 20 58 27 30 30 30 30 27 29 3b 0a 20 20 20  1, X'0000');.   
2d00: 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 70 41 28   INSERT INTO pA(
2d10: 72 6f 77 69 64 2c 20 78 29 20 56 41 4c 55 45 53  rowid, x) VALUES
2d20: 28 32 2c 20 58 27 39 39 39 39 27 29 3b 0a 20 20  (2, X'9999');.  
2d30: 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 70 41    INSERT INTO pA
2d40: 28 72 6f 77 69 64 2c 20 78 29 20 56 41 4c 55 45  (rowid, x) VALUE
2d50: 53 28 33 2c 20 58 27 41 42 43 44 27 29 3b 0a 20  S(3, X'ABCD');. 
2d60: 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 70     INSERT INTO p
2d70: 41 28 72 6f 77 69 64 2c 20 78 29 20 56 41 4c 55  A(rowid, x) VALU
2d80: 45 53 28 34 2c 20 58 27 31 32 33 34 27 29 3b 0a  ES(4, X'1234');.
2d90: 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f  .    INSERT INTO
2da0: 20 63 41 20 56 41 4c 55 45 53 28 58 27 41 42 43   cA VALUES(X'ABC
2db0: 44 27 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20  D');.    INSERT 
2dc0: 49 4e 54 4f 20 63 42 20 56 41 4c 55 45 53 28 58  INTO cB VALUES(X
2dd0: 27 31 32 33 34 27 29 3b 0a 20 20 7d 0a 7d 20 7b  '1234');.  }.} {
2de0: 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79  }.do_test e_fkey
2df0: 2d 32 32 2e 32 20 7b 0a 20 20 65 78 65 63 73 71  -22.2 {.  execsq
2e00: 6c 20 7b 0a 20 20 20 20 44 45 4c 45 54 45 20 46  l {.    DELETE F
2e10: 52 4f 4d 20 70 41 20 57 48 45 52 45 20 72 6f 77  ROM pA WHERE row
2e20: 69 64 20 3d 20 33 3b 0a 20 20 20 20 53 45 4c 45  id = 3;.    SELE
2e30: 43 54 20 71 75 6f 74 65 28 78 29 20 46 52 4f 4d  CT quote(x) FROM
2e40: 20 70 41 3b 0a 20 20 7d 0a 7d 20 7b 58 27 30 30   pA;.  }.} {X'00
2e50: 30 30 27 20 58 27 39 39 39 39 27 20 58 27 31 32  00' X'9999' X'12
2e60: 33 34 27 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66  34'}.do_test e_f
2e70: 6b 65 79 2d 32 32 2e 33 20 7b 0a 20 20 65 78 65  key-22.3 {.  exe
2e80: 63 73 71 6c 20 7b 20 53 45 4c 45 43 54 20 71 75  csql { SELECT qu
2e90: 6f 74 65 28 63 29 20 46 52 4f 4d 20 63 41 20 7d  ote(c) FROM cA }
2ea0: 0a 7d 20 7b 58 27 30 30 30 30 27 7d 0a 64 6f 5f  .} {X'0000'}.do_
2eb0: 74 65 73 74 20 65 5f 66 6b 65 79 2d 32 32 2e 34  test e_fkey-22.4
2ec0: 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20   {.  execsql {. 
2ed0: 20 20 20 55 50 44 41 54 45 20 70 41 20 53 45 54     UPDATE pA SET
2ee0: 20 78 20 3d 20 58 27 38 37 36 35 27 20 57 48 45   x = X'8765' WHE
2ef0: 52 45 20 72 6f 77 69 64 20 3d 20 34 3b 0a 20 20  RE rowid = 4;.  
2f00: 20 20 53 45 4c 45 43 54 20 71 75 6f 74 65 28 78    SELECT quote(x
2f10: 29 20 46 52 4f 4d 20 70 41 3b 0a 20 20 7d 0a 7d  ) FROM pA;.  }.}
2f20: 20 7b 58 27 30 30 30 30 27 20 58 27 39 39 39 39   {X'0000' X'9999
2f30: 27 20 58 27 38 37 36 35 27 7d 0a 64 6f 5f 74 65  ' X'8765'}.do_te
2f40: 73 74 20 65 5f 66 6b 65 79 2d 32 32 2e 35 20 7b  st e_fkey-22.5 {
2f50: 0a 20 20 65 78 65 63 73 71 6c 20 7b 20 53 45 4c  .  execsql { SEL
2f60: 45 43 54 20 71 75 6f 74 65 28 63 29 20 46 52 4f  ECT quote(c) FRO
2f70: 4d 20 63 42 20 7d 0a 7d 20 7b 58 27 39 39 39 39  M cB }.} {X'9999
2f80: 27 7d 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  '}..#-----------
2f90: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2fa0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2fb0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2fc0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23  --------------.#
2fd0: 20 2f 2a 20 45 56 3a 20 52 2d 36 31 33 37 36 2d   /* EV: R-61376-
2fe0: 35 37 32 36 37 20 2a 2f 0a 23 20 2f 2a 20 45 56  57267 */.# /* EV
2ff0: 3a 20 52 2d 36 31 38 30 39 2d 36 32 32 30 37 20  : R-61809-62207 
3000: 2a 2f 0a 23 0a 23 20 54 65 73 74 20 4f 4e 20 44  */.#.# Test ON D
3010: 45 4c 45 54 45 20 43 41 53 43 41 44 45 20 61 63  ELETE CASCADE ac
3020: 74 69 6f 6e 73 2e 0a 23 0a 64 72 6f 70 5f 61 6c  tions..#.drop_al
3030: 6c 5f 74 61 62 6c 65 73 0a 64 6f 5f 74 65 73 74  l_tables.do_test
3040: 20 65 5f 66 6b 65 79 2d 32 33 2e 31 20 7b 0a 20   e_fkey-23.1 {. 
3050: 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 43   execsql {.    C
3060: 52 45 41 54 45 20 54 41 42 4c 45 20 70 31 28 61  REATE TABLE p1(a
3070: 2c 20 62 20 55 4e 49 51 55 45 29 3b 0a 20 20 20  , b UNIQUE);.   
3080: 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 63 31   CREATE TABLE c1
3090: 28 63 20 52 45 46 45 52 45 4e 43 45 53 20 70 31  (c REFERENCES p1
30a0: 28 62 29 20 4f 4e 20 44 45 4c 45 54 45 20 43 41  (b) ON DELETE CA
30b0: 53 43 41 44 45 2c 20 64 29 3b 0a 20 20 20 20 49  SCADE, d);.    I
30c0: 4e 53 45 52 54 20 49 4e 54 4f 20 70 31 20 56 41  NSERT INTO p1 VA
30d0: 4c 55 45 53 28 4e 55 4c 4c 2c 20 4e 55 4c 4c 29  LUES(NULL, NULL)
30e0: 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54  ;.    INSERT INT
30f0: 4f 20 70 31 20 56 41 4c 55 45 53 28 34 2c 20 34  O p1 VALUES(4, 4
3100: 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e  );.    INSERT IN
3110: 54 4f 20 70 31 20 56 41 4c 55 45 53 28 35 2c 20  TO p1 VALUES(5, 
3120: 35 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49  5);.    INSERT I
3130: 4e 54 4f 20 63 31 20 56 41 4c 55 45 53 28 4e 55  NTO c1 VALUES(NU
3140: 4c 4c 2c 20 4e 55 4c 4c 29 3b 0a 20 20 20 20 49  LL, NULL);.    I
3150: 4e 53 45 52 54 20 49 4e 54 4f 20 63 31 20 56 41  NSERT INTO c1 VA
3160: 4c 55 45 53 28 34 2c 20 34 29 3b 0a 20 20 20 20  LUES(4, 4);.    
3170: 49 4e 53 45 52 54 20 49 4e 54 4f 20 63 31 20 56  INSERT INTO c1 V
3180: 41 4c 55 45 53 28 35 2c 20 35 29 3b 0a 20 20 20  ALUES(5, 5);.   
3190: 20 53 45 4c 45 43 54 20 63 6f 75 6e 74 28 2a 29   SELECT count(*)
31a0: 20 46 52 4f 4d 20 63 31 3b 0a 20 20 7d 0a 7d 20   FROM c1;.  }.} 
31b0: 7b 33 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b  {3}.do_test e_fk
31c0: 65 79 2d 32 33 2e 32 20 7b 0a 20 20 65 78 65 63  ey-23.2 {.  exec
31d0: 73 71 6c 20 7b 0a 20 20 20 20 44 45 4c 45 54 45  sql {.    DELETE
31e0: 20 46 52 4f 4d 20 70 31 20 57 48 45 52 45 20 61   FROM p1 WHERE a
31f0: 20 3d 20 34 3b 0a 20 20 20 20 53 45 4c 45 43 54   = 4;.    SELECT
3200: 20 64 2c 20 63 20 46 52 4f 4d 20 63 31 3b 0a 20   d, c FROM c1;. 
3210: 20 7d 0a 7d 20 7b 7b 7d 20 7b 7d 20 35 20 35 7d   }.} {{} {} 5 5}
3220: 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d  .do_test e_fkey-
3230: 32 33 2e 33 20 7b 0a 20 20 65 78 65 63 73 71 6c  23.3 {.  execsql
3240: 20 7b 0a 20 20 20 20 44 45 4c 45 54 45 20 46 52   {.    DELETE FR
3250: 4f 4d 20 70 31 3b 0a 20 20 20 20 53 45 4c 45 43  OM p1;.    SELEC
3260: 54 20 64 2c 20 63 20 46 52 4f 4d 20 63 31 3b 0a  T d, c FROM c1;.
3270: 20 20 7d 0a 7d 20 7b 7b 7d 20 7b 7d 7d 0a 64 6f    }.} {{} {}}.do
3280: 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 32 33 2e  _test e_fkey-23.
3290: 34 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 20  4 {.  execsql { 
32a0: 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 70 31  SELECT * FROM p1
32b0: 20 7d 0a 7d 20 7b 7d 0a 0a 0a 23 2d 2d 2d 2d 2d   }.} {}...#-----
32c0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
32d0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
32e0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
32f0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
3300: 2d 2d 2d 2d 0a 23 20 2f 2a 20 45 56 3a 20 52 2d  ----.# /* EV: R-
3310: 36 31 33 37 36 2d 35 37 32 36 37 20 2a 2f 0a 23  61376-57267 */.#
3320: 20 2f 2a 20 45 56 3a 20 52 2d 31 33 38 37 37 2d   /* EV: R-13877-
3330: 36 34 35 34 32 20 2a 2f 0a 23 0a 23 20 54 65 73  64542 */.#.# Tes
3340: 74 20 4f 4e 20 55 50 44 41 54 45 20 43 41 53 43  t ON UPDATE CASC
3350: 41 44 45 20 61 63 74 69 6f 6e 73 2e 0a 23 0a 64  ADE actions..#.d
3360: 72 6f 70 5f 61 6c 6c 5f 74 61 62 6c 65 73 0a 64  rop_all_tables.d
3370: 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 32 34  o_test e_fkey-24
3380: 2e 31 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b  .1 {.  execsql {
3390: 0a 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c  .    CREATE TABL
33a0: 45 20 70 31 28 61 2c 20 62 20 55 4e 49 51 55 45  E p1(a, b UNIQUE
33b0: 29 3b 0a 20 20 20 20 43 52 45 41 54 45 20 54 41  );.    CREATE TA
33c0: 42 4c 45 20 63 31 28 63 20 52 45 46 45 52 45 4e  BLE c1(c REFEREN
33d0: 43 45 53 20 70 31 28 62 29 20 4f 4e 20 55 50 44  CES p1(b) ON UPD
33e0: 41 54 45 20 43 41 53 43 41 44 45 2c 20 64 29 3b  ATE CASCADE, d);
33f0: 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f  .    INSERT INTO
3400: 20 70 31 20 56 41 4c 55 45 53 28 4e 55 4c 4c 2c   p1 VALUES(NULL,
3410: 20 4e 55 4c 4c 29 3b 0a 20 20 20 20 49 4e 53 45   NULL);.    INSE
3420: 52 54 20 49 4e 54 4f 20 70 31 20 56 41 4c 55 45  RT INTO p1 VALUE
3430: 53 28 34 2c 20 34 29 3b 0a 20 20 20 20 49 4e 53  S(4, 4);.    INS
3440: 45 52 54 20 49 4e 54 4f 20 70 31 20 56 41 4c 55  ERT INTO p1 VALU
3450: 45 53 28 35 2c 20 35 29 3b 0a 20 20 20 20 49 4e  ES(5, 5);.    IN
3460: 53 45 52 54 20 49 4e 54 4f 20 63 31 20 56 41 4c  SERT INTO c1 VAL
3470: 55 45 53 28 4e 55 4c 4c 2c 20 4e 55 4c 4c 29 3b  UES(NULL, NULL);
3480: 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f  .    INSERT INTO
3490: 20 63 31 20 56 41 4c 55 45 53 28 34 2c 20 34 29   c1 VALUES(4, 4)
34a0: 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54  ;.    INSERT INT
34b0: 4f 20 63 31 20 56 41 4c 55 45 53 28 35 2c 20 35  O c1 VALUES(5, 5
34c0: 29 3b 0a 20 20 20 20 53 45 4c 45 43 54 20 63 6f  );.    SELECT co
34d0: 75 6e 74 28 2a 29 20 46 52 4f 4d 20 63 31 3b 0a  unt(*) FROM c1;.
34e0: 20 20 7d 0a 7d 20 7b 33 7d 0a 64 6f 5f 74 65 73    }.} {3}.do_tes
34f0: 74 20 65 5f 66 6b 65 79 2d 32 34 2e 32 20 7b 0a  t e_fkey-24.2 {.
3500: 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20    execsql {.    
3510: 55 50 44 41 54 45 20 70 31 20 53 45 54 20 62 20  UPDATE p1 SET b 
3520: 3d 20 31 30 20 57 48 45 52 45 20 62 20 3d 20 35  = 10 WHERE b = 5
3530: 3b 0a 20 20 20 20 53 45 4c 45 43 54 20 64 2c 20  ;.    SELECT d, 
3540: 63 20 46 52 4f 4d 20 63 31 3b 0a 20 20 7d 0a 7d  c FROM c1;.  }.}
3550: 20 7b 7b 7d 20 7b 7d 20 34 20 34 20 35 20 31 30   {{} {} 4 4 5 10
3560: 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79  }.do_test e_fkey
3570: 2d 32 34 2e 33 20 7b 0a 20 20 65 78 65 63 73 71  -24.3 {.  execsq
3580: 6c 20 7b 0a 20 20 20 20 55 50 44 41 54 45 20 70  l {.    UPDATE p
3590: 31 20 53 45 54 20 62 20 3d 20 31 31 20 57 48 45  1 SET b = 11 WHE
35a0: 52 45 20 62 20 3d 20 34 3b 0a 20 20 20 20 53 45  RE b = 4;.    SE
35b0: 4c 45 43 54 20 64 2c 20 63 20 46 52 4f 4d 20 63  LECT d, c FROM c
35c0: 31 3b 0a 20 20 7d 0a 7d 20 7b 7b 7d 20 7b 7d 20  1;.  }.} {{} {} 
35d0: 34 20 31 31 20 35 20 31 30 7d 0a 64 6f 5f 74 65  4 11 5 10}.do_te
35e0: 73 74 20 65 5f 66 6b 65 79 2d 32 34 2e 34 20 7b  st e_fkey-24.4 {
35f0: 0a 20 20 65 78 65 63 73 71 6c 20 7b 20 0a 20 20  .  execsql { .  
3600: 20 20 55 50 44 41 54 45 20 70 31 20 53 45 54 20    UPDATE p1 SET 
3610: 62 20 3d 20 36 20 57 48 45 52 45 20 62 20 49 53  b = 6 WHERE b IS
3620: 20 4e 55 4c 4c 3b 0a 20 20 20 20 53 45 4c 45 43   NULL;.    SELEC
3630: 54 20 64 2c 20 63 20 46 52 4f 4d 20 63 31 3b 0a  T d, c FROM c1;.
3640: 20 20 7d 0a 7d 20 7b 7b 7d 20 7b 7d 20 34 20 31    }.} {{} {} 4 1
3650: 31 20 35 20 31 30 7d 0a 64 6f 5f 74 65 73 74 20  1 5 10}.do_test 
3660: 65 5f 66 6b 65 79 2d 32 33 2e 35 20 7b 0a 20 20  e_fkey-23.5 {.  
3670: 65 78 65 63 73 71 6c 20 7b 20 53 45 4c 45 43 54  execsql { SELECT
3680: 20 2a 20 46 52 4f 4d 20 70 31 20 7d 0a 7d 20 7b   * FROM p1 }.} {
3690: 7b 7d 20 36 20 34 20 31 31 20 35 20 31 30 7d 0a  {} 6 4 11 5 10}.
36a0: 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  .#--------------
36b0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
36c0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
36d0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
36e0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20 2f 2a  -----------.# /*
36f0: 20 45 56 3a 20 52 2d 35 31 33 32 39 2d 33 33 34   EV: R-51329-334
3700: 33 38 20 2a 2f 0a 23 0a 23 20 54 65 73 74 20 61  38 */.#.# Test a
3710: 6e 20 65 78 61 6d 70 6c 65 20 66 72 6f 6d 20 74  n example from t
3720: 68 65 20 22 4f 4e 20 44 45 4c 45 54 45 20 61 6e  he "ON DELETE an
3730: 64 20 4f 4e 20 55 50 44 41 54 45 20 41 63 74 69  d ON UPDATE Acti
3740: 6f 6e 73 22 20 73 65 63 74 69 6f 6e 20 0a 23 20  ons" section .# 
3750: 6f 66 20 66 6f 72 65 69 67 6e 6b 65 79 73 2e 68  of foreignkeys.h
3760: 74 6d 6c 2e 0a 23 0a 64 72 6f 70 5f 61 6c 6c 5f  tml..#.drop_all_
3770: 74 61 62 6c 65 73 0a 64 6f 5f 74 65 73 74 20 65  tables.do_test e
3780: 5f 66 6b 65 79 2d 31 35 2e 31 20 7b 0a 20 20 65  _fkey-15.1 {.  e
3790: 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 43 52 45  xecsql {.    CRE
37a0: 41 54 45 20 54 41 42 4c 45 20 61 72 74 69 73 74  ATE TABLE artist
37b0: 28 0a 20 20 20 20 20 20 61 72 74 69 73 74 69 64  (.      artistid
37c0: 20 20 20 20 49 4e 54 45 47 45 52 20 50 52 49 4d      INTEGER PRIM
37d0: 41 52 59 20 4b 45 59 2c 20 0a 20 20 20 20 20 20  ARY KEY, .      
37e0: 61 72 74 69 73 74 6e 61 6d 65 20 20 54 45 58 54  artistname  TEXT
37f0: 0a 20 20 20 20 29 3b 0a 20 20 20 20 43 52 45 41  .    );.    CREA
3800: 54 45 20 54 41 42 4c 45 20 74 72 61 63 6b 28 0a  TE TABLE track(.
3810: 20 20 20 20 20 20 74 72 61 63 6b 69 64 20 20 20        trackid   
3820: 20 20 49 4e 54 45 47 45 52 2c 0a 20 20 20 20 20    INTEGER,.     
3830: 20 74 72 61 63 6b 6e 61 6d 65 20 20 20 54 45 58   trackname   TEX
3840: 54 2c 20 0a 20 20 20 20 20 20 74 72 61 63 6b 61  T, .      tracka
3850: 72 74 69 73 74 20 49 4e 54 45 47 45 52 20 52 45  rtist INTEGER RE
3860: 46 45 52 45 4e 43 45 53 20 61 72 74 69 73 74 28  FERENCES artist(
3870: 61 72 74 69 73 74 69 64 29 20 4f 4e 20 55 50 44  artistid) ON UPD
3880: 41 54 45 20 43 41 53 43 41 44 45 0a 20 20 20 20  ATE CASCADE.    
3890: 29 3b 0a 0a 20 20 20 20 49 4e 53 45 52 54 20 49  );..    INSERT I
38a0: 4e 54 4f 20 61 72 74 69 73 74 20 56 41 4c 55 45  NTO artist VALUE
38b0: 53 28 31 2c 20 27 44 65 61 6e 20 4d 61 72 74 69  S(1, 'Dean Marti
38c0: 6e 27 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20  n');.    INSERT 
38d0: 49 4e 54 4f 20 61 72 74 69 73 74 20 56 41 4c 55  INTO artist VALU
38e0: 45 53 28 32 2c 20 27 46 72 61 6e 6b 20 53 69 6e  ES(2, 'Frank Sin
38f0: 61 74 72 61 27 29 3b 0a 20 20 20 20 49 4e 53 45  atra');.    INSE
3900: 52 54 20 49 4e 54 4f 20 74 72 61 63 6b 20 56 41  RT INTO track VA
3910: 4c 55 45 53 28 31 31 2c 20 27 54 68 61 74 27 27  LUES(11, 'That''
3920: 73 20 41 6d 6f 72 65 27 2c 20 31 29 3b 0a 20 20  s Amore', 1);.  
3930: 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 72    INSERT INTO tr
3940: 61 63 6b 20 56 41 4c 55 45 53 28 31 32 2c 20 27  ack VALUES(12, '
3950: 43 68 72 69 73 74 6d 61 73 20 42 6c 75 65 73 27  Christmas Blues'
3960: 2c 20 31 29 3b 0a 20 20 20 20 49 4e 53 45 52 54  , 1);.    INSERT
3970: 20 49 4e 54 4f 20 74 72 61 63 6b 20 56 41 4c 55   INTO track VALU
3980: 45 53 28 31 33 2c 20 27 4d 79 20 57 61 79 27 2c  ES(13, 'My Way',
3990: 20 32 29 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a 64 6f   2);.  }.} {}.do
39a0: 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 31 35 2e  _test e_fkey-15.
39b0: 32 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a  2 {.  execsql {.
39c0: 20 20 20 20 55 50 44 41 54 45 20 61 72 74 69 73      UPDATE artis
39d0: 74 20 53 45 54 20 61 72 74 69 73 74 69 64 20 3d  t SET artistid =
39e0: 20 31 30 30 20 57 48 45 52 45 20 61 72 74 69 73   100 WHERE artis
39f0: 74 6e 61 6d 65 20 3d 20 27 44 65 61 6e 20 4d 61  tname = 'Dean Ma
3a00: 72 74 69 6e 27 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a  rtin';.  }.} {}.
3a10: 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 31  do_test e_fkey-1
3a20: 35 2e 33 20 7b 0a 20 20 65 78 65 63 73 71 6c 20  5.3 {.  execsql 
3a30: 7b 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20  { SELECT * FROM 
3a40: 61 72 74 69 73 74 20 7d 0a 7d 20 7b 32 20 7b 46  artist }.} {2 {F
3a50: 72 61 6e 6b 20 53 69 6e 61 74 72 61 7d 20 31 30  rank Sinatra} 10
3a60: 30 20 7b 44 65 61 6e 20 4d 61 72 74 69 6e 7d 7d  0 {Dean Martin}}
3a70: 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d  .do_test e_fkey-
3a80: 31 35 2e 34 20 7b 0a 20 20 65 78 65 63 73 71 6c  15.4 {.  execsql
3a90: 20 7b 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d   { SELECT * FROM
3aa0: 20 74 72 61 63 6b 20 7d 0a 7d 20 7b 31 31 20 7b   track }.} {11 {
3ab0: 54 68 61 74 27 73 20 41 6d 6f 72 65 7d 20 31 30  That's Amore} 10
3ac0: 30 20 31 32 20 7b 43 68 72 69 73 74 6d 61 73 20  0 12 {Christmas 
3ad0: 42 6c 75 65 73 7d 20 31 30 30 20 31 33 20 7b 4d  Blues} 100 13 {M
3ae0: 79 20 57 61 79 7d 20 32 7d 0a 0a 0a 23 2d 2d 2d  y Way} 2}...#---
3af0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
3b00: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
3b10: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
3b20: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
3b30: 2d 2d 2d 2d 2d 2d 0a 23 20 2f 2a 20 45 56 3a 20  ------.# /* EV: 
3b40: 52 2d 35 33 39 36 38 2d 35 31 36 34 32 20 2a 2f  R-53968-51642 */
3b50: 0a 23 0a 23 20 56 65 72 69 66 79 20 74 68 61 74  .#.# Verify that
3b60: 20 61 64 64 69 6e 67 20 61 6e 20 46 4b 20 61 63   adding an FK ac
3b70: 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 61 62  tion does not ab
3b80: 73 6f 6c 76 65 20 74 68 65 20 75 73 65 72 20 6f  solve the user o
3b90: 66 20 74 68 65 20 0a 23 20 72 65 71 75 69 72 65  f the .# require
3ba0: 6d 65 6e 74 20 6e 6f 74 20 74 6f 20 76 69 6f 6c  ment not to viol
3bb0: 61 74 65 20 74 68 65 20 66 6f 72 65 69 67 6e 20  ate the foreign 
3bc0: 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 2e 0a  key constraint..
3bd0: 23 0a 64 72 6f 70 5f 61 6c 6c 5f 74 61 62 6c 65  #.drop_all_table
3be0: 73 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79  s.do_test e_fkey
3bf0: 2d 32 35 2e 31 20 7b 0a 20 20 65 78 65 63 73 71  -25.1 {.  execsq
3c00: 6c 20 7b 0a 20 20 20 20 43 52 45 41 54 45 20 54  l {.    CREATE T
3c10: 41 42 4c 45 20 70 61 72 65 6e 74 28 61 20 43 4f  ABLE parent(a CO
3c20: 4c 4c 41 54 45 20 6e 6f 63 61 73 65 2c 20 62 2c  LLATE nocase, b,
3c30: 20 63 2c 20 50 52 49 4d 41 52 59 20 4b 45 59 28   c, PRIMARY KEY(
3c40: 63 2c 20 61 29 29 3b 0a 20 20 20 20 43 52 45 41  c, a));.    CREA
3c50: 54 45 20 54 41 42 4c 45 20 63 68 69 6c 64 28 64  TE TABLE child(d
3c60: 20 44 45 46 41 55 4c 54 20 27 61 27 2c 20 65 2c   DEFAULT 'a', e,
3c70: 20 66 20 44 45 46 41 55 4c 54 20 27 63 27 2c 0a   f DEFAULT 'c',.
3c80: 20 20 20 20 20 20 46 4f 52 45 49 47 4e 20 4b 45        FOREIGN KE
3c90: 59 28 66 2c 20 64 29 20 52 45 46 45 52 45 4e 43  Y(f, d) REFERENC
3ca0: 45 53 20 70 61 72 65 6e 74 20 4f 4e 20 55 50 44  ES parent ON UPD
3cb0: 41 54 45 20 53 45 54 20 44 45 46 41 55 4c 54 0a  ATE SET DEFAULT.
3cc0: 20 20 20 20 29 3b 0a 0a 20 20 20 20 49 4e 53 45      );..    INSE
3cd0: 52 54 20 49 4e 54 4f 20 70 61 72 65 6e 74 20 56  RT INTO parent V
3ce0: 41 4c 55 45 53 28 27 41 27 2c 20 27 62 27 2c 20  ALUES('A', 'b', 
3cf0: 27 63 27 29 3b 0a 20 20 20 20 49 4e 53 45 52 54  'c');.    INSERT
3d00: 20 49 4e 54 4f 20 70 61 72 65 6e 74 20 56 41 4c   INTO parent VAL
3d10: 55 45 53 28 27 4f 4e 45 27 2c 20 27 74 77 6f 27  UES('ONE', 'two'
3d20: 2c 20 27 74 68 72 65 65 27 29 3b 0a 20 20 20 20  , 'three');.    
3d30: 49 4e 53 45 52 54 20 49 4e 54 4f 20 63 68 69 6c  INSERT INTO chil
3d40: 64 20 56 41 4c 55 45 53 28 27 6f 6e 65 27 2c 20  d VALUES('one', 
3d50: 27 74 77 6f 27 2c 20 27 74 68 72 65 65 27 29 3b  'two', 'three');
3d60: 0a 20 20 7d 0a 7d 20 7b 7d 0a 64 6f 5f 74 65 73  .  }.} {}.do_tes
3d70: 74 20 65 5f 66 6b 65 79 2d 32 35 2e 32 20 7b 0a  t e_fkey-25.2 {.
3d80: 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20    execsql {.    
3d90: 42 45 47 49 4e 3b 0a 20 20 20 20 20 20 55 50 44  BEGIN;.      UPD
3da0: 41 54 45 20 70 61 72 65 6e 74 20 53 45 54 20 61  ATE parent SET a
3db0: 20 3d 20 27 27 20 57 48 45 52 45 20 61 20 3d 20   = '' WHERE a = 
3dc0: 27 6f 4e 65 27 3b 0a 20 20 20 20 20 20 53 45 4c  'oNe';.      SEL
3dd0: 45 43 54 20 2a 20 46 52 4f 4d 20 63 68 69 6c 64  ECT * FROM child
3de0: 3b 0a 20 20 7d 0a 7d 20 7b 61 20 74 77 6f 20 63  ;.  }.} {a two c
3df0: 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79  }.do_test e_fkey
3e00: 2d 32 35 2e 33 20 7b 0a 20 20 65 78 65 63 73 71  -25.3 {.  execsq
3e10: 6c 20 7b 0a 20 20 20 20 52 4f 4c 4c 42 41 43 4b  l {.    ROLLBACK
3e20: 3b 0a 20 20 20 20 44 45 4c 45 54 45 20 46 52 4f  ;.    DELETE FRO
3e30: 4d 20 70 61 72 65 6e 74 20 57 48 45 52 45 20 61  M parent WHERE a
3e40: 20 3d 20 27 41 27 3b 0a 20 20 20 20 53 45 4c 45   = 'A';.    SELE
3e50: 43 54 20 2a 20 46 52 4f 4d 20 70 61 72 65 6e 74  CT * FROM parent
3e60: 3b 0a 20 20 7d 0a 7d 20 7b 4f 4e 45 20 74 77 6f  ;.  }.} {ONE two
3e70: 20 74 68 72 65 65 7d 0a 64 6f 5f 74 65 73 74 20   three}.do_test 
3e80: 65 5f 66 6b 65 79 2d 32 35 2e 34 20 7b 0a 20 20  e_fkey-25.4 {.  
3e90: 63 61 74 63 68 73 71 6c 20 7b 20 55 50 44 41 54  catchsql { UPDAT
3ea0: 45 20 70 61 72 65 6e 74 20 53 45 54 20 61 20 3d  E parent SET a =
3eb0: 20 27 27 20 57 48 45 52 45 20 61 20 3d 20 27 6f   '' WHERE a = 'o
3ec0: 4e 65 27 20 7d 0a 7d 20 7b 31 20 7b 66 6f 72 65  Ne' }.} {1 {fore
3ed0: 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69  ign key constrai
3ee0: 6e 74 20 66 61 69 6c 65 64 7d 7d 0a 0a 0a 23 2d  nt failed}}...#-
3ef0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
3f00: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
3f10: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
3f20: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
3f30: 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20 2f 2a 20 45 56  --------.# /* EV
3f40: 3a 20 52 2d 30 37 30 36 35 2d 35 39 35 38 38 20  : R-07065-59588 
3f50: 2a 2f 0a 23 20 2f 2a 20 45 56 3a 20 52 2d 32 38  */.# /* EV: R-28
3f60: 32 32 30 2d 34 36 36 39 34 20 2a 2f 0a 23 0a 23  220-46694 */.#.#
3f70: 20 54 65 73 74 20 61 6e 20 65 78 61 6d 70 6c 65   Test an example
3f80: 20 66 72 6f 6d 20 74 68 65 20 22 4f 4e 20 44 45   from the "ON DE
3f90: 4c 45 54 45 20 61 6e 64 20 4f 4e 20 55 50 44 41  LETE and ON UPDA
3fa0: 54 45 20 41 63 74 69 6f 6e 73 22 20 73 65 63 74  TE Actions" sect
3fb0: 69 6f 6e 20 0a 23 20 6f 66 20 66 6f 72 65 69 67  ion .# of foreig
3fc0: 6e 6b 65 79 73 2e 68 74 6d 6c 2e 20 54 68 69 73  nkeys.html. This
3fd0: 20 65 78 61 6d 70 6c 65 20 73 68 6f 77 73 20 74   example shows t
3fe0: 68 61 74 20 61 64 64 69 6e 67 20 61 6e 20 22 4f  hat adding an "O
3ff0: 4e 20 44 45 4c 45 54 45 20 44 45 46 41 55 4c 54  N DELETE DEFAULT
4000: 22 0a 23 20 63 6c 61 75 73 65 20 64 6f 65 73 20  ".# clause does 
4010: 6e 6f 74 20 61 62 72 6f 67 61 74 65 20 74 68 65  not abrogate the
4020: 20 6e 65 65 64 20 74 6f 20 73 61 74 69 73 66 79   need to satisfy
4030: 20 74 68 65 20 66 6f 72 65 69 67 6e 20 6b 65 79   the foreign key
4040: 20 63 6f 6e 73 74 72 61 69 6e 74 0a 23 20 28 52   constraint.# (R
4050: 2d 32 38 32 32 30 2d 34 36 36 39 34 29 2e 0a 23  -28220-46694)..#
4060: 0a 64 72 6f 70 5f 61 6c 6c 5f 74 61 62 6c 65 73  .drop_all_tables
4070: 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d  .do_test e_fkey-
4080: 31 34 2e 31 20 7b 0a 20 20 65 78 65 63 73 71 6c  14.1 {.  execsql
4090: 20 7b 0a 20 20 20 20 43 52 45 41 54 45 20 54 41   {.    CREATE TA
40a0: 42 4c 45 20 61 72 74 69 73 74 28 0a 20 20 20 20  BLE artist(.    
40b0: 20 20 61 72 74 69 73 74 69 64 20 20 20 20 49 4e    artistid    IN
40c0: 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45  TEGER PRIMARY KE
40d0: 59 2c 20 0a 20 20 20 20 20 20 61 72 74 69 73 74  Y, .      artist
40e0: 6e 61 6d 65 20 20 54 45 58 54 0a 20 20 20 20 29  name  TEXT.    )
40f0: 3b 0a 20 20 20 20 43 52 45 41 54 45 20 54 41 42  ;.    CREATE TAB
4100: 4c 45 20 74 72 61 63 6b 28 0a 20 20 20 20 20 20  LE track(.      
4110: 74 72 61 63 6b 69 64 20 20 20 20 20 49 4e 54 45  trackid     INTE
4120: 47 45 52 2c 0a 20 20 20 20 20 20 74 72 61 63 6b  GER,.      track
4130: 6e 61 6d 65 20 20 20 54 45 58 54 2c 20 0a 20 20  name   TEXT, .  
4140: 20 20 20 20 74 72 61 63 6b 61 72 74 69 73 74 20      trackartist 
4150: 49 4e 54 45 47 45 52 20 44 45 46 41 55 4c 54 20  INTEGER DEFAULT 
4160: 30 20 52 45 46 45 52 45 4e 43 45 53 20 61 72 74  0 REFERENCES art
4170: 69 73 74 28 61 72 74 69 73 74 69 64 29 20 4f 4e  ist(artistid) ON
4180: 20 44 45 4c 45 54 45 20 53 45 54 20 44 45 46 41   DELETE SET DEFA
4190: 55 4c 54 0a 20 20 20 20 29 3b 0a 20 20 20 20 49  ULT.    );.    I
41a0: 4e 53 45 52 54 20 49 4e 54 4f 20 61 72 74 69 73  NSERT INTO artis
41b0: 74 20 56 41 4c 55 45 53 28 33 2c 20 27 53 61 6d  t VALUES(3, 'Sam
41c0: 6d 79 20 44 61 76 69 73 20 4a 72 2e 27 29 3b 0a  my Davis Jr.');.
41d0: 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20      INSERT INTO 
41e0: 74 72 61 63 6b 20 56 41 4c 55 45 53 28 31 34 2c  track VALUES(14,
41f0: 20 27 4d 72 2e 20 42 6f 6a 61 6e 67 6c 65 73 27   'Mr. Bojangles'
4200: 2c 20 33 29 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a 64  , 3);.  }.} {}.d
4210: 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 31 34  o_test e_fkey-14
4220: 2e 32 20 7b 0a 20 20 63 61 74 63 68 73 71 6c 20  .2 {.  catchsql 
4230: 7b 20 44 45 4c 45 54 45 20 46 52 4f 4d 20 61 72  { DELETE FROM ar
4240: 74 69 73 74 20 57 48 45 52 45 20 61 72 74 69 73  tist WHERE artis
4250: 74 6e 61 6d 65 20 3d 20 27 53 61 6d 6d 79 20 44  tname = 'Sammy D
4260: 61 76 69 73 20 4a 72 2e 27 20 7d 0a 7d 20 7b 31  avis Jr.' }.} {1
4270: 20 7b 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f   {foreign key co
4280: 6e 73 74 72 61 69 6e 74 20 66 61 69 6c 65 64 7d  nstraint failed}
4290: 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79  }.do_test e_fkey
42a0: 2d 31 34 2e 33 20 7b 0a 20 20 65 78 65 63 73 71  -14.3 {.  execsq
42b0: 6c 20 7b 0a 20 20 20 20 49 4e 53 45 52 54 20 49  l {.    INSERT I
42c0: 4e 54 4f 20 61 72 74 69 73 74 20 56 41 4c 55 45  NTO artist VALUE
42d0: 53 28 30 2c 20 27 55 6e 6b 6e 6f 77 6e 20 41 72  S(0, 'Unknown Ar
42e0: 74 69 73 74 27 29 3b 0a 20 20 20 20 44 45 4c 45  tist');.    DELE
42f0: 54 45 20 46 52 4f 4d 20 61 72 74 69 73 74 20 57  TE FROM artist W
4300: 48 45 52 45 20 61 72 74 69 73 74 6e 61 6d 65 20  HERE artistname 
4310: 3d 20 27 53 61 6d 6d 79 20 44 61 76 69 73 20 4a  = 'Sammy Davis J
4320: 72 2e 27 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a 64 6f  r.';.  }.} {}.do
4330: 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 31 34 2e  _test e_fkey-14.
4340: 34 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 20  4 {.  execsql { 
4350: 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 61 72  SELECT * FROM ar
4360: 74 69 73 74 20 7d 0a 7d 20 7b 30 20 7b 55 6e 6b  tist }.} {0 {Unk
4370: 6e 6f 77 6e 20 41 72 74 69 73 74 7d 7d 0a 64 6f  nown Artist}}.do
4380: 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 31 34 2e  _test e_fkey-14.
4390: 35 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 20  5 {.  execsql { 
43a0: 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74 72  SELECT * FROM tr
43b0: 61 63 6b 20 7d 0a 7d 20 7b 31 34 20 7b 4d 72 2e  ack }.} {14 {Mr.
43c0: 20 42 6f 6a 61 6e 67 6c 65 73 7d 20 30 7d 0a 0a   Bojangles} 0}..
43d0: 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  #---------------
43e0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
43f0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
4400: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
4410: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20 2f 2a 20  ----------.# /* 
4420: 45 56 3a 20 52 2d 33 35 37 36 32 2d 33 30 33 30  EV: R-35762-3030
4430: 36 20 2a 2f 0a 23 0a 23 20 43 68 65 63 6b 20 74  6 */.#.# Check t
4440: 68 61 74 20 74 68 65 20 6f 72 64 65 72 20 6f 66  hat the order of
4450: 20 73 74 65 70 73 20 69 6e 20 61 6e 20 55 50 44   steps in an UPD
4460: 41 54 45 20 6f 72 20 44 45 4c 45 54 45 20 6f 6e  ATE or DELETE on
4470: 20 61 20 70 61 72 65 6e 74 20 0a 23 20 74 61 62   a parent .# tab
4480: 6c 65 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73  le is as follows
4490: 3a 0a 23 0a 23 20 20 20 31 2e 20 45 78 65 63 75  :.#.#   1. Execu
44a0: 74 65 20 61 70 70 6c 69 63 61 62 6c 65 20 42 45  te applicable BE
44b0: 46 4f 52 45 20 74 72 69 67 67 65 72 20 70 72 6f  FORE trigger pro
44c0: 67 72 61 6d 73 2c 0a 23 20 20 20 32 2e 20 43 68  grams,.#   2. Ch
44d0: 65 63 6b 20 6c 6f 63 61 6c 20 28 6e 6f 6e 20 66  eck local (non f
44e0: 6f 72 65 69 67 6e 20 6b 65 79 29 20 63 6f 6e 73  oreign key) cons
44f0: 74 72 61 69 6e 74 73 2c 0a 23 20 20 20 33 2e 20  traints,.#   3. 
4500: 55 70 64 61 74 65 20 6f 72 20 64 65 6c 65 74 65  Update or delete
4510: 20 74 68 65 20 72 6f 77 20 69 6e 20 74 68 65 20   the row in the 
4520: 70 61 72 65 6e 74 20 74 61 62 6c 65 2c 0a 23 20  parent table,.# 
4530: 20 20 34 2e 20 50 65 72 66 6f 72 6d 20 61 6e 79    4. Perform any
4540: 20 72 65 71 75 69 72 65 64 20 66 6f 72 65 69 67   required foreig
4550: 6e 20 6b 65 79 20 61 63 74 69 6f 6e 73 2c 0a 23  n key actions,.#
4560: 20 20 20 35 2e 20 45 78 65 63 75 74 65 20 61 70     5. Execute ap
4570: 70 6c 69 63 61 62 6c 65 20 41 46 54 45 52 20 74  plicable AFTER t
4580: 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d 73 2e  rigger programs.
4590: 20 0a 23 0a 64 72 6f 70 5f 61 6c 6c 5f 74 61 62   .#.drop_all_tab
45a0: 6c 65 73 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b  les.do_test e_fk
45b0: 65 79 2d 32 37 2e 31 20 7b 0a 20 20 70 72 6f 63  ey-27.1 {.  proc
45c0: 20 6d 61 78 70 61 72 65 6e 74 20 7b 61 72 67 73   maxparent {args
45d0: 7d 20 7b 20 64 62 20 6f 6e 65 20 7b 53 45 4c 45  } { db one {SELE
45e0: 43 54 20 6d 61 78 28 78 29 20 46 52 4f 4d 20 70  CT max(x) FROM p
45f0: 61 72 65 6e 74 7d 20 7d 0a 20 20 64 62 20 66 75  arent} }.  db fu
4600: 6e 63 20 6d 61 78 70 61 72 65 6e 74 20 6d 61 78  nc maxparent max
4610: 70 61 72 65 6e 74 0a 0a 20 20 65 78 65 63 73 71  parent..  execsq
4620: 6c 20 7b 0a 20 20 20 20 43 52 45 41 54 45 20 54  l {.    CREATE T
4630: 41 42 4c 45 20 70 61 72 65 6e 74 28 78 20 50 52  ABLE parent(x PR
4640: 49 4d 41 52 59 20 4b 45 59 29 3b 0a 0a 20 20 20  IMARY KEY);..   
4650: 20 43 52 45 41 54 45 20 54 52 49 47 47 45 52 20   CREATE TRIGGER 
4660: 62 75 20 42 45 46 4f 52 45 20 55 50 44 41 54 45  bu BEFORE UPDATE
4670: 20 4f 4e 20 70 61 72 65 6e 74 20 42 45 47 49 4e   ON parent BEGIN
4680: 0a 20 20 20 20 20 20 49 4e 53 45 52 54 20 49 4e  .      INSERT IN
4690: 54 4f 20 70 61 72 65 6e 74 20 56 41 4c 55 45 53  TO parent VALUES
46a0: 28 6e 65 77 2e 78 2d 6f 6c 64 2e 78 29 3b 0a 20  (new.x-old.x);. 
46b0: 20 20 20 45 4e 44 3b 0a 20 20 20 20 43 52 45 41     END;.    CREA
46c0: 54 45 20 54 41 42 4c 45 20 63 68 69 6c 64 28 0a  TE TABLE child(.
46d0: 20 20 20 20 20 20 61 20 44 45 46 41 55 4c 54 20        a DEFAULT 
46e0: 28 6d 61 78 70 61 72 65 6e 74 28 29 29 20 52 45  (maxparent()) RE
46f0: 46 45 52 45 4e 43 45 53 20 70 61 72 65 6e 74 20  FERENCES parent 
4700: 4f 4e 20 55 50 44 41 54 45 20 53 45 54 20 44 45  ON UPDATE SET DE
4710: 46 41 55 4c 54 0a 20 20 20 20 29 3b 0a 20 20 20  FAULT.    );.   
4720: 20 43 52 45 41 54 45 20 54 52 49 47 47 45 52 20   CREATE TRIGGER 
4730: 61 75 20 41 46 54 45 52 20 55 50 44 41 54 45 20  au AFTER UPDATE 
4740: 4f 4e 20 70 61 72 65 6e 74 20 42 45 47 49 4e 0a  ON parent BEGIN.
4750: 20 20 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54        INSERT INT
4760: 4f 20 70 61 72 65 6e 74 20 56 41 4c 55 45 53 28  O parent VALUES(
4770: 6e 65 77 2e 78 2b 6f 6c 64 2e 78 29 3b 0a 20 20  new.x+old.x);.  
4780: 20 20 45 4e 44 3b 0a 0a 20 20 20 20 49 4e 53 45    END;..    INSE
4790: 52 54 20 49 4e 54 4f 20 70 61 72 65 6e 74 20 56  RT INTO parent V
47a0: 41 4c 55 45 53 28 31 29 3b 0a 20 20 20 20 49 4e  ALUES(1);.    IN
47b0: 53 45 52 54 20 49 4e 54 4f 20 63 68 69 6c 64 20  SERT INTO child 
47c0: 56 41 4c 55 45 53 28 31 29 3b 0a 20 20 7d 0a 7d  VALUES(1);.  }.}
47d0: 20 7b 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b   {}.do_test e_fk
47e0: 65 79 2d 32 37 2e 32 20 7b 0a 20 20 65 78 65 63  ey-27.2 {.  exec
47f0: 73 71 6c 20 7b 0a 20 20 20 20 55 50 44 41 54 45  sql {.    UPDATE
4800: 20 70 61 72 65 6e 74 20 53 45 54 20 78 20 3d 20   parent SET x = 
4810: 32 32 3b 0a 20 20 20 20 53 45 4c 45 43 54 20 2a  22;.    SELECT *
4820: 20 46 52 4f 4d 20 70 61 72 65 6e 74 20 55 4e 49   FROM parent UNI
4830: 4f 4e 20 41 4c 4c 20 53 45 4c 45 43 54 20 27 78  ON ALL SELECT 'x
4840: 78 78 27 20 55 4e 49 4f 4e 20 41 4c 4c 20 53 45  xx' UNION ALL SE
4850: 4c 45 43 54 20 61 20 46 52 4f 4d 20 63 68 69 6c  LECT a FROM chil
4860: 64 3b 0a 20 20 7d 0a 7d 20 7b 32 32 20 32 31 20  d;.  }.} {22 21 
4870: 32 33 20 78 78 78 20 32 32 7d 0a 64 6f 5f 74 65  23 xxx 22}.do_te
4880: 73 74 20 65 5f 66 6b 65 79 2d 32 37 2e 33 20 7b  st e_fkey-27.3 {
4890: 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20  .  execsql {.   
48a0: 20 44 45 4c 45 54 45 20 46 52 4f 4d 20 63 68 69   DELETE FROM chi
48b0: 6c 64 3b 0a 20 20 20 20 44 45 4c 45 54 45 20 46  ld;.    DELETE F
48c0: 52 4f 4d 20 70 61 72 65 6e 74 3b 0a 20 20 20 20  ROM parent;.    
48d0: 49 4e 53 45 52 54 20 49 4e 54 4f 20 70 61 72 65  INSERT INTO pare
48e0: 6e 74 20 56 41 4c 55 45 53 28 2d 31 29 3b 0a 20  nt VALUES(-1);. 
48f0: 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 63     INSERT INTO c
4900: 68 69 6c 64 20 56 41 4c 55 45 53 28 2d 31 29 3b  hild VALUES(-1);
4910: 0a 20 20 20 20 55 50 44 41 54 45 20 70 61 72 65  .    UPDATE pare
4920: 6e 74 20 53 45 54 20 78 20 3d 20 32 32 3b 0a 20  nt SET x = 22;. 
4930: 20 20 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d     SELECT * FROM
4940: 20 70 61 72 65 6e 74 20 55 4e 49 4f 4e 20 41 4c   parent UNION AL
4950: 4c 20 53 45 4c 45 43 54 20 27 78 78 78 27 20 55  L SELECT 'xxx' U
4960: 4e 49 4f 4e 20 41 4c 4c 20 53 45 4c 45 43 54 20  NION ALL SELECT 
4970: 61 20 46 52 4f 4d 20 63 68 69 6c 64 3b 0a 20 20  a FROM child;.  
4980: 7d 0a 7d 20 7b 32 32 20 32 33 20 32 31 20 78 78  }.} {22 23 21 xx
4990: 78 20 32 33 7d 0a 0a 0a 23 2d 2d 2d 2d 2d 2d 2d  x 23}...#-------
49a0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
49b0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
49c0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
49d0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
49e0: 2d 2d 0a 23 20 2f 2a 20 45 56 3a 20 52 2d 32 37  --.# /* EV: R-27
49f0: 33 38 33 2d 31 30 32 34 36 20 2a 2f 0a 23 0a 23  383-10246 */.#.#
4a00: 20 56 65 72 69 66 79 20 74 68 61 74 20 4f 4e 20   Verify that ON 
4a10: 55 50 44 41 54 45 20 61 63 74 69 6f 6e 73 20 6f  UPDATE actions o
4a20: 6e 6c 79 20 61 63 74 75 61 6c 6c 79 20 74 61 6b  nly actually tak
4a30: 65 20 70 6c 61 63 65 20 69 66 20 74 68 65 20 70  e place if the p
4a40: 61 72 65 6e 74 20 6b 65 79 0a 23 20 69 73 20 73  arent key.# is s
4a50: 65 74 20 74 6f 20 61 20 6e 65 77 20 76 61 6c 75  et to a new valu
4a60: 65 20 74 68 61 74 20 69 73 20 64 69 73 74 69 6e  e that is distin
4a70: 63 74 20 66 72 6f 6d 20 74 68 65 20 6f 6c 64 20  ct from the old 
4a80: 76 61 6c 75 65 2e 20 54 68 65 20 64 65 66 61 75  value. The defau
4a90: 6c 74 0a 23 20 63 6f 6c 6c 61 74 69 6f 6e 20 73  lt.# collation s
4aa0: 65 71 75 65 6e 63 65 20 61 6e 64 20 61 66 66 69  equence and affi
4ab0: 6e 69 74 79 20 61 72 65 20 75 73 65 64 20 74 6f  nity are used to
4ac0: 20 64 65 74 65 72 6d 69 6e 65 20 69 66 20 74 68   determine if th
4ad0: 65 20 6e 65 77 20 76 61 6c 75 65 0a 23 20 69 73  e new value.# is
4ae0: 20 27 64 69 73 74 69 6e 63 74 27 20 66 72 6f 6d   'distinct' from
4af0: 20 74 68 65 20 6f 6c 64 20 6f 72 20 6e 6f 74 2e   the old or not.
4b00: 0a 23 0a 64 72 6f 70 5f 61 6c 6c 5f 74 61 62 6c  .#.drop_all_tabl
4b10: 65 73 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65  es.do_test e_fke
4b20: 79 2d 32 36 2e 31 20 7b 0a 20 20 65 78 65 63 73  y-26.1 {.  execs
4b30: 71 6c 20 7b 0a 20 20 20 20 43 52 45 41 54 45 20  ql {.    CREATE 
4b40: 54 41 42 4c 45 20 7a 65 75 73 28 61 20 49 4e 54  TABLE zeus(a INT
4b50: 45 47 45 52 20 43 4f 4c 4c 41 54 45 20 4e 4f 43  EGER COLLATE NOC
4b60: 41 53 45 2c 20 62 2c 20 50 52 49 4d 41 52 59 20  ASE, b, PRIMARY 
4b70: 4b 45 59 28 61 2c 20 62 29 29 3b 0a 20 20 20 20  KEY(a, b));.    
4b80: 43 52 45 41 54 45 20 54 41 42 4c 45 20 61 70 6f  CREATE TABLE apo
4b90: 6c 6c 6f 28 63 2c 20 64 2c 20 0a 20 20 20 20 20  llo(c, d, .     
4ba0: 20 46 4f 52 45 49 47 4e 20 4b 45 59 28 63 2c 20   FOREIGN KEY(c, 
4bb0: 64 29 20 52 45 46 45 52 45 4e 43 45 53 20 7a 65  d) REFERENCES ze
4bc0: 75 73 20 4f 4e 20 55 50 44 41 54 45 20 43 41 53  us ON UPDATE CAS
4bd0: 43 41 44 45 0a 20 20 20 20 29 3b 0a 20 20 20 20  CADE.    );.    
4be0: 49 4e 53 45 52 54 20 49 4e 54 4f 20 7a 65 75 73  INSERT INTO zeus
4bf0: 20 56 41 4c 55 45 53 28 27 61 62 63 27 2c 20 27   VALUES('abc', '
4c00: 78 79 7a 27 29 3b 0a 20 20 20 20 49 4e 53 45 52  xyz');.    INSER
4c10: 54 20 49 4e 54 4f 20 61 70 6f 6c 6c 6f 20 56 41  T INTO apollo VA
4c20: 4c 55 45 53 28 27 41 42 43 27 2c 20 27 78 79 7a  LUES('ABC', 'xyz
4c30: 27 29 3b 0a 20 20 7d 0a 20 20 65 78 65 63 73 71  ');.  }.  execsq
4c40: 6c 20 7b 0a 20 20 20 20 55 50 44 41 54 45 20 7a  l {.    UPDATE z
4c50: 65 75 73 20 53 45 54 20 61 20 3d 20 27 61 42 63  eus SET a = 'aBc
4c60: 27 3b 0a 20 20 20 20 53 45 4c 45 43 54 20 2a 20  ';.    SELECT * 
4c70: 46 52 4f 4d 20 61 70 6f 6c 6c 6f 3b 0a 20 20 7d  FROM apollo;.  }
4c80: 0a 7d 20 7b 41 42 43 20 78 79 7a 7d 0a 64 6f 5f  .} {ABC xyz}.do_
4c90: 74 65 73 74 20 65 5f 66 6b 65 79 2d 32 36 2e 32  test e_fkey-26.2
4ca0: 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20   {.  execsql {. 
4cb0: 20 20 20 55 50 44 41 54 45 20 7a 65 75 73 20 53     UPDATE zeus S
4cc0: 45 54 20 61 20 3d 20 31 2c 20 62 20 3d 20 31 3b  ET a = 1, b = 1;
4cd0: 0a 20 20 20 20 53 45 4c 45 43 54 20 2a 20 46 52  .    SELECT * FR
4ce0: 4f 4d 20 61 70 6f 6c 6c 6f 3b 0a 20 20 7d 0a 7d  OM apollo;.  }.}
4cf0: 20 7b 31 20 31 7d 0a 64 6f 5f 74 65 73 74 20 65   {1 1}.do_test e
4d00: 5f 66 6b 65 79 2d 32 36 2e 33 20 7b 0a 20 20 65  _fkey-26.3 {.  e
4d10: 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 55 50 44  xecsql {.    UPD
4d20: 41 54 45 20 7a 65 75 73 20 53 45 54 20 61 20 3d  ATE zeus SET a =
4d30: 20 31 2c 20 62 20 3d 20 31 3b 0a 20 20 20 20 53   1, b = 1;.    S
4d40: 45 4c 45 43 54 20 74 79 70 65 6f 66 28 63 29 2c  ELECT typeof(c),
4d50: 20 63 2c 20 74 79 70 65 6f 66 28 64 29 2c 20 64   c, typeof(d), d
4d60: 20 46 52 4f 4d 20 61 70 6f 6c 6c 6f 3b 0a 20 20   FROM apollo;.  
4d70: 7d 0a 7d 20 7b 69 6e 74 65 67 65 72 20 31 20 69  }.} {integer 1 i
4d80: 6e 74 65 67 65 72 20 31 7d 0a 64 6f 5f 74 65 73  nteger 1}.do_tes
4d90: 74 20 65 5f 66 6b 65 79 2d 32 36 2e 34 20 7b 0a  t e_fkey-26.4 {.
4da0: 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20    execsql {.    
4db0: 55 50 44 41 54 45 20 7a 65 75 73 20 53 45 54 20  UPDATE zeus SET 
4dc0: 61 20 3d 20 27 31 27 3b 0a 20 20 20 20 53 45 4c  a = '1';.    SEL
4dd0: 45 43 54 20 74 79 70 65 6f 66 28 63 29 2c 20 63  ECT typeof(c), c
4de0: 2c 20 74 79 70 65 6f 66 28 64 29 2c 20 64 20 46  , typeof(d), d F
4df0: 52 4f 4d 20 61 70 6f 6c 6c 6f 3b 0a 20 20 7d 0a  ROM apollo;.  }.
4e00: 7d 20 7b 69 6e 74 65 67 65 72 20 31 20 69 6e 74  } {integer 1 int
4e10: 65 67 65 72 20 31 7d 0a 64 6f 5f 74 65 73 74 20  eger 1}.do_test 
4e20: 65 5f 66 6b 65 79 2d 32 36 2e 35 20 7b 0a 20 20  e_fkey-26.5 {.  
4e30: 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 55 50  execsql {.    UP
4e40: 44 41 54 45 20 7a 65 75 73 20 53 45 54 20 62 20  DATE zeus SET b 
4e50: 3d 20 27 31 27 3b 0a 20 20 20 20 53 45 4c 45 43  = '1';.    SELEC
4e60: 54 20 74 79 70 65 6f 66 28 63 29 2c 20 63 2c 20  T typeof(c), c, 
4e70: 74 79 70 65 6f 66 28 64 29 2c 20 64 20 46 52 4f  typeof(d), d FRO
4e80: 4d 20 61 70 6f 6c 6c 6f 3b 0a 20 20 7d 0a 7d 20  M apollo;.  }.} 
4e90: 7b 69 6e 74 65 67 65 72 20 31 20 74 65 78 74 20  {integer 1 text 
4ea0: 31 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65  1}.do_test e_fke
4eb0: 79 2d 32 36 2e 36 20 7b 0a 20 20 65 78 65 63 73  y-26.6 {.  execs
4ec0: 71 6c 20 7b 0a 20 20 20 20 55 50 44 41 54 45 20  ql {.    UPDATE 
4ed0: 7a 65 75 73 20 53 45 54 20 62 20 3d 20 4e 55 4c  zeus SET b = NUL
4ee0: 4c 3b 0a 20 20 20 20 53 45 4c 45 43 54 20 74 79  L;.    SELECT ty
4ef0: 70 65 6f 66 28 63 29 2c 20 63 2c 20 74 79 70 65  peof(c), c, type
4f00: 6f 66 28 64 29 2c 20 64 20 46 52 4f 4d 20 61 70  of(d), d FROM ap
4f10: 6f 6c 6c 6f 3b 0a 20 20 7d 0a 7d 20 7b 69 6e 74  ollo;.  }.} {int
4f20: 65 67 65 72 20 31 20 6e 75 6c 6c 20 7b 7d 7d 0a  eger 1 null {}}.
4f30: 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  .#--------------
4f40: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
4f50: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
4f60: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
4f70: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20 2f 2a  -----------.# /*
4f80: 20 45 56 3a 20 52 2d 35 31 34 33 37 2d 33 39 38   EV: R-51437-398
4f90: 39 31 20 2a 2f 0a 23 0a 23 20 54 65 73 74 20 61  91 */.#.# Test a
4fa0: 6e 20 65 78 61 6d 70 6c 65 20 66 72 6f 6d 20 74  n example from t
4fb0: 68 65 20 22 4f 4e 20 44 45 4c 45 54 45 20 61 6e  he "ON DELETE an
4fc0: 64 20 4f 4e 20 55 50 44 41 54 45 20 41 63 74 69  d ON UPDATE Acti
4fd0: 6f 6e 73 22 20 73 65 63 74 69 6f 6e 20 0a 23 20  ons" section .# 
4fe0: 6f 66 20 66 6f 72 65 69 67 6e 6b 65 79 73 2e 68  of foreignkeys.h
4ff0: 74 6d 6c 2e 20 54 68 69 73 20 65 78 61 6d 70 6c  tml. This exampl
5000: 65 20 64 65 6d 6f 6e 73 74 72 61 74 65 73 20 74  e demonstrates t
5010: 68 61 74 20 4f 4e 20 55 50 44 41 54 45 20 61 63  hat ON UPDATE ac
5020: 74 69 6f 6e 73 0a 23 20 6f 6e 6c 79 20 74 61 6b  tions.# only tak
5030: 65 20 70 6c 61 63 65 20 69 66 20 61 74 20 6c 65  e place if at le
5040: 61 73 74 20 6f 6e 65 20 70 61 72 65 6e 74 20 6b  ast one parent k
5050: 65 79 20 63 6f 6c 75 6d 6e 20 69 73 20 73 65 74  ey column is set
5060: 20 74 6f 20 61 20 76 61 6c 75 65 20 0a 23 20 74   to a value .# t
5070: 68 61 74 20 69 73 20 64 69 73 74 69 6e 63 74 20  hat is distinct 
5080: 66 72 6f 6d 20 69 74 73 20 70 72 65 76 69 6f 75  from its previou
5090: 73 20 76 61 6c 75 65 2e 0a 23 0a 64 72 6f 70 5f  s value..#.drop_
50a0: 61 6c 6c 5f 74 61 62 6c 65 73 0a 64 6f 5f 74 65  all_tables.do_te
50b0: 73 74 20 65 5f 66 6b 65 79 2d 31 33 2e 31 20 7b  st e_fkey-13.1 {
50c0: 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20  .  execsql {.   
50d0: 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 70 61   CREATE TABLE pa
50e0: 72 65 6e 74 28 78 20 50 52 49 4d 41 52 59 20 4b  rent(x PRIMARY K
50f0: 45 59 29 3b 0a 20 20 20 20 43 52 45 41 54 45 20  EY);.    CREATE 
5100: 54 41 42 4c 45 20 63 68 69 6c 64 28 79 20 52 45  TABLE child(y RE
5110: 46 45 52 45 4e 43 45 53 20 70 61 72 65 6e 74 20  FERENCES parent 
5120: 4f 4e 20 55 50 44 41 54 45 20 53 45 54 20 4e 55  ON UPDATE SET NU
5130: 4c 4c 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20  LL);.    INSERT 
5140: 49 4e 54 4f 20 70 61 72 65 6e 74 20 56 41 4c 55  INTO parent VALU
5150: 45 53 28 27 6b 65 79 27 29 3b 0a 20 20 20 20 49  ES('key');.    I
5160: 4e 53 45 52 54 20 49 4e 54 4f 20 63 68 69 6c 64  NSERT INTO child
5170: 20 56 41 4c 55 45 53 28 27 6b 65 79 27 29 3b 0a   VALUES('key');.
5180: 20 20 7d 0a 7d 20 7b 7d 0a 64 6f 5f 74 65 73 74    }.} {}.do_test
5190: 20 65 5f 66 6b 65 79 2d 31 33 2e 32 20 7b 0a 20   e_fkey-13.2 {. 
51a0: 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 55   execsql {.    U
51b0: 50 44 41 54 45 20 70 61 72 65 6e 74 20 53 45 54  PDATE parent SET
51c0: 20 78 20 3d 20 27 6b 65 79 27 3b 0a 20 20 20 20   x = 'key';.    
51d0: 53 45 4c 45 43 54 20 49 46 4e 55 4c 4c 28 79 2c  SELECT IFNULL(y,
51e0: 20 27 6e 75 6c 6c 27 29 20 46 52 4f 4d 20 63 68   'null') FROM ch
51f0: 69 6c 64 3b 0a 20 20 7d 0a 7d 20 7b 6b 65 79 7d  ild;.  }.} {key}
5200: 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d  .do_test e_fkey-
5210: 31 33 2e 33 20 7b 0a 20 20 65 78 65 63 73 71 6c  13.3 {.  execsql
5220: 20 7b 0a 20 20 20 20 55 50 44 41 54 45 20 70 61   {.    UPDATE pa
5230: 72 65 6e 74 20 53 45 54 20 78 20 3d 20 27 6b 65  rent SET x = 'ke
5240: 79 32 27 3b 0a 20 20 20 20 53 45 4c 45 43 54 20  y2';.    SELECT 
5250: 49 46 4e 55 4c 4c 28 79 2c 20 27 6e 75 6c 6c 27  IFNULL(y, 'null'
5260: 29 20 46 52 4f 4d 20 63 68 69 6c 64 3b 0a 20 20  ) FROM child;.  
5270: 7d 0a 7d 20 7b 6e 75 6c 6c 7d 0a 0a 23 23 23 23  }.} {null}..####
5280: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
5290: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
52a0: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
52b0: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
52c0: 23 23 23 23 23 23 23 0a 23 23 23 20 53 45 43 54  #######.### SECT
52d0: 49 4f 4e 20 35 3a 20 43 52 45 41 54 45 2c 20 41  ION 5: CREATE, A
52e0: 4c 54 45 52 20 61 6e 64 20 44 52 4f 50 20 54 41  LTER and DROP TA
52f0: 42 4c 45 20 63 6f 6d 6d 61 6e 64 73 0a 23 23 23  BLE commands.###
5300: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
5310: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
5320: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
5330: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
5340: 23 23 23 23 23 23 23 23 0a 0a 23 2d 2d 2d 2d 2d  ########..#-----
5350: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
5360: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
5370: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
5380: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
5390: 2d 2d 2d 2d 0a 23 20 2f 2a 20 45 56 3a 20 52 2d  ----.# /* EV: R-
53a0: 33 36 30 31 38 2d 32 31 37 35 35 20 2a 2f 0a 23  36018-21755 */.#
53b0: 20 2f 2a 20 45 56 3a 20 52 2d 32 35 33 38 34 2d   /* EV: R-25384-
53c0: 33 39 33 33 37 20 2a 2f 0a 23 20 0a 23 20 54 65  39337 */.# .# Te
53d0: 73 74 20 74 68 61 74 20 70 61 72 65 6e 74 20 6b  st that parent k
53e0: 65 79 73 20 61 72 65 20 6e 6f 74 20 63 68 65 63  eys are not chec
53f0: 6b 65 64 20 77 68 65 6e 20 74 61 62 6c 65 73 20  ked when tables 
5400: 61 72 65 20 63 72 65 61 74 65 64 2e 0a 23 0a 23  are created..#.#
5410: 20 43 68 69 6c 64 20 6b 65 79 73 20 61 72 65 20   Child keys are 
5420: 63 68 65 63 6b 65 64 20 74 6f 20 65 6e 73 75 72  checked to ensur
5430: 65 20 61 6c 6c 20 63 6f 6d 70 6f 6e 65 6e 74 20  e all component 
5440: 63 6f 6c 75 6d 6e 73 20 65 78 69 73 74 2e 20 49  columns exist. I
5450: 66 20 70 61 72 65 6e 74 0a 23 20 6b 65 79 20 63  f parent.# key c
5460: 6f 6c 75 6d 6e 73 20 61 72 65 20 65 78 70 6c 69  olumns are expli
5470: 63 69 74 6c 79 20 73 70 65 63 69 66 69 65 64 2c  citly specified,
5480: 20 53 51 4c 69 74 65 20 63 68 65 63 6b 73 20 74   SQLite checks t
5490: 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 65 72  o make sure ther
54a0: 65 0a 23 20 61 72 65 20 74 68 65 20 73 61 6d 65  e.# are the same
54b0: 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d   number of colum
54c0: 6e 73 20 69 6e 20 74 68 65 20 63 68 69 6c 64 20  ns in the child 
54d0: 61 6e 64 20 70 61 72 65 6e 74 20 6b 65 79 73 2e  and parent keys.
54e0: 20 28 54 4f 44 4f 3a 20 54 68 69 73 0a 23 20 69   (TODO: This.# i
54f0: 73 20 74 65 73 74 65 64 20 62 75 74 20 64 6f 65  s tested but doe
5500: 73 20 6e 6f 74 20 63 6f 72 72 65 73 70 6f 6e 64  s not correspond
5510: 20 74 6f 20 61 6e 79 20 74 65 73 74 61 62 6c 65   to any testable
5520: 20 73 74 61 74 65 6d 65 6e 74 2e 29 0a 23 0a 23   statement.).#.#
5530: 20 2f 2a 20 45 56 3a 20 52 2d 35 30 31 36 33 2d   /* EV: R-50163-
5540: 35 34 32 32 39 20 2a 2f 0a 23 0a 23 20 41 6c 73  54229 */.#.# Als
5550: 6f 20 74 65 73 74 20 74 68 61 74 20 74 68 65 20  o test that the 
5560: 61 62 6f 76 65 20 73 74 61 74 65 6d 65 6e 74 73  above statements
5570: 20 61 72 65 20 74 72 75 65 20 72 65 67 61 72 64   are true regard
5580: 6c 65 73 73 20 6f 66 20 77 68 65 74 68 65 72 20  less of whether 
5590: 6f 72 20 6e 6f 74 0a 23 20 66 6f 72 65 69 67 6e  or not.# foreign
55a0: 20 6b 65 79 73 20 61 72 65 20 65 6e 61 62 6c 65   keys are enable
55b0: 64 2e 0a 23 20 0a 66 6f 72 65 61 63 68 20 7b 74  d..# .foreach {t
55c0: 6e 20 7a 43 72 65 61 74 65 54 62 6c 20 6c 52 65  n zCreateTbl lRe
55d0: 73 7d 20 7b 0a 20 20 31 20 22 43 52 45 41 54 45  s} {.  1 "CREATE
55e0: 20 54 41 42 4c 45 20 74 31 28 61 2c 20 62 20 52   TABLE t1(a, b R
55f0: 45 46 45 52 45 4e 43 45 53 20 74 31 29 22 20 20  EFERENCES t1)"  
5600: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5610: 20 20 20 20 20 20 20 20 20 20 7b 30 20 7b 7d 7d            {0 {}}
5620: 0a 20 20 32 20 22 43 52 45 41 54 45 20 54 41 42  .  2 "CREATE TAB
5630: 4c 45 20 74 31 28 61 2c 20 62 20 52 45 46 45 52  LE t1(a, b REFER
5640: 45 4e 43 45 53 20 74 32 29 22 20 20 20 20 20 20  ENCES t2)"      
5650: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5660: 20 20 20 20 20 20 7b 30 20 7b 7d 7d 0a 20 20 33        {0 {}}.  3
5670: 20 22 43 52 45 41 54 45 20 54 41 42 4c 45 20 74   "CREATE TABLE t
5680: 31 28 61 2c 20 62 2c 20 46 4f 52 45 49 47 4e 20  1(a, b, FOREIGN 
5690: 4b 45 59 28 61 2c 62 29 20 52 45 46 45 52 45 4e  KEY(a,b) REFEREN
56a0: 43 45 53 20 74 31 29 22 20 20 20 20 20 20 20 20  CES t1)"        
56b0: 20 20 7b 30 20 7b 7d 7d 0a 20 20 34 20 22 43 52    {0 {}}.  4 "CR
56c0: 45 41 54 45 20 54 41 42 4c 45 20 74 31 28 61 2c  EATE TABLE t1(a,
56d0: 20 62 2c 20 46 4f 52 45 49 47 4e 20 4b 45 59 28   b, FOREIGN KEY(
56e0: 61 2c 62 29 20 52 45 46 45 52 45 4e 43 45 53 20  a,b) REFERENCES 
56f0: 74 32 29 22 20 20 20 20 20 20 20 20 20 20 7b 30  t2)"          {0
5700: 20 7b 7d 7d 0a 20 20 35 20 22 43 52 45 41 54 45   {}}.  5 "CREATE
5710: 20 54 41 42 4c 45 20 74 31 28 61 2c 20 62 2c 20   TABLE t1(a, b, 
5720: 46 4f 52 45 49 47 4e 20 4b 45 59 28 61 2c 62 29  FOREIGN KEY(a,b)
5730: 20 52 45 46 45 52 45 4e 43 45 53 20 74 32 29 22   REFERENCES t2)"
5740: 20 20 20 20 20 20 20 20 20 20 7b 30 20 7b 7d 7d            {0 {}}
5750: 0a 20 20 36 20 22 43 52 45 41 54 45 20 54 41 42  .  6 "CREATE TAB
5760: 4c 45 20 74 31 28 61 2c 20 62 2c 20 46 4f 52 45  LE t1(a, b, FORE
5770: 49 47 4e 20 4b 45 59 28 61 2c 62 29 20 52 45 46  IGN KEY(a,b) REF
5780: 45 52 45 4e 43 45 53 20 74 32 28 6e 2c 64 29 29  ERENCES t2(n,d))
5790: 22 20 20 20 20 20 7b 30 20 7b 7d 7d 0a 20 20 37  "     {0 {}}.  7
57a0: 20 22 43 52 45 41 54 45 20 54 41 42 4c 45 20 74   "CREATE TABLE t
57b0: 31 28 61 2c 20 62 2c 20 46 4f 52 45 49 47 4e 20  1(a, b, FOREIGN 
57c0: 4b 45 59 28 61 2c 62 29 20 52 45 46 45 52 45 4e  KEY(a,b) REFEREN
57d0: 43 45 53 20 74 31 28 61 2c 62 29 29 22 20 20 20  CES t1(a,b))"   
57e0: 20 20 7b 30 20 7b 7d 7d 0a 0a 20 20 41 20 22 43    {0 {}}..  A "C
57f0: 52 45 41 54 45 20 54 41 42 4c 45 20 74 31 28 61  REATE TABLE t1(a
5800: 2c 20 62 2c 20 46 4f 52 45 49 47 4e 20 4b 45 59  , b, FOREIGN KEY
5810: 28 63 2c 62 29 20 52 45 46 45 52 45 4e 43 45 53  (c,b) REFERENCES
5820: 20 74 32 29 22 20 20 20 20 20 20 20 20 20 20 0a   t2)"          .
5830: 20 20 20 20 20 7b 31 20 7b 75 6e 6b 6e 6f 77 6e       {1 {unknown
5840: 20 63 6f 6c 75 6d 6e 20 22 63 22 20 69 6e 20 66   column "c" in f
5850: 6f 72 65 69 67 6e 20 6b 65 79 20 64 65 66 69 6e  oreign key defin
5860: 69 74 69 6f 6e 7d 7d 0a 20 20 42 20 22 43 52 45  ition}}.  B "CRE
5870: 41 54 45 20 54 41 42 4c 45 20 74 31 28 61 2c 20  ATE TABLE t1(a, 
5880: 62 2c 20 46 4f 52 45 49 47 4e 20 4b 45 59 28 63  b, FOREIGN KEY(c
5890: 2c 62 29 20 52 45 46 45 52 45 4e 43 45 53 20 74  ,b) REFERENCES t
58a0: 32 28 64 29 29 22 20 20 20 20 20 20 20 20 20 20  2(d))"          
58b0: 0a 20 20 20 20 20 7b 31 20 7b 6e 75 6d 62 65 72  .     {1 {number
58c0: 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 66   of columns in f
58d0: 6f 72 65 69 67 6e 20 6b 65 79 20 64 6f 65 73 20  oreign key does 
58e0: 6e 6f 74 20 6d 61 74 63 68 20 74 68 65 20 6e 75  not match the nu
58f0: 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20  mber of columns 
5900: 69 6e 20 74 68 65 20 72 65 66 65 72 65 6e 63 65  in the reference
5910: 64 20 74 61 62 6c 65 7d 7d 0a 7d 20 7b 0a 20 20  d table}}.} {.  
5920: 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 35  do_test e_fkey-5
5930: 2e 24 74 6e 2e 6f 66 66 20 7b 0a 20 20 20 20 64  .$tn.off {.    d
5940: 72 6f 70 5f 61 6c 6c 5f 74 61 62 6c 65 73 0a 20  rop_all_tables. 
5950: 20 20 20 65 78 65 63 73 71 6c 20 7b 50 52 41 47     execsql {PRAG
5960: 4d 41 20 66 6f 72 65 69 67 6e 5f 6b 65 79 73 20  MA foreign_keys 
5970: 3d 20 4f 46 46 7d 0a 20 20 20 20 63 61 74 63 68  = OFF}.    catch
5980: 73 71 6c 20 24 7a 43 72 65 61 74 65 54 62 6c 0a  sql $zCreateTbl.
5990: 20 20 7d 20 24 6c 52 65 73 0a 20 20 64 6f 5f 74    } $lRes.  do_t
59a0: 65 73 74 20 65 5f 66 6b 65 79 2d 35 2e 24 74 6e  est e_fkey-5.$tn
59b0: 2e 6f 6e 20 7b 0a 20 20 20 20 64 72 6f 70 5f 61  .on {.    drop_a
59c0: 6c 6c 5f 74 61 62 6c 65 73 0a 20 20 20 20 65 78  ll_tables.    ex
59d0: 65 63 73 71 6c 20 7b 50 52 41 47 4d 41 20 66 6f  ecsql {PRAGMA fo
59e0: 72 65 69 67 6e 5f 6b 65 79 73 20 3d 20 4f 4e 7d  reign_keys = ON}
59f0: 0a 20 20 20 20 63 61 74 63 68 73 71 6c 20 24 7a  .    catchsql $z
5a00: 43 72 65 61 74 65 54 62 6c 0a 20 20 7d 20 24 6c  CreateTbl.  } $l
5a10: 52 65 73 0a 7d 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d  Res.}..#--------
5a20: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
5a30: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
5a40: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
5a50: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
5a60: 2d 0a 23 20 2f 2a 20 45 56 3a 20 52 2d 34 37 39  -.# /* EV: R-479
5a70: 35 32 2d 36 32 34 39 38 20 2a 2f 0a 23 0a 70 72  52-62498 */.#.pr
5a80: 6f 63 20 74 65 73 74 5f 65 66 6b 65 79 5f 36 20  oc test_efkey_6 
5a90: 7b 74 6e 20 7a 41 6c 74 65 72 20 69 73 45 72 72  {tn zAlter isErr
5aa0: 6f 72 7d 20 7b 0a 20 20 64 72 6f 70 5f 61 6c 6c  or} {.  drop_all
5ab0: 5f 74 61 62 6c 65 73 20 0a 0a 20 20 64 6f 5f 74  _tables ..  do_t
5ac0: 65 73 74 20 65 5f 66 6b 65 79 2d 36 2e 24 74 6e  est e_fkey-6.$tn
5ad0: 2e 31 20 22 0a 20 20 20 20 65 78 65 63 73 71 6c  .1 ".    execsql
5ae0: 20 7b 20 43 52 45 41 54 45 20 54 41 42 4c 45 20   { CREATE TABLE 
5af0: 74 62 6c 28 61 2c 20 62 29 20 7d 0a 20 20 20 20  tbl(a, b) }.    
5b00: 5b 6c 69 73 74 20 63 61 74 63 68 73 71 6c 20 24  [list catchsql $
5b10: 7a 41 6c 74 65 72 5d 0a 20 20 22 20 5b 6c 69 6e  zAlter].  " [lin
5b20: 64 65 78 20 7b 7b 30 20 7b 7d 7d 20 7b 31 20 7b  dex {{0 {}} {1 {
5b30: 43 61 6e 6e 6f 74 20 61 64 64 20 61 20 52 45 46  Cannot add a REF
5b40: 45 52 45 4e 43 45 53 20 63 6f 6c 75 6d 6e 20 77  ERENCES column w
5b50: 69 74 68 20 6e 6f 6e 2d 4e 55 4c 4c 20 64 65 66  ith non-NULL def
5b60: 61 75 6c 74 20 76 61 6c 75 65 7d 7d 7d 20 24 69  ault value}}} $i
5b70: 73 45 72 72 6f 72 5d 0a 0a 7d 0a 0a 74 65 73 74  sError]..}..test
5b80: 5f 65 66 6b 65 79 5f 36 20 31 20 22 41 4c 54 45  _efkey_6 1 "ALTE
5b90: 52 20 54 41 42 4c 45 20 74 62 6c 20 41 44 44 20  R TABLE tbl ADD 
5ba0: 43 4f 4c 55 4d 4e 20 63 20 52 45 46 45 52 45 4e  COLUMN c REFEREN
5bb0: 43 45 53 20 78 78 22 20 30 0a 74 65 73 74 5f 65  CES xx" 0.test_e
5bc0: 66 6b 65 79 5f 36 20 32 20 22 41 4c 54 45 52 20  fkey_6 2 "ALTER 
5bd0: 54 41 42 4c 45 20 74 62 6c 20 41 44 44 20 43 4f  TABLE tbl ADD CO
5be0: 4c 55 4d 4e 20 63 20 44 45 46 41 55 4c 54 20 4e  LUMN c DEFAULT N
5bf0: 55 4c 4c 20 52 45 46 45 52 45 4e 43 45 53 20 78  ULL REFERENCES x
5c00: 78 22 20 30 0a 74 65 73 74 5f 65 66 6b 65 79 5f  x" 0.test_efkey_
5c10: 36 20 33 20 22 41 4c 54 45 52 20 54 41 42 4c 45  6 3 "ALTER TABLE
5c20: 20 74 62 6c 20 41 44 44 20 43 4f 4c 55 4d 4e 20   tbl ADD COLUMN 
5c30: 63 20 44 45 46 41 55 4c 54 20 30 20 52 45 46 45  c DEFAULT 0 REFE
5c40: 52 45 4e 43 45 53 20 78 78 22 20 31 0a 0a 23 2d  RENCES xx" 1..#-
5c50: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
5c60: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
5c70: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
5c80: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
5c90: 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20 2f 2a 20 45 56  --------.# /* EV
5ca0: 3a 20 52 2d 34 37 30 38 30 2d 30 32 30 36 39 20  : R-47080-02069 
5cb0: 2a 2f 0a 23 0a 23 20 54 65 73 74 20 74 68 61 74  */.#.# Test that
5cc0: 20 41 4c 54 45 52 20 54 41 42 4c 45 20 61 64 6a   ALTER TABLE adj
5cd0: 75 73 74 73 20 52 45 46 45 52 45 4e 43 45 53 20  usts REFERENCES 
5ce0: 63 6c 61 75 73 65 73 20 77 68 65 6e 20 74 68 65  clauses when the
5cf0: 20 70 61 72 65 6e 74 20 74 61 62 6c 65 0a 23 20   parent table.# 
5d00: 69 73 20 52 45 4e 41 4d 45 44 2e 0a 23 0a 23 20  is RENAMED..#.# 
5d10: 2f 2a 20 45 56 3a 20 52 2d 36 33 38 32 37 2d 35  /* EV: R-63827-5
5d20: 34 37 37 34 20 2a 2f 0a 23 0a 23 20 54 65 73 74  4774 */.#.# Test
5d30: 20 74 68 61 74 20 74 68 65 73 65 20 61 64 6a 75   that these adju
5d40: 73 74 6d 65 6e 74 73 20 61 72 65 20 76 69 73 69  stments are visi
5d50: 62 6c 65 20 69 6e 20 74 68 65 20 73 71 6c 69 74  ble in the sqlit
5d60: 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 2e 0a  e_master table..
5d70: 23 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79  #.do_test e_fkey
5d80: 2d 37 2e 31 20 7b 0a 20 20 64 72 6f 70 5f 61 6c  -7.1 {.  drop_al
5d90: 6c 5f 74 61 62 6c 65 73 0a 20 20 65 78 65 63 73  l_tables.  execs
5da0: 71 6c 20 7b 0a 20 20 20 20 43 52 45 41 54 45 20  ql {.    CREATE 
5db0: 54 41 42 4c 45 20 27 70 20 31 20 22 70 61 72 65  TABLE 'p 1 "pare
5dc0: 6e 74 20 6f 6e 65 22 27 28 61 20 52 45 46 45 52  nt one"'(a REFER
5dd0: 45 4e 43 45 53 20 27 70 20 31 20 22 70 61 72 65  ENCES 'p 1 "pare
5de0: 6e 74 20 6f 6e 65 22 27 2c 20 62 2c 20 50 52 49  nt one"', b, PRI
5df0: 4d 41 52 59 20 4b 45 59 28 62 29 29 3b 0a 0a 20  MARY KEY(b));.. 
5e00: 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20     CREATE TABLE 
5e10: 63 31 28 63 2c 20 64 20 52 45 46 45 52 45 4e 43  c1(c, d REFERENC
5e20: 45 53 20 27 70 20 31 20 22 70 61 72 65 6e 74 20  ES 'p 1 "parent 
5e30: 6f 6e 65 22 27 20 4f 4e 20 55 50 44 41 54 45 20  one"' ON UPDATE 
5e40: 43 41 53 43 41 44 45 29 3b 0a 20 20 20 20 43 52  CASCADE);.    CR
5e50: 45 41 54 45 20 54 41 42 4c 45 20 63 32 28 65 2c  EATE TABLE c2(e,
5e60: 20 66 2c 20 46 4f 52 45 49 47 4e 20 4b 45 59 28   f, FOREIGN KEY(
5e70: 66 29 20 52 45 46 45 52 45 4e 43 45 53 20 27 70  f) REFERENCES 'p
5e80: 20 31 20 22 70 61 72 65 6e 74 20 6f 6e 65 22 27   1 "parent one"'
5e90: 20 4f 4e 20 55 50 44 41 54 45 20 43 41 53 43 41   ON UPDATE CASCA
5ea0: 44 45 29 3b 0a 20 20 20 20 43 52 45 41 54 45 20  DE);.    CREATE 
5eb0: 54 41 42 4c 45 20 63 33 28 65 2c 20 27 66 20 63  TABLE c3(e, 'f c
5ec0: 6f 6c 20 32 27 2c 20 46 4f 52 45 49 47 4e 20 4b  ol 2', FOREIGN K
5ed0: 45 59 28 27 66 20 63 6f 6c 20 32 27 29 20 52 45  EY('f col 2') RE
5ee0: 46 45 52 45 4e 43 45 53 20 27 70 20 31 20 22 70  FERENCES 'p 1 "p
5ef0: 61 72 65 6e 74 20 6f 6e 65 22 27 20 4f 4e 20 55  arent one"' ON U
5f00: 50 44 41 54 45 20 43 41 53 43 41 44 45 29 3b 0a  PDATE CASCADE);.
5f10: 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f  .    INSERT INTO
5f20: 20 27 70 20 31 20 22 70 61 72 65 6e 74 20 6f 6e   'p 1 "parent on
5f30: 65 22 27 20 56 41 4c 55 45 53 28 31 2c 20 31 29  e"' VALUES(1, 1)
5f40: 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54  ;.    INSERT INT
5f50: 4f 20 63 31 20 56 41 4c 55 45 53 28 31 2c 20 31  O c1 VALUES(1, 1
5f60: 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e  );.    INSERT IN
5f70: 54 4f 20 63 32 20 56 41 4c 55 45 53 28 31 2c 20  TO c2 VALUES(1, 
5f80: 31 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49  1);.    INSERT I
5f90: 4e 54 4f 20 63 33 20 56 41 4c 55 45 53 28 31 2c  NTO c3 VALUES(1,
5fa0: 20 31 29 3b 0a 0a 20 20 20 20 2d 2d 20 43 52 45   1);..    -- CRE
5fb0: 41 54 45 20 54 41 42 4c 45 20 71 28 61 2c 20 62  ATE TABLE q(a, b
5fc0: 2c 20 50 52 49 4d 41 52 59 20 4b 45 59 28 62 29  , PRIMARY KEY(b)
5fd0: 29 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a 64 6f 5f 74  );.  }.} {}.do_t
5fe0: 65 73 74 20 65 5f 66 6b 65 79 2d 37 2e 32 20 7b  est e_fkey-7.2 {
5ff0: 0a 20 20 65 78 65 63 73 71 6c 20 7b 20 41 4c 54  .  execsql { ALT
6000: 45 52 20 54 41 42 4c 45 20 27 70 20 31 20 22 70  ER TABLE 'p 1 "p
6010: 61 72 65 6e 74 20 6f 6e 65 22 27 20 52 45 4e 41  arent one"' RENA
6020: 4d 45 20 54 4f 20 70 20 7d 0a 7d 20 7b 7d 0a 64  ME TO p }.} {}.d
6030: 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 37 2e  o_test e_fkey-7.
6040: 33 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a  3 {.  execsql {.
6050: 20 20 20 20 55 50 44 41 54 45 20 70 20 53 45 54      UPDATE p SET
6060: 20 61 20 3d 20 27 78 78 78 27 2c 20 62 20 3d 20   a = 'xxx', b = 
6070: 27 78 78 78 27 3b 0a 20 20 20 20 53 45 4c 45 43  'xxx';.    SELEC
6080: 54 20 2a 20 46 52 4f 4d 20 70 3b 0a 20 20 20 20  T * FROM p;.    
6090: 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 63 31  SELECT * FROM c1
60a0: 3b 0a 20 20 20 20 53 45 4c 45 43 54 20 2a 20 46  ;.    SELECT * F
60b0: 52 4f 4d 20 63 32 3b 0a 20 20 20 20 53 45 4c 45  ROM c2;.    SELE
60c0: 43 54 20 2a 20 46 52 4f 4d 20 63 33 3b 0a 20 20  CT * FROM c3;.  
60d0: 7d 0a 7d 20 7b 78 78 78 20 78 78 78 20 31 20 78  }.} {xxx xxx 1 x
60e0: 78 78 20 31 20 78 78 78 20 31 20 78 78 78 7d 0a  xx 1 xxx 1 xxx}.
60f0: 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 37  do_test e_fkey-7
6100: 2e 34 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b  .4 {.  execsql {
6110: 20 53 45 4c 45 43 54 20 73 71 6c 20 46 52 4f 4d   SELECT sql FROM
6120: 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 57   sqlite_master W
6130: 48 45 52 45 20 74 79 70 65 20 3d 20 27 74 61 62  HERE type = 'tab
6140: 6c 65 27 7d 0a 7d 20 5b 6c 69 73 74 20 20 20 20  le'}.} [list    
6150: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6160: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6170: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6180: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6190: 20 5c 0a 20 20 7b 43 52 45 41 54 45 20 54 41 42   \.  {CREATE TAB
61a0: 4c 45 20 22 70 22 28 61 20 52 45 46 45 52 45 4e  LE "p"(a REFEREN
61b0: 43 45 53 20 22 70 22 2c 20 62 2c 20 50 52 49 4d  CES "p", b, PRIM
61c0: 41 52 59 20 4b 45 59 28 62 29 29 7d 20 20 20 20  ARY KEY(b))}    
61d0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
61e0: 0a 20 20 7b 43 52 45 41 54 45 20 54 41 42 4c 45  .  {CREATE TABLE
61f0: 20 63 31 28 63 2c 20 64 20 52 45 46 45 52 45 4e   c1(c, d REFEREN
6200: 43 45 53 20 22 70 22 20 4f 4e 20 55 50 44 41 54  CES "p" ON UPDAT
6210: 45 20 43 41 53 43 41 44 45 29 7d 20 20 20 20 20  E CASCADE)}     
6220: 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
6230: 20 7b 43 52 45 41 54 45 20 54 41 42 4c 45 20 63   {CREATE TABLE c
6240: 32 28 65 2c 20 66 2c 20 46 4f 52 45 49 47 4e 20  2(e, f, FOREIGN 
6250: 4b 45 59 28 66 29 20 52 45 46 45 52 45 4e 43 45  KEY(f) REFERENCE
6260: 53 20 22 70 22 20 4f 4e 20 55 50 44 41 54 45 20  S "p" ON UPDATE 
6270: 43 41 53 43 41 44 45 29 7d 20 20 5c 0a 20 20 7b  CASCADE)}  \.  {
6280: 43 52 45 41 54 45 20 54 41 42 4c 45 20 63 33 28  CREATE TABLE c3(
6290: 65 2c 20 27 66 20 63 6f 6c 20 32 27 2c 20 46 4f  e, 'f col 2', FO
62a0: 52 45 49 47 4e 20 4b 45 59 28 27 66 20 63 6f 6c  REIGN KEY('f col
62b0: 20 32 27 29 20 52 45 46 45 52 45 4e 43 45 53 20   2') REFERENCES 
62c0: 22 70 22 20 4f 4e 20 55 50 44 41 54 45 20 43 41  "p" ON UPDATE CA
62d0: 53 43 41 44 45 29 7d 20 5c 0a 5d 0a 0a 23 2d 2d  SCADE)} \.]..#--
62e0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
62f0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
6300: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
6310: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
6320: 2d 2d 2d 2d 2d 2d 2d 0a 23 20 2f 2a 20 45 56 3a  -------.# /* EV:
6330: 20 52 2d 31 34 32 30 38 2d 32 33 39 38 36 20 2a   R-14208-23986 *
6340: 2f 0a 23 20 2f 2a 20 45 56 3a 20 52 2d 31 31 30  /.# /* EV: R-110
6350: 37 38 2d 30 33 39 34 35 20 2a 2f 0a 23 0a 23 20  78-03945 */.#.# 
6360: 43 68 65 63 6b 20 74 68 61 74 20 61 20 44 52 4f  Check that a DRO
6370: 50 20 54 41 42 4c 45 20 64 6f 65 73 20 61 6e 20  P TABLE does an 
6380: 69 6d 70 6c 69 63 69 74 20 44 45 4c 45 54 45 20  implicit DELETE 
6390: 46 52 4f 4d 2e 20 57 68 69 63 68 20 64 6f 65 73  FROM. Which does
63a0: 20 6e 6f 74 0a 23 20 63 61 75 73 65 20 61 6e 79   not.# cause any
63b0: 20 74 72 69 67 67 65 72 73 20 74 6f 20 66 69 72   triggers to fir
63c0: 65 2c 20 62 75 74 20 64 6f 65 73 20 66 69 72 65  e, but does fire
63d0: 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 61 63 74   foreign key act
63e0: 69 6f 6e 73 2e 0a 23 0a 64 6f 5f 74 65 73 74 20  ions..#.do_test 
63f0: 65 5f 66 6b 65 79 2d 38 2e 31 20 7b 0a 20 20 64  e_fkey-8.1 {.  d
6400: 72 6f 70 5f 61 6c 6c 5f 74 61 62 6c 65 73 0a 20  rop_all_tables. 
6410: 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 43   execsql {.    C
6420: 52 45 41 54 45 20 54 41 42 4c 45 20 70 28 61 2c  REATE TABLE p(a,
6430: 20 62 2c 20 50 52 49 4d 41 52 59 20 4b 45 59 28   b, PRIMARY KEY(
6440: 61 2c 20 62 29 29 3b 0a 0a 20 20 20 20 43 52 45  a, b));..    CRE
6450: 41 54 45 20 54 41 42 4c 45 20 63 31 28 63 2c 20  ATE TABLE c1(c, 
6460: 64 2c 20 46 4f 52 45 49 47 4e 20 4b 45 59 28 63  d, FOREIGN KEY(c
6470: 2c 20 64 29 20 52 45 46 45 52 45 4e 43 45 53 20  , d) REFERENCES 
6480: 70 20 4f 4e 20 44 45 4c 45 54 45 20 53 45 54 20  p ON DELETE SET 
6490: 4e 55 4c 4c 29 3b 0a 20 20 20 20 43 52 45 41 54  NULL);.    CREAT
64a0: 45 20 54 41 42 4c 45 20 63 32 28 63 2c 20 64 2c  E TABLE c2(c, d,
64b0: 20 46 4f 52 45 49 47 4e 20 4b 45 59 28 63 2c 20   FOREIGN KEY(c, 
64c0: 64 29 20 52 45 46 45 52 45 4e 43 45 53 20 70 20  d) REFERENCES p 
64d0: 4f 4e 20 44 45 4c 45 54 45 20 53 45 54 20 44 45  ON DELETE SET DE
64e0: 46 41 55 4c 54 29 3b 0a 20 20 20 20 43 52 45 41  FAULT);.    CREA
64f0: 54 45 20 54 41 42 4c 45 20 63 33 28 63 2c 20 64  TE TABLE c3(c, d
6500: 2c 20 46 4f 52 45 49 47 4e 20 4b 45 59 28 63 2c  , FOREIGN KEY(c,
6510: 20 64 29 20 52 45 46 45 52 45 4e 43 45 53 20 70   d) REFERENCES p
6520: 20 4f 4e 20 44 45 4c 45 54 45 20 43 41 53 43 41   ON DELETE CASCA
6530: 44 45 29 3b 0a 20 20 20 20 43 52 45 41 54 45 20  DE);.    CREATE 
6540: 54 41 42 4c 45 20 63 34 28 63 2c 20 64 2c 20 46  TABLE c4(c, d, F
6550: 4f 52 45 49 47 4e 20 4b 45 59 28 63 2c 20 64 29  OREIGN KEY(c, d)
6560: 20 52 45 46 45 52 45 4e 43 45 53 20 70 20 4f 4e   REFERENCES p ON
6570: 20 44 45 4c 45 54 45 20 52 45 53 54 52 49 43 54   DELETE RESTRICT
6580: 29 3b 0a 20 20 20 20 43 52 45 41 54 45 20 54 41  );.    CREATE TA
6590: 42 4c 45 20 63 35 28 63 2c 20 64 2c 20 46 4f 52  BLE c5(c, d, FOR
65a0: 45 49 47 4e 20 4b 45 59 28 63 2c 20 64 29 20 52  EIGN KEY(c, d) R
65b0: 45 46 45 52 45 4e 43 45 53 20 70 20 4f 4e 20 44  EFERENCES p ON D
65c0: 45 4c 45 54 45 20 4e 4f 20 41 43 54 49 4f 4e 29  ELETE NO ACTION)
65d0: 3b 0a 0a 20 20 20 20 43 52 45 41 54 45 20 54 41  ;..    CREATE TA
65e0: 42 4c 45 20 63 36 28 63 2c 20 64 2c 20 0a 20 20  BLE c6(c, d, .  
65f0: 20 20 20 20 46 4f 52 45 49 47 4e 20 4b 45 59 28      FOREIGN KEY(
6600: 63 2c 20 64 29 20 52 45 46 45 52 45 4e 43 45 53  c, d) REFERENCES
6610: 20 70 20 4f 4e 20 44 45 4c 45 54 45 20 52 45 53   p ON DELETE RES
6620: 54 52 49 43 54 20 0a 20 20 20 20 20 20 44 45 46  TRICT .      DEF
6630: 45 52 52 41 42 4c 45 20 49 4e 49 54 49 41 4c 4c  ERRABLE INITIALL
6640: 59 20 44 45 46 45 52 52 45 44 0a 20 20 20 20 29  Y DEFERRED.    )
6650: 3b 0a 20 20 20 20 43 52 45 41 54 45 20 54 41 42  ;.    CREATE TAB
6660: 4c 45 20 63 37 28 63 2c 20 64 2c 20 0a 20 20 20  LE c7(c, d, .   
6670: 20 20 20 46 4f 52 45 49 47 4e 20 4b 45 59 28 63     FOREIGN KEY(c
6680: 2c 20 64 29 20 52 45 46 45 52 45 4e 43 45 53 20  , d) REFERENCES 
6690: 70 20 4f 4e 20 44 45 4c 45 54 45 20 4e 4f 20 41  p ON DELETE NO A
66a0: 43 54 49 4f 4e 0a 20 20 20 20 20 20 44 45 46 45  CTION.      DEFE
66b0: 52 52 41 42 4c 45 20 49 4e 49 54 49 41 4c 4c 59  RRABLE INITIALLY
66c0: 20 44 45 46 45 52 52 45 44 0a 20 20 20 20 29 3b   DEFERRED.    );
66d0: 0a 0a 20 20 20 20 43 52 45 41 54 45 20 54 41 42  ..    CREATE TAB
66e0: 4c 45 20 6c 6f 67 28 6d 73 67 29 3b 0a 20 20 20  LE log(msg);.   
66f0: 20 43 52 45 41 54 45 20 54 52 49 47 47 45 52 20   CREATE TRIGGER 
6700: 74 74 20 41 46 54 45 52 20 44 45 4c 45 54 45 20  tt AFTER DELETE 
6710: 4f 4e 20 70 20 42 45 47 49 4e 0a 20 20 20 20 20  ON p BEGIN.     
6720: 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 6c 6f 67   INSERT INTO log
6730: 20 56 41 4c 55 45 53 28 27 64 65 6c 65 74 65 20   VALUES('delete 
6740: 27 20 7c 7c 20 6f 6c 64 2e 72 6f 77 69 64 29 3b  ' || old.rowid);
6750: 0a 20 20 20 20 45 4e 44 3b 0a 20 20 7d 0a 7d 20  .    END;.  }.} 
6760: 7b 7d 0a 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b  {}..do_test e_fk
6770: 65 79 2d 38 2e 32 20 7b 0a 20 20 65 78 65 63 73  ey-8.2 {.  execs
6780: 71 6c 20 7b 0a 20 20 20 20 49 4e 53 45 52 54 20  ql {.    INSERT 
6790: 49 4e 54 4f 20 70 20 56 41 4c 55 45 53 28 27 61  INTO p VALUES('a
67a0: 27 2c 20 27 62 27 29 3b 0a 20 20 20 20 49 4e 53  ', 'b');.    INS
67b0: 45 52 54 20 49 4e 54 4f 20 63 31 20 56 41 4c 55  ERT INTO c1 VALU
67c0: 45 53 28 27 61 27 2c 20 27 62 27 29 3b 0a 20 20  ES('a', 'b');.  
67d0: 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 63 32    INSERT INTO c2
67e0: 20 56 41 4c 55 45 53 28 27 61 27 2c 20 27 62 27   VALUES('a', 'b'
67f0: 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e  );.    INSERT IN
6800: 54 4f 20 63 33 20 56 41 4c 55 45 53 28 27 61 27  TO c3 VALUES('a'
6810: 2c 20 27 62 27 29 3b 0a 20 20 20 20 42 45 47 49  , 'b');.    BEGI
6820: 4e 3b 0a 20 20 20 20 20 20 44 52 4f 50 20 54 41  N;.      DROP TA
6830: 42 4c 45 20 70 3b 0a 20 20 20 20 20 20 53 45 4c  BLE p;.      SEL
6840: 45 43 54 20 2a 20 46 52 4f 4d 20 63 31 3b 0a 20  ECT * FROM c1;. 
6850: 20 7d 0a 7d 20 7b 7b 7d 20 7b 7d 7d 0a 64 6f 5f   }.} {{} {}}.do_
6860: 74 65 73 74 20 65 5f 66 6b 65 79 2d 38 2e 33 20  test e_fkey-8.3 
6870: 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 20 53 45  {.  execsql { SE
6880: 4c 45 43 54 20 2a 20 46 52 4f 4d 20 63 32 20 7d  LECT * FROM c2 }
6890: 0a 7d 20 7b 7b 7d 20 7b 7d 7d 0a 64 6f 5f 74 65  .} {{} {}}.do_te
68a0: 73 74 20 65 5f 66 6b 65 79 2d 38 2e 34 20 7b 0a  st e_fkey-8.4 {.
68b0: 20 20 65 78 65 63 73 71 6c 20 7b 20 53 45 4c 45    execsql { SELE
68c0: 43 54 20 2a 20 46 52 4f 4d 20 63 33 20 7d 0a 7d  CT * FROM c3 }.}
68d0: 20 7b 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b   {}.do_test e_fk
68e0: 65 79 2d 38 2e 35 20 7b 0a 20 20 65 78 65 63 73  ey-8.5 {.  execs
68f0: 71 6c 20 7b 20 53 45 4c 45 43 54 20 2a 20 46 52  ql { SELECT * FR
6900: 4f 4d 20 6c 6f 67 20 7d 0a 7d 20 7b 7d 0a 64 6f  OM log }.} {}.do
6910: 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 38 2e 36  _test e_fkey-8.6
6920: 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 52 4f 4c   {.  execsql ROL
6930: 4c 42 41 43 4b 0a 7d 20 7b 7d 0a 64 6f 5f 74 65  LBACK.} {}.do_te
6940: 73 74 20 65 5f 66 6b 65 79 2d 38 2e 37 20 7b 0a  st e_fkey-8.7 {.
6950: 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20    execsql {.    
6960: 42 45 47 49 4e 3b 0a 20 20 20 20 20 20 44 45 4c  BEGIN;.      DEL
6970: 45 54 45 20 46 52 4f 4d 20 70 3b 0a 20 20 20 20  ETE FROM p;.    
6980: 20 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20    SELECT * FROM 
6990: 6c 6f 67 3b 0a 20 20 20 20 52 4f 4c 4c 42 41 43  log;.    ROLLBAC
69a0: 4b 3b 0a 20 20 7d 0a 7d 20 7b 7b 64 65 6c 65 74  K;.  }.} {{delet
69b0: 65 20 31 7d 7d 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d  e 1}}..#--------
69c0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
69d0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
69e0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
69f0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
6a00: 2d 0a 23 20 2f 2a 20 45 56 3a 20 52 2d 33 32 37  -.# /* EV: R-327
6a10: 36 38 2d 34 37 39 32 35 20 2a 2f 0a 23 0a 23 20  68-47925 */.#.# 
6a20: 49 66 20 61 6e 20 49 4d 4d 45 44 49 41 54 45 20  If an IMMEDIATE 
6a30: 66 6f 72 65 69 67 6e 20 6b 65 79 20 66 61 69 6c  foreign key fail
6a40: 73 20 61 73 20 61 20 72 65 73 75 6c 74 20 6f 66  s as a result of
6a50: 20 61 20 44 52 4f 50 20 54 41 42 4c 45 2c 20 74   a DROP TABLE, t
6a60: 68 65 0a 23 20 44 52 4f 50 20 54 41 42 4c 45 20  he.# DROP TABLE 
6a70: 63 6f 6d 6d 61 6e 64 20 66 61 69 6c 73 2e 0a 23  command fails..#
6a80: 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d  .do_test e_fkey-
6a90: 39 2e 31 20 7b 0a 20 20 65 78 65 63 73 71 6c 20  9.1 {.  execsql 
6aa0: 7b 20 0a 20 20 20 20 44 45 4c 45 54 45 20 46 52  { .    DELETE FR
6ab0: 4f 4d 20 63 31 3b 0a 20 20 20 20 44 45 4c 45 54  OM c1;.    DELET
6ac0: 45 20 46 52 4f 4d 20 63 32 3b 0a 20 20 20 20 44  E FROM c2;.    D
6ad0: 45 4c 45 54 45 20 46 52 4f 4d 20 63 33 3b 0a 20  ELETE FROM c3;. 
6ae0: 20 7d 0a 20 20 65 78 65 63 73 71 6c 20 7b 20 49   }.  execsql { I
6af0: 4e 53 45 52 54 20 49 4e 54 4f 20 63 35 20 56 41  NSERT INTO c5 VA
6b00: 4c 55 45 53 28 27 61 27 2c 20 27 62 27 29 20 7d  LUES('a', 'b') }
6b10: 0a 20 20 63 61 74 63 68 73 71 6c 20 7b 20 44 52  .  catchsql { DR
6b20: 4f 50 20 54 41 42 4c 45 20 70 20 7d 0a 7d 20 7b  OP TABLE p }.} {
6b30: 31 20 7b 66 6f 72 65 69 67 6e 20 6b 65 79 20 63  1 {foreign key c
6b40: 6f 6e 73 74 72 61 69 6e 74 20 66 61 69 6c 65 64  onstraint failed
6b50: 7d 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65  }}.do_test e_fke
6b60: 79 2d 39 2e 32 20 7b 0a 20 20 65 78 65 63 73 71  y-9.2 {.  execsq
6b70: 6c 20 7b 20 53 45 4c 45 43 54 20 2a 20 46 52 4f  l { SELECT * FRO
6b80: 4d 20 70 20 7d 0a 7d 20 7b 61 20 62 7d 0a 64 6f  M p }.} {a b}.do
6b90: 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 39 2e 33  _test e_fkey-9.3
6ba0: 20 7b 0a 20 20 63 61 74 63 68 73 71 6c 20 7b 0a   {.  catchsql {.
6bb0: 20 20 20 20 42 45 47 49 4e 3b 0a 20 20 20 20 20      BEGIN;.     
6bc0: 20 44 52 4f 50 20 54 41 42 4c 45 20 70 3b 0a 20   DROP TABLE p;. 
6bd0: 20 7d 0a 7d 20 7b 31 20 7b 66 6f 72 65 69 67 6e   }.} {1 {foreign
6be0: 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 20   key constraint 
6bf0: 66 61 69 6c 65 64 7d 7d 0a 64 6f 5f 74 65 73 74  failed}}.do_test
6c00: 20 65 5f 66 6b 65 79 2d 39 2e 34 20 7b 0a 20 20   e_fkey-9.4 {.  
6c10: 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 53 45  execsql {.    SE
6c20: 4c 45 43 54 20 2a 20 46 52 4f 4d 20 70 3b 0a 20  LECT * FROM p;. 
6c30: 20 20 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d     SELECT * FROM
6c40: 20 63 35 3b 0a 20 20 20 20 52 4f 4c 4c 42 41 43   c5;.    ROLLBAC
6c50: 4b 3b 0a 20 20 7d 0a 7d 20 7b 61 20 62 20 61 20  K;.  }.} {a b a 
6c60: 62 7d 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  b}..#-----------
6c70: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
6c80: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
6c90: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
6ca0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23  --------------.#
6cb0: 20 2f 2a 20 45 56 3a 20 52 2d 30 35 39 30 33 2d   /* EV: R-05903-
6cc0: 30 38 34 36 30 20 2a 2f 0a 23 0a 23 20 49 66 20  08460 */.#.# If 
6cd0: 61 20 44 45 46 45 52 52 45 44 20 66 6f 72 65 69  a DEFERRED forei
6ce0: 67 6e 20 6b 65 79 20 66 61 69 6c 73 20 61 73 20  gn key fails as 
6cf0: 61 20 72 65 73 75 6c 74 20 6f 66 20 61 20 44 52  a result of a DR
6d00: 4f 50 20 54 41 42 4c 45 2c 20 61 74 74 65 6d 70  OP TABLE, attemp
6d10: 74 69 6e 67 0a 23 20 74 6f 20 63 6f 6d 6d 69 74  ting.# to commit
6d20: 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
6d30: 20 66 61 69 6c 73 20 75 6e 6c 65 73 73 20 74 68   fails unless th
6d40: 65 20 76 69 6f 6c 61 74 69 6f 6e 20 69 73 20 66  e violation is f
6d50: 69 78 65 64 2e 0a 23 0a 64 6f 5f 74 65 73 74 20  ixed..#.do_test 
6d60: 65 5f 66 6b 65 79 2d 31 30 2e 31 20 7b 0a 20 20  e_fkey-10.1 {.  
6d70: 65 78 65 63 73 71 6c 20 7b 20 0a 20 20 20 20 44  execsql { .    D
6d80: 45 4c 45 54 45 20 46 52 4f 4d 20 63 31 20 3b 20  ELETE FROM c1 ; 
6d90: 44 45 4c 45 54 45 20 46 52 4f 4d 20 63 32 20 3b  DELETE FROM c2 ;
6da0: 20 44 45 4c 45 54 45 20 46 52 4f 4d 20 63 33 20   DELETE FROM c3 
6db0: 3b 0a 20 20 20 20 44 45 4c 45 54 45 20 46 52 4f  ;.    DELETE FRO
6dc0: 4d 20 63 34 20 3b 20 44 45 4c 45 54 45 20 46 52  M c4 ; DELETE FR
6dd0: 4f 4d 20 63 35 20 3b 20 44 45 4c 45 54 45 20 46  OM c5 ; DELETE F
6de0: 52 4f 4d 20 63 36 20 3b 0a 20 20 20 20 44 45 4c  ROM c6 ;.    DEL
6df0: 45 54 45 20 46 52 4f 4d 20 63 37 20 0a 20 20 7d  ETE FROM c7 .  }
6e00: 0a 7d 20 7b 7d 0a 64 6f 5f 74 65 73 74 20 65 5f  .} {}.do_test e_
6e10: 66 6b 65 79 2d 31 30 2e 32 20 7b 0a 20 20 65 78  fkey-10.2 {.  ex
6e20: 65 63 73 71 6c 20 7b 20 49 4e 53 45 52 54 20 49  ecsql { INSERT I
6e30: 4e 54 4f 20 63 37 20 56 41 4c 55 45 53 28 27 61  NTO c7 VALUES('a
6e40: 27 2c 20 27 62 27 29 20 7d 0a 20 20 65 78 65 63  ', 'b') }.  exec
6e50: 73 71 6c 20 7b 0a 20 20 20 20 42 45 47 49 4e 3b  sql {.    BEGIN;
6e60: 0a 20 20 20 20 20 20 44 52 4f 50 20 54 41 42 4c  .      DROP TABL
6e70: 45 20 70 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a 64 6f  E p;.  }.} {}.do
6e80: 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 31 30 2e  _test e_fkey-10.
6e90: 33 20 7b 0a 20 20 63 61 74 63 68 73 71 6c 20 43  3 {.  catchsql C
6ea0: 4f 4d 4d 49 54 0a 7d 20 7b 31 20 7b 66 6f 72 65  OMMIT.} {1 {fore
6eb0: 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69  ign key constrai
6ec0: 6e 74 20 66 61 69 6c 65 64 7d 7d 0a 64 6f 5f 74  nt failed}}.do_t
6ed0: 65 73 74 20 65 5f 66 6b 65 79 2d 31 30 2e 34 20  est e_fkey-10.4 
6ee0: 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 20 43 52  {.  execsql { CR
6ef0: 45 41 54 45 20 54 41 42 4c 45 20 70 28 61 2c 20  EATE TABLE p(a, 
6f00: 62 2c 20 50 52 49 4d 41 52 59 20 4b 45 59 28 61  b, PRIMARY KEY(a
6f10: 2c 20 62 29 29 20 7d 0a 20 20 63 61 74 63 68 73  , b)) }.  catchs
6f20: 71 6c 20 43 4f 4d 4d 49 54 0a 7d 20 7b 31 20 7b  ql COMMIT.} {1 {
6f30: 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73  foreign key cons
6f40: 74 72 61 69 6e 74 20 66 61 69 6c 65 64 7d 7d 0a  traint failed}}.
6f50: 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 31  do_test e_fkey-1
6f60: 30 2e 35 20 7b 0a 20 20 65 78 65 63 73 71 6c 20  0.5 {.  execsql 
6f70: 7b 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 70 20  { INSERT INTO p 
6f80: 56 41 4c 55 45 53 28 27 61 27 2c 20 27 62 27 29  VALUES('a', 'b')
6f90: 20 7d 0a 20 20 65 78 65 63 73 71 6c 20 43 4f 4d   }.  execsql COM
6fa0: 4d 49 54 0a 7d 20 7b 7d 0a 0a 23 2d 2d 2d 2d 2d  MIT.} {}..#-----
6fb0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
6fc0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
6fd0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
6fe0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
6ff0: 2d 2d 2d 2d 0a 23 20 2f 2a 20 45 56 3a 20 52 2d  ----.# /* EV: R-
7000: 35 37 32 34 32 2d 33 37 30 30 35 20 2a 2f 0a 23  57242-37005 */.#
7010: 0a 23 20 41 6e 79 20 22 66 6f 72 65 69 67 6e 20  .# Any "foreign 
7020: 6b 65 79 20 6d 69 73 6d 61 74 63 68 22 20 65 72  key mismatch" er
7030: 72 6f 72 73 20 65 6e 63 6f 75 6e 74 65 72 65 64  rors encountered
7040: 20 77 68 69 6c 65 20 72 75 6e 6e 69 6e 67 20 61   while running a
7050: 6e 20 69 6d 70 6c 69 63 69 74 0a 23 20 22 44 45  n implicit.# "DE
7060: 4c 45 54 45 20 46 52 4f 4d 20 74 62 6c 22 20 61  LETE FROM tbl" a
7070: 72 65 20 69 67 6e 6f 72 65 64 2e 0a 23 0a 64 72  re ignored..#.dr
7080: 6f 70 5f 61 6c 6c 5f 74 61 62 6c 65 73 0a 64 6f  op_all_tables.do
7090: 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 31 31 2e  _test e_fkey-11.
70a0: 31 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a  1 {.  execsql {.
70b0: 20 20 20 20 50 52 41 47 4d 41 20 66 6f 72 65 69      PRAGMA forei
70c0: 67 6e 5f 6b 65 79 73 20 3d 20 4f 46 46 3b 0a 0a  gn_keys = OFF;..
70d0: 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45      CREATE TABLE
70e0: 20 70 28 61 20 50 52 49 4d 41 52 59 20 4b 45 59   p(a PRIMARY KEY
70f0: 2c 20 62 20 52 45 46 45 52 45 4e 43 45 53 20 6e  , b REFERENCES n
7100: 6f 73 75 63 68 74 61 62 6c 65 29 3b 0a 20 20 20  osuchtable);.   
7110: 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 63 31   CREATE TABLE c1
7120: 28 63 2c 20 64 2c 20 46 4f 52 45 49 47 4e 20 4b  (c, d, FOREIGN K
7130: 45 59 28 63 2c 20 64 29 20 52 45 46 45 52 45 4e  EY(c, d) REFEREN
7140: 43 45 53 20 61 29 3b 0a 20 20 20 20 43 52 45 41  CES a);.    CREA
7150: 54 45 20 54 41 42 4c 45 20 63 32 28 63 20 52 45  TE TABLE c2(c RE
7160: 46 45 52 45 4e 43 45 53 20 70 28 62 29 2c 20 64  FERENCES p(b), d
7170: 29 3b 0a 20 20 20 20 43 52 45 41 54 45 20 54 41  );.    CREATE TA
7180: 42 4c 45 20 63 33 28 63 20 52 45 46 45 52 45 4e  BLE c3(c REFEREN
7190: 43 45 53 20 70 20 4f 4e 20 44 45 4c 45 54 45 20  CES p ON DELETE 
71a0: 53 45 54 20 4e 55 4c 4c 2c 20 64 29 3b 0a 0a 20  SET NULL, d);.. 
71b0: 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 70     INSERT INTO p
71c0: 20 56 41 4c 55 45 53 28 31 2c 20 32 29 3b 0a 20   VALUES(1, 2);. 
71d0: 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 63     INSERT INTO c
71e0: 31 20 56 41 4c 55 45 53 28 31 2c 20 32 29 3b 0a  1 VALUES(1, 2);.
71f0: 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20      INSERT INTO 
7200: 63 32 20 56 41 4c 55 45 53 28 31 2c 20 32 29 3b  c2 VALUES(1, 2);
7210: 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f  .    INSERT INTO
7220: 20 63 33 20 56 41 4c 55 45 53 28 31 2c 20 32 29   c3 VALUES(1, 2)
7230: 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a 64 6f 5f 74 65  ;.  }.} {}.do_te
7240: 73 74 20 65 5f 66 6b 65 79 2d 31 31 2e 32 20 7b  st e_fkey-11.2 {
7250: 0a 20 20 65 78 65 63 73 71 6c 20 7b 20 50 52 41  .  execsql { PRA
7260: 47 4d 41 20 66 6f 72 65 69 67 6e 5f 6b 65 79 73  GMA foreign_keys
7270: 20 3d 20 4f 4e 20 7d 0a 20 20 63 61 74 63 68 73   = ON }.  catchs
7280: 71 6c 20 7b 20 44 45 4c 45 54 45 20 46 52 4f 4d  ql { DELETE FROM
7290: 20 70 20 7d 0a 7d 20 7b 31 20 7b 6e 6f 20 73 75   p }.} {1 {no su
72a0: 63 68 20 74 61 62 6c 65 3a 20 6d 61 69 6e 2e 6e  ch table: main.n
72b0: 6f 73 75 63 68 74 61 62 6c 65 7d 7d 0a 64 6f 5f  osuchtable}}.do_
72c0: 74 65 73 74 20 65 5f 66 6b 65 79 2d 31 31 2e 33  test e_fkey-11.3
72d0: 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20   {.  execsql {. 
72e0: 20 20 20 42 45 47 49 4e 3b 0a 20 20 20 20 20 20     BEGIN;.      
72f0: 44 52 4f 50 20 54 41 42 4c 45 20 70 3b 0a 20 20  DROP TABLE p;.  
7300: 20 20 20 20 53 45 4c 45 43 54 20 2a 20 46 52 4f      SELECT * FRO
7310: 4d 20 63 33 3b 0a 20 20 20 20 52 4f 4c 4c 42 41  M c3;.    ROLLBA
7320: 43 4b 3b 0a 20 20 7d 0a 7d 20 7b 7b 7d 20 32 7d  CK;.  }.} {{} 2}
7330: 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d  .do_test e_fkey-
7340: 31 31 2e 34 20 7b 0a 20 20 65 78 65 63 73 71 6c  11.4 {.  execsql
7350: 20 7b 20 43 52 45 41 54 45 20 54 41 42 4c 45 20   { CREATE TABLE 
7360: 6e 6f 73 75 63 68 74 61 62 6c 65 28 78 20 50 52  nosuchtable(x PR
7370: 49 4d 41 52 59 20 4b 45 59 29 20 7d 0a 20 20 63  IMARY KEY) }.  c
7380: 61 74 63 68 73 71 6c 20 7b 20 44 45 4c 45 54 45  atchsql { DELETE
7390: 20 46 52 4f 4d 20 70 20 7d 0a 7d 20 7b 31 20 7b   FROM p }.} {1 {
73a0: 66 6f 72 65 69 67 6e 20 6b 65 79 20 6d 69 73 6d  foreign key mism
73b0: 61 74 63 68 7d 7d 0a 64 6f 5f 74 65 73 74 20 65  atch}}.do_test e
73c0: 5f 66 6b 65 79 2d 31 31 2e 35 20 7b 0a 20 20 65  _fkey-11.5 {.  e
73d0: 78 65 63 73 71 6c 20 7b 20 44 52 4f 50 20 54 41  xecsql { DROP TA
73e0: 42 4c 45 20 63 31 20 7d 0a 20 20 63 61 74 63 68  BLE c1 }.  catch
73f0: 73 71 6c 20 7b 20 44 45 4c 45 54 45 20 46 52 4f  sql { DELETE FRO
7400: 4d 20 70 20 7d 0a 7d 20 7b 31 20 7b 66 6f 72 65  M p }.} {1 {fore
7410: 69 67 6e 20 6b 65 79 20 6d 69 73 6d 61 74 63 68  ign key mismatch
7420: 7d 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65  }}.do_test e_fke
7430: 79 2d 31 31 2e 36 20 7b 0a 20 20 65 78 65 63 73  y-11.6 {.  execs
7440: 71 6c 20 7b 20 44 52 4f 50 20 54 41 42 4c 45 20  ql { DROP TABLE 
7450: 63 32 20 7d 0a 20 20 65 78 65 63 73 71 6c 20 7b  c2 }.  execsql {
7460: 20 44 45 4c 45 54 45 20 46 52 4f 4d 20 70 20 7d   DELETE FROM p }
7470: 0a 7d 20 7b 7d 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d  .} {}..#--------
7480: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
7490: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
74a0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
74b0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
74c0: 2d 0a 23 20 2f 2a 20 45 56 3a 20 52 2d 35 34 31  -.# /* EV: R-541
74d0: 34 32 2d 34 31 33 34 36 20 2a 2f 0a 23 0a 23 20  42-41346 */.#.# 
74e0: 54 65 73 74 20 74 68 61 74 20 74 68 65 20 73 70  Test that the sp
74f0: 65 63 69 61 6c 20 62 65 68 61 76 69 6f 75 72 73  ecial behaviours
7500: 20 6f 66 20 41 4c 54 45 52 20 61 6e 64 20 44 52   of ALTER and DR
7510: 4f 50 20 54 41 42 4c 45 20 61 72 65 20 6f 6e 6c  OP TABLE are onl
7520: 79 0a 23 20 61 63 74 69 76 61 74 65 64 20 77 68  y.# activated wh
7530: 65 6e 20 66 6f 72 65 69 67 6e 20 6b 65 79 73 20  en foreign keys 
7540: 61 72 65 20 65 6e 61 62 6c 65 64 2e 20 53 70 65  are enabled. Spe
7550: 63 69 61 6c 20 62 65 68 61 76 69 6f 75 72 73 20  cial behaviours 
7560: 61 72 65 3a 0a 23 0a 23 20 20 20 31 2e 20 41 44  are:.#.#   1. AD
7570: 44 20 43 4f 4c 55 4d 4e 20 6e 6f 74 20 61 6c 6c  D COLUMN not all
7580: 6f 77 69 6e 67 20 61 20 52 45 46 45 52 45 4e 43  owing a REFERENC
7590: 45 53 20 63 6c 61 75 73 65 20 77 69 74 68 20 61  ES clause with a
75a0: 20 6e 6f 6e 2d 4e 55 4c 4c 20 0a 23 20 20 20 20   non-NULL .#    
75b0: 20 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65 2e    default value.
75c0: 0a 23 20 20 20 32 2e 20 4d 6f 64 69 66 79 69 6e  .#   2. Modifyin
75d0: 67 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 64 65  g foreign key de
75e0: 66 69 6e 69 74 69 6f 6e 73 20 77 68 65 6e 20 61  finitions when a
75f0: 20 70 61 72 65 6e 74 20 74 61 62 6c 65 20 69 73   parent table is
7600: 20 52 45 4e 41 4d 45 64 2e 0a 23 20 20 20 33 2e   RENAMEd..#   3.
7610: 20 52 75 6e 6e 69 6e 67 20 61 6e 20 69 6d 70 6c   Running an impl
7620: 69 63 69 74 20 44 45 4c 45 54 45 20 46 52 4f 4d  icit DELETE FROM
7630: 20 63 6f 6d 6d 61 6e 64 20 61 73 20 70 61 72 74   command as part
7640: 20 6f 66 20 44 52 4f 50 20 54 41 42 4c 45 2e 0a   of DROP TABLE..
7650: 23 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79  #.do_test e_fkey
7660: 2d 31 32 2e 31 2e 31 20 7b 0a 20 20 64 72 6f 70  -12.1.1 {.  drop
7670: 5f 61 6c 6c 5f 74 61 62 6c 65 73 0a 20 20 65 78  _all_tables.  ex
7680: 65 63 73 71 6c 20 7b 20 43 52 45 41 54 45 20 54  ecsql { CREATE T
7690: 41 42 4c 45 20 74 31 28 61 2c 20 62 29 20 7d 0a  ABLE t1(a, b) }.
76a0: 20 20 63 61 74 63 68 73 71 6c 20 7b 20 41 4c 54    catchsql { ALT
76b0: 45 52 20 54 41 42 4c 45 20 74 31 20 41 44 44 20  ER TABLE t1 ADD 
76c0: 43 4f 4c 55 4d 4e 20 63 20 44 45 46 41 55 4c 54  COLUMN c DEFAULT
76d0: 20 27 78 78 78 27 20 52 45 46 45 52 45 4e 43 45   'xxx' REFERENCE
76e0: 53 20 74 32 20 7d 0a 7d 20 7b 31 20 7b 43 61 6e  S t2 }.} {1 {Can
76f0: 6e 6f 74 20 61 64 64 20 61 20 52 45 46 45 52 45  not add a REFERE
7700: 4e 43 45 53 20 63 6f 6c 75 6d 6e 20 77 69 74 68  NCES column with
7710: 20 6e 6f 6e 2d 4e 55 4c 4c 20 64 65 66 61 75 6c   non-NULL defaul
7720: 74 20 76 61 6c 75 65 7d 7d 0a 64 6f 5f 74 65 73  t value}}.do_tes
7730: 74 20 65 5f 66 6b 65 79 2d 31 32 2e 31 2e 32 20  t e_fkey-12.1.2 
7740: 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 20 50 52  {.  execsql { PR
7750: 41 47 4d 41 20 66 6f 72 65 69 67 6e 5f 6b 65 79  AGMA foreign_key
7760: 73 20 3d 20 4f 46 46 20 7d 0a 20 20 65 78 65 63  s = OFF }.  exec
7770: 73 71 6c 20 7b 20 41 4c 54 45 52 20 54 41 42 4c  sql { ALTER TABL
7780: 45 20 74 31 20 41 44 44 20 43 4f 4c 55 4d 4e 20  E t1 ADD COLUMN 
7790: 63 20 44 45 46 41 55 4c 54 20 27 78 78 78 27 20  c DEFAULT 'xxx' 
77a0: 52 45 46 45 52 45 4e 43 45 53 20 74 32 20 7d 0a  REFERENCES t2 }.
77b0: 20 20 65 78 65 63 73 71 6c 20 7b 20 53 45 4c 45    execsql { SELE
77c0: 43 54 20 73 71 6c 20 46 52 4f 4d 20 73 71 6c 69  CT sql FROM sqli
77d0: 74 65 5f 6d 61 73 74 65 72 20 57 48 45 52 45 20  te_master WHERE 
77e0: 6e 61 6d 65 20 3d 20 27 74 31 27 20 7d 0a 7d 20  name = 't1' }.} 
77f0: 7b 7b 43 52 45 41 54 45 20 54 41 42 4c 45 20 74  {{CREATE TABLE t
7800: 31 28 61 2c 20 62 2c 20 63 20 44 45 46 41 55 4c  1(a, b, c DEFAUL
7810: 54 20 27 78 78 78 27 20 52 45 46 45 52 45 4e 43  T 'xxx' REFERENC
7820: 45 53 20 74 32 29 7d 7d 0a 64 6f 5f 74 65 73 74  ES t2)}}.do_test
7830: 20 65 5f 66 6b 65 79 2d 31 32 2e 31 2e 33 20 7b   e_fkey-12.1.3 {
7840: 0a 20 20 65 78 65 63 73 71 6c 20 7b 20 50 52 41  .  execsql { PRA
7850: 47 4d 41 20 66 6f 72 65 69 67 6e 5f 6b 65 79 73  GMA foreign_keys
7860: 20 3d 20 4f 4e 20 7d 0a 7d 20 7b 7d 0a 0a 64 6f   = ON }.} {}..do
7870: 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 31 32 2e  _test e_fkey-12.
7880: 32 2e 31 20 7b 0a 20 20 64 72 6f 70 5f 61 6c 6c  2.1 {.  drop_all
7890: 5f 74 61 62 6c 65 73 0a 20 20 65 78 65 63 73 71  _tables.  execsq
78a0: 6c 20 7b 0a 20 20 20 20 43 52 45 41 54 45 20 54  l {.    CREATE T
78b0: 41 42 4c 45 20 70 28 61 20 55 4e 49 51 55 45 29  ABLE p(a UNIQUE)
78c0: 3b 0a 20 20 20 20 43 52 45 41 54 45 20 54 41 42  ;.    CREATE TAB
78d0: 4c 45 20 63 28 62 20 52 45 46 45 52 45 4e 43 45  LE c(b REFERENCE
78e0: 53 20 70 28 61 29 29 3b 0a 20 20 20 20 42 45 47  S p(a));.    BEG
78f0: 49 4e 3b 0a 20 20 20 20 20 20 41 4c 54 45 52 20  IN;.      ALTER 
7900: 54 41 42 4c 45 20 70 20 52 45 4e 41 4d 45 20 54  TABLE p RENAME T
7910: 4f 20 70 61 72 65 6e 74 3b 0a 20 20 20 20 20 20  O parent;.      
7920: 53 45 4c 45 43 54 20 73 71 6c 20 46 52 4f 4d 20  SELECT sql FROM 
7930: 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 57 48  sqlite_master WH
7940: 45 52 45 20 6e 61 6d 65 20 3d 20 27 63 27 3b 0a  ERE name = 'c';.
7950: 20 20 20 20 52 4f 4c 4c 42 41 43 4b 3b 0a 20 20      ROLLBACK;.  
7960: 7d 0a 7d 20 7b 7b 43 52 45 41 54 45 20 54 41 42  }.} {{CREATE TAB
7970: 4c 45 20 63 28 62 20 52 45 46 45 52 45 4e 43 45  LE c(b REFERENCE
7980: 53 20 22 70 61 72 65 6e 74 22 28 61 29 29 7d 7d  S "parent"(a))}}
7990: 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d  .do_test e_fkey-
79a0: 31 32 2e 32 2e 32 20 7b 0a 20 20 65 78 65 63 73  12.2.2 {.  execs
79b0: 71 6c 20 7b 0a 20 20 20 20 50 52 41 47 4d 41 20  ql {.    PRAGMA 
79c0: 66 6f 72 65 69 67 6e 5f 6b 65 79 73 20 3d 20 4f  foreign_keys = O
79d0: 46 46 3b 0a 20 20 20 20 41 4c 54 45 52 20 54 41  FF;.    ALTER TA
79e0: 42 4c 45 20 70 20 52 45 4e 41 4d 45 20 54 4f 20  BLE p RENAME TO 
79f0: 70 61 72 65 6e 74 3b 0a 20 20 20 20 53 45 4c 45  parent;.    SELE
7a00: 43 54 20 73 71 6c 20 46 52 4f 4d 20 73 71 6c 69  CT sql FROM sqli
7a10: 74 65 5f 6d 61 73 74 65 72 20 57 48 45 52 45 20  te_master WHERE 
7a20: 6e 61 6d 65 20 3d 20 27 63 27 3b 0a 20 20 7d 0a  name = 'c';.  }.
7a30: 7d 20 7b 7b 43 52 45 41 54 45 20 54 41 42 4c 45  } {{CREATE TABLE
7a40: 20 63 28 62 20 52 45 46 45 52 45 4e 43 45 53 20   c(b REFERENCES 
7a50: 70 28 61 29 29 7d 7d 0a 64 6f 5f 74 65 73 74 20  p(a))}}.do_test 
7a60: 65 5f 66 6b 65 79 2d 31 32 2e 32 2e 33 20 7b 0a  e_fkey-12.2.3 {.
7a70: 20 20 65 78 65 63 73 71 6c 20 7b 20 50 52 41 47    execsql { PRAG
7a80: 4d 41 20 66 6f 72 65 69 67 6e 5f 6b 65 79 73 20  MA foreign_keys 
7a90: 3d 20 4f 4e 20 7d 0a 7d 20 7b 7d 0a 0a 64 6f 5f  = ON }.} {}..do_
7aa0: 74 65 73 74 20 65 5f 66 6b 65 79 2d 31 32 2e 33  test e_fkey-12.3
7ab0: 2e 31 20 7b 0a 20 20 64 72 6f 70 5f 61 6c 6c 5f  .1 {.  drop_all_
7ac0: 74 61 62 6c 65 73 0a 20 20 65 78 65 63 73 71 6c  tables.  execsql
7ad0: 20 7b 0a 20 20 20 20 43 52 45 41 54 45 20 54 41   {.    CREATE TA
7ae0: 42 4c 45 20 70 28 61 20 55 4e 49 51 55 45 29 3b  BLE p(a UNIQUE);
7af0: 0a 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c  .    CREATE TABL
7b00: 45 20 63 28 62 20 52 45 46 45 52 45 4e 43 45 53  E c(b REFERENCES
7b10: 20 70 28 61 29 20 4f 4e 20 44 45 4c 45 54 45 20   p(a) ON DELETE 
7b20: 53 45 54 20 4e 55 4c 4c 29 3b 0a 20 20 20 20 49  SET NULL);.    I
7b30: 4e 53 45 52 54 20 49 4e 54 4f 20 70 20 56 41 4c  NSERT INTO p VAL
7b40: 55 45 53 28 27 78 27 29 3b 0a 20 20 20 20 49 4e  UES('x');.    IN
7b50: 53 45 52 54 20 49 4e 54 4f 20 63 20 56 41 4c 55  SERT INTO c VALU
7b60: 45 53 28 27 78 27 29 3b 0a 20 20 20 20 42 45 47  ES('x');.    BEG
7b70: 49 4e 3b 0a 20 20 20 20 20 20 44 52 4f 50 20 54  IN;.      DROP T
7b80: 41 42 4c 45 20 70 3b 0a 20 20 20 20 20 20 53 45  ABLE p;.      SE
7b90: 4c 45 43 54 20 2a 20 46 52 4f 4d 20 63 3b 0a 20  LECT * FROM c;. 
7ba0: 20 20 20 52 4f 4c 4c 42 41 43 4b 3b 0a 20 20 7d     ROLLBACK;.  }
7bb0: 0a 7d 20 7b 7b 7d 7d 0a 64 6f 5f 74 65 73 74 20  .} {{}}.do_test 
7bc0: 65 5f 66 6b 65 79 2d 31 32 2e 33 2e 32 20 7b 0a  e_fkey-12.3.2 {.
7bd0: 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20    execsql {.    
7be0: 50 52 41 47 4d 41 20 66 6f 72 65 69 67 6e 5f 6b  PRAGMA foreign_k
7bf0: 65 79 73 20 3d 20 4f 46 46 3b 0a 20 20 20 20 44  eys = OFF;.    D
7c00: 52 4f 50 20 54 41 42 4c 45 20 70 3b 0a 20 20 20  ROP TABLE p;.   
7c10: 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 63   SELECT * FROM c
7c20: 3b 0a 20 20 7d 0a 7d 20 7b 78 7d 0a 64 6f 5f 74  ;.  }.} {x}.do_t
7c30: 65 73 74 20 65 5f 66 6b 65 79 2d 31 32 2e 33 2e  est e_fkey-12.3.
7c40: 33 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 20  3 {.  execsql { 
7c50: 50 52 41 47 4d 41 20 66 6f 72 65 69 67 6e 5f 6b  PRAGMA foreign_k
7c60: 65 79 73 20 3d 20 4f 4e 20 7d 0a 7d 20 7b 7d 0a  eys = ON }.} {}.
7c70: 0a 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  .###############
7c80: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
7c90: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
7ca0: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
7cb0: 23 23 23 23 23 23 23 23 23 23 23 23 0a 23 23 23  ############.###
7cc0: 20 53 45 43 54 49 4f 4e 20 36 3a 20 4c 69 6d 69   SECTION 6: Limi
7cd0: 74 73 20 61 6e 64 20 55 6e 73 75 70 70 6f 72 74  ts and Unsupport
7ce0: 65 64 20 46 65 61 74 75 72 65 73 0a 23 23 23 23  ed Features.####
7cf0: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
7d00: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
7d10: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
7d20: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
7d30: 23 23 23 23 23 23 23 0a 0a 23 2d 2d 2d 2d 2d 2d  #######..#------
7d40: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
7d50: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
7d60: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
7d70: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
7d80: 2d 2d 2d 0a 23 20 2f 2a 20 45 56 3a 20 52 2d 32  ---.# /* EV: R-2
7d90: 34 37 32 38 2d 31 33 32 33 30 20 2a 2f 0a 23 20  4728-13230 */.# 
7da0: 2f 2a 20 45 56 3a 20 52 2d 32 34 34 35 30 2d 34  /* EV: R-24450-4
7db0: 36 31 37 34 20 2a 2f 0a 23 0a 23 20 54 65 73 74  6174 */.#.# Test
7dc0: 20 74 68 61 74 20 4d 41 54 43 48 20 63 6c 61 75   that MATCH clau
7dd0: 73 65 73 20 61 72 65 20 70 61 72 73 65 64 2c 20  ses are parsed, 
7de0: 62 75 74 20 53 51 4c 69 74 65 20 74 72 65 61 74  but SQLite treat
7df0: 73 20 65 76 65 72 79 20 66 6f 72 65 69 67 6e 20  s every foreign 
7e00: 6b 65 79 0a 23 20 63 6f 6e 73 74 72 61 69 6e 74  key.# constraint
7e10: 20 61 73 20 69 66 20 69 74 20 77 65 72 65 20 22   as if it were "
7e20: 4d 41 54 43 48 20 53 49 4d 50 4c 45 22 2e 0a 23  MATCH SIMPLE"..#
7e30: 0a 66 6f 72 65 61 63 68 20 7a 4d 61 74 63 68 20  .foreach zMatch 
7e40: 5b 6c 69 73 74 20 53 49 4d 50 4c 45 20 50 41 52  [list SIMPLE PAR
7e50: 54 49 41 4c 20 46 55 4c 4c 20 53 69 6d 70 6c 65  TIAL FULL Simple
7e60: 20 70 61 72 54 49 41 4c 20 46 75 4c 4c 20 5d 20   parTIAL FuLL ] 
7e70: 7b 0a 20 20 64 72 6f 70 5f 61 6c 6c 5f 74 61 62  {.  drop_all_tab
7e80: 6c 65 73 0a 20 20 64 6f 5f 74 65 73 74 20 65 5f  les.  do_test e_
7e90: 66 6b 65 79 2d 31 2e 24 7a 4d 61 74 63 68 2e 31  fkey-1.$zMatch.1
7ea0: 20 7b 0a 20 20 20 20 65 78 65 63 73 71 6c 20 22   {.    execsql "
7eb0: 0a 20 20 20 20 20 20 43 52 45 41 54 45 20 54 41  .      CREATE TA
7ec0: 42 4c 45 20 70 28 61 2c 20 62 2c 20 63 2c 20 50  BLE p(a, b, c, P
7ed0: 52 49 4d 41 52 59 20 4b 45 59 28 62 2c 20 63 29  RIMARY KEY(b, c)
7ee0: 29 3b 0a 20 20 20 20 20 20 43 52 45 41 54 45 20  );.      CREATE 
7ef0: 54 41 42 4c 45 20 63 28 64 2c 20 65 2c 20 66 2c  TABLE c(d, e, f,
7f00: 20 46 4f 52 45 49 47 4e 20 4b 45 59 28 65 2c 20   FOREIGN KEY(e, 
7f10: 66 29 20 52 45 46 45 52 45 4e 43 45 53 20 70 20  f) REFERENCES p 
7f20: 4d 41 54 43 48 20 24 7a 4d 61 74 63 68 29 3b 0a  MATCH $zMatch);.
7f30: 20 20 20 20 22 0a 20 20 7d 20 7b 7d 0a 20 20 64      ".  } {}.  d
7f40: 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 31 2e  o_test e_fkey-1.
7f50: 24 7a 4d 61 74 63 68 2e 32 20 7b 0a 20 20 20 20  $zMatch.2 {.    
7f60: 65 78 65 63 73 71 6c 20 7b 20 49 4e 53 45 52 54  execsql { INSERT
7f70: 20 49 4e 54 4f 20 70 20 56 41 4c 55 45 53 28 31   INTO p VALUES(1
7f80: 2c 20 32 2c 20 33 29 20 20 20 20 20 20 20 20 20  , 2, 3)         
7f90: 7d 0a 0a 20 20 20 20 23 20 4d 41 54 43 48 20 53  }..    # MATCH S
7fa0: 49 4d 50 4c 45 20 62 65 68 61 76 69 6f 75 72 3a  IMPLE behaviour:
7fb0: 20 41 6c 6c 6f 77 20 61 6e 79 20 63 68 69 6c 64   Allow any child
7fc0: 20 6b 65 79 20 74 68 61 74 20 63 6f 6e 74 61 69   key that contai
7fd0: 6e 73 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 0a 20  ns one or more. 
7fe0: 20 20 20 23 20 4e 55 4c 4c 20 76 61 6c 75 65 20     # NULL value 
7ff0: 74 6f 20 62 65 20 69 6e 73 65 72 74 65 64 2e 20  to be inserted. 
8000: 4e 6f 6e 2d 4e 55 4c 4c 20 76 61 6c 75 65 73 20  Non-NULL values 
8010: 64 6f 20 6e 6f 74 20 68 61 76 65 20 74 6f 20 6d  do not have to m
8020: 61 70 20 74 6f 20 61 6e 79 0a 20 20 20 20 23 20  ap to any.    # 
8030: 70 61 72 65 6e 74 20 6b 65 79 20 76 61 6c 75 65  parent key value
8040: 73 2c 20 73 6f 20 6c 6f 6e 67 20 61 73 20 61 74  s, so long as at
8050: 20 6c 65 61 73 74 20 6f 6e 65 20 66 69 65 6c 64   least one field
8060: 20 6f 66 20 74 68 65 20 63 68 69 6c 64 20 6b 65   of the child ke
8070: 79 20 69 73 0a 20 20 20 20 23 20 4e 55 4c 4c 2e  y is.    # NULL.
8080: 0a 20 20 20 20 65 78 65 63 73 71 6c 20 7b 20 49  .    execsql { I
8090: 4e 53 45 52 54 20 49 4e 54 4f 20 63 20 56 41 4c  NSERT INTO c VAL
80a0: 55 45 53 28 27 77 27 2c 20 32 2c 20 33 29 20 20  UES('w', 2, 3)  
80b0: 20 20 20 20 20 7d 0a 20 20 20 20 65 78 65 63 73       }.    execs
80c0: 71 6c 20 7b 20 49 4e 53 45 52 54 20 49 4e 54 4f  ql { INSERT INTO
80d0: 20 63 20 56 41 4c 55 45 53 28 27 78 27 2c 20 27   c VALUES('x', '
80e0: 78 27 2c 20 4e 55 4c 4c 29 20 20 7d 0a 20 20 20  x', NULL)  }.   
80f0: 20 65 78 65 63 73 71 6c 20 7b 20 49 4e 53 45 52   execsql { INSER
8100: 54 20 49 4e 54 4f 20 63 20 56 41 4c 55 45 53 28  T INTO c VALUES(
8110: 27 79 27 2c 20 4e 55 4c 4c 2c 20 27 78 27 29 20  'y', NULL, 'x') 
8120: 20 7d 0a 20 20 20 20 65 78 65 63 73 71 6c 20 7b   }.    execsql {
8130: 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 63 20 56   INSERT INTO c V
8140: 41 4c 55 45 53 28 27 7a 27 2c 20 4e 55 4c 4c 2c  ALUES('z', NULL,
8150: 20 4e 55 4c 4c 29 20 7d 0a 0a 20 20 20 20 23 20   NULL) }..    # 
8160: 43 68 65 63 6b 20 74 68 61 74 20 74 68 65 20 46  Check that the F
8170: 4b 20 69 73 20 65 6e 66 6f 72 63 65 64 20 70 72  K is enforced pr
8180: 6f 70 65 72 6c 79 20 69 66 20 74 68 65 72 65 20  operly if there 
8190: 61 72 65 20 6e 6f 20 4e 55 4c 4c 20 76 61 6c 75  are no NULL valu
81a0: 65 73 20 0a 20 20 20 20 23 20 69 6e 20 74 68 65  es .    # in the
81b0: 20 63 68 69 6c 64 20 6b 65 79 20 63 6f 6c 75 6d   child key colum
81c0: 6e 73 2e 0a 20 20 20 20 63 61 74 63 68 73 71 6c  ns..    catchsql
81d0: 20 7b 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 63   { INSERT INTO c
81e0: 20 56 41 4c 55 45 53 28 27 61 27 2c 20 32 2c 20   VALUES('a', 2, 
81f0: 34 29 20 7d 0a 20 20 7d 20 7b 31 20 7b 66 6f 72  4) }.  } {1 {for
8200: 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61  eign key constra
8210: 69 6e 74 20 66 61 69 6c 65 64 7d 7d 0a 7d 0a 0a  int failed}}.}..
8220: 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  #---------------
8230: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
8240: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
8250: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
8260: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20 2f 2a 20  ----------.# /* 
8270: 45 56 3a 20 52 2d 32 31 35 39 39 2d 31 36 30 33  EV: R-21599-1603
8280: 38 20 2a 2f 0a 23 0a 23 20 54 65 73 74 20 74 68  8 */.#.# Test th
8290: 61 74 20 53 51 4c 69 74 65 20 64 6f 65 73 20 6e  at SQLite does n
82a0: 6f 74 20 73 75 70 70 6f 72 74 20 74 68 65 20 53  ot support the S
82b0: 45 54 20 43 4f 4e 53 54 52 41 49 4e 54 20 73 74  ET CONSTRAINT st
82c0: 61 74 65 6d 65 6e 74 2e 20 41 6e 64 0a 23 20 74  atement. And.# t
82d0: 68 61 74 20 69 74 20 69 73 20 70 6f 73 73 69 62  hat it is possib
82e0: 6c 65 20 74 6f 20 63 72 65 61 74 65 20 62 6f 74  le to create bot
82f0: 68 20 69 6d 6d 65 64 69 61 74 65 20 61 6e 64 20  h immediate and 
8300: 64 65 66 65 72 72 65 64 20 63 6f 6e 73 74 72 61  deferred constra
8310: 69 6e 74 73 2e 0a 23 0a 64 72 6f 70 5f 61 6c 6c  ints..#.drop_all
8320: 5f 74 61 62 6c 65 73 0a 64 6f 5f 74 65 73 74 20  _tables.do_test 
8330: 65 5f 66 6b 65 79 2d 32 2e 31 20 7b 0a 20 20 63  e_fkey-2.1 {.  c
8340: 61 74 63 68 73 71 6c 20 7b 20 53 45 54 20 43 4f  atchsql { SET CO
8350: 4e 53 54 52 41 49 4e 54 53 20 41 4c 4c 20 49 4d  NSTRAINTS ALL IM
8360: 4d 45 44 49 41 54 45 20 7d 0a 7d 20 7b 31 20 7b  MEDIATE }.} {1 {
8370: 6e 65 61 72 20 22 53 45 54 22 3a 20 73 79 6e 74  near "SET": synt
8380: 61 78 20 65 72 72 6f 72 7d 7d 0a 64 6f 5f 74 65  ax error}}.do_te
8390: 73 74 20 65 5f 66 6b 65 79 2d 32 2e 32 20 7b 0a  st e_fkey-2.2 {.
83a0: 20 20 63 61 74 63 68 73 71 6c 20 7b 20 53 45 54    catchsql { SET
83b0: 20 43 4f 4e 53 54 52 41 49 4e 54 53 20 41 4c 4c   CONSTRAINTS ALL
83c0: 20 44 45 46 45 52 52 45 44 20 7d 0a 7d 20 7b 31   DEFERRED }.} {1
83d0: 20 7b 6e 65 61 72 20 22 53 45 54 22 3a 20 73 79   {near "SET": sy
83e0: 6e 74 61 78 20 65 72 72 6f 72 7d 7d 0a 0a 64 6f  ntax error}}..do
83f0: 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 32 2e 33  _test e_fkey-2.3
8400: 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20   {.  execsql {. 
8410: 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20     CREATE TABLE 
8420: 70 28 61 2c 20 62 2c 20 50 52 49 4d 41 52 59 20  p(a, b, PRIMARY 
8430: 4b 45 59 28 61 2c 20 62 29 29 3b 0a 20 20 20 20  KEY(a, b));.    
8440: 43 52 45 41 54 45 20 54 41 42 4c 45 20 63 64 28  CREATE TABLE cd(
8450: 63 2c 20 64 2c 20 0a 20 20 20 20 20 20 46 4f 52  c, d, .      FOR
8460: 45 49 47 4e 20 4b 45 59 28 63 2c 20 64 29 20 52  EIGN KEY(c, d) R
8470: 45 46 45 52 45 4e 43 45 53 20 70 20 44 45 46 45  EFERENCES p DEFE
8480: 52 52 41 42 4c 45 20 49 4e 49 54 49 41 4c 4c 59  RRABLE INITIALLY
8490: 20 44 45 46 45 52 52 45 44 29 3b 0a 20 20 20 20   DEFERRED);.    
84a0: 43 52 45 41 54 45 20 54 41 42 4c 45 20 63 69 28  CREATE TABLE ci(
84b0: 63 2c 20 64 2c 20 0a 20 20 20 20 20 20 46 4f 52  c, d, .      FOR
84c0: 45 49 47 4e 20 4b 45 59 28 63 2c 20 64 29 20 52  EIGN KEY(c, d) R
84d0: 45 46 45 52 45 4e 43 45 53 20 70 20 44 45 46 45  EFERENCES p DEFE
84e0: 52 52 41 42 4c 45 20 49 4e 49 54 49 41 4c 4c 59  RRABLE INITIALLY
84f0: 20 49 4d 4d 45 44 49 41 54 45 29 3b 0a 20 20 20   IMMEDIATE);.   
8500: 20 42 45 47 49 4e 3b 0a 20 20 7d 0a 7d 20 7b 7d   BEGIN;.  }.} {}
8510: 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d  .do_test e_fkey-
8520: 32 2e 34 20 7b 0a 20 20 63 61 74 63 68 73 71 6c  2.4 {.  catchsql
8530: 20 7b 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 63   { INSERT INTO c
8540: 69 20 56 41 4c 55 45 53 28 27 78 27 2c 20 27 79  i VALUES('x', 'y
8550: 27 29 20 7d 0a 7d 20 7b 31 20 7b 66 6f 72 65 69  ') }.} {1 {forei
8560: 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e  gn key constrain
8570: 74 20 66 61 69 6c 65 64 7d 7d 0a 64 6f 5f 74 65  t failed}}.do_te
8580: 73 74 20 65 5f 66 6b 65 79 2d 32 2e 35 20 7b 0a  st e_fkey-2.5 {.
8590: 20 20 63 61 74 63 68 73 71 6c 20 7b 20 49 4e 53    catchsql { INS
85a0: 45 52 54 20 49 4e 54 4f 20 63 64 20 56 41 4c 55  ERT INTO cd VALU
85b0: 45 53 28 27 78 27 2c 20 27 79 27 29 20 7d 0a 7d  ES('x', 'y') }.}
85c0: 20 7b 30 20 7b 7d 7d 0a 64 6f 5f 74 65 73 74 20   {0 {}}.do_test 
85d0: 65 5f 66 6b 65 79 2d 32 2e 36 20 7b 0a 20 20 63  e_fkey-2.6 {.  c
85e0: 61 74 63 68 73 71 6c 20 7b 20 43 4f 4d 4d 49 54  atchsql { COMMIT
85f0: 20 7d 0a 7d 20 7b 31 20 7b 66 6f 72 65 69 67 6e   }.} {1 {foreign
8600: 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 20   key constraint 
8610: 66 61 69 6c 65 64 7d 7d 0a 64 6f 5f 74 65 73 74  failed}}.do_test
8620: 20 65 5f 66 6b 65 79 2d 32 2e 37 20 7b 0a 20 20   e_fkey-2.7 {.  
8630: 65 78 65 63 73 71 6c 20 7b 20 0a 20 20 20 20 44  execsql { .    D
8640: 45 4c 45 54 45 20 46 52 4f 4d 20 63 64 3b 0a 20  ELETE FROM cd;. 
8650: 20 20 20 43 4f 4d 4d 49 54 3b 0a 20 20 7d 0a 7d     COMMIT;.  }.}
8660: 20 7b 7d 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d   {}..#----------
8670: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
8680: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
8690: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
86a0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a  ---------------.
86b0: 23 20 2f 2a 20 45 56 3a 20 52 2d 34 32 32 36 34  # /* EV: R-42264
86c0: 2d 33 30 35 30 33 20 2a 2f 0a 23 0a 23 20 54 65  -30503 */.#.# Te
86d0: 73 74 20 74 68 61 74 20 74 68 65 20 6d 61 78 69  st that the maxi
86e0: 6d 75 6d 20 72 65 63 75 72 73 69 6f 6e 20 64 65  mum recursion de
86f0: 70 74 68 20 6f 66 20 66 6f 72 65 69 67 6e 20 6b  pth of foreign k
8700: 65 79 20 61 63 74 69 6f 6e 20 70 72 6f 67 72 61  ey action progra
8710: 6d 73 20 69 73 0a 23 20 67 6f 76 65 72 6e 65 64  ms is.# governed
8720: 20 62 79 20 74 68 65 20 53 51 4c 49 54 45 5f 4d   by the SQLITE_M
8730: 41 58 5f 54 52 49 47 47 45 52 5f 44 45 50 54 48  AX_TRIGGER_DEPTH
8740: 20 61 6e 64 20 53 51 4c 49 54 45 5f 4c 49 4d 49   and SQLITE_LIMI
8750: 54 5f 54 52 49 47 47 45 52 5f 44 45 50 54 48 0a  T_TRIGGER_DEPTH.
8760: 23 20 73 65 74 74 69 6e 67 73 2e 0a 23 0a 70 72  # settings..#.pr
8770: 6f 63 20 74 65 73 74 5f 6f 6e 5f 64 65 6c 65 74  oc test_on_delet
8780: 65 5f 72 65 63 75 72 73 69 6f 6e 20 7b 6c 69 6d  e_recursion {lim
8790: 69 74 7d 20 7b 0a 20 20 64 72 6f 70 5f 61 6c 6c  it} {.  drop_all
87a0: 5f 74 61 62 6c 65 73 0a 20 20 65 78 65 63 73 71  _tables.  execsq
87b0: 6c 20 7b 20 0a 20 20 20 20 42 45 47 49 4e 3b 0a  l { .    BEGIN;.
87c0: 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45      CREATE TABLE
87d0: 20 74 30 28 61 20 50 52 49 4d 41 52 59 20 4b 45   t0(a PRIMARY KE
87e0: 59 2c 20 62 29 3b 0a 20 20 20 20 49 4e 53 45 52  Y, b);.    INSER
87f0: 54 20 49 4e 54 4f 20 74 30 20 56 41 4c 55 45 53  T INTO t0 VALUES
8800: 28 27 78 30 27 2c 20 4e 55 4c 4c 29 3b 0a 20 20  ('x0', NULL);.  
8810: 7d 0a 20 20 66 6f 72 20 7b 73 65 74 20 69 20 31  }.  for {set i 1
8820: 7d 20 7b 24 69 20 3c 3d 20 24 6c 69 6d 69 74 7d  } {$i <= $limit}
8830: 20 7b 69 6e 63 72 20 69 7d 20 7b 0a 20 20 20 20   {incr i} {.    
8840: 65 78 65 63 73 71 6c 20 22 0a 20 20 20 20 20 20  execsql ".      
8850: 43 52 45 41 54 45 20 54 41 42 4c 45 20 74 24 69  CREATE TABLE t$i
8860: 20 28 0a 20 20 20 20 20 20 20 20 61 20 50 52 49   (.        a PRI
8870: 4d 41 52 59 20 4b 45 59 2c 20 62 20 52 45 46 45  MARY KEY, b REFE
8880: 52 45 4e 43 45 53 20 74 5b 65 78 70 72 20 24 69  RENCES t[expr $i
8890: 2d 31 5d 20 4f 4e 20 44 45 4c 45 54 45 20 43 41  -1] ON DELETE CA
88a0: 53 43 41 44 45 0a 20 20 20 20 20 20 29 3b 0a 20  SCADE.      );. 
88b0: 20 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f       INSERT INTO
88c0: 20 74 24 69 20 56 41 4c 55 45 53 28 27 78 24 69   t$i VALUES('x$i
88d0: 27 2c 20 27 78 5b 65 78 70 72 20 24 69 2d 31 5d  ', 'x[expr $i-1]
88e0: 27 29 3b 0a 20 20 20 20 22 0a 20 20 7d 0a 20 20  ');.    ".  }.  
88f0: 65 78 65 63 73 71 6c 20 43 4f 4d 4d 49 54 0a 20  execsql COMMIT. 
8900: 20 63 61 74 63 68 73 71 6c 20 22 0a 20 20 20 20   catchsql ".    
8910: 44 45 4c 45 54 45 20 46 52 4f 4d 20 74 30 3b 0a  DELETE FROM t0;.
8920: 20 20 20 20 53 45 4c 45 43 54 20 63 6f 75 6e 74      SELECT count
8930: 28 2a 29 20 46 52 4f 4d 20 74 24 6c 69 6d 69 74  (*) FROM t$limit
8940: 3b 0a 20 20 22 0a 7d 0a 70 72 6f 63 20 74 65 73  ;.  ".}.proc tes
8950: 74 5f 6f 6e 5f 75 70 64 61 74 65 5f 72 65 63 75  t_on_update_recu
8960: 72 73 69 6f 6e 20 7b 6c 69 6d 69 74 7d 20 7b 0a  rsion {limit} {.
8970: 20 20 64 72 6f 70 5f 61 6c 6c 5f 74 61 62 6c 65    drop_all_table
8980: 73 0a 20 20 65 78 65 63 73 71 6c 20 7b 20 0a 20  s.  execsql { . 
8990: 20 20 20 42 45 47 49 4e 3b 0a 20 20 20 20 43 52     BEGIN;.    CR
89a0: 45 41 54 45 20 54 41 42 4c 45 20 74 30 28 61 20  EATE TABLE t0(a 
89b0: 50 52 49 4d 41 52 59 20 4b 45 59 29 3b 0a 20 20  PRIMARY KEY);.  
89c0: 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 30    INSERT INTO t0
89d0: 20 56 41 4c 55 45 53 28 27 78 78 78 27 29 3b 0a   VALUES('xxx');.
89e0: 20 20 7d 0a 20 20 66 6f 72 20 7b 73 65 74 20 69    }.  for {set i
89f0: 20 31 7d 20 7b 24 69 20 3c 3d 20 24 6c 69 6d 69   1} {$i <= $limi
8a00: 74 7d 20 7b 69 6e 63 72 20 69 7d 20 7b 0a 20 20  t} {incr i} {.  
8a10: 20 20 73 65 74 20 6a 20 5b 65 78 70 72 20 24 69    set j [expr $i
8a20: 2d 31 5d 0a 0a 20 20 20 20 65 78 65 63 73 71 6c  -1]..    execsql
8a30: 20 22 0a 20 20 20 20 20 20 43 52 45 41 54 45 20   ".      CREATE 
8a40: 54 41 42 4c 45 20 74 24 69 20 28 61 20 50 52 49  TABLE t$i (a PRI
8a50: 4d 41 52 59 20 4b 45 59 20 52 45 46 45 52 45 4e  MARY KEY REFEREN
8a60: 43 45 53 20 74 24 6a 20 4f 4e 20 55 50 44 41 54  CES t$j ON UPDAT
8a70: 45 20 43 41 53 43 41 44 45 29 3b 0a 20 20 20 20  E CASCADE);.    
8a80: 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 24    INSERT INTO t$
8a90: 69 20 56 41 4c 55 45 53 28 27 78 78 78 27 29 3b  i VALUES('xxx');
8aa0: 0a 20 20 20 20 22 0a 20 20 7d 0a 20 20 65 78 65  .    ".  }.  exe
8ab0: 63 73 71 6c 20 43 4f 4d 4d 49 54 0a 20 20 63 61  csql COMMIT.  ca
8ac0: 74 63 68 73 71 6c 20 22 0a 20 20 20 20 55 50 44  tchsql ".    UPD
8ad0: 41 54 45 20 74 30 20 53 45 54 20 61 20 3d 20 27  ATE t0 SET a = '
8ae0: 79 79 79 27 3b 0a 20 20 20 20 53 45 4c 45 43 54  yyy';.    SELECT
8af0: 20 4e 4f 54 20 28 61 3d 27 79 79 79 27 29 20 46   NOT (a='yyy') F
8b00: 52 4f 4d 20 74 24 6c 69 6d 69 74 3b 0a 20 20 22  ROM t$limit;.  "
8b10: 0a 7d 0a 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b  .}..do_test e_fk
8b20: 65 79 2d 33 2e 31 2e 31 20 7b 0a 20 20 74 65 73  ey-3.1.1 {.  tes
8b30: 74 5f 6f 6e 5f 64 65 6c 65 74 65 5f 72 65 63 75  t_on_delete_recu
8b40: 72 73 69 6f 6e 20 24 53 51 4c 49 54 45 5f 4d 41  rsion $SQLITE_MA
8b50: 58 5f 54 52 49 47 47 45 52 5f 44 45 50 54 48 0a  X_TRIGGER_DEPTH.
8b60: 7d 20 7b 30 20 30 7d 0a 64 6f 5f 74 65 73 74 20  } {0 0}.do_test 
8b70: 65 5f 66 6b 65 79 2d 33 2e 31 2e 32 20 7b 0a 20  e_fkey-3.1.2 {. 
8b80: 20 74 65 73 74 5f 6f 6e 5f 64 65 6c 65 74 65 5f   test_on_delete_
8b90: 72 65 63 75 72 73 69 6f 6e 20 5b 65 78 70 72 20  recursion [expr 
8ba0: 24 53 51 4c 49 54 45 5f 4d 41 58 5f 54 52 49 47  $SQLITE_MAX_TRIG
8bb0: 47 45 52 5f 44 45 50 54 48 2b 31 5d 0a 7d 20 7b  GER_DEPTH+1].} {
8bc0: 31 20 7b 74 6f 6f 20 6d 61 6e 79 20 6c 65 76 65  1 {too many leve
8bd0: 6c 73 20 6f 66 20 74 72 69 67 67 65 72 20 72 65  ls of trigger re
8be0: 63 75 72 73 69 6f 6e 7d 7d 0a 64 6f 5f 74 65 73  cursion}}.do_tes
8bf0: 74 20 65 5f 66 6b 65 79 2d 33 2e 31 2e 33 20 7b  t e_fkey-3.1.3 {
8c00: 0a 20 20 73 71 6c 69 74 65 33 5f 6c 69 6d 69 74  .  sqlite3_limit
8c10: 20 64 62 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54   db SQLITE_LIMIT
8c20: 5f 54 52 49 47 47 45 52 5f 44 45 50 54 48 20 35  _TRIGGER_DEPTH 5
8c30: 0a 20 20 74 65 73 74 5f 6f 6e 5f 64 65 6c 65 74  .  test_on_delet
8c40: 65 5f 72 65 63 75 72 73 69 6f 6e 20 35 0a 7d 20  e_recursion 5.} 
8c50: 7b 30 20 30 7d 0a 64 6f 5f 74 65 73 74 20 65 5f  {0 0}.do_test e_
8c60: 66 6b 65 79 2d 33 2e 31 2e 34 20 7b 0a 20 20 74  fkey-3.1.4 {.  t
8c70: 65 73 74 5f 6f 6e 5f 64 65 6c 65 74 65 5f 72 65  est_on_delete_re
8c80: 63 75 72 73 69 6f 6e 20 36 0a 7d 20 7b 31 20 7b  cursion 6.} {1 {
8c90: 74 6f 6f 20 6d 61 6e 79 20 6c 65 76 65 6c 73 20  too many levels 
8ca0: 6f 66 20 74 72 69 67 67 65 72 20 72 65 63 75 72  of trigger recur
8cb0: 73 69 6f 6e 7d 7d 0a 64 6f 5f 74 65 73 74 20 65  sion}}.do_test e
8cc0: 5f 66 6b 65 79 2d 33 2e 31 2e 35 20 7b 0a 20 20  _fkey-3.1.5 {.  
8cd0: 73 71 6c 69 74 65 33 5f 6c 69 6d 69 74 20 64 62  sqlite3_limit db
8ce0: 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 54 52   SQLITE_LIMIT_TR
8cf0: 49 47 47 45 52 5f 44 45 50 54 48 20 31 30 30 30  IGGER_DEPTH 1000
8d00: 30 30 30 0a 7d 20 7b 35 7d 0a 64 6f 5f 74 65 73  000.} {5}.do_tes
8d10: 74 20 65 5f 66 6b 65 79 2d 33 2e 32 2e 31 20 7b  t e_fkey-3.2.1 {
8d20: 0a 20 20 74 65 73 74 5f 6f 6e 5f 75 70 64 61 74  .  test_on_updat
8d30: 65 5f 72 65 63 75 72 73 69 6f 6e 20 24 53 51 4c  e_recursion $SQL
8d40: 49 54 45 5f 4d 41 58 5f 54 52 49 47 47 45 52 5f  ITE_MAX_TRIGGER_
8d50: 44 45 50 54 48 0a 7d 20 7b 30 20 30 7d 0a 64 6f  DEPTH.} {0 0}.do
8d60: 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 33 2e 32  _test e_fkey-3.2
8d70: 2e 32 20 7b 0a 20 20 74 65 73 74 5f 6f 6e 5f 75  .2 {.  test_on_u
8d80: 70 64 61 74 65 5f 72 65 63 75 72 73 69 6f 6e 20  pdate_recursion 
8d90: 5b 65 78 70 72 20 24 53 51 4c 49 54 45 5f 4d 41  [expr $SQLITE_MA
8da0: 58 5f 54 52 49 47 47 45 52 5f 44 45 50 54 48 2b  X_TRIGGER_DEPTH+
8db0: 31 5d 0a 7d 20 7b 31 20 7b 74 6f 6f 20 6d 61 6e  1].} {1 {too man
8dc0: 79 20 6c 65 76 65 6c 73 20 6f 66 20 74 72 69 67  y levels of trig
8dd0: 67 65 72 20 72 65 63 75 72 73 69 6f 6e 7d 7d 0a  ger recursion}}.
8de0: 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 33  do_test e_fkey-3
8df0: 2e 32 2e 33 20 7b 0a 20 20 73 71 6c 69 74 65 33  .2.3 {.  sqlite3
8e00: 5f 6c 69 6d 69 74 20 64 62 20 53 51 4c 49 54 45  _limit db SQLITE
8e10: 5f 4c 49 4d 49 54 5f 54 52 49 47 47 45 52 5f 44  _LIMIT_TRIGGER_D
8e20: 45 50 54 48 20 35 0a 20 20 74 65 73 74 5f 6f 6e  EPTH 5.  test_on
8e30: 5f 75 70 64 61 74 65 5f 72 65 63 75 72 73 69 6f  _update_recursio
8e40: 6e 20 35 0a 7d 20 7b 30 20 30 7d 0a 64 6f 5f 74  n 5.} {0 0}.do_t
8e50: 65 73 74 20 65 5f 66 6b 65 79 2d 33 2e 32 2e 34  est e_fkey-3.2.4
8e60: 20 7b 0a 20 20 74 65 73 74 5f 6f 6e 5f 75 70 64   {.  test_on_upd
8e70: 61 74 65 5f 72 65 63 75 72 73 69 6f 6e 20 36 0a  ate_recursion 6.
8e80: 7d 20 7b 31 20 7b 74 6f 6f 20 6d 61 6e 79 20 6c  } {1 {too many l
8e90: 65 76 65 6c 73 20 6f 66 20 74 72 69 67 67 65 72  evels of trigger
8ea0: 20 72 65 63 75 72 73 69 6f 6e 7d 7d 0a 64 6f 5f   recursion}}.do_
8eb0: 74 65 73 74 20 65 5f 66 6b 65 79 2d 33 2e 32 2e  test e_fkey-3.2.
8ec0: 35 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6c 69  5 {.  sqlite3_li
8ed0: 6d 69 74 20 64 62 20 53 51 4c 49 54 45 5f 4c 49  mit db SQLITE_LI
8ee0: 4d 49 54 5f 54 52 49 47 47 45 52 5f 44 45 50 54  MIT_TRIGGER_DEPT
8ef0: 48 20 31 30 30 30 30 30 30 0a 7d 20 7b 35 7d 0a  H 1000000.} {5}.
8f00: 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  .#--------------
8f10: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
8f20: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
8f30: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
8f40: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20 2f 2a  -----------.# /*
8f50: 20 45 56 3a 20 52 2d 35 31 37 36 39 2d 33 32 37   EV: R-51769-327
8f60: 33 30 20 2a 2f 0a 23 0a 23 20 54 68 65 20 73 65  30 */.#.# The se
8f70: 74 74 69 6e 67 20 6f 66 20 74 68 65 20 72 65 63  tting of the rec
8f80: 75 72 73 69 76 65 5f 74 72 69 67 67 65 72 73 20  ursive_triggers 
8f90: 70 72 61 67 6d 61 20 64 6f 65 73 20 6e 6f 74 20  pragma does not 
8fa0: 61 66 66 65 63 74 20 66 6f 72 65 69 67 6e 0a 23  affect foreign.#
8fb0: 20 6b 65 79 20 61 63 74 69 6f 6e 73 2e 0a 23 0a   key actions..#.
8fc0: 66 6f 72 65 61 63 68 20 72 65 63 75 72 73 69 76  foreach recursiv
8fd0: 65 5f 74 72 69 67 67 65 72 73 5f 73 65 74 74 69  e_triggers_setti
8fe0: 6e 67 20 5b 6c 69 73 74 20 30 20 31 20 4f 4e 20  ng [list 0 1 ON 
8ff0: 4f 46 46 5d 20 7b 0a 20 20 64 72 6f 70 5f 61 6c  OFF] {.  drop_al
9000: 6c 5f 74 61 62 6c 65 73 0a 20 20 65 78 65 63 73  l_tables.  execs
9010: 71 6c 20 22 50 52 41 47 4d 41 20 72 65 63 75 72  ql "PRAGMA recur
9020: 73 69 76 65 5f 74 72 69 67 67 65 72 73 20 3d 20  sive_triggers = 
9030: 24 72 65 63 75 72 73 69 76 65 5f 74 72 69 67 67  $recursive_trigg
9040: 65 72 73 5f 73 65 74 74 69 6e 67 22 0a 0a 20 20  ers_setting"..  
9050: 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 34  do_test e_fkey-4
9060: 2e 24 72 65 63 75 72 73 69 76 65 5f 74 72 69 67  .$recursive_trig
9070: 67 65 72 73 5f 73 65 74 74 69 6e 67 2e 31 20 7b  gers_setting.1 {
9080: 0a 20 20 20 20 65 78 65 63 73 71 6c 20 7b 0a 20  .    execsql {. 
9090: 20 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c       CREATE TABL
90a0: 45 20 74 31 28 61 20 50 52 49 4d 41 52 59 20 4b  E t1(a PRIMARY K
90b0: 45 59 2c 20 62 20 52 45 46 45 52 45 4e 43 45 53  EY, b REFERENCES
90c0: 20 74 31 20 4f 4e 20 44 45 4c 45 54 45 20 43 41   t1 ON DELETE CA
90d0: 53 43 41 44 45 29 3b 0a 20 20 20 20 20 20 49 4e  SCADE);.      IN
90e0: 53 45 52 54 20 49 4e 54 4f 20 74 31 20 56 41 4c  SERT INTO t1 VAL
90f0: 55 45 53 28 31 2c 20 4e 55 4c 4c 29 3b 0a 20 20  UES(1, NULL);.  
9100: 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20      INSERT INTO 
9110: 74 31 20 56 41 4c 55 45 53 28 32 2c 20 31 29 3b  t1 VALUES(2, 1);
9120: 0a 20 20 20 20 20 20 49 4e 53 45 52 54 20 49 4e  .      INSERT IN
9130: 54 4f 20 74 31 20 56 41 4c 55 45 53 28 33 2c 20  TO t1 VALUES(3, 
9140: 32 29 3b 0a 20 20 20 20 20 20 49 4e 53 45 52 54  2);.      INSERT
9150: 20 49 4e 54 4f 20 74 31 20 56 41 4c 55 45 53 28   INTO t1 VALUES(
9160: 34 2c 20 33 29 3b 0a 20 20 20 20 20 20 49 4e 53  4, 3);.      INS
9170: 45 52 54 20 49 4e 54 4f 20 74 31 20 56 41 4c 55  ERT INTO t1 VALU
9180: 45 53 28 35 2c 20 34 29 3b 0a 20 20 20 20 20 20  ES(5, 4);.      
9190: 53 45 4c 45 43 54 20 63 6f 75 6e 74 28 2a 29 20  SELECT count(*) 
91a0: 46 52 4f 4d 20 74 31 3b 0a 20 20 20 20 7d 0a 20  FROM t1;.    }. 
91b0: 20 7d 20 7b 35 7d 0a 20 20 64 6f 5f 74 65 73 74   } {5}.  do_test
91c0: 20 65 5f 66 6b 65 79 2d 34 2e 24 72 65 63 75 72   e_fkey-4.$recur
91d0: 73 69 76 65 5f 74 72 69 67 67 65 72 73 5f 73 65  sive_triggers_se
91e0: 74 74 69 6e 67 2e 32 20 7b 0a 20 20 20 20 65 78  tting.2 {.    ex
91f0: 65 63 73 71 6c 20 7b 20 53 45 4c 45 43 54 20 63  ecsql { SELECT c
9200: 6f 75 6e 74 28 2a 29 20 46 52 4f 4d 20 74 31 20  ount(*) FROM t1 
9210: 57 48 45 52 45 20 61 20 3d 20 31 20 7d 0a 20 20  WHERE a = 1 }.  
9220: 7d 20 7b 31 7d 0a 20 20 64 6f 5f 74 65 73 74 20  } {1}.  do_test 
9230: 65 5f 66 6b 65 79 2d 34 2e 24 72 65 63 75 72 73  e_fkey-4.$recurs
9240: 69 76 65 5f 74 72 69 67 67 65 72 73 5f 73 65 74  ive_triggers_set
9250: 74 69 6e 67 2e 33 20 7b 0a 20 20 20 20 65 78 65  ting.3 {.    exe
9260: 63 73 71 6c 20 7b 20 0a 20 20 20 20 20 20 44 45  csql { .      DE
9270: 4c 45 54 45 20 46 52 4f 4d 20 74 31 20 57 48 45  LETE FROM t1 WHE
9280: 52 45 20 61 20 3d 20 31 3b 0a 20 20 20 20 20 20  RE a = 1;.      
9290: 53 45 4c 45 43 54 20 63 6f 75 6e 74 28 2a 29 20  SELECT count(*) 
92a0: 46 52 4f 4d 20 74 31 3b 0a 20 20 20 20 7d 0a 20  FROM t1;.    }. 
92b0: 20 7d 20 7b 30 7d 0a 7d 0a 0a 66 69 6e 69 73 68   } {0}.}..finish
92c0: 5f 74 65 73 74 0a                                _test.