/ Hex Artifact Content
Login

Artifact fd1fcf89badd5f2773d7ac04775b5ff3488eda17:


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 23 20  n document..#.# 
01d0: 54 68 65 20 74 65 73 74 73 20 69 6e 20 74 68 69  The tests in thi
01e0: 73 20 66 69 6c 65 20 61 72 65 20 61 72 72 61 6e  s file are arran
01f0: 67 65 64 20 74 6f 20 6d 69 72 72 6f 72 20 74 68  ged to mirror th
0200: 65 20 73 74 72 75 63 74 75 72 65 20 6f 66 20 0a  e structure of .
0210: 23 20 66 6f 72 65 69 67 6e 6b 65 79 2e 69 6e 2c  # foreignkey.in,
0220: 20 77 69 74 68 20 6f 6e 65 20 65 78 63 65 70 74   with one except
0230: 69 6f 6e 3a 20 54 68 65 20 73 74 61 74 65 6d 65  ion: The stateme
0240: 6e 74 73 20 69 6e 20 73 65 63 74 69 6f 6e 20 32  nts in section 2
0250: 2c 20 77 68 69 63 68 20 0a 23 20 64 65 61 6c 73  , which .# deals
0260: 20 77 69 74 68 20 65 6e 61 62 6c 69 6e 67 2f 64   with enabling/d
0270: 69 73 61 62 6c 69 6e 67 20 66 6f 72 65 69 67 6e  isabling foreign
0280: 20 6b 65 79 20 73 75 70 70 6f 72 74 2c 20 69 73   key support, is
0290: 20 74 65 73 74 65 64 20 66 69 72 73 74 2c 0a 23   tested first,.#
02a0: 20 62 65 66 6f 72 65 20 73 65 63 74 69 6f 6e 20   before section 
02b0: 31 2e 20 54 68 69 73 20 69 73 20 62 65 63 61 75  1. This is becau
02c0: 73 65 20 73 6f 6d 65 20 73 74 61 74 65 6d 65 6e  se some statemen
02d0: 74 73 20 69 6e 20 73 65 63 74 69 6f 6e 20 32 20  ts in section 2 
02e0: 64 65 61 6c 0a 23 20 77 69 74 68 20 62 75 69 6c  deal.# with buil
02f0: 64 73 20 74 68 61 74 20 64 6f 20 6e 6f 74 20 69  ds that do not i
0300: 6e 63 6c 75 64 65 20 63 6f 6d 70 6c 65 74 65 20  nclude complete 
0310: 66 6f 72 65 69 67 6e 20 6b 65 79 20 73 75 70 70  foreign key supp
0320: 6f 72 74 20 28 62 65 63 61 75 73 65 0a 23 20 65  ort (because.# e
0330: 69 74 68 65 72 20 53 51 4c 49 54 45 5f 4f 4d 49  ither SQLITE_OMI
0340: 54 5f 54 52 49 47 47 45 52 20 6f 72 20 53 51 4c  T_TRIGGER or SQL
0350: 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e  ITE_OMIT_FOREIGN
0360: 5f 4b 45 59 20 77 61 73 20 64 65 66 69 6e 65 64  _KEY was defined
0370: 0a 23 20 61 74 20 62 75 69 6c 64 20 74 69 6d 65  .# at build time
0380: 29 2e 0a 23 0a 0a 73 65 74 20 74 65 73 74 64 69  )..#..set testdi
0390: 72 20 5b 66 69 6c 65 20 64 69 72 6e 61 6d 65 20  r [file dirname 
03a0: 24 61 72 67 76 30 5d 0a 73 6f 75 72 63 65 20 24  $argv0].source $
03b0: 74 65 73 74 64 69 72 2f 74 65 73 74 65 72 2e 74  testdir/tester.t
03c0: 63 6c 0a 0a 70 72 6f 63 20 65 71 70 20 7b 73 71  cl..proc eqp {sq
03d0: 6c 20 7b 64 62 20 64 62 7d 7d 20 7b 20 75 70 6c  l {db db}} { upl
03e0: 65 76 65 6c 20 65 78 65 63 73 71 6c 20 5b 6c 69  evel execsql [li
03f0: 73 74 20 22 45 58 50 4c 41 49 4e 20 51 55 45 52  st "EXPLAIN QUER
0400: 59 20 50 4c 41 4e 20 24 73 71 6c 22 5d 20 24 64  Y PLAN $sql"] $d
0410: 62 20 7d 0a 0a 23 23 23 23 23 23 23 23 23 23 23  b }..###########
0420: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
0430: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
0440: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
0450: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
0460: 0a 23 23 23 20 53 45 43 54 49 4f 4e 20 32 3a 20  .### SECTION 2: 
0470: 45 6e 61 62 6c 69 6e 67 20 46 6f 72 65 69 67 6e  Enabling Foreign
0480: 20 4b 65 79 20 53 75 70 70 6f 72 74 0a 23 23 23   Key Support.###
0490: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
04a0: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
04b0: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
04c0: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
04d0: 23 23 23 23 23 23 23 23 0a 0a 23 2d 2d 2d 2d 2d  ########..#-----
04e0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
04f0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0500: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0510: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0520: 2d 2d 2d 2d 0a 23 20 2f 2a 20 45 56 3a 20 52 2d  ----.# /* EV: R-
0530: 33 33 37 31 30 2d 35 36 33 34 34 20 2a 2f 0a 23  33710-56344 */.#
0540: 0a 23 20 54 65 73 74 20 62 75 69 6c 64 73 20 6e  .# Test builds n
0550: 65 69 74 68 65 72 20 4f 4d 49 54 5f 46 4f 52 45  either OMIT_FORE
0560: 49 47 4e 5f 4b 45 59 20 6f 72 20 4f 4d 49 54 5f  IGN_KEY or OMIT_
0570: 54 52 49 47 47 45 52 20 64 65 66 69 6e 65 64 20  TRIGGER defined 
0580: 68 61 76 65 20 0a 23 20 66 6f 72 65 69 67 6e 20  have .# foreign 
0590: 6b 65 79 20 66 75 6e 63 74 69 6f 6e 61 6c 69 74  key functionalit
05a0: 79 2e 0a 23 0a 69 66 63 61 70 61 62 6c 65 20 74  y..#.ifcapable t
05b0: 72 69 67 67 65 72 26 26 66 6f 72 65 69 67 6e 6b  rigger&&foreignk
05c0: 65 79 20 7b 0a 20 20 64 6f 5f 74 65 73 74 20 65  ey {.  do_test e
05d0: 5f 66 6b 65 79 2d 31 20 7b 0a 20 20 20 20 65 78  _fkey-1 {.    ex
05e0: 65 63 73 71 6c 20 7b 0a 20 20 20 20 20 20 50 52  ecsql {.      PR
05f0: 41 47 4d 41 20 66 6f 72 65 69 67 6e 5f 6b 65 79  AGMA foreign_key
0600: 73 20 3d 20 4f 4e 3b 0a 20 20 20 20 20 20 43 52  s = ON;.      CR
0610: 45 41 54 45 20 54 41 42 4c 45 20 70 28 69 20 50  EATE TABLE p(i P
0620: 52 49 4d 41 52 59 20 4b 45 59 29 3b 0a 20 20 20  RIMARY KEY);.   
0630: 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20     CREATE TABLE 
0640: 63 28 6a 20 52 45 46 45 52 45 4e 43 45 53 20 70  c(j REFERENCES p
0650: 20 4f 4e 20 55 50 44 41 54 45 20 43 41 53 43 41   ON UPDATE CASCA
0660: 44 45 29 3b 0a 20 20 20 20 20 20 49 4e 53 45 52  DE);.      INSER
0670: 54 20 49 4e 54 4f 20 70 20 56 41 4c 55 45 53 28  T INTO p VALUES(
0680: 27 68 65 6c 6c 6f 27 29 3b 0a 20 20 20 20 20 20  'hello');.      
0690: 49 4e 53 45 52 54 20 49 4e 54 4f 20 63 20 56 41  INSERT INTO c VA
06a0: 4c 55 45 53 28 27 68 65 6c 6c 6f 27 29 3b 0a 20  LUES('hello');. 
06b0: 20 20 20 20 20 55 50 44 41 54 45 20 70 20 53 45       UPDATE p SE
06c0: 54 20 69 20 3d 20 27 77 6f 72 6c 64 27 3b 0a 20  T i = 'world';. 
06d0: 20 20 20 20 20 53 45 4c 45 43 54 20 2a 20 46 52       SELECT * FR
06e0: 4f 4d 20 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 20  OM c;.    }.  } 
06f0: 7b 77 6f 72 6c 64 7d 0a 7d 0a 0a 23 2d 2d 2d 2d  {world}.}..#----
0700: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0710: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0720: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0730: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0740: 2d 2d 2d 2d 2d 0a 23 20 2f 2a 20 45 56 3a 20 52  -----.# /* EV: R
0750: 2d 34 34 36 39 37 2d 36 31 35 34 33 20 2a 2f 0a  -44697-61543 */.
0760: 23 0a 23 20 54 65 73 74 20 74 68 65 20 65 66 66  #.# Test the eff
0770: 65 63 74 73 20 6f 66 20 64 65 66 69 6e 69 6e 67  ects of defining
0780: 20 4f 4d 49 54 5f 54 52 49 47 47 45 52 20 62 75   OMIT_TRIGGER bu
0790: 74 20 6e 6f 74 20 4f 4d 49 54 5f 46 4f 52 45 49  t not OMIT_FOREI
07a0: 47 4e 5f 4b 45 59 2e 0a 23 0a 23 20 2f 2a 20 45  GN_KEY..#.# /* E
07b0: 56 3a 20 52 2d 32 32 35 36 37 2d 34 34 30 33 39  V: R-22567-44039
07c0: 20 2a 2f 0a 23 20 2f 2a 20 45 56 3a 20 52 2d 34   */.# /* EV: R-4
07d0: 31 37 38 34 2d 31 33 33 33 39 20 2a 2f 0a 23 0a  1784-13339 */.#.
07e0: 23 20 53 70 65 63 69 66 69 63 61 6c 6c 79 2c 20  # Specifically, 
07f0: 74 65 73 74 20 74 68 61 74 20 22 50 52 41 47 4d  test that "PRAGM
0800: 41 20 66 6f 72 65 69 67 6e 5f 6b 65 79 73 22 20  A foreign_keys" 
0810: 69 73 20 61 20 6e 6f 2d 6f 70 20 69 6e 20 74 68  is a no-op in th
0820: 69 73 20 63 61 73 65 2e 0a 23 20 57 68 65 6e 20  is case..# When 
0830: 75 73 69 6e 67 20 74 68 65 20 70 72 61 67 6d 61  using the pragma
0840: 20 74 6f 20 71 75 65 72 79 20 74 68 65 20 63 75   to query the cu
0850: 72 72 65 6e 74 20 73 65 74 74 69 6e 67 2c 20 30  rrent setting, 0
0860: 20 72 6f 77 73 20 61 72 65 20 72 65 74 75 72 6e   rows are return
0870: 65 64 2e 0a 23 0a 72 65 73 65 74 5f 64 62 0a 69  ed..#.reset_db.i
0880: 66 63 61 70 61 62 6c 65 20 21 74 72 69 67 67 65  fcapable !trigge
0890: 72 26 26 66 6f 72 65 69 67 6e 6b 65 79 20 7b 0a  r&&foreignkey {.
08a0: 20 20 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79    do_test e_fkey
08b0: 2d 32 2e 31 20 7b 0a 20 20 20 20 65 78 65 63 73  -2.1 {.    execs
08c0: 71 6c 20 7b 0a 20 20 20 20 20 20 50 52 41 47 4d  ql {.      PRAGM
08d0: 41 20 66 6f 72 65 69 67 6e 5f 6b 65 79 73 20 3d  A foreign_keys =
08e0: 20 4f 4e 3b 0a 20 20 20 20 20 20 43 52 45 41 54   ON;.      CREAT
08f0: 45 20 54 41 42 4c 45 20 70 28 69 20 50 52 49 4d  E TABLE p(i PRIM
0900: 41 52 59 20 4b 45 59 29 3b 0a 20 20 20 20 20 20  ARY KEY);.      
0910: 43 52 45 41 54 45 20 54 41 42 4c 45 20 63 28 6a  CREATE TABLE c(j
0920: 20 52 45 46 45 52 45 4e 43 45 53 20 70 20 4f 4e   REFERENCES p ON
0930: 20 55 50 44 41 54 45 20 43 41 53 43 41 44 45 29   UPDATE CASCADE)
0940: 3b 0a 20 20 20 20 20 20 49 4e 53 45 52 54 20 49  ;.      INSERT I
0950: 4e 54 4f 20 70 20 56 41 4c 55 45 53 28 27 68 65  NTO p VALUES('he
0960: 6c 6c 6f 27 29 3b 0a 20 20 20 20 20 20 49 4e 53  llo');.      INS
0970: 45 52 54 20 49 4e 54 4f 20 63 20 56 41 4c 55 45  ERT INTO c VALUE
0980: 53 28 27 68 65 6c 6c 6f 27 29 3b 0a 20 20 20 20  S('hello');.    
0990: 20 20 55 50 44 41 54 45 20 70 20 53 45 54 20 69    UPDATE p SET i
09a0: 20 3d 20 27 77 6f 72 6c 64 27 3b 0a 20 20 20 20   = 'world';.    
09b0: 20 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20    SELECT * FROM 
09c0: 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 20 7b 68 65  c;.    }.  } {he
09d0: 6c 6c 6f 7d 0a 20 20 64 6f 5f 74 65 73 74 20 65  llo}.  do_test e
09e0: 5f 66 6b 65 79 2d 32 2e 32 20 7b 0a 20 20 20 20  _fkey-2.2 {.    
09f0: 65 78 65 63 73 71 6c 20 7b 20 50 52 41 47 4d 41  execsql { PRAGMA
0a00: 20 66 6f 72 65 69 67 6e 5f 6b 65 79 5f 6c 69 73   foreign_key_lis
0a10: 74 28 63 29 20 7d 0a 20 20 7d 20 7b 30 20 30 20  t(c) }.  } {0 0 
0a20: 70 20 6a 20 7b 7d 20 43 41 53 43 41 44 45 20 7b  p j {} CASCADE {
0a30: 4e 4f 20 41 43 54 49 4f 4e 7d 20 4e 4f 4e 45 7d  NO ACTION} NONE}
0a40: 0a 20 20 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65  .  do_test e_fke
0a50: 79 2d 32 2e 33 20 7b 0a 20 20 20 20 65 78 65 63  y-2.3 {.    exec
0a60: 73 71 6c 20 7b 20 50 52 41 47 4d 41 20 66 6f 72  sql { PRAGMA for
0a70: 65 69 67 6e 5f 6b 65 79 73 20 7d 0a 20 20 7d 20  eign_keys }.  } 
0a80: 7b 7d 0a 7d 0a 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d  {}.}...#--------
0a90: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0aa0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0ab0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0ac0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0ad0: 2d 0a 23 20 2f 2a 20 45 56 3a 20 52 2d 35 38 34  -.# /* EV: R-584
0ae0: 32 38 2d 33 36 36 36 30 20 2a 2f 0a 23 0a 23 20  28-36660 */.#.# 
0af0: 54 65 73 74 20 74 68 65 20 65 66 66 65 63 74 73  Test the effects
0b00: 20 6f 66 20 64 65 66 69 6e 69 6e 67 20 4f 4d 49   of defining OMI
0b10: 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 2e 0a 23  T_FOREIGN_KEY..#
0b20: 0a 23 20 2f 2a 20 45 56 3a 20 52 2d 35 38 34 32  .# /* EV: R-5842
0b30: 38 2d 33 36 36 36 30 20 2a 2f 0a 23 0a 23 20 53  8-36660 */.#.# S
0b40: 70 65 63 69 66 69 63 61 6c 6c 79 2c 20 74 65 73  pecifically, tes
0b50: 74 20 74 68 61 74 20 66 6f 72 65 69 67 6e 20 6b  t that foreign k
0b60: 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 63  ey constraints c
0b70: 61 6e 6e 6f 74 20 65 76 65 6e 20 62 65 20 70 61  annot even be pa
0b80: 72 73 65 64 20 0a 23 20 69 6e 20 73 75 63 68 20  rsed .# in such 
0b90: 61 20 62 75 69 6c 64 2e 0a 23 0a 72 65 73 65 74  a build..#.reset
0ba0: 5f 64 62 0a 69 66 63 61 70 61 62 6c 65 20 21 66  _db.ifcapable !f
0bb0: 6f 72 65 69 67 6e 6b 65 79 20 7b 0a 20 20 64 6f  oreignkey {.  do
0bc0: 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 33 2e 31  _test e_fkey-3.1
0bd0: 20 7b 0a 20 20 20 20 65 78 65 63 73 71 6c 20 7b   {.    execsql {
0be0: 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 70 28   CREATE TABLE p(
0bf0: 69 20 50 52 49 4d 41 52 59 20 4b 45 59 29 20 7d  i PRIMARY KEY) }
0c00: 0a 20 20 20 20 63 61 74 63 68 73 71 6c 20 7b 20  .    catchsql { 
0c10: 43 52 45 41 54 45 20 54 41 42 4c 45 20 63 28 6a  CREATE TABLE c(j
0c20: 20 52 45 46 45 52 45 4e 43 45 53 20 70 20 4f 4e   REFERENCES p ON
0c30: 20 55 50 44 41 54 45 20 43 41 53 43 41 44 45 29   UPDATE CASCADE)
0c40: 20 7d 0a 20 20 7d 20 7b 31 20 7b 6e 65 61 72 20   }.  } {1 {near 
0c50: 22 4f 4e 22 3a 20 73 79 6e 74 61 78 20 65 72 72  "ON": syntax err
0c60: 6f 72 7d 7d 0a 20 20 64 6f 5f 74 65 73 74 20 65  or}}.  do_test e
0c70: 5f 66 6b 65 79 2d 33 2e 32 20 7b 0a 20 20 20 20  _fkey-3.2 {.    
0c80: 23 20 54 68 69 73 20 69 73 20 61 6c 6c 6f 77 65  # This is allowe
0c90: 64 2c 20 61 73 20 69 6e 20 74 68 69 73 20 62 75  d, as in this bu
0ca0: 69 6c 64 2c 20 22 52 45 46 45 52 45 4e 43 45 53  ild, "REFERENCES
0cb0: 22 20 69 73 20 6e 6f 74 20 61 20 6b 65 79 77 6f  " is not a keywo
0cc0: 72 64 2e 0a 20 20 20 20 23 20 54 68 65 20 64 65  rd..    # The de
0cd0: 63 6c 61 72 65 64 20 64 61 74 61 74 79 70 65 20  clared datatype 
0ce0: 6f 66 20 63 6f 6c 75 6d 6e 20 6a 20 69 73 20 22  of column j is "
0cf0: 52 45 46 45 52 45 4e 43 45 53 20 70 22 2e 0a 20  REFERENCES p".. 
0d00: 20 20 20 65 78 65 63 73 71 6c 20 7b 20 43 52 45     execsql { CRE
0d10: 41 54 45 20 54 41 42 4c 45 20 63 28 6a 20 52 45  ATE TABLE c(j RE
0d20: 46 45 52 45 4e 43 45 53 20 70 29 20 7d 0a 20 20  FERENCES p) }.  
0d30: 7d 20 7b 7d 0a 20 20 64 6f 5f 74 65 73 74 20 65  } {}.  do_test e
0d40: 5f 66 6b 65 79 2d 33 2e 33 20 7b 0a 20 20 20 20  _fkey-3.3 {.    
0d50: 65 78 65 63 73 71 6c 20 7b 20 50 52 41 47 4d 41  execsql { PRAGMA
0d60: 20 74 61 62 6c 65 5f 69 6e 66 6f 28 63 29 20 7d   table_info(c) }
0d70: 0a 20 20 7d 20 7b 30 20 6a 20 7b 52 45 46 45 52  .  } {0 j {REFER
0d80: 45 4e 43 45 53 20 70 7d 20 30 20 7b 7d 20 30 7d  ENCES p} 0 {} 0}
0d90: 0a 20 20 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65  .  do_test e_fke
0da0: 79 2d 33 2e 34 20 7b 0a 20 20 20 20 65 78 65 63  y-3.4 {.    exec
0db0: 73 71 6c 20 7b 20 50 52 41 47 4d 41 20 66 6f 72  sql { PRAGMA for
0dc0: 65 69 67 6e 5f 6b 65 79 5f 6c 69 73 74 28 63 29  eign_key_list(c)
0dd0: 20 7d 0a 20 20 7d 20 7b 7d 0a 20 20 64 6f 5f 74   }.  } {}.  do_t
0de0: 65 73 74 20 65 5f 66 6b 65 79 2d 33 2e 35 20 7b  est e_fkey-3.5 {
0df0: 0a 20 20 20 20 65 78 65 63 73 71 6c 20 7b 20 50  .    execsql { P
0e00: 52 41 47 4d 41 20 66 6f 72 65 69 67 6e 5f 6b 65  RAGMA foreign_ke
0e10: 79 73 20 7d 0a 20 20 7d 20 7b 7d 0a 7d 0a 0a 69  ys }.  } {}.}..i
0e20: 66 63 61 70 61 62 6c 65 20 21 66 6f 72 65 69 67  fcapable !foreig
0e30: 6e 6b 65 79 7c 7c 21 74 72 69 67 67 65 72 20 7b  nkey||!trigger {
0e40: 20 66 69 6e 69 73 68 5f 74 65 73 74 20 3b 20 72   finish_test ; r
0e50: 65 74 75 72 6e 20 7d 0a 72 65 73 65 74 5f 64 62  eturn }.reset_db
0e60: 0a 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ...#------------
0e70: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0e80: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0e90: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0ea0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20  -------------.# 
0eb0: 2f 2a 20 45 56 3a 20 52 2d 30 37 32 38 30 2d 36  /* EV: R-07280-6
0ec0: 30 35 31 30 20 2a 2f 0a 23 0a 23 20 54 65 73 74  0510 */.#.# Test
0ed0: 20 74 68 61 74 20 65 76 65 6e 20 69 66 20 66 6f   that even if fo
0ee0: 72 65 69 67 6e 20 6b 65 79 73 20 61 72 65 20 73  reign keys are s
0ef0: 75 70 70 6f 72 74 65 64 20 62 79 20 74 68 65 20  upported by the 
0f00: 62 75 69 6c 64 2c 20 74 68 65 79 20 6d 75 73 74  build, they must
0f10: 0a 23 20 62 65 20 65 6e 61 62 6c 65 64 20 75 73  .# be enabled us
0f20: 69 6e 67 20 22 50 52 41 47 4d 41 20 66 6f 72 65  ing "PRAGMA fore
0f30: 69 67 6e 5f 6b 65 79 73 20 3d 20 4f 4e 22 20 28  ign_keys = ON" (
0f40: 6f 72 20 73 69 6d 69 6c 61 72 29 2e 0a 23 0a 23  or similar)..#.#
0f50: 20 2f 2a 20 45 56 3a 20 52 2d 35 39 35 37 38 2d   /* EV: R-59578-
0f60: 30 34 39 39 30 20 2a 2f 0a 23 0a 23 20 54 68 69  04990 */.#.# Thi
0f70: 73 20 61 6c 73 6f 20 74 65 73 74 73 20 74 68 61  s also tests tha
0f80: 74 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f  t foreign key co
0f90: 6e 73 74 72 61 69 6e 74 73 20 61 72 65 20 64 69  nstraints are di
0fa0: 73 61 62 6c 65 64 20 62 79 20 64 65 66 61 75 6c  sabled by defaul
0fb0: 74 2e 0a 23 0a 64 72 6f 70 5f 61 6c 6c 5f 74 61  t..#.drop_all_ta
0fc0: 62 6c 65 73 0a 64 6f 5f 74 65 73 74 20 65 5f 66  bles.do_test e_f
0fd0: 6b 65 79 2d 34 2e 31 20 7b 0a 20 20 65 78 65 63  key-4.1 {.  exec
0fe0: 73 71 6c 20 7b 0a 20 20 20 20 43 52 45 41 54 45  sql {.    CREATE
0ff0: 20 54 41 42 4c 45 20 70 28 69 20 50 52 49 4d 41   TABLE p(i PRIMA
1000: 52 59 20 4b 45 59 29 3b 0a 20 20 20 20 43 52 45  RY KEY);.    CRE
1010: 41 54 45 20 54 41 42 4c 45 20 63 28 6a 20 52 45  ATE TABLE c(j RE
1020: 46 45 52 45 4e 43 45 53 20 70 20 4f 4e 20 55 50  FERENCES p ON UP
1030: 44 41 54 45 20 43 41 53 43 41 44 45 29 3b 0a 20  DATE CASCADE);. 
1040: 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 70     INSERT INTO p
1050: 20 56 41 4c 55 45 53 28 27 68 65 6c 6c 6f 27 29   VALUES('hello')
1060: 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54  ;.    INSERT INT
1070: 4f 20 63 20 56 41 4c 55 45 53 28 27 68 65 6c 6c  O c VALUES('hell
1080: 6f 27 29 3b 0a 20 20 20 20 55 50 44 41 54 45 20  o');.    UPDATE 
1090: 70 20 53 45 54 20 69 20 3d 20 27 77 6f 72 6c 64  p SET i = 'world
10a0: 27 3b 0a 20 20 20 20 53 45 4c 45 43 54 20 2a 20  ';.    SELECT * 
10b0: 46 52 4f 4d 20 63 3b 0a 20 20 7d 20 0a 7d 20 7b  FROM c;.  } .} {
10c0: 68 65 6c 6c 6f 7d 0a 64 6f 5f 74 65 73 74 20 65  hello}.do_test e
10d0: 5f 66 6b 65 79 2d 34 2e 32 20 7b 0a 20 20 65 78  _fkey-4.2 {.  ex
10e0: 65 63 73 71 6c 20 7b 0a 20 20 20 20 44 45 4c 45  ecsql {.    DELE
10f0: 54 45 20 46 52 4f 4d 20 63 3b 0a 20 20 20 20 44  TE FROM c;.    D
1100: 45 4c 45 54 45 20 46 52 4f 4d 20 70 3b 0a 20 20  ELETE FROM p;.  
1110: 20 20 50 52 41 47 4d 41 20 66 6f 72 65 69 67 6e    PRAGMA foreign
1120: 5f 6b 65 79 73 20 3d 20 4f 4e 3b 0a 20 20 20 20  _keys = ON;.    
1130: 49 4e 53 45 52 54 20 49 4e 54 4f 20 70 20 56 41  INSERT INTO p VA
1140: 4c 55 45 53 28 27 68 65 6c 6c 6f 27 29 3b 0a 20  LUES('hello');. 
1150: 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 63     INSERT INTO c
1160: 20 56 41 4c 55 45 53 28 27 68 65 6c 6c 6f 27 29   VALUES('hello')
1170: 3b 0a 20 20 20 20 55 50 44 41 54 45 20 70 20 53  ;.    UPDATE p S
1180: 45 54 20 69 20 3d 20 27 77 6f 72 6c 64 27 3b 0a  ET i = 'world';.
1190: 20 20 20 20 53 45 4c 45 43 54 20 2a 20 46 52 4f      SELECT * FRO
11a0: 4d 20 63 3b 0a 20 20 7d 20 0a 7d 20 7b 77 6f 72  M c;.  } .} {wor
11b0: 6c 64 7d 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ld}..#----------
11c0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
11d0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
11e0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
11f0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a  ---------------.
1200: 23 20 2f 2a 20 45 56 3a 20 52 2d 31 35 32 37 38  # /* EV: R-15278
1210: 2d 35 34 34 35 36 20 2a 2f 0a 23 20 2f 2a 20 45  -54456 */.# /* E
1220: 56 3a 20 52 2d 31 31 32 35 35 2d 31 39 39 30 37  V: R-11255-19907
1230: 20 2a 2f 0a 23 0a 23 20 54 65 73 74 20 74 68 61   */.#.# Test tha
1240: 74 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f  t the applicatio
1250: 6e 20 63 61 6e 20 75 73 65 20 22 50 52 41 47 4d  n can use "PRAGM
1260: 41 20 66 6f 72 65 69 67 6e 5f 6b 65 79 73 22 20  A foreign_keys" 
1270: 74 6f 20 71 75 65 72 79 20 66 6f 72 0a 23 20 77  to query for.# w
1280: 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 66 6f  hether or not fo
1290: 72 65 69 67 6e 20 6b 65 79 73 20 61 72 65 20 63  reign keys are c
12a0: 75 72 72 65 6e 74 6c 79 20 65 6e 61 62 6c 65 64  urrently enabled
12b0: 2e 20 54 68 69 73 20 61 6c 73 6f 20 74 65 73 74  . This also test
12c0: 73 0a 23 20 74 68 65 20 65 78 61 6d 70 6c 65 20  s.# the example 
12d0: 63 6f 64 65 20 69 6e 20 73 65 63 74 69 6f 6e 20  code in section 
12e0: 32 20 6f 66 20 66 6f 72 65 69 67 6e 6b 65 79 73  2 of foreignkeys
12f0: 2e 69 6e 2e 0a 23 0a 72 65 73 65 74 5f 64 62 0a  .in..#.reset_db.
1300: 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 35  do_test e_fkey-5
1310: 2e 31 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b  .1 {.  execsql {
1320: 20 50 52 41 47 4d 41 20 66 6f 72 65 69 67 6e 5f   PRAGMA foreign_
1330: 6b 65 79 73 20 7d 0a 7d 20 7b 30 7d 0a 64 6f 5f  keys }.} {0}.do_
1340: 74 65 73 74 20 65 5f 66 6b 65 79 2d 35 2e 32 20  test e_fkey-5.2 
1350: 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 20 0a 20  {.  execsql { . 
1360: 20 20 20 50 52 41 47 4d 41 20 66 6f 72 65 69 67     PRAGMA foreig
1370: 6e 5f 6b 65 79 73 20 3d 20 4f 4e 3b 0a 20 20 20  n_keys = ON;.   
1380: 20 50 52 41 47 4d 41 20 66 6f 72 65 69 67 6e 5f   PRAGMA foreign_
1390: 6b 65 79 73 3b 0a 20 20 7d 0a 7d 20 7b 31 7d 0a  keys;.  }.} {1}.
13a0: 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 35  do_test e_fkey-5
13b0: 2e 33 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b  .3 {.  execsql {
13c0: 20 0a 20 20 20 20 50 52 41 47 4d 41 20 66 6f 72   .    PRAGMA for
13d0: 65 69 67 6e 5f 6b 65 79 73 20 3d 20 4f 46 46 3b  eign_keys = OFF;
13e0: 0a 20 20 20 20 50 52 41 47 4d 41 20 66 6f 72 65  .    PRAGMA fore
13f0: 69 67 6e 5f 6b 65 79 73 3b 0a 20 20 7d 0a 7d 20  ign_keys;.  }.} 
1400: 7b 30 7d 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  {0}..#----------
1410: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1420: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1430: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1440: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a  ---------------.
1450: 23 20 2f 2a 20 45 56 3a 20 52 2d 34 36 36 34 39  # /* EV: R-46649
1460: 2d 35 38 35 33 37 20 2a 2f 0a 23 0a 23 20 54 65  -58537 */.#.# Te
1470: 73 74 20 74 68 61 74 20 69 74 20 69 73 20 6e 6f  st that it is no
1480: 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 65 6e  t possible to en
1490: 61 62 6c 65 20 6f 72 20 64 69 73 61 62 6c 65 20  able or disable 
14a0: 66 6f 72 65 69 67 6e 20 6b 65 79 20 73 75 70 70  foreign key supp
14b0: 6f 72 74 0a 23 20 77 68 69 6c 65 20 6e 6f 74 20  ort.# while not 
14c0: 69 6e 20 61 75 74 6f 2d 63 6f 6d 6d 69 74 20 6d  in auto-commit m
14d0: 6f 64 65 2e 0a 23 0a 72 65 73 65 74 5f 64 62 0a  ode..#.reset_db.
14e0: 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 36  do_test e_fkey-6
14f0: 2e 31 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b  .1 {.  execsql {
1500: 0a 20 20 20 20 50 52 41 47 4d 41 20 66 6f 72 65  .    PRAGMA fore
1510: 69 67 6e 5f 6b 65 79 73 20 3d 20 4f 4e 3b 0a 20  ign_keys = ON;. 
1520: 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20     CREATE TABLE 
1530: 74 31 28 61 20 55 4e 49 51 55 45 2c 20 62 29 3b  t1(a UNIQUE, b);
1540: 0a 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c  .    CREATE TABL
1550: 45 20 74 32 28 63 2c 20 64 20 52 45 46 45 52 45  E t2(c, d REFERE
1560: 4e 43 45 53 20 74 31 28 61 29 29 3b 0a 20 20 20  NCES t1(a));.   
1570: 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 31 20   INSERT INTO t1 
1580: 56 41 4c 55 45 53 28 31 2c 20 32 29 3b 0a 20 20  VALUES(1, 2);.  
1590: 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 32    INSERT INTO t2
15a0: 20 56 41 4c 55 45 53 28 32 2c 20 31 29 3b 0a 20   VALUES(2, 1);. 
15b0: 20 20 20 42 45 47 49 4e 3b 0a 20 20 20 20 20 20     BEGIN;.      
15c0: 50 52 41 47 4d 41 20 66 6f 72 65 69 67 6e 5f 6b  PRAGMA foreign_k
15d0: 65 79 73 20 3d 20 4f 46 46 3b 0a 20 20 7d 0a 20  eys = OFF;.  }. 
15e0: 20 63 61 74 63 68 73 71 6c 20 7b 0a 20 20 20 20   catchsql {.    
15f0: 20 20 44 45 4c 45 54 45 20 46 52 4f 4d 20 74 31    DELETE FROM t1
1600: 0a 20 20 7d 0a 7d 20 7b 31 20 7b 66 6f 72 65 69  .  }.} {1 {forei
1610: 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e  gn key constrain
1620: 74 20 66 61 69 6c 65 64 7d 7d 0a 64 6f 5f 74 65  t failed}}.do_te
1630: 73 74 20 65 5f 66 6b 65 79 2d 36 2e 32 20 7b 0a  st e_fkey-6.2 {.
1640: 20 20 65 78 65 63 73 71 6c 20 7b 20 50 52 41 47    execsql { PRAG
1650: 4d 41 20 66 6f 72 65 69 67 6e 5f 6b 65 79 73 20  MA foreign_keys 
1660: 7d 0a 7d 20 7b 31 7d 0a 64 6f 5f 74 65 73 74 20  }.} {1}.do_test 
1670: 65 5f 66 6b 65 79 2d 36 2e 33 20 7b 0a 20 20 65  e_fkey-6.3 {.  e
1680: 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 43 4f 4d  xecsql {.    COM
1690: 4d 49 54 3b 0a 20 20 20 20 50 52 41 47 4d 41 20  MIT;.    PRAGMA 
16a0: 66 6f 72 65 69 67 6e 5f 6b 65 79 73 20 3d 20 4f  foreign_keys = O
16b0: 46 46 3b 0a 20 20 20 20 42 45 47 49 4e 3b 0a 20  FF;.    BEGIN;. 
16c0: 20 20 20 20 20 50 52 41 47 4d 41 20 66 6f 72 65       PRAGMA fore
16d0: 69 67 6e 5f 6b 65 79 73 20 3d 20 4f 4e 3b 0a 20  ign_keys = ON;. 
16e0: 20 20 20 20 20 44 45 4c 45 54 45 20 46 52 4f 4d       DELETE FROM
16f0: 20 74 31 3b 0a 20 20 20 20 20 20 50 52 41 47 4d   t1;.      PRAGM
1700: 41 20 66 6f 72 65 69 67 6e 5f 6b 65 79 73 3b 0a  A foreign_keys;.
1710: 20 20 7d 0a 7d 20 7b 30 7d 0a 64 6f 5f 74 65 73    }.} {0}.do_tes
1720: 74 20 65 5f 66 6b 65 79 2d 36 2e 34 20 7b 0a 20  t e_fkey-6.4 {. 
1730: 20 65 78 65 63 73 71 6c 20 43 4f 4d 4d 49 54 0a   execsql COMMIT.
1740: 7d 20 7b 7d 0a 0a 23 23 23 23 23 23 23 23 23 23  } {}..##########
1750: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
1760: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
1770: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
1780: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
1790: 23 0a 23 23 23 20 53 45 43 54 49 4f 4e 20 31 3a  #.### SECTION 1:
17a0: 20 49 6e 74 72 6f 64 75 63 74 69 6f 6e 20 74 6f   Introduction to
17b0: 20 46 6f 72 65 69 67 6e 20 4b 65 79 20 43 6f 6e   Foreign Key Con
17c0: 73 74 72 61 69 6e 74 73 0a 23 23 23 23 23 23 23  straints.#######
17d0: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
17e0: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
17f0: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
1800: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
1810: 23 23 23 23 0a 65 78 65 63 73 71 6c 20 22 50 52  ####.execsql "PR
1820: 41 47 4d 41 20 66 6f 72 65 69 67 6e 5f 6b 65 79  AGMA foreign_key
1830: 73 20 3d 20 4f 4e 22 0a 0a 23 2d 2d 2d 2d 2d 2d  s = ON"..#------
1840: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1850: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1860: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1870: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1880: 2d 2d 2d 0a 23 20 2f 2a 20 45 56 3a 20 52 2d 30  ---.# /* EV: R-0
1890: 34 30 34 32 2d 32 34 38 32 35 20 2a 2f 0a 23 0a  4042-24825 */.#.
18a0: 23 20 56 65 72 69 66 79 20 74 68 61 74 20 74 68  # Verify that th
18b0: 65 20 73 79 6e 74 61 78 20 69 6e 20 74 68 65 20  e syntax in the 
18c0: 66 69 72 73 74 20 65 78 61 6d 70 6c 65 20 69 6e  first example in
18d0: 20 73 65 63 74 69 6f 6e 20 31 20 69 73 20 76 61   section 1 is va
18e0: 6c 69 64 2e 0a 23 0a 64 6f 5f 74 65 73 74 20 65  lid..#.do_test e
18f0: 5f 66 6b 65 79 2d 37 2e 31 20 7b 0a 20 20 65 78  _fkey-7.1 {.  ex
1900: 65 63 73 71 6c 20 7b 0a 20 20 20 20 43 52 45 41  ecsql {.    CREA
1910: 54 45 20 54 41 42 4c 45 20 61 72 74 69 73 74 28  TE TABLE artist(
1920: 0a 20 20 20 20 20 20 61 72 74 69 73 74 69 64 20  .      artistid 
1930: 20 20 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41     INTEGER PRIMA
1940: 52 59 20 4b 45 59 2c 20 0a 20 20 20 20 20 20 61  RY KEY, .      a
1950: 72 74 69 73 74 6e 61 6d 65 20 20 54 45 58 54 0a  rtistname  TEXT.
1960: 20 20 20 20 29 3b 0a 20 20 20 20 43 52 45 41 54      );.    CREAT
1970: 45 20 54 41 42 4c 45 20 74 72 61 63 6b 28 0a 20  E TABLE track(. 
1980: 20 20 20 20 20 74 72 61 63 6b 69 64 20 20 20 20       trackid    
1990: 20 49 4e 54 45 47 45 52 2c 20 0a 20 20 20 20 20   INTEGER, .     
19a0: 20 74 72 61 63 6b 6e 61 6d 65 20 20 20 54 45 58   trackname   TEX
19b0: 54 2c 20 0a 20 20 20 20 20 20 74 72 61 63 6b 61  T, .      tracka
19c0: 72 74 69 73 74 20 49 4e 54 45 47 45 52 2c 0a 20  rtist INTEGER,. 
19d0: 20 20 20 20 20 46 4f 52 45 49 47 4e 20 4b 45 59       FOREIGN KEY
19e0: 28 74 72 61 63 6b 61 72 74 69 73 74 29 20 52 45  (trackartist) RE
19f0: 46 45 52 45 4e 43 45 53 20 61 72 74 69 73 74 28  FERENCES artist(
1a00: 61 72 74 69 73 74 69 64 29 0a 20 20 20 20 29 3b  artistid).    );
1a10: 0a 20 20 7d 0a 7d 20 7b 7d 0a 0a 23 2d 2d 2d 2d  .  }.} {}..#----
1a20: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1a30: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1a40: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1a50: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1a60: 2d 2d 2d 2d 2d 0a 23 20 2f 2a 20 45 56 3a 20 52  -----.# /* EV: R
1a70: 2d 36 31 33 36 32 2d 33 32 30 38 37 20 2a 2f 0a  -61362-32087 */.
1a80: 23 0a 23 20 41 74 74 65 6d 70 74 69 6e 67 20 74  #.# Attempting t
1a90: 6f 20 69 6e 73 65 72 74 20 61 20 72 6f 77 20 69  o insert a row i
1aa0: 6e 74 6f 20 74 68 65 20 27 74 72 61 63 6b 27 20  nto the 'track' 
1ab0: 74 61 62 6c 65 20 74 68 61 74 20 63 6f 72 72 65  table that corre
1ac0: 73 70 6f 6e 64 73 0a 23 20 74 6f 20 6e 6f 20 72  sponds.# to no r
1ad0: 6f 77 20 69 6e 20 74 68 65 20 27 61 72 74 69 73  ow in the 'artis
1ae0: 74 27 20 74 61 62 6c 65 20 66 61 69 6c 73 2e 0a  t' table fails..
1af0: 23 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79  #.do_test e_fkey
1b00: 2d 38 2e 31 20 7b 0a 20 20 63 61 74 63 68 73 71  -8.1 {.  catchsq
1b10: 6c 20 7b 20 49 4e 53 45 52 54 20 49 4e 54 4f 20  l { INSERT INTO 
1b20: 74 72 61 63 6b 20 56 41 4c 55 45 53 28 31 2c 20  track VALUES(1, 
1b30: 27 74 72 61 63 6b 20 31 27 2c 20 31 29 20 7d 0a  'track 1', 1) }.
1b40: 7d 20 7b 31 20 7b 66 6f 72 65 69 67 6e 20 6b 65  } {1 {foreign ke
1b50: 79 20 63 6f 6e 73 74 72 61 69 6e 74 20 66 61 69  y constraint fai
1b60: 6c 65 64 7d 7d 0a 64 6f 5f 74 65 73 74 20 65 5f  led}}.do_test e_
1b70: 66 6b 65 79 2d 38 2e 32 20 7b 0a 20 20 65 78 65  fkey-8.2 {.  exe
1b80: 63 73 71 6c 20 7b 20 49 4e 53 45 52 54 20 49 4e  csql { INSERT IN
1b90: 54 4f 20 61 72 74 69 73 74 20 56 41 4c 55 45 53  TO artist VALUES
1ba0: 28 32 2c 20 27 61 72 74 69 73 74 20 31 27 29 20  (2, 'artist 1') 
1bb0: 7d 0a 20 20 63 61 74 63 68 73 71 6c 20 7b 20 49  }.  catchsql { I
1bc0: 4e 53 45 52 54 20 49 4e 54 4f 20 74 72 61 63 6b  NSERT INTO track
1bd0: 20 56 41 4c 55 45 53 28 31 2c 20 27 74 72 61 63   VALUES(1, 'trac
1be0: 6b 20 31 27 2c 20 31 29 20 7d 0a 7d 20 7b 31 20  k 1', 1) }.} {1 
1bf0: 7b 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e  {foreign key con
1c00: 73 74 72 61 69 6e 74 20 66 61 69 6c 65 64 7d 7d  straint failed}}
1c10: 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d  .do_test e_fkey-
1c20: 38 2e 32 20 7b 0a 20 20 65 78 65 63 73 71 6c 20  8.2 {.  execsql 
1c30: 7b 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 72  { INSERT INTO tr
1c40: 61 63 6b 20 56 41 4c 55 45 53 28 31 2c 20 27 74  ack VALUES(1, 't
1c50: 72 61 63 6b 20 31 27 2c 20 32 29 20 7d 0a 7d 20  rack 1', 2) }.} 
1c60: 7b 7d 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  {}..#-----------
1c70: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1c80: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1c90: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1ca0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23  --------------.#
1cb0: 20 2f 2a 20 45 56 3a 20 52 2d 32 34 34 30 31 2d   /* EV: R-24401-
1cc0: 35 32 34 30 30 20 2a 2f 0a 23 0a 23 20 41 74 74  52400 */.#.# Att
1cd0: 65 6d 70 74 69 6e 67 20 74 6f 20 64 65 6c 65 74  empting to delet
1ce0: 65 20 61 20 72 6f 77 20 66 72 6f 6d 20 74 68 65  e a row from the
1cf0: 20 27 61 72 74 69 73 74 27 20 74 61 62 6c 65 20   'artist' table 
1d00: 77 68 69 6c 65 20 74 68 65 72 65 20 61 72 65 20  while there are 
1d10: 0a 23 20 64 65 70 65 6e 64 65 6e 74 20 72 6f 77  .# dependent row
1d20: 73 20 69 6e 20 74 68 65 20 74 72 61 63 6b 20 74  s in the track t
1d30: 61 62 6c 65 20 61 6c 73 6f 20 66 61 69 6c 73 2e  able also fails.
1d40: 0a 23 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65  .#.do_test e_fke
1d50: 79 2d 39 2e 31 20 7b 0a 20 20 63 61 74 63 68 73  y-9.1 {.  catchs
1d60: 71 6c 20 7b 20 44 45 4c 45 54 45 20 46 52 4f 4d  ql { DELETE FROM
1d70: 20 61 72 74 69 73 74 20 57 48 45 52 45 20 61 72   artist WHERE ar
1d80: 74 69 73 74 69 64 20 3d 20 32 20 7d 0a 7d 20 7b  tistid = 2 }.} {
1d90: 31 20 7b 66 6f 72 65 69 67 6e 20 6b 65 79 20 63  1 {foreign key c
1da0: 6f 6e 73 74 72 61 69 6e 74 20 66 61 69 6c 65 64  onstraint failed
1db0: 7d 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65  }}.do_test e_fke
1dc0: 79 2d 39 2e 32 20 7b 0a 20 20 65 78 65 63 73 71  y-9.2 {.  execsq
1dd0: 6c 20 7b 20 0a 20 20 20 20 44 45 4c 45 54 45 20  l { .    DELETE 
1de0: 46 52 4f 4d 20 74 72 61 63 6b 20 57 48 45 52 45  FROM track WHERE
1df0: 20 74 72 61 63 6b 61 72 74 69 73 74 20 3d 20 32   trackartist = 2
1e00: 3b 0a 20 20 20 20 44 45 4c 45 54 45 20 46 52 4f  ;.    DELETE FRO
1e10: 4d 20 61 72 74 69 73 74 20 57 48 45 52 45 20 61  M artist WHERE a
1e20: 72 74 69 73 74 69 64 20 3d 20 32 3b 0a 20 20 7d  rtistid = 2;.  }
1e30: 0a 7d 20 7b 7d 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d  .} {}..#--------
1e40: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1e50: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1e60: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1e70: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1e80: 2d 0a 23 20 2f 2a 20 45 56 3a 20 52 2d 32 33 39  -.# /* EV: R-239
1e90: 38 30 2d 34 38 38 35 39 20 2a 2f 0a 23 0a 23 20  80-48859 */.#.# 
1ea0: 49 66 20 74 68 65 20 66 6f 72 65 69 67 6e 20 6b  If the foreign k
1eb0: 65 79 20 63 6f 6c 75 6d 6e 20 28 74 72 61 63 6b  ey column (track
1ec0: 61 72 74 69 73 74 29 20 69 6e 20 74 61 62 6c 65  artist) in table
1ed0: 20 27 74 72 61 63 6b 27 20 69 73 20 73 65 74 20   'track' is set 
1ee0: 74 6f 20 4e 55 4c 4c 2c 0a 23 20 74 68 65 72 65  to NULL,.# there
1ef0: 20 69 73 20 6e 6f 20 72 65 71 75 69 72 65 6d 65   is no requireme
1f00: 6e 74 20 66 6f 72 20 61 20 6d 61 74 63 68 69 6e  nt for a matchin
1f10: 67 20 72 6f 77 20 69 6e 20 74 68 65 20 27 61 72  g row in the 'ar
1f20: 74 69 73 74 27 20 74 61 62 6c 65 2e 0a 23 0a 64  tist' table..#.d
1f30: 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 31 30  o_test e_fkey-10
1f40: 2e 31 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b  .1 {.  execsql {
1f50: 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f  .    INSERT INTO
1f60: 20 74 72 61 63 6b 20 56 41 4c 55 45 53 28 31 2c   track VALUES(1,
1f70: 20 27 74 72 61 63 6b 20 31 27 2c 20 4e 55 4c 4c   'track 1', NULL
1f80: 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e  );.    INSERT IN
1f90: 54 4f 20 74 72 61 63 6b 20 56 41 4c 55 45 53 28  TO track VALUES(
1fa0: 32 2c 20 27 74 72 61 63 6b 20 32 27 2c 20 4e 55  2, 'track 2', NU
1fb0: 4c 4c 29 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a 64 6f  LL);.  }.} {}.do
1fc0: 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 31 30 2e  _test e_fkey-10.
1fd0: 32 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 20  2 {.  execsql { 
1fe0: 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 61 72  SELECT * FROM ar
1ff0: 74 69 73 74 20 7d 0a 7d 20 7b 7d 0a 64 6f 5f 74  tist }.} {}.do_t
2000: 65 73 74 20 65 5f 66 6b 65 79 2d 31 30 2e 33 20  est e_fkey-10.3 
2010: 7b 0a 20 20 23 20 53 65 74 74 69 6e 67 20 74 68  {.  # Setting th
2020: 65 20 74 72 61 63 6b 69 64 20 74 6f 20 61 20 6e  e trackid to a n
2030: 6f 6e 2d 4e 55 4c 4c 20 76 61 6c 75 65 20 66 61  on-NULL value fa
2040: 69 6c 73 2c 20 6f 66 20 63 6f 75 72 73 65 2e 0a  ils, of course..
2050: 20 20 63 61 74 63 68 73 71 6c 20 7b 20 55 50 44    catchsql { UPD
2060: 41 54 45 20 74 72 61 63 6b 20 53 45 54 20 74 72  ATE track SET tr
2070: 61 63 6b 61 72 74 69 73 74 20 3d 20 35 20 57 48  ackartist = 5 WH
2080: 45 52 45 20 74 72 61 63 6b 69 64 20 3d 20 31 20  ERE trackid = 1 
2090: 7d 0a 7d 20 7b 31 20 7b 66 6f 72 65 69 67 6e 20  }.} {1 {foreign 
20a0: 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 20 66  key constraint f
20b0: 61 69 6c 65 64 7d 7d 0a 64 6f 5f 74 65 73 74 20  ailed}}.do_test 
20c0: 65 5f 66 6b 65 79 2d 31 30 2e 34 20 7b 0a 20 20  e_fkey-10.4 {.  
20d0: 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 49 4e  execsql {.    IN
20e0: 53 45 52 54 20 49 4e 54 4f 20 61 72 74 69 73 74  SERT INTO artist
20f0: 20 56 41 4c 55 45 53 28 35 2c 20 27 61 72 74 69   VALUES(5, 'arti
2100: 73 74 20 35 27 29 3b 0a 20 20 20 20 55 50 44 41  st 5');.    UPDA
2110: 54 45 20 74 72 61 63 6b 20 53 45 54 20 74 72 61  TE track SET tra
2120: 63 6b 61 72 74 69 73 74 20 3d 20 35 20 57 48 45  ckartist = 5 WHE
2130: 52 45 20 74 72 61 63 6b 69 64 20 3d 20 31 3b 0a  RE trackid = 1;.
2140: 20 20 7d 0a 20 20 63 61 74 63 68 73 71 6c 20 7b    }.  catchsql {
2150: 20 44 45 4c 45 54 45 20 46 52 4f 4d 20 61 72 74   DELETE FROM art
2160: 69 73 74 20 57 48 45 52 45 20 61 72 74 69 73 74  ist WHERE artist
2170: 69 64 20 3d 20 35 7d 0a 7d 20 7b 31 20 7b 66 6f  id = 5}.} {1 {fo
2180: 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72  reign key constr
2190: 61 69 6e 74 20 66 61 69 6c 65 64 7d 7d 0a 64 6f  aint failed}}.do
21a0: 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 31 30 2e  _test e_fkey-10.
21b0: 35 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 20  5 {.  execsql { 
21c0: 0a 20 20 20 20 55 50 44 41 54 45 20 74 72 61 63  .    UPDATE trac
21d0: 6b 20 53 45 54 20 74 72 61 63 6b 61 72 74 69 73  k SET trackartis
21e0: 74 20 3d 20 4e 55 4c 4c 20 57 48 45 52 45 20 74  t = NULL WHERE t
21f0: 72 61 63 6b 69 64 20 3d 20 31 3b 0a 20 20 20 20  rackid = 1;.    
2200: 44 45 4c 45 54 45 20 46 52 4f 4d 20 61 72 74 69  DELETE FROM arti
2210: 73 74 20 57 48 45 52 45 20 61 72 74 69 73 74 69  st WHERE artisti
2220: 64 20 3d 20 35 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a  d = 5;.  }.} {}.
2230: 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  .#--------------
2240: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2250: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2260: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2270: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20 2f 2a  -----------.# /*
2280: 20 45 56 3a 20 52 2d 35 32 34 38 36 2d 32 31 33   EV: R-52486-213
2290: 35 32 20 2a 2f 0a 23 0a 23 20 54 65 73 74 20 74  52 */.#.# Test t
22a0: 68 61 74 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e  hat the followin
22b0: 67 20 69 73 20 74 72 75 65 20 66 6f 20 61 6c 6c  g is true fo all
22c0: 20 72 6f 77 73 20 69 6e 20 74 68 65 20 74 72 61   rows in the tra
22d0: 63 6b 20 74 61 62 6c 65 3a 0a 23 0a 23 20 20 20  ck table:.#.#   
22e0: 74 72 61 63 6b 61 72 74 69 73 74 20 49 53 20 4e  trackartist IS N
22f0: 55 4c 4c 20 4f 52 20 0a 23 20 20 20 45 58 49 53  ULL OR .#   EXIS
2300: 54 53 28 53 45 4c 45 43 54 20 31 20 46 52 4f 4d  TS(SELECT 1 FROM
2310: 20 61 72 74 69 73 74 20 57 48 45 52 45 20 61 72   artist WHERE ar
2320: 74 69 73 74 69 64 3d 74 72 61 63 6b 61 72 74 69  tistid=trackarti
2330: 73 74 29 0a 23 0a 0a 23 20 54 68 69 73 20 70 72  st).#..# This pr
2340: 6f 63 65 64 75 72 65 20 65 78 65 63 75 74 65 73  ocedure executes
2350: 20 61 20 74 65 73 74 20 63 61 73 65 20 74 6f 20   a test case to 
2360: 63 68 65 63 6b 20 74 68 61 74 20 73 74 61 74 65  check that state
2370: 6d 65 6e 74 20 0a 23 20 52 2d 35 32 34 38 36 2d  ment .# R-52486-
2380: 32 31 33 35 32 20 69 73 20 74 72 75 65 20 61 66  21352 is true af
2390: 74 65 72 20 65 78 65 63 75 74 69 6e 67 20 74 68  ter executing th
23a0: 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20  e SQL statement 
23b0: 70 61 73 73 65 64 2e 0a 23 20 61 73 20 74 68 65  passed..# as the
23c0: 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74   second argument
23d0: 2e 0a 70 72 6f 63 20 74 65 73 74 5f 72 35 32 34  ..proc test_r524
23e0: 38 36 5f 32 31 33 35 32 20 7b 74 6e 20 73 71 6c  86_21352 {tn sql
23f0: 7d 20 7b 0a 20 20 73 65 74 20 72 65 73 20 5b 63  } {.  set res [c
2400: 61 74 63 68 73 71 6c 20 24 73 71 6c 5d 0a 20 20  atchsql $sql].  
2410: 73 65 74 20 72 65 73 75 6c 74 73 20 7b 0a 20 20  set results {.  
2420: 20 20 7b 30 20 7b 7d 7d 20 0a 20 20 20 20 7b 31    {0 {}} .    {1
2430: 20 7b 50 52 49 4d 41 52 59 20 4b 45 59 20 6d 75   {PRIMARY KEY mu
2440: 73 74 20 62 65 20 75 6e 69 71 75 65 7d 7d 20 0a  st be unique}} .
2450: 20 20 20 20 7b 31 20 7b 66 6f 72 65 69 67 6e 20      {1 {foreign 
2460: 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 20 66  key constraint f
2470: 61 69 6c 65 64 7d 7d 0a 20 20 7d 0a 20 20 69 66  ailed}}.  }.  if
2480: 20 7b 5b 6c 73 65 61 72 63 68 20 24 72 65 73 75   {[lsearch $resu
2490: 6c 74 73 20 24 72 65 73 5d 3c 30 7d 20 7b 0a 20  lts $res]<0} {. 
24a0: 20 20 20 65 72 72 6f 72 20 24 72 65 73 0a 20 20     error $res.  
24b0: 7d 0a 0a 20 20 64 6f 5f 74 65 73 74 20 65 5f 66  }..  do_test e_f
24c0: 6b 65 79 2d 31 31 2e 24 74 6e 20 7b 0a 20 20 20  key-11.$tn {.   
24d0: 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 20   execsql {.     
24e0: 20 53 45 4c 45 43 54 20 63 6f 75 6e 74 28 2a 29   SELECT count(*)
24f0: 20 46 52 4f 4d 20 74 72 61 63 6b 20 57 48 45 52   FROM track WHER
2500: 45 20 4e 4f 54 20 28 0a 20 20 20 20 20 20 20 20  E NOT (.        
2510: 74 72 61 63 6b 61 72 74 69 73 74 20 49 53 20 4e  trackartist IS N
2520: 55 4c 4c 20 4f 52 20 0a 20 20 20 20 20 20 20 20  ULL OR .        
2530: 45 58 49 53 54 53 28 53 45 4c 45 43 54 20 31 20  EXISTS(SELECT 1 
2540: 46 52 4f 4d 20 61 72 74 69 73 74 20 57 48 45 52  FROM artist WHER
2550: 45 20 61 72 74 69 73 74 69 64 3d 74 72 61 63 6b  E artistid=track
2560: 61 72 74 69 73 74 29 0a 20 20 20 20 20 20 29 0a  artist).      ).
2570: 20 20 20 20 7d 0a 20 20 7d 20 7b 30 7d 0a 7d 0a      }.  } {0}.}.
2580: 0a 23 20 45 78 65 63 75 74 65 20 61 20 73 65 72  .# Execute a ser
2590: 69 65 73 20 6f 66 20 72 61 6e 64 6f 6d 20 49 4e  ies of random IN
25a0: 53 45 52 54 2c 20 55 50 44 41 54 45 20 61 6e 64  SERT, UPDATE and
25b0: 20 44 45 4c 45 54 45 20 6f 70 65 72 61 74 69 6f   DELETE operatio
25c0: 6e 73 0a 23 20 28 73 6f 6d 65 20 6f 66 20 77 68  ns.# (some of wh
25d0: 69 63 68 20 6d 61 79 20 66 61 69 6c 20 64 75 65  ich may fail due
25e0: 20 74 6f 20 46 4b 20 6f 72 20 50 4b 20 63 6f 6e   to FK or PK con
25f0: 73 74 72 61 69 6e 74 20 76 69 6f 6c 61 74 69 6f  straint violatio
2600: 6e 73 29 20 6f 6e 20 0a 23 20 74 68 65 20 74 77  ns) on .# the tw
2610: 6f 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20  o tables in the 
2620: 65 78 61 6d 70 6c 65 20 73 63 68 65 6d 61 2e 20  example schema. 
2630: 54 65 73 74 20 74 68 61 74 20 52 2d 35 32 34 38  Test that R-5248
2640: 36 2d 32 31 33 35 32 0a 23 20 69 73 20 74 72 75  6-21352.# is tru
2650: 65 20 61 66 74 65 72 20 65 78 65 63 75 74 69 6e  e after executin
2660: 67 20 65 61 63 68 20 6f 70 65 72 61 74 69 6f 6e  g each operation
2670: 2e 0a 23 0a 73 65 74 20 54 65 6d 70 6c 61 74 65  ..#.set Template
2680: 20 7b 0a 20 20 7b 49 4e 53 45 52 54 20 49 4e 54   {.  {INSERT INT
2690: 4f 20 74 72 61 63 6b 20 56 41 4c 55 45 53 28 24  O track VALUES($
26a0: 74 2c 20 27 74 72 61 63 6b 20 24 74 27 2c 20 24  t, 'track $t', $
26b0: 61 29 7d 0a 20 20 7b 44 45 4c 45 54 45 20 46 52  a)}.  {DELETE FR
26c0: 4f 4d 20 74 72 61 63 6b 20 57 48 45 52 45 20 74  OM track WHERE t
26d0: 72 61 63 6b 69 64 20 3d 20 24 74 7d 0a 20 20 7b  rackid = $t}.  {
26e0: 55 50 44 41 54 45 20 74 72 61 63 6b 20 53 45 54  UPDATE track SET
26f0: 20 74 72 61 63 6b 61 72 74 69 73 74 20 3d 20 24   trackartist = $
2700: 61 20 57 48 45 52 45 20 74 72 61 63 6b 69 64 20  a WHERE trackid 
2710: 3d 20 24 74 7d 0a 20 20 7b 49 4e 53 45 52 54 20  = $t}.  {INSERT 
2720: 49 4e 54 4f 20 61 72 74 69 73 74 20 56 41 4c 55  INTO artist VALU
2730: 45 53 28 24 61 2c 20 27 61 72 74 69 73 74 20 24  ES($a, 'artist $
2740: 61 27 29 7d 0a 20 20 7b 44 45 4c 45 54 45 20 46  a')}.  {DELETE F
2750: 52 4f 4d 20 61 72 74 69 73 74 20 57 48 45 52 45  ROM artist WHERE
2760: 20 61 72 74 69 73 74 69 64 20 3d 20 24 61 7d 0a   artistid = $a}.
2770: 20 20 7b 55 50 44 41 54 45 20 61 72 74 69 73 74    {UPDATE artist
2780: 20 53 45 54 20 61 72 74 69 73 74 69 64 20 3d 20   SET artistid = 
2790: 24 61 32 20 57 48 45 52 45 20 61 72 74 69 73 74  $a2 WHERE artist
27a0: 69 64 20 3d 20 24 61 7d 0a 7d 0a 66 6f 72 20 7b  id = $a}.}.for {
27b0: 73 65 74 20 69 20 30 7d 20 7b 24 69 20 3c 20 35  set i 0} {$i < 5
27c0: 30 30 7d 20 7b 69 6e 63 72 20 69 7d 20 7b 0a 20  00} {incr i} {. 
27d0: 20 73 65 74 20 61 20 20 20 5b 65 78 70 72 20 69   set a   [expr i
27e0: 6e 74 28 72 61 6e 64 28 29 2a 31 30 29 5d 0a 20  nt(rand()*10)]. 
27f0: 20 73 65 74 20 61 32 20 20 5b 65 78 70 72 20 69   set a2  [expr i
2800: 6e 74 28 72 61 6e 64 28 29 2a 31 30 29 5d 0a 20  nt(rand()*10)]. 
2810: 20 73 65 74 20 74 20 20 20 5b 65 78 70 72 20 69   set t   [expr i
2820: 6e 74 28 72 61 6e 64 28 29 2a 35 30 29 5d 0a 20  nt(rand()*50)]. 
2830: 20 73 65 74 20 73 71 6c 20 5b 73 75 62 73 74 20   set sql [subst 
2840: 5b 6c 69 6e 64 65 78 20 24 54 65 6d 70 6c 61 74  [lindex $Templat
2850: 65 20 5b 65 78 70 72 20 69 6e 74 28 72 61 6e 64  e [expr int(rand
2860: 28 29 2a 36 29 5d 5d 5d 0a 0a 20 20 74 65 73 74  ()*6)]]]..  test
2870: 5f 72 35 32 34 38 36 5f 32 31 33 35 32 20 24 69  _r52486_21352 $i
2880: 20 24 73 71 6c 0a 7d 0a 0a 23 2d 2d 2d 2d 2d 2d   $sql.}..#------
2890: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
28a0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
28b0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
28c0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
28d0: 2d 2d 2d 0a 23 20 2f 2a 20 45 56 3a 20 52 2d 34  ---.# /* EV: R-4
28e0: 32 34 31 32 2d 35 39 33 32 31 20 2a 2f 0a 23 0a  2412-59321 */.#.
28f0: 23 20 43 68 65 63 6b 20 74 68 61 74 20 61 20 4e  # Check that a N
2900: 4f 54 20 4e 55 4c 4c 20 63 6f 6e 73 74 72 61 69  OT NULL constrai
2910: 6e 74 20 63 61 6e 20 62 65 20 61 64 64 65 64 20  nt can be added 
2920: 74 6f 20 74 68 65 20 65 78 61 6d 70 6c 65 20 73  to the example s
2930: 63 68 65 6d 61 0a 23 20 74 6f 20 70 72 6f 68 69  chema.# to prohi
2940: 62 69 74 20 4e 55 4c 4c 20 63 68 69 6c 64 20 6b  bit NULL child k
2950: 65 79 73 20 66 72 6f 6d 20 62 65 69 6e 67 20 69  eys from being i
2960: 6e 73 65 72 74 65 64 2e 0a 23 0a 64 72 6f 70 5f  nserted..#.drop_
2970: 61 6c 6c 5f 74 61 62 6c 65 73 0a 64 6f 5f 74 65  all_tables.do_te
2980: 73 74 20 65 5f 66 6b 65 79 2d 31 32 2e 31 20 7b  st e_fkey-12.1 {
2990: 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20  .  execsql {.   
29a0: 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 61 72   CREATE TABLE ar
29b0: 74 69 73 74 28 0a 20 20 20 20 20 20 61 72 74 69  tist(.      arti
29c0: 73 74 69 64 20 20 20 20 49 4e 54 45 47 45 52 20  stid    INTEGER 
29d0: 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 0a 20 20  PRIMARY KEY, .  
29e0: 20 20 20 20 61 72 74 69 73 74 6e 61 6d 65 20 20      artistname  
29f0: 54 45 58 54 0a 20 20 20 20 29 3b 0a 20 20 20 20  TEXT.    );.    
2a00: 43 52 45 41 54 45 20 54 41 42 4c 45 20 74 72 61  CREATE TABLE tra
2a10: 63 6b 28 0a 20 20 20 20 20 20 74 72 61 63 6b 69  ck(.      tracki
2a20: 64 20 20 20 20 20 49 4e 54 45 47 45 52 2c 20 0a  d     INTEGER, .
2a30: 20 20 20 20 20 20 74 72 61 63 6b 6e 61 6d 65 20        trackname 
2a40: 20 20 54 45 58 54 2c 20 0a 20 20 20 20 20 20 74    TEXT, .      t
2a50: 72 61 63 6b 61 72 74 69 73 74 20 49 4e 54 45 47  rackartist INTEG
2a60: 45 52 20 4e 4f 54 20 4e 55 4c 4c 2c 0a 20 20 20  ER NOT NULL,.   
2a70: 20 20 20 46 4f 52 45 49 47 4e 20 4b 45 59 28 74     FOREIGN KEY(t
2a80: 72 61 63 6b 61 72 74 69 73 74 29 20 52 45 46 45  rackartist) REFE
2a90: 52 45 4e 43 45 53 20 61 72 74 69 73 74 28 61 72  RENCES artist(ar
2aa0: 74 69 73 74 69 64 29 0a 20 20 20 20 29 3b 0a 20  tistid).    );. 
2ab0: 20 7d 0a 7d 20 7b 7d 0a 64 6f 5f 74 65 73 74 20   }.} {}.do_test 
2ac0: 65 5f 66 6b 65 79 2d 31 32 2e 32 20 7b 0a 20 20  e_fkey-12.2 {.  
2ad0: 63 61 74 63 68 73 71 6c 20 7b 20 49 4e 53 45 52  catchsql { INSER
2ae0: 54 20 49 4e 54 4f 20 74 72 61 63 6b 20 56 41 4c  T INTO track VAL
2af0: 55 45 53 28 31 34 2c 20 27 4d 72 2e 20 42 6f 6a  UES(14, 'Mr. Boj
2b00: 61 6e 67 6c 65 73 27 2c 20 4e 55 4c 4c 29 20 7d  angles', NULL) }
2b10: 0a 7d 20 7b 31 20 7b 74 72 61 63 6b 2e 74 72 61  .} {1 {track.tra
2b20: 63 6b 61 72 74 69 73 74 20 6d 61 79 20 6e 6f 74  ckartist may not
2b30: 20 62 65 20 4e 55 4c 4c 7d 7d 0a 0a 23 2d 2d 2d   be NULL}}..#---
2b40: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2b50: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
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 0a 23 20 2f 2a 20 45 56 3a 20  ------.# /* EV: 
2b90: 52 2d 31 37 39 30 32 2d 35 39 32 35 30 20 2a 2f  R-17902-59250 */
2ba0: 0a 23 0a 23 20 54 65 73 74 20 61 6e 20 65 78 61  .#.# Test an exa
2bb0: 6d 70 6c 65 20 66 72 6f 6d 20 66 6f 72 65 69 67  mple from foreig
2bc0: 6e 6b 65 79 73 2e 68 74 6d 6c 2e 0a 23 0a 64 72  nkeys.html..#.dr
2bd0: 6f 70 5f 61 6c 6c 5f 74 61 62 6c 65 73 0a 64 6f  op_all_tables.do
2be0: 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 31 33 2e  _test e_fkey-13.
2bf0: 31 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a  1 {.  execsql {.
2c00: 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45      CREATE TABLE
2c10: 20 61 72 74 69 73 74 28 0a 20 20 20 20 20 20 61   artist(.      a
2c20: 72 74 69 73 74 69 64 20 20 20 20 49 4e 54 45 47  rtistid    INTEG
2c30: 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20  ER PRIMARY KEY, 
2c40: 0a 20 20 20 20 20 20 61 72 74 69 73 74 6e 61 6d  .      artistnam
2c50: 65 20 20 54 45 58 54 0a 20 20 20 20 29 3b 0a 20  e  TEXT.    );. 
2c60: 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20     CREATE TABLE 
2c70: 74 72 61 63 6b 28 0a 20 20 20 20 20 20 74 72 61  track(.      tra
2c80: 63 6b 69 64 20 20 20 20 20 49 4e 54 45 47 45 52  ckid     INTEGER
2c90: 2c 20 0a 20 20 20 20 20 20 74 72 61 63 6b 6e 61  , .      trackna
2ca0: 6d 65 20 20 20 54 45 58 54 2c 20 0a 20 20 20 20  me   TEXT, .    
2cb0: 20 20 74 72 61 63 6b 61 72 74 69 73 74 20 49 4e    trackartist IN
2cc0: 54 45 47 45 52 2c 0a 20 20 20 20 20 20 46 4f 52  TEGER,.      FOR
2cd0: 45 49 47 4e 20 4b 45 59 28 74 72 61 63 6b 61 72  EIGN KEY(trackar
2ce0: 74 69 73 74 29 20 52 45 46 45 52 45 4e 43 45 53  tist) REFERENCES
2cf0: 20 61 72 74 69 73 74 28 61 72 74 69 73 74 69 64   artist(artistid
2d00: 29 0a 20 20 20 20 29 3b 0a 20 20 20 20 49 4e 53  ).    );.    INS
2d10: 45 52 54 20 49 4e 54 4f 20 61 72 74 69 73 74 20  ERT INTO artist 
2d20: 56 41 4c 55 45 53 28 31 2c 20 27 44 65 61 6e 20  VALUES(1, 'Dean 
2d30: 4d 61 72 74 69 6e 27 29 3b 0a 20 20 20 20 49 4e  Martin');.    IN
2d40: 53 45 52 54 20 49 4e 54 4f 20 61 72 74 69 73 74  SERT INTO artist
2d50: 20 56 41 4c 55 45 53 28 32 2c 20 27 46 72 61 6e   VALUES(2, 'Fran
2d60: 6b 20 53 69 6e 61 74 72 61 27 29 3b 0a 20 20 20  k Sinatra');.   
2d70: 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 72 61   INSERT INTO tra
2d80: 63 6b 20 56 41 4c 55 45 53 28 31 31 2c 20 27 54  ck VALUES(11, 'T
2d90: 68 61 74 27 27 73 20 41 6d 6f 72 65 27 2c 20 31  hat''s Amore', 1
2da0: 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e  );.    INSERT IN
2db0: 54 4f 20 74 72 61 63 6b 20 56 41 4c 55 45 53 28  TO track VALUES(
2dc0: 31 32 2c 20 27 43 68 72 69 73 74 6d 61 73 20 42  12, 'Christmas B
2dd0: 6c 75 65 73 27 2c 20 31 29 3b 0a 20 20 20 20 49  lues', 1);.    I
2de0: 4e 53 45 52 54 20 49 4e 54 4f 20 74 72 61 63 6b  NSERT INTO track
2df0: 20 56 41 4c 55 45 53 28 31 33 2c 20 27 4d 79 20   VALUES(13, 'My 
2e00: 57 61 79 27 2c 20 32 29 3b 0a 20 20 7d 0a 7d 20  Way', 2);.  }.} 
2e10: 7b 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65  {}.do_test e_fke
2e20: 79 2d 31 33 2e 32 20 7b 0a 20 20 63 61 74 63 68  y-13.2 {.  catch
2e30: 73 71 6c 20 7b 20 49 4e 53 45 52 54 20 49 4e 54  sql { INSERT INT
2e40: 4f 20 74 72 61 63 6b 20 56 41 4c 55 45 53 28 31  O track VALUES(1
2e50: 34 2c 20 27 4d 72 2e 20 42 6f 6a 61 6e 67 6c 65  4, 'Mr. Bojangle
2e60: 73 27 2c 20 33 29 20 7d 0a 7d 20 7b 31 20 7b 66  s', 3) }.} {1 {f
2e70: 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74  oreign key const
2e80: 72 61 69 6e 74 20 66 61 69 6c 65 64 7d 7d 0a 64  raint failed}}.d
2e90: 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 31 33  o_test e_fkey-13
2ea0: 2e 33 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b  .3 {.  execsql {
2eb0: 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 72 61   INSERT INTO tra
2ec0: 63 6b 20 56 41 4c 55 45 53 28 31 34 2c 20 27 4d  ck VALUES(14, 'M
2ed0: 72 2e 20 42 6f 6a 61 6e 67 6c 65 73 27 2c 20 4e  r. Bojangles', N
2ee0: 55 4c 4c 29 20 7d 0a 7d 20 7b 7d 0a 64 6f 5f 74  ULL) }.} {}.do_t
2ef0: 65 73 74 20 65 5f 66 6b 65 79 2d 31 33 2e 34 20  est e_fkey-13.4 
2f00: 7b 0a 20 20 63 61 74 63 68 73 71 6c 20 7b 20 0a  {.  catchsql { .
2f10: 20 20 20 20 55 50 44 41 54 45 20 74 72 61 63 6b      UPDATE track
2f20: 20 53 45 54 20 74 72 61 63 6b 61 72 74 69 73 74   SET trackartist
2f30: 20 3d 20 33 20 57 48 45 52 45 20 74 72 61 63 6b   = 3 WHERE track
2f40: 6e 61 6d 65 20 3d 20 27 4d 72 2e 20 42 6f 6a 61  name = 'Mr. Boja
2f50: 6e 67 6c 65 73 27 3b 0a 20 20 7d 0a 7d 20 7b 31  ngles';.  }.} {1
2f60: 20 7b 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f   {foreign key co
2f70: 6e 73 74 72 61 69 6e 74 20 66 61 69 6c 65 64 7d  nstraint failed}
2f80: 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79  }.do_test e_fkey
2f90: 2d 31 33 2e 35 20 7b 0a 20 20 65 78 65 63 73 71  -13.5 {.  execsq
2fa0: 6c 20 7b 0a 20 20 20 20 49 4e 53 45 52 54 20 49  l {.    INSERT I
2fb0: 4e 54 4f 20 61 72 74 69 73 74 20 56 41 4c 55 45  NTO artist VALUE
2fc0: 53 28 33 2c 20 27 53 61 6d 6d 79 20 44 61 76 69  S(3, 'Sammy Davi
2fd0: 73 20 4a 72 2e 27 29 3b 0a 20 20 20 20 55 50 44  s Jr.');.    UPD
2fe0: 41 54 45 20 74 72 61 63 6b 20 53 45 54 20 74 72  ATE track SET tr
2ff0: 61 63 6b 61 72 74 69 73 74 20 3d 20 33 20 57 48  ackartist = 3 WH
3000: 45 52 45 20 74 72 61 63 6b 6e 61 6d 65 20 3d 20  ERE trackname = 
3010: 27 4d 72 2e 20 42 6f 6a 61 6e 67 6c 65 73 27 3b  'Mr. Bojangles';
3020: 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f  .    INSERT INTO
3030: 20 74 72 61 63 6b 20 56 41 4c 55 45 53 28 31 35   track VALUES(15
3040: 2c 20 27 42 6f 6f 67 69 65 20 57 6f 6f 67 69 65  , 'Boogie Woogie
3050: 27 2c 20 33 29 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a  ', 3);.  }.} {}.
3060: 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  .#--------------
3070: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
3080: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
3090: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
30a0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20 2f 2a  -----------.# /*
30b0: 20 45 56 3a 20 52 2d 31 35 30 33 34 2d 36 34 33   EV: R-15034-643
30c0: 33 31 20 2a 2f 0a 23 0a 23 20 54 65 73 74 20 74  31 */.#.# Test t
30d0: 68 65 20 73 65 63 6f 6e 64 20 65 78 61 6d 70 6c  he second exampl
30e0: 65 20 66 72 6f 6d 20 74 68 65 20 66 69 72 73 74  e from the first
30f0: 20 73 65 63 74 69 6f 6e 20 6f 66 20 66 6f 72 65   section of fore
3100: 69 67 6e 6b 65 79 73 2e 68 74 6d 6c 2e 0a 23 0a  ignkeys.html..#.
3110: 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 31  do_test e_fkey-1
3120: 34 2e 31 20 7b 0a 20 20 63 61 74 63 68 73 71 6c  4.1 {.  catchsql
3130: 20 7b 0a 20 20 20 20 44 45 4c 45 54 45 20 46 52   {.    DELETE FR
3140: 4f 4d 20 61 72 74 69 73 74 20 57 48 45 52 45 20  OM artist WHERE 
3150: 61 72 74 69 73 74 6e 61 6d 65 20 3d 20 27 46 72  artistname = 'Fr
3160: 61 6e 6b 20 53 69 6e 61 74 72 61 27 3b 0a 20 20  ank Sinatra';.  
3170: 7d 0a 7d 20 7b 31 20 7b 66 6f 72 65 69 67 6e 20  }.} {1 {foreign 
3180: 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 20 66  key constraint f
3190: 61 69 6c 65 64 7d 7d 0a 64 6f 5f 74 65 73 74 20  ailed}}.do_test 
31a0: 65 5f 66 6b 65 79 2d 31 34 2e 32 20 7b 0a 20 20  e_fkey-14.2 {.  
31b0: 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 44 45  execsql {.    DE
31c0: 4c 45 54 45 20 46 52 4f 4d 20 74 72 61 63 6b 20  LETE FROM track 
31d0: 57 48 45 52 45 20 74 72 61 63 6b 6e 61 6d 65 20  WHERE trackname 
31e0: 3d 20 27 4d 79 20 57 61 79 27 3b 0a 20 20 20 20  = 'My Way';.    
31f0: 44 45 4c 45 54 45 20 46 52 4f 4d 20 61 72 74 69  DELETE FROM arti
3200: 73 74 20 57 48 45 52 45 20 61 72 74 69 73 74 6e  st WHERE artistn
3210: 61 6d 65 20 3d 20 27 46 72 61 6e 6b 20 53 69 6e  ame = 'Frank Sin
3220: 61 74 72 61 27 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a  atra';.  }.} {}.
3230: 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 31  do_test e_fkey-1
3240: 34 2e 33 20 7b 0a 20 20 63 61 74 63 68 73 71 6c  4.3 {.  catchsql
3250: 20 7b 0a 20 20 20 20 55 50 44 41 54 45 20 61 72   {.    UPDATE ar
3260: 74 69 73 74 20 53 45 54 20 61 72 74 69 73 74 69  tist SET artisti
3270: 64 3d 34 20 57 48 45 52 45 20 61 72 74 69 73 74  d=4 WHERE artist
3280: 6e 61 6d 65 20 3d 20 27 44 65 61 6e 20 4d 61 72  name = 'Dean Mar
3290: 74 69 6e 27 3b 0a 20 20 7d 0a 7d 20 7b 31 20 7b  tin';.  }.} {1 {
32a0: 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73  foreign key cons
32b0: 74 72 61 69 6e 74 20 66 61 69 6c 65 64 7d 7d 0a  traint failed}}.
32c0: 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 31  do_test e_fkey-1
32d0: 34 2e 34 20 7b 0a 20 20 65 78 65 63 73 71 6c 20  4.4 {.  execsql 
32e0: 7b 0a 20 20 20 20 44 45 4c 45 54 45 20 46 52 4f  {.    DELETE FRO
32f0: 4d 20 74 72 61 63 6b 20 57 48 45 52 45 20 74 72  M track WHERE tr
3300: 61 63 6b 6e 61 6d 65 20 49 4e 28 27 54 68 61 74  ackname IN('That
3310: 27 27 73 20 41 6d 6f 72 65 27 2c 20 27 43 68 72  ''s Amore', 'Chr
3320: 69 73 74 6d 61 73 20 42 6c 75 65 73 27 29 3b 0a  istmas Blues');.
3330: 20 20 20 20 55 50 44 41 54 45 20 61 72 74 69 73      UPDATE artis
3340: 74 20 53 45 54 20 61 72 74 69 73 74 69 64 3d 34  t SET artistid=4
3350: 20 57 48 45 52 45 20 61 72 74 69 73 74 6e 61 6d   WHERE artistnam
3360: 65 20 3d 20 27 44 65 61 6e 20 4d 61 72 74 69 6e  e = 'Dean Martin
3370: 27 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a 0a 0a 23 2d  ';.  }.} {}...#-
3380: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
3390: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
33a0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
33b0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
33c0: 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20 2f 2a 20 45 56  --------.# /* EV
33d0: 3a 20 52 2d 35 36 30 33 32 2d 32 34 39 32 33 20  : R-56032-24923 
33e0: 2a 2f 0a 23 0a 23 20 54 65 73 74 20 74 68 61 74  */.#.# Test that
33f0: 20 61 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 63   a foreign key c
3400: 6f 6e 73 74 72 61 69 6e 74 20 69 73 20 73 61 74  onstraint is sat
3410: 69 73 69 66 65 64 20 69 66 20 22 66 6f 72 20 65  isifed if "for e
3420: 61 63 68 20 72 6f 77 20 69 6e 20 74 68 65 20 63  ach row in the c
3430: 68 69 6c 64 0a 23 20 74 61 62 6c 65 20 65 69 74  hild.# table eit
3440: 68 65 72 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20  her one or more 
3450: 6f 66 20 74 68 65 20 63 68 69 6c 64 20 6b 65 79  of the child key
3460: 20 63 6f 6c 75 6d 6e 73 20 61 72 65 20 4e 55 4c   columns are NUL
3470: 4c 2c 20 6f 72 20 74 68 65 72 65 20 65 78 69 73  L, or there exis
3480: 74 73 20 61 0a 23 20 72 6f 77 20 69 6e 20 74 68  ts a.# row in th
3490: 65 20 70 61 72 65 6e 74 20 74 61 62 6c 65 20 66  e parent table f
34a0: 6f 72 20 77 68 69 63 68 20 65 61 63 68 20 70 61  or which each pa
34b0: 72 65 6e 74 20 6b 65 79 20 63 6f 6c 75 6d 6e 20  rent key column 
34c0: 63 6f 6e 74 61 69 6e 73 20 61 20 76 61 6c 75 65  contains a value
34d0: 0a 23 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20  .# equal to the 
34e0: 76 61 6c 75 65 20 69 6e 20 69 74 73 20 61 73 73  value in its ass
34f0: 6f 63 69 61 74 65 64 20 63 68 69 6c 64 20 6b 65  ociated child ke
3500: 79 20 63 6f 6c 75 6d 6e 22 2e 0a 23 0a 23 20 2f  y column"..#.# /
3510: 2a 20 45 56 3a 20 52 2d 35 37 37 36 35 2d 31 32  * EV: R-57765-12
3520: 33 38 30 20 2a 2f 0a 23 0a 23 20 54 65 73 74 20  380 */.#.# Test 
3530: 61 6c 73 6f 20 74 68 61 74 20 74 68 65 20 63 6f  also that the co
3540: 6d 70 61 72 69 73 6f 6e 20 72 75 6c 65 73 20 61  mparison rules a
3550: 72 65 20 75 73 65 64 20 77 68 65 6e 20 74 65 73  re used when tes
3560: 74 69 6e 67 20 69 66 20 74 68 65 72 65 20 0a 23  ting if there .#
3570: 20 69 73 20 61 20 6d 61 74 63 68 69 6e 67 20 72   is a matching r
3580: 6f 77 20 69 6e 20 74 68 65 20 70 61 72 65 6e 74  ow in the parent
3590: 20 74 61 62 6c 65 20 6f 66 20 61 20 66 6f 72 65   table of a fore
35a0: 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69  ign key constrai
35b0: 6e 74 2e 0a 23 0a 64 72 6f 70 5f 61 6c 6c 5f 74  nt..#.drop_all_t
35c0: 61 62 6c 65 73 0a 64 6f 5f 74 65 73 74 20 65 5f  ables.do_test e_
35d0: 66 6b 65 79 2d 31 35 2e 31 20 7b 0a 20 20 65 78  fkey-15.1 {.  ex
35e0: 65 63 73 71 6c 20 7b 0a 20 20 20 20 43 52 45 41  ecsql {.    CREA
35f0: 54 45 20 54 41 42 4c 45 20 70 61 72 28 70 20 50  TE TABLE par(p P
3600: 52 49 4d 41 52 59 20 4b 45 59 29 3b 0a 20 20 20  RIMARY KEY);.   
3610: 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 63 68   CREATE TABLE ch
3620: 69 28 63 20 52 45 46 45 52 45 4e 43 45 53 20 70  i(c REFERENCES p
3630: 61 72 29 3b 0a 0a 20 20 20 20 49 4e 53 45 52 54  ar);..    INSERT
3640: 20 49 4e 54 4f 20 70 61 72 20 56 41 4c 55 45 53   INTO par VALUES
3650: 28 31 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20  (1);.    INSERT 
3660: 49 4e 54 4f 20 70 61 72 20 56 41 4c 55 45 53 28  INTO par VALUES(
3670: 27 31 27 29 3b 0a 20 20 20 20 49 4e 53 45 52 54  '1');.    INSERT
3680: 20 49 4e 54 4f 20 70 61 72 20 56 41 4c 55 45 53   INTO par VALUES
3690: 28 58 27 33 31 27 29 3b 0a 20 20 20 20 53 45 4c  (X'31');.    SEL
36a0: 45 43 54 20 74 79 70 65 6f 66 28 70 29 20 46 52  ECT typeof(p) FR
36b0: 4f 4d 20 70 61 72 3b 0a 20 20 7d 0a 7d 20 7b 69  OM par;.  }.} {i
36c0: 6e 74 65 67 65 72 20 74 65 78 74 20 62 6c 6f 62  nteger text blob
36d0: 7d 0a 0a 70 72 6f 63 20 74 65 73 74 5f 65 66 6b  }..proc test_efk
36e0: 65 79 5f 34 35 20 7b 74 6e 20 69 73 45 72 72 6f  ey_45 {tn isErro
36f0: 72 20 73 71 6c 7d 20 7b 0a 20 20 64 6f 5f 74 65  r sql} {.  do_te
3700: 73 74 20 65 5f 66 6b 65 79 2d 31 35 2e 24 74 6e  st e_fkey-15.$tn
3710: 2e 31 20 22 0a 20 20 20 20 63 61 74 63 68 73 71  .1 ".    catchsq
3720: 6c 20 7b 24 73 71 6c 7d 0a 20 20 22 20 5b 6c 69  l {$sql}.  " [li
3730: 6e 64 65 78 20 7b 7b 30 20 7b 7d 7d 20 7b 31 20  ndex {{0 {}} {1 
3740: 7b 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e  {foreign key con
3750: 73 74 72 61 69 6e 74 20 66 61 69 6c 65 64 7d 7d  straint failed}}
3760: 7d 20 24 69 73 45 72 72 6f 72 5d 0a 0a 20 20 64  } $isError]..  d
3770: 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 31 35  o_test e_fkey-15
3780: 2e 24 74 6e 2e 32 20 7b 0a 20 20 20 20 65 78 65  .$tn.2 {.    exe
3790: 63 73 71 6c 20 7b 0a 20 20 20 20 20 20 53 45 4c  csql {.      SEL
37a0: 45 43 54 20 2a 20 46 52 4f 4d 20 63 68 69 20 57  ECT * FROM chi W
37b0: 48 45 52 45 20 63 20 49 53 20 4e 4f 54 20 4e 55  HERE c IS NOT NU
37c0: 4c 4c 20 41 4e 44 20 63 20 4e 4f 54 20 49 4e 20  LL AND c NOT IN 
37d0: 28 53 45 4c 45 43 54 20 70 20 46 52 4f 4d 20 70  (SELECT p FROM p
37e0: 61 72 29 0a 20 20 20 20 7d 0a 20 20 7d 20 7b 7d  ar).    }.  } {}
37f0: 0a 7d 0a 0a 74 65 73 74 5f 65 66 6b 65 79 5f 34  .}..test_efkey_4
3800: 35 20 31 20 30 20 22 49 4e 53 45 52 54 20 49 4e  5 1 0 "INSERT IN
3810: 54 4f 20 63 68 69 20 56 41 4c 55 45 53 28 31 29  TO chi VALUES(1)
3820: 22 0a 74 65 73 74 5f 65 66 6b 65 79 5f 34 35 20  ".test_efkey_45 
3830: 32 20 31 20 22 49 4e 53 45 52 54 20 49 4e 54 4f  2 1 "INSERT INTO
3840: 20 63 68 69 20 56 41 4c 55 45 53 28 27 31 2e 30   chi VALUES('1.0
3850: 27 29 22 0a 74 65 73 74 5f 65 66 6b 65 79 5f 34  ')".test_efkey_4
3860: 35 20 33 20 30 20 22 49 4e 53 45 52 54 20 49 4e  5 3 0 "INSERT IN
3870: 54 4f 20 63 68 69 20 56 41 4c 55 45 53 28 27 31  TO chi VALUES('1
3880: 27 29 22 0a 74 65 73 74 5f 65 66 6b 65 79 5f 34  ')".test_efkey_4
3890: 35 20 34 20 31 20 22 44 45 4c 45 54 45 20 46 52  5 4 1 "DELETE FR
38a0: 4f 4d 20 70 61 72 20 57 48 45 52 45 20 70 20 3d  OM par WHERE p =
38b0: 20 27 31 27 22 0a 74 65 73 74 5f 65 66 6b 65 79   '1'".test_efkey
38c0: 5f 34 35 20 35 20 30 20 22 44 45 4c 45 54 45 20  _45 5 0 "DELETE 
38d0: 46 52 4f 4d 20 63 68 69 20 57 48 45 52 45 20 63  FROM chi WHERE c
38e0: 20 3d 20 27 31 27 22 0a 74 65 73 74 5f 65 66 6b   = '1'".test_efk
38f0: 65 79 5f 34 35 20 36 20 30 20 22 44 45 4c 45 54  ey_45 6 0 "DELET
3900: 45 20 46 52 4f 4d 20 70 61 72 20 57 48 45 52 45  E FROM par WHERE
3910: 20 70 20 3d 20 27 31 27 22 0a 74 65 73 74 5f 65   p = '1'".test_e
3920: 66 6b 65 79 5f 34 35 20 37 20 31 20 22 49 4e 53  fkey_45 7 1 "INS
3930: 45 52 54 20 49 4e 54 4f 20 63 68 69 20 56 41 4c  ERT INTO chi VAL
3940: 55 45 53 28 27 31 27 29 22 0a 74 65 73 74 5f 65  UES('1')".test_e
3950: 66 6b 65 79 5f 34 35 20 38 20 30 20 22 49 4e 53  fkey_45 8 0 "INS
3960: 45 52 54 20 49 4e 54 4f 20 63 68 69 20 56 41 4c  ERT INTO chi VAL
3970: 55 45 53 28 58 27 33 31 27 29 22 0a 74 65 73 74  UES(X'31')".test
3980: 5f 65 66 6b 65 79 5f 34 35 20 39 20 31 20 22 49  _efkey_45 9 1 "I
3990: 4e 53 45 52 54 20 49 4e 54 4f 20 63 68 69 20 56  NSERT INTO chi V
39a0: 41 4c 55 45 53 28 58 27 33 32 27 29 22 0a 0a 23  ALUES(X'32')"..#
39b0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
39c0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
39d0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
39e0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
39f0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20 2f 2a 20 45  ---------.# /* E
3a00: 56 3a 20 52 2d 31 35 37 39 36 2d 34 37 35 31 33  V: R-15796-47513
3a10: 20 2a 2f 0a 23 0a 23 20 53 70 65 63 69 66 69 63   */.#.# Specific
3a20: 61 6c 6c 79 2c 20 74 65 73 74 20 74 68 61 74 20  ally, test that 
3a30: 77 68 65 6e 20 63 6f 6d 70 61 72 69 6e 67 20 63  when comparing c
3a40: 68 69 6c 64 20 61 6e 64 20 70 61 72 65 6e 74 20  hild and parent 
3a50: 6b 65 79 20 76 61 6c 75 65 73 20 74 68 65 0a 23  key values the.#
3a60: 20 64 65 66 61 75 6c 74 20 63 6f 6c 6c 61 74 69   default collati
3a70: 6f 6e 20 73 65 71 75 65 6e 63 65 20 6f 66 20 74  on sequence of t
3a80: 68 65 20 70 61 72 65 6e 74 20 6b 65 79 20 63 6f  he parent key co
3a90: 6c 75 6d 6e 20 69 73 20 75 73 65 64 2e 0a 23 0a  lumn is used..#.
3aa0: 64 72 6f 70 5f 61 6c 6c 5f 74 61 62 6c 65 73 0a  drop_all_tables.
3ab0: 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 31  do_test e_fkey-1
3ac0: 36 2e 31 20 7b 0a 20 20 65 78 65 63 73 71 6c 20  6.1 {.  execsql 
3ad0: 7b 0a 20 20 20 20 43 52 45 41 54 45 20 54 41 42  {.    CREATE TAB
3ae0: 4c 45 20 74 31 28 61 20 43 4f 4c 4c 41 54 45 20  LE t1(a COLLATE 
3af0: 6e 6f 63 61 73 65 20 50 52 49 4d 41 52 59 20 4b  nocase PRIMARY K
3b00: 45 59 29 3b 0a 20 20 20 20 43 52 45 41 54 45 20  EY);.    CREATE 
3b10: 54 41 42 4c 45 20 74 32 28 62 20 52 45 46 45 52  TABLE t2(b REFER
3b20: 45 4e 43 45 53 20 74 31 29 3b 0a 20 20 7d 0a 7d  ENCES t1);.  }.}
3b30: 20 7b 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b   {}.do_test e_fk
3b40: 65 79 2d 31 36 2e 32 20 7b 0a 20 20 65 78 65 63  ey-16.2 {.  exec
3b50: 73 71 6c 20 7b 0a 20 20 20 20 49 4e 53 45 52 54  sql {.    INSERT
3b60: 20 49 4e 54 4f 20 74 31 20 56 41 4c 55 45 53 28   INTO t1 VALUES(
3b70: 27 6f 4e 65 27 29 3b 0a 20 20 20 20 49 4e 53 45  'oNe');.    INSE
3b80: 52 54 20 49 4e 54 4f 20 74 32 20 56 41 4c 55 45  RT INTO t2 VALUE
3b90: 53 28 27 6f 6e 65 27 29 3b 0a 20 20 20 20 49 4e  S('one');.    IN
3ba0: 53 45 52 54 20 49 4e 54 4f 20 74 32 20 56 41 4c  SERT INTO t2 VAL
3bb0: 55 45 53 28 27 4f 4e 45 27 29 3b 0a 20 20 20 20  UES('ONE');.    
3bc0: 55 50 44 41 54 45 20 74 32 20 53 45 54 20 62 20  UPDATE t2 SET b 
3bd0: 3d 20 27 4f 6e 45 27 3b 0a 20 20 20 20 55 50 44  = 'OnE';.    UPD
3be0: 41 54 45 20 74 31 20 53 45 54 20 61 20 3d 20 27  ATE t1 SET a = '
3bf0: 4f 4e 45 27 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a 64  ONE';.  }.} {}.d
3c00: 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 31 36  o_test e_fkey-16
3c10: 2e 33 20 7b 0a 20 20 63 61 74 63 68 73 71 6c 20  .3 {.  catchsql 
3c20: 7b 20 55 50 44 41 54 45 20 74 32 20 53 45 54 20  { UPDATE t2 SET 
3c30: 62 20 3d 20 27 74 77 6f 27 20 57 48 45 52 45 20  b = 'two' WHERE 
3c40: 72 6f 77 69 64 20 3d 20 31 20 7d 0a 7d 20 7b 31  rowid = 1 }.} {1
3c50: 20 7b 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f   {foreign key co
3c60: 6e 73 74 72 61 69 6e 74 20 66 61 69 6c 65 64 7d  nstraint failed}
3c70: 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79  }.do_test e_fkey
3c80: 2d 31 36 2e 34 20 7b 0a 20 20 63 61 74 63 68 73  -16.4 {.  catchs
3c90: 71 6c 20 7b 20 44 45 4c 45 54 45 20 46 52 4f 4d  ql { DELETE FROM
3ca0: 20 74 31 20 57 48 45 52 45 20 72 6f 77 69 64 20   t1 WHERE rowid 
3cb0: 3d 20 31 20 7d 0a 7d 20 7b 31 20 7b 66 6f 72 65  = 1 }.} {1 {fore
3cc0: 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69  ign key constrai
3cd0: 6e 74 20 66 61 69 6c 65 64 7d 7d 0a 0a 23 2d 2d  nt failed}}..#--
3ce0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
3cf0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
3d00: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
3d10: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
3d20: 2d 2d 2d 2d 2d 2d 2d 0a 23 20 2f 2a 20 45 56 3a  -------.# /* EV:
3d30: 20 52 2d 30 34 32 34 30 2d 31 33 38 36 30 20 2a   R-04240-13860 *
3d40: 2f 0a 23 0a 23 20 53 70 65 63 69 66 69 63 61 6c  /.#.# Specifical
3d50: 6c 79 2c 20 74 65 73 74 20 74 68 61 74 20 77 68  ly, test that wh
3d60: 65 6e 20 63 6f 6d 70 61 72 69 6e 67 20 63 68 69  en comparing chi
3d70: 6c 64 20 61 6e 64 20 70 61 72 65 6e 74 20 6b 65  ld and parent ke
3d80: 79 20 76 61 6c 75 65 73 20 74 68 65 0a 23 20 61  y values the.# a
3d90: 66 66 69 6e 69 74 79 20 6f 66 20 74 68 65 20 70  ffinity of the p
3da0: 61 72 65 6e 74 20 6b 65 79 20 63 6f 6c 75 6d 6e  arent key column
3db0: 20 69 73 20 61 70 70 6c 69 65 64 20 74 6f 20 74   is applied to t
3dc0: 68 65 20 63 68 69 6c 64 20 6b 65 79 20 76 61 6c  he child key val
3dd0: 75 65 0a 23 20 62 65 66 6f 72 65 20 74 68 65 20  ue.# before the 
3de0: 63 6f 6d 70 61 72 69 73 6f 6e 20 74 61 6b 65 73  comparison takes
3df0: 20 70 6c 61 63 65 2e 0a 23 0a 64 72 6f 70 5f 61   place..#.drop_a
3e00: 6c 6c 5f 74 61 62 6c 65 73 0a 64 6f 5f 74 65 73  ll_tables.do_tes
3e10: 74 20 65 5f 66 6b 65 79 2d 31 37 2e 31 20 7b 0a  t e_fkey-17.1 {.
3e20: 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20    execsql {.    
3e30: 43 52 45 41 54 45 20 54 41 42 4c 45 20 74 31 28  CREATE TABLE t1(
3e40: 61 20 4e 55 4d 45 52 49 43 20 50 52 49 4d 41 52  a NUMERIC PRIMAR
3e50: 59 20 4b 45 59 29 3b 0a 20 20 20 20 43 52 45 41  Y KEY);.    CREA
3e60: 54 45 20 54 41 42 4c 45 20 74 32 28 62 20 54 45  TE TABLE t2(b TE
3e70: 58 54 20 52 45 46 45 52 45 4e 43 45 53 20 74 31  XT REFERENCES t1
3e80: 29 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a 64 6f 5f 74  );.  }.} {}.do_t
3e90: 65 73 74 20 65 5f 66 6b 65 79 2d 31 37 2e 32 20  est e_fkey-17.2 
3ea0: 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20  {.  execsql {.  
3eb0: 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 31    INSERT INTO t1
3ec0: 20 56 41 4c 55 45 53 28 31 29 3b 0a 20 20 20 20   VALUES(1);.    
3ed0: 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 31 20 56  INSERT INTO t1 V
3ee0: 41 4c 55 45 53 28 32 29 3b 0a 20 20 20 20 49 4e  ALUES(2);.    IN
3ef0: 53 45 52 54 20 49 4e 54 4f 20 74 31 20 56 41 4c  SERT INTO t1 VAL
3f00: 55 45 53 28 27 74 68 72 65 65 27 29 3b 0a 20 20  UES('three');.  
3f10: 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 32    INSERT INTO t2
3f20: 20 56 41 4c 55 45 53 28 27 32 2e 30 27 29 3b 0a   VALUES('2.0');.
3f30: 20 20 20 20 53 45 4c 45 43 54 20 62 2c 20 74 79      SELECT b, ty
3f40: 70 65 6f 66 28 62 29 20 46 52 4f 4d 20 74 32 3b  peof(b) FROM t2;
3f50: 0a 20 20 7d 0a 7d 20 7b 32 2e 30 20 74 65 78 74  .  }.} {2.0 text
3f60: 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79  }.do_test e_fkey
3f70: 2d 31 37 2e 33 20 7b 0a 20 20 65 78 65 63 73 71  -17.3 {.  execsq
3f80: 6c 20 7b 20 53 45 4c 45 43 54 20 74 79 70 65 6f  l { SELECT typeo
3f90: 66 28 61 29 20 46 52 4f 4d 20 74 31 20 7d 0a 7d  f(a) FROM t1 }.}
3fa0: 20 7b 69 6e 74 65 67 65 72 20 69 6e 74 65 67 65   {integer intege
3fb0: 72 20 74 65 78 74 7d 0a 64 6f 5f 74 65 73 74 20  r text}.do_test 
3fc0: 65 5f 66 6b 65 79 2d 31 37 2e 34 20 7b 0a 20 20  e_fkey-17.4 {.  
3fd0: 63 61 74 63 68 73 71 6c 20 7b 20 44 45 4c 45 54  catchsql { DELET
3fe0: 45 20 46 52 4f 4d 20 74 31 20 57 48 45 52 45 20  E FROM t1 WHERE 
3ff0: 72 6f 77 69 64 20 3d 20 32 20 7d 0a 7d 20 7b 31  rowid = 2 }.} {1
4000: 20 7b 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f   {foreign key co
4010: 6e 73 74 72 61 69 6e 74 20 66 61 69 6c 65 64 7d  nstraint failed}
4020: 7d 0a 0a 23 23 23 23 23 23 23 23 23 23 23 23 23  }..#############
4030: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
4040: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
4050: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
4060: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 0a 23  ##############.#
4070: 23 23 20 53 45 43 54 49 4f 4e 20 33 3a 20 52 65  ## SECTION 3: Re
4080: 71 75 69 72 65 64 20 61 6e 64 20 53 75 67 67 65  quired and Sugge
4090: 73 74 65 64 20 44 61 74 61 62 61 73 65 20 49 6e  sted Database In
40a0: 64 65 78 65 73 0a 23 23 23 23 23 23 23 23 23 23  dexes.##########
40b0: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
40c0: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
40d0: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
40e0: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
40f0: 23 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  #..#------------
4100: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
4110: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
4120: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
4130: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20  -------------.# 
4140: 2f 2a 20 45 56 3a 20 52 2d 31 33 34 33 35 2d 32  /* EV: R-13435-2
4150: 36 33 31 31 20 2a 2f 0a 23 0a 23 20 41 20 70 61  6311 */.#.# A pa
4160: 72 65 6e 74 20 6b 65 79 20 6d 75 73 74 20 62 65  rent key must be
4170: 20 65 69 74 68 65 72 20 61 20 50 52 49 4d 41 52   either a PRIMAR
4180: 59 20 4b 45 59 2c 20 73 75 62 6a 65 63 74 20 74  Y KEY, subject t
4190: 6f 20 61 20 55 4e 49 51 55 45 20 0a 23 20 63 6f  o a UNIQUE .# co
41a0: 6e 73 74 72 61 69 6e 74 2c 20 6f 72 20 68 61 76  nstraint, or hav
41b0: 65 20 61 20 55 4e 49 51 55 45 20 69 6e 64 65 78  e a UNIQUE index
41c0: 20 63 72 65 61 74 65 64 20 6f 6e 20 69 74 2e 0a   created on it..
41d0: 23 20 0a 23 20 2f 2a 20 45 56 3a 20 52 2d 30 30  # .# /* EV: R-00
41e0: 33 37 36 2d 33 39 32 31 32 20 2a 2f 0a 23 0a 23  376-39212 */.#.#
41f0: 20 41 6c 73 6f 20 74 65 73 74 20 74 68 61 74 20   Also test that 
4200: 69 66 20 61 20 70 61 72 65 6e 74 20 6b 65 79 20  if a parent key 
4210: 69 73 20 6e 6f 74 20 73 75 62 6a 65 63 74 20 74  is not subject t
4220: 6f 20 61 20 50 52 49 4d 41 52 59 20 4b 45 59 20  o a PRIMARY KEY 
4230: 6f 72 20 55 4e 49 51 55 45 0a 23 20 63 6f 6e 73  or UNIQUE.# cons
4240: 74 72 61 69 6e 74 2c 20 62 75 74 20 64 6f 65 73  traint, but does
4250: 20 68 61 76 65 20 61 20 55 4e 49 51 55 45 20 69   have a UNIQUE i
4260: 6e 64 65 78 20 63 72 65 61 74 65 64 20 6f 6e 20  ndex created on 
4270: 69 74 2c 20 74 68 65 6e 20 74 68 65 20 55 4e 49  it, then the UNI
4280: 51 55 45 20 69 6e 64 65 78 0a 23 20 6d 75 73 74  QUE index.# must
4290: 20 75 73 65 20 74 68 65 20 64 65 66 61 75 6c 74   use the default
42a0: 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65   collation seque
42b0: 6e 63 65 73 20 61 73 73 6f 63 69 61 74 65 64 20  nces associated 
42c0: 77 69 74 68 20 74 68 65 20 70 61 72 65 6e 74 20  with the parent 
42d0: 6b 65 79 0a 23 20 63 6f 6c 75 6d 6e 73 2e 0a 23  key.# columns..#
42e0: 0a 64 72 6f 70 5f 61 6c 6c 5f 74 61 62 6c 65 73  .drop_all_tables
42f0: 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d  .do_test e_fkey-
4300: 31 38 2e 31 20 7b 0a 20 20 65 78 65 63 73 71 6c  18.1 {.  execsql
4310: 20 7b 0a 20 20 20 20 43 52 45 41 54 45 20 54 41   {.    CREATE TA
4320: 42 4c 45 20 74 32 28 61 20 52 45 46 45 52 45 4e  BLE t2(a REFEREN
4330: 43 45 53 20 74 31 28 78 29 29 3b 0a 20 20 7d 0a  CES t1(x));.  }.
4340: 7d 20 7b 7d 0a 70 72 6f 63 20 74 65 73 74 5f 65  } {}.proc test_e
4350: 66 6b 65 79 5f 35 37 20 7b 74 6e 20 69 73 45 72  fkey_57 {tn isEr
4360: 72 6f 72 20 73 71 6c 7d 20 7b 0a 20 20 63 61 74  ror sql} {.  cat
4370: 63 68 73 71 6c 20 7b 20 44 52 4f 50 20 54 41 42  chsql { DROP TAB
4380: 4c 45 20 74 31 20 7d 0a 20 20 65 78 65 63 73 71  LE t1 }.  execsq
4390: 6c 20 24 73 71 6c 0a 20 20 64 6f 5f 74 65 73 74  l $sql.  do_test
43a0: 20 65 5f 66 6b 65 79 2d 31 38 2e 24 74 6e 20 7b   e_fkey-18.$tn {
43b0: 0a 20 20 20 20 63 61 74 63 68 73 71 6c 20 7b 20  .    catchsql { 
43c0: 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 32 20 56  INSERT INTO t2 V
43d0: 41 4c 55 45 53 28 4e 55 4c 4c 29 20 7d 0a 20 20  ALUES(NULL) }.  
43e0: 7d 20 5b 6c 69 6e 64 65 78 20 7b 7b 30 20 7b 7d  } [lindex {{0 {}
43f0: 7d 20 7b 31 20 7b 66 6f 72 65 69 67 6e 20 6b 65  } {1 {foreign ke
4400: 79 20 6d 69 73 6d 61 74 63 68 7d 7d 7d 20 24 69  y mismatch}}} $i
4410: 73 45 72 72 6f 72 5d 0a 7d 0a 74 65 73 74 5f 65  sError].}.test_e
4420: 66 6b 65 79 5f 35 37 20 32 20 30 20 7b 20 43 52  fkey_57 2 0 { CR
4430: 45 41 54 45 20 54 41 42 4c 45 20 74 31 28 78 20  EATE TABLE t1(x 
4440: 50 52 49 4d 41 52 59 20 4b 45 59 29 20 7d 0a 74  PRIMARY KEY) }.t
4450: 65 73 74 5f 65 66 6b 65 79 5f 35 37 20 33 20 30  est_efkey_57 3 0
4460: 20 7b 20 43 52 45 41 54 45 20 54 41 42 4c 45 20   { CREATE TABLE 
4470: 74 31 28 78 20 55 4e 49 51 55 45 29 20 7d 0a 74  t1(x UNIQUE) }.t
4480: 65 73 74 5f 65 66 6b 65 79 5f 35 37 20 34 20 30  est_efkey_57 4 0
4490: 20 7b 20 43 52 45 41 54 45 20 54 41 42 4c 45 20   { CREATE TABLE 
44a0: 74 31 28 78 29 3b 20 43 52 45 41 54 45 20 55 4e  t1(x); CREATE UN
44b0: 49 51 55 45 20 49 4e 44 45 58 20 74 31 69 20 4f  IQUE INDEX t1i O
44c0: 4e 20 74 31 28 78 29 20 7d 0a 74 65 73 74 5f 65  N t1(x) }.test_e
44d0: 66 6b 65 79 5f 35 37 20 35 20 31 20 7b 20 0a 20  fkey_57 5 1 { . 
44e0: 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 74 31   CREATE TABLE t1
44f0: 28 78 29 3b 20 0a 20 20 43 52 45 41 54 45 20 55  (x); .  CREATE U
4500: 4e 49 51 55 45 20 49 4e 44 45 58 20 74 31 69 20  NIQUE INDEX t1i 
4510: 4f 4e 20 74 31 28 78 20 43 4f 4c 4c 41 54 45 20  ON t1(x COLLATE 
4520: 6e 6f 63 61 73 65 29 3b 0a 7d 0a 74 65 73 74 5f  nocase);.}.test_
4530: 65 66 6b 65 79 5f 35 37 20 36 20 31 20 7b 20 43  efkey_57 6 1 { C
4540: 52 45 41 54 45 20 54 41 42 4c 45 20 74 31 28 78  REATE TABLE t1(x
4550: 29 20 7d 0a 74 65 73 74 5f 65 66 6b 65 79 5f 35  ) }.test_efkey_5
4560: 37 20 37 20 31 20 7b 20 43 52 45 41 54 45 20 54  7 7 1 { CREATE T
4570: 41 42 4c 45 20 74 31 28 78 2c 20 79 2c 20 50 52  ABLE t1(x, y, PR
4580: 49 4d 41 52 59 20 4b 45 59 28 78 2c 20 79 29 29  IMARY KEY(x, y))
4590: 20 7d 0a 74 65 73 74 5f 65 66 6b 65 79 5f 35 37   }.test_efkey_57
45a0: 20 38 20 31 20 7b 20 43 52 45 41 54 45 20 54 41   8 1 { CREATE TA
45b0: 42 4c 45 20 74 31 28 78 2c 20 79 2c 20 55 4e 49  BLE t1(x, y, UNI
45c0: 51 55 45 28 78 2c 20 79 29 29 20 7d 0a 74 65 73  QUE(x, y)) }.tes
45d0: 74 5f 65 66 6b 65 79 5f 35 37 20 39 20 31 20 7b  t_efkey_57 9 1 {
45e0: 20 0a 20 20 43 52 45 41 54 45 20 54 41 42 4c 45   .  CREATE TABLE
45f0: 20 74 31 28 78 2c 20 79 29 3b 20 0a 20 20 43 52   t1(x, y); .  CR
4600: 45 41 54 45 20 55 4e 49 51 55 45 20 49 4e 44 45  EATE UNIQUE INDE
4610: 58 20 74 31 69 20 4f 4e 20 74 31 28 78 2c 20 79  X t1i ON t1(x, y
4620: 29 3b 0a 7d 0a 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d  );.}...#--------
4630: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
4640: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
4650: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
4660: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
4670: 2d 0a 23 20 54 68 69 73 20 62 6c 6f 63 6b 20 74  -.# This block t
4680: 65 73 74 73 20 61 6e 20 65 78 61 6d 70 6c 65 20  ests an example 
4690: 69 6e 20 66 6f 72 65 69 67 6e 6b 65 79 73 2e 68  in foreignkeys.h
46a0: 74 6d 6c 2e 20 53 65 76 65 72 61 6c 20 74 65 73  tml. Several tes
46b0: 74 61 62 6c 65 0a 23 20 73 74 61 74 65 6d 65 6e  table.# statemen
46c0: 74 73 20 72 65 66 65 72 20 74 6f 20 74 68 69 73  ts refer to this
46d0: 20 65 78 61 6d 70 6c 65 2c 20 61 73 20 66 6f 6c   example, as fol
46e0: 6c 6f 77 73 0a 23 0a 23 20 2f 2a 20 45 56 3a 20  lows.#.# /* EV: 
46f0: 52 2d 32 37 34 38 34 2d 30 31 34 36 37 20 2a 2f  R-27484-01467 */
4700: 0a 23 0a 23 20 46 4b 20 43 6f 6e 73 74 72 61 69  .#.# FK Constrai
4710: 6e 74 73 20 6f 6e 20 63 68 69 6c 64 31 2c 20 63  nts on child1, c
4720: 68 69 6c 64 32 20 61 6e 64 20 63 68 69 6c 64 33  hild2 and child3
4730: 20 61 72 65 20 4f 6b 2e 0a 23 0a 23 20 2f 2a 20   are Ok..#.# /* 
4740: 45 56 3a 20 52 2d 35 31 30 33 39 2d 34 34 38 34  EV: R-51039-4484
4750: 30 20 2a 2f 0a 23 0a 23 20 50 72 6f 62 6c 65 6d  0 */.#.# Problem
4760: 20 77 69 74 68 20 46 4b 20 6f 6e 20 63 68 69 6c   with FK on chil
4770: 64 34 2e 0a 23 0a 23 20 2f 2a 20 45 56 3a 20 52  d4..#.# /* EV: R
4780: 2d 30 31 30 36 30 2d 34 38 37 38 38 20 2a 2f 0a  -01060-48788 */.
4790: 23 0a 23 20 50 72 6f 62 6c 65 6d 20 77 69 74 68  #.# Problem with
47a0: 20 46 4b 20 6f 6e 20 63 68 69 6c 64 35 2e 0a 23   FK on child5..#
47b0: 0a 23 20 2f 2a 20 45 56 3a 20 52 2d 36 33 30 38  .# /* EV: R-6308
47c0: 38 2d 33 37 34 36 39 20 2a 2f 0a 23 0a 23 20 50  8-37469 */.#.# P
47d0: 72 6f 62 6c 65 6d 20 77 69 74 68 20 46 4b 20 6f  roblem with FK o
47e0: 6e 20 63 68 69 6c 64 36 20 61 6e 64 20 63 68 69  n child6 and chi
47f0: 6c 64 37 2e 0a 23 0a 64 72 6f 70 5f 61 6c 6c 5f  ld7..#.drop_all_
4800: 74 61 62 6c 65 73 0a 64 6f 5f 74 65 73 74 20 65  tables.do_test e
4810: 5f 66 6b 65 79 2d 31 39 2e 31 20 7b 0a 20 20 65  _fkey-19.1 {.  e
4820: 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 43 52 45  xecsql {.    CRE
4830: 41 54 45 20 54 41 42 4c 45 20 70 61 72 65 6e 74  ATE TABLE parent
4840: 28 61 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20  (a PRIMARY KEY, 
4850: 62 20 55 4e 49 51 55 45 2c 20 63 2c 20 64 2c 20  b UNIQUE, c, d, 
4860: 65 2c 20 66 29 3b 0a 20 20 20 20 43 52 45 41 54  e, f);.    CREAT
4870: 45 20 55 4e 49 51 55 45 20 49 4e 44 45 58 20 69  E UNIQUE INDEX i
4880: 31 20 4f 4e 20 70 61 72 65 6e 74 28 63 2c 20 64  1 ON parent(c, d
4890: 29 3b 0a 20 20 20 20 43 52 45 41 54 45 20 49 4e  );.    CREATE IN
48a0: 44 45 58 20 69 32 20 4f 4e 20 70 61 72 65 6e 74  DEX i2 ON parent
48b0: 28 65 29 3b 0a 20 20 20 20 43 52 45 41 54 45 20  (e);.    CREATE 
48c0: 55 4e 49 51 55 45 20 49 4e 44 45 58 20 69 33 20  UNIQUE INDEX i3 
48d0: 4f 4e 20 70 61 72 65 6e 74 28 66 20 43 4f 4c 4c  ON parent(f COLL
48e0: 41 54 45 20 6e 6f 63 61 73 65 29 3b 0a 0a 20 20  ATE nocase);..  
48f0: 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 63    CREATE TABLE c
4900: 68 69 6c 64 31 28 66 2c 20 67 20 52 45 46 45 52  hild1(f, g REFER
4910: 45 4e 43 45 53 20 70 61 72 65 6e 74 28 61 29 29  ENCES parent(a))
4920: 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
4930: 20 20 20 20 20 20 20 20 2d 2d 20 4f 6b 0a 20 20          -- Ok.  
4940: 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 63    CREATE TABLE c
4950: 68 69 6c 64 32 28 68 2c 20 69 20 52 45 46 45 52  hild2(h, i REFER
4960: 45 4e 43 45 53 20 70 61 72 65 6e 74 28 62 29 29  ENCES parent(b))
4970: 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
4980: 20 20 20 20 20 20 20 20 2d 2d 20 4f 6b 0a 20 20          -- Ok.  
4990: 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 63    CREATE TABLE c
49a0: 68 69 6c 64 33 28 6a 2c 20 6b 2c 20 46 4f 52 45  hild3(j, k, FORE
49b0: 49 47 4e 20 4b 45 59 28 6a 2c 20 6b 29 20 52 45  IGN KEY(j, k) RE
49c0: 46 45 52 45 4e 43 45 53 20 70 61 72 65 6e 74 28  FERENCES parent(
49d0: 63 2c 20 64 29 29 3b 20 2d 2d 20 4f 6b 0a 20 20  c, d)); -- Ok.  
49e0: 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 63    CREATE TABLE c
49f0: 68 69 6c 64 34 28 6c 2c 20 6d 20 52 45 46 45 52  hild4(l, m REFER
4a00: 45 4e 43 45 53 20 70 61 72 65 6e 74 28 65 29 29  ENCES parent(e))
4a10: 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
4a20: 20 20 20 20 20 20 20 20 2d 2d 20 45 72 72 0a 20          -- Err. 
4a30: 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20     CREATE TABLE 
4a40: 63 68 69 6c 64 35 28 6e 2c 20 6f 20 52 45 46 45  child5(n, o REFE
4a50: 52 45 4e 43 45 53 20 70 61 72 65 6e 74 28 66 29  RENCES parent(f)
4a60: 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  );              
4a70: 20 20 20 20 20 20 20 20 20 2d 2d 20 45 72 72 0a           -- Err.
4a80: 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45      CREATE TABLE
4a90: 20 63 68 69 6c 64 36 28 70 2c 20 71 2c 20 46 4f   child6(p, q, FO
4aa0: 52 45 49 47 4e 20 4b 45 59 28 70 2c 71 29 20 52  REIGN KEY(p,q) R
4ab0: 45 46 45 52 45 4e 43 45 53 20 70 61 72 65 6e 74  EFERENCES parent
4ac0: 28 62 2c 20 63 29 29 3b 20 20 2d 2d 20 45 72 72  (b, c));  -- Err
4ad0: 0a 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c  .    CREATE TABL
4ae0: 45 20 63 68 69 6c 64 37 28 72 20 52 45 46 45 52  E child7(r REFER
4af0: 45 4e 43 45 53 20 70 61 72 65 6e 74 28 63 29 29  ENCES parent(c))
4b00: 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
4b10: 20 20 20 20 20 20 20 20 20 20 20 2d 2d 20 45 72             -- Er
4b20: 72 0a 20 20 7d 0a 7d 20 7b 7d 0a 64 6f 5f 74 65  r.  }.} {}.do_te
4b30: 73 74 20 65 5f 66 6b 65 79 2d 31 39 2e 32 20 7b  st e_fkey-19.2 {
4b40: 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20  .  execsql {.   
4b50: 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 70 61 72   INSERT INTO par
4b60: 65 6e 74 20 56 41 4c 55 45 53 28 31 2c 20 32 2c  ent VALUES(1, 2,
4b70: 20 33 2c 20 34 2c 20 35 2c 20 36 29 3b 0a 20 20   3, 4, 5, 6);.  
4b80: 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 63 68    INSERT INTO ch
4b90: 69 6c 64 31 20 56 41 4c 55 45 53 28 27 78 78 78  ild1 VALUES('xxx
4ba0: 27 2c 20 31 29 3b 0a 20 20 20 20 49 4e 53 45 52  ', 1);.    INSER
4bb0: 54 20 49 4e 54 4f 20 63 68 69 6c 64 32 20 56 41  T INTO child2 VA
4bc0: 4c 55 45 53 28 27 78 78 78 27 2c 20 32 29 3b 0a  LUES('xxx', 2);.
4bd0: 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20      INSERT INTO 
4be0: 63 68 69 6c 64 33 20 56 41 4c 55 45 53 28 33 2c  child3 VALUES(3,
4bf0: 20 34 29 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a 64 6f   4);.  }.} {}.do
4c00: 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 31 39 2e  _test e_fkey-19.
4c10: 32 20 7b 0a 20 20 63 61 74 63 68 73 71 6c 20 7b  2 {.  catchsql {
4c20: 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 63 68 69   INSERT INTO chi
4c30: 6c 64 34 20 56 41 4c 55 45 53 28 27 78 78 78 27  ld4 VALUES('xxx'
4c40: 2c 20 35 29 20 7d 0a 7d 20 7b 31 20 7b 66 6f 72  , 5) }.} {1 {for
4c50: 65 69 67 6e 20 6b 65 79 20 6d 69 73 6d 61 74 63  eign key mismatc
4c60: 68 7d 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b  h}}.do_test e_fk
4c70: 65 79 2d 31 39 2e 33 20 7b 0a 20 20 63 61 74 63  ey-19.3 {.  catc
4c80: 68 73 71 6c 20 7b 20 49 4e 53 45 52 54 20 49 4e  hsql { INSERT IN
4c90: 54 4f 20 63 68 69 6c 64 35 20 56 41 4c 55 45 53  TO child5 VALUES
4ca0: 28 27 78 78 78 27 2c 20 36 29 20 7d 0a 7d 20 7b  ('xxx', 6) }.} {
4cb0: 31 20 7b 66 6f 72 65 69 67 6e 20 6b 65 79 20 6d  1 {foreign key m
4cc0: 69 73 6d 61 74 63 68 7d 7d 0a 64 6f 5f 74 65 73  ismatch}}.do_tes
4cd0: 74 20 65 5f 66 6b 65 79 2d 31 39 2e 34 20 7b 0a  t e_fkey-19.4 {.
4ce0: 20 20 63 61 74 63 68 73 71 6c 20 7b 20 49 4e 53    catchsql { INS
4cf0: 45 52 54 20 49 4e 54 4f 20 63 68 69 6c 64 36 20  ERT INTO child6 
4d00: 56 41 4c 55 45 53 28 32 2c 20 33 29 20 7d 0a 7d  VALUES(2, 3) }.}
4d10: 20 7b 31 20 7b 66 6f 72 65 69 67 6e 20 6b 65 79   {1 {foreign key
4d20: 20 6d 69 73 6d 61 74 63 68 7d 7d 0a 64 6f 5f 74   mismatch}}.do_t
4d30: 65 73 74 20 65 5f 66 6b 65 79 2d 31 39 2e 35 20  est e_fkey-19.5 
4d40: 7b 0a 20 20 63 61 74 63 68 73 71 6c 20 7b 20 49  {.  catchsql { I
4d50: 4e 53 45 52 54 20 49 4e 54 4f 20 63 68 69 6c 64  NSERT INTO child
4d60: 37 20 56 41 4c 55 45 53 28 33 29 20 7d 0a 7d 20  7 VALUES(3) }.} 
4d70: 7b 31 20 7b 66 6f 72 65 69 67 6e 20 6b 65 79 20  {1 {foreign key 
4d80: 6d 69 73 6d 61 74 63 68 7d 7d 0a 0a 23 2d 2d 2d  mismatch}}..#---
4d90: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
4da0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
4db0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
4dc0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
4dd0: 2d 2d 2d 2d 2d 2d 0a 23 20 2f 2a 20 45 56 3a 20  ------.# /* EV: 
4de0: 52 2d 34 35 34 38 38 2d 30 38 35 30 34 20 2a 2f  R-45488-08504 */
4df0: 0a 23 20 2f 2a 20 45 56 3a 20 52 2d 34 38 33 39  .# /* EV: R-4839
4e00: 31 2d 33 38 34 37 32 20 2a 2f 0a 23 20 2f 2a 20  1-38472 */.# /* 
4e10: 45 56 3a 20 52 2d 30 33 31 30 38 2d 36 33 36 35  EV: R-03108-6365
4e20: 39 20 2a 2f 0a 23 20 2f 2a 20 45 56 3a 20 52 2d  9 */.# /* EV: R-
4e30: 36 30 37 38 31 2d 32 36 35 37 36 20 2a 2f 0a 23  60781-26576 */.#
4e40: 0a 23 20 54 65 73 74 20 65 72 72 6f 72 73 20 69  .# Test errors i
4e50: 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73  n the database s
4e60: 63 68 65 6d 61 20 74 68 61 74 20 61 72 65 20 64  chema that are d
4e70: 65 74 65 63 74 65 64 20 77 68 69 6c 65 20 70 72  etected while pr
4e80: 65 70 61 72 69 6e 67 0a 23 20 44 4d 4c 20 73 74  eparing.# DML st
4e90: 61 74 65 6d 65 6e 74 73 2e 20 54 68 65 20 65 72  atements. The er
4ea0: 72 6f 72 20 74 65 78 74 20 66 6f 72 20 74 68 65  ror text for the
4eb0: 73 65 20 6d 65 73 73 61 67 65 73 20 61 6c 77 61  se messages alwa
4ec0: 79 73 20 6d 61 74 63 68 65 73 20 0a 23 20 65 69  ys matches .# ei
4ed0: 74 68 65 72 20 22 66 6f 72 65 69 67 6e 20 6b 65  ther "foreign ke
4ee0: 79 20 6d 69 73 6d 61 74 63 68 22 20 6f 72 20 22  y mismatch" or "
4ef0: 6e 6f 20 73 75 63 68 20 74 61 62 6c 65 2a 22 20  no such table*" 
4f00: 28 75 73 69 6e 67 20 5b 73 74 72 69 6e 67 20 6d  (using [string m
4f10: 61 74 63 68 5d 29 2e 0a 23 0a 64 6f 5f 74 65 73  atch])..#.do_tes
4f20: 74 20 65 5f 66 6b 65 79 2d 32 30 2e 31 20 7b 0a  t e_fkey-20.1 {.
4f30: 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20    execsql {.    
4f40: 43 52 45 41 54 45 20 54 41 42 4c 45 20 63 31 28  CREATE TABLE c1(
4f50: 63 20 52 45 46 45 52 45 4e 43 45 53 20 6e 6f 73  c REFERENCES nos
4f60: 75 63 68 74 61 62 6c 65 2c 20 64 29 3b 0a 0a 20  uchtable, d);.. 
4f70: 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20     CREATE TABLE 
4f80: 70 32 28 61 2c 20 62 2c 20 55 4e 49 51 55 45 28  p2(a, b, UNIQUE(
4f90: 61 2c 20 62 29 29 3b 0a 20 20 20 20 43 52 45 41  a, b));.    CREA
4fa0: 54 45 20 54 41 42 4c 45 20 63 32 28 63 2c 20 64  TE TABLE c2(c, d
4fb0: 2c 20 46 4f 52 45 49 47 4e 20 4b 45 59 28 63 2c  , FOREIGN KEY(c,
4fc0: 20 64 29 20 52 45 46 45 52 45 4e 43 45 53 20 70   d) REFERENCES p
4fd0: 32 28 61 2c 20 78 29 29 3b 0a 0a 20 20 20 20 43  2(a, x));..    C
4fe0: 52 45 41 54 45 20 54 41 42 4c 45 20 70 33 28 61  REATE TABLE p3(a
4ff0: 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 62 29   PRIMARY KEY, b)
5000: 3b 0a 20 20 20 20 43 52 45 41 54 45 20 54 41 42  ;.    CREATE TAB
5010: 4c 45 20 63 33 28 63 20 52 45 46 45 52 45 4e 43  LE c3(c REFERENC
5020: 45 53 20 70 33 28 62 29 2c 20 64 29 3b 0a 0a 20  ES p3(b), d);.. 
5030: 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20     CREATE TABLE 
5040: 70 34 28 61 20 50 52 49 4d 41 52 59 20 4b 45 59  p4(a PRIMARY KEY
5050: 2c 20 62 29 3b 0a 20 20 20 20 43 52 45 41 54 45  , b);.    CREATE
5060: 20 55 4e 49 51 55 45 20 49 4e 44 45 58 20 70 34   UNIQUE INDEX p4
5070: 69 20 4f 4e 20 70 34 28 62 20 43 4f 4c 4c 41 54  i ON p4(b COLLAT
5080: 45 20 6e 6f 63 61 73 65 29 3b 0a 20 20 20 20 43  E nocase);.    C
5090: 52 45 41 54 45 20 54 41 42 4c 45 20 63 34 28 63  REATE TABLE c4(c
50a0: 20 52 45 46 45 52 45 4e 43 45 53 20 70 34 28 62   REFERENCES p4(b
50b0: 29 2c 20 64 29 3b 0a 0a 20 20 20 20 43 52 45 41  ), d);..    CREA
50c0: 54 45 20 54 41 42 4c 45 20 70 35 28 61 20 50 52  TE TABLE p5(a PR
50d0: 49 4d 41 52 59 20 4b 45 59 2c 20 62 20 43 4f 4c  IMARY KEY, b COL
50e0: 4c 41 54 45 20 6e 6f 63 61 73 65 29 3b 0a 20 20  LATE nocase);.  
50f0: 20 20 43 52 45 41 54 45 20 55 4e 49 51 55 45 20    CREATE UNIQUE 
5100: 49 4e 44 45 58 20 70 35 69 20 4f 4e 20 70 35 28  INDEX p5i ON p5(
5110: 62 20 43 4f 4c 4c 41 54 45 20 62 69 6e 61 72 79  b COLLATE binary
5120: 29 3b 0a 20 20 20 20 43 52 45 41 54 45 20 54 41  );.    CREATE TA
5130: 42 4c 45 20 63 35 28 63 20 52 45 46 45 52 45 4e  BLE c5(c REFEREN
5140: 43 45 53 20 70 35 28 62 29 2c 20 64 29 3b 0a 0a  CES p5(b), d);..
5150: 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45      CREATE TABLE
5160: 20 70 36 28 61 20 50 52 49 4d 41 52 59 20 4b 45   p6(a PRIMARY KE
5170: 59 2c 20 62 29 3b 0a 20 20 20 20 43 52 45 41 54  Y, b);.    CREAT
5180: 45 20 54 41 42 4c 45 20 63 36 28 63 2c 20 64 2c  E TABLE c6(c, d,
5190: 20 46 4f 52 45 49 47 4e 20 4b 45 59 28 63 2c 20   FOREIGN KEY(c, 
51a0: 64 29 20 52 45 46 45 52 45 4e 43 45 53 20 70 36  d) REFERENCES p6
51b0: 29 3b 0a 0a 20 20 20 20 43 52 45 41 54 45 20 54  );..    CREATE T
51c0: 41 42 4c 45 20 70 37 28 61 2c 20 62 2c 20 50 52  ABLE p7(a, b, PR
51d0: 49 4d 41 52 59 20 4b 45 59 28 61 2c 20 62 29 29  IMARY KEY(a, b))
51e0: 3b 0a 20 20 20 20 43 52 45 41 54 45 20 54 41 42  ;.    CREATE TAB
51f0: 4c 45 20 63 37 28 63 2c 20 64 20 52 45 46 45 52  LE c7(c, d REFER
5200: 45 4e 43 45 53 20 70 37 29 3b 0a 20 20 7d 0a 7d  ENCES p7);.  }.}
5210: 20 7b 7d 0a 0a 66 6f 72 65 61 63 68 20 7b 74 6e   {}..foreach {tn
5220: 20 74 62 6c 20 70 74 62 6c 20 65 72 72 7d 20 7b   tbl ptbl err} {
5230: 0a 20 20 32 20 63 31 20 7b 7d 20 22 6e 6f 20 73  .  2 c1 {} "no s
5240: 75 63 68 20 74 61 62 6c 65 3a 20 6d 61 69 6e 2e  uch table: main.
5250: 6e 6f 73 75 63 68 74 61 62 6c 65 22 0a 20 20 33  nosuchtable".  3
5260: 20 63 32 20 70 32 20 22 66 6f 72 65 69 67 6e 20   c2 p2 "foreign 
5270: 6b 65 79 20 6d 69 73 6d 61 74 63 68 22 0a 20 20  key mismatch".  
5280: 34 20 63 33 20 70 33 20 22 66 6f 72 65 69 67 6e  4 c3 p3 "foreign
5290: 20 6b 65 79 20 6d 69 73 6d 61 74 63 68 22 0a 20   key mismatch". 
52a0: 20 35 20 63 34 20 70 34 20 22 66 6f 72 65 69 67   5 c4 p4 "foreig
52b0: 6e 20 6b 65 79 20 6d 69 73 6d 61 74 63 68 22 0a  n key mismatch".
52c0: 20 20 36 20 63 35 20 70 35 20 22 66 6f 72 65 69    6 c5 p5 "forei
52d0: 67 6e 20 6b 65 79 20 6d 69 73 6d 61 74 63 68 22  gn key mismatch"
52e0: 0a 20 20 37 20 63 36 20 70 36 20 22 66 6f 72 65  .  7 c6 p6 "fore
52f0: 69 67 6e 20 6b 65 79 20 6d 69 73 6d 61 74 63 68  ign key mismatch
5300: 22 0a 20 20 38 20 63 37 20 70 37 20 22 66 6f 72  ".  8 c7 p7 "for
5310: 65 69 67 6e 20 6b 65 79 20 6d 69 73 6d 61 74 63  eign key mismatc
5320: 68 22 0a 7d 20 7b 0a 20 20 64 6f 5f 74 65 73 74  h".} {.  do_test
5330: 20 65 5f 66 6b 65 79 2d 32 30 2e 24 74 6e 2e 31   e_fkey-20.$tn.1
5340: 20 7b 0a 20 20 20 20 63 61 74 63 68 73 71 6c 20   {.    catchsql 
5350: 22 49 4e 53 45 52 54 20 49 4e 54 4f 20 24 74 62  "INSERT INTO $tb
5360: 6c 20 56 41 4c 55 45 53 28 27 61 27 2c 20 27 62  l VALUES('a', 'b
5370: 27 29 22 0a 20 20 7d 20 5b 6c 69 73 74 20 31 20  ')".  } [list 1 
5380: 24 65 72 72 5d 0a 20 20 64 6f 5f 74 65 73 74 20  $err].  do_test 
5390: 65 5f 66 6b 65 79 2d 32 30 2e 24 74 6e 2e 32 20  e_fkey-20.$tn.2 
53a0: 7b 0a 20 20 20 20 63 61 74 63 68 73 71 6c 20 22  {.    catchsql "
53b0: 55 50 44 41 54 45 20 24 74 62 6c 20 53 45 54 20  UPDATE $tbl SET 
53c0: 63 20 3d 20 3f 2c 20 64 20 3d 20 3f 22 0a 20 20  c = ?, d = ?".  
53d0: 7d 20 5b 6c 69 73 74 20 31 20 24 65 72 72 5d 0a  } [list 1 $err].
53e0: 20 20 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79    do_test e_fkey
53f0: 2d 32 30 2e 24 74 6e 2e 33 20 7b 0a 20 20 20 20  -20.$tn.3 {.    
5400: 63 61 74 63 68 73 71 6c 20 22 49 4e 53 45 52 54  catchsql "INSERT
5410: 20 49 4e 54 4f 20 24 74 62 6c 20 53 45 4c 45 43   INTO $tbl SELEC
5420: 54 20 3f 2c 20 3f 22 0a 20 20 7d 20 5b 6c 69 73  T ?, ?".  } [lis
5430: 74 20 31 20 24 65 72 72 5d 0a 0a 20 20 69 66 20  t 1 $err]..  if 
5440: 7b 24 70 74 62 6c 20 6e 65 20 22 22 7d 20 7b 0a  {$ptbl ne ""} {.
5450: 20 20 20 20 64 6f 5f 74 65 73 74 20 65 5f 66 6b      do_test e_fk
5460: 65 79 2d 32 30 2e 24 74 6e 2e 34 20 7b 0a 20 20  ey-20.$tn.4 {.  
5470: 20 20 20 20 63 61 74 63 68 73 71 6c 20 22 44 45      catchsql "DE
5480: 4c 45 54 45 20 46 52 4f 4d 20 24 70 74 62 6c 22  LETE FROM $ptbl"
5490: 0a 20 20 20 20 7d 20 5b 6c 69 73 74 20 31 20 24  .    } [list 1 $
54a0: 65 72 72 5d 0a 20 20 20 20 64 6f 5f 74 65 73 74  err].    do_test
54b0: 20 65 5f 66 6b 65 79 2d 32 30 2e 24 74 6e 2e 35   e_fkey-20.$tn.5
54c0: 20 7b 0a 20 20 20 20 20 20 63 61 74 63 68 73 71   {.      catchsq
54d0: 6c 20 22 55 50 44 41 54 45 20 24 70 74 62 6c 20  l "UPDATE $ptbl 
54e0: 53 45 54 20 61 20 3d 20 3f 2c 20 62 20 3d 20 3f  SET a = ?, b = ?
54f0: 22 0a 20 20 20 20 7d 20 5b 6c 69 73 74 20 31 20  ".    } [list 1 
5500: 24 65 72 72 5d 0a 20 20 20 20 64 6f 5f 74 65 73  $err].    do_tes
5510: 74 20 65 5f 66 6b 65 79 2d 32 30 2e 24 74 6e 2e  t e_fkey-20.$tn.
5520: 36 20 7b 0a 20 20 20 20 20 20 63 61 74 63 68 73  6 {.      catchs
5530: 71 6c 20 22 49 4e 53 45 52 54 20 49 4e 54 4f 20  ql "INSERT INTO 
5540: 24 70 74 62 6c 20 53 45 4c 45 43 54 20 3f 2c 20  $ptbl SELECT ?, 
5550: 3f 22 0a 20 20 20 20 7d 20 5b 6c 69 73 74 20 31  ?".    } [list 1
5560: 20 24 65 72 72 5d 0a 20 20 7d 0a 7d 0a 0a 23 2d   $err].  }.}..#-
5570: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
5580: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
5590: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
55a0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
55b0: 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20 2f 2a 20 45 56  --------.# /* EV
55c0: 3a 20 52 2d 31 39 33 35 33 2d 34 33 36 34 33 20  : R-19353-43643 
55d0: 2a 2f 0a 23 0a 23 20 54 65 73 74 20 74 68 65 20  */.#.# Test the 
55e0: 65 78 61 6d 70 6c 65 20 6f 66 20 66 6f 72 65 69  example of forei
55f0: 67 6e 20 6b 65 79 20 6d 69 73 6d 61 74 63 68 20  gn key mismatch 
5600: 65 72 72 6f 72 73 20 63 61 75 73 65 64 20 62 79  errors caused by
5610: 20 69 6d 70 6c 69 63 69 74 6c 79 0a 23 20 6d 61   implicitly.# ma
5620: 70 70 69 6e 67 20 61 20 63 68 69 6c 64 20 6b 65  pping a child ke
5630: 79 20 74 6f 20 74 68 65 20 70 72 69 6d 61 72 79  y to the primary
5640: 20 6b 65 79 20 6f 66 20 74 68 65 20 70 61 72 65   key of the pare
5650: 6e 74 20 74 61 62 6c 65 20 77 68 65 6e 20 74 68  nt table when th
5660: 65 0a 23 20 63 68 69 6c 64 20 6b 65 79 20 63 6f  e.# child key co
5670: 6e 73 69 73 74 73 20 6f 66 20 61 20 64 69 66 66  nsists of a diff
5680: 65 72 65 6e 74 20 6e 75 6d 62 65 72 20 6f 66 20  erent number of 
5690: 63 6f 6c 75 6d 6e 73 20 74 6f 20 74 68 61 74 20  columns to that 
56a0: 70 72 69 6d 61 72 79 20 6b 65 79 2e 0a 23 20 0a  primary key..# .
56b0: 64 72 6f 70 5f 61 6c 6c 5f 74 61 62 6c 65 73 0a  drop_all_tables.
56c0: 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 32  do_test e_fkey-2
56d0: 31 2e 31 20 7b 0a 20 20 65 78 65 63 73 71 6c 20  1.1 {.  execsql 
56e0: 7b 0a 20 20 20 20 43 52 45 41 54 45 20 54 41 42  {.    CREATE TAB
56f0: 4c 45 20 70 61 72 65 6e 74 32 28 61 2c 20 62 2c  LE parent2(a, b,
5700: 20 50 52 49 4d 41 52 59 20 4b 45 59 28 61 2c 62   PRIMARY KEY(a,b
5710: 29 29 3b 0a 0a 20 20 20 20 43 52 45 41 54 45 20  ));..    CREATE 
5720: 54 41 42 4c 45 20 63 68 69 6c 64 38 28 78 2c 20  TABLE child8(x, 
5730: 79 2c 20 46 4f 52 45 49 47 4e 20 4b 45 59 28 78  y, FOREIGN KEY(x
5740: 2c 79 29 20 52 45 46 45 52 45 4e 43 45 53 20 70  ,y) REFERENCES p
5750: 61 72 65 6e 74 32 29 3b 20 20 20 20 20 2d 2d 20  arent2);     -- 
5760: 4f 6b 0a 20 20 20 20 43 52 45 41 54 45 20 54 41  Ok.    CREATE TA
5770: 42 4c 45 20 63 68 69 6c 64 39 28 78 20 52 45 46  BLE child9(x REF
5780: 45 52 45 4e 43 45 53 20 70 61 72 65 6e 74 32 29  ERENCES parent2)
5790: 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
57a0: 20 20 20 20 20 20 20 20 20 20 20 2d 2d 20 45 72             -- Er
57b0: 72 0a 20 20 20 20 43 52 45 41 54 45 20 54 41 42  r.    CREATE TAB
57c0: 4c 45 20 63 68 69 6c 64 31 30 28 78 2c 79 2c 7a  LE child10(x,y,z
57d0: 2c 20 46 4f 52 45 49 47 4e 20 4b 45 59 28 78 2c  , FOREIGN KEY(x,
57e0: 79 2c 7a 29 20 52 45 46 45 52 45 4e 43 45 53 20  y,z) REFERENCES 
57f0: 70 61 72 65 6e 74 32 29 3b 20 2d 2d 20 45 72 72  parent2); -- Err
5800: 0a 20 20 7d 0a 7d 20 7b 7d 0a 64 6f 5f 74 65 73  .  }.} {}.do_tes
5810: 74 20 65 5f 66 6b 65 79 2d 32 31 2e 32 20 7b 0a  t e_fkey-21.2 {.
5820: 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20    execsql {.    
5830: 49 4e 53 45 52 54 20 49 4e 54 4f 20 70 61 72 65  INSERT INTO pare
5840: 6e 74 32 20 56 41 4c 55 45 53 28 27 49 27 2c 20  nt2 VALUES('I', 
5850: 27 49 49 27 29 3b 0a 20 20 20 20 49 4e 53 45 52  'II');.    INSER
5860: 54 20 49 4e 54 4f 20 63 68 69 6c 64 38 20 56 41  T INTO child8 VA
5870: 4c 55 45 53 28 27 49 27 2c 20 27 49 49 27 29 3b  LUES('I', 'II');
5880: 0a 20 20 7d 0a 7d 20 7b 7d 0a 64 6f 5f 74 65 73  .  }.} {}.do_tes
5890: 74 20 65 5f 66 6b 65 79 2d 32 31 2e 33 20 7b 0a  t e_fkey-21.3 {.
58a0: 20 20 63 61 74 63 68 73 71 6c 20 7b 20 49 4e 53    catchsql { INS
58b0: 45 52 54 20 49 4e 54 4f 20 63 68 69 6c 64 39 20  ERT INTO child9 
58c0: 56 41 4c 55 45 53 28 27 49 27 29 20 7d 0a 7d 20  VALUES('I') }.} 
58d0: 7b 31 20 7b 66 6f 72 65 69 67 6e 20 6b 65 79 20  {1 {foreign key 
58e0: 6d 69 73 6d 61 74 63 68 7d 7d 0a 64 6f 5f 74 65  mismatch}}.do_te
58f0: 73 74 20 65 5f 66 6b 65 79 2d 32 31 2e 34 20 7b  st e_fkey-21.4 {
5900: 0a 20 20 63 61 74 63 68 73 71 6c 20 7b 20 49 4e  .  catchsql { IN
5910: 53 45 52 54 20 49 4e 54 4f 20 63 68 69 6c 64 39  SERT INTO child9
5920: 20 56 41 4c 55 45 53 28 27 49 49 27 29 20 7d 0a   VALUES('II') }.
5930: 7d 20 7b 31 20 7b 66 6f 72 65 69 67 6e 20 6b 65  } {1 {foreign ke
5940: 79 20 6d 69 73 6d 61 74 63 68 7d 7d 0a 64 6f 5f  y mismatch}}.do_
5950: 74 65 73 74 20 65 5f 66 6b 65 79 2d 32 31 2e 35  test e_fkey-21.5
5960: 20 7b 0a 20 20 63 61 74 63 68 73 71 6c 20 7b 20   {.  catchsql { 
5970: 49 4e 53 45 52 54 20 49 4e 54 4f 20 63 68 69 6c  INSERT INTO chil
5980: 64 39 20 56 41 4c 55 45 53 28 4e 55 4c 4c 29 20  d9 VALUES(NULL) 
5990: 7d 0a 7d 20 7b 31 20 7b 66 6f 72 65 69 67 6e 20  }.} {1 {foreign 
59a0: 6b 65 79 20 6d 69 73 6d 61 74 63 68 7d 7d 0a 64  key mismatch}}.d
59b0: 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 32 31  o_test e_fkey-21
59c0: 2e 36 20 7b 0a 20 20 63 61 74 63 68 73 71 6c 20  .6 {.  catchsql 
59d0: 7b 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 63 68  { INSERT INTO ch
59e0: 69 6c 64 31 30 20 56 41 4c 55 45 53 28 27 49 27  ild10 VALUES('I'
59f0: 2c 20 27 49 49 27 2c 20 27 49 49 49 27 29 20 7d  , 'II', 'III') }
5a00: 0a 7d 20 7b 31 20 7b 66 6f 72 65 69 67 6e 20 6b  .} {1 {foreign k
5a10: 65 79 20 6d 69 73 6d 61 74 63 68 7d 7d 0a 64 6f  ey mismatch}}.do
5a20: 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 32 31 2e  _test e_fkey-21.
5a30: 37 20 7b 0a 20 20 63 61 74 63 68 73 71 6c 20 7b  7 {.  catchsql {
5a40: 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 63 68 69   INSERT INTO chi
5a50: 6c 64 31 30 20 56 41 4c 55 45 53 28 31 2c 20 32  ld10 VALUES(1, 2
5a60: 2c 20 33 29 20 7d 0a 7d 20 7b 31 20 7b 66 6f 72  , 3) }.} {1 {for
5a70: 65 69 67 6e 20 6b 65 79 20 6d 69 73 6d 61 74 63  eign key mismatc
5a80: 68 7d 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b  h}}.do_test e_fk
5a90: 65 79 2d 32 31 2e 38 20 7b 0a 20 20 63 61 74 63  ey-21.8 {.  catc
5aa0: 68 73 71 6c 20 7b 20 49 4e 53 45 52 54 20 49 4e  hsql { INSERT IN
5ab0: 54 4f 20 63 68 69 6c 64 31 30 20 56 41 4c 55 45  TO child10 VALUE
5ac0: 53 28 4e 55 4c 4c 2c 20 4e 55 4c 4c 2c 20 4e 55  S(NULL, NULL, NU
5ad0: 4c 4c 29 20 7d 0a 7d 20 7b 31 20 7b 66 6f 72 65  LL) }.} {1 {fore
5ae0: 69 67 6e 20 6b 65 79 20 6d 69 73 6d 61 74 63 68  ign key mismatch
5af0: 7d 7d 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  }}..#-----------
5b00: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
5b10: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
5b20: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
5b30: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23  --------------.#
5b40: 20 2f 2a 20 45 56 3a 20 52 2d 32 33 36 38 32 2d   /* EV: R-23682-
5b50: 35 39 38 32 30 20 2a 2f 0a 23 0a 23 20 54 65 73  59820 */.#.# Tes
5b60: 74 20 65 72 72 6f 72 73 20 74 68 61 74 20 61 72  t errors that ar
5b70: 65 20 72 65 70 6f 72 74 65 64 20 77 68 65 6e 20  e reported when 
5b80: 63 72 65 61 74 69 6e 67 20 74 68 65 20 63 68 69  creating the chi
5b90: 6c 64 20 74 61 62 6c 65 2e 20 0a 23 20 53 70 65  ld table. .# Spe
5ba0: 63 69 66 69 63 61 6c 6c 79 3a 0a 23 0a 23 20 20  cifically:.#.#  
5bb0: 20 2a 20 64 69 66 66 65 72 65 6e 74 20 6e 75 6d   * different num
5bc0: 62 65 72 20 6f 66 20 63 68 69 6c 64 20 61 6e 64  ber of child and
5bd0: 20 70 61 72 65 6e 74 20 6b 65 79 20 63 6f 6c 75   parent key colu
5be0: 6d 6e 73 2c 20 61 6e 64 0a 23 20 20 20 2a 20 63  mns, and.#   * c
5bf0: 68 69 6c 64 20 63 6f 6c 75 6d 6e 73 20 74 68 61  hild columns tha
5c00: 74 20 64 6f 20 6e 6f 74 20 65 78 69 73 74 2e 0a  t do not exist..
5c10: 23 0a 23 20 2f 2a 20 45 56 3a 20 52 2d 33 33 38  #.# /* EV: R-338
5c20: 38 33 2d 32 38 38 33 33 20 2a 2f 0a 23 0a 23 20  83-28833 */.#.# 
5c30: 54 68 65 73 65 20 65 72 72 6f 72 73 20 61 72 65  These errors are
5c40: 20 72 65 70 6f 72 74 65 64 20 77 68 65 74 68 65   reported whethe
5c50: 72 20 6f 72 20 6e 6f 74 20 46 4b 20 73 75 70 70  r or not FK supp
5c60: 6f 72 74 20 69 73 20 65 6e 61 62 6c 65 64 2e 0a  ort is enabled..
5c70: 23 0a 64 72 6f 70 5f 61 6c 6c 5f 74 61 62 6c 65  #.drop_all_table
5c80: 73 0a 66 6f 72 65 61 63 68 20 66 6b 20 5b 6c 69  s.foreach fk [li
5c90: 73 74 20 4f 46 46 20 4f 4e 5d 20 7b 0a 20 20 65  st OFF ON] {.  e
5ca0: 78 65 63 73 71 6c 20 22 50 52 41 47 4d 41 20 66  xecsql "PRAGMA f
5cb0: 6f 72 65 69 67 6e 5f 6b 65 79 73 20 3d 20 24 66  oreign_keys = $f
5cc0: 6b 22 0a 20 20 73 65 74 20 69 20 30 0a 20 20 66  k".  set i 0.  f
5cd0: 6f 72 65 61 63 68 20 7b 73 71 6c 20 65 72 72 6f  oreach {sql erro
5ce0: 72 7d 20 7b 0a 20 20 20 20 22 43 52 45 41 54 45  r} {.    "CREATE
5cf0: 20 54 41 42 4c 45 20 63 68 69 6c 64 31 28 61 2c   TABLE child1(a,
5d00: 20 62 2c 20 46 4f 52 45 49 47 4e 20 4b 45 59 28   b, FOREIGN KEY(
5d10: 61 2c 20 62 29 20 52 45 46 45 52 45 4e 43 45 53  a, b) REFERENCES
5d20: 20 70 28 63 29 29 22 0a 20 20 20 20 20 20 7b 6e   p(c))".      {n
5d30: 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73  umber of columns
5d40: 20 69 6e 20 66 6f 72 65 69 67 6e 20 6b 65 79 20   in foreign key 
5d50: 64 6f 65 73 20 6e 6f 74 20 6d 61 74 63 68 20 74  does not match t
5d60: 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c  he number of col
5d70: 75 6d 6e 73 20 69 6e 20 74 68 65 20 72 65 66 65  umns in the refe
5d80: 72 65 6e 63 65 64 20 74 61 62 6c 65 7d 0a 20 20  renced table}.  
5d90: 20 20 22 43 52 45 41 54 45 20 54 41 42 4c 45 20    "CREATE TABLE 
5da0: 63 68 69 6c 64 32 28 61 2c 20 62 2c 20 46 4f 52  child2(a, b, FOR
5db0: 45 49 47 4e 20 4b 45 59 28 61 2c 20 62 29 20 52  EIGN KEY(a, b) R
5dc0: 45 46 45 52 45 4e 43 45 53 20 70 28 63 2c 20 64  EFERENCES p(c, d
5dd0: 2c 20 65 29 29 22 0a 20 20 20 20 20 20 7b 6e 75  , e))".      {nu
5de0: 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20  mber of columns 
5df0: 69 6e 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 64  in foreign key d
5e00: 6f 65 73 20 6e 6f 74 20 6d 61 74 63 68 20 74 68  oes not match th
5e10: 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75  e number of colu
5e20: 6d 6e 73 20 69 6e 20 74 68 65 20 72 65 66 65 72  mns in the refer
5e30: 65 6e 63 65 64 20 74 61 62 6c 65 7d 0a 20 20 20  enced table}.   
5e40: 20 22 43 52 45 41 54 45 20 54 41 42 4c 45 20 63   "CREATE TABLE c
5e50: 68 69 6c 64 32 28 61 2c 20 62 2c 20 46 4f 52 45  hild2(a, b, FORE
5e60: 49 47 4e 20 4b 45 59 28 61 2c 20 63 29 20 52 45  IGN KEY(a, c) RE
5e70: 46 45 52 45 4e 43 45 53 20 70 28 63 2c 20 64 29  FERENCES p(c, d)
5e80: 29 22 0a 20 20 20 20 20 20 7b 75 6e 6b 6e 6f 77  )".      {unknow
5e90: 6e 20 63 6f 6c 75 6d 6e 20 22 63 22 20 69 6e 20  n column "c" in 
5ea0: 66 6f 72 65 69 67 6e 20 6b 65 79 20 64 65 66 69  foreign key defi
5eb0: 6e 69 74 69 6f 6e 7d 0a 20 20 20 20 22 43 52 45  nition}.    "CRE
5ec0: 41 54 45 20 54 41 42 4c 45 20 63 68 69 6c 64 32  ATE TABLE child2
5ed0: 28 61 2c 20 62 2c 20 46 4f 52 45 49 47 4e 20 4b  (a, b, FOREIGN K
5ee0: 45 59 28 63 2c 20 62 29 20 52 45 46 45 52 45 4e  EY(c, b) REFEREN
5ef0: 43 45 53 20 70 28 63 2c 20 64 29 29 22 0a 20 20  CES p(c, d))".  
5f00: 20 20 20 20 7b 75 6e 6b 6e 6f 77 6e 20 63 6f 6c      {unknown col
5f10: 75 6d 6e 20 22 63 22 20 69 6e 20 66 6f 72 65 69  umn "c" in forei
5f20: 67 6e 20 6b 65 79 20 64 65 66 69 6e 69 74 69 6f  gn key definitio
5f30: 6e 7d 0a 20 20 7d 20 7b 0a 20 20 20 20 64 6f 5f  n}.  } {.    do_
5f40: 74 65 73 74 20 65 5f 66 6b 65 79 2d 32 32 2e 24  test e_fkey-22.$
5f50: 66 6b 2e 5b 69 6e 63 72 20 69 5d 20 7b 0a 20 20  fk.[incr i] {.  
5f60: 20 20 20 20 63 61 74 63 68 73 71 6c 20 24 73 71      catchsql $sq
5f70: 6c 0a 20 20 20 20 7d 20 5b 6c 69 73 74 20 31 20  l.    } [list 1 
5f80: 24 65 72 72 6f 72 5d 0a 20 20 7d 0a 7d 0a 0a 23  $error].  }.}..#
5f90: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
5fa0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
5fb0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
5fc0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
5fd0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20 2f 2a 20 45  ---------.# /* E
5fe0: 56 3a 20 52 2d 34 37 31 30 39 2d 34 30 35 38 31  V: R-47109-40581
5ff0: 20 2a 2f 0a 23 0a 23 20 54 65 73 74 20 74 68 61   */.#.# Test tha
6000: 74 20 61 20 52 45 46 45 52 45 4e 43 49 4e 47 20  t a REFERENCING 
6010: 63 6c 61 75 73 65 20 74 68 61 74 20 64 6f 65 73  clause that does
6020: 20 6e 6f 74 20 73 70 65 63 69 66 79 20 70 61 72   not specify par
6030: 65 6e 74 20 6b 65 79 20 63 6f 6c 75 6d 6e 73 0a  ent key columns.
6040: 23 20 69 6d 70 6c 69 63 69 74 6c 79 20 6d 61 70  # implicitly map
6050: 73 20 74 6f 20 74 68 65 20 70 72 69 6d 61 72 79  s to the primary
6060: 20 6b 65 79 20 6f 66 20 74 68 65 20 70 61 72 65   key of the pare
6070: 6e 74 20 74 61 62 6c 65 2e 0a 23 20 0a 64 6f 5f  nt table..# .do_
6080: 74 65 73 74 20 65 5f 66 6b 65 79 2d 32 33 2e 31  test e_fkey-23.1
6090: 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20   {.  execsql {. 
60a0: 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20     CREATE TABLE 
60b0: 70 31 28 61 2c 20 62 2c 20 50 52 49 4d 41 52 59  p1(a, b, PRIMARY
60c0: 20 4b 45 59 28 61 2c 20 62 29 29 3b 0a 20 20 20   KEY(a, b));.   
60d0: 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 70 32   CREATE TABLE p2
60e0: 28 61 2c 20 62 20 50 52 49 4d 41 52 59 20 4b 45  (a, b PRIMARY KE
60f0: 59 29 3b 0a 20 20 20 20 43 52 45 41 54 45 20 54  Y);.    CREATE T
6100: 41 42 4c 45 20 63 31 28 63 2c 20 64 2c 20 46 4f  ABLE c1(c, d, FO
6110: 52 45 49 47 4e 20 4b 45 59 28 63 2c 20 64 29 20  REIGN KEY(c, d) 
6120: 52 45 46 45 52 45 4e 43 45 53 20 70 31 29 3b 0a  REFERENCES p1);.
6130: 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45      CREATE TABLE
6140: 20 63 32 28 61 2c 20 62 20 52 45 46 45 52 45 4e   c2(a, b REFEREN
6150: 43 45 53 20 70 32 29 3b 0a 20 20 7d 0a 7d 20 7b  CES p2);.  }.} {
6160: 7d 0a 70 72 6f 63 20 74 65 73 74 5f 65 66 6b 65  }.proc test_efke
6170: 79 5f 36 30 20 7b 74 6e 20 69 73 45 72 72 6f 72  y_60 {tn isError
6180: 20 73 71 6c 7d 20 7b 0a 20 20 64 6f 5f 74 65 73   sql} {.  do_tes
6190: 74 20 65 5f 66 6b 65 79 2d 32 33 2e 24 74 6e 20  t e_fkey-23.$tn 
61a0: 22 0a 20 20 20 20 63 61 74 63 68 73 71 6c 20 7b  ".    catchsql {
61b0: 24 73 71 6c 7d 0a 20 20 22 20 5b 6c 69 6e 64 65  $sql}.  " [linde
61c0: 78 20 7b 7b 30 20 7b 7d 7d 20 7b 31 20 7b 66 6f  x {{0 {}} {1 {fo
61d0: 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72  reign key constr
61e0: 61 69 6e 74 20 66 61 69 6c 65 64 7d 7d 7d 20 24  aint failed}}} $
61f0: 69 73 45 72 72 6f 72 5d 0a 7d 0a 0a 74 65 73 74  isError].}..test
6200: 5f 65 66 6b 65 79 5f 36 30 20 32 20 31 20 22 49  _efkey_60 2 1 "I
6210: 4e 53 45 52 54 20 49 4e 54 4f 20 63 31 20 56 41  NSERT INTO c1 VA
6220: 4c 55 45 53 28 32 33 39 2c 20 32 33 31 29 22 0a  LUES(239, 231)".
6230: 74 65 73 74 5f 65 66 6b 65 79 5f 36 30 20 33 20  test_efkey_60 3 
6240: 30 20 22 49 4e 53 45 52 54 20 49 4e 54 4f 20 70  0 "INSERT INTO p
6250: 31 20 56 41 4c 55 45 53 28 32 33 39 2c 20 32 33  1 VALUES(239, 23
6260: 31 29 22 0a 74 65 73 74 5f 65 66 6b 65 79 5f 36  1)".test_efkey_6
6270: 30 20 34 20 30 20 22 49 4e 53 45 52 54 20 49 4e  0 4 0 "INSERT IN
6280: 54 4f 20 63 31 20 56 41 4c 55 45 53 28 32 33 39  TO c1 VALUES(239
6290: 2c 20 32 33 31 29 22 0a 74 65 73 74 5f 65 66 6b  , 231)".test_efk
62a0: 65 79 5f 36 30 20 35 20 31 20 22 49 4e 53 45 52  ey_60 5 1 "INSER
62b0: 54 20 49 4e 54 4f 20 63 32 20 56 41 4c 55 45 53  T INTO c2 VALUES
62c0: 28 32 33 39 2c 20 32 33 31 29 22 0a 74 65 73 74  (239, 231)".test
62d0: 5f 65 66 6b 65 79 5f 36 30 20 36 20 30 20 22 49  _efkey_60 6 0 "I
62e0: 4e 53 45 52 54 20 49 4e 54 4f 20 70 32 20 56 41  NSERT INTO p2 VA
62f0: 4c 55 45 53 28 32 33 39 2c 20 32 33 31 29 22 0a  LUES(239, 231)".
6300: 74 65 73 74 5f 65 66 6b 65 79 5f 36 30 20 37 20  test_efkey_60 7 
6310: 30 20 22 49 4e 53 45 52 54 20 49 4e 54 4f 20 63  0 "INSERT INTO c
6320: 32 20 56 41 4c 55 45 53 28 32 33 39 2c 20 32 33  2 VALUES(239, 23
6330: 31 29 22 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  1)"..#----------
6340: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
6350: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
6360: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
6370: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a  ---------------.
6380: 23 20 2f 2a 20 45 56 3a 20 52 2d 31 35 34 31 37  # /* EV: R-15417
6390: 2d 32 38 30 31 34 20 2a 2f 0a 23 0a 23 20 54 65  -28014 */.#.# Te
63a0: 73 74 20 74 68 61 74 20 61 6e 20 69 6e 64 65 78  st that an index
63b0: 20 6f 6e 20 6f 6e 20 74 68 65 20 63 68 69 6c 64   on on the child
63c0: 20 6b 65 79 20 63 6f 6c 75 6d 6e 73 20 6f 66 20   key columns of 
63d0: 61 6e 20 46 4b 20 63 6f 6e 73 74 72 61 69 6e 74  an FK constraint
63e0: 0a 23 20 69 73 20 6f 70 74 69 6f 6e 61 6c 2e 0a  .# is optional..
63f0: 23 0a 23 20 2f 2a 20 45 56 3a 20 52 2d 31 35 37  #.# /* EV: R-157
6400: 34 31 2d 35 30 38 39 33 20 2a 2f 0a 23 0a 23 20  41-50893 */.#.# 
6410: 41 6c 73 6f 20 74 65 73 74 20 74 68 61 74 20 69  Also test that i
6420: 66 20 61 6e 20 69 6e 64 65 78 20 69 73 20 63 72  f an index is cr
6430: 65 61 74 65 64 20 6f 6e 20 74 68 65 20 63 68 69  eated on the chi
6440: 6c 64 20 6b 65 79 20 63 6f 6c 75 6d 6e 73 2c 20  ld key columns, 
6450: 69 74 20 64 6f 65 73 0a 23 20 6e 6f 74 20 6d 61  it does.# not ma
6460: 6b 65 20 61 20 64 69 66 66 65 72 65 6e 63 65 20  ke a difference 
6470: 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 69  whether or not i
6480: 74 20 69 73 20 61 20 55 4e 49 51 55 45 20 69 6e  t is a UNIQUE in
6490: 64 65 78 2e 0a 23 0a 64 72 6f 70 5f 61 6c 6c 5f  dex..#.drop_all_
64a0: 74 61 62 6c 65 73 0a 64 6f 5f 74 65 73 74 20 65  tables.do_test e
64b0: 5f 66 6b 65 79 2d 32 34 2e 31 20 7b 0a 20 20 65  _fkey-24.1 {.  e
64c0: 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 43 52 45  xecsql {.    CRE
64d0: 41 54 45 20 54 41 42 4c 45 20 70 61 72 65 6e 74  ATE TABLE parent
64e0: 28 78 2c 20 79 2c 20 55 4e 49 51 55 45 28 79 2c  (x, y, UNIQUE(y,
64f0: 20 78 29 29 3b 0a 20 20 20 20 43 52 45 41 54 45   x));.    CREATE
6500: 20 54 41 42 4c 45 20 63 31 28 61 2c 20 62 2c 20   TABLE c1(a, b, 
6510: 46 4f 52 45 49 47 4e 20 4b 45 59 28 61 2c 20 62  FOREIGN KEY(a, b
6520: 29 20 52 45 46 45 52 45 4e 43 45 53 20 70 61 72  ) REFERENCES par
6530: 65 6e 74 28 78 2c 20 79 29 29 3b 0a 20 20 20 20  ent(x, y));.    
6540: 43 52 45 41 54 45 20 54 41 42 4c 45 20 63 32 28  CREATE TABLE c2(
6550: 61 2c 20 62 2c 20 46 4f 52 45 49 47 4e 20 4b 45  a, b, FOREIGN KE
6560: 59 28 61 2c 20 62 29 20 52 45 46 45 52 45 4e 43  Y(a, b) REFERENC
6570: 45 53 20 70 61 72 65 6e 74 28 78 2c 20 79 29 29  ES parent(x, y))
6580: 3b 0a 20 20 20 20 43 52 45 41 54 45 20 54 41 42  ;.    CREATE TAB
6590: 4c 45 20 63 33 28 61 2c 20 62 2c 20 46 4f 52 45  LE c3(a, b, FORE
65a0: 49 47 4e 20 4b 45 59 28 61 2c 20 62 29 20 52 45  IGN KEY(a, b) RE
65b0: 46 45 52 45 4e 43 45 53 20 70 61 72 65 6e 74 28  FERENCES parent(
65c0: 78 2c 20 79 29 29 3b 0a 20 20 20 20 43 52 45 41  x, y));.    CREA
65d0: 54 45 20 49 4e 44 45 58 20 63 32 69 20 4f 4e 20  TE INDEX c2i ON 
65e0: 63 32 28 61 2c 20 62 29 3b 0a 20 20 20 20 43 52  c2(a, b);.    CR
65f0: 45 41 54 45 20 55 4e 49 51 55 45 20 49 4e 44 45  EATE UNIQUE INDE
6600: 58 20 63 33 69 20 4f 4e 20 63 32 28 62 2c 20 61  X c3i ON c2(b, a
6610: 29 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a 70 72 6f 63  );.  }.} {}.proc
6620: 20 74 65 73 74 5f 65 66 6b 65 79 5f 36 31 20 7b   test_efkey_61 {
6630: 74 6e 20 69 73 45 72 72 6f 72 20 73 71 6c 7d 20  tn isError sql} 
6640: 7b 0a 20 20 64 6f 5f 74 65 73 74 20 65 5f 66 6b  {.  do_test e_fk
6650: 65 79 2d 32 34 2e 24 74 6e 20 22 0a 20 20 20 20  ey-24.$tn ".    
6660: 63 61 74 63 68 73 71 6c 20 7b 24 73 71 6c 7d 0a  catchsql {$sql}.
6670: 20 20 22 20 5b 6c 69 6e 64 65 78 20 7b 7b 30 20    " [lindex {{0 
6680: 7b 7d 7d 20 7b 31 20 7b 66 6f 72 65 69 67 6e 20  {}} {1 {foreign 
6690: 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 20 66  key constraint f
66a0: 61 69 6c 65 64 7d 7d 7d 20 24 69 73 45 72 72 6f  ailed}}} $isErro
66b0: 72 5d 0a 7d 0a 66 6f 72 65 61 63 68 20 7b 74 6e  r].}.foreach {tn
66c0: 20 63 7d 20 5b 6c 69 73 74 20 32 20 63 31 20 33   c} [list 2 c1 3
66d0: 20 63 32 20 34 20 63 33 5d 20 7b 0a 20 20 74 65   c2 4 c3] {.  te
66e0: 73 74 5f 65 66 6b 65 79 5f 36 31 20 24 74 6e 2e  st_efkey_61 $tn.
66f0: 31 20 31 20 22 49 4e 53 45 52 54 20 49 4e 54 4f  1 1 "INSERT INTO
6700: 20 24 63 20 56 41 4c 55 45 53 28 31 2c 20 32 29   $c VALUES(1, 2)
6710: 22 0a 20 20 74 65 73 74 5f 65 66 6b 65 79 5f 36  ".  test_efkey_6
6720: 31 20 24 74 6e 2e 32 20 30 20 22 49 4e 53 45 52  1 $tn.2 0 "INSER
6730: 54 20 49 4e 54 4f 20 70 61 72 65 6e 74 20 56 41  T INTO parent VA
6740: 4c 55 45 53 28 31 2c 20 32 29 22 0a 20 20 74 65  LUES(1, 2)".  te
6750: 73 74 5f 65 66 6b 65 79 5f 36 31 20 24 74 6e 2e  st_efkey_61 $tn.
6760: 33 20 30 20 22 49 4e 53 45 52 54 20 49 4e 54 4f  3 0 "INSERT INTO
6770: 20 24 63 20 56 41 4c 55 45 53 28 31 2c 20 32 29   $c VALUES(1, 2)
6780: 22 0a 0a 20 20 65 78 65 63 73 71 6c 20 22 44 45  "..  execsql "DE
6790: 4c 45 54 45 20 46 52 4f 4d 20 24 63 20 3b 20 44  LETE FROM $c ; D
67a0: 45 4c 45 54 45 20 46 52 4f 4d 20 70 61 72 65 6e  ELETE FROM paren
67b0: 74 22 0a 7d 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d  t".}..#---------
67c0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
67d0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
67e0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
67f0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
6800: 0a 23 20 2f 2a 20 45 56 3a 20 52 2d 30 30 32 37  .# /* EV: R-0027
6810: 39 2d 35 32 32 38 33 20 2a 2f 0a 23 0a 23 20 54  9-52283 */.#.# T
6820: 65 73 74 20 61 6e 20 65 78 61 6d 70 6c 65 20 73  est an example s
6830: 68 6f 77 69 6e 67 20 74 68 61 74 20 77 68 65 6e  howing that when
6840: 20 61 20 72 6f 77 20 69 73 20 64 65 6c 65 74 65   a row is delete
6850: 64 20 66 72 6f 6d 20 74 68 65 20 70 61 72 65 6e  d from the paren
6860: 74 20 0a 23 20 74 61 62 6c 65 2c 20 74 68 65 20  t .# table, the 
6870: 63 68 69 6c 64 20 74 61 62 6c 65 20 69 73 20 71  child table is q
6880: 75 65 72 69 65 64 20 66 6f 72 20 6f 72 70 68 61  ueried for orpha
6890: 6e 65 64 20 72 6f 77 73 20 61 73 20 66 6f 6c 6c  ned rows as foll
68a0: 6f 77 73 3a 0a 23 0a 23 20 20 20 53 45 4c 45 43  ows:.#.#   SELEC
68b0: 54 20 72 6f 77 69 64 20 46 52 4f 4d 20 74 72 61  T rowid FROM tra
68c0: 63 6b 20 57 48 45 52 45 20 74 72 61 63 6b 61 72  ck WHERE trackar
68d0: 74 69 73 74 20 3d 20 3f 0a 23 0a 23 20 2f 2a 20  tist = ?.#.# /* 
68e0: 45 56 3a 20 52 2d 32 33 33 30 32 2d 33 30 39 35  EV: R-23302-3095
68f0: 36 20 2a 2f 0a 23 0a 23 20 41 6c 73 6f 20 74 65  6 */.#.# Also te
6900: 73 74 20 74 68 61 74 20 69 66 20 74 68 65 20 53  st that if the S
6910: 45 4c 45 43 54 20 61 62 6f 76 65 20 77 6f 75 6c  ELECT above woul
6920: 64 20 72 65 74 75 72 6e 20 61 6e 79 20 72 6f 77  d return any row
6930: 73 2c 20 61 20 66 6f 72 65 69 67 6e 0a 23 20 6b  s, a foreign.# k
6940: 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 20 69 73  ey constraint is
6950: 20 76 69 6f 6c 61 74 65 64 2e 0a 23 0a 64 6f 5f   violated..#.do_
6960: 74 65 73 74 20 65 5f 66 6b 65 79 2d 32 35 2e 31  test e_fkey-25.1
6970: 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20   {.  execsql {. 
6980: 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20     CREATE TABLE 
6990: 61 72 74 69 73 74 28 0a 20 20 20 20 20 20 61 72  artist(.      ar
69a0: 74 69 73 74 69 64 20 20 20 20 49 4e 54 45 47 45  tistid    INTEGE
69b0: 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 0a  R PRIMARY KEY, .
69c0: 20 20 20 20 20 20 61 72 74 69 73 74 6e 61 6d 65        artistname
69d0: 20 20 54 45 58 54 0a 20 20 20 20 29 3b 0a 20 20    TEXT.    );.  
69e0: 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 74    CREATE TABLE t
69f0: 72 61 63 6b 28 0a 20 20 20 20 20 20 74 72 61 63  rack(.      trac
6a00: 6b 69 64 20 20 20 20 20 49 4e 54 45 47 45 52 2c  kid     INTEGER,
6a10: 20 0a 20 20 20 20 20 20 74 72 61 63 6b 6e 61 6d   .      tracknam
6a20: 65 20 20 20 54 45 58 54 2c 20 0a 20 20 20 20 20  e   TEXT, .     
6a30: 20 74 72 61 63 6b 61 72 74 69 73 74 20 49 4e 54   trackartist INT
6a40: 45 47 45 52 2c 0a 20 20 20 20 20 20 46 4f 52 45  EGER,.      FORE
6a50: 49 47 4e 20 4b 45 59 28 74 72 61 63 6b 61 72 74  IGN KEY(trackart
6a60: 69 73 74 29 20 52 45 46 45 52 45 4e 43 45 53 20  ist) REFERENCES 
6a70: 61 72 74 69 73 74 28 61 72 74 69 73 74 69 64 29  artist(artistid)
6a80: 0a 20 20 20 20 29 3b 0a 20 20 7d 0a 7d 20 7b 7d  .    );.  }.} {}
6a90: 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d  .do_test e_fkey-
6aa0: 32 35 2e 32 20 7b 0a 20 20 65 78 65 63 73 71 6c  25.2 {.  execsql
6ab0: 20 7b 0a 20 20 20 20 50 52 41 47 4d 41 20 66 6f   {.    PRAGMA fo
6ac0: 72 65 69 67 6e 5f 6b 65 79 73 20 3d 20 4f 46 46  reign_keys = OFF
6ad0: 3b 0a 20 20 20 20 45 58 50 4c 41 49 4e 20 51 55  ;.    EXPLAIN QU
6ae0: 45 52 59 20 50 4c 41 4e 20 44 45 4c 45 54 45 20  ERY PLAN DELETE 
6af0: 46 52 4f 4d 20 61 72 74 69 73 74 20 57 48 45 52  FROM artist WHER
6b00: 45 20 31 3b 0a 20 20 20 20 45 58 50 4c 41 49 4e  E 1;.    EXPLAIN
6b10: 20 51 55 45 52 59 20 50 4c 41 4e 20 53 45 4c 45   QUERY PLAN SELE
6b20: 43 54 20 72 6f 77 69 64 20 46 52 4f 4d 20 74 72  CT rowid FROM tr
6b30: 61 63 6b 20 57 48 45 52 45 20 74 72 61 63 6b 61  ack WHERE tracka
6b40: 72 74 69 73 74 20 3d 20 3f 3b 0a 20 20 7d 0a 7d  rtist = ?;.  }.}
6b50: 20 7b 30 20 30 20 7b 54 41 42 4c 45 20 61 72 74   {0 0 {TABLE art
6b60: 69 73 74 7d 20 30 20 30 20 7b 54 41 42 4c 45 20  ist} 0 0 {TABLE 
6b70: 74 72 61 63 6b 7d 7d 0a 64 6f 5f 74 65 73 74 20  track}}.do_test 
6b80: 65 5f 66 6b 65 79 2d 32 35 2e 33 20 7b 0a 20 20  e_fkey-25.3 {.  
6b90: 65 78 65 63 73 71 6c 20 7b 20 0a 20 20 20 20 50  execsql { .    P
6ba0: 52 41 47 4d 41 20 66 6f 72 65 69 67 6e 5f 6b 65  RAGMA foreign_ke
6bb0: 79 73 20 3d 20 4f 4e 3b 0a 20 20 20 20 45 58 50  ys = ON;.    EXP
6bc0: 4c 41 49 4e 20 51 55 45 52 59 20 50 4c 41 4e 20  LAIN QUERY PLAN 
6bd0: 44 45 4c 45 54 45 20 46 52 4f 4d 20 61 72 74 69  DELETE FROM arti
6be0: 73 74 20 57 48 45 52 45 20 31 3b 0a 20 20 7d 0a  st WHERE 1;.  }.
6bf0: 7d 20 7b 30 20 30 20 7b 54 41 42 4c 45 20 61 72  } {0 0 {TABLE ar
6c00: 74 69 73 74 7d 20 30 20 30 20 7b 54 41 42 4c 45  tist} 0 0 {TABLE
6c10: 20 74 72 61 63 6b 7d 7d 0a 64 6f 5f 74 65 73 74   track}}.do_test
6c20: 20 65 5f 66 6b 65 79 2d 32 35 2e 34 20 7b 0a 20   e_fkey-25.4 {. 
6c30: 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 49   execsql {.    I
6c40: 4e 53 45 52 54 20 49 4e 54 4f 20 61 72 74 69 73  NSERT INTO artis
6c50: 74 20 56 41 4c 55 45 53 28 35 2c 20 27 61 72 74  t VALUES(5, 'art
6c60: 69 73 74 20 35 27 29 3b 0a 20 20 20 20 49 4e 53  ist 5');.    INS
6c70: 45 52 54 20 49 4e 54 4f 20 61 72 74 69 73 74 20  ERT INTO artist 
6c80: 56 41 4c 55 45 53 28 36 2c 20 27 61 72 74 69 73  VALUES(6, 'artis
6c90: 74 20 36 27 29 3b 0a 20 20 20 20 49 4e 53 45 52  t 6');.    INSER
6ca0: 54 20 49 4e 54 4f 20 61 72 74 69 73 74 20 56 41  T INTO artist VA
6cb0: 4c 55 45 53 28 37 2c 20 27 61 72 74 69 73 74 20  LUES(7, 'artist 
6cc0: 37 27 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20  7');.    INSERT 
6cd0: 49 4e 54 4f 20 74 72 61 63 6b 20 56 41 4c 55 45  INTO track VALUE
6ce0: 53 28 31 2c 20 27 74 72 61 63 6b 20 31 27 2c 20  S(1, 'track 1', 
6cf0: 35 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49  5);.    INSERT I
6d00: 4e 54 4f 20 74 72 61 63 6b 20 56 41 4c 55 45 53  NTO track VALUES
6d10: 28 32 2c 20 27 74 72 61 63 6b 20 32 27 2c 20 36  (2, 'track 2', 6
6d20: 29 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a 0a 64 6f 5f  );.  }.} {}..do_
6d30: 74 65 73 74 20 65 5f 66 6b 65 79 2d 32 35 2e 35  test e_fkey-25.5
6d40: 20 7b 0a 20 20 63 6f 6e 63 61 74 20 5c 0a 20 20   {.  concat \.  
6d50: 20 20 5b 65 78 65 63 73 71 6c 20 7b 20 53 45 4c    [execsql { SEL
6d60: 45 43 54 20 72 6f 77 69 64 20 46 52 4f 4d 20 74  ECT rowid FROM t
6d70: 72 61 63 6b 20 57 48 45 52 45 20 74 72 61 63 6b  rack WHERE track
6d80: 61 72 74 69 73 74 20 3d 20 35 20 7d 5d 20 20 20  artist = 5 }]   
6d90: 5c 0a 20 20 20 20 5b 63 61 74 63 68 73 71 6c 20  \.    [catchsql 
6da0: 7b 20 44 45 4c 45 54 45 20 46 52 4f 4d 20 61 72  { DELETE FROM ar
6db0: 74 69 73 74 20 57 48 45 52 45 20 61 72 74 69 73  tist WHERE artis
6dc0: 74 69 64 20 3d 20 35 20 7d 5d 0a 7d 20 7b 31 20  tid = 5 }].} {1 
6dd0: 31 20 7b 66 6f 72 65 69 67 6e 20 6b 65 79 20 63  1 {foreign key c
6de0: 6f 6e 73 74 72 61 69 6e 74 20 66 61 69 6c 65 64  onstraint failed
6df0: 7d 7d 0a 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b  }}..do_test e_fk
6e00: 65 79 2d 32 35 2e 36 20 7b 0a 20 20 63 6f 6e 63  ey-25.6 {.  conc
6e10: 61 74 20 5c 0a 20 20 20 20 5b 65 78 65 63 73 71  at \.    [execsq
6e20: 6c 20 7b 20 53 45 4c 45 43 54 20 72 6f 77 69 64  l { SELECT rowid
6e30: 20 46 52 4f 4d 20 74 72 61 63 6b 20 57 48 45 52   FROM track WHER
6e40: 45 20 74 72 61 63 6b 61 72 74 69 73 74 20 3d 20  E trackartist = 
6e50: 37 20 7d 5d 20 20 20 5c 0a 20 20 20 20 5b 63 61  7 }]   \.    [ca
6e60: 74 63 68 73 71 6c 20 7b 20 44 45 4c 45 54 45 20  tchsql { DELETE 
6e70: 46 52 4f 4d 20 61 72 74 69 73 74 20 57 48 45 52  FROM artist WHER
6e80: 45 20 61 72 74 69 73 74 69 64 20 3d 20 37 20 7d  E artistid = 7 }
6e90: 5d 0a 7d 20 7b 30 20 7b 7d 7d 0a 0a 64 6f 5f 74  ].} {0 {}}..do_t
6ea0: 65 73 74 20 65 5f 66 6b 65 79 2d 32 35 2e 37 20  est e_fkey-25.7 
6eb0: 7b 0a 20 20 63 6f 6e 63 61 74 20 5c 0a 20 20 20  {.  concat \.   
6ec0: 20 5b 65 78 65 63 73 71 6c 20 7b 20 53 45 4c 45   [execsql { SELE
6ed0: 43 54 20 72 6f 77 69 64 20 46 52 4f 4d 20 74 72  CT rowid FROM tr
6ee0: 61 63 6b 20 57 48 45 52 45 20 74 72 61 63 6b 61  ack WHERE tracka
6ef0: 72 74 69 73 74 20 3d 20 36 20 7d 5d 20 20 20 5c  rtist = 6 }]   \
6f00: 0a 20 20 20 20 5b 63 61 74 63 68 73 71 6c 20 7b  .    [catchsql {
6f10: 20 44 45 4c 45 54 45 20 46 52 4f 4d 20 61 72 74   DELETE FROM art
6f20: 69 73 74 20 57 48 45 52 45 20 61 72 74 69 73 74  ist WHERE artist
6f30: 69 64 20 3d 20 36 20 7d 5d 0a 7d 20 7b 32 20 31  id = 6 }].} {2 1
6f40: 20 7b 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f   {foreign key co
6f50: 6e 73 74 72 61 69 6e 74 20 66 61 69 6c 65 64 7d  nstraint failed}
6f60: 7d 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  }..#------------
6f70: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
6f80: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
6f90: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
6fa0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20  -------------.# 
6fb0: 2f 2a 20 45 56 3a 20 52 2d 35 34 31 37 32 2d 35  /* EV: R-54172-5
6fc0: 35 38 34 38 20 2a 2f 0a 23 0a 23 20 54 65 73 74  5848 */.#.# Test
6fd0: 20 74 68 61 74 20 77 68 65 6e 20 61 20 72 6f 77   that when a row
6fe0: 20 69 73 20 64 65 6c 65 74 65 64 20 66 72 6f 6d   is deleted from
6ff0: 20 74 68 65 20 70 61 72 65 6e 74 20 74 61 62 6c   the parent tabl
7000: 65 20 6f 66 20 61 6e 20 46 4b 20 0a 23 20 63 6f  e of an FK .# co
7010: 6e 73 74 72 61 69 6e 74 2c 20 74 68 65 20 63 68  nstraint, the ch
7020: 69 6c 64 20 74 61 62 6c 65 20 69 73 20 71 75 65  ild table is que
7030: 72 69 65 64 20 66 6f 72 20 6f 72 70 68 61 6e 65  ried for orphane
7040: 64 20 72 6f 77 73 2e 20 54 68 65 0a 23 20 71 75  d rows. The.# qu
7050: 65 72 79 20 69 73 20 65 71 75 69 76 61 6c 65 6e  ery is equivalen
7060: 74 20 74 6f 3a 0a 23 0a 23 20 20 20 53 45 4c 45  t to:.#.#   SELE
7070: 43 54 20 72 6f 77 69 64 20 46 52 4f 4d 20 3c 63  CT rowid FROM <c
7080: 68 69 6c 64 2d 74 61 62 6c 65 3e 20 57 48 45 52  hild-table> WHER
7090: 45 20 3c 63 68 69 6c 64 2d 6b 65 79 3e 20 3d 20  E <child-key> = 
70a0: 3a 70 61 72 65 6e 74 5f 6b 65 79 5f 76 61 6c 75  :parent_key_valu
70b0: 65 0a 23 0a 23 20 2f 2a 20 45 56 3a 20 52 2d 36  e.#.# /* EV: R-6
70c0: 31 36 31 36 2d 34 36 37 30 30 20 2a 2f 0a 23 0a  1616-46700 */.#.
70d0: 23 20 41 6c 73 6f 20 74 65 73 74 20 74 68 61 74  # Also test that
70e0: 20 77 68 65 6e 20 61 20 72 6f 77 20 69 73 20 69   when a row is i
70f0: 6e 73 65 72 74 65 64 20 69 6e 74 6f 20 74 68 65  nserted into the
7100: 20 70 61 72 65 6e 74 20 74 61 62 6c 65 2c 20 6f   parent table, o
7110: 72 20 77 68 65 6e 20 74 68 65 20 0a 23 20 70 61  r when the .# pa
7120: 72 65 6e 74 20 6b 65 79 20 76 61 6c 75 65 73 20  rent key values 
7130: 6f 66 20 61 6e 20 65 78 69 73 74 69 6e 67 20 72  of an existing r
7140: 6f 77 20 61 72 65 20 6d 6f 64 69 66 69 65 64 2c  ow are modified,
7150: 20 61 20 71 75 65 72 79 20 65 71 75 69 76 61 6c   a query equival
7160: 65 6e 74 0a 23 20 74 6f 20 74 68 65 20 66 6f 6c  ent.# to the fol
7170: 6c 6f 77 69 6e 67 20 69 73 20 70 6c 61 6e 6e 65  lowing is planne
7180: 64 2e 20 49 6e 20 73 6f 6d 65 20 63 61 73 65 73  d. In some cases
7190: 20 69 74 20 69 73 20 6e 6f 74 20 65 78 65 63 75   it is not execu
71a0: 74 65 64 2c 20 62 75 74 20 69 74 0a 23 20 69 73  ted, but it.# is
71b0: 20 61 6c 77 61 79 73 20 70 6c 61 6e 6e 65 64 2e   always planned.
71c0: 0a 23 0a 23 20 20 20 53 45 4c 45 43 54 20 72 6f  .#.#   SELECT ro
71d0: 77 69 64 20 46 52 4f 4d 20 3c 63 68 69 6c 64 2d  wid FROM <child-
71e0: 74 61 62 6c 65 3e 20 57 48 45 52 45 20 3c 63 68  table> WHERE <ch
71f0: 69 6c 64 2d 6b 65 79 3e 20 3d 20 3a 70 61 72 65  ild-key> = :pare
7200: 6e 74 5f 6b 65 79 5f 76 61 6c 75 65 0a 23 0a 23  nt_key_value.#.#
7210: 0a 64 72 6f 70 5f 61 6c 6c 5f 74 61 62 6c 65 73  .drop_all_tables
7220: 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d  .do_test e_fkey-
7230: 32 36 2e 31 20 7b 0a 20 20 65 78 65 63 73 71 6c  26.1 {.  execsql
7240: 20 7b 20 43 52 45 41 54 45 20 54 41 42 4c 45 20   { CREATE TABLE 
7250: 70 61 72 65 6e 74 28 78 2c 20 79 2c 20 55 4e 49  parent(x, y, UNI
7260: 51 55 45 28 79 2c 20 78 29 29 20 7d 0a 7d 20 7b  QUE(y, x)) }.} {
7270: 7d 0a 66 6f 72 65 61 63 68 20 7b 74 6e 20 73 71  }.foreach {tn sq
7280: 6c 7d 20 7b 0a 20 20 32 20 7b 20 0a 20 20 20 20  l} {.  2 { .    
7290: 43 52 45 41 54 45 20 54 41 42 4c 45 20 63 68 69  CREATE TABLE chi
72a0: 6c 64 28 61 2c 20 62 2c 20 46 4f 52 45 49 47 4e  ld(a, b, FOREIGN
72b0: 20 4b 45 59 28 61 2c 20 62 29 20 52 45 46 45 52   KEY(a, b) REFER
72c0: 45 4e 43 45 53 20 70 61 72 65 6e 74 28 78 2c 20  ENCES parent(x, 
72d0: 79 29 29 0a 20 20 7d 0a 20 20 33 20 7b 20 0a 20  y)).  }.  3 { . 
72e0: 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20     CREATE TABLE 
72f0: 63 68 69 6c 64 28 61 2c 20 62 2c 20 46 4f 52 45  child(a, b, FORE
7300: 49 47 4e 20 4b 45 59 28 61 2c 20 62 29 20 52 45  IGN KEY(a, b) RE
7310: 46 45 52 45 4e 43 45 53 20 70 61 72 65 6e 74 28  FERENCES parent(
7320: 78 2c 20 79 29 29 3b 0a 20 20 20 20 43 52 45 41  x, y));.    CREA
7330: 54 45 20 49 4e 44 45 58 20 63 68 69 6c 64 69 20  TE INDEX childi 
7340: 4f 4e 20 63 68 69 6c 64 28 61 2c 20 62 29 3b 0a  ON child(a, b);.
7350: 20 20 7d 0a 20 20 34 20 7b 20 0a 20 20 20 20 43    }.  4 { .    C
7360: 52 45 41 54 45 20 54 41 42 4c 45 20 63 68 69 6c  REATE TABLE chil
7370: 64 28 61 2c 20 62 2c 20 46 4f 52 45 49 47 4e 20  d(a, b, FOREIGN 
7380: 4b 45 59 28 61 2c 20 62 29 20 52 45 46 45 52 45  KEY(a, b) REFERE
7390: 4e 43 45 53 20 70 61 72 65 6e 74 28 78 2c 20 79  NCES parent(x, y
73a0: 29 29 3b 0a 20 20 20 20 43 52 45 41 54 45 20 55  ));.    CREATE U
73b0: 4e 49 51 55 45 20 49 4e 44 45 58 20 63 68 69 6c  NIQUE INDEX chil
73c0: 64 69 20 4f 4e 20 63 68 69 6c 64 28 62 2c 20 61  di ON child(b, a
73d0: 29 3b 0a 20 20 7d 0a 7d 20 7b 0a 20 20 65 78 65  );.  }.} {.  exe
73e0: 63 73 71 6c 20 24 73 71 6c 0a 0a 20 20 65 78 65  csql $sql..  exe
73f0: 63 73 71 6c 20 7b 50 52 41 47 4d 41 20 66 6f 72  csql {PRAGMA for
7400: 65 69 67 6e 5f 6b 65 79 73 20 3d 20 4f 46 46 7d  eign_keys = OFF}
7410: 0a 20 20 73 65 74 20 64 65 6c 65 74 65 20 5b 63  .  set delete [c
7420: 6f 6e 63 61 74 20 5c 0a 20 20 20 20 20 20 5b 65  oncat \.      [e
7430: 71 70 20 22 44 45 4c 45 54 45 20 46 52 4f 4d 20  qp "DELETE FROM 
7440: 70 61 72 65 6e 74 20 57 48 45 52 45 20 31 22 5d  parent WHERE 1"]
7450: 20 5c 0a 20 20 20 20 20 20 5b 65 71 70 20 22 53   \.      [eqp "S
7460: 45 4c 45 43 54 20 72 6f 77 69 64 20 46 52 4f 4d  ELECT rowid FROM
7470: 20 63 68 69 6c 64 20 57 48 45 52 45 20 61 20 3d   child WHERE a =
7480: 20 3f 20 41 4e 44 20 62 20 3d 20 3f 22 5d 0a 20   ? AND b = ?"]. 
7490: 20 5d 0a 20 20 73 65 74 20 75 70 64 61 74 65 20   ].  set update 
74a0: 5b 63 6f 6e 63 61 74 20 5c 0a 20 20 20 20 20 20  [concat \.      
74b0: 5b 65 71 70 20 22 55 50 44 41 54 45 20 70 61 72  [eqp "UPDATE par
74c0: 65 6e 74 20 53 45 54 20 78 3d 3f 2c 20 79 3d 3f  ent SET x=?, y=?
74d0: 22 5d 20 5c 0a 20 20 20 20 20 20 5b 65 71 70 20  "] \.      [eqp 
74e0: 22 53 45 4c 45 43 54 20 72 6f 77 69 64 20 46 52  "SELECT rowid FR
74f0: 4f 4d 20 63 68 69 6c 64 20 57 48 45 52 45 20 61  OM child WHERE a
7500: 20 3d 20 3f 20 41 4e 44 20 62 20 3d 20 3f 22 5d   = ? AND b = ?"]
7510: 20 5c 0a 20 20 20 20 20 20 5b 65 71 70 20 22 53   \.      [eqp "S
7520: 45 4c 45 43 54 20 72 6f 77 69 64 20 46 52 4f 4d  ELECT rowid FROM
7530: 20 63 68 69 6c 64 20 57 48 45 52 45 20 61 20 3d   child WHERE a =
7540: 20 3f 20 41 4e 44 20 62 20 3d 20 3f 22 5d 0a 20   ? AND b = ?"]. 
7550: 20 5d 0a 20 20 65 78 65 63 73 71 6c 20 7b 50 52   ].  execsql {PR
7560: 41 47 4d 41 20 66 6f 72 65 69 67 6e 5f 6b 65 79  AGMA foreign_key
7570: 73 20 3d 20 4f 4e 7d 0a 0a 20 20 64 6f 5f 74 65  s = ON}..  do_te
7580: 73 74 20 65 5f 66 6b 65 79 2d 32 36 2e 24 74 6e  st e_fkey-26.$tn
7590: 2e 31 20 7b 20 65 71 70 20 22 44 45 4c 45 54 45  .1 { eqp "DELETE
75a0: 20 46 52 4f 4d 20 70 61 72 65 6e 74 20 57 48 45   FROM parent WHE
75b0: 52 45 20 31 22 20 7d 20 24 64 65 6c 65 74 65 0a  RE 1" } $delete.
75c0: 20 20 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79    do_test e_fkey
75d0: 2d 32 36 2e 24 74 6e 2e 32 20 7b 20 65 71 70 20  -26.$tn.2 { eqp 
75e0: 22 55 50 44 41 54 45 20 70 61 72 65 6e 74 20 73  "UPDATE parent s
75f0: 65 74 20 78 3d 3f 2c 20 79 3d 3f 22 20 7d 20 24  et x=?, y=?" } $
7600: 75 70 64 61 74 65 0a 0a 20 20 65 78 65 63 73 71  update..  execsq
7610: 6c 20 7b 44 52 4f 50 20 54 41 42 4c 45 20 63 68  l {DROP TABLE ch
7620: 69 6c 64 7d 0a 7d 0a 0a 23 2d 2d 2d 2d 2d 2d 2d  ild}.}..#-------
7630: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
7640: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
7650: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
7660: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
7670: 2d 2d 0a 23 20 2f 2a 20 45 56 3a 20 52 2d 31 34  --.# /* EV: R-14
7680: 35 35 33 2d 33 34 30 31 33 20 2a 2f 0a 23 0a 23  553-34013 */.#.#
7690: 20 54 65 73 74 20 74 68 65 20 65 78 61 6d 70 6c   Test the exampl
76a0: 65 20 73 63 68 65 6d 61 20 61 74 20 74 68 65 20  e schema at the 
76b0: 65 6e 64 20 6f 66 20 73 65 63 74 69 6f 6e 20 33  end of section 3
76c0: 2e 20 41 6c 73 6f 20 74 65 73 74 20 74 68 61 74  . Also test that
76d0: 20 69 73 0a 23 20 69 73 20 22 65 66 66 69 63 69   is.# is "effici
76e0: 65 6e 74 22 2e 20 49 6e 20 74 68 69 73 20 63 61  ent". In this ca
76f0: 73 65 20 22 65 66 66 69 63 69 65 6e 74 22 20 6d  se "efficient" m
7700: 65 61 6e 73 20 74 68 61 74 20 66 6f 72 65 69 67  eans that foreig
7710: 6e 20 6b 65 79 0a 23 20 72 65 6c 61 74 65 64 20  n key.# related 
7720: 6f 70 65 72 61 74 69 6f 6e 73 20 6f 6e 20 74 68  operations on th
7730: 65 20 70 61 72 65 6e 74 20 74 61 62 6c 65 20 64  e parent table d
7740: 6f 20 6e 6f 74 20 70 72 6f 76 6f 6b 65 20 6c 69  o not provoke li
7750: 6e 65 61 72 20 73 63 61 6e 73 2e 0a 23 0a 64 72  near scans..#.dr
7760: 6f 70 5f 61 6c 6c 5f 74 61 62 6c 65 73 0a 64 6f  op_all_tables.do
7770: 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 32 37 2e  _test e_fkey-27.
7780: 31 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a  1 {.  execsql {.
7790: 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45      CREATE TABLE
77a0: 20 61 72 74 69 73 74 28 0a 20 20 20 20 20 20 61   artist(.      a
77b0: 72 74 69 73 74 69 64 20 20 20 20 49 4e 54 45 47  rtistid    INTEG
77c0: 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20  ER PRIMARY KEY, 
77d0: 0a 20 20 20 20 20 20 61 72 74 69 73 74 6e 61 6d  .      artistnam
77e0: 65 20 20 54 45 58 54 0a 20 20 20 20 29 3b 0a 20  e  TEXT.    );. 
77f0: 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20     CREATE TABLE 
7800: 74 72 61 63 6b 28 0a 20 20 20 20 20 20 74 72 61  track(.      tra
7810: 63 6b 69 64 20 20 20 20 20 49 4e 54 45 47 45 52  ckid     INTEGER
7820: 2c 0a 20 20 20 20 20 20 74 72 61 63 6b 6e 61 6d  ,.      tracknam
7830: 65 20 20 20 54 45 58 54 2c 20 0a 20 20 20 20 20  e   TEXT, .     
7840: 20 74 72 61 63 6b 61 72 74 69 73 74 20 49 4e 54   trackartist INT
7850: 45 47 45 52 20 52 45 46 45 52 45 4e 43 45 53 20  EGER REFERENCES 
7860: 61 72 74 69 73 74 0a 20 20 20 20 29 3b 0a 20 20  artist.    );.  
7870: 20 20 43 52 45 41 54 45 20 49 4e 44 45 58 20 74    CREATE INDEX t
7880: 72 61 63 6b 69 6e 64 65 78 20 4f 4e 20 74 72 61  rackindex ON tra
7890: 63 6b 28 74 72 61 63 6b 61 72 74 69 73 74 29 3b  ck(trackartist);
78a0: 0a 20 20 7d 0a 7d 20 7b 7d 0a 64 6f 5f 74 65 73  .  }.} {}.do_tes
78b0: 74 20 65 5f 66 6b 65 79 2d 32 37 2e 32 20 7b 0a  t e_fkey-27.2 {.
78c0: 20 20 65 71 70 20 7b 20 49 4e 53 45 52 54 20 49    eqp { INSERT I
78d0: 4e 54 4f 20 61 72 74 69 73 74 20 56 41 4c 55 45  NTO artist VALUE
78e0: 53 28 3f 2c 20 3f 29 20 7d 0a 7d 20 7b 7d 0a 64  S(?, ?) }.} {}.d
78f0: 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 32 37  o_test e_fkey-27
7900: 2e 33 20 7b 0a 20 20 65 71 70 20 7b 20 55 50 44  .3 {.  eqp { UPD
7910: 41 54 45 20 61 72 74 69 73 74 20 53 45 54 20 61  ATE artist SET a
7920: 72 74 69 73 74 69 64 20 3d 20 3f 2c 20 61 72 74  rtistid = ?, art
7930: 69 73 74 6e 61 6d 65 20 3d 20 3f 20 7d 0a 7d 20  istname = ? }.} 
7940: 5b 6c 69 73 74 20 5c 0a 20 20 30 20 30 20 7b 54  [list \.  0 0 {T
7950: 41 42 4c 45 20 61 72 74 69 73 74 7d 20 5c 0a 20  ABLE artist} \. 
7960: 20 30 20 30 20 7b 54 41 42 4c 45 20 74 72 61 63   0 0 {TABLE trac
7970: 6b 20 57 49 54 48 20 49 4e 44 45 58 20 74 72 61  k WITH INDEX tra
7980: 63 6b 69 6e 64 65 78 7d 20 5c 0a 20 20 30 20 30  ckindex} \.  0 0
7990: 20 7b 54 41 42 4c 45 20 74 72 61 63 6b 20 57 49   {TABLE track WI
79a0: 54 48 20 49 4e 44 45 58 20 74 72 61 63 6b 69 6e  TH INDEX trackin
79b0: 64 65 78 7d 0a 5d 0a 64 6f 5f 74 65 73 74 20 65  dex}.].do_test e
79c0: 5f 66 6b 65 79 2d 32 37 2e 34 20 7b 0a 20 20 65  _fkey-27.4 {.  e
79d0: 71 70 20 7b 20 44 45 4c 45 54 45 20 46 52 4f 4d  qp { DELETE FROM
79e0: 20 61 72 74 69 73 74 20 7d 0a 7d 20 5b 6c 69 73   artist }.} [lis
79f0: 74 20 5c 0a 20 20 30 20 30 20 7b 54 41 42 4c 45  t \.  0 0 {TABLE
7a00: 20 61 72 74 69 73 74 7d 20 5c 0a 20 20 30 20 30   artist} \.  0 0
7a10: 20 7b 54 41 42 4c 45 20 74 72 61 63 6b 20 57 49   {TABLE track WI
7a20: 54 48 20 49 4e 44 45 58 20 74 72 61 63 6b 69 6e  TH INDEX trackin
7a30: 64 65 78 7d 0a 5d 0a 0a 0a 23 23 23 23 23 23 23  dex}.]...#######
7a40: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
7a50: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
7a60: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
7a70: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
7a80: 23 23 23 23 0a 23 23 23 20 53 45 43 54 49 4f 4e  ####.### SECTION
7a90: 20 34 2e 31 3a 20 43 6f 6d 70 6f 73 69 74 65 20   4.1: Composite 
7aa0: 46 6f 72 65 69 67 6e 20 4b 65 79 20 43 6f 6e 73  Foreign Key Cons
7ab0: 74 72 61 69 6e 74 73 0a 23 23 23 23 23 23 23 23  traints.########
7ac0: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
7ad0: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
7ae0: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
7af0: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
7b00: 23 23 23 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ###..#----------
7b10: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
7b20: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
7b30: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
7b40: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a  ---------------.
7b50: 23 20 2f 2a 20 45 56 3a 20 52 2d 34 31 30 36 32  # /* EV: R-41062
7b60: 2d 33 34 34 33 31 20 2a 2f 0a 23 0a 23 20 43 68  -34431 */.#.# Ch
7b70: 65 63 6b 20 74 68 61 74 20 70 61 72 65 6e 74 20  eck that parent 
7b80: 61 6e 64 20 63 68 69 6c 64 20 6b 65 79 73 20 6d  and child keys m
7b90: 75 73 74 20 68 61 76 65 20 74 68 65 20 73 61 6d  ust have the sam
7ba0: 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75  e number of colu
7bb0: 6d 6e 73 2e 0a 23 0a 66 6f 72 65 61 63 68 20 7b  mns..#.foreach {
7bc0: 74 6e 20 73 71 6c 20 65 72 72 7d 20 7b 0a 20 20  tn sql err} {.  
7bd0: 31 20 22 43 52 45 41 54 45 20 54 41 42 4c 45 20  1 "CREATE TABLE 
7be0: 63 28 6a 6a 20 52 45 46 45 52 45 4e 43 45 53 20  c(jj REFERENCES 
7bf0: 70 28 78 2c 20 79 29 29 22 20 0a 20 20 20 20 7b  p(x, y))" .    {
7c00: 66 6f 72 65 69 67 6e 20 6b 65 79 20 6f 6e 20 6a  foreign key on j
7c10: 6a 20 73 68 6f 75 6c 64 20 72 65 66 65 72 65 6e  j should referen
7c20: 63 65 20 6f 6e 6c 79 20 6f 6e 65 20 63 6f 6c 75  ce only one colu
7c30: 6d 6e 20 6f 66 20 74 61 62 6c 65 20 70 7d 0a 0a  mn of table p}..
7c40: 20 20 32 20 22 43 52 45 41 54 45 20 54 41 42 4c    2 "CREATE TABL
7c50: 45 20 63 28 6a 6a 20 52 45 46 45 52 45 4e 43 45  E c(jj REFERENCE
7c60: 53 20 70 28 29 29 22 20 7b 6e 65 61 72 20 22 29  S p())" {near ")
7c70: 22 3a 20 73 79 6e 74 61 78 20 65 72 72 6f 72 7d  ": syntax error}
7c80: 0a 0a 20 20 33 20 22 43 52 45 41 54 45 20 54 41  ..  3 "CREATE TA
7c90: 42 4c 45 20 63 28 6a 6a 2c 20 46 4f 52 45 49 47  BLE c(jj, FOREIG
7ca0: 4e 20 4b 45 59 28 6a 6a 29 20 52 45 46 45 52 45  N KEY(jj) REFERE
7cb0: 4e 43 45 53 20 70 28 78 2c 20 79 29 29 22 20 0a  NCES p(x, y))" .
7cc0: 20 20 20 20 7b 6e 75 6d 62 65 72 20 6f 66 20 63      {number of c
7cd0: 6f 6c 75 6d 6e 73 20 69 6e 20 66 6f 72 65 69 67  olumns in foreig
7ce0: 6e 20 6b 65 79 20 64 6f 65 73 20 6e 6f 74 20 6d  n key does not m
7cf0: 61 74 63 68 20 74 68 65 20 6e 75 6d 62 65 72 20  atch the number 
7d00: 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68  of columns in th
7d10: 65 20 72 65 66 65 72 65 6e 63 65 64 20 74 61 62  e referenced tab
7d20: 6c 65 7d 0a 0a 20 20 34 20 22 43 52 45 41 54 45  le}..  4 "CREATE
7d30: 20 54 41 42 4c 45 20 63 28 6a 6a 2c 20 46 4f 52   TABLE c(jj, FOR
7d40: 45 49 47 4e 20 4b 45 59 28 6a 6a 29 20 52 45 46  EIGN KEY(jj) REF
7d50: 45 52 45 4e 43 45 53 20 70 28 29 29 22 20 0a 20  ERENCES p())" . 
7d60: 20 20 20 7b 6e 65 61 72 20 22 29 22 3a 20 73 79     {near ")": sy
7d70: 6e 74 61 78 20 65 72 72 6f 72 7d 0a 0a 20 20 35  ntax error}..  5
7d80: 20 22 43 52 45 41 54 45 20 54 41 42 4c 45 20 63   "CREATE TABLE c
7d90: 28 69 69 2c 20 6a 6a 2c 20 46 4f 52 45 49 47 4e  (ii, jj, FOREIGN
7da0: 20 4b 45 59 28 6a 6a 2c 20 69 69 29 20 52 45 46   KEY(jj, ii) REF
7db0: 45 52 45 4e 43 45 53 20 70 28 29 29 22 20 0a 20  ERENCES p())" . 
7dc0: 20 20 20 7b 6e 65 61 72 20 22 29 22 3a 20 73 79     {near ")": sy
7dd0: 6e 74 61 78 20 65 72 72 6f 72 7d 0a 0a 20 20 36  ntax error}..  6
7de0: 20 22 43 52 45 41 54 45 20 54 41 42 4c 45 20 63   "CREATE TABLE c
7df0: 28 69 69 2c 20 6a 6a 2c 20 46 4f 52 45 49 47 4e  (ii, jj, FOREIGN
7e00: 20 4b 45 59 28 6a 6a 2c 20 69 69 29 20 52 45 46   KEY(jj, ii) REF
7e10: 45 52 45 4e 43 45 53 20 70 28 78 29 29 22 20 0a  ERENCES p(x))" .
7e20: 20 20 20 20 7b 6e 75 6d 62 65 72 20 6f 66 20 63      {number of c
7e30: 6f 6c 75 6d 6e 73 20 69 6e 20 66 6f 72 65 69 67  olumns in foreig
7e40: 6e 20 6b 65 79 20 64 6f 65 73 20 6e 6f 74 20 6d  n key does not m
7e50: 61 74 63 68 20 74 68 65 20 6e 75 6d 62 65 72 20  atch the number 
7e60: 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68  of columns in th
7e70: 65 20 72 65 66 65 72 65 6e 63 65 64 20 74 61 62  e referenced tab
7e80: 6c 65 7d 0a 0a 20 20 37 20 22 43 52 45 41 54 45  le}..  7 "CREATE
7e90: 20 54 41 42 4c 45 20 63 28 69 69 2c 20 6a 6a 2c   TABLE c(ii, jj,
7ea0: 20 46 4f 52 45 49 47 4e 20 4b 45 59 28 6a 6a 2c   FOREIGN KEY(jj,
7eb0: 20 69 69 29 20 52 45 46 45 52 45 4e 43 45 53 20   ii) REFERENCES 
7ec0: 70 28 78 2c 79 2c 7a 29 29 22 20 0a 20 20 20 20  p(x,y,z))" .    
7ed0: 7b 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d  {number of colum
7ee0: 6e 73 20 69 6e 20 66 6f 72 65 69 67 6e 20 6b 65  ns in foreign ke
7ef0: 79 20 64 6f 65 73 20 6e 6f 74 20 6d 61 74 63 68  y does not match
7f00: 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63   the number of c
7f10: 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 72 65  olumns in the re
7f20: 66 65 72 65 6e 63 65 64 20 74 61 62 6c 65 7d 0a  ferenced table}.
7f30: 7d 20 7b 0a 20 20 64 72 6f 70 5f 61 6c 6c 5f 74  } {.  drop_all_t
7f40: 61 62 6c 65 73 0a 20 20 64 6f 5f 74 65 73 74 20  ables.  do_test 
7f50: 65 5f 66 6b 65 79 2d 32 38 2e 24 74 6e 20 5b 6c  e_fkey-28.$tn [l
7f60: 69 73 74 20 63 61 74 63 68 73 71 6c 20 24 73 71  ist catchsql $sq
7f70: 6c 5d 20 5b 6c 69 73 74 20 31 20 24 65 72 72 5d  l] [list 1 $err]
7f80: 0a 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65  .}.do_test e_fke
7f90: 79 2d 32 38 2e 38 20 7b 0a 20 20 64 72 6f 70 5f  y-28.8 {.  drop_
7fa0: 61 6c 6c 5f 74 61 62 6c 65 73 0a 20 20 65 78 65  all_tables.  exe
7fb0: 63 73 71 6c 20 7b 0a 20 20 20 20 43 52 45 41 54  csql {.    CREAT
7fc0: 45 20 54 41 42 4c 45 20 70 28 78 20 50 52 49 4d  E TABLE p(x PRIM
7fd0: 41 52 59 20 4b 45 59 29 3b 0a 20 20 20 20 43 52  ARY KEY);.    CR
7fe0: 45 41 54 45 20 54 41 42 4c 45 20 63 28 61 2c 20  EATE TABLE c(a, 
7ff0: 62 2c 20 46 4f 52 45 49 47 4e 20 4b 45 59 28 61  b, FOREIGN KEY(a
8000: 2c 62 29 20 52 45 46 45 52 45 4e 43 45 53 20 70  ,b) REFERENCES p
8010: 29 3b 0a 20 20 7d 0a 20 20 63 61 74 63 68 73 71  );.  }.  catchsq
8020: 6c 20 7b 44 45 4c 45 54 45 20 46 52 4f 4d 20 70  l {DELETE FROM p
8030: 7d 0a 7d 20 7b 31 20 7b 66 6f 72 65 69 67 6e 20  }.} {1 {foreign 
8040: 6b 65 79 20 6d 69 73 6d 61 74 63 68 7d 7d 0a 64  key mismatch}}.d
8050: 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 32 38  o_test e_fkey-28
8060: 2e 39 20 7b 0a 20 20 64 72 6f 70 5f 61 6c 6c 5f  .9 {.  drop_all_
8070: 74 61 62 6c 65 73 0a 20 20 65 78 65 63 73 71 6c  tables.  execsql
8080: 20 7b 0a 20 20 20 20 43 52 45 41 54 45 20 54 41   {.    CREATE TA
8090: 42 4c 45 20 70 28 78 2c 20 79 2c 20 50 52 49 4d  BLE p(x, y, PRIM
80a0: 41 52 59 20 4b 45 59 28 78 2c 79 29 29 3b 0a 20  ARY KEY(x,y));. 
80b0: 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20     CREATE TABLE 
80c0: 63 28 61 20 52 45 46 45 52 45 4e 43 45 53 20 70  c(a REFERENCES p
80d0: 29 3b 0a 20 20 7d 0a 20 20 63 61 74 63 68 73 71  );.  }.  catchsq
80e0: 6c 20 7b 44 45 4c 45 54 45 20 46 52 4f 4d 20 70  l {DELETE FROM p
80f0: 7d 0a 7d 20 7b 31 20 7b 66 6f 72 65 69 67 6e 20  }.} {1 {foreign 
8100: 6b 65 79 20 6d 69 73 6d 61 74 63 68 7d 7d 0a 0a  key mismatch}}..
8110: 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  .#--------------
8120: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
8130: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
8140: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
8150: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20 2f 2a  -----------.# /*
8160: 20 45 56 3a 20 52 2d 32 34 36 37 36 2d 30 39 38   EV: R-24676-098
8170: 35 39 20 2a 2f 0a 23 0a 23 20 54 65 73 74 20 74  59 */.#.# Test t
8180: 68 65 20 65 78 61 6d 70 6c 65 20 73 63 68 65 6d  he example schem
8190: 61 20 69 6e 20 74 68 65 20 22 43 6f 6d 70 6f 73  a in the "Compos
81a0: 69 74 65 20 46 6f 72 65 69 67 6e 20 4b 65 79 20  ite Foreign Key 
81b0: 43 6f 6e 73 74 72 61 69 6e 74 73 22 20 0a 23 20  Constraints" .# 
81c0: 73 65 63 74 69 6f 6e 2e 0a 23 0a 64 6f 5f 74 65  section..#.do_te
81d0: 73 74 20 65 5f 66 6b 65 79 2d 32 39 2e 31 20 7b  st e_fkey-29.1 {
81e0: 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20  .  execsql {.   
81f0: 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 61 6c   CREATE TABLE al
8200: 62 75 6d 28 0a 20 20 20 20 20 20 61 6c 62 75 6d  bum(.      album
8210: 61 72 74 69 73 74 20 54 45 58 54 2c 0a 20 20 20  artist TEXT,.   
8220: 20 20 20 61 6c 62 75 6d 6e 61 6d 65 20 54 45 58     albumname TEX
8230: 54 2c 0a 20 20 20 20 20 20 61 6c 62 75 6d 63 6f  T,.      albumco
8240: 76 65 72 20 42 49 4e 41 52 59 2c 0a 20 20 20 20  ver BINARY,.    
8250: 20 20 50 52 49 4d 41 52 59 20 4b 45 59 28 61 6c    PRIMARY KEY(al
8260: 62 75 6d 61 72 74 69 73 74 2c 20 61 6c 62 75 6d  bumartist, album
8270: 6e 61 6d 65 29 0a 20 20 20 20 29 3b 0a 20 20 20  name).    );.   
8280: 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 73 6f   CREATE TABLE so
8290: 6e 67 28 0a 20 20 20 20 20 20 73 6f 6e 67 69 64  ng(.      songid
82a0: 20 49 4e 54 45 47 45 52 2c 0a 20 20 20 20 20 20   INTEGER,.      
82b0: 73 6f 6e 67 61 72 74 69 73 74 20 54 45 58 54 2c  songartist TEXT,
82c0: 0a 20 20 20 20 20 20 73 6f 6e 67 61 6c 62 75 6d  .      songalbum
82d0: 20 54 45 58 54 2c 0a 20 20 20 20 20 20 73 6f 6e   TEXT,.      son
82e0: 67 6e 61 6d 65 20 54 45 58 54 2c 0a 20 20 20 20  gname TEXT,.    
82f0: 20 20 46 4f 52 45 49 47 4e 20 4b 45 59 28 73 6f    FOREIGN KEY(so
8300: 6e 67 61 72 74 69 73 74 2c 20 73 6f 6e 67 61 6c  ngartist, songal
8310: 62 75 6d 29 20 52 45 46 45 52 45 4e 43 45 53 20  bum) REFERENCES 
8320: 61 6c 62 75 6d 28 61 6c 62 75 6d 61 72 74 69 73  album(albumartis
8330: 74 2c 61 6c 62 75 6d 6e 61 6d 65 29 0a 20 20 20  t,albumname).   
8340: 20 29 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a 0a 64 6f   );.  }.} {}..do
8350: 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 32 39 2e  _test e_fkey-29.
8360: 32 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a  2 {.  execsql {.
8370: 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20      INSERT INTO 
8380: 61 6c 62 75 6d 20 56 41 4c 55 45 53 28 27 45 6c  album VALUES('El
8390: 76 69 73 20 50 72 65 73 6c 65 79 27 2c 20 27 45  vis Presley', 'E
83a0: 6c 76 69 73 27 27 20 43 68 72 69 73 74 6d 61 73  lvis'' Christmas
83b0: 20 41 6c 62 75 6d 27 2c 20 4e 55 4c 4c 29 3b 0a   Album', NULL);.
83c0: 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20      INSERT INTO 
83d0: 73 6f 6e 67 20 56 41 4c 55 45 53 28 0a 20 20 20  song VALUES(.   
83e0: 20 20 20 31 2c 20 27 45 6c 76 69 73 20 50 72 65     1, 'Elvis Pre
83f0: 73 6c 65 79 27 2c 20 27 45 6c 76 69 73 27 27 20  sley', 'Elvis'' 
8400: 43 68 72 69 73 74 6d 61 73 20 41 6c 62 75 6d 27  Christmas Album'
8410: 2c 20 27 48 65 72 65 20 43 6f 6d 65 73 20 53 61  , 'Here Comes Sa
8420: 6e 74 61 20 43 6c 61 75 73 65 27 0a 20 20 20 20  nta Clause'.    
8430: 29 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a 64 6f 5f 74  );.  }.} {}.do_t
8440: 65 73 74 20 65 5f 66 6b 65 79 2d 32 39 2e 33 20  est e_fkey-29.3 
8450: 7b 0a 20 20 63 61 74 63 68 73 71 6c 20 7b 0a 20  {.  catchsql {. 
8460: 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 73     INSERT INTO s
8470: 6f 6e 67 20 56 41 4c 55 45 53 28 32 2c 20 27 45  ong VALUES(2, 'E
8480: 6c 76 69 73 20 50 72 65 73 6c 65 79 27 2c 20 27  lvis Presley', '
8490: 45 6c 76 69 73 20 49 73 20 42 61 63 6b 21 27 2c  Elvis Is Back!',
84a0: 20 27 46 65 76 65 72 27 29 3b 0a 20 20 7d 0a 7d   'Fever');.  }.}
84b0: 20 7b 31 20 7b 66 6f 72 65 69 67 6e 20 6b 65 79   {1 {foreign key
84c0: 20 63 6f 6e 73 74 72 61 69 6e 74 20 66 61 69 6c   constraint fail
84d0: 65 64 7d 7d 0a 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d  ed}}...#--------
84e0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
84f0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
8500: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
8510: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
8520: 2d 0a 23 20 2f 2a 20 45 56 3a 20 52 2d 33 33 36  -.# /* EV: R-336
8530: 32 36 2d 34 38 34 31 38 20 2a 2f 0a 23 0a 23 20  26-48418 */.#.# 
8540: 43 68 65 63 6b 20 74 68 61 74 20 69 66 20 61 6e  Check that if an
8550: 79 20 6f 66 20 74 68 65 20 63 68 69 6c 64 20 6b  y of the child k
8560: 65 79 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68  ey columns in th
8570: 65 20 61 62 6f 76 65 20 73 63 68 65 6d 61 20 61  e above schema a
8580: 72 65 20 4e 55 4c 4c 2c 0a 23 20 74 68 65 72 65  re NULL,.# there
8590: 20 69 73 20 6e 6f 20 72 65 71 75 69 72 65 6d 65   is no requireme
85a0: 6e 74 20 66 6f 72 20 61 20 63 6f 72 72 65 73 70  nt for a corresp
85b0: 6f 6e 64 69 6e 67 20 70 61 72 65 6e 74 20 6b 65  onding parent ke
85c0: 79 2e 0a 23 0a 64 6f 5f 74 65 73 74 20 65 5f 66  y..#.do_test e_f
85d0: 6b 65 79 2d 33 30 2e 31 20 7b 0a 20 20 65 78 65  key-30.1 {.  exe
85e0: 63 73 71 6c 20 7b 0a 20 20 20 20 49 4e 53 45 52  csql {.    INSER
85f0: 54 20 49 4e 54 4f 20 73 6f 6e 67 20 56 41 4c 55  T INTO song VALU
8600: 45 53 28 32 2c 20 27 45 6c 76 69 73 20 50 72 65  ES(2, 'Elvis Pre
8610: 73 6c 65 79 27 2c 20 4e 55 4c 4c 2c 20 27 46 65  sley', NULL, 'Fe
8620: 76 65 72 27 29 3b 0a 20 20 20 20 49 4e 53 45 52  ver');.    INSER
8630: 54 20 49 4e 54 4f 20 73 6f 6e 67 20 56 41 4c 55  T INTO song VALU
8640: 45 53 28 33 2c 20 4e 55 4c 4c 2c 20 27 45 6c 76  ES(3, NULL, 'Elv
8650: 69 73 20 49 73 20 42 61 63 6b 27 2c 20 27 53 6f  is Is Back', 'So
8660: 6c 64 69 65 72 20 42 6f 79 27 29 3b 0a 20 20 7d  ldier Boy');.  }
8670: 0a 7d 20 7b 7d 0a 0a 23 23 23 23 23 23 23 23 23  .} {}..#########
8680: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
8690: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
86a0: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
86b0: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
86c0: 23 23 0a 23 23 23 20 53 45 43 54 49 4f 4e 20 34  ##.### SECTION 4
86d0: 2e 32 3a 20 44 65 66 65 72 72 65 64 20 46 6f 72  .2: Deferred For
86e0: 65 69 67 6e 20 4b 65 79 20 43 6f 6e 73 74 72 61  eign Key Constra
86f0: 69 6e 74 73 0a 23 23 23 23 23 23 23 23 23 23 23  ints.###########
8700: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
8710: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
8720: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
8730: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
8740: 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ..#-------------
8750: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
8760: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
8770: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
8780: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20 4e  ------------.# N
8790: 6f 74 65 3a 20 52 2d 33 35 32 39 30 2d 31 36 34  ote: R-35290-164
87a0: 36 30 20 69 73 20 74 65 73 74 65 64 20 62 65 6c  60 is tested bel
87b0: 6f 77 2e 0a 23 0a 23 20 54 4f 44 4f 3a 20 52 2d  ow..#.# TODO: R-
87c0: 33 30 33 32 33 2d 32 31 39 31 37 0a 0a 23 2d 2d  30323-21917..#--
87d0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
87e0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
87f0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
8800: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
8810: 2d 2d 2d 2d 2d 2d 2d 0a 23 20 2f 2a 20 45 56 3a  -------.# /* EV:
8820: 20 52 2d 30 39 33 32 33 2d 33 30 34 37 30 20 2a   R-09323-30470 *
8830: 2f 0a 23 0a 23 20 54 65 73 74 20 74 68 61 74 20  /.#.# Test that 
8840: 69 66 20 61 20 73 74 61 74 65 6d 65 6e 74 20 76  if a statement v
8850: 69 6f 6c 61 74 65 73 20 61 6e 20 69 6d 6d 65 64  iolates an immed
8860: 69 61 74 65 20 46 4b 20 63 6f 6e 73 74 72 61 69  iate FK constrai
8870: 6e 74 2c 20 61 6e 64 20 74 68 65 0a 23 20 64 61  nt, and the.# da
8880: 74 61 62 61 73 65 20 64 6f 65 73 20 6e 6f 74 20  tabase does not 
8890: 73 61 74 69 73 66 79 20 74 68 65 20 46 4b 20 63  satisfy the FK c
88a0: 6f 6e 73 74 72 61 69 6e 74 20 6f 6e 63 65 20 61  onstraint once a
88b0: 6c 6c 20 65 66 66 65 63 74 73 20 6f 66 20 74 68  ll effects of th
88c0: 65 0a 23 20 73 74 61 74 65 6d 65 6e 74 20 68 61  e.# statement ha
88d0: 76 65 20 62 65 65 6e 20 61 70 70 6c 69 65 64 2c  ve been applied,
88e0: 20 61 6e 20 65 72 72 6f 72 20 69 73 20 72 65 70   an error is rep
88f0: 6f 72 74 65 64 20 61 6e 64 20 74 68 65 20 65 66  orted and the ef
8900: 66 65 63 74 73 20 6f 66 0a 23 20 74 68 65 20 73  fects of.# the s
8910: 74 61 74 65 6d 65 6e 74 20 72 6f 6c 6c 65 64 20  tatement rolled 
8920: 62 61 63 6b 2e 0a 23 0a 64 72 6f 70 5f 61 6c 6c  back..#.drop_all
8930: 5f 74 61 62 6c 65 73 0a 64 6f 5f 74 65 73 74 20  _tables.do_test 
8940: 65 5f 66 6b 65 79 2d 33 31 2e 31 20 7b 0a 20 20  e_fkey-31.1 {.  
8950: 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 43 52  execsql {.    CR
8960: 45 41 54 45 20 54 41 42 4c 45 20 6b 69 6e 67 28  EATE TABLE king(
8970: 61 2c 20 62 2c 20 50 52 49 4d 41 52 59 20 4b 45  a, b, PRIMARY KE
8980: 59 28 61 29 29 3b 0a 20 20 20 20 43 52 45 41 54  Y(a));.    CREAT
8990: 45 20 54 41 42 4c 45 20 70 72 69 6e 63 65 28 63  E TABLE prince(c
89a0: 20 52 45 46 45 52 45 4e 43 45 53 20 6b 69 6e 67   REFERENCES king
89b0: 2c 20 64 29 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a 0a  , d);.  }.} {}..
89c0: 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 33  do_test e_fkey-3
89d0: 31 2e 32 20 7b 0a 20 20 23 20 45 78 65 63 75 74  1.2 {.  # Execut
89e0: 65 20 61 20 73 74 61 74 65 6d 65 6e 74 20 74 68  e a statement th
89f0: 61 74 20 76 69 6f 6c 61 74 65 73 20 74 68 65 20  at violates the 
8a00: 69 6d 6d 65 64 69 61 74 65 20 46 4b 20 63 6f 6e  immediate FK con
8a10: 73 74 72 61 69 6e 74 2e 0a 20 20 63 61 74 63 68  straint..  catch
8a20: 73 71 6c 20 7b 20 49 4e 53 45 52 54 20 49 4e 54  sql { INSERT INT
8a30: 4f 20 70 72 69 6e 63 65 20 56 41 4c 55 45 53 28  O prince VALUES(
8a40: 31 2c 20 32 29 20 7d 0a 7d 20 7b 31 20 7b 66 6f  1, 2) }.} {1 {fo
8a50: 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72  reign key constr
8a60: 61 69 6e 74 20 66 61 69 6c 65 64 7d 7d 0a 0a 64  aint failed}}..d
8a70: 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 33 31  o_test e_fkey-31
8a80: 2e 33 20 7b 0a 20 20 23 20 54 68 69 73 20 74 69  .3 {.  # This ti
8a90: 6d 65 2c 20 75 73 65 20 61 20 74 72 69 67 67 65  me, use a trigge
8aa0: 72 20 74 6f 20 66 69 78 20 74 68 65 20 63 6f 6e  r to fix the con
8ab0: 73 74 72 61 69 6e 74 20 76 69 6f 6c 61 74 69 6f  straint violatio
8ac0: 6e 20 62 65 66 6f 72 65 20 74 68 65 0a 20 20 23  n before the.  #
8ad0: 20 73 74 61 74 65 6d 65 6e 74 20 68 61 73 20 66   statement has f
8ae0: 69 6e 69 73 68 65 64 20 65 78 65 63 75 74 69 6e  inished executin
8af0: 67 2e 20 54 68 65 6e 20 65 78 65 63 75 74 65 20  g. Then execute 
8b00: 74 68 65 20 73 61 6d 65 20 73 74 61 74 65 6d 65  the same stateme
8b10: 6e 74 20 61 73 0a 20 20 23 20 69 6e 20 74 68 65  nt as.  # in the
8b20: 20 70 72 65 76 69 6f 75 73 20 74 65 73 74 20 63   previous test c
8b30: 61 73 65 2e 20 54 68 69 73 20 74 69 6d 65 2c 20  ase. This time, 
8b40: 6e 6f 20 65 72 72 6f 72 2e 0a 20 20 65 78 65 63  no error..  exec
8b50: 73 71 6c 20 7b 0a 20 20 20 20 43 52 45 41 54 45  sql {.    CREATE
8b60: 20 54 52 49 47 47 45 52 20 6b 74 20 41 46 54 45   TRIGGER kt AFTE
8b70: 52 20 49 4e 53 45 52 54 20 4f 4e 20 70 72 69 6e  R INSERT ON prin
8b80: 63 65 20 57 48 45 4e 0a 20 20 20 20 20 20 4e 4f  ce WHEN.      NO
8b90: 54 20 45 58 49 53 54 53 20 28 53 45 4c 45 43 54  T EXISTS (SELECT
8ba0: 20 61 20 46 52 4f 4d 20 6b 69 6e 67 20 57 48 45   a FROM king WHE
8bb0: 52 45 20 61 20 3d 20 6e 65 77 2e 63 29 0a 20 20  RE a = new.c).  
8bc0: 20 20 42 45 47 49 4e 0a 20 20 20 20 20 20 49 4e    BEGIN.      IN
8bd0: 53 45 52 54 20 49 4e 54 4f 20 6b 69 6e 67 20 56  SERT INTO king V
8be0: 41 4c 55 45 53 28 6e 65 77 2e 63 2c 20 4e 55 4c  ALUES(new.c, NUL
8bf0: 4c 29 3b 0a 20 20 20 20 45 4e 44 0a 20 20 7d 0a  L);.    END.  }.
8c00: 20 20 65 78 65 63 73 71 6c 20 7b 20 49 4e 53 45    execsql { INSE
8c10: 52 54 20 49 4e 54 4f 20 70 72 69 6e 63 65 20 56  RT INTO prince V
8c20: 41 4c 55 45 53 28 31 2c 20 32 29 20 7d 0a 7d 20  ALUES(1, 2) }.} 
8c30: 7b 7d 0a 0a 23 20 54 65 73 74 20 74 68 61 74 20  {}..# Test that 
8c40: 6f 70 65 72 61 74 69 6e 67 20 69 6e 73 69 64 65  operating inside
8c50: 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d   a transaction m
8c60: 61 6b 65 73 20 6e 6f 20 64 69 66 66 65 72 65 6e  akes no differen
8c70: 63 65 20 74 6f 20 0a 23 20 69 6d 6d 65 64 69 61  ce to .# immedia
8c80: 74 65 20 63 6f 6e 73 74 72 61 69 6e 74 20 76 69  te constraint vi
8c90: 6f 6c 61 74 69 6f 6e 20 68 61 6e 64 6c 69 6e 67  olation handling
8ca0: 2e 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79  ..do_test e_fkey
8cb0: 2d 33 31 2e 34 20 7b 0a 20 20 65 78 65 63 73 71  -31.4 {.  execsq
8cc0: 6c 20 7b 0a 20 20 20 20 42 45 47 49 4e 3b 0a 20  l {.    BEGIN;. 
8cd0: 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 70     INSERT INTO p
8ce0: 72 69 6e 63 65 20 56 41 4c 55 45 53 28 32 2c 20  rince VALUES(2, 
8cf0: 33 29 3b 0a 20 20 20 20 44 52 4f 50 20 54 52 49  3);.    DROP TRI
8d00: 47 47 45 52 20 6b 74 3b 0a 20 20 7d 0a 20 20 63  GGER kt;.  }.  c
8d10: 61 74 63 68 73 71 6c 20 7b 20 49 4e 53 45 52 54  atchsql { INSERT
8d20: 20 49 4e 54 4f 20 70 72 69 6e 63 65 20 56 41 4c   INTO prince VAL
8d30: 55 45 53 28 33 2c 20 34 29 20 7d 0a 7d 20 7b 31  UES(3, 4) }.} {1
8d40: 20 7b 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f   {foreign key co
8d50: 6e 73 74 72 61 69 6e 74 20 66 61 69 6c 65 64 7d  nstraint failed}
8d60: 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79  }.do_test e_fkey
8d70: 2d 33 31 2e 35 20 7b 0a 20 20 65 78 65 63 73 71  -31.5 {.  execsq
8d80: 6c 20 7b 0a 20 20 20 20 43 4f 4d 4d 49 54 3b 0a  l {.    COMMIT;.
8d90: 20 20 20 20 53 45 4c 45 43 54 20 2a 20 46 52 4f      SELECT * FRO
8da0: 4d 20 6b 69 6e 67 3b 0a 20 20 7d 0a 7d 20 7b 31  M king;.  }.} {1
8db0: 20 7b 7d 20 32 20 7b 7d 7d 0a 0a 23 2d 2d 2d 2d   {} 2 {}}..#----
8dc0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
8dd0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
8de0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
8df0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
8e00: 2d 2d 2d 2d 2d 0a 23 20 2f 2a 20 45 56 3a 20 52  -----.# /* EV: R
8e10: 2d 34 39 31 37 38 2d 32 31 33 35 38 20 2a 2f 0a  -49178-21358 */.
8e20: 23 20 2f 2a 20 45 56 3a 20 52 2d 33 39 36 39 32  # /* EV: R-39692
8e30: 2d 31 32 34 38 38 20 2a 2f 0a 23 20 2f 2a 20 45  -12488 */.# /* E
8e40: 56 3a 20 52 2d 35 35 31 34 37 2d 34 37 36 36 34  V: R-55147-47664
8e50: 20 2a 2f 0a 23 20 2f 2a 20 45 56 3a 20 52 2d 32   */.# /* EV: R-2
8e60: 39 36 30 34 2d 33 30 33 39 35 20 2a 2f 0a 23 0a  9604-30395 */.#.
8e70: 23 20 54 65 73 74 20 74 68 61 74 20 69 66 20 61  # Test that if a
8e80: 20 64 65 66 65 72 72 65 64 20 63 6f 6e 73 74 72   deferred constr
8e90: 61 69 6e 74 20 69 73 20 76 69 6f 6c 61 74 65 64  aint is violated
8ea0: 20 77 69 74 68 69 6e 20 61 20 74 72 61 6e 73 61   within a transa
8eb0: 63 74 69 6f 6e 2c 0a 23 20 6e 6f 74 68 69 6e 67  ction,.# nothing
8ec0: 20 68 61 70 70 65 6e 73 20 69 6d 6d 65 64 69 61   happens immedia
8ed0: 74 65 6c 79 20 61 6e 64 20 74 68 65 20 64 61 74  tely and the dat
8ee0: 61 62 61 73 65 20 69 73 20 61 6c 6c 6f 77 65 64  abase is allowed
8ef0: 20 74 6f 20 70 65 72 73 69 73 74 0a 23 20 69 6e   to persist.# in
8f00: 20 61 20 73 74 61 74 65 20 74 68 61 74 20 64 6f   a state that do
8f10: 65 73 20 6e 6f 74 20 73 61 74 69 73 66 79 20 74  es not satisfy t
8f20: 68 65 20 46 4b 20 63 6f 6e 73 74 72 61 69 6e 74  he FK constraint
8f30: 2e 20 48 6f 77 65 76 65 72 20 61 74 74 65 6d 70  . However attemp
8f40: 74 73 0a 23 20 74 6f 20 43 4f 4d 4d 49 54 20 74  ts.# to COMMIT t
8f50: 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 66  he transaction f
8f60: 61 69 6c 20 75 6e 74 69 6c 20 74 68 65 20 46 4b  ail until the FK
8f70: 20 63 6f 6e 73 74 72 61 69 6e 74 20 69 73 20 73   constraint is s
8f80: 61 74 69 73 66 69 65 64 2e 0a 23 0a 70 72 6f 63  atisfied..#.proc
8f90: 20 74 65 73 74 5f 65 66 6b 65 79 5f 33 34 20 7b   test_efkey_34 {
8fa0: 74 6e 20 69 73 45 72 72 6f 72 20 73 71 6c 7d 20  tn isError sql} 
8fb0: 7b 0a 20 20 64 6f 5f 74 65 73 74 20 65 5f 66 6b  {.  do_test e_fk
8fc0: 65 79 2d 33 32 2e 24 74 6e 20 22 0a 20 20 20 20  ey-32.$tn ".    
8fd0: 63 61 74 63 68 73 71 6c 20 7b 24 73 71 6c 7d 0a  catchsql {$sql}.
8fe0: 20 20 22 20 5b 6c 69 6e 64 65 78 20 7b 7b 30 20    " [lindex {{0 
8ff0: 7b 7d 7d 20 7b 31 20 7b 66 6f 72 65 69 67 6e 20  {}} {1 {foreign 
9000: 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 20 66  key constraint f
9010: 61 69 6c 65 64 7d 7d 7d 20 24 69 73 45 72 72 6f  ailed}}} $isErro
9020: 72 5d 0a 7d 0a 64 72 6f 70 5f 61 6c 6c 5f 74 61  r].}.drop_all_ta
9030: 62 6c 65 73 0a 0a 74 65 73 74 5f 65 66 6b 65 79  bles..test_efkey
9040: 5f 33 34 20 20 31 20 30 20 7b 0a 20 20 43 52 45  _34  1 0 {.  CRE
9050: 41 54 45 20 54 41 42 4c 45 20 6c 6c 28 6b 20 50  ATE TABLE ll(k P
9060: 52 49 4d 41 52 59 20 4b 45 59 29 3b 0a 20 20 43  RIMARY KEY);.  C
9070: 52 45 41 54 45 20 54 41 42 4c 45 20 6b 6b 28 63  REATE TABLE kk(c
9080: 20 52 45 46 45 52 45 4e 43 45 53 20 6c 6c 20 44   REFERENCES ll D
9090: 45 46 45 52 52 41 42 4c 45 20 49 4e 49 54 49 41  EFERRABLE INITIA
90a0: 4c 4c 59 20 44 45 46 45 52 52 45 44 29 3b 0a 7d  LLY DEFERRED);.}
90b0: 0a 74 65 73 74 5f 65 66 6b 65 79 5f 33 34 20 20  .test_efkey_34  
90c0: 32 20 30 20 22 42 45 47 49 4e 22 0a 74 65 73 74  2 0 "BEGIN".test
90d0: 5f 65 66 6b 65 79 5f 33 34 20 20 33 20 30 20 20  _efkey_34  3 0  
90e0: 20 22 49 4e 53 45 52 54 20 49 4e 54 4f 20 6b 6b   "INSERT INTO kk
90f0: 20 56 41 4c 55 45 53 28 35 29 22 0a 74 65 73 74   VALUES(5)".test
9100: 5f 65 66 6b 65 79 5f 33 34 20 20 34 20 30 20 20  _efkey_34  4 0  
9110: 20 22 49 4e 53 45 52 54 20 49 4e 54 4f 20 6b 6b   "INSERT INTO kk
9120: 20 56 41 4c 55 45 53 28 31 30 29 22 0a 74 65 73   VALUES(10)".tes
9130: 74 5f 65 66 6b 65 79 5f 33 34 20 20 35 20 31 20  t_efkey_34  5 1 
9140: 22 43 4f 4d 4d 49 54 22 0a 74 65 73 74 5f 65 66  "COMMIT".test_ef
9150: 6b 65 79 5f 33 34 20 20 36 20 30 20 20 20 22 49  key_34  6 0   "I
9160: 4e 53 45 52 54 20 49 4e 54 4f 20 6c 6c 20 56 41  NSERT INTO ll VA
9170: 4c 55 45 53 28 31 30 29 22 0a 74 65 73 74 5f 65  LUES(10)".test_e
9180: 66 6b 65 79 5f 33 34 20 20 37 20 31 20 22 43 4f  fkey_34  7 1 "CO
9190: 4d 4d 49 54 22 0a 74 65 73 74 5f 65 66 6b 65 79  MMIT".test_efkey
91a0: 5f 33 34 20 20 38 20 30 20 20 20 22 49 4e 53 45  _34  8 0   "INSE
91b0: 52 54 20 49 4e 54 4f 20 6c 6c 20 56 41 4c 55 45  RT INTO ll VALUE
91c0: 53 28 35 29 22 0a 74 65 73 74 5f 65 66 6b 65 79  S(5)".test_efkey
91d0: 5f 33 34 20 20 39 20 30 20 22 43 4f 4d 4d 49 54  _34  9 0 "COMMIT
91e0: 22 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  "..#------------
91f0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
9200: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
9210: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
9220: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20  -------------.# 
9230: 2f 2a 20 45 56 3a 20 52 2d 35 36 38 34 34 2d 36  /* EV: R-56844-6
9240: 31 37 30 35 20 2a 2f 0a 23 0a 23 20 57 68 65 6e  1705 */.#.# When
9250: 20 6e 6f 74 20 72 75 6e 6e 69 6e 67 20 69 6e 73   not running ins
9260: 69 64 65 20 61 20 74 72 61 6e 73 61 63 74 69 6f  ide a transactio
9270: 6e 2c 20 61 20 64 65 66 65 72 72 65 64 20 63 6f  n, a deferred co
9280: 6e 73 74 72 61 69 6e 74 20 69 73 20 73 69 6d 69  nstraint is simi
9290: 6c 61 72 0a 23 20 74 6f 20 61 6e 20 69 6d 6d 65  lar.# to an imme
92a0: 64 69 61 74 65 20 63 6f 6e 73 74 72 61 69 6e 74  diate constraint
92b0: 20 28 76 69 6f 6c 61 74 69 6f 6e 73 20 61 72 65   (violations are
92c0: 20 72 65 70 6f 72 74 65 64 20 69 6d 6d 65 64 69   reported immedi
92d0: 61 74 65 6c 79 29 2e 0a 23 0a 64 72 6f 70 5f 61  ately)..#.drop_a
92e0: 6c 6c 5f 74 61 62 6c 65 73 0a 70 72 6f 63 20 74  ll_tables.proc t
92f0: 65 73 74 5f 65 66 6b 65 79 5f 33 35 20 7b 74 6e  est_efkey_35 {tn
9300: 20 69 73 45 72 72 6f 72 20 73 71 6c 7d 20 7b 0a   isError sql} {.
9310: 20 20 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79    do_test e_fkey
9320: 2d 33 33 2e 24 74 6e 20 22 0a 20 20 20 20 63 61  -33.$tn ".    ca
9330: 74 63 68 73 71 6c 20 7b 24 73 71 6c 7d 0a 20 20  tchsql {$sql}.  
9340: 22 20 5b 6c 69 6e 64 65 78 20 7b 7b 30 20 7b 7d  " [lindex {{0 {}
9350: 7d 20 7b 31 20 7b 66 6f 72 65 69 67 6e 20 6b 65  } {1 {foreign ke
9360: 79 20 63 6f 6e 73 74 72 61 69 6e 74 20 66 61 69  y constraint fai
9370: 6c 65 64 7d 7d 7d 20 24 69 73 45 72 72 6f 72 5d  led}}} $isError]
9380: 0a 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65  .}.do_test e_fke
9390: 79 2d 33 33 2e 31 20 7b 0a 20 20 65 78 65 63 73  y-33.1 {.  execs
93a0: 71 6c 20 7b 0a 20 20 20 20 43 52 45 41 54 45 20  ql {.    CREATE 
93b0: 54 41 42 4c 45 20 70 61 72 65 6e 74 28 78 2c 20  TABLE parent(x, 
93c0: 79 29 3b 0a 20 20 20 20 43 52 45 41 54 45 20 55  y);.    CREATE U
93d0: 4e 49 51 55 45 20 49 4e 44 45 58 20 70 69 20 4f  NIQUE INDEX pi O
93e0: 4e 20 70 61 72 65 6e 74 28 78 2c 20 79 29 3b 0a  N parent(x, y);.
93f0: 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45      CREATE TABLE
9400: 20 63 68 69 6c 64 28 61 2c 20 62 2c 0a 20 20 20   child(a, b,.   
9410: 20 20 20 46 4f 52 45 49 47 4e 20 4b 45 59 28 61     FOREIGN KEY(a
9420: 2c 20 62 29 20 52 45 46 45 52 45 4e 43 45 53 20  , b) REFERENCES 
9430: 70 61 72 65 6e 74 28 78 2c 20 79 29 20 44 45 46  parent(x, y) DEF
9440: 45 52 52 41 42 4c 45 20 49 4e 49 54 49 41 4c 4c  ERRABLE INITIALL
9450: 59 20 44 45 46 45 52 52 45 44 0a 20 20 20 20 29  Y DEFERRED.    )
9460: 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a 74 65 73 74 5f  ;.  }.} {}.test_
9470: 65 66 6b 65 79 5f 33 35 20 32 20 31 20 22 49 4e  efkey_35 2 1 "IN
9480: 53 45 52 54 20 49 4e 54 4f 20 63 68 69 6c 64 20  SERT INTO child 
9490: 20 56 41 4c 55 45 53 28 27 78 27 2c 20 27 79 27   VALUES('x', 'y'
94a0: 29 22 0a 74 65 73 74 5f 65 66 6b 65 79 5f 33 35  )".test_efkey_35
94b0: 20 33 20 30 20 22 49 4e 53 45 52 54 20 49 4e 54   3 0 "INSERT INT
94c0: 4f 20 70 61 72 65 6e 74 20 56 41 4c 55 45 53 28  O parent VALUES(
94d0: 27 78 27 2c 20 27 79 27 29 22 0a 74 65 73 74 5f  'x', 'y')".test_
94e0: 65 66 6b 65 79 5f 33 35 20 34 20 30 20 22 49 4e  efkey_35 4 0 "IN
94f0: 53 45 52 54 20 49 4e 54 4f 20 63 68 69 6c 64 20  SERT INTO child 
9500: 20 56 41 4c 55 45 53 28 27 78 27 2c 20 27 79 27   VALUES('x', 'y'
9510: 29 22 0a 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  )"...#----------
9520: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
9530: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
9540: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
9550: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a  ---------------.
9560: 23 20 2f 2a 20 45 56 3a 20 52 2d 31 32 37 38 32  # /* EV: R-12782
9570: 2d 36 31 38 34 31 20 2a 2f 0a 23 0a 23 20 54 65  -61841 */.#.# Te
9580: 73 74 20 74 68 61 74 20 61 6e 20 46 4b 20 63 6f  st that an FK co
9590: 6e 73 74 72 61 69 6e 74 20 69 73 20 6d 61 64 65  nstraint is made
95a0: 20 64 65 66 65 72 72 65 64 20 62 79 20 61 64 64   deferred by add
95b0: 69 6e 67 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e  ing the followin
95c0: 67 0a 23 20 74 6f 20 74 68 65 20 64 65 66 69 6e  g.# to the defin
95d0: 69 74 69 6f 6e 3a 0a 23 0a 23 20 20 20 44 45 46  ition:.#.#   DEF
95e0: 45 52 52 41 42 4c 45 20 49 4e 49 54 49 41 4c 4c  ERRABLE INITIALL
95f0: 59 20 44 45 46 45 52 52 45 44 0a 23 0a 23 20 2f  Y DEFERRED.#.# /
9600: 2a 20 45 56 3a 20 52 2d 30 39 30 30 35 2d 32 38  * EV: R-09005-28
9610: 37 39 31 20 2a 2f 0a 23 0a 23 20 41 6c 73 6f 20  791 */.#.# Also 
9620: 74 65 73 74 20 74 68 61 74 20 61 64 64 69 6e 67  test that adding
9630: 20 61 6e 79 20 6f 66 20 74 68 65 20 66 6f 6c 6c   any of the foll
9640: 6f 77 69 6e 67 20 74 6f 20 61 20 66 6f 72 65 69  owing to a forei
9650: 67 6e 20 6b 65 79 20 64 65 66 69 6e 69 74 69 6f  gn key definitio
9660: 6e 20 0a 23 20 6d 61 6b 65 73 20 74 68 65 20 63  n .# makes the c
9670: 6f 6e 73 74 72 61 69 6e 74 20 49 4d 4d 45 44 49  onstraint IMMEDI
9680: 41 54 45 3a 0a 23 0a 23 20 20 20 4e 4f 54 20 44  ATE:.#.#   NOT D
9690: 45 46 45 52 52 41 42 4c 45 20 49 4e 49 54 49 41  EFERRABLE INITIA
96a0: 4c 4c 59 20 44 45 46 45 52 52 45 44 0a 23 20 20  LLY DEFERRED.#  
96b0: 20 4e 4f 54 20 44 45 46 45 52 52 41 42 4c 45 20   NOT DEFERRABLE 
96c0: 49 4e 49 54 49 41 4c 4c 59 20 49 4d 4d 45 44 49  INITIALLY IMMEDI
96d0: 41 54 45 0a 23 20 20 20 4e 4f 54 20 44 45 46 45  ATE.#   NOT DEFE
96e0: 52 52 41 42 4c 45 0a 23 20 20 20 44 45 46 45 52  RRABLE.#   DEFER
96f0: 52 41 42 4c 45 20 49 4e 49 54 49 41 4c 4c 59 20  RABLE INITIALLY 
9700: 49 4d 4d 45 44 49 41 54 45 0a 23 20 20 20 44 45  IMMEDIATE.#   DE
9710: 46 45 52 52 41 42 4c 45 0a 23 0a 23 20 2f 2a 20  FERRABLE.#.# /* 
9720: 45 56 3a 20 52 2d 33 35 32 39 30 2d 31 36 34 36  EV: R-35290-1646
9730: 30 20 2a 2f 0a 23 0a 23 20 46 6f 72 65 69 67 6e  0 */.#.# Foreign
9740: 20 6b 65 79 73 20 61 72 65 20 49 4d 4d 45 44 49   keys are IMMEDI
9750: 41 54 45 20 62 79 20 64 65 66 61 75 6c 74 20 28  ATE by default (
9760: 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 44  if there is no D
9770: 45 46 45 52 52 41 42 4c 45 20 6f 72 20 4e 4f 54  EFERRABLE or NOT
9780: 0a 23 20 44 45 46 45 52 52 41 42 4c 45 20 63 6c  .# DEFERRABLE cl
9790: 61 75 73 65 29 2e 0a 23 0a 23 20 2f 2a 20 45 56  ause)..#.# /* EV
97a0: 3a 20 52 2d 33 30 33 32 33 2d 32 31 39 31 37 20  : R-30323-21917 
97b0: 2a 2f 20 20 46 4b 73 20 61 72 65 20 65 69 74 68  */  FKs are eith
97c0: 65 72 20 49 4d 4d 45 44 49 41 54 45 20 6f 72 20  er IMMEDIATE or 
97d0: 44 45 46 45 52 52 45 44 2e 0a 23 0a 64 72 6f 70  DEFERRED..#.drop
97e0: 5f 61 6c 6c 5f 74 61 62 6c 65 73 0a 64 6f 5f 74  _all_tables.do_t
97f0: 65 73 74 20 65 5f 66 6b 65 79 2d 33 34 2e 31 20  est e_fkey-34.1 
9800: 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20  {.  execsql {.  
9810: 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 70    CREATE TABLE p
9820: 61 72 65 6e 74 28 78 2c 20 79 2c 20 7a 2c 20 50  arent(x, y, z, P
9830: 52 49 4d 41 52 59 20 4b 45 59 28 78 2c 79 2c 7a  RIMARY KEY(x,y,z
9840: 29 29 3b 0a 20 20 20 20 43 52 45 41 54 45 20 54  ));.    CREATE T
9850: 41 42 4c 45 20 63 31 28 61 2c 20 62 2c 20 63 2c  ABLE c1(a, b, c,
9860: 0a 20 20 20 20 20 20 46 4f 52 45 49 47 4e 20 4b  .      FOREIGN K
9870: 45 59 28 61 2c 20 62 2c 20 63 29 20 52 45 46 45  EY(a, b, c) REFE
9880: 52 45 4e 43 45 53 20 70 61 72 65 6e 74 20 4e 4f  RENCES parent NO
9890: 54 20 44 45 46 45 52 52 41 42 4c 45 20 49 4e 49  T DEFERRABLE INI
98a0: 54 49 41 4c 4c 59 20 44 45 46 45 52 52 45 44 0a  TIALLY DEFERRED.
98b0: 20 20 20 20 29 3b 0a 20 20 20 20 43 52 45 41 54      );.    CREAT
98c0: 45 20 54 41 42 4c 45 20 63 32 28 61 2c 20 62 2c  E TABLE c2(a, b,
98d0: 20 63 2c 0a 20 20 20 20 20 20 46 4f 52 45 49 47   c,.      FOREIG
98e0: 4e 20 4b 45 59 28 61 2c 20 62 2c 20 63 29 20 52  N KEY(a, b, c) R
98f0: 45 46 45 52 45 4e 43 45 53 20 70 61 72 65 6e 74  EFERENCES parent
9900: 20 4e 4f 54 20 44 45 46 45 52 52 41 42 4c 45 20   NOT DEFERRABLE 
9910: 49 4e 49 54 49 41 4c 4c 59 20 49 4d 4d 45 44 49  INITIALLY IMMEDI
9920: 41 54 45 0a 20 20 20 20 29 3b 0a 20 20 20 20 43  ATE.    );.    C
9930: 52 45 41 54 45 20 54 41 42 4c 45 20 63 33 28 61  REATE TABLE c3(a
9940: 2c 20 62 2c 20 63 2c 0a 20 20 20 20 20 20 46 4f  , b, c,.      FO
9950: 52 45 49 47 4e 20 4b 45 59 28 61 2c 20 62 2c 20  REIGN KEY(a, b, 
9960: 63 29 20 52 45 46 45 52 45 4e 43 45 53 20 70 61  c) REFERENCES pa
9970: 72 65 6e 74 20 4e 4f 54 20 44 45 46 45 52 52 41  rent NOT DEFERRA
9980: 42 4c 45 0a 20 20 20 20 29 3b 0a 20 20 20 20 43  BLE.    );.    C
9990: 52 45 41 54 45 20 54 41 42 4c 45 20 63 34 28 61  REATE TABLE c4(a
99a0: 2c 20 62 2c 20 63 2c 0a 20 20 20 20 20 20 46 4f  , b, c,.      FO
99b0: 52 45 49 47 4e 20 4b 45 59 28 61 2c 20 62 2c 20  REIGN KEY(a, b, 
99c0: 63 29 20 52 45 46 45 52 45 4e 43 45 53 20 70 61  c) REFERENCES pa
99d0: 72 65 6e 74 20 44 45 46 45 52 52 41 42 4c 45 20  rent DEFERRABLE 
99e0: 49 4e 49 54 49 41 4c 4c 59 20 49 4d 4d 45 44 49  INITIALLY IMMEDI
99f0: 41 54 45 0a 20 20 20 20 29 3b 0a 20 20 20 20 43  ATE.    );.    C
9a00: 52 45 41 54 45 20 54 41 42 4c 45 20 63 35 28 61  REATE TABLE c5(a
9a10: 2c 20 62 2c 20 63 2c 0a 20 20 20 20 20 20 46 4f  , b, c,.      FO
9a20: 52 45 49 47 4e 20 4b 45 59 28 61 2c 20 62 2c 20  REIGN KEY(a, b, 
9a30: 63 29 20 52 45 46 45 52 45 4e 43 45 53 20 70 61  c) REFERENCES pa
9a40: 72 65 6e 74 20 44 45 46 45 52 52 41 42 4c 45 0a  rent DEFERRABLE.
9a50: 20 20 20 20 29 3b 0a 20 20 20 20 43 52 45 41 54      );.    CREAT
9a60: 45 20 54 41 42 4c 45 20 63 36 28 61 2c 20 62 2c  E TABLE c6(a, b,
9a70: 20 63 2c 20 46 4f 52 45 49 47 4e 20 4b 45 59 28   c, FOREIGN KEY(
9a80: 61 2c 20 62 2c 20 63 29 20 52 45 46 45 52 45 4e  a, b, c) REFEREN
9a90: 43 45 53 20 70 61 72 65 6e 74 29 3b 0a 0a 20 20  CES parent);..  
9aa0: 20 20 2d 2d 20 54 68 69 73 20 46 4b 20 63 6f 6e    -- This FK con
9ab0: 73 74 72 61 69 6e 74 20 69 73 20 74 68 65 20 6f  straint is the o
9ac0: 6e 6c 79 20 64 65 66 65 72 72 61 62 6c 65 20 6f  nly deferrable o
9ad0: 6e 65 2e 0a 20 20 20 20 43 52 45 41 54 45 20 54  ne..    CREATE T
9ae0: 41 42 4c 45 20 63 37 28 61 2c 20 62 2c 20 63 2c  ABLE c7(a, b, c,
9af0: 0a 20 20 20 20 20 20 46 4f 52 45 49 47 4e 20 4b  .      FOREIGN K
9b00: 45 59 28 61 2c 20 62 2c 20 63 29 20 52 45 46 45  EY(a, b, c) REFE
9b10: 52 45 4e 43 45 53 20 70 61 72 65 6e 74 20 44 45  RENCES parent DE
9b20: 46 45 52 52 41 42 4c 45 20 49 4e 49 54 49 41 4c  FERRABLE INITIAL
9b30: 4c 59 20 44 45 46 45 52 52 45 44 0a 20 20 20 20  LY DEFERRED.    
9b40: 29 3b 0a 0a 20 20 20 20 49 4e 53 45 52 54 20 49  );..    INSERT I
9b50: 4e 54 4f 20 70 61 72 65 6e 74 20 56 41 4c 55 45  NTO parent VALUE
9b60: 53 28 27 61 27 2c 20 27 62 27 2c 20 27 63 27 29  S('a', 'b', 'c')
9b70: 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54  ;.    INSERT INT
9b80: 4f 20 70 61 72 65 6e 74 20 56 41 4c 55 45 53 28  O parent VALUES(
9b90: 27 64 27 2c 20 27 65 27 2c 20 27 66 27 29 3b 0a  'd', 'e', 'f');.
9ba0: 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20      INSERT INTO 
9bb0: 70 61 72 65 6e 74 20 56 41 4c 55 45 53 28 27 67  parent VALUES('g
9bc0: 27 2c 20 27 68 27 2c 20 27 69 27 29 3b 0a 20 20  ', 'h', 'i');.  
9bd0: 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 70 61    INSERT INTO pa
9be0: 72 65 6e 74 20 56 41 4c 55 45 53 28 27 6a 27 2c  rent VALUES('j',
9bf0: 20 27 6b 27 2c 20 27 6c 27 29 3b 0a 20 20 20 20   'k', 'l');.    
9c00: 49 4e 53 45 52 54 20 49 4e 54 4f 20 70 61 72 65  INSERT INTO pare
9c10: 6e 74 20 56 41 4c 55 45 53 28 27 6d 27 2c 20 27  nt VALUES('m', '
9c20: 6e 27 2c 20 27 6f 27 29 3b 0a 20 20 20 20 49 4e  n', 'o');.    IN
9c30: 53 45 52 54 20 49 4e 54 4f 20 70 61 72 65 6e 74  SERT INTO parent
9c40: 20 56 41 4c 55 45 53 28 27 70 27 2c 20 27 71 27   VALUES('p', 'q'
9c50: 2c 20 27 72 27 29 3b 0a 20 20 20 20 49 4e 53 45  , 'r');.    INSE
9c60: 52 54 20 49 4e 54 4f 20 70 61 72 65 6e 74 20 56  RT INTO parent V
9c70: 41 4c 55 45 53 28 27 73 27 2c 20 27 74 27 2c 20  ALUES('s', 't', 
9c80: 27 75 27 29 3b 0a 0a 20 20 20 20 49 4e 53 45 52  'u');..    INSER
9c90: 54 20 49 4e 54 4f 20 63 31 20 56 41 4c 55 45 53  T INTO c1 VALUES
9ca0: 28 27 61 27 2c 20 27 62 27 2c 20 27 63 27 29 3b  ('a', 'b', 'c');
9cb0: 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f  .    INSERT INTO
9cc0: 20 63 32 20 56 41 4c 55 45 53 28 27 64 27 2c 20   c2 VALUES('d', 
9cd0: 27 65 27 2c 20 27 66 27 29 3b 0a 20 20 20 20 49  'e', 'f');.    I
9ce0: 4e 53 45 52 54 20 49 4e 54 4f 20 63 33 20 56 41  NSERT INTO c3 VA
9cf0: 4c 55 45 53 28 27 67 27 2c 20 27 68 27 2c 20 27  LUES('g', 'h', '
9d00: 69 27 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20  i');.    INSERT 
9d10: 49 4e 54 4f 20 63 34 20 56 41 4c 55 45 53 28 27  INTO c4 VALUES('
9d20: 6a 27 2c 20 27 6b 27 2c 20 27 6c 27 29 3b 0a 20  j', 'k', 'l');. 
9d30: 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 63     INSERT INTO c
9d40: 35 20 56 41 4c 55 45 53 28 27 6d 27 2c 20 27 6e  5 VALUES('m', 'n
9d50: 27 2c 20 27 6f 27 29 3b 0a 20 20 20 20 49 4e 53  ', 'o');.    INS
9d60: 45 52 54 20 49 4e 54 4f 20 63 36 20 56 41 4c 55  ERT INTO c6 VALU
9d70: 45 53 28 27 70 27 2c 20 27 71 27 2c 20 27 72 27  ES('p', 'q', 'r'
9d80: 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e  );.    INSERT IN
9d90: 54 4f 20 63 37 20 56 41 4c 55 45 53 28 27 73 27  TO c7 VALUES('s'
9da0: 2c 20 27 74 27 2c 20 27 75 27 29 3b 0a 20 20 7d  , 't', 'u');.  }
9db0: 0a 7d 20 7b 7d 0a 0a 70 72 6f 63 20 74 65 73 74  .} {}..proc test
9dc0: 5f 65 66 6b 65 79 5f 32 39 20 7b 74 6e 20 73 71  _efkey_29 {tn sq
9dd0: 6c 20 69 73 45 72 72 6f 72 7d 20 7b 0a 20 20 64  l isError} {.  d
9de0: 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 33 34  o_test e_fkey-34
9df0: 2e 24 74 6e 20 22 63 61 74 63 68 73 71 6c 20 7b  .$tn "catchsql {
9e00: 24 73 71 6c 7d 22 20 5b 0a 20 20 20 20 6c 69 6e  $sql}" [.    lin
9e10: 64 65 78 20 7b 7b 30 20 7b 7d 7d 20 7b 31 20 7b  dex {{0 {}} {1 {
9e20: 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73  foreign key cons
9e30: 74 72 61 69 6e 74 20 66 61 69 6c 65 64 7d 7d 7d  traint failed}}}
9e40: 20 24 69 73 45 72 72 6f 72 0a 20 20 5d 0a 7d 0a   $isError.  ].}.
9e50: 74 65 73 74 5f 65 66 6b 65 79 5f 32 39 20 20 32  test_efkey_29  2
9e60: 20 22 42 45 47 49 4e 22 20 20 20 20 20 20 20 20   "BEGIN"        
9e70: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9e80: 20 20 20 20 20 20 20 20 20 20 20 30 0a 74 65 73             0.tes
9e90: 74 5f 65 66 6b 65 79 5f 32 39 20 20 33 20 22 44  t_efkey_29  3 "D
9ea0: 45 4c 45 54 45 20 46 52 4f 4d 20 70 61 72 65 6e  ELETE FROM paren
9eb0: 74 20 57 48 45 52 45 20 78 20 3d 20 27 61 27 22  t WHERE x = 'a'"
9ec0: 20 20 20 20 20 20 20 20 31 0a 74 65 73 74 5f 65          1.test_e
9ed0: 66 6b 65 79 5f 32 39 20 20 34 20 22 44 45 4c 45  fkey_29  4 "DELE
9ee0: 54 45 20 46 52 4f 4d 20 70 61 72 65 6e 74 20 57  TE FROM parent W
9ef0: 48 45 52 45 20 78 20 3d 20 27 64 27 22 20 20 20  HERE x = 'd'"   
9f00: 20 20 20 20 20 31 0a 74 65 73 74 5f 65 66 6b 65       1.test_efke
9f10: 79 5f 32 39 20 20 35 20 22 44 45 4c 45 54 45 20  y_29  5 "DELETE 
9f20: 46 52 4f 4d 20 70 61 72 65 6e 74 20 57 48 45 52  FROM parent WHER
9f30: 45 20 78 20 3d 20 27 67 27 22 20 20 20 20 20 20  E x = 'g'"      
9f40: 20 20 31 0a 74 65 73 74 5f 65 66 6b 65 79 5f 32    1.test_efkey_2
9f50: 39 20 20 36 20 22 44 45 4c 45 54 45 20 46 52 4f  9  6 "DELETE FRO
9f60: 4d 20 70 61 72 65 6e 74 20 57 48 45 52 45 20 78  M parent WHERE x
9f70: 20 3d 20 27 6a 27 22 20 20 20 20 20 20 20 20 31   = 'j'"        1
9f80: 0a 74 65 73 74 5f 65 66 6b 65 79 5f 32 39 20 20  .test_efkey_29  
9f90: 37 20 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 70  7 "DELETE FROM p
9fa0: 61 72 65 6e 74 20 57 48 45 52 45 20 78 20 3d 20  arent WHERE x = 
9fb0: 27 6d 27 22 20 20 20 20 20 20 20 20 31 0a 74 65  'm'"        1.te
9fc0: 73 74 5f 65 66 6b 65 79 5f 32 39 20 20 38 20 22  st_efkey_29  8 "
9fd0: 44 45 4c 45 54 45 20 46 52 4f 4d 20 70 61 72 65  DELETE FROM pare
9fe0: 6e 74 20 57 48 45 52 45 20 78 20 3d 20 27 70 27  nt WHERE x = 'p'
9ff0: 22 20 20 20 20 20 20 20 20 31 0a 74 65 73 74 5f  "        1.test_
a000: 65 66 6b 65 79 5f 32 39 20 20 39 20 22 44 45 4c  efkey_29  9 "DEL
a010: 45 54 45 20 46 52 4f 4d 20 70 61 72 65 6e 74 20  ETE FROM parent 
a020: 57 48 45 52 45 20 78 20 3d 20 27 73 27 22 20 20  WHERE x = 's'"  
a030: 20 20 20 20 20 20 30 0a 74 65 73 74 5f 65 66 6b        0.test_efk
a040: 65 79 5f 32 39 20 31 30 20 22 43 4f 4d 4d 49 54  ey_29 10 "COMMIT
a050: 22 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  "               
a060: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a070: 20 20 20 31 0a 74 65 73 74 5f 65 66 6b 65 79 5f     1.test_efkey_
a080: 32 39 20 31 31 20 22 52 4f 4c 4c 42 41 43 4b 22  29 11 "ROLLBACK"
a090: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a0a0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a0b0: 30 0a 0a 74 65 73 74 5f 65 66 6b 65 79 5f 32 39  0..test_efkey_29
a0c0: 20 20 39 20 22 42 45 47 49 4e 22 20 20 20 20 20    9 "BEGIN"     
a0d0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a0e0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 0a                0.
a0f0: 74 65 73 74 5f 65 66 6b 65 79 5f 32 39 20 31 30  test_efkey_29 10
a100: 20 22 55 50 44 41 54 45 20 70 61 72 65 6e 74 20   "UPDATE parent 
a110: 53 45 54 20 7a 20 3d 20 27 7a 27 20 57 48 45 52  SET z = 'z' WHER
a120: 45 20 7a 20 3d 20 27 63 27 22 20 31 0a 74 65 73  E z = 'c'" 1.tes
a130: 74 5f 65 66 6b 65 79 5f 32 39 20 31 31 20 22 55  t_efkey_29 11 "U
a140: 50 44 41 54 45 20 70 61 72 65 6e 74 20 53 45 54  PDATE parent SET
a150: 20 7a 20 3d 20 27 7a 27 20 57 48 45 52 45 20 7a   z = 'z' WHERE z
a160: 20 3d 20 27 66 27 22 20 31 0a 74 65 73 74 5f 65   = 'f'" 1.test_e
a170: 66 6b 65 79 5f 32 39 20 31 32 20 22 55 50 44 41  fkey_29 12 "UPDA
a180: 54 45 20 70 61 72 65 6e 74 20 53 45 54 20 7a 20  TE parent SET z 
a190: 3d 20 27 7a 27 20 57 48 45 52 45 20 7a 20 3d 20  = 'z' WHERE z = 
a1a0: 27 69 27 22 20 31 0a 74 65 73 74 5f 65 66 6b 65  'i'" 1.test_efke
a1b0: 79 5f 32 39 20 31 33 20 22 55 50 44 41 54 45 20  y_29 13 "UPDATE 
a1c0: 70 61 72 65 6e 74 20 53 45 54 20 7a 20 3d 20 27  parent SET z = '
a1d0: 7a 27 20 57 48 45 52 45 20 7a 20 3d 20 27 6c 27  z' WHERE z = 'l'
a1e0: 22 20 31 0a 74 65 73 74 5f 65 66 6b 65 79 5f 32  " 1.test_efkey_2
a1f0: 39 20 31 34 20 22 55 50 44 41 54 45 20 70 61 72  9 14 "UPDATE par
a200: 65 6e 74 20 53 45 54 20 7a 20 3d 20 27 7a 27 20  ent SET z = 'z' 
a210: 57 48 45 52 45 20 7a 20 3d 20 27 6f 27 22 20 31  WHERE z = 'o'" 1
a220: 0a 74 65 73 74 5f 65 66 6b 65 79 5f 32 39 20 31  .test_efkey_29 1
a230: 35 20 22 55 50 44 41 54 45 20 70 61 72 65 6e 74  5 "UPDATE parent
a240: 20 53 45 54 20 7a 20 3d 20 27 7a 27 20 57 48 45   SET z = 'z' WHE
a250: 52 45 20 7a 20 3d 20 27 72 27 22 20 31 0a 74 65  RE z = 'r'" 1.te
a260: 73 74 5f 65 66 6b 65 79 5f 32 39 20 31 36 20 22  st_efkey_29 16 "
a270: 55 50 44 41 54 45 20 70 61 72 65 6e 74 20 53 45  UPDATE parent SE
a280: 54 20 7a 20 3d 20 27 7a 27 20 57 48 45 52 45 20  T z = 'z' WHERE 
a290: 7a 20 3d 20 27 75 27 22 20 30 0a 74 65 73 74 5f  z = 'u'" 0.test_
a2a0: 65 66 6b 65 79 5f 32 39 20 31 37 20 22 43 4f 4d  efkey_29 17 "COM
a2b0: 4d 49 54 22 20 20 20 20 20 20 20 20 20 20 20 20  MIT"            
a2c0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a2d0: 20 20 20 20 20 20 31 0a 74 65 73 74 5f 65 66 6b        1.test_efk
a2e0: 65 79 5f 32 39 20 31 38 20 22 52 4f 4c 4c 42 41  ey_29 18 "ROLLBA
a2f0: 43 4b 22 20 20 20 20 20 20 20 20 20 20 20 20 20  CK"             
a300: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a310: 20 20 20 30 0a 0a 74 65 73 74 5f 65 66 6b 65 79     0..test_efkey
a320: 5f 32 39 20 31 37 20 22 42 45 47 49 4e 22 20 20  _29 17 "BEGIN"  
a330: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a340: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a350: 20 30 0a 74 65 73 74 5f 65 66 6b 65 79 5f 32 39   0.test_efkey_29
a360: 20 31 38 20 22 49 4e 53 45 52 54 20 49 4e 54 4f   18 "INSERT INTO
a370: 20 63 31 20 56 41 4c 55 45 53 28 31 2c 20 32 2c   c1 VALUES(1, 2,
a380: 20 33 29 22 20 20 20 20 20 20 20 20 20 20 31 0a   3)"          1.
a390: 74 65 73 74 5f 65 66 6b 65 79 5f 32 39 20 31 39  test_efkey_29 19
a3a0: 20 22 49 4e 53 45 52 54 20 49 4e 54 4f 20 63 32   "INSERT INTO c2
a3b0: 20 56 41 4c 55 45 53 28 31 2c 20 32 2c 20 33 29   VALUES(1, 2, 3)
a3c0: 22 20 20 20 20 20 20 20 20 20 20 31 0a 74 65 73  "          1.tes
a3d0: 74 5f 65 66 6b 65 79 5f 32 39 20 32 30 20 22 49  t_efkey_29 20 "I
a3e0: 4e 53 45 52 54 20 49 4e 54 4f 20 63 33 20 56 41  NSERT INTO c3 VA
a3f0: 4c 55 45 53 28 31 2c 20 32 2c 20 33 29 22 20 20  LUES(1, 2, 3)"  
a400: 20 20 20 20 20 20 20 20 31 0a 74 65 73 74 5f 65          1.test_e
a410: 66 6b 65 79 5f 32 39 20 32 31 20 22 49 4e 53 45  fkey_29 21 "INSE
a420: 52 54 20 49 4e 54 4f 20 63 34 20 56 41 4c 55 45  RT INTO c4 VALUE
a430: 53 28 31 2c 20 32 2c 20 33 29 22 20 20 20 20 20  S(1, 2, 3)"     
a440: 20 20 20 20 20 31 0a 74 65 73 74 5f 65 66 6b 65       1.test_efke
a450: 79 5f 32 39 20 32 32 20 22 49 4e 53 45 52 54 20  y_29 22 "INSERT 
a460: 49 4e 54 4f 20 63 35 20 56 41 4c 55 45 53 28 31  INTO c5 VALUES(1
a470: 2c 20 32 2c 20 33 29 22 20 20 20 20 20 20 20 20  , 2, 3)"        
a480: 20 20 31 0a 74 65 73 74 5f 65 66 6b 65 79 5f 32    1.test_efkey_2
a490: 39 20 32 32 20 22 49 4e 53 45 52 54 20 49 4e 54  9 22 "INSERT INT
a4a0: 4f 20 63 36 20 56 41 4c 55 45 53 28 31 2c 20 32  O c6 VALUES(1, 2
a4b0: 2c 20 33 29 22 20 20 20 20 20 20 20 20 20 20 31  , 3)"          1
a4c0: 0a 74 65 73 74 5f 65 66 6b 65 79 5f 32 39 20 32  .test_efkey_29 2
a4d0: 32 20 22 49 4e 53 45 52 54 20 49 4e 54 4f 20 63  2 "INSERT INTO c
a4e0: 37 20 56 41 4c 55 45 53 28 31 2c 20 32 2c 20 33  7 VALUES(1, 2, 3
a4f0: 29 22 20 20 20 20 20 20 20 20 20 20 30 0a 74 65  )"          0.te
a500: 73 74 5f 65 66 6b 65 79 5f 32 39 20 32 33 20 22  st_efkey_29 23 "
a510: 43 4f 4d 4d 49 54 22 20 20 20 20 20 20 20 20 20  COMMIT"         
a520: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a530: 20 20 20 20 20 20 20 20 20 31 0a 74 65 73 74 5f           1.test_
a540: 65 66 6b 65 79 5f 32 39 20 32 34 20 22 49 4e 53  efkey_29 24 "INS
a550: 45 52 54 20 49 4e 54 4f 20 70 61 72 65 6e 74 20  ERT INTO parent 
a560: 56 41 4c 55 45 53 28 31 2c 20 32 2c 20 33 29 22  VALUES(1, 2, 3)"
a570: 20 20 20 20 20 20 30 0a 74 65 73 74 5f 65 66 6b        0.test_efk
a580: 65 79 5f 32 39 20 32 35 20 22 43 4f 4d 4d 49 54  ey_29 25 "COMMIT
a590: 22 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  "               
a5a0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a5b0: 20 20 20 30 0a 0a 74 65 73 74 5f 65 66 6b 65 79     0..test_efkey
a5c0: 5f 32 39 20 32 36 20 22 42 45 47 49 4e 22 20 20  _29 26 "BEGIN"  
a5d0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a5e0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a5f0: 20 30 0a 74 65 73 74 5f 65 66 6b 65 79 5f 32 39   0.test_efkey_29
a600: 20 32 37 20 22 55 50 44 41 54 45 20 63 31 20 53   27 "UPDATE c1 S
a610: 45 54 20 61 20 3d 20 31 30 22 20 20 20 20 20 20  ET a = 10"      
a620: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 0a                1.
a630: 74 65 73 74 5f 65 66 6b 65 79 5f 32 39 20 32 38  test_efkey_29 28
a640: 20 22 55 50 44 41 54 45 20 63 32 20 53 45 54 20   "UPDATE c2 SET 
a650: 61 20 3d 20 31 30 22 20 20 20 20 20 20 20 20 20  a = 10"         
a660: 20 20 20 20 20 20 20 20 20 20 20 31 0a 74 65 73             1.tes
a670: 74 5f 65 66 6b 65 79 5f 32 39 20 32 39 20 22 55  t_efkey_29 29 "U
a680: 50 44 41 54 45 20 63 33 20 53 45 54 20 61 20 3d  PDATE c3 SET a =
a690: 20 31 30 22 20 20 20 20 20 20 20 20 20 20 20 20   10"            
a6a0: 20 20 20 20 20 20 20 20 31 0a 74 65 73 74 5f 65          1.test_e
a6b0: 66 6b 65 79 5f 32 39 20 33 30 20 22 55 50 44 41  fkey_29 30 "UPDA
a6c0: 54 45 20 63 34 20 53 45 54 20 61 20 3d 20 31 30  TE c4 SET a = 10
a6d0: 22 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  "               
a6e0: 20 20 20 20 20 31 0a 74 65 73 74 5f 65 66 6b 65       1.test_efke
a6f0: 79 5f 32 39 20 33 31 20 22 55 50 44 41 54 45 20  y_29 31 "UPDATE 
a700: 63 35 20 53 45 54 20 61 20 3d 20 31 30 22 20 20  c5 SET a = 10"  
a710: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a720: 20 20 31 0a 74 65 73 74 5f 65 66 6b 65 79 5f 32    1.test_efkey_2
a730: 39 20 33 31 20 22 55 50 44 41 54 45 20 63 36 20  9 31 "UPDATE c6 
a740: 53 45 54 20 61 20 3d 20 31 30 22 20 20 20 20 20  SET a = 10"     
a750: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
a760: 0a 74 65 73 74 5f 65 66 6b 65 79 5f 32 39 20 33  .test_efkey_29 3
a770: 31 20 22 55 50 44 41 54 45 20 63 37 20 53 45 54  1 "UPDATE c7 SET
a780: 20 61 20 3d 20 31 30 22 20 20 20 20 20 20 20 20   a = 10"        
a790: 20 20 20 20 20 20 20 20 20 20 20 20 30 0a 74 65              0.te
a7a0: 73 74 5f 65 66 6b 65 79 5f 32 39 20 33 32 20 22  st_efkey_29 32 "
a7b0: 43 4f 4d 4d 49 54 22 20 20 20 20 20 20 20 20 20  COMMIT"         
a7c0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a7d0: 20 20 20 20 20 20 20 20 20 31 0a 74 65 73 74 5f           1.test_
a7e0: 65 66 6b 65 79 5f 32 39 20 33 33 20 22 52 4f 4c  efkey_29 33 "ROL
a7f0: 4c 42 41 43 4b 22 20 20 20 20 20 20 20 20 20 20  LBACK"          
a800: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a810: 20 20 20 20 20 20 30 0a 0a 23 2d 2d 2d 2d 2d 2d        0..#------
a820: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
a830: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
a840: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
a850: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
a860: 2d 2d 2d 0a 23 20 2f 2a 20 45 56 3a 20 52 2d 33  ---.# /* EV: R-3
a870: 35 30 34 33 2d 30 31 35 34 36 20 2a 2f 0a 23 0a  5043-01546 */.#.
a880: 23 20 54 65 73 74 20 61 6e 20 65 78 61 6d 70 6c  # Test an exampl
a890: 65 20 66 72 6f 6d 20 66 6f 72 65 69 67 6e 6b 65  e from foreignke
a8a0: 79 73 2e 68 74 6d 6c 20 64 65 61 6c 69 6e 67 20  ys.html dealing 
a8b0: 77 69 74 68 20 61 20 64 65 66 65 72 72 65 64 20  with a deferred 
a8c0: 66 6f 72 65 69 67 6e 20 0a 23 20 6b 65 79 20 63  foreign .# key c
a8d0: 6f 6e 73 74 72 61 69 6e 74 2e 0a 23 0a 64 6f 5f  onstraint..#.do_
a8e0: 74 65 73 74 20 65 5f 66 6b 65 79 2d 33 35 2e 31  test e_fkey-35.1
a8f0: 20 7b 0a 20 20 64 72 6f 70 5f 61 6c 6c 5f 74 61   {.  drop_all_ta
a900: 62 6c 65 73 0a 20 20 65 78 65 63 73 71 6c 20 7b  bles.  execsql {
a910: 0a 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c  .    CREATE TABL
a920: 45 20 61 72 74 69 73 74 28 0a 20 20 20 20 20 20  E artist(.      
a930: 61 72 74 69 73 74 69 64 20 20 20 20 49 4e 54 45  artistid    INTE
a940: 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c  GER PRIMARY KEY,
a950: 20 0a 20 20 20 20 20 20 61 72 74 69 73 74 6e 61   .      artistna
a960: 6d 65 20 20 54 45 58 54 0a 20 20 20 20 29 3b 0a  me  TEXT.    );.
a970: 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45      CREATE TABLE
a980: 20 74 72 61 63 6b 28 0a 20 20 20 20 20 20 74 72   track(.      tr
a990: 61 63 6b 69 64 20 20 20 20 20 49 4e 54 45 47 45  ackid     INTEGE
a9a0: 52 2c 0a 20 20 20 20 20 20 74 72 61 63 6b 6e 61  R,.      trackna
a9b0: 6d 65 20 20 20 54 45 58 54 2c 20 0a 20 20 20 20  me   TEXT, .    
a9c0: 20 20 74 72 61 63 6b 61 72 74 69 73 74 20 49 4e    trackartist IN
a9d0: 54 45 47 45 52 20 52 45 46 45 52 45 4e 43 45 53  TEGER REFERENCES
a9e0: 20 61 72 74 69 73 74 28 61 72 74 69 73 74 69 64   artist(artistid
a9f0: 29 20 44 45 46 45 52 52 41 42 4c 45 20 49 4e 49  ) DEFERRABLE INI
aa00: 54 49 41 4c 4c 59 20 44 45 46 45 52 52 45 44 0a  TIALLY DEFERRED.
aa10: 20 20 20 20 29 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a      );.  }.} {}.
aa20: 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 33  do_test e_fkey-3
aa30: 35 2e 32 20 7b 0a 20 20 65 78 65 63 73 71 6c 20  5.2 {.  execsql 
aa40: 7b 0a 20 20 20 20 42 45 47 49 4e 3b 0a 20 20 20  {.    BEGIN;.   
aa50: 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74     INSERT INTO t
aa60: 72 61 63 6b 20 56 41 4c 55 45 53 28 31 2c 20 27  rack VALUES(1, '
aa70: 57 68 69 74 65 20 43 68 72 69 73 74 6d 61 73 27  White Christmas'
aa80: 2c 20 35 29 3b 0a 20 20 7d 0a 20 20 63 61 74 63  , 5);.  }.  catc
aa90: 68 73 71 6c 20 43 4f 4d 4d 49 54 0a 7d 20 7b 31  hsql COMMIT.} {1
aaa0: 20 7b 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f   {foreign key co
aab0: 6e 73 74 72 61 69 6e 74 20 66 61 69 6c 65 64 7d  nstraint failed}
aac0: 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79  }.do_test e_fkey
aad0: 2d 33 35 2e 33 20 7b 0a 20 20 65 78 65 63 73 71  -35.3 {.  execsq
aae0: 6c 20 7b 0a 20 20 20 20 49 4e 53 45 52 54 20 49  l {.    INSERT I
aaf0: 4e 54 4f 20 61 72 74 69 73 74 20 56 41 4c 55 45  NTO artist VALUE
ab00: 53 28 35 2c 20 27 42 69 6e 67 20 43 72 6f 73 62  S(5, 'Bing Crosb
ab10: 79 27 29 3b 0a 20 20 20 20 43 4f 4d 4d 49 54 3b  y');.    COMMIT;
ab20: 0a 20 20 7d 0a 7d 20 7b 7d 0a 0a 23 2d 2d 2d 2d  .  }.} {}..#----
ab30: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
ab40: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
ab50: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
ab60: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
ab70: 2d 2d 2d 2d 2d 0a 23 20 2f 2a 20 45 56 3a 20 52  -----.# /* EV: R
ab80: 2d 30 37 32 32 33 2d 34 38 33 32 33 20 2a 2f 0a  -07223-48323 */.
ab90: 23 0a 23 20 56 65 72 69 66 79 20 74 68 61 74 20  #.# Verify that 
aba0: 61 20 6e 65 73 74 65 64 20 73 61 76 65 70 6f 69  a nested savepoi
abb0: 6e 74 20 6d 61 79 20 62 65 20 72 65 6c 65 61 73  nt may be releas
abc0: 65 64 20 77 69 74 68 6f 75 74 20 73 61 74 69 73  ed without satis
abd0: 66 79 69 6e 67 20 0a 23 20 64 65 66 65 72 72 65  fying .# deferre
abe0: 64 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f  d foreign key co
abf0: 6e 73 74 72 61 69 6e 74 73 2e 0a 23 0a 64 72 6f  nstraints..#.dro
ac00: 70 5f 61 6c 6c 5f 74 61 62 6c 65 73 0a 64 6f 5f  p_all_tables.do_
ac10: 74 65 73 74 20 65 5f 66 6b 65 79 2d 33 36 2e 31  test e_fkey-36.1
ac20: 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20   {.  execsql {. 
ac30: 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20     CREATE TABLE 
ac40: 74 31 28 61 20 50 52 49 4d 41 52 59 20 4b 45 59  t1(a PRIMARY KEY
ac50: 2c 0a 20 20 20 20 20 20 62 20 52 45 46 45 52 45  ,.      b REFERE
ac60: 4e 43 45 53 20 74 31 20 44 45 46 45 52 52 41 42  NCES t1 DEFERRAB
ac70: 4c 45 20 49 4e 49 54 49 41 4c 4c 59 20 44 45 46  LE INITIALLY DEF
ac80: 45 52 52 45 44 0a 20 20 20 20 29 3b 0a 20 20 20  ERRED.    );.   
ac90: 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 31 20   INSERT INTO t1 
aca0: 56 41 4c 55 45 53 28 31 2c 20 31 29 3b 0a 20 20  VALUES(1, 1);.  
acb0: 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 31    INSERT INTO t1
acc0: 20 56 41 4c 55 45 53 28 32 2c 20 32 29 3b 0a 20   VALUES(2, 2);. 
acd0: 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74     INSERT INTO t
ace0: 31 20 56 41 4c 55 45 53 28 33 2c 20 33 29 3b 0a  1 VALUES(3, 3);.
acf0: 20 20 7d 0a 7d 20 7b 7d 0a 64 6f 5f 74 65 73 74    }.} {}.do_test
ad00: 20 65 5f 66 6b 65 79 2d 33 36 2e 32 20 7b 0a 20   e_fkey-36.2 {. 
ad10: 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 42   execsql {.    B
ad20: 45 47 49 4e 3b 0a 20 20 20 20 20 20 53 41 56 45  EGIN;.      SAVE
ad30: 50 4f 49 4e 54 20 6f 6e 65 3b 0a 20 20 20 20 20  POINT one;.     
ad40: 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74     INSERT INTO t
ad50: 31 20 56 41 4c 55 45 53 28 34 2c 20 35 29 3b 0a  1 VALUES(4, 5);.
ad60: 20 20 20 20 20 20 52 45 4c 45 41 53 45 20 6f 6e        RELEASE on
ad70: 65 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a 64 6f 5f 74  e;.  }.} {}.do_t
ad80: 65 73 74 20 65 5f 66 6b 65 79 2d 33 36 2e 33 20  est e_fkey-36.3 
ad90: 7b 0a 20 20 63 61 74 63 68 73 71 6c 20 43 4f 4d  {.  catchsql COM
ada0: 4d 49 54 0a 7d 20 7b 31 20 7b 66 6f 72 65 69 67  MIT.} {1 {foreig
adb0: 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74  n key constraint
adc0: 20 66 61 69 6c 65 64 7d 7d 0a 64 6f 5f 74 65 73   failed}}.do_tes
add0: 74 20 65 5f 66 6b 65 79 2d 33 36 2e 34 20 7b 0a  t e_fkey-36.4 {.
ade0: 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20    execsql {.    
adf0: 55 50 44 41 54 45 20 74 31 20 53 45 54 20 61 20  UPDATE t1 SET a 
ae00: 3d 20 35 20 57 48 45 52 45 20 61 20 3d 20 34 3b  = 5 WHERE a = 4;
ae10: 0a 20 20 20 20 43 4f 4d 4d 49 54 3b 0a 20 20 7d  .    COMMIT;.  }
ae20: 0a 7d 20 7b 7d 0a 0a 0a 23 2d 2d 2d 2d 2d 2d 2d  .} {}...#-------
ae30: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
ae40: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
ae50: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
ae60: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
ae70: 2d 2d 0a 23 20 2f 2a 20 45 56 3a 20 52 2d 34 34  --.# /* EV: R-44
ae80: 32 39 35 2d 31 33 38 32 33 20 2a 2f 0a 23 0a 23  295-13823 */.#.#
ae90: 20 43 68 65 63 6b 20 74 68 61 74 20 61 20 74 72   Check that a tr
aea0: 61 6e 73 61 63 74 69 6f 6e 20 73 61 76 65 70 6f  ansaction savepo
aeb0: 69 6e 74 20 28 61 6e 20 6f 75 74 65 72 6d 6f 73  int (an outermos
aec0: 74 20 73 61 76 65 70 6f 69 6e 74 20 6f 70 65 6e  t savepoint open
aed0: 65 64 20 77 68 65 6e 0a 23 20 74 68 65 20 64 61  ed when.# the da
aee0: 74 61 62 61 73 65 20 77 61 73 20 69 6e 20 61 75  tabase was in au
aef0: 74 6f 2d 63 6f 6d 6d 69 74 20 6d 6f 64 65 29 20  to-commit mode) 
af00: 63 61 6e 6e 6f 74 20 62 65 20 72 65 6c 65 61 73  cannot be releas
af10: 65 64 20 77 69 74 68 6f 75 74 0a 23 20 73 61 74  ed without.# sat
af20: 69 73 66 79 69 6e 67 20 64 65 66 65 72 72 65 64  isfying deferred
af30: 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e   foreign key con
af40: 73 74 72 61 69 6e 74 73 2e 20 49 74 20 6d 61 79  straints. It may
af50: 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e   be rolled back.
af60: 0a 23 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65  .#.do_test e_fke
af70: 79 2d 33 37 2e 31 20 7b 0a 20 20 65 78 65 63 73  y-37.1 {.  execs
af80: 71 6c 20 7b 0a 20 20 20 20 53 41 56 45 50 4f 49  ql {.    SAVEPOI
af90: 4e 54 20 6f 6e 65 3b 0a 20 20 20 20 20 20 53 41  NT one;.      SA
afa0: 56 45 50 4f 49 4e 54 20 74 77 6f 3b 0a 20 20 20  VEPOINT two;.   
afb0: 20 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f       INSERT INTO
afc0: 20 74 31 20 56 41 4c 55 45 53 28 36 2c 20 37 29   t1 VALUES(6, 7)
afd0: 3b 0a 20 20 20 20 20 20 52 45 4c 45 41 53 45 20  ;.      RELEASE 
afe0: 74 77 6f 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a 64 6f  two;.  }.} {}.do
aff0: 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 33 37 2e  _test e_fkey-37.
b000: 32 20 7b 0a 20 20 63 61 74 63 68 73 71 6c 20 7b  2 {.  catchsql {
b010: 52 45 4c 45 41 53 45 20 6f 6e 65 7d 0a 7d 20 7b  RELEASE one}.} {
b020: 31 20 7b 66 6f 72 65 69 67 6e 20 6b 65 79 20 63  1 {foreign key c
b030: 6f 6e 73 74 72 61 69 6e 74 20 66 61 69 6c 65 64  onstraint failed
b040: 7d 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65  }}.do_test e_fke
b050: 79 2d 33 37 2e 33 20 7b 0a 20 20 65 78 65 63 73  y-37.3 {.  execs
b060: 71 6c 20 7b 0a 20 20 20 20 20 20 55 50 44 41 54  ql {.      UPDAT
b070: 45 20 74 31 20 53 45 54 20 61 20 3d 20 37 20 57  E t1 SET a = 7 W
b080: 48 45 52 45 20 61 20 3d 20 36 3b 0a 20 20 20 20  HERE a = 6;.    
b090: 52 45 4c 45 41 53 45 20 6f 6e 65 3b 0a 20 20 7d  RELEASE one;.  }
b0a0: 0a 7d 20 7b 7d 0a 64 6f 5f 74 65 73 74 20 65 5f  .} {}.do_test e_
b0b0: 66 6b 65 79 2d 33 37 2e 34 20 7b 0a 20 20 65 78  fkey-37.4 {.  ex
b0c0: 65 63 73 71 6c 20 7b 0a 20 20 20 20 53 41 56 45  ecsql {.    SAVE
b0d0: 50 4f 49 4e 54 20 6f 6e 65 3b 0a 20 20 20 20 20  POINT one;.     
b0e0: 20 53 41 56 45 50 4f 49 4e 54 20 74 77 6f 3b 0a   SAVEPOINT two;.
b0f0: 20 20 20 20 20 20 20 20 49 4e 53 45 52 54 20 49          INSERT I
b100: 4e 54 4f 20 74 31 20 56 41 4c 55 45 53 28 39 2c  NTO t1 VALUES(9,
b110: 20 31 30 29 3b 0a 20 20 20 20 20 20 52 45 4c 45   10);.      RELE
b120: 41 53 45 20 74 77 6f 3b 0a 20 20 7d 0a 7d 20 7b  ASE two;.  }.} {
b130: 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79  }.do_test e_fkey
b140: 2d 33 37 2e 35 20 7b 0a 20 20 63 61 74 63 68 73  -37.5 {.  catchs
b150: 71 6c 20 7b 52 45 4c 45 41 53 45 20 6f 6e 65 7d  ql {RELEASE one}
b160: 0a 7d 20 7b 31 20 7b 66 6f 72 65 69 67 6e 20 6b  .} {1 {foreign k
b170: 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 20 66 61  ey constraint fa
b180: 69 6c 65 64 7d 7d 0a 64 6f 5f 74 65 73 74 20 65  iled}}.do_test e
b190: 5f 66 6b 65 79 2d 33 37 2e 36 20 7b 0a 20 20 65  _fkey-37.6 {.  e
b1a0: 78 65 63 73 71 6c 20 7b 52 4f 4c 4c 42 41 43 4b  xecsql {ROLLBACK
b1b0: 20 54 4f 20 6f 6e 65 20 3b 20 52 45 4c 45 41 53   TO one ; RELEAS
b1c0: 45 20 6f 6e 65 7d 0a 7d 20 7b 7d 0a 0a 23 2d 2d  E one}.} {}..#--
b1d0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
b1e0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
b1f0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
b200: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
b210: 2d 2d 2d 2d 2d 2d 2d 0a 23 20 2f 2a 20 45 56 3a  -------.# /* EV:
b220: 20 52 2d 33 37 37 33 36 2d 34 32 36 31 36 20 2a   R-37736-42616 *
b230: 2f 0a 23 0a 23 20 54 65 73 74 20 74 68 61 74 20  /.#.# Test that 
b240: 69 66 20 61 20 43 4f 4d 4d 49 54 20 6f 70 65 72  if a COMMIT oper
b250: 61 74 69 6f 6e 20 66 61 69 6c 73 20 64 75 65 20  ation fails due 
b260: 74 6f 20 64 65 66 65 72 72 65 64 20 66 6f 72 65  to deferred fore
b270: 69 67 6e 20 6b 65 79 20 0a 23 20 63 6f 6e 73 74  ign key .# const
b280: 72 61 69 6e 74 73 2c 20 61 6e 79 20 6e 65 73 74  raints, any nest
b290: 65 64 20 73 61 76 65 70 6f 69 6e 74 73 20 72 65  ed savepoints re
b2a0: 6d 61 69 6e 20 6f 70 65 6e 2e 0a 23 0a 64 6f 5f  main open..#.do_
b2b0: 74 65 73 74 20 65 5f 66 6b 65 79 2d 33 38 2e 31  test e_fkey-38.1
b2c0: 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20   {.  execsql {. 
b2d0: 20 20 20 44 45 4c 45 54 45 20 46 52 4f 4d 20 74     DELETE FROM t
b2e0: 31 20 57 48 45 52 45 20 61 3e 33 3b 0a 20 20 20  1 WHERE a>3;.   
b2f0: 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74   SELECT * FROM t
b300: 31 3b 0a 20 20 7d 0a 7d 20 7b 31 20 31 20 32 20  1;.  }.} {1 1 2 
b310: 32 20 33 20 33 7d 0a 64 6f 5f 74 65 73 74 20 65  2 3 3}.do_test e
b320: 5f 66 6b 65 79 2d 33 38 2e 32 20 7b 0a 20 20 65  _fkey-38.2 {.  e
b330: 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 42 45 47  xecsql {.    BEG
b340: 49 4e 3b 0a 20 20 20 20 20 20 49 4e 53 45 52 54  IN;.      INSERT
b350: 20 49 4e 54 4f 20 74 31 20 56 41 4c 55 45 53 28   INTO t1 VALUES(
b360: 34 2c 20 34 29 3b 0a 20 20 20 20 20 20 53 41 56  4, 4);.      SAV
b370: 45 50 4f 49 4e 54 20 6f 6e 65 3b 0a 20 20 20 20  EPOINT one;.    
b380: 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20      INSERT INTO 
b390: 74 31 20 56 41 4c 55 45 53 28 35 2c 20 36 29 3b  t1 VALUES(5, 6);
b3a0: 0a 20 20 20 20 20 20 20 20 53 45 4c 45 43 54 20  .        SELECT 
b3b0: 2a 20 46 52 4f 4d 20 74 31 3b 0a 20 20 7d 0a 7d  * FROM t1;.  }.}
b3c0: 20 7b 31 20 31 20 32 20 32 20 33 20 33 20 34 20   {1 1 2 2 3 3 4 
b3d0: 34 20 35 20 36 7d 0a 64 6f 5f 74 65 73 74 20 65  4 5 6}.do_test e
b3e0: 5f 66 6b 65 79 2d 33 38 2e 33 20 7b 0a 20 20 63  _fkey-38.3 {.  c
b3f0: 61 74 63 68 73 71 6c 20 43 4f 4d 4d 49 54 0a 7d  atchsql COMMIT.}
b400: 20 7b 31 20 7b 66 6f 72 65 69 67 6e 20 6b 65 79   {1 {foreign key
b410: 20 63 6f 6e 73 74 72 61 69 6e 74 20 66 61 69 6c   constraint fail
b420: 65 64 7d 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66  ed}}.do_test e_f
b430: 6b 65 79 2d 33 38 2e 34 20 7b 0a 20 20 65 78 65  key-38.4 {.  exe
b440: 63 73 71 6c 20 7b 0a 20 20 20 20 52 4f 4c 4c 42  csql {.    ROLLB
b450: 41 43 4b 20 54 4f 20 6f 6e 65 3b 0a 20 20 20 20  ACK TO one;.    
b460: 43 4f 4d 4d 49 54 3b 0a 20 20 20 20 53 45 4c 45  COMMIT;.    SELE
b470: 43 54 20 2a 20 46 52 4f 4d 20 74 31 3b 0a 20 20  CT * FROM t1;.  
b480: 7d 0a 7d 20 7b 31 20 31 20 32 20 32 20 33 20 33  }.} {1 1 2 2 3 3
b490: 20 34 20 34 7d 0a 0a 64 6f 5f 74 65 73 74 20 65   4 4}..do_test e
b4a0: 5f 66 6b 65 79 2d 33 38 2e 35 20 7b 0a 20 20 65  _fkey-38.5 {.  e
b4b0: 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 53 41 56  xecsql {.    SAV
b4c0: 45 50 4f 49 4e 54 20 61 3b 0a 20 20 20 20 20 20  EPOINT a;.      
b4d0: 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 31 20 56  INSERT INTO t1 V
b4e0: 41 4c 55 45 53 28 35 2c 20 35 29 3b 0a 20 20 20  ALUES(5, 5);.   
b4f0: 20 20 20 53 41 56 45 50 4f 49 4e 54 20 62 3b 0a     SAVEPOINT b;.
b500: 20 20 20 20 20 20 20 20 49 4e 53 45 52 54 20 49          INSERT I
b510: 4e 54 4f 20 74 31 20 56 41 4c 55 45 53 28 36 2c  NTO t1 VALUES(6,
b520: 20 37 29 3b 0a 20 20 20 20 20 20 20 20 53 41 56   7);.        SAV
b530: 45 50 4f 49 4e 54 20 63 3b 0a 20 20 20 20 20 20  EPOINT c;.      
b540: 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20      INSERT INTO 
b550: 74 31 20 56 41 4c 55 45 53 28 37 2c 20 38 29 3b  t1 VALUES(7, 8);
b560: 0a 20 20 7d 0a 7d 20 7b 7d 0a 64 6f 5f 74 65 73  .  }.} {}.do_tes
b570: 74 20 65 5f 66 6b 65 79 2d 33 38 2e 36 20 7b 0a  t e_fkey-38.6 {.
b580: 20 20 63 61 74 63 68 73 71 6c 20 7b 52 45 4c 45    catchsql {RELE
b590: 41 53 45 20 61 7d 0a 7d 20 7b 31 20 7b 66 6f 72  ASE a}.} {1 {for
b5a0: 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61  eign key constra
b5b0: 69 6e 74 20 66 61 69 6c 65 64 7d 7d 0a 64 6f 5f  int failed}}.do_
b5c0: 74 65 73 74 20 65 5f 66 6b 65 79 2d 33 38 2e 37  test e_fkey-38.7
b5d0: 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 20 7b 52   {.  execsql  {R
b5e0: 4f 4c 4c 42 41 43 4b 20 54 4f 20 63 7d 0a 20 20  OLLBACK TO c}.  
b5f0: 63 61 74 63 68 73 71 6c 20 7b 52 45 4c 45 41 53  catchsql {RELEAS
b600: 45 20 61 7d 0a 7d 20 7b 31 20 7b 66 6f 72 65 69  E a}.} {1 {forei
b610: 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e  gn key constrain
b620: 74 20 66 61 69 6c 65 64 7d 7d 0a 64 6f 5f 74 65  t failed}}.do_te
b630: 73 74 20 65 5f 66 6b 65 79 2d 33 38 2e 38 20 7b  st e_fkey-38.8 {
b640: 0a 20 20 65 78 65 63 73 71 6c 20 20 7b 0a 20 20  .  execsql  {.  
b650: 20 20 52 4f 4c 4c 42 41 43 4b 20 54 4f 20 62 3b    ROLLBACK TO b;
b660: 0a 20 20 20 20 52 45 4c 45 41 53 45 20 61 3b 0a  .    RELEASE a;.
b670: 20 20 20 20 53 45 4c 45 43 54 20 2a 20 46 52 4f      SELECT * FRO
b680: 4d 20 74 31 3b 0a 20 20 7d 0a 7d 20 7b 31 20 31  M t1;.  }.} {1 1
b690: 20 32 20 32 20 33 20 33 20 34 20 34 20 35 20 35   2 2 3 3 4 4 5 5
b6a0: 7d 0a 0a 23 23 23 23 23 23 23 23 23 23 23 23 23  }..#############
b6b0: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
b6c0: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
b6d0: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
b6e0: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 0a 23  ##############.#
b6f0: 23 23 20 53 45 43 54 49 4f 4e 20 34 2e 33 3a 20  ## SECTION 4.3: 
b700: 4f 4e 20 44 45 4c 45 54 45 20 61 6e 64 20 4f 4e  ON DELETE and ON
b710: 20 55 50 44 41 54 45 20 41 63 74 69 6f 6e 73 0a   UPDATE Actions.
b720: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
b730: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
b740: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
b750: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
b760: 23 23 23 23 23 23 23 23 23 23 23 0a 0a 23 2d 2d  ###########..#--
b770: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
b780: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
b790: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
b7a0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
b7b0: 2d 2d 2d 2d 2d 2d 2d 0a 23 20 2f 2a 20 45 56 3a  -------.# /* EV:
b7c0: 20 52 2d 34 38 32 37 30 2d 34 34 32 38 32 20 2a   R-48270-44282 *
b7d0: 2f 0a 23 0a 23 20 54 65 73 74 20 74 68 61 74 20  /.#.# Test that 
b7e0: 63 6f 6e 66 69 67 75 72 65 64 20 4f 4e 20 44 45  configured ON DE
b7f0: 4c 45 54 45 20 61 6e 64 20 4f 4e 20 55 50 44 41  LETE and ON UPDA
b800: 54 45 20 61 63 74 69 6f 6e 73 20 74 61 6b 65 20  TE actions take 
b810: 70 6c 61 63 65 20 77 68 65 6e 0a 23 20 64 65 6c  place when.# del
b820: 65 74 69 6e 67 20 6f 72 20 6d 6f 64 69 66 79 69  eting or modifyi
b830: 6e 67 20 72 6f 77 73 20 6f 66 20 74 68 65 20 70  ng rows of the p
b840: 61 72 65 6e 74 20 74 61 62 6c 65 2c 20 72 65 73  arent table, res
b850: 70 65 63 74 69 76 65 6c 79 2e 0a 23 0a 23 20 2f  pectively..#.# /
b860: 2a 20 45 56 3a 20 52 2d 34 38 31 32 34 2d 36 33  * EV: R-48124-63
b870: 32 32 35 20 2a 2f 0a 23 0a 23 20 54 65 73 74 20  225 */.#.# Test 
b880: 74 68 61 74 20 61 20 73 69 6e 67 6c 65 20 46 4b  that a single FK
b890: 20 63 6f 6e 73 74 72 61 69 6e 74 20 6d 61 79 20   constraint may 
b8a0: 68 61 76 65 20 64 69 66 66 65 72 65 6e 74 20 61  have different a
b8b0: 63 74 69 6f 6e 73 20 63 6f 6e 66 69 67 75 72 65  ctions configure
b8c0: 64 0a 23 20 66 6f 72 20 4f 4e 20 44 45 4c 45 54  d.# for ON DELET
b8d0: 45 20 61 6e 64 20 4f 4e 20 55 50 44 41 54 45 2e  E and ON UPDATE.
b8e0: 0a 23 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65  .#.do_test e_fke
b8f0: 79 2d 33 39 2e 31 20 7b 0a 20 20 65 78 65 63 73  y-39.1 {.  execs
b900: 71 6c 20 7b 0a 20 20 20 20 43 52 45 41 54 45 20  ql {.    CREATE 
b910: 54 41 42 4c 45 20 70 28 61 2c 20 62 20 50 52 49  TABLE p(a, b PRI
b920: 4d 41 52 59 20 4b 45 59 2c 20 63 29 3b 0a 20 20  MARY KEY, c);.  
b930: 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 63    CREATE TABLE c
b940: 31 28 64 2c 20 65 2c 20 66 20 44 45 46 41 55 4c  1(d, e, f DEFAUL
b950: 54 20 27 6b 30 27 20 52 45 46 45 52 45 4e 43 45  T 'k0' REFERENCE
b960: 53 20 70 20 0a 20 20 20 20 20 20 4f 4e 20 55 50  S p .      ON UP
b970: 44 41 54 45 20 53 45 54 20 44 45 46 41 55 4c 54  DATE SET DEFAULT
b980: 0a 20 20 20 20 20 20 4f 4e 20 44 45 4c 45 54 45  .      ON DELETE
b990: 20 53 45 54 20 4e 55 4c 4c 0a 20 20 20 20 29 3b   SET NULL.    );
b9a0: 0a 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54  ..    INSERT INT
b9b0: 4f 20 70 20 56 41 4c 55 45 53 28 30 2c 20 27 6b  O p VALUES(0, 'k
b9c0: 30 27 2c 20 27 27 29 3b 0a 20 20 20 20 49 4e 53  0', '');.    INS
b9d0: 45 52 54 20 49 4e 54 4f 20 70 20 56 41 4c 55 45  ERT INTO p VALUE
b9e0: 53 28 31 2c 20 27 6b 31 27 2c 20 27 49 27 29 3b  S(1, 'k1', 'I');
b9f0: 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f  .    INSERT INTO
ba00: 20 70 20 56 41 4c 55 45 53 28 32 2c 20 27 6b 32   p VALUES(2, 'k2
ba10: 27 2c 20 27 49 49 27 29 3b 0a 20 20 20 20 49 4e  ', 'II');.    IN
ba20: 53 45 52 54 20 49 4e 54 4f 20 70 20 56 41 4c 55  SERT INTO p VALU
ba30: 45 53 28 33 2c 20 27 6b 33 27 2c 20 27 49 49 49  ES(3, 'k3', 'III
ba40: 27 29 3b 0a 0a 20 20 20 20 49 4e 53 45 52 54 20  ');..    INSERT 
ba50: 49 4e 54 4f 20 63 31 20 56 41 4c 55 45 53 28 31  INTO c1 VALUES(1
ba60: 2c 20 27 78 78 27 2c 20 27 6b 31 27 29 3b 0a 20  , 'xx', 'k1');. 
ba70: 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 63     INSERT INTO c
ba80: 31 20 56 41 4c 55 45 53 28 32 2c 20 27 78 78 27  1 VALUES(2, 'xx'
ba90: 2c 20 27 6b 32 27 29 3b 0a 20 20 20 20 49 4e 53  , 'k2');.    INS
baa0: 45 52 54 20 49 4e 54 4f 20 63 31 20 56 41 4c 55  ERT INTO c1 VALU
bab0: 45 53 28 33 2c 20 27 78 78 27 2c 20 27 6b 33 27  ES(3, 'xx', 'k3'
bac0: 29 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a 64 6f 5f 74  );.  }.} {}.do_t
bad0: 65 73 74 20 65 5f 66 6b 65 79 2d 33 39 2e 32 20  est e_fkey-39.2 
bae0: 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20  {.  execsql {.  
baf0: 20 20 55 50 44 41 54 45 20 70 20 53 45 54 20 62    UPDATE p SET b
bb00: 20 3d 20 27 6b 34 27 20 57 48 45 52 45 20 61 20   = 'k4' WHERE a 
bb10: 3d 20 31 3b 0a 20 20 20 20 53 45 4c 45 43 54 20  = 1;.    SELECT 
bb20: 2a 20 46 52 4f 4d 20 63 31 3b 0a 20 20 7d 0a 7d  * FROM c1;.  }.}
bb30: 20 7b 31 20 78 78 20 6b 30 20 32 20 78 78 20 6b   {1 xx k0 2 xx k
bb40: 32 20 33 20 78 78 20 6b 33 7d 0a 64 6f 5f 74 65  2 3 xx k3}.do_te
bb50: 73 74 20 65 5f 66 6b 65 79 2d 33 39 2e 33 20 7b  st e_fkey-39.3 {
bb60: 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20  .  execsql {.   
bb70: 20 44 45 4c 45 54 45 20 46 52 4f 4d 20 70 20 57   DELETE FROM p W
bb80: 48 45 52 45 20 61 20 3d 20 32 3b 0a 20 20 20 20  HERE a = 2;.    
bb90: 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 63 31  SELECT * FROM c1
bba0: 3b 0a 20 20 7d 0a 7d 20 7b 31 20 78 78 20 6b 30  ;.  }.} {1 xx k0
bbb0: 20 32 20 78 78 20 7b 7d 20 33 20 78 78 20 6b 33   2 xx {} 3 xx k3
bbc0: 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79  }.do_test e_fkey
bbd0: 2d 33 39 2e 34 20 7b 0a 20 20 65 78 65 63 73 71  -39.4 {.  execsq
bbe0: 6c 20 7b 0a 20 20 20 20 43 52 45 41 54 45 20 55  l {.    CREATE U
bbf0: 4e 49 51 55 45 20 49 4e 44 45 58 20 70 69 20 4f  NIQUE INDEX pi O
bc00: 4e 20 70 28 63 29 3b 0a 20 20 20 20 52 45 50 4c  N p(c);.    REPL
bc10: 41 43 45 20 49 4e 54 4f 20 70 20 56 41 4c 55 45  ACE INTO p VALUE
bc20: 53 28 35 2c 20 27 6b 35 27 2c 20 27 49 49 49 27  S(5, 'k5', 'III'
bc30: 29 3b 0a 20 20 20 20 53 45 4c 45 43 54 20 2a 20  );.    SELECT * 
bc40: 46 52 4f 4d 20 63 31 3b 0a 20 20 7d 0a 7d 20 7b  FROM c1;.  }.} {
bc50: 31 20 78 78 20 6b 30 20 32 20 78 78 20 7b 7d 20  1 xx k0 2 xx {} 
bc60: 33 20 78 78 20 7b 7d 7d 0a 0a 23 2d 2d 2d 2d 2d  3 xx {}}..#-----
bc70: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
bc80: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
bc90: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
bca0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
bcb0: 2d 2d 2d 2d 0a 23 20 2f 2a 20 45 56 3a 20 52 2d  ----.# /* EV: R-
bcc0: 33 33 33 32 36 2d 34 35 32 35 32 20 2a 2f 0a 23  33326-45252 */.#
bcd0: 0a 23 20 45 61 63 68 20 66 6f 72 65 69 67 6e 20  .# Each foreign 
bce0: 6b 65 79 20 69 6e 20 74 68 65 20 73 79 73 74 65  key in the syste
bcf0: 6d 20 68 61 73 20 61 6e 20 4f 4e 20 55 50 44 41  m has an ON UPDA
bd00: 54 45 20 61 6e 64 20 4f 4e 20 44 45 4c 45 54 45  TE and ON DELETE
bd10: 20 61 63 74 69 6f 6e 2c 0a 23 20 65 69 74 68 65   action,.# eithe
bd20: 72 20 22 4e 4f 20 41 43 54 49 4f 4e 22 2c 20 22  r "NO ACTION", "
bd30: 52 45 53 54 52 49 43 54 22 2c 20 22 53 45 54 20  RESTRICT", "SET 
bd40: 4e 55 4c 4c 22 2c 20 22 53 45 54 20 44 45 46 41  NULL", "SET DEFA
bd50: 55 4c 54 22 20 6f 72 20 22 43 41 53 43 41 44 45  ULT" or "CASCADE
bd60: 22 2e 0a 23 0a 23 20 2f 2a 20 45 56 3a 20 52 2d  "..#.# /* EV: R-
bd70: 31 39 38 30 33 2d 34 35 38 38 34 20 2a 2f 0a 23  19803-45884 */.#
bd80: 0a 23 20 49 66 20 6e 6f 6e 65 20 69 73 20 73 70  .# If none is sp
bd90: 65 63 69 66 69 65 64 20 65 78 70 6c 69 63 69 74  ecified explicit
bda0: 6c 79 2c 20 22 4e 4f 20 41 43 54 49 4f 4e 22 20  ly, "NO ACTION" 
bdb0: 69 73 20 74 68 65 20 64 65 66 61 75 6c 74 2e 0a  is the default..
bdc0: 23 20 0a 64 72 6f 70 5f 61 6c 6c 5f 74 61 62 6c  # .drop_all_tabl
bdd0: 65 73 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65  es.do_test e_fke
bde0: 79 2d 34 30 2e 31 20 7b 0a 20 20 65 78 65 63 73  y-40.1 {.  execs
bdf0: 71 6c 20 7b 0a 20 20 20 20 43 52 45 41 54 45 20  ql {.    CREATE 
be00: 54 41 42 4c 45 20 70 61 72 65 6e 74 28 78 20 50  TABLE parent(x P
be10: 52 49 4d 41 52 59 20 4b 45 59 2c 20 79 29 3b 0a  RIMARY KEY, y);.
be20: 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45      CREATE TABLE
be30: 20 63 68 69 6c 64 31 28 61 2c 20 0a 20 20 20 20   child1(a, .    
be40: 20 20 62 20 52 45 46 45 52 45 4e 43 45 53 20 70    b REFERENCES p
be50: 61 72 65 6e 74 20 4f 4e 20 55 50 44 41 54 45 20  arent ON UPDATE 
be60: 4e 4f 20 41 43 54 49 4f 4e 20 4f 4e 20 44 45 4c  NO ACTION ON DEL
be70: 45 54 45 20 52 45 53 54 52 49 43 54 0a 20 20 20  ETE RESTRICT.   
be80: 20 29 3b 0a 20 20 20 20 43 52 45 41 54 45 20 54   );.    CREATE T
be90: 41 42 4c 45 20 63 68 69 6c 64 32 28 61 2c 20 0a  ABLE child2(a, .
bea0: 20 20 20 20 20 20 62 20 52 45 46 45 52 45 4e 43        b REFERENC
beb0: 45 53 20 70 61 72 65 6e 74 20 4f 4e 20 55 50 44  ES parent ON UPD
bec0: 41 54 45 20 52 45 53 54 52 49 43 54 20 4f 4e 20  ATE RESTRICT ON 
bed0: 44 45 4c 45 54 45 20 53 45 54 20 4e 55 4c 4c 0a  DELETE SET NULL.
bee0: 20 20 20 20 29 3b 0a 20 20 20 20 43 52 45 41 54      );.    CREAT
bef0: 45 20 54 41 42 4c 45 20 63 68 69 6c 64 33 28 61  E TABLE child3(a
bf00: 2c 20 0a 20 20 20 20 20 20 62 20 52 45 46 45 52  , .      b REFER
bf10: 45 4e 43 45 53 20 70 61 72 65 6e 74 20 4f 4e 20  ENCES parent ON 
bf20: 55 50 44 41 54 45 20 53 45 54 20 4e 55 4c 4c 20  UPDATE SET NULL 
bf30: 4f 4e 20 44 45 4c 45 54 45 20 53 45 54 20 44 45  ON DELETE SET DE
bf40: 46 41 55 4c 54 0a 20 20 20 20 29 3b 0a 20 20 20  FAULT.    );.   
bf50: 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 63 68   CREATE TABLE ch
bf60: 69 6c 64 34 28 61 2c 20 0a 20 20 20 20 20 20 62  ild4(a, .      b
bf70: 20 52 45 46 45 52 45 4e 43 45 53 20 70 61 72 65   REFERENCES pare
bf80: 6e 74 20 4f 4e 20 55 50 44 41 54 45 20 53 45 54  nt ON UPDATE SET
bf90: 20 44 45 46 41 55 4c 54 20 4f 4e 20 44 45 4c 45   DEFAULT ON DELE
bfa0: 54 45 20 43 41 53 43 41 44 45 0a 20 20 20 20 29  TE CASCADE.    )
bfb0: 3b 0a 0a 20 20 20 20 2d 2d 20 43 72 65 61 74 65  ;..    -- Create
bfc0: 20 73 6f 6d 65 20 66 6f 72 65 69 67 6e 20 6b 65   some foreign ke
bfd0: 79 73 20 74 68 61 74 20 75 73 65 20 74 68 65 20  ys that use the 
bfe0: 64 65 66 61 75 6c 74 20 61 63 74 69 6f 6e 20 2d  default action -
bff0: 20 22 4e 4f 20 41 43 54 49 4f 4e 22 0a 20 20 20   "NO ACTION".   
c000: 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 63 68   CREATE TABLE ch
c010: 69 6c 64 35 28 61 2c 20 62 20 52 45 46 45 52 45  ild5(a, b REFERE
c020: 4e 43 45 53 20 70 61 72 65 6e 74 20 4f 4e 20 55  NCES parent ON U
c030: 50 44 41 54 45 20 43 41 53 43 41 44 45 29 3b 0a  PDATE CASCADE);.
c040: 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45      CREATE TABLE
c050: 20 63 68 69 6c 64 36 28 61 2c 20 62 20 52 45 46   child6(a, b REF
c060: 45 52 45 4e 43 45 53 20 70 61 72 65 6e 74 20 4f  ERENCES parent O
c070: 4e 20 44 45 4c 45 54 45 20 52 45 53 54 52 49 43  N DELETE RESTRIC
c080: 54 29 3b 0a 20 20 20 20 43 52 45 41 54 45 20 54  T);.    CREATE T
c090: 41 42 4c 45 20 63 68 69 6c 64 37 28 61 2c 20 62  ABLE child7(a, b
c0a0: 20 52 45 46 45 52 45 4e 43 45 53 20 70 61 72 65   REFERENCES pare
c0b0: 6e 74 20 4f 4e 20 44 45 4c 45 54 45 20 4e 4f 20  nt ON DELETE NO 
c0c0: 41 43 54 49 4f 4e 29 3b 0a 20 20 20 20 43 52 45  ACTION);.    CRE
c0d0: 41 54 45 20 54 41 42 4c 45 20 63 68 69 6c 64 38  ATE TABLE child8
c0e0: 28 61 2c 20 62 20 52 45 46 45 52 45 4e 43 45 53  (a, b REFERENCES
c0f0: 20 70 61 72 65 6e 74 20 4f 4e 20 55 50 44 41 54   parent ON UPDAT
c100: 45 20 4e 4f 20 41 43 54 49 4f 4e 29 3b 0a 20 20  E NO ACTION);.  
c110: 7d 0a 7d 20 7b 7d 0a 0a 66 6f 72 65 61 63 68 20  }.} {}..foreach 
c120: 7b 74 6e 20 7a 54 61 62 20 6c 52 65 73 7d 20 7b  {tn zTab lRes} {
c130: 0a 20 20 32 20 63 68 69 6c 64 31 20 7b 30 20 30  .  2 child1 {0 0
c140: 20 70 61 72 65 6e 74 20 62 20 7b 7d 20 7b 4e 4f   parent b {} {NO
c150: 20 41 43 54 49 4f 4e 7d 20 52 45 53 54 52 49 43   ACTION} RESTRIC
c160: 54 20 4e 4f 4e 45 7d 0a 20 20 33 20 63 68 69 6c  T NONE}.  3 chil
c170: 64 32 20 7b 30 20 30 20 70 61 72 65 6e 74 20 62  d2 {0 0 parent b
c180: 20 7b 7d 20 52 45 53 54 52 49 43 54 20 7b 53 45   {} RESTRICT {SE
c190: 54 20 4e 55 4c 4c 7d 20 4e 4f 4e 45 7d 0a 20 20  T NULL} NONE}.  
c1a0: 34 20 63 68 69 6c 64 33 20 7b 30 20 30 20 70 61  4 child3 {0 0 pa
c1b0: 72 65 6e 74 20 62 20 7b 7d 20 7b 53 45 54 20 4e  rent b {} {SET N
c1c0: 55 4c 4c 7d 20 7b 53 45 54 20 44 45 46 41 55 4c  ULL} {SET DEFAUL
c1d0: 54 7d 20 4e 4f 4e 45 7d 0a 20 20 35 20 63 68 69  T} NONE}.  5 chi
c1e0: 6c 64 34 20 7b 30 20 30 20 70 61 72 65 6e 74 20  ld4 {0 0 parent 
c1f0: 62 20 7b 7d 20 7b 53 45 54 20 44 45 46 41 55 4c  b {} {SET DEFAUL
c200: 54 7d 20 43 41 53 43 41 44 45 20 4e 4f 4e 45 7d  T} CASCADE NONE}
c210: 0a 20 20 36 20 63 68 69 6c 64 35 20 7b 30 20 30  .  6 child5 {0 0
c220: 20 70 61 72 65 6e 74 20 62 20 7b 7d 20 43 41 53   parent b {} CAS
c230: 43 41 44 45 20 7b 4e 4f 20 41 43 54 49 4f 4e 7d  CADE {NO ACTION}
c240: 20 4e 4f 4e 45 7d 0a 20 20 37 20 63 68 69 6c 64   NONE}.  7 child
c250: 36 20 7b 30 20 30 20 70 61 72 65 6e 74 20 62 20  6 {0 0 parent b 
c260: 7b 7d 20 7b 4e 4f 20 41 43 54 49 4f 4e 7d 20 52  {} {NO ACTION} R
c270: 45 53 54 52 49 43 54 20 4e 4f 4e 45 7d 0a 20 20  ESTRICT NONE}.  
c280: 38 20 63 68 69 6c 64 37 20 7b 30 20 30 20 70 61  8 child7 {0 0 pa
c290: 72 65 6e 74 20 62 20 7b 7d 20 7b 4e 4f 20 41 43  rent b {} {NO AC
c2a0: 54 49 4f 4e 7d 20 7b 4e 4f 20 41 43 54 49 4f 4e  TION} {NO ACTION
c2b0: 7d 20 4e 4f 4e 45 7d 0a 20 20 39 20 63 68 69 6c  } NONE}.  9 chil
c2c0: 64 38 20 7b 30 20 30 20 70 61 72 65 6e 74 20 62  d8 {0 0 parent b
c2d0: 20 7b 7d 20 7b 4e 4f 20 41 43 54 49 4f 4e 7d 20   {} {NO ACTION} 
c2e0: 7b 4e 4f 20 41 43 54 49 4f 4e 7d 20 4e 4f 4e 45  {NO ACTION} NONE
c2f0: 7d 0a 7d 20 7b 0a 20 20 64 6f 5f 74 65 73 74 20  }.} {.  do_test 
c300: 65 5f 66 6b 65 79 2d 34 30 2e 24 74 6e 20 7b 20  e_fkey-40.$tn { 
c310: 65 78 65 63 73 71 6c 20 22 50 52 41 47 4d 41 20  execsql "PRAGMA 
c320: 66 6f 72 65 69 67 6e 5f 6b 65 79 5f 6c 69 73 74  foreign_key_list
c330: 28 24 7a 54 61 62 29 22 20 7d 20 24 6c 52 65 73  ($zTab)" } $lRes
c340: 0a 7d 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  .}..#-----------
c350: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
c360: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
c370: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
c380: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23  --------------.#
c390: 20 2f 2a 20 45 56 3a 20 52 2d 31 39 39 37 31 2d   /* EV: R-19971-
c3a0: 35 34 39 37 36 20 2a 2f 0a 23 0a 23 20 54 65 73  54976 */.#.# Tes
c3b0: 74 20 74 68 61 74 20 22 4e 4f 20 41 43 54 49 4f  t that "NO ACTIO
c3c0: 4e 22 20 6d 65 61 6e 73 20 74 68 61 74 20 6e 6f  N" means that no
c3d0: 74 68 69 6e 67 20 68 61 70 70 65 6e 73 20 74 6f  thing happens to
c3e0: 20 61 20 63 68 69 6c 64 20 72 6f 77 20 77 68 65   a child row whe
c3f0: 6e 0a 23 20 69 74 27 73 20 70 61 72 65 6e 74 20  n.# it's parent 
c400: 72 6f 77 20 69 73 20 75 70 64 61 74 65 64 20 6f  row is updated o
c410: 72 20 64 65 6c 65 74 65 64 2e 0a 23 0a 64 72 6f  r deleted..#.dro
c420: 70 5f 61 6c 6c 5f 74 61 62 6c 65 73 0a 64 6f 5f  p_all_tables.do_
c430: 74 65 73 74 20 65 5f 66 6b 65 79 2d 34 31 2e 31  test e_fkey-41.1
c440: 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20   {.  execsql {. 
c450: 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20     CREATE TABLE 
c460: 70 61 72 65 6e 74 28 70 31 2c 20 70 32 2c 20 50  parent(p1, p2, P
c470: 52 49 4d 41 52 59 20 4b 45 59 28 70 31 2c 20 70  RIMARY KEY(p1, p
c480: 32 29 29 3b 0a 20 20 20 20 43 52 45 41 54 45 20  2));.    CREATE 
c490: 54 41 42 4c 45 20 63 68 69 6c 64 28 63 31 2c 20  TABLE child(c1, 
c4a0: 63 32 2c 20 0a 20 20 20 20 20 20 46 4f 52 45 49  c2, .      FOREI
c4b0: 47 4e 20 4b 45 59 28 63 31 2c 20 63 32 29 20 52  GN KEY(c1, c2) R
c4c0: 45 46 45 52 45 4e 43 45 53 20 70 61 72 65 6e 74  EFERENCES parent
c4d0: 0a 20 20 20 20 20 20 4f 4e 20 55 50 44 41 54 45  .      ON UPDATE
c4e0: 20 4e 4f 20 41 43 54 49 4f 4e 0a 20 20 20 20 20   NO ACTION.     
c4f0: 20 4f 4e 20 44 45 4c 45 54 45 20 4e 4f 20 41 43   ON DELETE NO AC
c500: 54 49 4f 4e 0a 20 20 20 20 20 20 44 45 46 45 52  TION.      DEFER
c510: 52 41 42 4c 45 20 49 4e 49 54 49 41 4c 4c 59 20  RABLE INITIALLY 
c520: 44 45 46 45 52 52 45 44 0a 20 20 20 20 29 3b 0a  DEFERRED.    );.
c530: 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20      INSERT INTO 
c540: 70 61 72 65 6e 74 20 56 41 4c 55 45 53 28 27 6a  parent VALUES('j
c550: 27 2c 20 27 6b 27 29 3b 0a 20 20 20 20 49 4e 53  ', 'k');.    INS
c560: 45 52 54 20 49 4e 54 4f 20 70 61 72 65 6e 74 20  ERT INTO parent 
c570: 56 41 4c 55 45 53 28 27 6c 27 2c 20 27 6d 27 29  VALUES('l', 'm')
c580: 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54  ;.    INSERT INT
c590: 4f 20 63 68 69 6c 64 20 56 41 4c 55 45 53 28 27  O child VALUES('
c5a0: 6a 27 2c 20 27 6b 27 29 3b 0a 20 20 20 20 49 4e  j', 'k');.    IN
c5b0: 53 45 52 54 20 49 4e 54 4f 20 63 68 69 6c 64 20  SERT INTO child 
c5c0: 56 41 4c 55 45 53 28 27 6c 27 2c 20 27 6d 27 29  VALUES('l', 'm')
c5d0: 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a 64 6f 5f 74 65  ;.  }.} {}.do_te
c5e0: 73 74 20 65 5f 66 6b 65 79 2d 34 31 2e 32 20 7b  st e_fkey-41.2 {
c5f0: 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20  .  execsql {.   
c600: 20 42 45 47 49 4e 3b 0a 20 20 20 20 20 20 55 50   BEGIN;.      UP
c610: 44 41 54 45 20 70 61 72 65 6e 74 20 53 45 54 20  DATE parent SET 
c620: 70 31 3d 27 6b 27 20 57 48 45 52 45 20 70 31 3d  p1='k' WHERE p1=
c630: 27 6a 27 3b 0a 20 20 20 20 20 20 44 45 4c 45 54  'j';.      DELET
c640: 45 20 46 52 4f 4d 20 70 61 72 65 6e 74 20 57 48  E FROM parent WH
c650: 45 52 45 20 70 31 3d 27 6c 27 3b 0a 20 20 20 20  ERE p1='l';.    
c660: 20 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20    SELECT * FROM 
c670: 63 68 69 6c 64 3b 0a 20 20 7d 0a 7d 20 7b 6a 20  child;.  }.} {j 
c680: 6b 20 6c 20 6d 7d 0a 64 6f 5f 74 65 73 74 20 65  k l m}.do_test e
c690: 5f 66 6b 65 79 2d 34 31 2e 33 20 7b 0a 20 20 63  _fkey-41.3 {.  c
c6a0: 61 74 63 68 73 71 6c 20 43 4f 4d 4d 49 54 0a 7d  atchsql COMMIT.}
c6b0: 20 7b 31 20 7b 66 6f 72 65 69 67 6e 20 6b 65 79   {1 {foreign key
c6c0: 20 63 6f 6e 73 74 72 61 69 6e 74 20 66 61 69 6c   constraint fail
c6d0: 65 64 7d 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66  ed}}.do_test e_f
c6e0: 6b 65 79 2d 34 31 2e 34 20 7b 0a 20 20 65 78 65  key-41.4 {.  exe
c6f0: 63 73 71 6c 20 52 4f 4c 4c 42 41 43 4b 0a 7d 20  csql ROLLBACK.} 
c700: 7b 7d 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  {}..#-----------
c710: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
c720: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
c730: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
c740: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23  --------------.#
c750: 20 2f 2a 20 45 56 3a 20 52 2d 30 34 32 37 32 2d   /* EV: R-04272-
c760: 33 38 36 35 33 20 2a 2f 0a 23 0a 23 20 54 65 73  38653 */.#.# Tes
c770: 74 20 74 68 61 74 20 22 52 45 53 54 52 49 43 54  t that "RESTRICT
c780: 22 20 6d 65 61 6e 73 20 74 68 65 20 61 70 70 6c  " means the appl
c790: 69 63 61 74 69 6f 6e 20 69 73 20 70 72 6f 68 69  ication is prohi
c7a0: 62 69 74 65 64 20 66 72 6f 6d 20 64 65 6c 65 74  bited from delet
c7b0: 69 6e 67 0a 23 20 6f 72 20 75 70 64 61 74 69 6e  ing.# or updatin
c7c0: 67 20 61 20 70 61 72 65 6e 74 20 74 61 62 6c 65  g a parent table
c7d0: 20 72 6f 77 20 77 68 65 6e 20 74 68 65 72 65 20   row when there 
c7e0: 65 78 69 73 74 73 20 6f 6e 65 20 6f 72 20 6d 6f  exists one or mo
c7f0: 72 65 20 63 68 69 6c 64 20 6b 65 79 73 0a 23 20  re child keys.# 
c800: 6d 61 70 70 65 64 20 74 6f 20 69 74 2e 0a 23 0a  mapped to it..#.
c810: 64 72 6f 70 5f 61 6c 6c 5f 74 61 62 6c 65 73 0a  drop_all_tables.
c820: 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 34  do_test e_fkey-4
c830: 31 2e 31 20 7b 0a 20 20 65 78 65 63 73 71 6c 20  1.1 {.  execsql 
c840: 7b 0a 20 20 20 20 43 52 45 41 54 45 20 54 41 42  {.    CREATE TAB
c850: 4c 45 20 70 61 72 65 6e 74 28 70 31 2c 20 70 32  LE parent(p1, p2
c860: 29 3b 0a 20 20 20 20 43 52 45 41 54 45 20 55 4e  );.    CREATE UN
c870: 49 51 55 45 20 49 4e 44 45 58 20 70 61 72 65 6e  IQUE INDEX paren
c880: 74 5f 69 20 4f 4e 20 70 61 72 65 6e 74 28 70 31  t_i ON parent(p1
c890: 2c 20 70 32 29 3b 0a 20 20 20 20 43 52 45 41 54  , p2);.    CREAT
c8a0: 45 20 54 41 42 4c 45 20 63 68 69 6c 64 31 28 63  E TABLE child1(c
c8b0: 31 2c 20 63 32 2c 20 0a 20 20 20 20 20 20 46 4f  1, c2, .      FO
c8c0: 52 45 49 47 4e 20 4b 45 59 28 63 32 2c 20 63 31  REIGN KEY(c2, c1
c8d0: 29 20 52 45 46 45 52 45 4e 43 45 53 20 70 61 72  ) REFERENCES par
c8e0: 65 6e 74 28 70 31 2c 20 70 32 29 20 4f 4e 20 44  ent(p1, p2) ON D
c8f0: 45 4c 45 54 45 20 52 45 53 54 52 49 43 54 0a 20  ELETE RESTRICT. 
c900: 20 20 20 29 3b 0a 20 20 20 20 43 52 45 41 54 45     );.    CREATE
c910: 20 54 41 42 4c 45 20 63 68 69 6c 64 32 28 63 31   TABLE child2(c1
c920: 2c 20 63 32 2c 20 0a 20 20 20 20 20 20 46 4f 52  , c2, .      FOR
c930: 45 49 47 4e 20 4b 45 59 28 63 32 2c 20 63 31 29  EIGN KEY(c2, c1)
c940: 20 52 45 46 45 52 45 4e 43 45 53 20 70 61 72 65   REFERENCES pare
c950: 6e 74 28 70 31 2c 20 70 32 29 20 4f 4e 20 55 50  nt(p1, p2) ON UP
c960: 44 41 54 45 20 52 45 53 54 52 49 43 54 0a 20 20  DATE RESTRICT.  
c970: 20 20 29 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a 64 6f    );.  }.} {}.do
c980: 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 34 31 2e  _test e_fkey-41.
c990: 32 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a  2 {.  execsql {.
c9a0: 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20      INSERT INTO 
c9b0: 70 61 72 65 6e 74 20 56 41 4c 55 45 53 28 27 61  parent VALUES('a
c9c0: 27 2c 20 27 62 27 29 3b 0a 20 20 20 20 49 4e 53  ', 'b');.    INS
c9d0: 45 52 54 20 49 4e 54 4f 20 70 61 72 65 6e 74 20  ERT INTO parent 
c9e0: 56 41 4c 55 45 53 28 27 63 27 2c 20 27 64 27 29  VALUES('c', 'd')
c9f0: 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54  ;.    INSERT INT
ca00: 4f 20 63 68 69 6c 64 31 20 56 41 4c 55 45 53 28  O child1 VALUES(
ca10: 27 62 27 2c 20 27 61 27 29 3b 0a 20 20 20 20 49  'b', 'a');.    I
ca20: 4e 53 45 52 54 20 49 4e 54 4f 20 63 68 69 6c 64  NSERT INTO child
ca30: 32 20 56 41 4c 55 45 53 28 27 64 27 2c 20 27 63  2 VALUES('d', 'c
ca40: 27 29 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a 64 6f 5f  ');.  }.} {}.do_
ca50: 74 65 73 74 20 65 5f 66 6b 65 79 2d 34 31 2e 33  test e_fkey-41.3
ca60: 20 7b 0a 20 20 63 61 74 63 68 73 71 6c 20 7b 20   {.  catchsql { 
ca70: 44 45 4c 45 54 45 20 46 52 4f 4d 20 70 61 72 65  DELETE FROM pare
ca80: 6e 74 20 57 48 45 52 45 20 70 31 20 3d 20 27 61  nt WHERE p1 = 'a
ca90: 27 20 7d 0a 7d 20 7b 31 20 7b 66 6f 72 65 69 67  ' }.} {1 {foreig
caa0: 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74  n key constraint
cab0: 20 66 61 69 6c 65 64 7d 7d 0a 64 6f 5f 74 65 73   failed}}.do_tes
cac0: 74 20 65 5f 66 6b 65 79 2d 34 31 2e 34 20 7b 0a  t e_fkey-41.4 {.
cad0: 20 20 63 61 74 63 68 73 71 6c 20 7b 20 55 50 44    catchsql { UPD
cae0: 41 54 45 20 70 61 72 65 6e 74 20 53 45 54 20 70  ATE parent SET p
caf0: 32 20 3d 20 27 65 27 20 57 48 45 52 45 20 70 31  2 = 'e' WHERE p1
cb00: 20 3d 20 27 63 27 20 7d 0a 7d 20 7b 31 20 7b 66   = 'c' }.} {1 {f
cb10: 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74  oreign key const
cb20: 72 61 69 6e 74 20 66 61 69 6c 65 64 7d 7d 0a 0a  raint failed}}..
cb30: 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  #---------------
cb40: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
cb50: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
cb60: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
cb70: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20 2f 2a 20  ----------.# /* 
cb80: 45 56 3a 20 52 2d 33 37 39 39 37 2d 34 32 31 38  EV: R-37997-4218
cb90: 37 20 2a 2f 0a 23 20 0a 23 20 54 65 73 74 20 74  7 */.# .# Test t
cba0: 68 61 74 20 52 45 53 54 52 49 43 54 20 69 73 20  hat RESTRICT is 
cbb0: 73 6c 69 67 68 74 6c 79 20 64 69 66 66 65 72 65  slightly differe
cbc0: 6e 74 20 66 72 6f 6d 20 4e 4f 20 41 43 54 49 4f  nt from NO ACTIO
cbd0: 4e 20 66 6f 72 20 49 4d 4d 45 44 49 41 54 45 0a  N for IMMEDIATE.
cbe0: 23 20 63 6f 6e 73 74 72 61 69 6e 74 73 2c 20 69  # constraints, i
cbf0: 6e 20 74 68 61 74 20 69 74 20 69 73 20 65 6e 66  n that it is enf
cc00: 6f 72 63 65 64 20 69 6d 6d 65 64 69 61 74 65 6c  orced immediatel
cc10: 79 2c 20 6e 6f 74 20 61 74 20 74 68 65 20 65 6e  y, not at the en
cc20: 64 20 6f 66 20 74 68 65 20 0a 23 20 73 74 61 74  d of the .# stat
cc30: 65 6d 65 6e 74 2e 0a 23 0a 64 72 6f 70 5f 61 6c  ement..#.drop_al
cc40: 6c 5f 74 61 62 6c 65 73 0a 64 6f 5f 74 65 73 74  l_tables.do_test
cc50: 20 65 5f 66 6b 65 79 2d 34 32 2e 31 20 7b 0a 20   e_fkey-42.1 {. 
cc60: 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 43   execsql {.    C
cc70: 52 45 41 54 45 20 54 41 42 4c 45 20 70 61 72 65  REATE TABLE pare
cc80: 6e 74 28 78 20 50 52 49 4d 41 52 59 20 4b 45 59  nt(x PRIMARY KEY
cc90: 29 3b 0a 20 20 20 20 43 52 45 41 54 45 20 54 41  );.    CREATE TA
cca0: 42 4c 45 20 63 68 69 6c 64 31 28 63 20 52 45 46  BLE child1(c REF
ccb0: 45 52 45 4e 43 45 53 20 70 61 72 65 6e 74 20 4f  ERENCES parent O
ccc0: 4e 20 55 50 44 41 54 45 20 52 45 53 54 52 49 43  N UPDATE RESTRIC
ccd0: 54 29 3b 0a 20 20 20 20 43 52 45 41 54 45 20 54  T);.    CREATE T
cce0: 41 42 4c 45 20 63 68 69 6c 64 32 28 63 20 52 45  ABLE child2(c RE
ccf0: 46 45 52 45 4e 43 45 53 20 70 61 72 65 6e 74 20  FERENCES parent 
cd00: 4f 4e 20 55 50 44 41 54 45 20 4e 4f 20 41 43 54  ON UPDATE NO ACT
cd10: 49 4f 4e 29 3b 0a 0a 20 20 20 20 49 4e 53 45 52  ION);..    INSER
cd20: 54 20 49 4e 54 4f 20 70 61 72 65 6e 74 20 56 41  T INTO parent VA
cd30: 4c 55 45 53 28 27 6b 65 79 31 27 29 3b 0a 20 20  LUES('key1');.  
cd40: 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 70 61    INSERT INTO pa
cd50: 72 65 6e 74 20 56 41 4c 55 45 53 28 27 6b 65 79  rent VALUES('key
cd60: 32 27 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20  2');.    INSERT 
cd70: 49 4e 54 4f 20 63 68 69 6c 64 31 20 56 41 4c 55  INTO child1 VALU
cd80: 45 53 28 27 6b 65 79 31 27 29 3b 0a 20 20 20 20  ES('key1');.    
cd90: 49 4e 53 45 52 54 20 49 4e 54 4f 20 63 68 69 6c  INSERT INTO chil
cda0: 64 32 20 56 41 4c 55 45 53 28 27 6b 65 79 32 27  d2 VALUES('key2'
cdb0: 29 3b 0a 0a 20 20 20 20 43 52 45 41 54 45 20 54  );..    CREATE T
cdc0: 52 49 47 47 45 52 20 70 61 72 65 6e 74 5f 74 20  RIGGER parent_t 
cdd0: 41 46 54 45 52 20 55 50 44 41 54 45 20 4f 4e 20  AFTER UPDATE ON 
cde0: 70 61 72 65 6e 74 20 42 45 47 49 4e 0a 20 20 20  parent BEGIN.   
cdf0: 20 20 20 55 50 44 41 54 45 20 63 68 69 6c 64 31     UPDATE child1
ce00: 20 73 65 74 20 63 20 3d 20 6e 65 77 2e 78 20 57   set c = new.x W
ce10: 48 45 52 45 20 63 20 3d 20 6f 6c 64 2e 78 3b 0a  HERE c = old.x;.
ce20: 20 20 20 20 20 20 55 50 44 41 54 45 20 63 68 69        UPDATE chi
ce30: 6c 64 32 20 73 65 74 20 63 20 3d 20 6e 65 77 2e  ld2 set c = new.
ce40: 78 20 57 48 45 52 45 20 63 20 3d 20 6f 6c 64 2e  x WHERE c = old.
ce50: 78 3b 0a 20 20 20 20 45 4e 44 3b 0a 20 20 7d 0a  x;.    END;.  }.
ce60: 7d 20 7b 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66  } {}.do_test e_f
ce70: 6b 65 79 2d 34 32 2e 32 20 7b 0a 20 20 63 61 74  key-42.2 {.  cat
ce80: 63 68 73 71 6c 20 7b 20 55 50 44 41 54 45 20 70  chsql { UPDATE p
ce90: 61 72 65 6e 74 20 53 45 54 20 78 20 3d 20 27 6b  arent SET x = 'k
cea0: 65 79 20 6f 6e 65 27 20 57 48 45 52 45 20 78 20  ey one' WHERE x 
ceb0: 3d 20 27 6b 65 79 31 27 20 7d 0a 7d 20 7b 31 20  = 'key1' }.} {1 
cec0: 7b 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e  {foreign key con
ced0: 73 74 72 61 69 6e 74 20 66 61 69 6c 65 64 7d 7d  straint failed}}
cee0: 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d  .do_test e_fkey-
cef0: 34 32 2e 33 20 7b 0a 20 20 65 78 65 63 73 71 6c  42.3 {.  execsql
cf00: 20 7b 20 0a 20 20 20 20 55 50 44 41 54 45 20 70   { .    UPDATE p
cf10: 61 72 65 6e 74 20 53 45 54 20 78 20 3d 20 27 6b  arent SET x = 'k
cf20: 65 79 20 74 77 6f 27 20 57 48 45 52 45 20 78 20  ey two' WHERE x 
cf30: 3d 20 27 6b 65 79 32 27 3b 0a 20 20 20 20 53 45  = 'key2';.    SE
cf40: 4c 45 43 54 20 2a 20 46 52 4f 4d 20 63 68 69 6c  LECT * FROM chil
cf50: 64 32 3b 0a 20 20 7d 0a 7d 20 7b 7b 6b 65 79 20  d2;.  }.} {{key 
cf60: 74 77 6f 7d 7d 0a 0a 64 72 6f 70 5f 61 6c 6c 5f  two}}..drop_all_
cf70: 74 61 62 6c 65 73 0a 64 6f 5f 74 65 73 74 20 65  tables.do_test e
cf80: 5f 66 6b 65 79 2d 34 32 2e 34 20 7b 0a 20 20 65  _fkey-42.4 {.  e
cf90: 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 43 52 45  xecsql {.    CRE
cfa0: 41 54 45 20 54 41 42 4c 45 20 70 61 72 65 6e 74  ATE TABLE parent
cfb0: 28 78 20 50 52 49 4d 41 52 59 20 4b 45 59 29 3b  (x PRIMARY KEY);
cfc0: 0a 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c  .    CREATE TABL
cfd0: 45 20 63 68 69 6c 64 31 28 63 20 52 45 46 45 52  E child1(c REFER
cfe0: 45 4e 43 45 53 20 70 61 72 65 6e 74 20 4f 4e 20  ENCES parent ON 
cff0: 44 45 4c 45 54 45 20 52 45 53 54 52 49 43 54 29  DELETE RESTRICT)
d000: 3b 0a 20 20 20 20 43 52 45 41 54 45 20 54 41 42  ;.    CREATE TAB
d010: 4c 45 20 63 68 69 6c 64 32 28 63 20 52 45 46 45  LE child2(c REFE
d020: 52 45 4e 43 45 53 20 70 61 72 65 6e 74 20 4f 4e  RENCES parent ON
d030: 20 44 45 4c 45 54 45 20 4e 4f 20 41 43 54 49 4f   DELETE NO ACTIO
d040: 4e 29 3b 0a 0a 20 20 20 20 49 4e 53 45 52 54 20  N);..    INSERT 
d050: 49 4e 54 4f 20 70 61 72 65 6e 74 20 56 41 4c 55  INTO parent VALU
d060: 45 53 28 27 6b 65 79 31 27 29 3b 0a 20 20 20 20  ES('key1');.    
d070: 49 4e 53 45 52 54 20 49 4e 54 4f 20 70 61 72 65  INSERT INTO pare
d080: 6e 74 20 56 41 4c 55 45 53 28 27 6b 65 79 32 27  nt VALUES('key2'
d090: 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e  );.    INSERT IN
d0a0: 54 4f 20 63 68 69 6c 64 31 20 56 41 4c 55 45 53  TO child1 VALUES
d0b0: 28 27 6b 65 79 31 27 29 3b 0a 20 20 20 20 49 4e  ('key1');.    IN
d0c0: 53 45 52 54 20 49 4e 54 4f 20 63 68 69 6c 64 32  SERT INTO child2
d0d0: 20 56 41 4c 55 45 53 28 27 6b 65 79 32 27 29 3b   VALUES('key2');
d0e0: 0a 0a 20 20 20 20 43 52 45 41 54 45 20 54 52 49  ..    CREATE TRI
d0f0: 47 47 45 52 20 70 61 72 65 6e 74 5f 74 20 41 46  GGER parent_t AF
d100: 54 45 52 20 44 45 4c 45 54 45 20 4f 4e 20 70 61  TER DELETE ON pa
d110: 72 65 6e 74 20 42 45 47 49 4e 0a 20 20 20 20 20  rent BEGIN.     
d120: 20 55 50 44 41 54 45 20 63 68 69 6c 64 31 20 53   UPDATE child1 S
d130: 45 54 20 63 20 3d 20 4e 55 4c 4c 20 57 48 45 52  ET c = NULL WHER
d140: 45 20 63 20 3d 20 6f 6c 64 2e 78 3b 0a 20 20 20  E c = old.x;.   
d150: 20 20 20 55 50 44 41 54 45 20 63 68 69 6c 64 32     UPDATE child2
d160: 20 53 45 54 20 63 20 3d 20 4e 55 4c 4c 20 57 48   SET c = NULL WH
d170: 45 52 45 20 63 20 3d 20 6f 6c 64 2e 78 3b 0a 20  ERE c = old.x;. 
d180: 20 20 20 45 4e 44 3b 0a 20 20 7d 0a 7d 20 7b 7d     END;.  }.} {}
d190: 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d  .do_test e_fkey-
d1a0: 34 32 2e 35 20 7b 0a 20 20 63 61 74 63 68 73 71  42.5 {.  catchsq
d1b0: 6c 20 7b 20 44 45 4c 45 54 45 20 46 52 4f 4d 20  l { DELETE FROM 
d1c0: 70 61 72 65 6e 74 20 57 48 45 52 45 20 78 20 3d  parent WHERE x =
d1d0: 20 27 6b 65 79 31 27 20 7d 0a 7d 20 7b 31 20 7b   'key1' }.} {1 {
d1e0: 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73  foreign key cons
d1f0: 74 72 61 69 6e 74 20 66 61 69 6c 65 64 7d 7d 0a  traint failed}}.
d200: 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 34  do_test e_fkey-4
d210: 32 2e 36 20 7b 0a 20 20 65 78 65 63 73 71 6c 20  2.6 {.  execsql 
d220: 7b 20 0a 20 20 20 20 44 45 4c 45 54 45 20 46 52  { .    DELETE FR
d230: 4f 4d 20 70 61 72 65 6e 74 20 57 48 45 52 45 20  OM parent WHERE 
d240: 78 20 3d 20 27 6b 65 79 32 27 3b 0a 20 20 20 20  x = 'key2';.    
d250: 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 63 68  SELECT * FROM ch
d260: 69 6c 64 32 3b 0a 20 20 7d 0a 7d 20 7b 7b 7d 7d  ild2;.  }.} {{}}
d270: 0a 0a 64 72 6f 70 5f 61 6c 6c 5f 74 61 62 6c 65  ..drop_all_table
d280: 73 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79  s.do_test e_fkey
d290: 2d 34 32 2e 37 20 7b 0a 20 20 65 78 65 63 73 71  -42.7 {.  execsq
d2a0: 6c 20 7b 0a 20 20 20 20 43 52 45 41 54 45 20 54  l {.    CREATE T
d2b0: 41 42 4c 45 20 70 61 72 65 6e 74 28 78 20 50 52  ABLE parent(x PR
d2c0: 49 4d 41 52 59 20 4b 45 59 29 3b 0a 20 20 20 20  IMARY KEY);.    
d2d0: 43 52 45 41 54 45 20 54 41 42 4c 45 20 63 68 69  CREATE TABLE chi
d2e0: 6c 64 31 28 63 20 52 45 46 45 52 45 4e 43 45 53  ld1(c REFERENCES
d2f0: 20 70 61 72 65 6e 74 20 4f 4e 20 44 45 4c 45 54   parent ON DELET
d300: 45 20 52 45 53 54 52 49 43 54 29 3b 0a 20 20 20  E RESTRICT);.   
d310: 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 63 68   CREATE TABLE ch
d320: 69 6c 64 32 28 63 20 52 45 46 45 52 45 4e 43 45  ild2(c REFERENCE
d330: 53 20 70 61 72 65 6e 74 20 4f 4e 20 44 45 4c 45  S parent ON DELE
d340: 54 45 20 4e 4f 20 41 43 54 49 4f 4e 29 3b 0a 0a  TE NO ACTION);..
d350: 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20      INSERT INTO 
d360: 70 61 72 65 6e 74 20 56 41 4c 55 45 53 28 27 6b  parent VALUES('k
d370: 65 79 31 27 29 3b 0a 20 20 20 20 49 4e 53 45 52  ey1');.    INSER
d380: 54 20 49 4e 54 4f 20 70 61 72 65 6e 74 20 56 41  T INTO parent VA
d390: 4c 55 45 53 28 27 6b 65 79 32 27 29 3b 0a 20 20  LUES('key2');.  
d3a0: 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 63 68    INSERT INTO ch
d3b0: 69 6c 64 31 20 56 41 4c 55 45 53 28 27 6b 65 79  ild1 VALUES('key
d3c0: 31 27 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20  1');.    INSERT 
d3d0: 49 4e 54 4f 20 63 68 69 6c 64 32 20 56 41 4c 55  INTO child2 VALU
d3e0: 45 53 28 27 6b 65 79 32 27 29 3b 0a 20 20 7d 0a  ES('key2');.  }.
d3f0: 7d 20 7b 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66  } {}.do_test e_f
d400: 6b 65 79 2d 34 32 2e 38 20 7b 0a 20 20 63 61 74  key-42.8 {.  cat
d410: 63 68 73 71 6c 20 7b 20 52 45 50 4c 41 43 45 20  chsql { REPLACE 
d420: 49 4e 54 4f 20 70 61 72 65 6e 74 20 56 41 4c 55  INTO parent VALU
d430: 45 53 28 27 6b 65 79 31 27 29 20 7d 0a 7d 20 7b  ES('key1') }.} {
d440: 31 20 7b 66 6f 72 65 69 67 6e 20 6b 65 79 20 63  1 {foreign key c
d450: 6f 6e 73 74 72 61 69 6e 74 20 66 61 69 6c 65 64  onstraint failed
d460: 7d 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65  }}.do_test e_fke
d470: 79 2d 34 32 2e 39 20 7b 0a 20 20 65 78 65 63 73  y-42.9 {.  execs
d480: 71 6c 20 7b 20 0a 20 20 20 20 52 45 50 4c 41 43  ql { .    REPLAC
d490: 45 20 49 4e 54 4f 20 70 61 72 65 6e 74 20 56 41  E INTO parent VA
d4a0: 4c 55 45 53 28 27 6b 65 79 32 27 29 3b 0a 20 20  LUES('key2');.  
d4b0: 20 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20    SELECT * FROM 
d4c0: 63 68 69 6c 64 32 3b 0a 20 20 7d 0a 7d 20 7b 6b  child2;.  }.} {k
d4d0: 65 79 32 7d 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d  ey2}..#---------
d4e0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
d4f0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
d500: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
d510: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
d520: 0a 23 20 2f 2a 20 45 56 3a 20 52 2d 32 34 31 37  .# /* EV: R-2417
d530: 39 2d 36 30 35 32 33 20 2a 2f 0a 23 20 0a 23 20  9-60523 */.# .# 
d540: 54 65 73 74 20 74 68 61 74 20 52 45 53 54 52 49  Test that RESTRI
d550: 43 54 20 69 73 20 65 6e 66 6f 72 63 65 64 20 69  CT is enforced i
d560: 6d 6d 65 64 69 61 74 65 6c 79 2c 20 65 76 65 6e  mmediately, even
d570: 20 66 6f 72 20 61 20 44 45 46 45 52 52 45 44 20   for a DEFERRED 
d580: 63 6f 6e 73 74 72 61 69 6e 74 2e 0a 23 0a 64 72  constraint..#.dr
d590: 6f 70 5f 61 6c 6c 5f 74 61 62 6c 65 73 0a 64 6f  op_all_tables.do
d5a0: 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 34 33 2e  _test e_fkey-43.
d5b0: 31 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a  1 {.  execsql {.
d5c0: 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45      CREATE TABLE
d5d0: 20 70 61 72 65 6e 74 28 78 20 50 52 49 4d 41 52   parent(x PRIMAR
d5e0: 59 20 4b 45 59 29 3b 0a 20 20 20 20 43 52 45 41  Y KEY);.    CREA
d5f0: 54 45 20 54 41 42 4c 45 20 63 68 69 6c 64 31 28  TE TABLE child1(
d600: 63 20 52 45 46 45 52 45 4e 43 45 53 20 70 61 72  c REFERENCES par
d610: 65 6e 74 20 4f 4e 20 55 50 44 41 54 45 20 52 45  ent ON UPDATE RE
d620: 53 54 52 49 43 54 0a 20 20 20 20 20 20 44 45 46  STRICT.      DEF
d630: 45 52 52 41 42 4c 45 20 49 4e 49 54 49 41 4c 4c  ERRABLE INITIALL
d640: 59 20 44 45 46 45 52 52 45 44 0a 20 20 20 20 29  Y DEFERRED.    )
d650: 3b 0a 20 20 20 20 43 52 45 41 54 45 20 54 41 42  ;.    CREATE TAB
d660: 4c 45 20 63 68 69 6c 64 32 28 63 20 52 45 46 45  LE child2(c REFE
d670: 52 45 4e 43 45 53 20 70 61 72 65 6e 74 20 4f 4e  RENCES parent ON
d680: 20 55 50 44 41 54 45 20 4e 4f 20 41 43 54 49 4f   UPDATE NO ACTIO
d690: 4e 0a 20 20 20 20 20 20 44 45 46 45 52 52 41 42  N.      DEFERRAB
d6a0: 4c 45 20 49 4e 49 54 49 41 4c 4c 59 20 44 45 46  LE INITIALLY DEF
d6b0: 45 52 52 45 44 0a 20 20 20 20 29 3b 0a 0a 20 20  ERRED.    );..  
d6c0: 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 70 61    INSERT INTO pa
d6d0: 72 65 6e 74 20 56 41 4c 55 45 53 28 27 6b 65 79  rent VALUES('key
d6e0: 31 27 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20  1');.    INSERT 
d6f0: 49 4e 54 4f 20 70 61 72 65 6e 74 20 56 41 4c 55  INTO parent VALU
d700: 45 53 28 27 6b 65 79 32 27 29 3b 0a 20 20 20 20  ES('key2');.    
d710: 49 4e 53 45 52 54 20 49 4e 54 4f 20 63 68 69 6c  INSERT INTO chil
d720: 64 31 20 56 41 4c 55 45 53 28 27 6b 65 79 31 27  d1 VALUES('key1'
d730: 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e  );.    INSERT IN
d740: 54 4f 20 63 68 69 6c 64 32 20 56 41 4c 55 45 53  TO child2 VALUES
d750: 28 27 6b 65 79 32 27 29 3b 0a 20 20 20 20 42 45  ('key2');.    BE
d760: 47 49 4e 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a 64 6f  GIN;.  }.} {}.do
d770: 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 34 33 2e  _test e_fkey-43.
d780: 32 20 7b 0a 20 20 63 61 74 63 68 73 71 6c 20 7b  2 {.  catchsql {
d790: 20 55 50 44 41 54 45 20 70 61 72 65 6e 74 20 53   UPDATE parent S
d7a0: 45 54 20 78 20 3d 20 27 6b 65 79 20 6f 6e 65 27  ET x = 'key one'
d7b0: 20 57 48 45 52 45 20 78 20 3d 20 27 6b 65 79 31   WHERE x = 'key1
d7c0: 27 20 7d 0a 7d 20 7b 31 20 7b 66 6f 72 65 69 67  ' }.} {1 {foreig
d7d0: 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74  n key constraint
d7e0: 20 66 61 69 6c 65 64 7d 7d 0a 64 6f 5f 74 65 73   failed}}.do_tes
d7f0: 74 20 65 5f 66 6b 65 79 2d 34 33 2e 33 20 7b 0a  t e_fkey-43.3 {.
d800: 20 20 65 78 65 63 73 71 6c 20 7b 20 55 50 44 41    execsql { UPDA
d810: 54 45 20 70 61 72 65 6e 74 20 53 45 54 20 78 20  TE parent SET x 
d820: 3d 20 27 6b 65 79 20 74 77 6f 27 20 57 48 45 52  = 'key two' WHER
d830: 45 20 78 20 3d 20 27 6b 65 79 32 27 20 7d 0a 7d  E x = 'key2' }.}
d840: 20 7b 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b   {}.do_test e_fk
d850: 65 79 2d 34 33 2e 34 20 7b 0a 20 20 63 61 74 63  ey-43.4 {.  catc
d860: 68 73 71 6c 20 43 4f 4d 4d 49 54 0a 7d 20 7b 31  hsql COMMIT.} {1
d870: 20 7b 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f   {foreign key co
d880: 6e 73 74 72 61 69 6e 74 20 66 61 69 6c 65 64 7d  nstraint failed}
d890: 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79  }.do_test e_fkey
d8a0: 2d 34 33 2e 35 20 7b 0a 20 20 65 78 65 63 73 71  -43.5 {.  execsq
d8b0: 6c 20 7b 0a 20 20 20 20 55 50 44 41 54 45 20 63  l {.    UPDATE c
d8c0: 68 69 6c 64 32 20 53 45 54 20 63 20 3d 20 27 6b  hild2 SET c = 'k
d8d0: 65 79 20 74 77 6f 27 3b 0a 20 20 20 20 43 4f 4d  ey two';.    COM
d8e0: 4d 49 54 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a 0a 64  MIT;.  }.} {}..d
d8f0: 72 6f 70 5f 61 6c 6c 5f 74 61 62 6c 65 73 0a 64  rop_all_tables.d
d900: 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 34 33  o_test e_fkey-43
d910: 2e 36 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b  .6 {.  execsql {
d920: 0a 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c  .    CREATE TABL
d930: 45 20 70 61 72 65 6e 74 28 78 20 50 52 49 4d 41  E parent(x PRIMA
d940: 52 59 20 4b 45 59 29 3b 0a 20 20 20 20 43 52 45  RY KEY);.    CRE
d950: 41 54 45 20 54 41 42 4c 45 20 63 68 69 6c 64 31  ATE TABLE child1
d960: 28 63 20 52 45 46 45 52 45 4e 43 45 53 20 70 61  (c REFERENCES pa
d970: 72 65 6e 74 20 4f 4e 20 44 45 4c 45 54 45 20 52  rent ON DELETE R
d980: 45 53 54 52 49 43 54 0a 20 20 20 20 20 20 44 45  ESTRICT.      DE
d990: 46 45 52 52 41 42 4c 45 20 49 4e 49 54 49 41 4c  FERRABLE INITIAL
d9a0: 4c 59 20 44 45 46 45 52 52 45 44 0a 20 20 20 20  LY DEFERRED.    
d9b0: 29 3b 0a 20 20 20 20 43 52 45 41 54 45 20 54 41  );.    CREATE TA
d9c0: 42 4c 45 20 63 68 69 6c 64 32 28 63 20 52 45 46  BLE child2(c REF
d9d0: 45 52 45 4e 43 45 53 20 70 61 72 65 6e 74 20 4f  ERENCES parent O
d9e0: 4e 20 44 45 4c 45 54 45 20 4e 4f 20 41 43 54 49  N DELETE NO ACTI
d9f0: 4f 4e 0a 20 20 20 20 20 20 44 45 46 45 52 52 41  ON.      DEFERRA
da00: 42 4c 45 20 49 4e 49 54 49 41 4c 4c 59 20 44 45  BLE INITIALLY DE
da10: 46 45 52 52 45 44 0a 20 20 20 20 29 3b 0a 0a 20  FERRED.    );.. 
da20: 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 70     INSERT INTO p
da30: 61 72 65 6e 74 20 56 41 4c 55 45 53 28 27 6b 65  arent VALUES('ke
da40: 79 31 27 29 3b 0a 20 20 20 20 49 4e 53 45 52 54  y1');.    INSERT
da50: 20 49 4e 54 4f 20 70 61 72 65 6e 74 20 56 41 4c   INTO parent VAL
da60: 55 45 53 28 27 6b 65 79 32 27 29 3b 0a 20 20 20  UES('key2');.   
da70: 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 63 68 69   INSERT INTO chi
da80: 6c 64 31 20 56 41 4c 55 45 53 28 27 6b 65 79 31  ld1 VALUES('key1
da90: 27 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49  ');.    INSERT I
daa0: 4e 54 4f 20 63 68 69 6c 64 32 20 56 41 4c 55 45  NTO child2 VALUE
dab0: 53 28 27 6b 65 79 32 27 29 3b 0a 20 20 20 20 42  S('key2');.    B
dac0: 45 47 49 4e 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a 64  EGIN;.  }.} {}.d
dad0: 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 34 33  o_test e_fkey-43
dae0: 2e 37 20 7b 0a 20 20 63 61 74 63 68 73 71 6c 20  .7 {.  catchsql 
daf0: 7b 20 44 45 4c 45 54 45 20 46 52 4f 4d 20 70 61  { DELETE FROM pa
db00: 72 65 6e 74 20 57 48 45 52 45 20 78 20 3d 20 27  rent WHERE x = '
db10: 6b 65 79 31 27 20 7d 0a 7d 20 7b 31 20 7b 66 6f  key1' }.} {1 {fo
db20: 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72  reign key constr
db30: 61 69 6e 74 20 66 61 69 6c 65 64 7d 7d 0a 64 6f  aint failed}}.do
db40: 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 34 33 2e  _test e_fkey-43.
db50: 38 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 20  8 {.  execsql { 
db60: 44 45 4c 45 54 45 20 46 52 4f 4d 20 70 61 72 65  DELETE FROM pare
db70: 6e 74 20 57 48 45 52 45 20 78 20 3d 20 27 6b 65  nt WHERE x = 'ke
db80: 79 32 27 20 7d 0a 7d 20 7b 7d 0a 64 6f 5f 74 65  y2' }.} {}.do_te
db90: 73 74 20 65 5f 66 6b 65 79 2d 34 33 2e 39 20 7b  st e_fkey-43.9 {
dba0: 0a 20 20 63 61 74 63 68 73 71 6c 20 43 4f 4d 4d  .  catchsql COMM
dbb0: 49 54 0a 7d 20 7b 31 20 7b 66 6f 72 65 69 67 6e  IT.} {1 {foreign
dbc0: 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 20   key constraint 
dbd0: 66 61 69 6c 65 64 7d 7d 0a 64 6f 5f 74 65 73 74  failed}}.do_test
dbe0: 20 65 5f 66 6b 65 79 2d 34 33 2e 31 30 20 7b 0a   e_fkey-43.10 {.
dbf0: 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20    execsql {.    
dc00: 55 50 44 41 54 45 20 63 68 69 6c 64 32 20 53 45  UPDATE child2 SE
dc10: 54 20 63 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20  T c = NULL;.    
dc20: 43 4f 4d 4d 49 54 3b 0a 20 20 7d 0a 7d 20 7b 7d  COMMIT;.  }.} {}
dc30: 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ..#-------------
dc40: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
dc50: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
dc60: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
dc70: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20 2f  ------------.# /
dc80: 2a 20 45 56 3a 20 52 2d 30 33 33 35 33 2d 30 35  * EV: R-03353-05
dc90: 33 32 37 20 2a 2f 0a 23 0a 23 20 54 65 73 74 20  327 */.#.# Test 
dca0: 53 45 54 20 4e 55 4c 4c 20 61 63 74 69 6f 6e 73  SET NULL actions
dcb0: 2e 0a 23 0a 64 72 6f 70 5f 61 6c 6c 5f 74 61 62  ..#.drop_all_tab
dcc0: 6c 65 73 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b  les.do_test e_fk
dcd0: 65 79 2d 34 34 2e 31 20 7b 0a 20 20 65 78 65 63  ey-44.1 {.  exec
dce0: 73 71 6c 20 7b 0a 20 20 20 20 43 52 45 41 54 45  sql {.    CREATE
dcf0: 20 54 41 42 4c 45 20 70 41 28 78 20 50 52 49 4d   TABLE pA(x PRIM
dd00: 41 52 59 20 4b 45 59 29 3b 0a 20 20 20 20 43 52  ARY KEY);.    CR
dd10: 45 41 54 45 20 54 41 42 4c 45 20 63 41 28 63 20  EATE TABLE cA(c 
dd20: 52 45 46 45 52 45 4e 43 45 53 20 70 41 20 4f 4e  REFERENCES pA ON
dd30: 20 44 45 4c 45 54 45 20 53 45 54 20 4e 55 4c 4c   DELETE SET NULL
dd40: 29 3b 0a 20 20 20 20 43 52 45 41 54 45 20 54 41  );.    CREATE TA
dd50: 42 4c 45 20 63 42 28 63 20 52 45 46 45 52 45 4e  BLE cB(c REFEREN
dd60: 43 45 53 20 70 41 20 4f 4e 20 55 50 44 41 54 45  CES pA ON UPDATE
dd70: 20 53 45 54 20 4e 55 4c 4c 29 3b 0a 0a 20 20 20   SET NULL);..   
dd80: 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 70 41 20   INSERT INTO pA 
dd90: 56 41 4c 55 45 53 28 58 27 41 42 43 44 27 29 3b  VALUES(X'ABCD');
dda0: 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f  .    INSERT INTO
ddb0: 20 70 41 20 56 41 4c 55 45 53 28 58 27 31 32 33   pA VALUES(X'123
ddc0: 34 27 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20  4');.    INSERT 
ddd0: 49 4e 54 4f 20 63 41 20 56 41 4c 55 45 53 28 58  INTO cA VALUES(X
dde0: 27 41 42 43 44 27 29 3b 0a 20 20 20 20 49 4e 53  'ABCD');.    INS
ddf0: 45 52 54 20 49 4e 54 4f 20 63 42 20 56 41 4c 55  ERT INTO cB VALU
de00: 45 53 28 58 27 31 32 33 34 27 29 3b 0a 20 20 7d  ES(X'1234');.  }
de10: 0a 7d 20 7b 7d 0a 64 6f 5f 74 65 73 74 20 65 5f  .} {}.do_test e_
de20: 66 6b 65 79 2d 34 34 2e 32 20 7b 0a 20 20 65 78  fkey-44.2 {.  ex
de30: 65 63 73 71 6c 20 7b 0a 20 20 20 20 44 45 4c 45  ecsql {.    DELE
de40: 54 45 20 46 52 4f 4d 20 70 41 20 57 48 45 52 45  TE FROM pA WHERE
de50: 20 72 6f 77 69 64 20 3d 20 31 3b 0a 20 20 20 20   rowid = 1;.    
de60: 53 45 4c 45 43 54 20 71 75 6f 74 65 28 78 29 20  SELECT quote(x) 
de70: 46 52 4f 4d 20 70 41 3b 0a 20 20 7d 0a 7d 20 7b  FROM pA;.  }.} {
de80: 58 27 31 32 33 34 27 7d 0a 64 6f 5f 74 65 73 74  X'1234'}.do_test
de90: 20 65 5f 66 6b 65 79 2d 34 34 2e 33 20 7b 0a 20   e_fkey-44.3 {. 
dea0: 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 53   execsql {.    S
deb0: 45 4c 45 43 54 20 71 75 6f 74 65 28 63 29 20 46  ELECT quote(c) F
dec0: 52 4f 4d 20 63 41 3b 0a 20 20 7d 0a 7d 20 7b 4e  ROM cA;.  }.} {N
ded0: 55 4c 4c 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66  ULL}.do_test e_f
dee0: 6b 65 79 2d 34 34 2e 34 20 7b 0a 20 20 65 78 65  key-44.4 {.  exe
def0: 63 73 71 6c 20 7b 0a 20 20 20 20 55 50 44 41 54  csql {.    UPDAT
df00: 45 20 70 41 20 53 45 54 20 78 20 3d 20 58 27 38  E pA SET x = X'8
df10: 37 36 35 27 20 57 48 45 52 45 20 72 6f 77 69 64  765' WHERE rowid
df20: 20 3d 20 32 3b 0a 20 20 20 20 53 45 4c 45 43 54   = 2;.    SELECT
df30: 20 71 75 6f 74 65 28 78 29 20 46 52 4f 4d 20 70   quote(x) FROM p
df40: 41 3b 0a 20 20 7d 0a 7d 20 7b 58 27 38 37 36 35  A;.  }.} {X'8765
df50: 27 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65  '}.do_test e_fke
df60: 79 2d 34 34 2e 35 20 7b 0a 20 20 65 78 65 63 73  y-44.5 {.  execs
df70: 71 6c 20 7b 20 53 45 4c 45 43 54 20 71 75 6f 74  ql { SELECT quot
df80: 65 28 63 29 20 46 52 4f 4d 20 63 42 20 7d 0a 7d  e(c) FROM cB }.}
df90: 20 7b 4e 55 4c 4c 7d 0a 0a 23 2d 2d 2d 2d 2d 2d   {NULL}..#------
dfa0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
dfb0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
dfc0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
dfd0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
dfe0: 2d 2d 2d 0a 23 20 2f 2a 20 45 56 3a 20 52 2d 34  ---.# /* EV: R-4
dff0: 33 30 35 34 2d 35 34 38 33 32 20 2a 2f 0a 23 0a  3054-54832 */.#.
e000: 23 20 54 65 73 74 20 53 45 54 20 44 45 46 41 55  # Test SET DEFAU
e010: 4c 54 20 61 63 74 69 6f 6e 73 2e 0a 23 0a 64 72  LT actions..#.dr
e020: 6f 70 5f 61 6c 6c 5f 74 61 62 6c 65 73 0a 64 6f  op_all_tables.do
e030: 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 34 35 2e  _test e_fkey-45.
e040: 31 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a  1 {.  execsql {.
e050: 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45      CREATE TABLE
e060: 20 70 41 28 78 20 50 52 49 4d 41 52 59 20 4b 45   pA(x PRIMARY KE
e070: 59 29 3b 0a 20 20 20 20 43 52 45 41 54 45 20 54  Y);.    CREATE T
e080: 41 42 4c 45 20 63 41 28 63 20 44 45 46 41 55 4c  ABLE cA(c DEFAUL
e090: 54 20 58 27 30 30 30 30 27 20 52 45 46 45 52 45  T X'0000' REFERE
e0a0: 4e 43 45 53 20 70 41 20 4f 4e 20 44 45 4c 45 54  NCES pA ON DELET
e0b0: 45 20 53 45 54 20 44 45 46 41 55 4c 54 29 3b 0a  E SET DEFAULT);.
e0c0: 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45      CREATE TABLE
e0d0: 20 63 42 28 63 20 44 45 46 41 55 4c 54 20 58 27   cB(c DEFAULT X'
e0e0: 39 39 39 39 27 20 52 45 46 45 52 45 4e 43 45 53  9999' REFERENCES
e0f0: 20 70 41 20 4f 4e 20 55 50 44 41 54 45 20 53 45   pA ON UPDATE SE
e100: 54 20 44 45 46 41 55 4c 54 29 3b 0a 0a 20 20 20  T DEFAULT);..   
e110: 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 70 41 28   INSERT INTO pA(
e120: 72 6f 77 69 64 2c 20 78 29 20 56 41 4c 55 45 53  rowid, x) VALUES
e130: 28 31 2c 20 58 27 30 30 30 30 27 29 3b 0a 20 20  (1, X'0000');.  
e140: 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 70 41    INSERT INTO pA
e150: 28 72 6f 77 69 64 2c 20 78 29 20 56 41 4c 55 45  (rowid, x) VALUE
e160: 53 28 32 2c 20 58 27 39 39 39 39 27 29 3b 0a 20  S(2, X'9999');. 
e170: 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 70     INSERT INTO p
e180: 41 28 72 6f 77 69 64 2c 20 78 29 20 56 41 4c 55  A(rowid, x) VALU
e190: 45 53 28 33 2c 20 58 27 41 42 43 44 27 29 3b 0a  ES(3, X'ABCD');.
e1a0: 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20      INSERT INTO 
e1b0: 70 41 28 72 6f 77 69 64 2c 20 78 29 20 56 41 4c  pA(rowid, x) VAL
e1c0: 55 45 53 28 34 2c 20 58 27 31 32 33 34 27 29 3b  UES(4, X'1234');
e1d0: 0a 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54  ..    INSERT INT
e1e0: 4f 20 63 41 20 56 41 4c 55 45 53 28 58 27 41 42  O cA VALUES(X'AB
e1f0: 43 44 27 29 3b 0a 20 20 20 20 49 4e 53 45 52 54  CD');.    INSERT
e200: 20 49 4e 54 4f 20 63 42 20 56 41 4c 55 45 53 28   INTO cB VALUES(
e210: 58 27 31 32 33 34 27 29 3b 0a 20 20 7d 0a 7d 20  X'1234');.  }.} 
e220: 7b 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65  {}.do_test e_fke
e230: 79 2d 34 35 2e 32 20 7b 0a 20 20 65 78 65 63 73  y-45.2 {.  execs
e240: 71 6c 20 7b 0a 20 20 20 20 44 45 4c 45 54 45 20  ql {.    DELETE 
e250: 46 52 4f 4d 20 70 41 20 57 48 45 52 45 20 72 6f  FROM pA WHERE ro
e260: 77 69 64 20 3d 20 33 3b 0a 20 20 20 20 53 45 4c  wid = 3;.    SEL
e270: 45 43 54 20 71 75 6f 74 65 28 78 29 20 46 52 4f  ECT quote(x) FRO
e280: 4d 20 70 41 3b 0a 20 20 7d 0a 7d 20 7b 58 27 30  M pA;.  }.} {X'0
e290: 30 30 30 27 20 58 27 39 39 39 39 27 20 58 27 31  000' X'9999' X'1
e2a0: 32 33 34 27 7d 0a 64 6f 5f 74 65 73 74 20 65 5f  234'}.do_test e_
e2b0: 66 6b 65 79 2d 34 35 2e 33 20 7b 0a 20 20 65 78  fkey-45.3 {.  ex
e2c0: 65 63 73 71 6c 20 7b 20 53 45 4c 45 43 54 20 71  ecsql { SELECT q
e2d0: 75 6f 74 65 28 63 29 20 46 52 4f 4d 20 63 41 20  uote(c) FROM cA 
e2e0: 7d 0a 7d 20 7b 58 27 30 30 30 30 27 7d 0a 64 6f  }.} {X'0000'}.do
e2f0: 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 34 35 2e  _test e_fkey-45.
e300: 34 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a  4 {.  execsql {.
e310: 20 20 20 20 55 50 44 41 54 45 20 70 41 20 53 45      UPDATE pA SE
e320: 54 20 78 20 3d 20 58 27 38 37 36 35 27 20 57 48  T x = X'8765' WH
e330: 45 52 45 20 72 6f 77 69 64 20 3d 20 34 3b 0a 20  ERE rowid = 4;. 
e340: 20 20 20 53 45 4c 45 43 54 20 71 75 6f 74 65 28     SELECT quote(
e350: 78 29 20 46 52 4f 4d 20 70 41 3b 0a 20 20 7d 0a  x) FROM pA;.  }.
e360: 7d 20 7b 58 27 30 30 30 30 27 20 58 27 39 39 39  } {X'0000' X'999
e370: 39 27 20 58 27 38 37 36 35 27 7d 0a 64 6f 5f 74  9' X'8765'}.do_t
e380: 65 73 74 20 65 5f 66 6b 65 79 2d 34 35 2e 35 20  est e_fkey-45.5 
e390: 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 20 53 45  {.  execsql { SE
e3a0: 4c 45 43 54 20 71 75 6f 74 65 28 63 29 20 46 52  LECT quote(c) FR
e3b0: 4f 4d 20 63 42 20 7d 0a 7d 20 7b 58 27 39 39 39  OM cB }.} {X'999
e3c0: 39 27 7d 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  9'}..#----------
e3d0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
e3e0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
e3f0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
e400: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a  ---------------.
e410: 23 20 2f 2a 20 45 56 3a 20 52 2d 36 31 33 37 36  # /* EV: R-61376
e420: 2d 35 37 32 36 37 20 2a 2f 0a 23 20 2f 2a 20 45  -57267 */.# /* E
e430: 56 3a 20 52 2d 36 31 38 30 39 2d 36 32 32 30 37  V: R-61809-62207
e440: 20 2a 2f 0a 23 0a 23 20 54 65 73 74 20 4f 4e 20   */.#.# Test ON 
e450: 44 45 4c 45 54 45 20 43 41 53 43 41 44 45 20 61  DELETE CASCADE a
e460: 63 74 69 6f 6e 73 2e 0a 23 0a 64 72 6f 70 5f 61  ctions..#.drop_a
e470: 6c 6c 5f 74 61 62 6c 65 73 0a 64 6f 5f 74 65 73  ll_tables.do_tes
e480: 74 20 65 5f 66 6b 65 79 2d 34 36 2e 31 20 7b 0a  t e_fkey-46.1 {.
e490: 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20    execsql {.    
e4a0: 43 52 45 41 54 45 20 54 41 42 4c 45 20 70 31 28  CREATE TABLE p1(
e4b0: 61 2c 20 62 20 55 4e 49 51 55 45 29 3b 0a 20 20  a, b UNIQUE);.  
e4c0: 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 63    CREATE TABLE c
e4d0: 31 28 63 20 52 45 46 45 52 45 4e 43 45 53 20 70  1(c REFERENCES p
e4e0: 31 28 62 29 20 4f 4e 20 44 45 4c 45 54 45 20 43  1(b) ON DELETE C
e4f0: 41 53 43 41 44 45 2c 20 64 29 3b 0a 20 20 20 20  ASCADE, d);.    
e500: 49 4e 53 45 52 54 20 49 4e 54 4f 20 70 31 20 56  INSERT INTO p1 V
e510: 41 4c 55 45 53 28 4e 55 4c 4c 2c 20 4e 55 4c 4c  ALUES(NULL, NULL
e520: 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e  );.    INSERT IN
e530: 54 4f 20 70 31 20 56 41 4c 55 45 53 28 34 2c 20  TO p1 VALUES(4, 
e540: 34 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49  4);.    INSERT I
e550: 4e 54 4f 20 70 31 20 56 41 4c 55 45 53 28 35 2c  NTO p1 VALUES(5,
e560: 20 35 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20   5);.    INSERT 
e570: 49 4e 54 4f 20 63 31 20 56 41 4c 55 45 53 28 4e  INTO c1 VALUES(N
e580: 55 4c 4c 2c 20 4e 55 4c 4c 29 3b 0a 20 20 20 20  ULL, NULL);.    
e590: 49 4e 53 45 52 54 20 49 4e 54 4f 20 63 31 20 56  INSERT INTO c1 V
e5a0: 41 4c 55 45 53 28 34 2c 20 34 29 3b 0a 20 20 20  ALUES(4, 4);.   
e5b0: 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 63 31 20   INSERT INTO c1 
e5c0: 56 41 4c 55 45 53 28 35 2c 20 35 29 3b 0a 20 20  VALUES(5, 5);.  
e5d0: 20 20 53 45 4c 45 43 54 20 63 6f 75 6e 74 28 2a    SELECT count(*
e5e0: 29 20 46 52 4f 4d 20 63 31 3b 0a 20 20 7d 0a 7d  ) FROM c1;.  }.}
e5f0: 20 7b 33 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66   {3}.do_test e_f
e600: 6b 65 79 2d 34 36 2e 32 20 7b 0a 20 20 65 78 65  key-46.2 {.  exe
e610: 63 73 71 6c 20 7b 0a 20 20 20 20 44 45 4c 45 54  csql {.    DELET
e620: 45 20 46 52 4f 4d 20 70 31 20 57 48 45 52 45 20  E FROM p1 WHERE 
e630: 61 20 3d 20 34 3b 0a 20 20 20 20 53 45 4c 45 43  a = 4;.    SELEC
e640: 54 20 64 2c 20 63 20 46 52 4f 4d 20 63 31 3b 0a  T d, c FROM c1;.
e650: 20 20 7d 0a 7d 20 7b 7b 7d 20 7b 7d 20 35 20 35    }.} {{} {} 5 5
e660: 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79  }.do_test e_fkey
e670: 2d 34 36 2e 33 20 7b 0a 20 20 65 78 65 63 73 71  -46.3 {.  execsq
e680: 6c 20 7b 0a 20 20 20 20 44 45 4c 45 54 45 20 46  l {.    DELETE F
e690: 52 4f 4d 20 70 31 3b 0a 20 20 20 20 53 45 4c 45  ROM p1;.    SELE
e6a0: 43 54 20 64 2c 20 63 20 46 52 4f 4d 20 63 31 3b  CT d, c FROM c1;
e6b0: 0a 20 20 7d 0a 7d 20 7b 7b 7d 20 7b 7d 7d 0a 64  .  }.} {{} {}}.d
e6c0: 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 34 36  o_test e_fkey-46
e6d0: 2e 34 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b  .4 {.  execsql {
e6e0: 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 70   SELECT * FROM p
e6f0: 31 20 7d 0a 7d 20 7b 7d 0a 0a 0a 23 2d 2d 2d 2d  1 }.} {}...#----
e700: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
e710: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
e720: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
e730: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
e740: 2d 2d 2d 2d 2d 0a 23 20 2f 2a 20 45 56 3a 20 52  -----.# /* EV: R
e750: 2d 36 31 33 37 36 2d 35 37 32 36 37 20 2a 2f 0a  -61376-57267 */.
e760: 23 20 2f 2a 20 45 56 3a 20 52 2d 31 33 38 37 37  # /* EV: R-13877
e770: 2d 36 34 35 34 32 20 2a 2f 0a 23 0a 23 20 54 65  -64542 */.#.# Te
e780: 73 74 20 4f 4e 20 55 50 44 41 54 45 20 43 41 53  st ON UPDATE CAS
e790: 43 41 44 45 20 61 63 74 69 6f 6e 73 2e 0a 23 0a  CADE actions..#.
e7a0: 64 72 6f 70 5f 61 6c 6c 5f 74 61 62 6c 65 73 0a  drop_all_tables.
e7b0: 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 34  do_test e_fkey-4
e7c0: 37 2e 31 20 7b 0a 20 20 65 78 65 63 73 71 6c 20  7.1 {.  execsql 
e7d0: 7b 0a 20 20 20 20 43 52 45 41 54 45 20 54 41 42  {.    CREATE TAB
e7e0: 4c 45 20 70 31 28 61 2c 20 62 20 55 4e 49 51 55  LE p1(a, b UNIQU
e7f0: 45 29 3b 0a 20 20 20 20 43 52 45 41 54 45 20 54  E);.    CREATE T
e800: 41 42 4c 45 20 63 31 28 63 20 52 45 46 45 52 45  ABLE c1(c REFERE
e810: 4e 43 45 53 20 70 31 28 62 29 20 4f 4e 20 55 50  NCES p1(b) ON UP
e820: 44 41 54 45 20 43 41 53 43 41 44 45 2c 20 64 29  DATE CASCADE, d)
e830: 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54  ;.    INSERT INT
e840: 4f 20 70 31 20 56 41 4c 55 45 53 28 4e 55 4c 4c  O p1 VALUES(NULL
e850: 2c 20 4e 55 4c 4c 29 3b 0a 20 20 20 20 49 4e 53  , NULL);.    INS
e860: 45 52 54 20 49 4e 54 4f 20 70 31 20 56 41 4c 55  ERT INTO p1 VALU
e870: 45 53 28 34 2c 20 34 29 3b 0a 20 20 20 20 49 4e  ES(4, 4);.    IN
e880: 53 45 52 54 20 49 4e 54 4f 20 70 31 20 56 41 4c  SERT INTO p1 VAL
e890: 55 45 53 28 35 2c 20 35 29 3b 0a 20 20 20 20 49  UES(5, 5);.    I
e8a0: 4e 53 45 52 54 20 49 4e 54 4f 20 63 31 20 56 41  NSERT INTO c1 VA
e8b0: 4c 55 45 53 28 4e 55 4c 4c 2c 20 4e 55 4c 4c 29  LUES(NULL, NULL)
e8c0: 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54  ;.    INSERT INT
e8d0: 4f 20 63 31 20 56 41 4c 55 45 53 28 34 2c 20 34  O c1 VALUES(4, 4
e8e0: 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e  );.    INSERT IN
e8f0: 54 4f 20 63 31 20 56 41 4c 55 45 53 28 35 2c 20  TO c1 VALUES(5, 
e900: 35 29 3b 0a 20 20 20 20 53 45 4c 45 43 54 20 63  5);.    SELECT c
e910: 6f 75 6e 74 28 2a 29 20 46 52 4f 4d 20 63 31 3b  ount(*) FROM c1;
e920: 0a 20 20 7d 0a 7d 20 7b 33 7d 0a 64 6f 5f 74 65  .  }.} {3}.do_te
e930: 73 74 20 65 5f 66 6b 65 79 2d 34 37 2e 32 20 7b  st e_fkey-47.2 {
e940: 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20  .  execsql {.   
e950: 20 55 50 44 41 54 45 20 70 31 20 53 45 54 20 62   UPDATE p1 SET b
e960: 20 3d 20 31 30 20 57 48 45 52 45 20 62 20 3d 20   = 10 WHERE b = 
e970: 35 3b 0a 20 20 20 20 53 45 4c 45 43 54 20 64 2c  5;.    SELECT d,
e980: 20 63 20 46 52 4f 4d 20 63 31 3b 0a 20 20 7d 0a   c FROM c1;.  }.
e990: 7d 20 7b 7b 7d 20 7b 7d 20 34 20 34 20 35 20 31  } {{} {} 4 4 5 1
e9a0: 30 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65  0}.do_test e_fke
e9b0: 79 2d 34 37 2e 33 20 7b 0a 20 20 65 78 65 63 73  y-47.3 {.  execs
e9c0: 71 6c 20 7b 0a 20 20 20 20 55 50 44 41 54 45 20  ql {.    UPDATE 
e9d0: 70 31 20 53 45 54 20 62 20 3d 20 31 31 20 57 48  p1 SET b = 11 WH
e9e0: 45 52 45 20 62 20 3d 20 34 3b 0a 20 20 20 20 53  ERE b = 4;.    S
e9f0: 45 4c 45 43 54 20 64 2c 20 63 20 46 52 4f 4d 20  ELECT d, c FROM 
ea00: 63 31 3b 0a 20 20 7d 0a 7d 20 7b 7b 7d 20 7b 7d  c1;.  }.} {{} {}
ea10: 20 34 20 31 31 20 35 20 31 30 7d 0a 64 6f 5f 74   4 11 5 10}.do_t
ea20: 65 73 74 20 65 5f 66 6b 65 79 2d 34 37 2e 34 20  est e_fkey-47.4 
ea30: 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 20 0a 20  {.  execsql { . 
ea40: 20 20 20 55 50 44 41 54 45 20 70 31 20 53 45 54     UPDATE p1 SET
ea50: 20 62 20 3d 20 36 20 57 48 45 52 45 20 62 20 49   b = 6 WHERE b I
ea60: 53 20 4e 55 4c 4c 3b 0a 20 20 20 20 53 45 4c 45  S NULL;.    SELE
ea70: 43 54 20 64 2c 20 63 20 46 52 4f 4d 20 63 31 3b  CT d, c FROM c1;
ea80: 0a 20 20 7d 0a 7d 20 7b 7b 7d 20 7b 7d 20 34 20  .  }.} {{} {} 4 
ea90: 31 31 20 35 20 31 30 7d 0a 64 6f 5f 74 65 73 74  11 5 10}.do_test
eaa0: 20 65 5f 66 6b 65 79 2d 34 36 2e 35 20 7b 0a 20   e_fkey-46.5 {. 
eab0: 20 65 78 65 63 73 71 6c 20 7b 20 53 45 4c 45 43   execsql { SELEC
eac0: 54 20 2a 20 46 52 4f 4d 20 70 31 20 7d 0a 7d 20  T * FROM p1 }.} 
ead0: 7b 7b 7d 20 36 20 34 20 31 31 20 35 20 31 30 7d  {{} 6 4 11 5 10}
eae0: 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ..#-------------
eaf0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
eb00: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
eb10: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
eb20: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20 2f  ------------.# /
eb30: 2a 20 45 56 3a 20 52 2d 35 31 33 32 39 2d 33 33  * EV: R-51329-33
eb40: 34 33 38 20 2a 2f 0a 23 0a 23 20 54 65 73 74 20  438 */.#.# Test 
eb50: 61 6e 20 65 78 61 6d 70 6c 65 20 66 72 6f 6d 20  an example from 
eb60: 74 68 65 20 22 4f 4e 20 44 45 4c 45 54 45 20 61  the "ON DELETE a
eb70: 6e 64 20 4f 4e 20 55 50 44 41 54 45 20 41 63 74  nd ON UPDATE Act
eb80: 69 6f 6e 73 22 20 73 65 63 74 69 6f 6e 20 0a 23  ions" section .#
eb90: 20 6f 66 20 66 6f 72 65 69 67 6e 6b 65 79 73 2e   of foreignkeys.
eba0: 68 74 6d 6c 2e 0a 23 0a 64 72 6f 70 5f 61 6c 6c  html..#.drop_all
ebb0: 5f 74 61 62 6c 65 73 0a 64 6f 5f 74 65 73 74 20  _tables.do_test 
ebc0: 65 5f 66 6b 65 79 2d 34 38 2e 31 20 7b 0a 20 20  e_fkey-48.1 {.  
ebd0: 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 43 52  execsql {.    CR
ebe0: 45 41 54 45 20 54 41 42 4c 45 20 61 72 74 69 73  EATE TABLE artis
ebf0: 74 28 0a 20 20 20 20 20 20 61 72 74 69 73 74 69  t(.      artisti
ec00: 64 20 20 20 20 49 4e 54 45 47 45 52 20 50 52 49  d    INTEGER PRI
ec10: 4d 41 52 59 20 4b 45 59 2c 20 0a 20 20 20 20 20  MARY KEY, .     
ec20: 20 61 72 74 69 73 74 6e 61 6d 65 20 20 54 45 58   artistname  TEX
ec30: 54 0a 20 20 20 20 29 3b 0a 20 20 20 20 43 52 45  T.    );.    CRE
ec40: 41 54 45 20 54 41 42 4c 45 20 74 72 61 63 6b 28  ATE TABLE track(
ec50: 0a 20 20 20 20 20 20 74 72 61 63 6b 69 64 20 20  .      trackid  
ec60: 20 20 20 49 4e 54 45 47 45 52 2c 0a 20 20 20 20     INTEGER,.    
ec70: 20 20 74 72 61 63 6b 6e 61 6d 65 20 20 20 54 45    trackname   TE
ec80: 58 54 2c 20 0a 20 20 20 20 20 20 74 72 61 63 6b  XT, .      track
ec90: 61 72 74 69 73 74 20 49 4e 54 45 47 45 52 20 52  artist INTEGER R
eca0: 45 46 45 52 45 4e 43 45 53 20 61 72 74 69 73 74  EFERENCES artist
ecb0: 28 61 72 74 69 73 74 69 64 29 20 4f 4e 20 55 50  (artistid) ON UP
ecc0: 44 41 54 45 20 43 41 53 43 41 44 45 0a 20 20 20  DATE CASCADE.   
ecd0: 20 29 3b 0a 0a 20 20 20 20 49 4e 53 45 52 54 20   );..    INSERT 
ece0: 49 4e 54 4f 20 61 72 74 69 73 74 20 56 41 4c 55  INTO artist VALU
ecf0: 45 53 28 31 2c 20 27 44 65 61 6e 20 4d 61 72 74  ES(1, 'Dean Mart
ed00: 69 6e 27 29 3b 0a 20 20 20 20 49 4e 53 45 52 54  in');.    INSERT
ed10: 20 49 4e 54 4f 20 61 72 74 69 73 74 20 56 41 4c   INTO artist VAL
ed20: 55 45 53 28 32 2c 20 27 46 72 61 6e 6b 20 53 69  UES(2, 'Frank Si
ed30: 6e 61 74 72 61 27 29 3b 0a 20 20 20 20 49 4e 53  natra');.    INS
ed40: 45 52 54 20 49 4e 54 4f 20 74 72 61 63 6b 20 56  ERT INTO track V
ed50: 41 4c 55 45 53 28 31 31 2c 20 27 54 68 61 74 27  ALUES(11, 'That'
ed60: 27 73 20 41 6d 6f 72 65 27 2c 20 31 29 3b 0a 20  's Amore', 1);. 
ed70: 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74     INSERT INTO t
ed80: 72 61 63 6b 20 56 41 4c 55 45 53 28 31 32 2c 20  rack VALUES(12, 
ed90: 27 43 68 72 69 73 74 6d 61 73 20 42 6c 75 65 73  'Christmas Blues
eda0: 27 2c 20 31 29 3b 0a 20 20 20 20 49 4e 53 45 52  ', 1);.    INSER
edb0: 54 20 49 4e 54 4f 20 74 72 61 63 6b 20 56 41 4c  T INTO track VAL
edc0: 55 45 53 28 31 33 2c 20 27 4d 79 20 57 61 79 27  UES(13, 'My Way'
edd0: 2c 20 32 29 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a 64  , 2);.  }.} {}.d
ede0: 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 34 38  o_test e_fkey-48
edf0: 2e 32 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b  .2 {.  execsql {
ee00: 0a 20 20 20 20 55 50 44 41 54 45 20 61 72 74 69  .    UPDATE arti
ee10: 73 74 20 53 45 54 20 61 72 74 69 73 74 69 64 20  st SET artistid 
ee20: 3d 20 31 30 30 20 57 48 45 52 45 20 61 72 74 69  = 100 WHERE arti
ee30: 73 74 6e 61 6d 65 20 3d 20 27 44 65 61 6e 20 4d  stname = 'Dean M
ee40: 61 72 74 69 6e 27 3b 0a 20 20 7d 0a 7d 20 7b 7d  artin';.  }.} {}
ee50: 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d  .do_test e_fkey-
ee60: 34 38 2e 33 20 7b 0a 20 20 65 78 65 63 73 71 6c  48.3 {.  execsql
ee70: 20 7b 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d   { SELECT * FROM
ee80: 20 61 72 74 69 73 74 20 7d 0a 7d 20 7b 32 20 7b   artist }.} {2 {
ee90: 46 72 61 6e 6b 20 53 69 6e 61 74 72 61 7d 20 31  Frank Sinatra} 1
eea0: 30 30 20 7b 44 65 61 6e 20 4d 61 72 74 69 6e 7d  00 {Dean Martin}
eeb0: 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79  }.do_test e_fkey
eec0: 2d 34 38 2e 34 20 7b 0a 20 20 65 78 65 63 73 71  -48.4 {.  execsq
eed0: 6c 20 7b 20 53 45 4c 45 43 54 20 2a 20 46 52 4f  l { SELECT * FRO
eee0: 4d 20 74 72 61 63 6b 20 7d 0a 7d 20 7b 31 31 20  M track }.} {11 
eef0: 7b 54 68 61 74 27 73 20 41 6d 6f 72 65 7d 20 31  {That's Amore} 1
ef00: 30 30 20 31 32 20 7b 43 68 72 69 73 74 6d 61 73  00 12 {Christmas
ef10: 20 42 6c 75 65 73 7d 20 31 30 30 20 31 33 20 7b   Blues} 100 13 {
ef20: 4d 79 20 57 61 79 7d 20 32 7d 0a 0a 0a 23 2d 2d  My Way} 2}...#--
ef30: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
ef40: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
ef50: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
ef60: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
ef70: 2d 2d 2d 2d 2d 2d 2d 0a 23 20 2f 2a 20 45 56 3a  -------.# /* EV:
ef80: 20 52 2d 35 33 39 36 38 2d 35 31 36 34 32 20 2a   R-53968-51642 *
ef90: 2f 0a 23 0a 23 20 56 65 72 69 66 79 20 74 68 61  /.#.# Verify tha
efa0: 74 20 61 64 64 69 6e 67 20 61 6e 20 46 4b 20 61  t adding an FK a
efb0: 63 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 61  ction does not a
efc0: 62 73 6f 6c 76 65 20 74 68 65 20 75 73 65 72 20  bsolve the user 
efd0: 6f 66 20 74 68 65 20 0a 23 20 72 65 71 75 69 72  of the .# requir
efe0: 65 6d 65 6e 74 20 6e 6f 74 20 74 6f 20 76 69 6f  ement not to vio
eff0: 6c 61 74 65 20 74 68 65 20 66 6f 72 65 69 67 6e  late the foreign
f000: 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 2e   key constraint.
f010: 0a 23 0a 64 72 6f 70 5f 61 6c 6c 5f 74 61 62 6c  .#.drop_all_tabl
f020: 65 73 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65  es.do_test e_fke
f030: 79 2d 34 39 2e 31 20 7b 0a 20 20 65 78 65 63 73  y-49.1 {.  execs
f040: 71 6c 20 7b 0a 20 20 20 20 43 52 45 41 54 45 20  ql {.    CREATE 
f050: 54 41 42 4c 45 20 70 61 72 65 6e 74 28 61 20 43  TABLE parent(a C
f060: 4f 4c 4c 41 54 45 20 6e 6f 63 61 73 65 2c 20 62  OLLATE nocase, b
f070: 2c 20 63 2c 20 50 52 49 4d 41 52 59 20 4b 45 59  , c, PRIMARY KEY
f080: 28 63 2c 20 61 29 29 3b 0a 20 20 20 20 43 52 45  (c, a));.    CRE
f090: 41 54 45 20 54 41 42 4c 45 20 63 68 69 6c 64 28  ATE TABLE child(
f0a0: 64 20 44 45 46 41 55 4c 54 20 27 61 27 2c 20 65  d DEFAULT 'a', e
f0b0: 2c 20 66 20 44 45 46 41 55 4c 54 20 27 63 27 2c  , f DEFAULT 'c',
f0c0: 0a 20 20 20 20 20 20 46 4f 52 45 49 47 4e 20 4b  .      FOREIGN K
f0d0: 45 59 28 66 2c 20 64 29 20 52 45 46 45 52 45 4e  EY(f, d) REFEREN
f0e0: 43 45 53 20 70 61 72 65 6e 74 20 4f 4e 20 55 50  CES parent ON UP
f0f0: 44 41 54 45 20 53 45 54 20 44 45 46 41 55 4c 54  DATE SET DEFAULT
f100: 0a 20 20 20 20 29 3b 0a 0a 20 20 20 20 49 4e 53  .    );..    INS
f110: 45 52 54 20 49 4e 54 4f 20 70 61 72 65 6e 74 20  ERT INTO parent 
f120: 56 41 4c 55 45 53 28 27 41 27 2c 20 27 62 27 2c  VALUES('A', 'b',
f130: 20 27 63 27 29 3b 0a 20 20 20 20 49 4e 53 45 52   'c');.    INSER
f140: 54 20 49 4e 54 4f 20 70 61 72 65 6e 74 20 56 41  T INTO parent VA
f150: 4c 55 45 53 28 27 4f 4e 45 27 2c 20 27 74 77 6f  LUES('ONE', 'two
f160: 27 2c 20 27 74 68 72 65 65 27 29 3b 0a 20 20 20  ', 'three');.   
f170: 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 63 68 69   INSERT INTO chi
f180: 6c 64 20 56 41 4c 55 45 53 28 27 6f 6e 65 27 2c  ld VALUES('one',
f190: 20 27 74 77 6f 27 2c 20 27 74 68 72 65 65 27 29   'two', 'three')
f1a0: 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a 64 6f 5f 74 65  ;.  }.} {}.do_te
f1b0: 73 74 20 65 5f 66 6b 65 79 2d 34 39 2e 32 20 7b  st e_fkey-49.2 {
f1c0: 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20  .  execsql {.   
f1d0: 20 42 45 47 49 4e 3b 0a 20 20 20 20 20 20 55 50   BEGIN;.      UP
f1e0: 44 41 54 45 20 70 61 72 65 6e 74 20 53 45 54 20  DATE parent SET 
f1f0: 61 20 3d 20 27 27 20 57 48 45 52 45 20 61 20 3d  a = '' WHERE a =
f200: 20 27 6f 4e 65 27 3b 0a 20 20 20 20 20 20 53 45   'oNe';.      SE
f210: 4c 45 43 54 20 2a 20 46 52 4f 4d 20 63 68 69 6c  LECT * FROM chil
f220: 64 3b 0a 20 20 7d 0a 7d 20 7b 61 20 74 77 6f 20  d;.  }.} {a two 
f230: 63 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65  c}.do_test e_fke
f240: 79 2d 34 39 2e 33 20 7b 0a 20 20 65 78 65 63 73  y-49.3 {.  execs
f250: 71 6c 20 7b 0a 20 20 20 20 52 4f 4c 4c 42 41 43  ql {.    ROLLBAC
f260: 4b 3b 0a 20 20 20 20 44 45 4c 45 54 45 20 46 52  K;.    DELETE FR
f270: 4f 4d 20 70 61 72 65 6e 74 20 57 48 45 52 45 20  OM parent WHERE 
f280: 61 20 3d 20 27 41 27 3b 0a 20 20 20 20 53 45 4c  a = 'A';.    SEL
f290: 45 43 54 20 2a 20 46 52 4f 4d 20 70 61 72 65 6e  ECT * FROM paren
f2a0: 74 3b 0a 20 20 7d 0a 7d 20 7b 4f 4e 45 20 74 77  t;.  }.} {ONE tw
f2b0: 6f 20 74 68 72 65 65 7d 0a 64 6f 5f 74 65 73 74  o three}.do_test
f2c0: 20 65 5f 66 6b 65 79 2d 34 39 2e 34 20 7b 0a 20   e_fkey-49.4 {. 
f2d0: 20 63 61 74 63 68 73 71 6c 20 7b 20 55 50 44 41   catchsql { UPDA
f2e0: 54 45 20 70 61 72 65 6e 74 20 53 45 54 20 61 20  TE parent SET a 
f2f0: 3d 20 27 27 20 57 48 45 52 45 20 61 20 3d 20 27  = '' WHERE a = '
f300: 6f 4e 65 27 20 7d 0a 7d 20 7b 31 20 7b 66 6f 72  oNe' }.} {1 {for
f310: 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61  eign key constra
f320: 69 6e 74 20 66 61 69 6c 65 64 7d 7d 0a 0a 0a 23  int failed}}...#
f330: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
f340: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
f350: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
f360: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
f370: 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20 2f 2a 20 45  ---------.# /* E
f380: 56 3a 20 52 2d 30 37 30 36 35 2d 35 39 35 38 38  V: R-07065-59588
f390: 20 2a 2f 0a 23 20 2f 2a 20 45 56 3a 20 52 2d 32   */.# /* EV: R-2
f3a0: 38 32 32 30 2d 34 36 36 39 34 20 2a 2f 0a 23 0a  8220-46694 */.#.
f3b0: 23 20 54 65 73 74 20 61 6e 20 65 78 61 6d 70 6c  # Test an exampl
f3c0: 65 20 66 72 6f 6d 20 74 68 65 20 22 4f 4e 20 44  e from the "ON D
f3d0: 45 4c 45 54 45 20 61 6e 64 20 4f 4e 20 55 50 44  ELETE and ON UPD
f3e0: 41 54 45 20 41 63 74 69 6f 6e 73 22 20 73 65 63  ATE Actions" sec
f3f0: 74 69 6f 6e 20 0a 23 20 6f 66 20 66 6f 72 65 69  tion .# of forei
f400: 67 6e 6b 65 79 73 2e 68 74 6d 6c 2e 20 54 68 69  gnkeys.html. Thi
f410: 73 20 65 78 61 6d 70 6c 65 20 73 68 6f 77 73 20  s example shows 
f420: 74 68 61 74 20 61 64 64 69 6e 67 20 61 6e 20 22  that adding an "
f430: 4f 4e 20 44 45 4c 45 54 45 20 44 45 46 41 55 4c  ON DELETE DEFAUL
f440: 54 22 0a 23 20 63 6c 61 75 73 65 20 64 6f 65 73  T".# clause does
f450: 20 6e 6f 74 20 61 62 72 6f 67 61 74 65 20 74 68   not abrogate th
f460: 65 20 6e 65 65 64 20 74 6f 20 73 61 74 69 73 66  e need to satisf
f470: 79 20 74 68 65 20 66 6f 72 65 69 67 6e 20 6b 65  y the foreign ke
f480: 79 20 63 6f 6e 73 74 72 61 69 6e 74 0a 23 20 28  y constraint.# (
f490: 52 2d 32 38 32 32 30 2d 34 36 36 39 34 29 2e 0a  R-28220-46694)..
f4a0: 23 0a 64 72 6f 70 5f 61 6c 6c 5f 74 61 62 6c 65  #.drop_all_table
f4b0: 73 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79  s.do_test e_fkey
f4c0: 2d 35 30 2e 31 20 7b 0a 20 20 65 78 65 63 73 71  -50.1 {.  execsq
f4d0: 6c 20 7b 0a 20 20 20 20 43 52 45 41 54 45 20 54  l {.    CREATE T
f4e0: 41 42 4c 45 20 61 72 74 69 73 74 28 0a 20 20 20  ABLE artist(.   
f4f0: 20 20 20 61 72 74 69 73 74 69 64 20 20 20 20 49     artistid    I
f500: 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b  NTEGER PRIMARY K
f510: 45 59 2c 20 0a 20 20 20 20 20 20 61 72 74 69 73  EY, .      artis
f520: 74 6e 61 6d 65 20 20 54 45 58 54 0a 20 20 20 20  tname  TEXT.    
f530: 29 3b 0a 20 20 20 20 43 52 45 41 54 45 20 54 41  );.    CREATE TA
f540: 42 4c 45 20 74 72 61 63 6b 28 0a 20 20 20 20 20  BLE track(.     
f550: 20 74 72 61 63 6b 69 64 20 20 20 20 20 49 4e 54   trackid     INT
f560: 45 47 45 52 2c 0a 20 20 20 20 20 20 74 72 61 63  EGER,.      trac
f570: 6b 6e 61 6d 65 20 20 20 54 45 58 54 2c 20 0a 20  kname   TEXT, . 
f580: 20 20 20 20 20 74 72 61 63 6b 61 72 74 69 73 74       trackartist
f590: 20 49 4e 54 45 47 45 52 20 44 45 46 41 55 4c 54   INTEGER DEFAULT
f5a0: 20 30 20 52 45 46 45 52 45 4e 43 45 53 20 61 72   0 REFERENCES ar
f5b0: 74 69 73 74 28 61 72 74 69 73 74 69 64 29 20 4f  tist(artistid) O
f5c0: 4e 20 44 45 4c 45 54 45 20 53 45 54 20 44 45 46  N DELETE SET DEF
f5d0: 41 55 4c 54 0a 20 20 20 20 29 3b 0a 20 20 20 20  AULT.    );.    
f5e0: 49 4e 53 45 52 54 20 49 4e 54 4f 20 61 72 74 69  INSERT INTO arti
f5f0: 73 74 20 56 41 4c 55 45 53 28 33 2c 20 27 53 61  st VALUES(3, 'Sa
f600: 6d 6d 79 20 44 61 76 69 73 20 4a 72 2e 27 29 3b  mmy Davis Jr.');
f610: 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f  .    INSERT INTO
f620: 20 74 72 61 63 6b 20 56 41 4c 55 45 53 28 31 34   track VALUES(14
f630: 2c 20 27 4d 72 2e 20 42 6f 6a 61 6e 67 6c 65 73  , 'Mr. Bojangles
f640: 27 2c 20 33 29 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a  ', 3);.  }.} {}.
f650: 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 35  do_test e_fkey-5
f660: 30 2e 32 20 7b 0a 20 20 63 61 74 63 68 73 71 6c  0.2 {.  catchsql
f670: 20 7b 20 44 45 4c 45 54 45 20 46 52 4f 4d 20 61   { DELETE FROM a
f680: 72 74 69 73 74 20 57 48 45 52 45 20 61 72 74 69  rtist WHERE arti
f690: 73 74 6e 61 6d 65 20 3d 20 27 53 61 6d 6d 79 20  stname = 'Sammy 
f6a0: 44 61 76 69 73 20 4a 72 2e 27 20 7d 0a 7d 20 7b  Davis Jr.' }.} {
f6b0: 31 20 7b 66 6f 72 65 69 67 6e 20 6b 65 79 20 63  1 {foreign key c
f6c0: 6f 6e 73 74 72 61 69 6e 74 20 66 61 69 6c 65 64  onstraint failed
f6d0: 7d 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65  }}.do_test e_fke
f6e0: 79 2d 35 30 2e 33 20 7b 0a 20 20 65 78 65 63 73  y-50.3 {.  execs
f6f0: 71 6c 20 7b 0a 20 20 20 20 49 4e 53 45 52 54 20  ql {.    INSERT 
f700: 49 4e 54 4f 20 61 72 74 69 73 74 20 56 41 4c 55  INTO artist VALU
f710: 45 53 28 30 2c 20 27 55 6e 6b 6e 6f 77 6e 20 41  ES(0, 'Unknown A
f720: 72 74 69 73 74 27 29 3b 0a 20 20 20 20 44 45 4c  rtist');.    DEL
f730: 45 54 45 20 46 52 4f 4d 20 61 72 74 69 73 74 20  ETE FROM artist 
f740: 57 48 45 52 45 20 61 72 74 69 73 74 6e 61 6d 65  WHERE artistname
f750: 20 3d 20 27 53 61 6d 6d 79 20 44 61 76 69 73 20   = 'Sammy Davis 
f760: 4a 72 2e 27 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a 64  Jr.';.  }.} {}.d
f770: 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 35 30  o_test e_fkey-50
f780: 2e 34 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b  .4 {.  execsql {
f790: 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 61   SELECT * FROM a
f7a0: 72 74 69 73 74 20 7d 0a 7d 20 7b 30 20 7b 55 6e  rtist }.} {0 {Un
f7b0: 6b 6e 6f 77 6e 20 41 72 74 69 73 74 7d 7d 0a 64  known Artist}}.d
f7c0: 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 35 30  o_test e_fkey-50
f7d0: 2e 35 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b  .5 {.  execsql {
f7e0: 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74   SELECT * FROM t
f7f0: 72 61 63 6b 20 7d 0a 7d 20 7b 31 34 20 7b 4d 72  rack }.} {14 {Mr
f800: 2e 20 42 6f 6a 61 6e 67 6c 65 73 7d 20 30 7d 0a  . Bojangles} 0}.
f810: 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  .#--------------
f820: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
f830: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
f840: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
f850: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20 2f 2a  -----------.# /*
f860: 20 45 56 3a 20 52 2d 30 39 35 36 34 2d 32 32 31   EV: R-09564-221
f870: 37 30 20 2a 2f 0a 23 0a 23 20 43 68 65 63 6b 20  70 */.#.# Check 
f880: 74 68 61 74 20 74 68 65 20 6f 72 64 65 72 20 6f  that the order o
f890: 66 20 73 74 65 70 73 20 69 6e 20 61 6e 20 55 50  f steps in an UP
f8a0: 44 41 54 45 20 6f 72 20 44 45 4c 45 54 45 20 6f  DATE or DELETE o
f8b0: 6e 20 61 20 70 61 72 65 6e 74 20 0a 23 20 74 61  n a parent .# ta
f8c0: 62 6c 65 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77  ble is as follow
f8d0: 73 3a 0a 23 0a 23 20 20 20 31 2e 20 45 78 65 63  s:.#.#   1. Exec
f8e0: 75 74 65 20 61 70 70 6c 69 63 61 62 6c 65 20 42  ute applicable B
f8f0: 45 46 4f 52 45 20 74 72 69 67 67 65 72 20 70 72  EFORE trigger pr
f900: 6f 67 72 61 6d 73 2c 0a 23 20 20 20 32 2e 20 43  ograms,.#   2. C
f910: 68 65 63 6b 20 6c 6f 63 61 6c 20 28 6e 6f 6e 20  heck local (non 
f920: 66 6f 72 65 69 67 6e 20 6b 65 79 29 20 63 6f 6e  foreign key) con
f930: 73 74 72 61 69 6e 74 73 2c 0a 23 20 20 20 33 2e  straints,.#   3.
f940: 20 55 70 64 61 74 65 20 6f 72 20 64 65 6c 65 74   Update or delet
f950: 65 20 74 68 65 20 72 6f 77 20 69 6e 20 74 68 65  e the row in the
f960: 20 70 61 72 65 6e 74 20 74 61 62 6c 65 2c 0a 23   parent table,.#
f970: 20 20 20 34 2e 20 50 65 72 66 6f 72 6d 20 61 6e     4. Perform an
f980: 79 20 72 65 71 75 69 72 65 64 20 66 6f 72 65 69  y required forei
f990: 67 6e 20 6b 65 79 20 61 63 74 69 6f 6e 73 2c 0a  gn key actions,.
f9a0: 23 20 20 20 35 2e 20 45 78 65 63 75 74 65 20 61  #   5. Execute a
f9b0: 70 70 6c 69 63 61 62 6c 65 20 41 46 54 45 52 20  pplicable AFTER 
f9c0: 74 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d 73  trigger programs
f9d0: 2e 20 0a 23 0a 64 72 6f 70 5f 61 6c 6c 5f 74 61  . .#.drop_all_ta
f9e0: 62 6c 65 73 0a 64 6f 5f 74 65 73 74 20 65 5f 66  bles.do_test e_f
f9f0: 6b 65 79 2d 35 31 2e 31 20 7b 0a 20 20 70 72 6f  key-51.1 {.  pro
fa00: 63 20 6d 61 78 70 61 72 65 6e 74 20 7b 61 72 67  c maxparent {arg
fa10: 73 7d 20 7b 20 64 62 20 6f 6e 65 20 7b 53 45 4c  s} { db one {SEL
fa20: 45 43 54 20 6d 61 78 28 78 29 20 46 52 4f 4d 20  ECT max(x) FROM 
fa30: 70 61 72 65 6e 74 7d 20 7d 0a 20 20 64 62 20 66  parent} }.  db f
fa40: 75 6e 63 20 6d 61 78 70 61 72 65 6e 74 20 6d 61  unc maxparent ma
fa50: 78 70 61 72 65 6e 74 0a 0a 20 20 65 78 65 63 73  xparent..  execs
fa60: 71 6c 20 7b 0a 20 20 20 20 43 52 45 41 54 45 20  ql {.    CREATE 
fa70: 54 41 42 4c 45 20 70 61 72 65 6e 74 28 78 20 50  TABLE parent(x P
fa80: 52 49 4d 41 52 59 20 4b 45 59 29 3b 0a 0a 20 20  RIMARY KEY);..  
fa90: 20 20 43 52 45 41 54 45 20 54 52 49 47 47 45 52    CREATE TRIGGER
faa0: 20 62 75 20 42 45 46 4f 52 45 20 55 50 44 41 54   bu BEFORE UPDAT
fab0: 45 20 4f 4e 20 70 61 72 65 6e 74 20 42 45 47 49  E ON parent BEGI
fac0: 4e 0a 20 20 20 20 20 20 49 4e 53 45 52 54 20 49  N.      INSERT I
fad0: 4e 54 4f 20 70 61 72 65 6e 74 20 56 41 4c 55 45  NTO parent VALUE
fae0: 53 28 6e 65 77 2e 78 2d 6f 6c 64 2e 78 29 3b 0a  S(new.x-old.x);.
faf0: 20 20 20 20 45 4e 44 3b 0a 20 20 20 20 43 52 45      END;.    CRE
fb00: 41 54 45 20 54 41 42 4c 45 20 63 68 69 6c 64 28  ATE TABLE child(
fb10: 0a 20 20 20 20 20 20 61 20 44 45 46 41 55 4c 54  .      a DEFAULT
fb20: 20 28 6d 61 78 70 61 72 65 6e 74 28 29 29 20 52   (maxparent()) R
fb30: 45 46 45 52 45 4e 43 45 53 20 70 61 72 65 6e 74  EFERENCES parent
fb40: 20 4f 4e 20 55 50 44 41 54 45 20 53 45 54 20 44   ON UPDATE SET D
fb50: 45 46 41 55 4c 54 0a 20 20 20 20 29 3b 0a 20 20  EFAULT.    );.  
fb60: 20 20 43 52 45 41 54 45 20 54 52 49 47 47 45 52    CREATE TRIGGER
fb70: 20 61 75 20 41 46 54 45 52 20 55 50 44 41 54 45   au AFTER UPDATE
fb80: 20 4f 4e 20 70 61 72 65 6e 74 20 42 45 47 49 4e   ON parent BEGIN
fb90: 0a 20 20 20 20 20 20 49 4e 53 45 52 54 20 49 4e  .      INSERT IN
fba0: 54 4f 20 70 61 72 65 6e 74 20 56 41 4c 55 45 53  TO parent VALUES
fbb0: 28 6e 65 77 2e 78 2b 6f 6c 64 2e 78 29 3b 0a 20  (new.x+old.x);. 
fbc0: 20 20 20 45 4e 44 3b 0a 0a 20 20 20 20 49 4e 53     END;..    INS
fbd0: 45 52 54 20 49 4e 54 4f 20 70 61 72 65 6e 74 20  ERT INTO parent 
fbe0: 56 41 4c 55 45 53 28 31 29 3b 0a 20 20 20 20 49  VALUES(1);.    I
fbf0: 4e 53 45 52 54 20 49 4e 54 4f 20 63 68 69 6c 64  NSERT INTO child
fc00: 20 56 41 4c 55 45 53 28 31 29 3b 0a 20 20 7d 0a   VALUES(1);.  }.
fc10: 7d 20 7b 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66  } {}.do_test e_f
fc20: 6b 65 79 2d 35 31 2e 32 20 7b 0a 20 20 65 78 65  key-51.2 {.  exe
fc30: 63 73 71 6c 20 7b 0a 20 20 20 20 55 50 44 41 54  csql {.    UPDAT
fc40: 45 20 70 61 72 65 6e 74 20 53 45 54 20 78 20 3d  E parent SET x =
fc50: 20 32 32 3b 0a 20 20 20 20 53 45 4c 45 43 54 20   22;.    SELECT 
fc60: 2a 20 46 52 4f 4d 20 70 61 72 65 6e 74 20 55 4e  * FROM parent UN
fc70: 49 4f 4e 20 41 4c 4c 20 53 45 4c 45 43 54 20 27  ION ALL SELECT '
fc80: 78 78 78 27 20 55 4e 49 4f 4e 20 41 4c 4c 20 53  xxx' UNION ALL S
fc90: 45 4c 45 43 54 20 61 20 46 52 4f 4d 20 63 68 69  ELECT a FROM chi
fca0: 6c 64 3b 0a 20 20 7d 0a 7d 20 7b 32 32 20 32 31  ld;.  }.} {22 21
fcb0: 20 32 33 20 78 78 78 20 32 32 7d 0a 64 6f 5f 74   23 xxx 22}.do_t
fcc0: 65 73 74 20 65 5f 66 6b 65 79 2d 35 31 2e 33 20  est e_fkey-51.3 
fcd0: 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20  {.  execsql {.  
fce0: 20 20 44 45 4c 45 54 45 20 46 52 4f 4d 20 63 68    DELETE FROM ch
fcf0: 69 6c 64 3b 0a 20 20 20 20 44 45 4c 45 54 45 20  ild;.    DELETE 
fd00: 46 52 4f 4d 20 70 61 72 65 6e 74 3b 0a 20 20 20  FROM parent;.   
fd10: 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 70 61 72   INSERT INTO par
fd20: 65 6e 74 20 56 41 4c 55 45 53 28 2d 31 29 3b 0a  ent VALUES(-1);.
fd30: 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20      INSERT INTO 
fd40: 63 68 69 6c 64 20 56 41 4c 55 45 53 28 2d 31 29  child VALUES(-1)
fd50: 3b 0a 20 20 20 20 55 50 44 41 54 45 20 70 61 72  ;.    UPDATE par
fd60: 65 6e 74 20 53 45 54 20 78 20 3d 20 32 32 3b 0a  ent SET x = 22;.
fd70: 20 20 20 20 53 45 4c 45 43 54 20 2a 20 46 52 4f      SELECT * FRO
fd80: 4d 20 70 61 72 65 6e 74 20 55 4e 49 4f 4e 20 41  M parent UNION A
fd90: 4c 4c 20 53 45 4c 45 43 54 20 27 78 78 78 27 20  LL SELECT 'xxx' 
fda0: 55 4e 49 4f 4e 20 41 4c 4c 20 53 45 4c 45 43 54  UNION ALL SELECT
fdb0: 20 61 20 46 52 4f 4d 20 63 68 69 6c 64 3b 0a 20   a FROM child;. 
fdc0: 20 7d 0a 7d 20 7b 32 32 20 32 33 20 32 31 20 78   }.} {22 23 21 x
fdd0: 78 78 20 32 33 7d 0a 0a 0a 23 2d 2d 2d 2d 2d 2d  xx 23}...#------
fde0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
fdf0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
fe00: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
fe10: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
fe20: 2d 2d 2d 0a 23 20 2f 2a 20 45 56 3a 20 52 2d 32  ---.# /* EV: R-2
fe30: 37 33 38 33 2d 31 30 32 34 36 20 2a 2f 0a 23 0a  7383-10246 */.#.
fe40: 23 20 56 65 72 69 66 79 20 74 68 61 74 20 4f 4e  # Verify that ON
fe50: 20 55 50 44 41 54 45 20 61 63 74 69 6f 6e 73 20   UPDATE actions 
fe60: 6f 6e 6c 79 20 61 63 74 75 61 6c 6c 79 20 74 61  only actually ta
fe70: 6b 65 20 70 6c 61 63 65 20 69 66 20 74 68 65 20  ke place if the 
fe80: 70 61 72 65 6e 74 20 6b 65 79 0a 23 20 69 73 20  parent key.# is 
fe90: 73 65 74 20 74 6f 20 61 20 6e 65 77 20 76 61 6c  set to a new val
fea0: 75 65 20 74 68 61 74 20 69 73 20 64 69 73 74 69  ue that is disti
feb0: 6e 63 74 20 66 72 6f 6d 20 74 68 65 20 6f 6c 64  nct from the old
fec0: 20 76 61 6c 75 65 2e 20 54 68 65 20 64 65 66 61   value. The defa
fed0: 75 6c 74 0a 23 20 63 6f 6c 6c 61 74 69 6f 6e 20  ult.# collation 
fee0: 73 65 71 75 65 6e 63 65 20 61 6e 64 20 61 66 66  sequence and aff
fef0: 69 6e 69 74 79 20 61 72 65 20 75 73 65 64 20 74  inity are used t
ff00: 6f 20 64 65 74 65 72 6d 69 6e 65 20 69 66 20 74  o determine if t
ff10: 68 65 20 6e 65 77 20 76 61 6c 75 65 0a 23 20 69  he new value.# i
ff20: 73 20 27 64 69 73 74 69 6e 63 74 27 20 66 72 6f  s 'distinct' fro
ff30: 6d 20 74 68 65 20 6f 6c 64 20 6f 72 20 6e 6f 74  m the old or not
ff40: 2e 0a 23 0a 64 72 6f 70 5f 61 6c 6c 5f 74 61 62  ..#.drop_all_tab
ff50: 6c 65 73 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b  les.do_test e_fk
ff60: 65 79 2d 35 32 2e 31 20 7b 0a 20 20 65 78 65 63  ey-52.1 {.  exec
ff70: 73 71 6c 20 7b 0a 20 20 20 20 43 52 45 41 54 45  sql {.    CREATE
ff80: 20 54 41 42 4c 45 20 7a 65 75 73 28 61 20 49 4e   TABLE zeus(a IN
ff90: 54 45 47 45 52 20 43 4f 4c 4c 41 54 45 20 4e 4f  TEGER COLLATE NO
ffa0: 43 41 53 45 2c 20 62 2c 20 50 52 49 4d 41 52 59  CASE, b, PRIMARY
ffb0: 20 4b 45 59 28 61 2c 20 62 29 29 3b 0a 20 20 20   KEY(a, b));.   
ffc0: 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 61 70   CREATE TABLE ap
ffd0: 6f 6c 6c 6f 28 63 2c 20 64 2c 20 0a 20 20 20 20  ollo(c, d, .    
ffe0: 20 20 46 4f 52 45 49 47 4e 20 4b 45 59 28 63 2c    FOREIGN KEY(c,
fff0: 20 64 29 20 52 45 46 45 52 45 4e 43 45 53 20 7a   d) REFERENCES z
10000 65 75 73 20 4f 4e 20 55 50 44 41 54 45 20 43 41  eus ON UPDATE CA
10010 53 43 41 44 45 0a 20 20 20 20 29 3b 0a 20 20 20  SCADE.    );.   
10020 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 7a 65 75   INSERT INTO zeu
10030 73 20 56 41 4c 55 45 53 28 27 61 62 63 27 2c 20  s VALUES('abc', 
10040 27 78 79 7a 27 29 3b 0a 20 20 20 20 49 4e 53 45  'xyz');.    INSE
10050 52 54 20 49 4e 54 4f 20 61 70 6f 6c 6c 6f 20 56  RT INTO apollo V
10060 41 4c 55 45 53 28 27 41 42 43 27 2c 20 27 78 79  ALUES('ABC', 'xy
10070 7a 27 29 3b 0a 20 20 7d 0a 20 20 65 78 65 63 73  z');.  }.  execs
10080 71 6c 20 7b 0a 20 20 20 20 55 50 44 41 54 45 20  ql {.    UPDATE 
10090 7a 65 75 73 20 53 45 54 20 61 20 3d 20 27 61 42  zeus SET a = 'aB
100a0 63 27 3b 0a 20 20 20 20 53 45 4c 45 43 54 20 2a  c';.    SELECT *
100b0 20 46 52 4f 4d 20 61 70 6f 6c 6c 6f 3b 0a 20 20   FROM apollo;.  
100c0 7d 0a 7d 20 7b 41 42 43 20 78 79 7a 7d 0a 64 6f  }.} {ABC xyz}.do
100d0 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 35 32 2e  _test e_fkey-52.
100e0 32 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a  2 {.  execsql {.
100f0 20 20 20 20 55 50 44 41 54 45 20 7a 65 75 73 20      UPDATE zeus 
10100 53 45 54 20 61 20 3d 20 31 2c 20 62 20 3d 20 31  SET a = 1, b = 1
10110 3b 0a 20 20 20 20 53 45 4c 45 43 54 20 2a 20 46  ;.    SELECT * F
10120 52 4f 4d 20 61 70 6f 6c 6c 6f 3b 0a 20 20 7d 0a  ROM apollo;.  }.
10130 7d 20 7b 31 20 31 7d 0a 64 6f 5f 74 65 73 74 20  } {1 1}.do_test 
10140 65 5f 66 6b 65 79 2d 35 32 2e 33 20 7b 0a 20 20  e_fkey-52.3 {.  
10150 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 55 50  execsql {.    UP
10160 44 41 54 45 20 7a 65 75 73 20 53 45 54 20 61 20  DATE zeus SET a 
10170 3d 20 31 2c 20 62 20 3d 20 31 3b 0a 20 20 20 20  = 1, b = 1;.    
10180 53 45 4c 45 43 54 20 74 79 70 65 6f 66 28 63 29  SELECT typeof(c)
10190 2c 20 63 2c 20 74 79 70 65 6f 66 28 64 29 2c 20  , c, typeof(d), 
101a0 64 20 46 52 4f 4d 20 61 70 6f 6c 6c 6f 3b 0a 20  d FROM apollo;. 
101b0 20 7d 0a 7d 20 7b 69 6e 74 65 67 65 72 20 31 20   }.} {integer 1 
101c0 69 6e 74 65 67 65 72 20 31 7d 0a 64 6f 5f 74 65  integer 1}.do_te
101d0 73 74 20 65 5f 66 6b 65 79 2d 35 32 2e 34 20 7b  st e_fkey-52.4 {
101e0 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20  .  execsql {.   
101f0 20 55 50 44 41 54 45 20 7a 65 75 73 20 53 45 54   UPDATE zeus SET
10200 20 61 20 3d 20 27 31 27 3b 0a 20 20 20 20 53 45   a = '1';.    SE
10210 4c 45 43 54 20 74 79 70 65 6f 66 28 63 29 2c 20  LECT typeof(c), 
10220 63 2c 20 74 79 70 65 6f 66 28 64 29 2c 20 64 20  c, typeof(d), d 
10230 46 52 4f 4d 20 61 70 6f 6c 6c 6f 3b 0a 20 20 7d  FROM apollo;.  }
10240 0a 7d 20 7b 69 6e 74 65 67 65 72 20 31 20 69 6e  .} {integer 1 in
10250 74 65 67 65 72 20 31 7d 0a 64 6f 5f 74 65 73 74  teger 1}.do_test
10260 20 65 5f 66 6b 65 79 2d 35 32 2e 35 20 7b 0a 20   e_fkey-52.5 {. 
10270 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 55   execsql {.    U
10280 50 44 41 54 45 20 7a 65 75 73 20 53 45 54 20 62  PDATE zeus SET b
10290 20 3d 20 27 31 27 3b 0a 20 20 20 20 53 45 4c 45   = '1';.    SELE
102a0 43 54 20 74 79 70 65 6f 66 28 63 29 2c 20 63 2c  CT typeof(c), c,
102b0 20 74 79 70 65 6f 66 28 64 29 2c 20 64 20 46 52   typeof(d), d FR
102c0 4f 4d 20 61 70 6f 6c 6c 6f 3b 0a 20 20 7d 0a 7d  OM apollo;.  }.}
102d0 20 7b 69 6e 74 65 67 65 72 20 31 20 74 65 78 74   {integer 1 text
102e0 20 31 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b   1}.do_test e_fk
102f0 65 79 2d 35 32 2e 36 20 7b 0a 20 20 65 78 65 63  ey-52.6 {.  exec
10300 73 71 6c 20 7b 0a 20 20 20 20 55 50 44 41 54 45  sql {.    UPDATE
10310 20 7a 65 75 73 20 53 45 54 20 62 20 3d 20 4e 55   zeus SET b = NU
10320 4c 4c 3b 0a 20 20 20 20 53 45 4c 45 43 54 20 74  LL;.    SELECT t
10330 79 70 65 6f 66 28 63 29 2c 20 63 2c 20 74 79 70  ypeof(c), c, typ
10340 65 6f 66 28 64 29 2c 20 64 20 46 52 4f 4d 20 61  eof(d), d FROM a
10350 70 6f 6c 6c 6f 3b 0a 20 20 7d 0a 7d 20 7b 69 6e  pollo;.  }.} {in
10360 74 65 67 65 72 20 31 20 6e 75 6c 6c 20 7b 7d 7d  teger 1 null {}}
10370 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ..#-------------
10380 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
10390 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
103a0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
103b0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20 2f  ------------.# /
103c0 2a 20 45 56 3a 20 52 2d 35 38 35 38 39 2d 35 30  * EV: R-58589-50
103d0 37 38 31 20 2a 2f 0a 23 0a 23 20 54 65 73 74 20  781 */.#.# Test 
103e0 61 6e 20 65 78 61 6d 70 6c 65 20 66 72 6f 6d 20  an example from 
103f0 74 68 65 20 22 4f 4e 20 44 45 4c 45 54 45 20 61  the "ON DELETE a
10400 6e 64 20 4f 4e 20 55 50 44 41 54 45 20 41 63 74  nd ON UPDATE Act
10410 69 6f 6e 73 22 20 73 65 63 74 69 6f 6e 20 0a 23  ions" section .#
10420 20 6f 66 20 66 6f 72 65 69 67 6e 6b 65 79 73 2e   of foreignkeys.
10430 68 74 6d 6c 2e 20 54 68 69 73 20 65 78 61 6d 70  html. This examp
10440 6c 65 20 64 65 6d 6f 6e 73 74 72 61 74 65 73 20  le demonstrates 
10450 74 68 61 74 20 4f 4e 20 55 50 44 41 54 45 20 61  that ON UPDATE a
10460 63 74 69 6f 6e 73 0a 23 20 6f 6e 6c 79 20 74 61  ctions.# only ta
10470 6b 65 20 70 6c 61 63 65 20 69 66 20 61 74 20 6c  ke place if at l
10480 65 61 73 74 20 6f 6e 65 20 70 61 72 65 6e 74 20  east one parent 
10490 6b 65 79 20 63 6f 6c 75 6d 6e 20 69 73 20 73 65  key column is se
104a0 74 20 74 6f 20 61 20 76 61 6c 75 65 20 0a 23 20  t to a value .# 
104b0 74 68 61 74 20 69 73 20 64 69 73 74 69 6e 63 74  that is distinct
104c0 20 66 72 6f 6d 20 69 74 73 20 70 72 65 76 69 6f   from its previo
104d0 75 73 20 76 61 6c 75 65 2e 0a 23 0a 64 72 6f 70  us value..#.drop
104e0 5f 61 6c 6c 5f 74 61 62 6c 65 73 0a 64 6f 5f 74  _all_tables.do_t
104f0 65 73 74 20 65 5f 66 6b 65 79 2d 35 33 2e 31 20  est e_fkey-53.1 
10500 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20  {.  execsql {.  
10510 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 70    CREATE TABLE p
10520 61 72 65 6e 74 28 78 20 50 52 49 4d 41 52 59 20  arent(x PRIMARY 
10530 4b 45 59 29 3b 0a 20 20 20 20 43 52 45 41 54 45  KEY);.    CREATE
10540 20 54 41 42 4c 45 20 63 68 69 6c 64 28 79 20 52   TABLE child(y R
10550 45 46 45 52 45 4e 43 45 53 20 70 61 72 65 6e 74  EFERENCES parent
10560 20 4f 4e 20 55 50 44 41 54 45 20 53 45 54 20 4e   ON UPDATE SET N
10570 55 4c 4c 29 3b 0a 20 20 20 20 49 4e 53 45 52 54  ULL);.    INSERT
10580 20 49 4e 54 4f 20 70 61 72 65 6e 74 20 56 41 4c   INTO parent VAL
10590 55 45 53 28 27 6b 65 79 27 29 3b 0a 20 20 20 20  UES('key');.    
105a0 49 4e 53 45 52 54 20 49 4e 54 4f 20 63 68 69 6c  INSERT INTO chil
105b0 64 20 56 41 4c 55 45 53 28 27 6b 65 79 27 29 3b  d VALUES('key');
105c0 0a 20 20 7d 0a 7d 20 7b 7d 0a 64 6f 5f 74 65 73  .  }.} {}.do_tes
105d0 74 20 65 5f 66 6b 65 79 2d 35 33 2e 32 20 7b 0a  t e_fkey-53.2 {.
105e0 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20    execsql {.    
105f0 55 50 44 41 54 45 20 70 61 72 65 6e 74 20 53 45  UPDATE parent SE
10600 54 20 78 20 3d 20 27 6b 65 79 27 3b 0a 20 20 20  T x = 'key';.   
10610 20 53 45 4c 45 43 54 20 49 46 4e 55 4c 4c 28 79   SELECT IFNULL(y
10620 2c 20 27 6e 75 6c 6c 27 29 20 46 52 4f 4d 20 63  , 'null') FROM c
10630 68 69 6c 64 3b 0a 20 20 7d 0a 7d 20 7b 6b 65 79  hild;.  }.} {key
10640 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79  }.do_test e_fkey
10650 2d 35 33 2e 33 20 7b 0a 20 20 65 78 65 63 73 71  -53.3 {.  execsq
10660 6c 20 7b 0a 20 20 20 20 55 50 44 41 54 45 20 70  l {.    UPDATE p
10670 61 72 65 6e 74 20 53 45 54 20 78 20 3d 20 27 6b  arent SET x = 'k
10680 65 79 32 27 3b 0a 20 20 20 20 53 45 4c 45 43 54  ey2';.    SELECT
10690 20 49 46 4e 55 4c 4c 28 79 2c 20 27 6e 75 6c 6c   IFNULL(y, 'null
106a0 27 29 20 46 52 4f 4d 20 63 68 69 6c 64 3b 0a 20  ') FROM child;. 
106b0 20 7d 0a 7d 20 7b 6e 75 6c 6c 7d 0a 0a 23 23 23   }.} {null}..###
106c0 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
106d0 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
106e0 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
106f0 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
10700 23 23 23 23 23 23 23 23 0a 23 23 23 20 53 45 43  ########.### SEC
10710 54 49 4f 4e 20 35 3a 20 43 52 45 41 54 45 2c 20  TION 5: CREATE, 
10720 41 4c 54 45 52 20 61 6e 64 20 44 52 4f 50 20 54  ALTER and DROP T
10730 41 42 4c 45 20 63 6f 6d 6d 61 6e 64 73 0a 23 23  ABLE commands.##
10740 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
10750 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
10760 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
10770 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
10780 23 23 23 23 23 23 23 23 23 0a 0a 23 2d 2d 2d 2d  #########..#----
10790 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
107a0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
107b0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
107c0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
107d0 2d 2d 2d 2d 2d 0a 23 20 2f 2a 20 45 56 3a 20 52  -----.# /* EV: R
107e0 2d 33 36 30 31 38 2d 32 31 37 35 35 20 2a 2f 0a  -36018-21755 */.
107f0 23 20 2f 2a 20 45 56 3a 20 52 2d 32 35 33 38 34  # /* EV: R-25384
10800 2d 33 39 33 33 37 20 2a 2f 0a 23 20 0a 23 20 54  -39337 */.# .# T
10810 65 73 74 20 74 68 61 74 20 70 61 72 65 6e 74 20  est that parent 
10820 6b 65 79 73 20 61 72 65 20 6e 6f 74 20 63 68 65  keys are not che
10830 63 6b 65 64 20 77 68 65 6e 20 74 61 62 6c 65 73  cked when tables
10840 20 61 72 65 20 63 72 65 61 74 65 64 2e 0a 23 0a   are created..#.
10850 23 20 43 68 69 6c 64 20 6b 65 79 73 20 61 72 65  # Child keys are
10860 20 63 68 65 63 6b 65 64 20 74 6f 20 65 6e 73 75   checked to ensu
10870 72 65 20 61 6c 6c 20 63 6f 6d 70 6f 6e 65 6e 74  re all component
10880 20 63 6f 6c 75 6d 6e 73 20 65 78 69 73 74 2e 20   columns exist. 
10890 49 66 20 70 61 72 65 6e 74 0a 23 20 6b 65 79 20  If parent.# key 
108a0 63 6f 6c 75 6d 6e 73 20 61 72 65 20 65 78 70 6c  columns are expl
108b0 69 63 69 74 6c 79 20 73 70 65 63 69 66 69 65 64  icitly specified
108c0 2c 20 53 51 4c 69 74 65 20 63 68 65 63 6b 73 20  , SQLite checks 
108d0 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 65  to make sure the
108e0 72 65 0a 23 20 61 72 65 20 74 68 65 20 73 61 6d  re.# are the sam
108f0 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75  e number of colu
10900 6d 6e 73 20 69 6e 20 74 68 65 20 63 68 69 6c 64  mns in the child
10910 20 61 6e 64 20 70 61 72 65 6e 74 20 6b 65 79 73   and parent keys
10920 2e 20 28 54 4f 44 4f 3a 20 54 68 69 73 0a 23 20  . (TODO: This.# 
10930 69 73 20 74 65 73 74 65 64 20 62 75 74 20 64 6f  is tested but do
10940 65 73 20 6e 6f 74 20 63 6f 72 72 65 73 70 6f 6e  es not correspon
10950 64 20 74 6f 20 61 6e 79 20 74 65 73 74 61 62 6c  d to any testabl
10960 65 20 73 74 61 74 65 6d 65 6e 74 2e 29 0a 23 0a  e statement.).#.
10970 23 20 2f 2a 20 45 56 3a 20 52 2d 30 38 39 30 38  # /* EV: R-08908
10980 2d 32 33 34 33 39 20 2a 2f 0a 23 0a 23 20 41 6c  -23439 */.#.# Al
10990 73 6f 20 74 65 73 74 20 74 68 61 74 20 74 68 65  so test that the
109a0 20 61 62 6f 76 65 20 73 74 61 74 65 6d 65 6e 74   above statement
109b0 73 20 61 72 65 20 74 72 75 65 20 72 65 67 61 72  s are true regar
109c0 64 6c 65 73 73 20 6f 66 20 77 68 65 74 68 65 72  dless of whether
109d0 20 6f 72 20 6e 6f 74 0a 23 20 66 6f 72 65 69 67   or not.# foreig
109e0 6e 20 6b 65 79 73 20 61 72 65 20 65 6e 61 62 6c  n keys are enabl
109f0 65 64 3a 20 20 22 41 20 43 52 45 41 54 45 20 54  ed:  "A CREATE T
10a00 41 42 4c 45 20 63 6f 6d 6d 61 6e 64 20 6f 70 65  ABLE command ope
10a10 72 61 74 65 73 20 74 68 65 20 73 61 6d 65 20 77  rates the same w
10a20 68 65 74 68 65 72 0a 23 20 6f 72 20 6e 6f 74 20  hether.# or not 
10a30 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73  foreign key cons
10a40 74 72 61 69 6e 74 73 20 61 72 65 20 65 6e 61 62  traints are enab
10a50 6c 65 64 2e 22 0a 23 20 0a 66 6f 72 65 61 63 68  led.".# .foreach
10a60 20 7b 74 6e 20 7a 43 72 65 61 74 65 54 62 6c 20   {tn zCreateTbl 
10a70 6c 52 65 73 7d 20 7b 0a 20 20 31 20 22 43 52 45  lRes} {.  1 "CRE
10a80 41 54 45 20 54 41 42 4c 45 20 74 31 28 61 2c 20  ATE TABLE t1(a, 
10a90 62 20 52 45 46 45 52 45 4e 43 45 53 20 74 31 29  b REFERENCES t1)
10aa0 22 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  "               
10ab0 20 20 20 20 20 20 20 20 20 20 20 20 20 7b 30 20               {0 
10ac0 7b 7d 7d 0a 20 20 32 20 22 43 52 45 41 54 45 20  {}}.  2 "CREATE 
10ad0 54 41 42 4c 45 20 74 31 28 61 2c 20 62 20 52 45  TABLE t1(a, b RE
10ae0 46 45 52 45 4e 43 45 53 20 74 32 29 22 20 20 20  FERENCES t2)"   
10af0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10b00 20 20 20 20 20 20 20 20 20 7b 30 20 7b 7d 7d 0a           {0 {}}.
10b10 20 20 33 20 22 43 52 45 41 54 45 20 54 41 42 4c    3 "CREATE TABL
10b20 45 20 74 31 28 61 2c 20 62 2c 20 46 4f 52 45 49  E t1(a, b, FOREI
10b30 47 4e 20 4b 45 59 28 61 2c 62 29 20 52 45 46 45  GN KEY(a,b) REFE
10b40 52 45 4e 43 45 53 20 74 31 29 22 20 20 20 20 20  RENCES t1)"     
10b50 20 20 20 20 20 7b 30 20 7b 7d 7d 0a 20 20 34 20       {0 {}}.  4 
10b60 22 43 52 45 41 54 45 20 54 41 42 4c 45 20 74 31  "CREATE TABLE t1
10b70 28 61 2c 20 62 2c 20 46 4f 52 45 49 47 4e 20 4b  (a, b, FOREIGN K
10b80 45 59 28 61 2c 62 29 20 52 45 46 45 52 45 4e 43  EY(a,b) REFERENC
10b90 45 53 20 74 32 29 22 20 20 20 20 20 20 20 20 20  ES t2)"         
10ba0 20 7b 30 20 7b 7d 7d 0a 20 20 35 20 22 43 52 45   {0 {}}.  5 "CRE
10bb0 41 54 45 20 54 41 42 4c 45 20 74 31 28 61 2c 20  ATE TABLE t1(a, 
10bc0 62 2c 20 46 4f 52 45 49 47 4e 20 4b 45 59 28 61  b, FOREIGN KEY(a
10bd0 2c 62 29 20 52 45 46 45 52 45 4e 43 45 53 20 74  ,b) REFERENCES t
10be0 32 29 22 20 20 20 20 20 20 20 20 20 20 7b 30 20  2)"          {0 
10bf0 7b 7d 7d 0a 20 20 36 20 22 43 52 45 41 54 45 20  {}}.  6 "CREATE 
10c00 54 41 42 4c 45 20 74 31 28 61 2c 20 62 2c 20 46  TABLE t1(a, b, F
10c10 4f 52 45 49 47 4e 20 4b 45 59 28 61 2c 62 29 20  OREIGN KEY(a,b) 
10c20 52 45 46 45 52 45 4e 43 45 53 20 74 32 28 6e 2c  REFERENCES t2(n,
10c30 64 29 29 22 20 20 20 20 20 7b 30 20 7b 7d 7d 0a  d))"     {0 {}}.
10c40 20 20 37 20 22 43 52 45 41 54 45 20 54 41 42 4c    7 "CREATE TABL
10c50 45 20 74 31 28 61 2c 20 62 2c 20 46 4f 52 45 49  E t1(a, b, FOREI
10c60 47 4e 20 4b 45 59 28 61 2c 62 29 20 52 45 46 45  GN KEY(a,b) REFE
10c70 52 45 4e 43 45 53 20 74 31 28 61 2c 62 29 29 22  RENCES t1(a,b))"
10c80 20 20 20 20 20 7b 30 20 7b 7d 7d 0a 0a 20 20 41       {0 {}}..  A
10c90 20 22 43 52 45 41 54 45 20 54 41 42 4c 45 20 74   "CREATE TABLE t
10ca0 31 28 61 2c 20 62 2c 20 46 4f 52 45 49 47 4e 20  1(a, b, FOREIGN 
10cb0 4b 45 59 28 63 2c 62 29 20 52 45 46 45 52 45 4e  KEY(c,b) REFEREN
10cc0 43 45 53 20 74 32 29 22 20 20 20 20 20 20 20 20  CES t2)"        
10cd0 20 20 0a 20 20 20 20 20 7b 31 20 7b 75 6e 6b 6e    .     {1 {unkn
10ce0 6f 77 6e 20 63 6f 6c 75 6d 6e 20 22 63 22 20 69  own column "c" i
10cf0 6e 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 64 65  n foreign key de
10d00 66 69 6e 69 74 69 6f 6e 7d 7d 0a 20 20 42 20 22  finition}}.  B "
10d10 43 52 45 41 54 45 20 54 41 42 4c 45 20 74 31 28  CREATE TABLE t1(
10d20 61 2c 20 62 2c 20 46 4f 52 45 49 47 4e 20 4b 45  a, b, FOREIGN KE
10d30 59 28 63 2c 62 29 20 52 45 46 45 52 45 4e 43 45  Y(c,b) REFERENCE
10d40 53 20 74 32 28 64 29 29 22 20 20 20 20 20 20 20  S t2(d))"       
10d50 20 20 20 0a 20 20 20 20 20 7b 31 20 7b 6e 75 6d     .     {1 {num
10d60 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69  ber of columns i
10d70 6e 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 64 6f  n foreign key do
10d80 65 73 20 6e 6f 74 20 6d 61 74 63 68 20 74 68 65  es not match the
10d90 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d   number of colum
10da0 6e 73 20 69 6e 20 74 68 65 20 72 65 66 65 72 65  ns in the refere
10db0 6e 63 65 64 20 74 61 62 6c 65 7d 7d 0a 7d 20 7b  nced table}}.} {
10dc0 0a 20 20 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65  .  do_test e_fke
10dd0 79 2d 35 34 2e 24 74 6e 2e 6f 66 66 20 7b 0a 20  y-54.$tn.off {. 
10de0 20 20 20 64 72 6f 70 5f 61 6c 6c 5f 74 61 62 6c     drop_all_tabl
10df0 65 73 0a 20 20 20 20 65 78 65 63 73 71 6c 20 7b  es.    execsql {
10e00 50 52 41 47 4d 41 20 66 6f 72 65 69 67 6e 5f 6b  PRAGMA foreign_k
10e10 65 79 73 20 3d 20 4f 46 46 7d 0a 20 20 20 20 63  eys = OFF}.    c
10e20 61 74 63 68 73 71 6c 20 24 7a 43 72 65 61 74 65  atchsql $zCreate
10e30 54 62 6c 0a 20 20 7d 20 24 6c 52 65 73 0a 20 20  Tbl.  } $lRes.  
10e40 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 35  do_test e_fkey-5
10e50 34 2e 24 74 6e 2e 6f 6e 20 7b 0a 20 20 20 20 64  4.$tn.on {.    d
10e60 72 6f 70 5f 61 6c 6c 5f 74 61 62 6c 65 73 0a 20  rop_all_tables. 
10e70 20 20 20 65 78 65 63 73 71 6c 20 7b 50 52 41 47     execsql {PRAG
10e80 4d 41 20 66 6f 72 65 69 67 6e 5f 6b 65 79 73 20  MA foreign_keys 
10e90 3d 20 4f 4e 7d 0a 20 20 20 20 63 61 74 63 68 73  = ON}.    catchs
10ea0 71 6c 20 24 7a 43 72 65 61 74 65 54 62 6c 0a 20  ql $zCreateTbl. 
10eb0 20 7d 20 24 6c 52 65 73 0a 7d 0a 0a 23 2d 2d 2d   } $lRes.}..#---
10ec0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
10ed0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
10ee0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
10ef0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
10f00 2d 2d 2d 2d 2d 2d 0a 23 20 2f 2a 20 45 56 3a 20  ------.# /* EV: 
10f10 52 2d 34 37 39 35 32 2d 36 32 34 39 38 20 2a 2f  R-47952-62498 */
10f20 0a 23 0a 70 72 6f 63 20 74 65 73 74 5f 65 66 6b  .#.proc test_efk
10f30 65 79 5f 36 20 7b 74 6e 20 7a 41 6c 74 65 72 20  ey_6 {tn zAlter 
10f40 69 73 45 72 72 6f 72 7d 20 7b 0a 20 20 64 72 6f  isError} {.  dro
10f50 70 5f 61 6c 6c 5f 74 61 62 6c 65 73 20 0a 0a 20  p_all_tables .. 
10f60 20 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d   do_test e_fkey-
10f70 35 36 2e 24 74 6e 2e 31 20 22 0a 20 20 20 20 65  56.$tn.1 ".    e
10f80 78 65 63 73 71 6c 20 7b 20 43 52 45 41 54 45 20  xecsql { CREATE 
10f90 54 41 42 4c 45 20 74 62 6c 28 61 2c 20 62 29 20  TABLE tbl(a, b) 
10fa0 7d 0a 20 20 20 20 5b 6c 69 73 74 20 63 61 74 63  }.    [list catc
10fb0 68 73 71 6c 20 24 7a 41 6c 74 65 72 5d 0a 20 20  hsql $zAlter].  
10fc0 22 20 5b 6c 69 6e 64 65 78 20 7b 7b 30 20 7b 7d  " [lindex {{0 {}
10fd0 7d 20 7b 31 20 7b 43 61 6e 6e 6f 74 20 61 64 64  } {1 {Cannot add
10fe0 20 61 20 52 45 46 45 52 45 4e 43 45 53 20 63 6f   a REFERENCES co
10ff0 6c 75 6d 6e 20 77 69 74 68 20 6e 6f 6e 2d 4e 55  lumn with non-NU
11000 4c 4c 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65  LL default value
11010 7d 7d 7d 20 24 69 73 45 72 72 6f 72 5d 0a 0a 7d  }}} $isError]..}
11020 0a 0a 74 65 73 74 5f 65 66 6b 65 79 5f 36 20 31  ..test_efkey_6 1
11030 20 22 41 4c 54 45 52 20 54 41 42 4c 45 20 74 62   "ALTER TABLE tb
11040 6c 20 41 44 44 20 43 4f 4c 55 4d 4e 20 63 20 52  l ADD COLUMN c R
11050 45 46 45 52 45 4e 43 45 53 20 78 78 22 20 30 0a  EFERENCES xx" 0.
11060 74 65 73 74 5f 65 66 6b 65 79 5f 36 20 32 20 22  test_efkey_6 2 "
11070 41 4c 54 45 52 20 54 41 42 4c 45 20 74 62 6c 20  ALTER TABLE tbl 
11080 41 44 44 20 43 4f 4c 55 4d 4e 20 63 20 44 45 46  ADD COLUMN c DEF
11090 41 55 4c 54 20 4e 55 4c 4c 20 52 45 46 45 52 45  AULT NULL REFERE
110a0 4e 43 45 53 20 78 78 22 20 30 0a 74 65 73 74 5f  NCES xx" 0.test_
110b0 65 66 6b 65 79 5f 36 20 33 20 22 41 4c 54 45 52  efkey_6 3 "ALTER
110c0 20 54 41 42 4c 45 20 74 62 6c 20 41 44 44 20 43   TABLE tbl ADD C
110d0 4f 4c 55 4d 4e 20 63 20 44 45 46 41 55 4c 54 20  OLUMN c DEFAULT 
110e0 30 20 52 45 46 45 52 45 4e 43 45 53 20 78 78 22  0 REFERENCES xx"
110f0 20 31 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d   1..#-----------
11100 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
11110 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
11120 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
11130 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23  --------------.#
11140 20 2f 2a 20 45 56 3a 20 52 2d 34 37 30 38 30 2d   /* EV: R-47080-
11150 30 32 30 36 39 20 2a 2f 0a 23 0a 23 20 54 65 73  02069 */.#.# Tes
11160 74 20 74 68 61 74 20 41 4c 54 45 52 20 54 41 42  t that ALTER TAB
11170 4c 45 20 61 64 6a 75 73 74 73 20 52 45 46 45 52  LE adjusts REFER
11180 45 4e 43 45 53 20 63 6c 61 75 73 65 73 20 77 68  ENCES clauses wh
11190 65 6e 20 74 68 65 20 70 61 72 65 6e 74 20 74 61  en the parent ta
111a0 62 6c 65 0a 23 20 69 73 20 52 45 4e 41 4d 45 44  ble.# is RENAMED
111b0 2e 0a 23 0a 23 20 2f 2a 20 45 56 3a 20 52 2d 36  ..#.# /* EV: R-6
111c0 33 38 32 37 2d 35 34 37 37 34 20 2a 2f 0a 23 0a  3827-54774 */.#.
111d0 23 20 54 65 73 74 20 74 68 61 74 20 74 68 65 73  # Test that thes
111e0 65 20 61 64 6a 75 73 74 6d 65 6e 74 73 20 61 72  e adjustments ar
111f0 65 20 76 69 73 69 62 6c 65 20 69 6e 20 74 68 65  e visible in the
11200 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74   sqlite_master t
11210 61 62 6c 65 2e 0a 23 0a 64 6f 5f 74 65 73 74 20  able..#.do_test 
11220 65 5f 66 6b 65 79 2d 35 36 2e 31 20 7b 0a 20 20  e_fkey-56.1 {.  
11230 64 72 6f 70 5f 61 6c 6c 5f 74 61 62 6c 65 73 0a  drop_all_tables.
11240 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20    execsql {.    
11250 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 70 20  CREATE TABLE 'p 
11260 31 20 22 70 61 72 65 6e 74 20 6f 6e 65 22 27 28  1 "parent one"'(
11270 61 20 52 45 46 45 52 45 4e 43 45 53 20 27 70 20  a REFERENCES 'p 
11280 31 20 22 70 61 72 65 6e 74 20 6f 6e 65 22 27 2c  1 "parent one"',
11290 20 62 2c 20 50 52 49 4d 41 52 59 20 4b 45 59 28   b, PRIMARY KEY(
112a0 62 29 29 3b 0a 0a 20 20 20 20 43 52 45 41 54 45  b));..    CREATE
112b0 20 54 41 42 4c 45 20 63 31 28 63 2c 20 64 20 52   TABLE c1(c, d R
112c0 45 46 45 52 45 4e 43 45 53 20 27 70 20 31 20 22  EFERENCES 'p 1 "
112d0 70 61 72 65 6e 74 20 6f 6e 65 22 27 20 4f 4e 20  parent one"' ON 
112e0 55 50 44 41 54 45 20 43 41 53 43 41 44 45 29 3b  UPDATE CASCADE);
112f0 0a 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c  .    CREATE TABL
11300 45 20 63 32 28 65 2c 20 66 2c 20 46 4f 52 45 49  E c2(e, f, FOREI
11310 47 4e 20 4b 45 59 28 66 29 20 52 45 46 45 52 45  GN KEY(f) REFERE
11320 4e 43 45 53 20 27 70 20 31 20 22 70 61 72 65 6e  NCES 'p 1 "paren
11330 74 20 6f 6e 65 22 27 20 4f 4e 20 55 50 44 41 54  t one"' ON UPDAT
11340 45 20 43 41 53 43 41 44 45 29 3b 0a 20 20 20 20  E CASCADE);.    
11350 43 52 45 41 54 45 20 54 41 42 4c 45 20 63 33 28  CREATE TABLE c3(
11360 65 2c 20 27 66 20 63 6f 6c 20 32 27 2c 20 46 4f  e, 'f col 2', FO
11370 52 45 49 47 4e 20 4b 45 59 28 27 66 20 63 6f 6c  REIGN KEY('f col
11380 20 32 27 29 20 52 45 46 45 52 45 4e 43 45 53 20   2') REFERENCES 
11390 27 70 20 31 20 22 70 61 72 65 6e 74 20 6f 6e 65  'p 1 "parent one
113a0 22 27 20 4f 4e 20 55 50 44 41 54 45 20 43 41 53  "' ON UPDATE CAS
113b0 43 41 44 45 29 3b 0a 0a 20 20 20 20 49 4e 53 45  CADE);..    INSE
113c0 52 54 20 49 4e 54 4f 20 27 70 20 31 20 22 70 61  RT INTO 'p 1 "pa
113d0 72 65 6e 74 20 6f 6e 65 22 27 20 56 41 4c 55 45  rent one"' VALUE
113e0 53 28 31 2c 20 31 29 3b 0a 20 20 20 20 49 4e 53  S(1, 1);.    INS
113f0 45 52 54 20 49 4e 54 4f 20 63 31 20 56 41 4c 55  ERT INTO c1 VALU
11400 45 53 28 31 2c 20 31 29 3b 0a 20 20 20 20 49 4e  ES(1, 1);.    IN
11410 53 45 52 54 20 49 4e 54 4f 20 63 32 20 56 41 4c  SERT INTO c2 VAL
11420 55 45 53 28 31 2c 20 31 29 3b 0a 20 20 20 20 49  UES(1, 1);.    I
11430 4e 53 45 52 54 20 49 4e 54 4f 20 63 33 20 56 41  NSERT INTO c3 VA
11440 4c 55 45 53 28 31 2c 20 31 29 3b 0a 0a 20 20 20  LUES(1, 1);..   
11450 20 2d 2d 20 43 52 45 41 54 45 20 54 41 42 4c 45   -- CREATE TABLE
11460 20 71 28 61 2c 20 62 2c 20 50 52 49 4d 41 52 59   q(a, b, PRIMARY
11470 20 4b 45 59 28 62 29 29 3b 0a 20 20 7d 0a 7d 20   KEY(b));.  }.} 
11480 7b 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65  {}.do_test e_fke
11490 79 2d 35 36 2e 32 20 7b 0a 20 20 65 78 65 63 73  y-56.2 {.  execs
114a0 71 6c 20 7b 20 41 4c 54 45 52 20 54 41 42 4c 45  ql { ALTER TABLE
114b0 20 27 70 20 31 20 22 70 61 72 65 6e 74 20 6f 6e   'p 1 "parent on
114c0 65 22 27 20 52 45 4e 41 4d 45 20 54 4f 20 70 20  e"' RENAME TO p 
114d0 7d 0a 7d 20 7b 7d 0a 64 6f 5f 74 65 73 74 20 65  }.} {}.do_test e
114e0 5f 66 6b 65 79 2d 35 36 2e 33 20 7b 0a 20 20 65  _fkey-56.3 {.  e
114f0 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 55 50 44  xecsql {.    UPD
11500 41 54 45 20 70 20 53 45 54 20 61 20 3d 20 27 78  ATE p SET a = 'x
11510 78 78 27 2c 20 62 20 3d 20 27 78 78 78 27 3b 0a  xx', b = 'xxx';.
11520 20 20 20 20 53 45 4c 45 43 54 20 2a 20 46 52 4f      SELECT * FRO
11530 4d 20 70 3b 0a 20 20 20 20 53 45 4c 45 43 54 20  M p;.    SELECT 
11540 2a 20 46 52 4f 4d 20 63 31 3b 0a 20 20 20 20 53  * FROM c1;.    S
11550 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 63 32 3b  ELECT * FROM c2;
11560 0a 20 20 20 20 53 45 4c 45 43 54 20 2a 20 46 52  .    SELECT * FR
11570 4f 4d 20 63 33 3b 0a 20 20 7d 0a 7d 20 7b 78 78  OM c3;.  }.} {xx
11580 78 20 78 78 78 20 31 20 78 78 78 20 31 20 78 78  x xxx 1 xxx 1 xx
11590 78 20 31 20 78 78 78 7d 0a 64 6f 5f 74 65 73 74  x 1 xxx}.do_test
115a0 20 65 5f 66 6b 65 79 2d 35 36 2e 34 20 7b 0a 20   e_fkey-56.4 {. 
115b0 20 65 78 65 63 73 71 6c 20 7b 20 53 45 4c 45 43   execsql { SELEC
115c0 54 20 73 71 6c 20 46 52 4f 4d 20 73 71 6c 69 74  T sql FROM sqlit
115d0 65 5f 6d 61 73 74 65 72 20 57 48 45 52 45 20 74  e_master WHERE t
115e0 79 70 65 20 3d 20 27 74 61 62 6c 65 27 7d 0a 7d  ype = 'table'}.}
115f0 20 5b 6c 69 73 74 20 20 20 20 20 20 20 20 20 20   [list          
11600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11610 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11630 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 7b             \.  {
11640 43 52 45 41 54 45 20 54 41 42 4c 45 20 22 70 22  CREATE TABLE "p"
11650 28 61 20 52 45 46 45 52 45 4e 43 45 53 20 22 70  (a REFERENCES "p
11660 22 2c 20 62 2c 20 50 52 49 4d 41 52 59 20 4b 45  ", b, PRIMARY KE
11670 59 28 62 29 29 7d 20 20 20 20 20 20 20 20 20 20  Y(b))}          
11680 20 20 20 20 20 20 20 20 20 5c 0a 20 20 7b 43 52           \.  {CR
11690 45 41 54 45 20 54 41 42 4c 45 20 63 31 28 63 2c  EATE TABLE c1(c,
116a0 20 64 20 52 45 46 45 52 45 4e 43 45 53 20 22 70   d REFERENCES "p
116b0 22 20 4f 4e 20 55 50 44 41 54 45 20 43 41 53 43  " ON UPDATE CASC
116c0 41 44 45 29 7d 20 20 20 20 20 20 20 20 20 20 20  ADE)}           
116d0 20 20 20 20 20 20 20 5c 0a 20 20 7b 43 52 45 41         \.  {CREA
116e0 54 45 20 54 41 42 4c 45 20 63 32 28 65 2c 20 66  TE TABLE c2(e, f
116f0 2c 20 46 4f 52 45 49 47 4e 20 4b 45 59 28 66 29  , FOREIGN KEY(f)
11700 20 52 45 46 45 52 45 4e 43 45 53 20 22 70 22 20   REFERENCES "p" 
11710 4f 4e 20 55 50 44 41 54 45 20 43 41 53 43 41 44  ON UPDATE CASCAD
11720 45 29 7d 20 20 5c 0a 20 20 7b 43 52 45 41 54 45  E)}  \.  {CREATE
11730 20 54 41 42 4c 45 20 63 33 28 65 2c 20 27 66 20   TABLE c3(e, 'f 
11740 63 6f 6c 20 32 27 2c 20 46 4f 52 45 49 47 4e 20  col 2', FOREIGN 
11750 4b 45 59 28 27 66 20 63 6f 6c 20 32 27 29 20 52  KEY('f col 2') R
11760 45 46 45 52 45 4e 43 45 53 20 22 70 22 20 4f 4e  EFERENCES "p" ON
11770 20 55 50 44 41 54 45 20 43 41 53 43 41 44 45 29   UPDATE CASCADE)
11780 7d 20 5c 0a 5d 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d  } \.]..#--------
11790 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
117a0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
117b0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
117c0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
117d0 2d 0a 23 20 2f 2a 20 45 56 3a 20 52 2d 31 34 32  -.# /* EV: R-142
117e0 30 38 2d 32 33 39 38 36 20 2a 2f 0a 23 20 2f 2a  08-23986 */.# /*
117f0 20 45 56 3a 20 52 2d 31 31 30 37 38 2d 30 33 39   EV: R-11078-039
11800 34 35 20 2a 2f 0a 23 0a 23 20 43 68 65 63 6b 20  45 */.#.# Check 
11810 74 68 61 74 20 61 20 44 52 4f 50 20 54 41 42 4c  that a DROP TABL
11820 45 20 64 6f 65 73 20 61 6e 20 69 6d 70 6c 69 63  E does an implic
11830 69 74 20 44 45 4c 45 54 45 20 46 52 4f 4d 2e 20  it DELETE FROM. 
11840 57 68 69 63 68 20 64 6f 65 73 20 6e 6f 74 0a 23  Which does not.#
11850 20 63 61 75 73 65 20 61 6e 79 20 74 72 69 67 67   cause any trigg
11860 65 72 73 20 74 6f 20 66 69 72 65 2c 20 62 75 74  ers to fire, but
11870 20 64 6f 65 73 20 66 69 72 65 20 66 6f 72 65 69   does fire forei
11880 67 6e 20 6b 65 79 20 61 63 74 69 6f 6e 73 2e 0a  gn key actions..
11890 23 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79  #.do_test e_fkey
118a0 2d 35 37 2e 31 20 7b 0a 20 20 64 72 6f 70 5f 61  -57.1 {.  drop_a
118b0 6c 6c 5f 74 61 62 6c 65 73 0a 20 20 65 78 65 63  ll_tables.  exec
118c0 73 71 6c 20 7b 0a 20 20 20 20 43 52 45 41 54 45  sql {.    CREATE
118d0 20 54 41 42 4c 45 20 70 28 61 2c 20 62 2c 20 50   TABLE p(a, b, P
118e0 52 49 4d 41 52 59 20 4b 45 59 28 61 2c 20 62 29  RIMARY KEY(a, b)
118f0 29 3b 0a 0a 20 20 20 20 43 52 45 41 54 45 20 54  );..    CREATE T
11900 41 42 4c 45 20 63 31 28 63 2c 20 64 2c 20 46 4f  ABLE c1(c, d, FO
11910 52 45 49 47 4e 20 4b 45 59 28 63 2c 20 64 29 20  REIGN KEY(c, d) 
11920 52 45 46 45 52 45 4e 43 45 53 20 70 20 4f 4e 20  REFERENCES p ON 
11930 44 45 4c 45 54 45 20 53 45 54 20 4e 55 4c 4c 29  DELETE SET NULL)
11940 3b 0a 20 20 20 20 43 52 45 41 54 45 20 54 41 42  ;.    CREATE TAB
11950 4c 45 20 63 32 28 63 2c 20 64 2c 20 46 4f 52 45  LE c2(c, d, FORE
11960 49 47 4e 20 4b 45 59 28 63 2c 20 64 29 20 52 45  IGN KEY(c, d) RE
11970 46 45 52 45 4e 43 45 53 20 70 20 4f 4e 20 44 45  FERENCES p ON DE
11980 4c 45 54 45 20 53 45 54 20 44 45 46 41 55 4c 54  LETE SET DEFAULT
11990 29 3b 0a 20 20 20 20 43 52 45 41 54 45 20 54 41  );.    CREATE TA
119a0 42 4c 45 20 63 33 28 63 2c 20 64 2c 20 46 4f 52  BLE c3(c, d, FOR
119b0 45 49 47 4e 20 4b 45 59 28 63 2c 20 64 29 20 52  EIGN KEY(c, d) R
119c0 45 46 45 52 45 4e 43 45 53 20 70 20 4f 4e 20 44  EFERENCES p ON D
119d0 45 4c 45 54 45 20 43 41 53 43 41 44 45 29 3b 0a  ELETE CASCADE);.
119e0 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45      CREATE TABLE
119f0 20 63 34 28 63 2c 20 64 2c 20 46 4f 52 45 49 47   c4(c, d, FOREIG
11a00 4e 20 4b 45 59 28 63 2c 20 64 29 20 52 45 46 45  N KEY(c, d) REFE
11a10 52 45 4e 43 45 53 20 70 20 4f 4e 20 44 45 4c 45  RENCES p ON DELE
11a20 54 45 20 52 45 53 54 52 49 43 54 29 3b 0a 20 20  TE RESTRICT);.  
11a30 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 63    CREATE TABLE c
11a40 35 28 63 2c 20 64 2c 20 46 4f 52 45 49 47 4e 20  5(c, d, FOREIGN 
11a50 4b 45 59 28 63 2c 20 64 29 20 52 45 46 45 52 45  KEY(c, d) REFERE
11a60 4e 43 45 53 20 70 20 4f 4e 20 44 45 4c 45 54 45  NCES p ON DELETE
11a70 20 4e 4f 20 41 43 54 49 4f 4e 29 3b 0a 0a 20 20   NO ACTION);..  
11a80 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 63    CREATE TABLE c
11a90 36 28 63 2c 20 64 2c 20 0a 20 20 20 20 20 20 46  6(c, d, .      F
11aa0 4f 52 45 49 47 4e 20 4b 45 59 28 63 2c 20 64 29  OREIGN KEY(c, d)
11ab0 20 52 45 46 45 52 45 4e 43 45 53 20 70 20 4f 4e   REFERENCES p ON
11ac0 20 44 45 4c 45 54 45 20 52 45 53 54 52 49 43 54   DELETE RESTRICT
11ad0 20 0a 20 20 20 20 20 20 44 45 46 45 52 52 41 42   .      DEFERRAB
11ae0 4c 45 20 49 4e 49 54 49 41 4c 4c 59 20 44 45 46  LE INITIALLY DEF
11af0 45 52 52 45 44 0a 20 20 20 20 29 3b 0a 20 20 20  ERRED.    );.   
11b00 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 63 37   CREATE TABLE c7
11b10 28 63 2c 20 64 2c 20 0a 20 20 20 20 20 20 46 4f  (c, d, .      FO
11b20 52 45 49 47 4e 20 4b 45 59 28 63 2c 20 64 29 20  REIGN KEY(c, d) 
11b30 52 45 46 45 52 45 4e 43 45 53 20 70 20 4f 4e 20  REFERENCES p ON 
11b40 44 45 4c 45 54 45 20 4e 4f 20 41 43 54 49 4f 4e  DELETE NO ACTION
11b50 0a 20 20 20 20 20 20 44 45 46 45 52 52 41 42 4c  .      DEFERRABL
11b60 45 20 49 4e 49 54 49 41 4c 4c 59 20 44 45 46 45  E INITIALLY DEFE
11b70 52 52 45 44 0a 20 20 20 20 29 3b 0a 0a 20 20 20  RRED.    );..   
11b80 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 6c 6f   CREATE TABLE lo
11b90 67 28 6d 73 67 29 3b 0a 20 20 20 20 43 52 45 41  g(msg);.    CREA
11ba0 54 45 20 54 52 49 47 47 45 52 20 74 74 20 41 46  TE TRIGGER tt AF
11bb0 54 45 52 20 44 45 4c 45 54 45 20 4f 4e 20 70 20  TER DELETE ON p 
11bc0 42 45 47 49 4e 0a 20 20 20 20 20 20 49 4e 53 45  BEGIN.      INSE
11bd0 52 54 20 49 4e 54 4f 20 6c 6f 67 20 56 41 4c 55  RT INTO log VALU
11be0 45 53 28 27 64 65 6c 65 74 65 20 27 20 7c 7c 20  ES('delete ' || 
11bf0 6f 6c 64 2e 72 6f 77 69 64 29 3b 0a 20 20 20 20  old.rowid);.    
11c00 45 4e 44 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a 0a 64  END;.  }.} {}..d
11c10 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 35 37  o_test e_fkey-57
11c20 2e 32 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b  .2 {.  execsql {
11c30 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f  .    INSERT INTO
11c40 20 70 20 56 41 4c 55 45 53 28 27 61 27 2c 20 27   p VALUES('a', '
11c50 62 27 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20  b');.    INSERT 
11c60 49 4e 54 4f 20 63 31 20 56 41 4c 55 45 53 28 27  INTO c1 VALUES('
11c70 61 27 2c 20 27 62 27 29 3b 0a 20 20 20 20 49 4e  a', 'b');.    IN
11c80 53 45 52 54 20 49 4e 54 4f 20 63 32 20 56 41 4c  SERT INTO c2 VAL
11c90 55 45 53 28 27 61 27 2c 20 27 62 27 29 3b 0a 20  UES('a', 'b');. 
11ca0 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 63     INSERT INTO c
11cb0 33 20 56 41 4c 55 45 53 28 27 61 27 2c 20 27 62  3 VALUES('a', 'b
11cc0 27 29 3b 0a 20 20 20 20 42 45 47 49 4e 3b 0a 20  ');.    BEGIN;. 
11cd0 20 20 20 20 20 44 52 4f 50 20 54 41 42 4c 45 20       DROP TABLE 
11ce0 70 3b 0a 20 20 20 20 20 20 53 45 4c 45 43 54 20  p;.      SELECT 
11cf0 2a 20 46 52 4f 4d 20 63 31 3b 0a 20 20 7d 0a 7d  * FROM c1;.  }.}
11d00 20 7b 7b 7d 20 7b 7d 7d 0a 64 6f 5f 74 65 73 74   {{} {}}.do_test
11d10 20 65 5f 66 6b 65 79 2d 35 37 2e 33 20 7b 0a 20   e_fkey-57.3 {. 
11d20 20 65 78 65 63 73 71 6c 20 7b 20 53 45 4c 45 43   execsql { SELEC
11d30 54 20 2a 20 46 52 4f 4d 20 63 32 20 7d 0a 7d 20  T * FROM c2 }.} 
11d40 7b 7b 7d 20 7b 7d 7d 0a 64 6f 5f 74 65 73 74 20  {{} {}}.do_test 
11d50 65 5f 66 6b 65 79 2d 35 37 2e 34 20 7b 0a 20 20  e_fkey-57.4 {.  
11d60 65 78 65 63 73 71 6c 20 7b 20 53 45 4c 45 43 54  execsql { SELECT
11d70 20 2a 20 46 52 4f 4d 20 63 33 20 7d 0a 7d 20 7b   * FROM c3 }.} {
11d80 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79  }.do_test e_fkey
11d90 2d 35 37 2e 35 20 7b 0a 20 20 65 78 65 63 73 71  -57.5 {.  execsq
11da0 6c 20 7b 20 53 45 4c 45 43 54 20 2a 20 46 52 4f  l { SELECT * FRO
11db0 4d 20 6c 6f 67 20 7d 0a 7d 20 7b 7d 0a 64 6f 5f  M log }.} {}.do_
11dc0 74 65 73 74 20 65 5f 66 6b 65 79 2d 35 37 2e 36  test e_fkey-57.6
11dd0 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 52 4f 4c   {.  execsql ROL
11de0 4c 42 41 43 4b 0a 7d 20 7b 7d 0a 64 6f 5f 74 65  LBACK.} {}.do_te
11df0 73 74 20 65 5f 66 6b 65 79 2d 35 37 2e 37 20 7b  st e_fkey-57.7 {
11e00 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20  .  execsql {.   
11e10 20 42 45 47 49 4e 3b 0a 20 20 20 20 20 20 44 45   BEGIN;.      DE
11e20 4c 45 54 45 20 46 52 4f 4d 20 70 3b 0a 20 20 20  LETE FROM p;.   
11e30 20 20 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d     SELECT * FROM
11e40 20 6c 6f 67 3b 0a 20 20 20 20 52 4f 4c 4c 42 41   log;.    ROLLBA
11e50 43 4b 3b 0a 20 20 7d 0a 7d 20 7b 7b 64 65 6c 65  CK;.  }.} {{dele
11e60 74 65 20 31 7d 7d 0a 0a 23 2d 2d 2d 2d 2d 2d 2d  te 1}}..#-------
11e70 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
11e80 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
11e90 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
11ea0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
11eb0 2d 2d 0a 23 20 2f 2a 20 45 56 3a 20 52 2d 33 32  --.# /* EV: R-32
11ec0 37 36 38 2d 34 37 39 32 35 20 2a 2f 0a 23 0a 23  768-47925 */.#.#
11ed0 20 49 66 20 61 6e 20 49 4d 4d 45 44 49 41 54 45   If an IMMEDIATE
11ee0 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 66 61 69   foreign key fai
11ef0 6c 73 20 61 73 20 61 20 72 65 73 75 6c 74 20 6f  ls as a result o
11f00 66 20 61 20 44 52 4f 50 20 54 41 42 4c 45 2c 20  f a DROP TABLE, 
11f10 74 68 65 0a 23 20 44 52 4f 50 20 54 41 42 4c 45  the.# DROP TABLE
11f20 20 63 6f 6d 6d 61 6e 64 20 66 61 69 6c 73 2e 0a   command fails..
11f30 23 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79  #.do_test e_fkey
11f40 2d 35 38 2e 31 20 7b 0a 20 20 65 78 65 63 73 71  -58.1 {.  execsq
11f50 6c 20 7b 20 0a 20 20 20 20 44 45 4c 45 54 45 20  l { .    DELETE 
11f60 46 52 4f 4d 20 63 31 3b 0a 20 20 20 20 44 45 4c  FROM c1;.    DEL
11f70 45 54 45 20 46 52 4f 4d 20 63 32 3b 0a 20 20 20  ETE FROM c2;.   
11f80 20 44 45 4c 45 54 45 20 46 52 4f 4d 20 63 33 3b   DELETE FROM c3;
11f90 0a 20 20 7d 0a 20 20 65 78 65 63 73 71 6c 20 7b  .  }.  execsql {
11fa0 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 63 35 20   INSERT INTO c5 
11fb0 56 41 4c 55 45 53 28 27 61 27 2c 20 27 62 27 29  VALUES('a', 'b')
11fc0 20 7d 0a 20 20 63 61 74 63 68 73 71 6c 20 7b 20   }.  catchsql { 
11fd0 44 52 4f 50 20 54 41 42 4c 45 20 70 20 7d 0a 7d  DROP TABLE p }.}
11fe0 20 7b 31 20 7b 66 6f 72 65 69 67 6e 20 6b 65 79   {1 {foreign key
11ff0 20 63 6f 6e 73 74 72 61 69 6e 74 20 66 61 69 6c   constraint fail
12000 65 64 7d 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66  ed}}.do_test e_f
12010 6b 65 79 2d 35 38 2e 32 20 7b 0a 20 20 65 78 65  key-58.2 {.  exe
12020 63 73 71 6c 20 7b 20 53 45 4c 45 43 54 20 2a 20  csql { SELECT * 
12030 46 52 4f 4d 20 70 20 7d 0a 7d 20 7b 61 20 62 7d  FROM p }.} {a b}
12040 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d  .do_test e_fkey-
12050 35 38 2e 33 20 7b 0a 20 20 63 61 74 63 68 73 71  58.3 {.  catchsq
12060 6c 20 7b 0a 20 20 20 20 42 45 47 49 4e 3b 0a 20  l {.    BEGIN;. 
12070 20 20 20 20 20 44 52 4f 50 20 54 41 42 4c 45 20       DROP TABLE 
12080 70 3b 0a 20 20 7d 0a 7d 20 7b 31 20 7b 66 6f 72  p;.  }.} {1 {for
12090 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61  eign key constra
120a0 69 6e 74 20 66 61 69 6c 65 64 7d 7d 0a 64 6f 5f  int failed}}.do_
120b0 74 65 73 74 20 65 5f 66 6b 65 79 2d 35 38 2e 34  test e_fkey-58.4
120c0 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20   {.  execsql {. 
120d0 20 20 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d     SELECT * FROM
120e0 20 70 3b 0a 20 20 20 20 53 45 4c 45 43 54 20 2a   p;.    SELECT *
120f0 20 46 52 4f 4d 20 63 35 3b 0a 20 20 20 20 52 4f   FROM c5;.    RO
12100 4c 4c 42 41 43 4b 3b 0a 20 20 7d 0a 7d 20 7b 61  LLBACK;.  }.} {a
12110 20 62 20 61 20 62 7d 0a 0a 23 2d 2d 2d 2d 2d 2d   b a b}..#------
12120 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
12130 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
12140 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
12150 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
12160 2d 2d 2d 0a 23 20 2f 2a 20 45 56 3a 20 52 2d 30  ---.# /* EV: R-0
12170 35 39 30 33 2d 30 38 34 36 30 20 2a 2f 0a 23 0a  5903-08460 */.#.
12180 23 20 49 66 20 61 20 44 45 46 45 52 52 45 44 20  # If a DEFERRED 
12190 66 6f 72 65 69 67 6e 20 6b 65 79 20 66 61 69 6c  foreign key fail
121a0 73 20 61 73 20 61 20 72 65 73 75 6c 74 20 6f 66  s as a result of
121b0 20 61 20 44 52 4f 50 20 54 41 42 4c 45 2c 20 61   a DROP TABLE, a
121c0 74 74 65 6d 70 74 69 6e 67 0a 23 20 74 6f 20 63  ttempting.# to c
121d0 6f 6d 6d 69 74 20 74 68 65 20 74 72 61 6e 73 61  ommit the transa
121e0 63 74 69 6f 6e 20 66 61 69 6c 73 20 75 6e 6c 65  ction fails unle
121f0 73 73 20 74 68 65 20 76 69 6f 6c 61 74 69 6f 6e  ss the violation
12200 20 69 73 20 66 69 78 65 64 2e 0a 23 0a 64 6f 5f   is fixed..#.do_
12210 74 65 73 74 20 65 5f 66 6b 65 79 2d 35 39 2e 31  test e_fkey-59.1
12220 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 20 0a   {.  execsql { .
12230 20 20 20 20 44 45 4c 45 54 45 20 46 52 4f 4d 20      DELETE FROM 
12240 63 31 20 3b 20 44 45 4c 45 54 45 20 46 52 4f 4d  c1 ; DELETE FROM
12250 20 63 32 20 3b 20 44 45 4c 45 54 45 20 46 52 4f   c2 ; DELETE FRO
12260 4d 20 63 33 20 3b 0a 20 20 20 20 44 45 4c 45 54  M c3 ;.    DELET
12270 45 20 46 52 4f 4d 20 63 34 20 3b 20 44 45 4c 45  E FROM c4 ; DELE
12280 54 45 20 46 52 4f 4d 20 63 35 20 3b 20 44 45 4c  TE FROM c5 ; DEL
12290 45 54 45 20 46 52 4f 4d 20 63 36 20 3b 0a 20 20  ETE FROM c6 ;.  
122a0 20 20 44 45 4c 45 54 45 20 46 52 4f 4d 20 63 37    DELETE FROM c7
122b0 20 0a 20 20 7d 0a 7d 20 7b 7d 0a 64 6f 5f 74 65   .  }.} {}.do_te
122c0 73 74 20 65 5f 66 6b 65 79 2d 35 39 2e 32 20 7b  st e_fkey-59.2 {
122d0 0a 20 20 65 78 65 63 73 71 6c 20 7b 20 49 4e 53  .  execsql { INS
122e0 45 52 54 20 49 4e 54 4f 20 63 37 20 56 41 4c 55  ERT INTO c7 VALU
122f0 45 53 28 27 61 27 2c 20 27 62 27 29 20 7d 0a 20  ES('a', 'b') }. 
12300 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 42   execsql {.    B
12310 45 47 49 4e 3b 0a 20 20 20 20 20 20 44 52 4f 50  EGIN;.      DROP
12320 20 54 41 42 4c 45 20 70 3b 0a 20 20 7d 0a 7d 20   TABLE p;.  }.} 
12330 7b 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65  {}.do_test e_fke
12340 79 2d 35 39 2e 33 20 7b 0a 20 20 63 61 74 63 68  y-59.3 {.  catch
12350 73 71 6c 20 43 4f 4d 4d 49 54 0a 7d 20 7b 31 20  sql COMMIT.} {1 
12360 7b 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e  {foreign key con
12370 73 74 72 61 69 6e 74 20 66 61 69 6c 65 64 7d 7d  straint failed}}
12380 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d  .do_test e_fkey-
12390 35 39 2e 34 20 7b 0a 20 20 65 78 65 63 73 71 6c  59.4 {.  execsql
123a0 20 7b 20 43 52 45 41 54 45 20 54 41 42 4c 45 20   { CREATE TABLE 
123b0 70 28 61 2c 20 62 2c 20 50 52 49 4d 41 52 59 20  p(a, b, PRIMARY 
123c0 4b 45 59 28 61 2c 20 62 29 29 20 7d 0a 20 20 63  KEY(a, b)) }.  c
123d0 61 74 63 68 73 71 6c 20 43 4f 4d 4d 49 54 0a 7d  atchsql COMMIT.}
123e0 20 7b 31 20 7b 66 6f 72 65 69 67 6e 20 6b 65 79   {1 {foreign key
123f0 20 63 6f 6e 73 74 72 61 69 6e 74 20 66 61 69 6c   constraint fail
12400 65 64 7d 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66  ed}}.do_test e_f
12410 6b 65 79 2d 35 39 2e 35 20 7b 0a 20 20 65 78 65  key-59.5 {.  exe
12420 63 73 71 6c 20 7b 20 49 4e 53 45 52 54 20 49 4e  csql { INSERT IN
12430 54 4f 20 70 20 56 41 4c 55 45 53 28 27 61 27 2c  TO p VALUES('a',
12440 20 27 62 27 29 20 7d 0a 20 20 65 78 65 63 73 71   'b') }.  execsq
12450 6c 20 43 4f 4d 4d 49 54 0a 7d 20 7b 7d 0a 0a 23  l COMMIT.} {}..#
12460 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
12470 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
12480 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
12490 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
124a0 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20 2f 2a 20 45  ---------.# /* E
124b0 56 3a 20 52 2d 35 37 32 34 32 2d 33 37 30 30 35  V: R-57242-37005
124c0 20 2a 2f 0a 23 0a 23 20 41 6e 79 20 22 66 6f 72   */.#.# Any "for
124d0 65 69 67 6e 20 6b 65 79 20 6d 69 73 6d 61 74 63  eign key mismatc
124e0 68 22 20 65 72 72 6f 72 73 20 65 6e 63 6f 75 6e  h" errors encoun
124f0 74 65 72 65 64 20 77 68 69 6c 65 20 72 75 6e 6e  tered while runn
12500 69 6e 67 20 61 6e 20 69 6d 70 6c 69 63 69 74 0a  ing an implicit.
12510 23 20 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 74  # "DELETE FROM t
12520 62 6c 22 20 61 72 65 20 69 67 6e 6f 72 65 64 2e  bl" are ignored.
12530 0a 23 0a 64 72 6f 70 5f 61 6c 6c 5f 74 61 62 6c  .#.drop_all_tabl
12540 65 73 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65  es.do_test e_fke
12550 79 2d 36 30 2e 31 20 7b 0a 20 20 65 78 65 63 73  y-60.1 {.  execs
12560 71 6c 20 7b 0a 20 20 20 20 50 52 41 47 4d 41 20  ql {.    PRAGMA 
12570 66 6f 72 65 69 67 6e 5f 6b 65 79 73 20 3d 20 4f  foreign_keys = O
12580 46 46 3b 0a 0a 20 20 20 20 43 52 45 41 54 45 20  FF;..    CREATE 
12590 54 41 42 4c 45 20 70 28 61 20 50 52 49 4d 41 52  TABLE p(a PRIMAR
125a0 59 20 4b 45 59 2c 20 62 20 52 45 46 45 52 45 4e  Y KEY, b REFEREN
125b0 43 45 53 20 6e 6f 73 75 63 68 74 61 62 6c 65 29  CES nosuchtable)
125c0 3b 0a 20 20 20 20 43 52 45 41 54 45 20 54 41 42  ;.    CREATE TAB
125d0 4c 45 20 63 31 28 63 2c 20 64 2c 20 46 4f 52 45  LE c1(c, d, FORE
125e0 49 47 4e 20 4b 45 59 28 63 2c 20 64 29 20 52 45  IGN KEY(c, d) RE
125f0 46 45 52 45 4e 43 45 53 20 61 29 3b 0a 20 20 20  FERENCES a);.   
12600 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 63 32   CREATE TABLE c2
12610 28 63 20 52 45 46 45 52 45 4e 43 45 53 20 70 28  (c REFERENCES p(
12620 62 29 2c 20 64 29 3b 0a 20 20 20 20 43 52 45 41  b), d);.    CREA
12630 54 45 20 54 41 42 4c 45 20 63 33 28 63 20 52 45  TE TABLE c3(c RE
12640 46 45 52 45 4e 43 45 53 20 70 20 4f 4e 20 44 45  FERENCES p ON DE
12650 4c 45 54 45 20 53 45 54 20 4e 55 4c 4c 2c 20 64  LETE SET NULL, d
12660 29 3b 0a 0a 20 20 20 20 49 4e 53 45 52 54 20 49  );..    INSERT I
12670 4e 54 4f 20 70 20 56 41 4c 55 45 53 28 31 2c 20  NTO p VALUES(1, 
12680 32 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49  2);.    INSERT I
12690 4e 54 4f 20 63 31 20 56 41 4c 55 45 53 28 31 2c  NTO c1 VALUES(1,
126a0 20 32 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20   2);.    INSERT 
126b0 49 4e 54 4f 20 63 32 20 56 41 4c 55 45 53 28 31  INTO c2 VALUES(1
126c0 2c 20 32 29 3b 0a 20 20 20 20 49 4e 53 45 52 54  , 2);.    INSERT
126d0 20 49 4e 54 4f 20 63 33 20 56 41 4c 55 45 53 28   INTO c3 VALUES(
126e0 31 2c 20 32 29 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a  1, 2);.  }.} {}.
126f0 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 36  do_test e_fkey-6
12700 30 2e 32 20 7b 0a 20 20 65 78 65 63 73 71 6c 20  0.2 {.  execsql 
12710 7b 20 50 52 41 47 4d 41 20 66 6f 72 65 69 67 6e  { PRAGMA foreign
12720 5f 6b 65 79 73 20 3d 20 4f 4e 20 7d 0a 20 20 63  _keys = ON }.  c
12730 61 74 63 68 73 71 6c 20 7b 20 44 45 4c 45 54 45  atchsql { DELETE
12740 20 46 52 4f 4d 20 70 20 7d 0a 7d 20 7b 31 20 7b   FROM p }.} {1 {
12750 6e 6f 20 73 75 63 68 20 74 61 62 6c 65 3a 20 6d  no such table: m
12760 61 69 6e 2e 6e 6f 73 75 63 68 74 61 62 6c 65 7d  ain.nosuchtable}
12770 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79  }.do_test e_fkey
12780 2d 36 30 2e 33 20 7b 0a 20 20 65 78 65 63 73 71  -60.3 {.  execsq
12790 6c 20 7b 0a 20 20 20 20 42 45 47 49 4e 3b 0a 20  l {.    BEGIN;. 
127a0 20 20 20 20 20 44 52 4f 50 20 54 41 42 4c 45 20       DROP TABLE 
127b0 70 3b 0a 20 20 20 20 20 20 53 45 4c 45 43 54 20  p;.      SELECT 
127c0 2a 20 46 52 4f 4d 20 63 33 3b 0a 20 20 20 20 52  * FROM c3;.    R
127d0 4f 4c 4c 42 41 43 4b 3b 0a 20 20 7d 0a 7d 20 7b  OLLBACK;.  }.} {
127e0 7b 7d 20 32 7d 0a 64 6f 5f 74 65 73 74 20 65 5f  {} 2}.do_test e_
127f0 66 6b 65 79 2d 36 30 2e 34 20 7b 0a 20 20 65 78  fkey-60.4 {.  ex
12800 65 63 73 71 6c 20 7b 20 43 52 45 41 54 45 20 54  ecsql { CREATE T
12810 41 42 4c 45 20 6e 6f 73 75 63 68 74 61 62 6c 65  ABLE nosuchtable
12820 28 78 20 50 52 49 4d 41 52 59 20 4b 45 59 29 20  (x PRIMARY KEY) 
12830 7d 0a 20 20 63 61 74 63 68 73 71 6c 20 7b 20 44  }.  catchsql { D
12840 45 4c 45 54 45 20 46 52 4f 4d 20 70 20 7d 0a 7d  ELETE FROM p }.}
12850 20 7b 31 20 7b 66 6f 72 65 69 67 6e 20 6b 65 79   {1 {foreign key
12860 20 6d 69 73 6d 61 74 63 68 7d 7d 0a 64 6f 5f 74   mismatch}}.do_t
12870 65 73 74 20 65 5f 66 6b 65 79 2d 36 30 2e 35 20  est e_fkey-60.5 
12880 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 20 44 52  {.  execsql { DR
12890 4f 50 20 54 41 42 4c 45 20 63 31 20 7d 0a 20 20  OP TABLE c1 }.  
128a0 63 61 74 63 68 73 71 6c 20 7b 20 44 45 4c 45 54  catchsql { DELET
128b0 45 20 46 52 4f 4d 20 70 20 7d 0a 7d 20 7b 31 20  E FROM p }.} {1 
128c0 7b 66 6f 72 65 69 67 6e 20 6b 65 79 20 6d 69 73  {foreign key mis
128d0 6d 61 74 63 68 7d 7d 0a 64 6f 5f 74 65 73 74 20  match}}.do_test 
128e0 65 5f 66 6b 65 79 2d 36 30 2e 36 20 7b 0a 20 20  e_fkey-60.6 {.  
128f0 65 78 65 63 73 71 6c 20 7b 20 44 52 4f 50 20 54  execsql { DROP T
12900 41 42 4c 45 20 63 32 20 7d 0a 20 20 65 78 65 63  ABLE c2 }.  exec
12910 73 71 6c 20 7b 20 44 45 4c 45 54 45 20 46 52 4f  sql { DELETE FRO
12920 4d 20 70 20 7d 0a 7d 20 7b 7d 0a 0a 23 2d 2d 2d  M p }.} {}..#---
12930 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
12940 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
12950 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
12960 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
12970 2d 2d 2d 2d 2d 2d 0a 23 20 2f 2a 20 45 56 3a 20  ------.# /* EV: 
12980 52 2d 35 34 31 34 32 2d 34 31 33 34 36 20 2a 2f  R-54142-41346 */
12990 0a 23 0a 23 20 54 65 73 74 20 74 68 61 74 20 74  .#.# Test that t
129a0 68 65 20 73 70 65 63 69 61 6c 20 62 65 68 61 76  he special behav
129b0 69 6f 75 72 73 20 6f 66 20 41 4c 54 45 52 20 61  iours of ALTER a
129c0 6e 64 20 44 52 4f 50 20 54 41 42 4c 45 20 61 72  nd DROP TABLE ar
129d0 65 20 6f 6e 6c 79 0a 23 20 61 63 74 69 76 61 74  e only.# activat
129e0 65 64 20 77 68 65 6e 20 66 6f 72 65 69 67 6e 20  ed when foreign 
129f0 6b 65 79 73 20 61 72 65 20 65 6e 61 62 6c 65 64  keys are enabled
12a00 2e 20 53 70 65 63 69 61 6c 20 62 65 68 61 76 69  . Special behavi
12a10 6f 75 72 73 20 61 72 65 3a 0a 23 0a 23 20 20 20  ours are:.#.#   
12a20 31 2e 20 41 44 44 20 43 4f 4c 55 4d 4e 20 6e 6f  1. ADD COLUMN no
12a30 74 20 61 6c 6c 6f 77 69 6e 67 20 61 20 52 45 46  t allowing a REF
12a40 45 52 45 4e 43 45 53 20 63 6c 61 75 73 65 20 77  ERENCES clause w
12a50 69 74 68 20 61 20 6e 6f 6e 2d 4e 55 4c 4c 20 0a  ith a non-NULL .
12a60 23 20 20 20 20 20 20 64 65 66 61 75 6c 74 20 76  #      default v
12a70 61 6c 75 65 2e 0a 23 20 20 20 32 2e 20 4d 6f 64  alue..#   2. Mod
12a80 69 66 79 69 6e 67 20 66 6f 72 65 69 67 6e 20 6b  ifying foreign k
12a90 65 79 20 64 65 66 69 6e 69 74 69 6f 6e 73 20 77  ey definitions w
12aa0 68 65 6e 20 61 20 70 61 72 65 6e 74 20 74 61 62  hen a parent tab
12ab0 6c 65 20 69 73 20 52 45 4e 41 4d 45 64 2e 0a 23  le is RENAMEd..#
12ac0 20 20 20 33 2e 20 52 75 6e 6e 69 6e 67 20 61 6e     3. Running an
12ad0 20 69 6d 70 6c 69 63 69 74 20 44 45 4c 45 54 45   implicit DELETE
12ae0 20 46 52 4f 4d 20 63 6f 6d 6d 61 6e 64 20 61 73   FROM command as
12af0 20 70 61 72 74 20 6f 66 20 44 52 4f 50 20 54 41   part of DROP TA
12b00 42 4c 45 2e 0a 23 0a 64 6f 5f 74 65 73 74 20 65  BLE..#.do_test e
12b10 5f 66 6b 65 79 2d 36 31 2e 31 2e 31 20 7b 0a 20  _fkey-61.1.1 {. 
12b20 20 64 72 6f 70 5f 61 6c 6c 5f 74 61 62 6c 65 73   drop_all_tables
12b30 0a 20 20 65 78 65 63 73 71 6c 20 7b 20 43 52 45  .  execsql { CRE
12b40 41 54 45 20 54 41 42 4c 45 20 74 31 28 61 2c 20  ATE TABLE t1(a, 
12b50 62 29 20 7d 0a 20 20 63 61 74 63 68 73 71 6c 20  b) }.  catchsql 
12b60 7b 20 41 4c 54 45 52 20 54 41 42 4c 45 20 74 31  { ALTER TABLE t1
12b70 20 41 44 44 20 43 4f 4c 55 4d 4e 20 63 20 44 45   ADD COLUMN c DE
12b80 46 41 55 4c 54 20 27 78 78 78 27 20 52 45 46 45  FAULT 'xxx' REFE
12b90 52 45 4e 43 45 53 20 74 32 20 7d 0a 7d 20 7b 31  RENCES t2 }.} {1
12ba0 20 7b 43 61 6e 6e 6f 74 20 61 64 64 20 61 20 52   {Cannot add a R
12bb0 45 46 45 52 45 4e 43 45 53 20 63 6f 6c 75 6d 6e  EFERENCES column
12bc0 20 77 69 74 68 20 6e 6f 6e 2d 4e 55 4c 4c 20 64   with non-NULL d
12bd0 65 66 61 75 6c 74 20 76 61 6c 75 65 7d 7d 0a 64  efault value}}.d
12be0 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 36 31  o_test e_fkey-61
12bf0 2e 31 2e 32 20 7b 0a 20 20 65 78 65 63 73 71 6c  .1.2 {.  execsql
12c00 20 7b 20 50 52 41 47 4d 41 20 66 6f 72 65 69 67   { PRAGMA foreig
12c10 6e 5f 6b 65 79 73 20 3d 20 4f 46 46 20 7d 0a 20  n_keys = OFF }. 
12c20 20 65 78 65 63 73 71 6c 20 7b 20 41 4c 54 45 52   execsql { ALTER
12c30 20 54 41 42 4c 45 20 74 31 20 41 44 44 20 43 4f   TABLE t1 ADD CO
12c40 4c 55 4d 4e 20 63 20 44 45 46 41 55 4c 54 20 27  LUMN c DEFAULT '
12c50 78 78 78 27 20 52 45 46 45 52 45 4e 43 45 53 20  xxx' REFERENCES 
12c60 74 32 20 7d 0a 20 20 65 78 65 63 73 71 6c 20 7b  t2 }.  execsql {
12c70 20 53 45 4c 45 43 54 20 73 71 6c 20 46 52 4f 4d   SELECT sql FROM
12c80 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 57   sqlite_master W
12c90 48 45 52 45 20 6e 61 6d 65 20 3d 20 27 74 31 27  HERE name = 't1'
12ca0 20 7d 0a 7d 20 7b 7b 43 52 45 41 54 45 20 54 41   }.} {{CREATE TA
12cb0 42 4c 45 20 74 31 28 61 2c 20 62 2c 20 63 20 44  BLE t1(a, b, c D
12cc0 45 46 41 55 4c 54 20 27 78 78 78 27 20 52 45 46  EFAULT 'xxx' REF
12cd0 45 52 45 4e 43 45 53 20 74 32 29 7d 7d 0a 64 6f  ERENCES t2)}}.do
12ce0 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 36 31 2e  _test e_fkey-61.
12cf0 31 2e 33 20 7b 0a 20 20 65 78 65 63 73 71 6c 20  1.3 {.  execsql 
12d00 7b 20 50 52 41 47 4d 41 20 66 6f 72 65 69 67 6e  { PRAGMA foreign
12d10 5f 6b 65 79 73 20 3d 20 4f 4e 20 7d 0a 7d 20 7b  _keys = ON }.} {
12d20 7d 0a 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65  }..do_test e_fke
12d30 79 2d 36 31 2e 32 2e 31 20 7b 0a 20 20 64 72 6f  y-61.2.1 {.  dro
12d40 70 5f 61 6c 6c 5f 74 61 62 6c 65 73 0a 20 20 65  p_all_tables.  e
12d50 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 43 52 45  xecsql {.    CRE
12d60 41 54 45 20 54 41 42 4c 45 20 70 28 61 20 55 4e  ATE TABLE p(a UN
12d70 49 51 55 45 29 3b 0a 20 20 20 20 43 52 45 41 54  IQUE);.    CREAT
12d80 45 20 54 41 42 4c 45 20 63 28 62 20 52 45 46 45  E TABLE c(b REFE
12d90 52 45 4e 43 45 53 20 70 28 61 29 29 3b 0a 20 20  RENCES p(a));.  
12da0 20 20 42 45 47 49 4e 3b 0a 20 20 20 20 20 20 41    BEGIN;.      A
12db0 4c 54 45 52 20 54 41 42 4c 45 20 70 20 52 45 4e  LTER TABLE p REN
12dc0 41 4d 45 20 54 4f 20 70 61 72 65 6e 74 3b 0a 20  AME TO parent;. 
12dd0 20 20 20 20 20 53 45 4c 45 43 54 20 73 71 6c 20       SELECT sql 
12de0 46 52 4f 4d 20 73 71 6c 69 74 65 5f 6d 61 73 74  FROM sqlite_mast
12df0 65 72 20 57 48 45 52 45 20 6e 61 6d 65 20 3d 20  er WHERE name = 
12e00 27 63 27 3b 0a 20 20 20 20 52 4f 4c 4c 42 41 43  'c';.    ROLLBAC
12e10 4b 3b 0a 20 20 7d 0a 7d 20 7b 7b 43 52 45 41 54  K;.  }.} {{CREAT
12e20 45 20 54 41 42 4c 45 20 63 28 62 20 52 45 46 45  E TABLE c(b REFE
12e30 52 45 4e 43 45 53 20 22 70 61 72 65 6e 74 22 28  RENCES "parent"(
12e40 61 29 29 7d 7d 0a 64 6f 5f 74 65 73 74 20 65 5f  a))}}.do_test e_
12e50 66 6b 65 79 2d 36 31 2e 32 2e 32 20 7b 0a 20 20  fkey-61.2.2 {.  
12e60 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 50 52  execsql {.    PR
12e70 41 47 4d 41 20 66 6f 72 65 69 67 6e 5f 6b 65 79  AGMA foreign_key
12e80 73 20 3d 20 4f 46 46 3b 0a 20 20 20 20 41 4c 54  s = OFF;.    ALT
12e90 45 52 20 54 41 42 4c 45 20 70 20 52 45 4e 41 4d  ER TABLE p RENAM
12ea0 45 20 54 4f 20 70 61 72 65 6e 74 3b 0a 20 20 20  E TO parent;.   
12eb0 20 53 45 4c 45 43 54 20 73 71 6c 20 46 52 4f 4d   SELECT sql FROM
12ec0 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 57   sqlite_master W
12ed0 48 45 52 45 20 6e 61 6d 65 20 3d 20 27 63 27 3b  HERE name = 'c';
12ee0 0a 20 20 7d 0a 7d 20 7b 7b 43 52 45 41 54 45 20  .  }.} {{CREATE 
12ef0 54 41 42 4c 45 20 63 28 62 20 52 45 46 45 52 45  TABLE c(b REFERE
12f00 4e 43 45 53 20 70 28 61 29 29 7d 7d 0a 64 6f 5f  NCES p(a))}}.do_
12f10 74 65 73 74 20 65 5f 66 6b 65 79 2d 36 31 2e 32  test e_fkey-61.2
12f20 2e 33 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b  .3 {.  execsql {
12f30 20 50 52 41 47 4d 41 20 66 6f 72 65 69 67 6e 5f   PRAGMA foreign_
12f40 6b 65 79 73 20 3d 20 4f 4e 20 7d 0a 7d 20 7b 7d  keys = ON }.} {}
12f50 0a 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79  ..do_test e_fkey
12f60 2d 36 31 2e 33 2e 31 20 7b 0a 20 20 64 72 6f 70  -61.3.1 {.  drop
12f70 5f 61 6c 6c 5f 74 61 62 6c 65 73 0a 20 20 65 78  _all_tables.  ex
12f80 65 63 73 71 6c 20 7b 0a 20 20 20 20 43 52 45 41  ecsql {.    CREA
12f90 54 45 20 54 41 42 4c 45 20 70 28 61 20 55 4e 49  TE TABLE p(a UNI
12fa0 51 55 45 29 3b 0a 20 20 20 20 43 52 45 41 54 45  QUE);.    CREATE
12fb0 20 54 41 42 4c 45 20 63 28 62 20 52 45 46 45 52   TABLE c(b REFER
12fc0 45 4e 43 45 53 20 70 28 61 29 20 4f 4e 20 44 45  ENCES p(a) ON DE
12fd0 4c 45 54 45 20 53 45 54 20 4e 55 4c 4c 29 3b 0a  LETE SET NULL);.
12fe0 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20      INSERT INTO 
12ff0 70 20 56 41 4c 55 45 53 28 27 78 27 29 3b 0a 20  p VALUES('x');. 
13000 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 63     INSERT INTO c
13010 20 56 41 4c 55 45 53 28 27 78 27 29 3b 0a 20 20   VALUES('x');.  
13020 20 20 42 45 47 49 4e 3b 0a 20 20 20 20 20 20 44    BEGIN;.      D
13030 52 4f 50 20 54 41 42 4c 45 20 70 3b 0a 20 20 20  ROP TABLE p;.   
13040 20 20 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d     SELECT * FROM
13050 20 63 3b 0a 20 20 20 20 52 4f 4c 4c 42 41 43 4b   c;.    ROLLBACK
13060 3b 0a 20 20 7d 0a 7d 20 7b 7b 7d 7d 0a 64 6f 5f  ;.  }.} {{}}.do_
13070 74 65 73 74 20 65 5f 66 6b 65 79 2d 36 31 2e 33  test e_fkey-61.3
13080 2e 32 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b  .2 {.  execsql {
13090 0a 20 20 20 20 50 52 41 47 4d 41 20 66 6f 72 65  .    PRAGMA fore
130a0 69 67 6e 5f 6b 65 79 73 20 3d 20 4f 46 46 3b 0a  ign_keys = OFF;.
130b0 20 20 20 20 44 52 4f 50 20 54 41 42 4c 45 20 70      DROP TABLE p
130c0 3b 0a 20 20 20 20 53 45 4c 45 43 54 20 2a 20 46  ;.    SELECT * F
130d0 52 4f 4d 20 63 3b 0a 20 20 7d 0a 7d 20 7b 78 7d  ROM c;.  }.} {x}
130e0 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d  .do_test e_fkey-
130f0 36 31 2e 33 2e 33 20 7b 0a 20 20 65 78 65 63 73  61.3.3 {.  execs
13100 71 6c 20 7b 20 50 52 41 47 4d 41 20 66 6f 72 65  ql { PRAGMA fore
13110 69 67 6e 5f 6b 65 79 73 20 3d 20 4f 4e 20 7d 0a  ign_keys = ON }.
13120 7d 20 7b 7d 0a 0a 23 23 23 23 23 23 23 23 23 23  } {}..##########
13130 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
13140 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
13150 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
13160 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
13170 23 0a 23 23 23 20 53 45 43 54 49 4f 4e 20 36 3a  #.### SECTION 6:
13180 20 4c 69 6d 69 74 73 20 61 6e 64 20 55 6e 73 75   Limits and Unsu
13190 70 70 6f 72 74 65 64 20 46 65 61 74 75 72 65 73  pported Features
131a0 0a 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  .###############
131b0 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
131c0 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
131d0 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
131e0 23 23 23 23 23 23 23 23 23 23 23 23 0a 0a 23 2d  ############..#-
131f0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
13200 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
13210 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
13220 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
13230 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20 2f 2a 20 45 56  --------.# /* EV
13240 3a 20 52 2d 32 34 37 32 38 2d 31 33 32 33 30 20  : R-24728-13230 
13250 2a 2f 0a 23 20 2f 2a 20 45 56 3a 20 52 2d 32 34  */.# /* EV: R-24
13260 34 35 30 2d 34 36 31 37 34 20 2a 2f 0a 23 0a 23  450-46174 */.#.#
13270 20 54 65 73 74 20 74 68 61 74 20 4d 41 54 43 48   Test that MATCH
13280 20 63 6c 61 75 73 65 73 20 61 72 65 20 70 61 72   clauses are par
13290 73 65 64 2c 20 62 75 74 20 53 51 4c 69 74 65 20  sed, but SQLite 
132a0 74 72 65 61 74 73 20 65 76 65 72 79 20 66 6f 72  treats every for
132b0 65 69 67 6e 20 6b 65 79 0a 23 20 63 6f 6e 73 74  eign key.# const
132c0 72 61 69 6e 74 20 61 73 20 69 66 20 69 74 20 77  raint as if it w
132d0 65 72 65 20 22 4d 41 54 43 48 20 53 49 4d 50 4c  ere "MATCH SIMPL
132e0 45 22 2e 0a 23 0a 66 6f 72 65 61 63 68 20 7a 4d  E"..#.foreach zM
132f0 61 74 63 68 20 5b 6c 69 73 74 20 53 49 4d 50 4c  atch [list SIMPL
13300 45 20 50 41 52 54 49 41 4c 20 46 55 4c 4c 20 53  E PARTIAL FULL S
13310 69 6d 70 6c 65 20 70 61 72 54 49 41 4c 20 46 75  imple parTIAL Fu
13320 4c 4c 20 5d 20 7b 0a 20 20 64 72 6f 70 5f 61 6c  LL ] {.  drop_al
13330 6c 5f 74 61 62 6c 65 73 0a 20 20 64 6f 5f 74 65  l_tables.  do_te
13340 73 74 20 65 5f 66 6b 65 79 2d 36 32 2e 24 7a 4d  st e_fkey-62.$zM
13350 61 74 63 68 2e 31 20 7b 0a 20 20 20 20 65 78 65  atch.1 {.    exe
13360 63 73 71 6c 20 22 0a 20 20 20 20 20 20 43 52 45  csql ".      CRE
13370 41 54 45 20 54 41 42 4c 45 20 70 28 61 2c 20 62  ATE TABLE p(a, b
13380 2c 20 63 2c 20 50 52 49 4d 41 52 59 20 4b 45 59  , c, PRIMARY KEY
13390 28 62 2c 20 63 29 29 3b 0a 20 20 20 20 20 20 43  (b, c));.      C
133a0 52 45 41 54 45 20 54 41 42 4c 45 20 63 28 64 2c  REATE TABLE c(d,
133b0 20 65 2c 20 66 2c 20 46 4f 52 45 49 47 4e 20 4b   e, f, FOREIGN K
133c0 45 59 28 65 2c 20 66 29 20 52 45 46 45 52 45 4e  EY(e, f) REFEREN
133d0 43 45 53 20 70 20 4d 41 54 43 48 20 24 7a 4d 61  CES p MATCH $zMa
133e0 74 63 68 29 3b 0a 20 20 20 20 22 0a 20 20 7d 20  tch);.    ".  } 
133f0 7b 7d 0a 20 20 64 6f 5f 74 65 73 74 20 65 5f 66  {}.  do_test e_f
13400 6b 65 79 2d 36 32 2e 24 7a 4d 61 74 63 68 2e 32  key-62.$zMatch.2
13410 20 7b 0a 20 20 20 20 65 78 65 63 73 71 6c 20 7b   {.    execsql {
13420 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 70 20 56   INSERT INTO p V
13430 41 4c 55 45 53 28 31 2c 20 32 2c 20 33 29 20 20  ALUES(1, 2, 3)  
13440 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 23 20         }..    # 
13450 4d 41 54 43 48 20 53 49 4d 50 4c 45 20 62 65 68  MATCH SIMPLE beh
13460 61 76 69 6f 75 72 3a 20 41 6c 6c 6f 77 20 61 6e  aviour: Allow an
13470 79 20 63 68 69 6c 64 20 6b 65 79 20 74 68 61 74  y child key that
13480 20 63 6f 6e 74 61 69 6e 73 20 6f 6e 65 20 6f 72   contains one or
13490 20 6d 6f 72 65 0a 20 20 20 20 23 20 4e 55 4c 4c   more.    # NULL
134a0 20 76 61 6c 75 65 20 74 6f 20 62 65 20 69 6e 73   value to be ins
134b0 65 72 74 65 64 2e 20 4e 6f 6e 2d 4e 55 4c 4c 20  erted. Non-NULL 
134c0 76 61 6c 75 65 73 20 64 6f 20 6e 6f 74 20 68 61  values do not ha
134d0 76 65 20 74 6f 20 6d 61 70 20 74 6f 20 61 6e 79  ve to map to any
134e0 0a 20 20 20 20 23 20 70 61 72 65 6e 74 20 6b 65  .    # parent ke
134f0 79 20 76 61 6c 75 65 73 2c 20 73 6f 20 6c 6f 6e  y values, so lon
13500 67 20 61 73 20 61 74 20 6c 65 61 73 74 20 6f 6e  g as at least on
13510 65 20 66 69 65 6c 64 20 6f 66 20 74 68 65 20 63  e field of the c
13520 68 69 6c 64 20 6b 65 79 20 69 73 0a 20 20 20 20  hild key is.    
13530 23 20 4e 55 4c 4c 2e 0a 20 20 20 20 65 78 65 63  # NULL..    exec
13540 73 71 6c 20 7b 20 49 4e 53 45 52 54 20 49 4e 54  sql { INSERT INT
13550 4f 20 63 20 56 41 4c 55 45 53 28 27 77 27 2c 20  O c VALUES('w', 
13560 32 2c 20 33 29 20 20 20 20 20 20 20 7d 0a 20 20  2, 3)       }.  
13570 20 20 65 78 65 63 73 71 6c 20 7b 20 49 4e 53 45    execsql { INSE
13580 52 54 20 49 4e 54 4f 20 63 20 56 41 4c 55 45 53  RT INTO c VALUES
13590 28 27 78 27 2c 20 27 78 27 2c 20 4e 55 4c 4c 29  ('x', 'x', NULL)
135a0 20 20 7d 0a 20 20 20 20 65 78 65 63 73 71 6c 20    }.    execsql 
135b0 7b 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 63 20  { INSERT INTO c 
135c0 56 41 4c 55 45 53 28 27 79 27 2c 20 4e 55 4c 4c  VALUES('y', NULL
135d0 2c 20 27 78 27 29 20 20 7d 0a 20 20 20 20 65 78  , 'x')  }.    ex
135e0 65 63 73 71 6c 20 7b 20 49 4e 53 45 52 54 20 49  ecsql { INSERT I
135f0 4e 54 4f 20 63 20 56 41 4c 55 45 53 28 27 7a 27  NTO c VALUES('z'
13600 2c 20 4e 55 4c 4c 2c 20 4e 55 4c 4c 29 20 7d 0a  , NULL, NULL) }.
13610 0a 20 20 20 20 23 20 43 68 65 63 6b 20 74 68 61  .    # Check tha
13620 74 20 74 68 65 20 46 4b 20 69 73 20 65 6e 66 6f  t the FK is enfo
13630 72 63 65 64 20 70 72 6f 70 65 72 6c 79 20 69 66  rced properly if
13640 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 4e 55   there are no NU
13650 4c 4c 20 76 61 6c 75 65 73 20 0a 20 20 20 20 23  LL values .    #
13660 20 69 6e 20 74 68 65 20 63 68 69 6c 64 20 6b 65   in the child ke
13670 79 20 63 6f 6c 75 6d 6e 73 2e 0a 20 20 20 20 63  y columns..    c
13680 61 74 63 68 73 71 6c 20 7b 20 49 4e 53 45 52 54  atchsql { INSERT
13690 20 49 4e 54 4f 20 63 20 56 41 4c 55 45 53 28 27   INTO c VALUES('
136a0 61 27 2c 20 32 2c 20 34 29 20 7d 0a 20 20 7d 20  a', 2, 4) }.  } 
136b0 7b 31 20 7b 66 6f 72 65 69 67 6e 20 6b 65 79 20  {1 {foreign key 
136c0 63 6f 6e 73 74 72 61 69 6e 74 20 66 61 69 6c 65  constraint faile
136d0 64 7d 7d 0a 7d 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d  d}}.}..#--------
136e0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
136f0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
13700 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
13710 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
13720 2d 0a 23 20 2f 2a 20 45 56 3a 20 52 2d 32 31 35  -.# /* EV: R-215
13730 39 39 2d 31 36 30 33 38 20 2a 2f 0a 23 0a 23 20  99-16038 */.#.# 
13740 54 65 73 74 20 74 68 61 74 20 53 51 4c 69 74 65  Test that SQLite
13750 20 64 6f 65 73 20 6e 6f 74 20 73 75 70 70 6f 72   does not suppor
13760 74 20 74 68 65 20 53 45 54 20 43 4f 4e 53 54 52  t the SET CONSTR
13770 41 49 4e 54 20 73 74 61 74 65 6d 65 6e 74 2e 20  AINT statement. 
13780 41 6e 64 0a 23 20 74 68 61 74 20 69 74 20 69 73  And.# that it is
13790 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 63 72 65   possible to cre
137a0 61 74 65 20 62 6f 74 68 20 69 6d 6d 65 64 69 61  ate both immedia
137b0 74 65 20 61 6e 64 20 64 65 66 65 72 72 65 64 20  te and deferred 
137c0 63 6f 6e 73 74 72 61 69 6e 74 73 2e 0a 23 0a 64  constraints..#.d
137d0 72 6f 70 5f 61 6c 6c 5f 74 61 62 6c 65 73 0a 64  rop_all_tables.d
137e0 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 36 32  o_test e_fkey-62
137f0 2e 31 20 7b 0a 20 20 63 61 74 63 68 73 71 6c 20  .1 {.  catchsql 
13800 7b 20 53 45 54 20 43 4f 4e 53 54 52 41 49 4e 54  { SET CONSTRAINT
13810 53 20 41 4c 4c 20 49 4d 4d 45 44 49 41 54 45 20  S ALL IMMEDIATE 
13820 7d 0a 7d 20 7b 31 20 7b 6e 65 61 72 20 22 53 45  }.} {1 {near "SE
13830 54 22 3a 20 73 79 6e 74 61 78 20 65 72 72 6f 72  T": syntax error
13840 7d 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65  }}.do_test e_fke
13850 79 2d 36 32 2e 32 20 7b 0a 20 20 63 61 74 63 68  y-62.2 {.  catch
13860 73 71 6c 20 7b 20 53 45 54 20 43 4f 4e 53 54 52  sql { SET CONSTR
13870 41 49 4e 54 53 20 41 4c 4c 20 44 45 46 45 52 52  AINTS ALL DEFERR
13880 45 44 20 7d 0a 7d 20 7b 31 20 7b 6e 65 61 72 20  ED }.} {1 {near 
13890 22 53 45 54 22 3a 20 73 79 6e 74 61 78 20 65 72  "SET": syntax er
138a0 72 6f 72 7d 7d 0a 0a 64 6f 5f 74 65 73 74 20 65  ror}}..do_test e
138b0 5f 66 6b 65 79 2d 36 32 2e 33 20 7b 0a 20 20 65  _fkey-62.3 {.  e
138c0 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 43 52 45  xecsql {.    CRE
138d0 41 54 45 20 54 41 42 4c 45 20 70 28 61 2c 20 62  ATE TABLE p(a, b
138e0 2c 20 50 52 49 4d 41 52 59 20 4b 45 59 28 61 2c  , PRIMARY KEY(a,
138f0 20 62 29 29 3b 0a 20 20 20 20 43 52 45 41 54 45   b));.    CREATE
13900 20 54 41 42 4c 45 20 63 64 28 63 2c 20 64 2c 20   TABLE cd(c, d, 
13910 0a 20 20 20 20 20 20 46 4f 52 45 49 47 4e 20 4b  .      FOREIGN K
13920 45 59 28 63 2c 20 64 29 20 52 45 46 45 52 45 4e  EY(c, d) REFEREN
13930 43 45 53 20 70 20 44 45 46 45 52 52 41 42 4c 45  CES p DEFERRABLE
13940 20 49 4e 49 54 49 41 4c 4c 59 20 44 45 46 45 52   INITIALLY DEFER
13950 52 45 44 29 3b 0a 20 20 20 20 43 52 45 41 54 45  RED);.    CREATE
13960 20 54 41 42 4c 45 20 63 69 28 63 2c 20 64 2c 20   TABLE ci(c, d, 
13970 0a 20 20 20 20 20 20 46 4f 52 45 49 47 4e 20 4b  .      FOREIGN K
13980 45 59 28 63 2c 20 64 29 20 52 45 46 45 52 45 4e  EY(c, d) REFEREN
13990 43 45 53 20 70 20 44 45 46 45 52 52 41 42 4c 45  CES p DEFERRABLE
139a0 20 49 4e 49 54 49 41 4c 4c 59 20 49 4d 4d 45 44   INITIALLY IMMED
139b0 49 41 54 45 29 3b 0a 20 20 20 20 42 45 47 49 4e  IATE);.    BEGIN
139c0 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a 64 6f 5f 74 65  ;.  }.} {}.do_te
139d0 73 74 20 65 5f 66 6b 65 79 2d 36 32 2e 34 20 7b  st e_fkey-62.4 {
139e0 0a 20 20 63 61 74 63 68 73 71 6c 20 7b 20 49 4e  .  catchsql { IN
139f0 53 45 52 54 20 49 4e 54 4f 20 63 69 20 56 41 4c  SERT INTO ci VAL
13a00 55 45 53 28 27 78 27 2c 20 27 79 27 29 20 7d 0a  UES('x', 'y') }.
13a10 7d 20 7b 31 20 7b 66 6f 72 65 69 67 6e 20 6b 65  } {1 {foreign ke
13a20 79 20 63 6f 6e 73 74 72 61 69 6e 74 20 66 61 69  y constraint fai
13a30 6c 65 64 7d 7d 0a 64 6f 5f 74 65 73 74 20 65 5f  led}}.do_test e_
13a40 66 6b 65 79 2d 36 32 2e 35 20 7b 0a 20 20 63 61  fkey-62.5 {.  ca
13a50 74 63 68 73 71 6c 20 7b 20 49 4e 53 45 52 54 20  tchsql { INSERT 
13a60 49 4e 54 4f 20 63 64 20 56 41 4c 55 45 53 28 27  INTO cd VALUES('
13a70 78 27 2c 20 27 79 27 29 20 7d 0a 7d 20 7b 30 20  x', 'y') }.} {0 
13a80 7b 7d 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b  {}}.do_test e_fk
13a90 65 79 2d 36 32 2e 36 20 7b 0a 20 20 63 61 74 63  ey-62.6 {.  catc
13aa0 68 73 71 6c 20 7b 20 43 4f 4d 4d 49 54 20 7d 0a  hsql { COMMIT }.
13ab0 7d 20 7b 31 20 7b 66 6f 72 65 69 67 6e 20 6b 65  } {1 {foreign ke
13ac0 79 20 63 6f 6e 73 74 72 61 69 6e 74 20 66 61 69  y constraint fai
13ad0 6c 65 64 7d 7d 0a 64 6f 5f 74 65 73 74 20 65 5f  led}}.do_test e_
13ae0 66 6b 65 79 2d 36 32 2e 37 20 7b 0a 20 20 65 78  fkey-62.7 {.  ex
13af0 65 63 73 71 6c 20 7b 20 0a 20 20 20 20 44 45 4c  ecsql { .    DEL
13b00 45 54 45 20 46 52 4f 4d 20 63 64 3b 0a 20 20 20  ETE FROM cd;.   
13b10 20 43 4f 4d 4d 49 54 3b 0a 20 20 7d 0a 7d 20 7b   COMMIT;.  }.} {
13b20 7d 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  }..#------------
13b30 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
13b40 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
13b50 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
13b60 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20  -------------.# 
13b70 2f 2a 20 45 56 3a 20 52 2d 34 32 32 36 34 2d 33  /* EV: R-42264-3
13b80 30 35 30 33 20 2a 2f 0a 23 0a 23 20 54 65 73 74  0503 */.#.# Test
13b90 20 74 68 61 74 20 74 68 65 20 6d 61 78 69 6d 75   that the maximu
13ba0 6d 20 72 65 63 75 72 73 69 6f 6e 20 64 65 70 74  m recursion dept
13bb0 68 20 6f 66 20 66 6f 72 65 69 67 6e 20 6b 65 79  h of foreign key
13bc0 20 61 63 74 69 6f 6e 20 70 72 6f 67 72 61 6d 73   action programs
13bd0 20 69 73 0a 23 20 67 6f 76 65 72 6e 65 64 20 62   is.# governed b
13be0 79 20 74 68 65 20 53 51 4c 49 54 45 5f 4d 41 58  y the SQLITE_MAX
13bf0 5f 54 52 49 47 47 45 52 5f 44 45 50 54 48 20 61  _TRIGGER_DEPTH a
13c00 6e 64 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f  nd SQLITE_LIMIT_
13c10 54 52 49 47 47 45 52 5f 44 45 50 54 48 0a 23 20  TRIGGER_DEPTH.# 
13c20 73 65 74 74 69 6e 67 73 2e 0a 23 0a 70 72 6f 63  settings..#.proc
13c30 20 74 65 73 74 5f 6f 6e 5f 64 65 6c 65 74 65 5f   test_on_delete_
13c40 72 65 63 75 72 73 69 6f 6e 20 7b 6c 69 6d 69 74  recursion {limit
13c50 7d 20 7b 0a 20 20 64 72 6f 70 5f 61 6c 6c 5f 74  } {.  drop_all_t
13c60 61 62 6c 65 73 0a 20 20 65 78 65 63 73 71 6c 20  ables.  execsql 
13c70 7b 20 0a 20 20 20 20 42 45 47 49 4e 3b 0a 20 20  { .    BEGIN;.  
13c80 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 74    CREATE TABLE t
13c90 30 28 61 20 50 52 49 4d 41 52 59 20 4b 45 59 2c  0(a PRIMARY KEY,
13ca0 20 62 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20   b);.    INSERT 
13cb0 49 4e 54 4f 20 74 30 20 56 41 4c 55 45 53 28 27  INTO t0 VALUES('
13cc0 78 30 27 2c 20 4e 55 4c 4c 29 3b 0a 20 20 7d 0a  x0', NULL);.  }.
13cd0 20 20 66 6f 72 20 7b 73 65 74 20 69 20 31 7d 20    for {set i 1} 
13ce0 7b 24 69 20 3c 3d 20 24 6c 69 6d 69 74 7d 20 7b  {$i <= $limit} {
13cf0 69 6e 63 72 20 69 7d 20 7b 0a 20 20 20 20 65 78  incr i} {.    ex
13d00 65 63 73 71 6c 20 22 0a 20 20 20 20 20 20 43 52  ecsql ".      CR
13d10 45 41 54 45 20 54 41 42 4c 45 20 74 24 69 20 28  EATE TABLE t$i (
13d20 0a 20 20 20 20 20 20 20 20 61 20 50 52 49 4d 41  .        a PRIMA
13d30 52 59 20 4b 45 59 2c 20 62 20 52 45 46 45 52 45  RY KEY, b REFERE
13d40 4e 43 45 53 20 74 5b 65 78 70 72 20 24 69 2d 31  NCES t[expr $i-1
13d50 5d 20 4f 4e 20 44 45 4c 45 54 45 20 43 41 53 43  ] ON DELETE CASC
13d60 41 44 45 0a 20 20 20 20 20 20 29 3b 0a 20 20 20  ADE.      );.   
13d70 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74     INSERT INTO t
13d80 24 69 20 56 41 4c 55 45 53 28 27 78 24 69 27 2c  $i VALUES('x$i',
13d90 20 27 78 5b 65 78 70 72 20 24 69 2d 31 5d 27 29   'x[expr $i-1]')
13da0 3b 0a 20 20 20 20 22 0a 20 20 7d 0a 20 20 65 78  ;.    ".  }.  ex
13db0 65 63 73 71 6c 20 43 4f 4d 4d 49 54 0a 20 20 63  ecsql COMMIT.  c
13dc0 61 74 63 68 73 71 6c 20 22 0a 20 20 20 20 44 45  atchsql ".    DE
13dd0 4c 45 54 45 20 46 52 4f 4d 20 74 30 3b 0a 20 20  LETE FROM t0;.  
13de0 20 20 53 45 4c 45 43 54 20 63 6f 75 6e 74 28 2a    SELECT count(*
13df0 29 20 46 52 4f 4d 20 74 24 6c 69 6d 69 74 3b 0a  ) FROM t$limit;.
13e00 20 20 22 0a 7d 0a 70 72 6f 63 20 74 65 73 74 5f    ".}.proc test_
13e10 6f 6e 5f 75 70 64 61 74 65 5f 72 65 63 75 72 73  on_update_recurs
13e20 69 6f 6e 20 7b 6c 69 6d 69 74 7d 20 7b 0a 20 20  ion {limit} {.  
13e30 64 72 6f 70 5f 61 6c 6c 5f 74 61 62 6c 65 73 0a  drop_all_tables.
13e40 20 20 65 78 65 63 73 71 6c 20 7b 20 0a 20 20 20    execsql { .   
13e50 20 42 45 47 49 4e 3b 0a 20 20 20 20 43 52 45 41   BEGIN;.    CREA
13e60 54 45 20 54 41 42 4c 45 20 74 30 28 61 20 50 52  TE TABLE t0(a PR
13e70 49 4d 41 52 59 20 4b 45 59 29 3b 0a 20 20 20 20  IMARY KEY);.    
13e80 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 30 20 56  INSERT INTO t0 V
13e90 41 4c 55 45 53 28 27 78 78 78 27 29 3b 0a 20 20  ALUES('xxx');.  
13ea0 7d 0a 20 20 66 6f 72 20 7b 73 65 74 20 69 20 31  }.  for {set i 1
13eb0 7d 20 7b 24 69 20 3c 3d 20 24 6c 69 6d 69 74 7d  } {$i <= $limit}
13ec0 20 7b 69 6e 63 72 20 69 7d 20 7b 0a 20 20 20 20   {incr i} {.    
13ed0 73 65 74 20 6a 20 5b 65 78 70 72 20 24 69 2d 31  set j [expr $i-1
13ee0 5d 0a 0a 20 20 20 20 65 78 65 63 73 71 6c 20 22  ]..    execsql "
13ef0 0a 20 20 20 20 20 20 43 52 45 41 54 45 20 54 41  .      CREATE TA
13f00 42 4c 45 20 74 24 69 20 28 61 20 50 52 49 4d 41  BLE t$i (a PRIMA
13f10 52 59 20 4b 45 59 20 52 45 46 45 52 45 4e 43 45  RY KEY REFERENCE
13f20 53 20 74 24 6a 20 4f 4e 20 55 50 44 41 54 45 20  S t$j ON UPDATE 
13f30 43 41 53 43 41 44 45 29 3b 0a 20 20 20 20 20 20  CASCADE);.      
13f40 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 24 69 20  INSERT INTO t$i 
13f50 56 41 4c 55 45 53 28 27 78 78 78 27 29 3b 0a 20  VALUES('xxx');. 
13f60 20 20 20 22 0a 20 20 7d 0a 20 20 65 78 65 63 73     ".  }.  execs
13f70 71 6c 20 43 4f 4d 4d 49 54 0a 20 20 63 61 74 63  ql COMMIT.  catc
13f80 68 73 71 6c 20 22 0a 20 20 20 20 55 50 44 41 54  hsql ".    UPDAT
13f90 45 20 74 30 20 53 45 54 20 61 20 3d 20 27 79 79  E t0 SET a = 'yy
13fa0 79 27 3b 0a 20 20 20 20 53 45 4c 45 43 54 20 4e  y';.    SELECT N
13fb0 4f 54 20 28 61 3d 27 79 79 79 27 29 20 46 52 4f  OT (a='yyy') FRO
13fc0 4d 20 74 24 6c 69 6d 69 74 3b 0a 20 20 22 0a 7d  M t$limit;.  ".}
13fd0 0a 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79  ..do_test e_fkey
13fe0 2d 36 33 2e 31 2e 31 20 7b 0a 20 20 74 65 73 74  -63.1.1 {.  test
13ff0 5f 6f 6e 5f 64 65 6c 65 74 65 5f 72 65 63 75 72  _on_delete_recur
14000 73 69 6f 6e 20 24 53 51 4c 49 54 45 5f 4d 41 58  sion $SQLITE_MAX
14010 5f 54 52 49 47 47 45 52 5f 44 45 50 54 48 0a 7d  _TRIGGER_DEPTH.}
14020 20 7b 30 20 30 7d 0a 64 6f 5f 74 65 73 74 20 65   {0 0}.do_test e
14030 5f 66 6b 65 79 2d 36 33 2e 31 2e 32 20 7b 0a 20  _fkey-63.1.2 {. 
14040 20 74 65 73 74 5f 6f 6e 5f 64 65 6c 65 74 65 5f   test_on_delete_
14050 72 65 63 75 72 73 69 6f 6e 20 5b 65 78 70 72 20  recursion [expr 
14060 24 53 51 4c 49 54 45 5f 4d 41 58 5f 54 52 49 47  $SQLITE_MAX_TRIG
14070 47 45 52 5f 44 45 50 54 48 2b 31 5d 0a 7d 20 7b  GER_DEPTH+1].} {
14080 31 20 7b 74 6f 6f 20 6d 61 6e 79 20 6c 65 76 65  1 {too many leve
14090 6c 73 20 6f 66 20 74 72 69 67 67 65 72 20 72 65  ls of trigger re
140a0 63 75 72 73 69 6f 6e 7d 7d 0a 64 6f 5f 74 65 73  cursion}}.do_tes
140b0 74 20 65 5f 66 6b 65 79 2d 36 33 2e 31 2e 33 20  t e_fkey-63.1.3 
140c0 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6c 69 6d 69  {.  sqlite3_limi
140d0 74 20 64 62 20 53 51 4c 49 54 45 5f 4c 49 4d 49  t db SQLITE_LIMI
140e0 54 5f 54 52 49 47 47 45 52 5f 44 45 50 54 48 20  T_TRIGGER_DEPTH 
140f0 35 0a 20 20 74 65 73 74 5f 6f 6e 5f 64 65 6c 65  5.  test_on_dele
14100 74 65 5f 72 65 63 75 72 73 69 6f 6e 20 35 0a 7d  te_recursion 5.}
14110 20 7b 30 20 30 7d 0a 64 6f 5f 74 65 73 74 20 65   {0 0}.do_test e
14120 5f 66 6b 65 79 2d 36 33 2e 31 2e 34 20 7b 0a 20  _fkey-63.1.4 {. 
14130 20 74 65 73 74 5f 6f 6e 5f 64 65 6c 65 74 65 5f   test_on_delete_
14140 72 65 63 75 72 73 69 6f 6e 20 36 0a 7d 20 7b 31  recursion 6.} {1
14150 20 7b 74 6f 6f 20 6d 61 6e 79 20 6c 65 76 65 6c   {too many level
14160 73 20 6f 66 20 74 72 69 67 67 65 72 20 72 65 63  s of trigger rec
14170 75 72 73 69 6f 6e 7d 7d 0a 64 6f 5f 74 65 73 74  ursion}}.do_test
14180 20 65 5f 66 6b 65 79 2d 36 33 2e 31 2e 35 20 7b   e_fkey-63.1.5 {
14190 0a 20 20 73 71 6c 69 74 65 33 5f 6c 69 6d 69 74  .  sqlite3_limit
141a0 20 64 62 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54   db SQLITE_LIMIT
141b0 5f 54 52 49 47 47 45 52 5f 44 45 50 54 48 20 31  _TRIGGER_DEPTH 1
141c0 30 30 30 30 30 30 0a 7d 20 7b 35 7d 0a 64 6f 5f  000000.} {5}.do_
141d0 74 65 73 74 20 65 5f 66 6b 65 79 2d 36 33 2e 32  test e_fkey-63.2
141e0 2e 31 20 7b 0a 20 20 74 65 73 74 5f 6f 6e 5f 75  .1 {.  test_on_u
141f0 70 64 61 74 65 5f 72 65 63 75 72 73 69 6f 6e 20  pdate_recursion 
14200 24 53 51 4c 49 54 45 5f 4d 41 58 5f 54 52 49 47  $SQLITE_MAX_TRIG
14210 47 45 52 5f 44 45 50 54 48 0a 7d 20 7b 30 20 30  GER_DEPTH.} {0 0
14220 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79  }.do_test e_fkey
14230 2d 36 33 2e 32 2e 32 20 7b 0a 20 20 74 65 73 74  -63.2.2 {.  test
14240 5f 6f 6e 5f 75 70 64 61 74 65 5f 72 65 63 75 72  _on_update_recur
14250 73 69 6f 6e 20 5b 65 78 70 72 20 24 53 51 4c 49  sion [expr $SQLI
14260 54 45 5f 4d 41 58 5f 54 52 49 47 47 45 52 5f 44  TE_MAX_TRIGGER_D
14270 45 50 54 48 2b 31 5d 0a 7d 20 7b 31 20 7b 74 6f  EPTH+1].} {1 {to
14280 6f 20 6d 61 6e 79 20 6c 65 76 65 6c 73 20 6f 66  o many levels of
14290 20 74 72 69 67 67 65 72 20 72 65 63 75 72 73 69   trigger recursi
142a0 6f 6e 7d 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66  on}}.do_test e_f
142b0 6b 65 79 2d 36 33 2e 32 2e 33 20 7b 0a 20 20 73  key-63.2.3 {.  s
142c0 71 6c 69 74 65 33 5f 6c 69 6d 69 74 20 64 62 20  qlite3_limit db 
142d0 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 54 52 49  SQLITE_LIMIT_TRI
142e0 47 47 45 52 5f 44 45 50 54 48 20 35 0a 20 20 74  GGER_DEPTH 5.  t
142f0 65 73 74 5f 6f 6e 5f 75 70 64 61 74 65 5f 72 65  est_on_update_re
14300 63 75 72 73 69 6f 6e 20 35 0a 7d 20 7b 30 20 30  cursion 5.} {0 0
14310 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79  }.do_test e_fkey
14320 2d 36 33 2e 32 2e 34 20 7b 0a 20 20 74 65 73 74  -63.2.4 {.  test
14330 5f 6f 6e 5f 75 70 64 61 74 65 5f 72 65 63 75 72  _on_update_recur
14340 73 69 6f 6e 20 36 0a 7d 20 7b 31 20 7b 74 6f 6f  sion 6.} {1 {too
14350 20 6d 61 6e 79 20 6c 65 76 65 6c 73 20 6f 66 20   many levels of 
14360 74 72 69 67 67 65 72 20 72 65 63 75 72 73 69 6f  trigger recursio
14370 6e 7d 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b  n}}.do_test e_fk
14380 65 79 2d 36 33 2e 32 2e 35 20 7b 0a 20 20 73 71  ey-63.2.5 {.  sq
14390 6c 69 74 65 33 5f 6c 69 6d 69 74 20 64 62 20 53  lite3_limit db S
143a0 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 54 52 49 47  QLITE_LIMIT_TRIG
143b0 47 45 52 5f 44 45 50 54 48 20 31 30 30 30 30 30  GER_DEPTH 100000
143c0 30 0a 7d 20 7b 35 7d 0a 0a 23 2d 2d 2d 2d 2d 2d  0.} {5}..#------
143d0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
143e0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
143f0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
14400 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
14410 2d 2d 2d 0a 23 20 2f 2a 20 45 56 3a 20 52 2d 35  ---.# /* EV: R-5
14420 31 37 36 39 2d 33 32 37 33 30 20 2a 2f 0a 23 0a  1769-32730 */.#.
14430 23 20 54 68 65 20 73 65 74 74 69 6e 67 20 6f 66  # The setting of
14440 20 74 68 65 20 72 65 63 75 72 73 69 76 65 5f 74   the recursive_t
14450 72 69 67 67 65 72 73 20 70 72 61 67 6d 61 20 64  riggers pragma d
14460 6f 65 73 20 6e 6f 74 20 61 66 66 65 63 74 20 66  oes not affect f
14470 6f 72 65 69 67 6e 0a 23 20 6b 65 79 20 61 63 74  oreign.# key act
14480 69 6f 6e 73 2e 0a 23 0a 66 6f 72 65 61 63 68 20  ions..#.foreach 
14490 72 65 63 75 72 73 69 76 65 5f 74 72 69 67 67 65  recursive_trigge
144a0 72 73 5f 73 65 74 74 69 6e 67 20 5b 6c 69 73 74  rs_setting [list
144b0 20 30 20 31 20 4f 4e 20 4f 46 46 5d 20 7b 0a 20   0 1 ON OFF] {. 
144c0 20 64 72 6f 70 5f 61 6c 6c 5f 74 61 62 6c 65 73   drop_all_tables
144d0 0a 20 20 65 78 65 63 73 71 6c 20 22 50 52 41 47  .  execsql "PRAG
144e0 4d 41 20 72 65 63 75 72 73 69 76 65 5f 74 72 69  MA recursive_tri
144f0 67 67 65 72 73 20 3d 20 24 72 65 63 75 72 73 69  ggers = $recursi
14500 76 65 5f 74 72 69 67 67 65 72 73 5f 73 65 74 74  ve_triggers_sett
14510 69 6e 67 22 0a 0a 20 20 64 6f 5f 74 65 73 74 20  ing"..  do_test 
14520 65 5f 66 6b 65 79 2d 36 34 2e 24 72 65 63 75 72  e_fkey-64.$recur
14530 73 69 76 65 5f 74 72 69 67 67 65 72 73 5f 73 65  sive_triggers_se
14540 74 74 69 6e 67 2e 31 20 7b 0a 20 20 20 20 65 78  tting.1 {.    ex
14550 65 63 73 71 6c 20 7b 0a 20 20 20 20 20 20 43 52  ecsql {.      CR
14560 45 41 54 45 20 54 41 42 4c 45 20 74 31 28 61 20  EATE TABLE t1(a 
14570 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 62 20 52  PRIMARY KEY, b R
14580 45 46 45 52 45 4e 43 45 53 20 74 31 20 4f 4e 20  EFERENCES t1 ON 
14590 44 45 4c 45 54 45 20 43 41 53 43 41 44 45 29 3b  DELETE CASCADE);
145a0 0a 20 20 20 20 20 20 49 4e 53 45 52 54 20 49 4e  .      INSERT IN
145b0 54 4f 20 74 31 20 56 41 4c 55 45 53 28 31 2c 20  TO t1 VALUES(1, 
145c0 4e 55 4c 4c 29 3b 0a 20 20 20 20 20 20 49 4e 53  NULL);.      INS
145d0 45 52 54 20 49 4e 54 4f 20 74 31 20 56 41 4c 55  ERT INTO t1 VALU
145e0 45 53 28 32 2c 20 31 29 3b 0a 20 20 20 20 20 20  ES(2, 1);.      
145f0 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 31 20 56  INSERT INTO t1 V
14600 41 4c 55 45 53 28 33 2c 20 32 29 3b 0a 20 20 20  ALUES(3, 2);.   
14610 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74     INSERT INTO t
14620 31 20 56 41 4c 55 45 53 28 34 2c 20 33 29 3b 0a  1 VALUES(4, 3);.
14630 20 20 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54        INSERT INT
14640 4f 20 74 31 20 56 41 4c 55 45 53 28 35 2c 20 34  O t1 VALUES(5, 4
14650 29 3b 0a 20 20 20 20 20 20 53 45 4c 45 43 54 20  );.      SELECT 
14660 63 6f 75 6e 74 28 2a 29 20 46 52 4f 4d 20 74 31  count(*) FROM t1
14670 3b 0a 20 20 20 20 7d 0a 20 20 7d 20 7b 35 7d 0a  ;.    }.  } {5}.
14680 20 20 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79    do_test e_fkey
14690 2d 36 34 2e 24 72 65 63 75 72 73 69 76 65 5f 74  -64.$recursive_t
146a0 72 69 67 67 65 72 73 5f 73 65 74 74 69 6e 67 2e  riggers_setting.
146b0 32 20 7b 0a 20 20 20 20 65 78 65 63 73 71 6c 20  2 {.    execsql 
146c0 7b 20 53 45 4c 45 43 54 20 63 6f 75 6e 74 28 2a  { SELECT count(*
146d0 29 20 46 52 4f 4d 20 74 31 20 57 48 45 52 45 20  ) FROM t1 WHERE 
146e0 61 20 3d 20 31 20 7d 0a 20 20 7d 20 7b 31 7d 0a  a = 1 }.  } {1}.
146f0 20 20 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79    do_test e_fkey
14700 2d 36 34 2e 24 72 65 63 75 72 73 69 76 65 5f 74  -64.$recursive_t
14710 72 69 67 67 65 72 73 5f 73 65 74 74 69 6e 67 2e  riggers_setting.
14720 33 20 7b 0a 20 20 20 20 65 78 65 63 73 71 6c 20  3 {.    execsql 
14730 7b 20 0a 20 20 20 20 20 20 44 45 4c 45 54 45 20  { .      DELETE 
14740 46 52 4f 4d 20 74 31 20 57 48 45 52 45 20 61 20  FROM t1 WHERE a 
14750 3d 20 31 3b 0a 20 20 20 20 20 20 53 45 4c 45 43  = 1;.      SELEC
14760 54 20 63 6f 75 6e 74 28 2a 29 20 46 52 4f 4d 20  T count(*) FROM 
14770 74 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 20 7b 30  t1;.    }.  } {0
14780 7d 0a 7d 0a 0a 66 69 6e 69 73 68 5f 74 65 73 74  }.}..finish_test
14790 0a                                               .