/ Hex Artifact Content
Login

Artifact 372ed0d5465dc32385a32e549a0e6b339025b5c1:


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 23 23 23 23 23 23 23 23 23 23 23 23  cl..############
03d0: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
03e0: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
03f0: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
0400: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 0a  ###############.
0410: 23 23 23 20 53 45 43 54 49 4f 4e 20 32 3a 20 45  ### SECTION 2: E
0420: 6e 61 62 6c 69 6e 67 20 46 6f 72 65 69 67 6e 20  nabling Foreign 
0430: 4b 65 79 20 53 75 70 70 6f 72 74 0a 23 23 23 23  Key Support.####
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: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
0470: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
0480: 23 23 23 23 23 23 23 0a 0a 23 2d 2d 2d 2d 2d 2d  #######..#------
0490: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
04a0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
04b0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
04c0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
04d0: 2d 2d 2d 0a 23 20 2f 2a 20 45 56 3a 20 52 2d 33  ---.# /* EV: R-3
04e0: 33 37 31 30 2d 35 36 33 34 34 20 2a 2f 0a 23 0a  3710-56344 */.#.
04f0: 23 20 54 65 73 74 20 62 75 69 6c 64 73 20 6e 65  # Test builds ne
0500: 69 74 68 65 72 20 4f 4d 49 54 5f 46 4f 52 45 49  ither OMIT_FOREI
0510: 47 4e 5f 4b 45 59 20 6f 72 20 4f 4d 49 54 5f 54  GN_KEY or OMIT_T
0520: 52 49 47 47 45 52 20 64 65 66 69 6e 65 64 20 68  RIGGER defined h
0530: 61 76 65 20 0a 23 20 66 6f 72 65 69 67 6e 20 6b  ave .# foreign k
0540: 65 79 20 66 75 6e 63 74 69 6f 6e 61 6c 69 74 79  ey functionality
0550: 2e 0a 23 0a 69 66 63 61 70 61 62 6c 65 20 74 72  ..#.ifcapable tr
0560: 69 67 67 65 72 26 26 66 6f 72 65 69 67 6e 6b 65  igger&&foreignke
0570: 79 20 7b 0a 20 20 64 6f 5f 74 65 73 74 20 65 5f  y {.  do_test e_
0580: 66 6b 65 79 2d 34 39 20 7b 0a 20 20 20 20 65 78  fkey-49 {.    ex
0590: 65 63 73 71 6c 20 7b 0a 20 20 20 20 20 20 50 52  ecsql {.      PR
05a0: 41 47 4d 41 20 66 6f 72 65 69 67 6e 5f 6b 65 79  AGMA foreign_key
05b0: 73 20 3d 20 4f 4e 3b 0a 20 20 20 20 20 20 43 52  s = ON;.      CR
05c0: 45 41 54 45 20 54 41 42 4c 45 20 70 28 69 20 50  EATE TABLE p(i P
05d0: 52 49 4d 41 52 59 20 4b 45 59 29 3b 0a 20 20 20  RIMARY KEY);.   
05e0: 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20     CREATE TABLE 
05f0: 63 28 6a 20 52 45 46 45 52 45 4e 43 45 53 20 70  c(j REFERENCES p
0600: 20 4f 4e 20 55 50 44 41 54 45 20 43 41 53 43 41   ON UPDATE CASCA
0610: 44 45 29 3b 0a 20 20 20 20 20 20 49 4e 53 45 52  DE);.      INSER
0620: 54 20 49 4e 54 4f 20 70 20 56 41 4c 55 45 53 28  T INTO p VALUES(
0630: 27 68 65 6c 6c 6f 27 29 3b 0a 20 20 20 20 20 20  'hello');.      
0640: 49 4e 53 45 52 54 20 49 4e 54 4f 20 63 20 56 41  INSERT INTO c VA
0650: 4c 55 45 53 28 27 68 65 6c 6c 6f 27 29 3b 0a 20  LUES('hello');. 
0660: 20 20 20 20 20 55 50 44 41 54 45 20 70 20 53 45       UPDATE p SE
0670: 54 20 69 20 3d 20 27 77 6f 72 6c 64 27 3b 0a 20  T i = 'world';. 
0680: 20 20 20 20 20 53 45 4c 45 43 54 20 2a 20 46 52       SELECT * FR
0690: 4f 4d 20 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 20  OM c;.    }.  } 
06a0: 7b 77 6f 72 6c 64 7d 0a 7d 0a 0a 23 2d 2d 2d 2d  {world}.}..#----
06b0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
06c0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
06d0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
06e0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
06f0: 2d 2d 2d 2d 2d 0a 23 20 2f 2a 20 45 56 3a 20 52  -----.# /* EV: R
0700: 2d 34 34 36 39 37 2d 36 31 35 34 33 20 2a 2f 0a  -44697-61543 */.
0710: 23 0a 23 20 54 65 73 74 20 74 68 65 20 65 66 66  #.# Test the eff
0720: 65 63 74 73 20 6f 66 20 64 65 66 69 6e 69 6e 67  ects of defining
0730: 20 4f 4d 49 54 5f 54 52 49 47 47 45 52 20 62 75   OMIT_TRIGGER bu
0740: 74 20 6e 6f 74 20 4f 4d 49 54 5f 46 4f 52 45 49  t not OMIT_FOREI
0750: 47 4e 5f 4b 45 59 2e 0a 23 0a 23 20 2f 2a 20 45  GN_KEY..#.# /* E
0760: 56 3a 20 52 2d 32 32 35 36 37 2d 34 34 30 33 39  V: R-22567-44039
0770: 20 2a 2f 0a 23 20 2f 2a 20 45 56 3a 20 52 2d 34   */.# /* EV: R-4
0780: 31 37 38 34 2d 31 33 33 33 39 20 2a 2f 0a 23 0a  1784-13339 */.#.
0790: 23 20 53 70 65 63 69 66 69 63 61 6c 6c 79 2c 20  # Specifically, 
07a0: 74 65 73 74 20 74 68 61 74 20 22 50 52 41 47 4d  test that "PRAGM
07b0: 41 20 66 6f 72 65 69 67 6e 5f 6b 65 79 73 22 20  A foreign_keys" 
07c0: 69 73 20 61 20 6e 6f 2d 6f 70 20 69 6e 20 74 68  is a no-op in th
07d0: 69 73 20 63 61 73 65 2e 0a 23 20 57 68 65 6e 20  is case..# When 
07e0: 75 73 69 6e 67 20 74 68 65 20 70 72 61 67 6d 61  using the pragma
07f0: 20 74 6f 20 71 75 65 72 79 20 74 68 65 20 63 75   to query the cu
0800: 72 72 65 6e 74 20 73 65 74 74 69 6e 67 2c 20 30  rrent setting, 0
0810: 20 72 6f 77 73 20 61 72 65 20 72 65 74 75 72 6e   rows are return
0820: 65 64 2e 0a 23 0a 72 65 73 65 74 5f 64 62 0a 69  ed..#.reset_db.i
0830: 66 63 61 70 61 62 6c 65 20 21 74 72 69 67 67 65  fcapable !trigge
0840: 72 26 26 66 6f 72 65 69 67 6e 6b 65 79 20 7b 0a  r&&foreignkey {.
0850: 20 20 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79    do_test e_fkey
0860: 2d 35 31 2e 31 20 7b 0a 20 20 20 20 65 78 65 63  -51.1 {.    exec
0870: 73 71 6c 20 7b 0a 20 20 20 20 20 20 50 52 41 47  sql {.      PRAG
0880: 4d 41 20 66 6f 72 65 69 67 6e 5f 6b 65 79 73 20  MA foreign_keys 
0890: 3d 20 4f 4e 3b 0a 20 20 20 20 20 20 43 52 45 41  = ON;.      CREA
08a0: 54 45 20 54 41 42 4c 45 20 70 28 69 20 50 52 49  TE TABLE p(i PRI
08b0: 4d 41 52 59 20 4b 45 59 29 3b 0a 20 20 20 20 20  MARY KEY);.     
08c0: 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 63 28   CREATE TABLE c(
08d0: 6a 20 52 45 46 45 52 45 4e 43 45 53 20 70 20 4f  j REFERENCES p O
08e0: 4e 20 55 50 44 41 54 45 20 43 41 53 43 41 44 45  N UPDATE CASCADE
08f0: 29 3b 0a 20 20 20 20 20 20 49 4e 53 45 52 54 20  );.      INSERT 
0900: 49 4e 54 4f 20 70 20 56 41 4c 55 45 53 28 27 68  INTO p VALUES('h
0910: 65 6c 6c 6f 27 29 3b 0a 20 20 20 20 20 20 49 4e  ello');.      IN
0920: 53 45 52 54 20 49 4e 54 4f 20 63 20 56 41 4c 55  SERT INTO c VALU
0930: 45 53 28 27 68 65 6c 6c 6f 27 29 3b 0a 20 20 20  ES('hello');.   
0940: 20 20 20 55 50 44 41 54 45 20 70 20 53 45 54 20     UPDATE p SET 
0950: 69 20 3d 20 27 77 6f 72 6c 64 27 3b 0a 20 20 20  i = 'world';.   
0960: 20 20 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d     SELECT * FROM
0970: 20 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 20 7b 68   c;.    }.  } {h
0980: 65 6c 6c 6f 7d 0a 20 20 64 6f 5f 74 65 73 74 20  ello}.  do_test 
0990: 65 5f 66 6b 65 79 2d 35 31 2e 32 20 7b 0a 20 20  e_fkey-51.2 {.  
09a0: 20 20 65 78 65 63 73 71 6c 20 7b 20 50 52 41 47    execsql { PRAG
09b0: 4d 41 20 66 6f 72 65 69 67 6e 5f 6b 65 79 5f 6c  MA foreign_key_l
09c0: 69 73 74 28 63 29 20 7d 0a 20 20 7d 20 7b 30 20  ist(c) }.  } {0 
09d0: 30 20 70 20 6a 20 7b 7d 20 43 41 53 43 41 44 45  0 p j {} CASCADE
09e0: 20 7b 4e 4f 20 41 43 54 49 4f 4e 7d 20 4e 4f 4e   {NO ACTION} NON
09f0: 45 7d 0a 20 20 64 6f 5f 74 65 73 74 20 65 5f 66  E}.  do_test e_f
0a00: 6b 65 79 2d 35 31 2e 33 20 7b 0a 20 20 20 20 65  key-51.3 {.    e
0a10: 78 65 63 73 71 6c 20 7b 20 50 52 41 47 4d 41 20  xecsql { PRAGMA 
0a20: 66 6f 72 65 69 67 6e 5f 6b 65 79 73 20 7d 0a 20  foreign_keys }. 
0a30: 20 7d 20 7b 7d 0a 7d 0a 0a 0a 23 2d 2d 2d 2d 2d   } {}.}...#-----
0a40: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0a50: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0a60: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0a70: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0a80: 2d 2d 2d 2d 0a 23 20 2f 2a 20 45 56 3a 20 52 2d  ----.# /* EV: R-
0a90: 35 38 34 32 38 2d 33 36 36 36 30 20 2a 2f 0a 23  58428-36660 */.#
0aa0: 0a 23 20 54 65 73 74 20 74 68 65 20 65 66 66 65  .# Test the effe
0ab0: 63 74 73 20 6f 66 20 64 65 66 69 6e 69 6e 67 20  cts of defining 
0ac0: 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59  OMIT_FOREIGN_KEY
0ad0: 2e 0a 23 0a 23 20 2f 2a 20 45 56 3a 20 52 2d 35  ..#.# /* EV: R-5
0ae0: 38 34 32 38 2d 33 36 36 36 30 20 2a 2f 0a 23 0a  8428-36660 */.#.
0af0: 23 20 53 70 65 63 69 66 69 63 61 6c 6c 79 2c 20  # Specifically, 
0b00: 74 65 73 74 20 74 68 61 74 20 66 6f 72 65 69 67  test that foreig
0b10: 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74  n key constraint
0b20: 73 20 63 61 6e 6e 6f 74 20 65 76 65 6e 20 62 65  s cannot even be
0b30: 20 70 61 72 73 65 64 20 0a 23 20 69 6e 20 73 75   parsed .# in su
0b40: 63 68 20 61 20 62 75 69 6c 64 2e 0a 23 0a 72 65  ch a build..#.re
0b50: 73 65 74 5f 64 62 0a 69 66 63 61 70 61 62 6c 65  set_db.ifcapable
0b60: 20 21 66 6f 72 65 69 67 6e 6b 65 79 20 7b 0a 20   !foreignkey {. 
0b70: 20 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d   do_test e_fkey-
0b80: 35 32 2e 31 20 7b 0a 20 20 20 20 65 78 65 63 73  52.1 {.    execs
0b90: 71 6c 20 7b 20 43 52 45 41 54 45 20 54 41 42 4c  ql { CREATE TABL
0ba0: 45 20 70 28 69 20 50 52 49 4d 41 52 59 20 4b 45  E p(i PRIMARY KE
0bb0: 59 29 20 7d 0a 20 20 20 20 63 61 74 63 68 73 71  Y) }.    catchsq
0bc0: 6c 20 7b 20 43 52 45 41 54 45 20 54 41 42 4c 45  l { CREATE TABLE
0bd0: 20 63 28 6a 20 52 45 46 45 52 45 4e 43 45 53 20   c(j REFERENCES 
0be0: 70 20 4f 4e 20 55 50 44 41 54 45 20 43 41 53 43  p ON UPDATE CASC
0bf0: 41 44 45 29 20 7d 0a 20 20 7d 20 7b 31 20 7b 6e  ADE) }.  } {1 {n
0c00: 65 61 72 20 22 4f 4e 22 3a 20 73 79 6e 74 61 78  ear "ON": syntax
0c10: 20 65 72 72 6f 72 7d 7d 0a 20 20 64 6f 5f 74 65   error}}.  do_te
0c20: 73 74 20 65 5f 66 6b 65 79 2d 35 32 2e 32 20 7b  st e_fkey-52.2 {
0c30: 0a 20 20 20 20 23 20 54 68 69 73 20 69 73 20 61  .    # This is a
0c40: 6c 6c 6f 77 65 64 2c 20 61 73 20 69 6e 20 74 68  llowed, as in th
0c50: 69 73 20 62 75 69 6c 64 2c 20 22 52 45 46 45 52  is build, "REFER
0c60: 45 4e 43 45 53 22 20 69 73 20 6e 6f 74 20 61 20  ENCES" is not a 
0c70: 6b 65 79 77 6f 72 64 2e 0a 20 20 20 20 23 20 54  keyword..    # T
0c80: 68 65 20 64 65 63 6c 61 72 65 64 20 64 61 74 61  he declared data
0c90: 74 79 70 65 20 6f 66 20 63 6f 6c 75 6d 6e 20 6a  type of column j
0ca0: 20 69 73 20 22 52 45 46 45 52 45 4e 43 45 53 20   is "REFERENCES 
0cb0: 70 22 2e 0a 20 20 20 20 65 78 65 63 73 71 6c 20  p"..    execsql 
0cc0: 7b 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 63  { CREATE TABLE c
0cd0: 28 6a 20 52 45 46 45 52 45 4e 43 45 53 20 70 29  (j REFERENCES p)
0ce0: 20 7d 0a 20 20 7d 20 7b 7d 0a 20 20 64 6f 5f 74   }.  } {}.  do_t
0cf0: 65 73 74 20 65 5f 66 6b 65 79 2d 35 32 2e 33 20  est e_fkey-52.3 
0d00: 7b 0a 20 20 20 20 65 78 65 63 73 71 6c 20 7b 20  {.    execsql { 
0d10: 50 52 41 47 4d 41 20 74 61 62 6c 65 5f 69 6e 66  PRAGMA table_inf
0d20: 6f 28 63 29 20 7d 0a 20 20 7d 20 7b 30 20 6a 20  o(c) }.  } {0 j 
0d30: 7b 52 45 46 45 52 45 4e 43 45 53 20 70 7d 20 30  {REFERENCES p} 0
0d40: 20 7b 7d 20 30 7d 0a 20 20 64 6f 5f 74 65 73 74   {} 0}.  do_test
0d50: 20 65 5f 66 6b 65 79 2d 35 32 2e 34 20 7b 0a 20   e_fkey-52.4 {. 
0d60: 20 20 20 65 78 65 63 73 71 6c 20 7b 20 50 52 41     execsql { PRA
0d70: 47 4d 41 20 66 6f 72 65 69 67 6e 5f 6b 65 79 5f  GMA foreign_key_
0d80: 6c 69 73 74 28 63 29 20 7d 0a 20 20 7d 20 7b 7d  list(c) }.  } {}
0d90: 0a 20 20 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65  .  do_test e_fke
0da0: 79 2d 35 32 2e 35 20 7b 0a 20 20 20 20 65 78 65  y-52.5 {.    exe
0db0: 63 73 71 6c 20 7b 20 50 52 41 47 4d 41 20 66 6f  csql { PRAGMA fo
0dc0: 72 65 69 67 6e 5f 6b 65 79 73 20 7d 0a 20 20 7d  reign_keys }.  }
0dd0: 20 7b 7d 0a 7d 0a 0a 69 66 63 61 70 61 62 6c 65   {}.}..ifcapable
0de0: 20 21 66 6f 72 65 69 67 6e 6b 65 79 7c 7c 21 74   !foreignkey||!t
0df0: 72 69 67 67 65 72 20 7b 20 66 69 6e 69 73 68 5f  rigger { finish_
0e00: 74 65 73 74 20 3b 20 72 65 74 75 72 6e 20 7d 0a  test ; return }.
0e10: 72 65 73 65 74 5f 64 62 0a 0a 0a 23 2d 2d 2d 2d  reset_db...#----
0e20: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0e30: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0e40: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0e50: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0e60: 2d 2d 2d 2d 2d 0a 23 20 2f 2a 20 45 56 3a 20 52  -----.# /* EV: R
0e70: 2d 30 37 32 38 30 2d 36 30 35 31 30 20 2a 2f 0a  -07280-60510 */.
0e80: 23 0a 23 20 54 65 73 74 20 74 68 61 74 20 65 76  #.# Test that ev
0e90: 65 6e 20 69 66 20 66 6f 72 65 69 67 6e 20 6b 65  en if foreign ke
0ea0: 79 73 20 61 72 65 20 73 75 70 70 6f 72 74 65 64  ys are supported
0eb0: 20 62 79 20 74 68 65 20 62 75 69 6c 64 2c 20 74   by the build, t
0ec0: 68 65 79 20 6d 75 73 74 0a 23 20 62 65 20 65 6e  hey must.# be en
0ed0: 61 62 6c 65 64 20 75 73 69 6e 67 20 22 50 52 41  abled using "PRA
0ee0: 47 4d 41 20 66 6f 72 65 69 67 6e 5f 6b 65 79 73  GMA foreign_keys
0ef0: 20 3d 20 4f 4e 22 20 28 6f 72 20 73 69 6d 69 6c   = ON" (or simil
0f00: 61 72 29 2e 0a 23 0a 23 20 2f 2a 20 45 56 3a 20  ar)..#.# /* EV: 
0f10: 52 2d 35 39 35 37 38 2d 30 34 39 39 30 20 2a 2f  R-59578-04990 */
0f20: 0a 23 0a 23 20 54 68 69 73 20 61 6c 73 6f 20 74  .#.# This also t
0f30: 65 73 74 73 20 74 68 61 74 20 66 6f 72 65 69 67  ests that foreig
0f40: 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74  n key constraint
0f50: 73 20 61 72 65 20 64 69 73 61 62 6c 65 64 20 62  s are disabled b
0f60: 79 20 64 65 66 61 75 6c 74 2e 0a 23 0a 64 72 6f  y default..#.dro
0f70: 70 5f 61 6c 6c 5f 74 61 62 6c 65 73 0a 64 6f 5f  p_all_tables.do_
0f80: 74 65 73 74 20 65 5f 66 6b 65 79 2d 35 33 2e 31  test e_fkey-53.1
0f90: 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20   {.  execsql {. 
0fa0: 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20     CREATE TABLE 
0fb0: 70 28 69 20 50 52 49 4d 41 52 59 20 4b 45 59 29  p(i PRIMARY KEY)
0fc0: 3b 0a 20 20 20 20 43 52 45 41 54 45 20 54 41 42  ;.    CREATE TAB
0fd0: 4c 45 20 63 28 6a 20 52 45 46 45 52 45 4e 43 45  LE c(j REFERENCE
0fe0: 53 20 70 20 4f 4e 20 55 50 44 41 54 45 20 43 41  S p ON UPDATE CA
0ff0: 53 43 41 44 45 29 3b 0a 20 20 20 20 49 4e 53 45  SCADE);.    INSE
1000: 52 54 20 49 4e 54 4f 20 70 20 56 41 4c 55 45 53  RT INTO p VALUES
1010: 28 27 68 65 6c 6c 6f 27 29 3b 0a 20 20 20 20 49  ('hello');.    I
1020: 4e 53 45 52 54 20 49 4e 54 4f 20 63 20 56 41 4c  NSERT INTO c VAL
1030: 55 45 53 28 27 68 65 6c 6c 6f 27 29 3b 0a 20 20  UES('hello');.  
1040: 20 20 55 50 44 41 54 45 20 70 20 53 45 54 20 69    UPDATE p SET i
1050: 20 3d 20 27 77 6f 72 6c 64 27 3b 0a 20 20 20 20   = 'world';.    
1060: 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 63 3b  SELECT * FROM c;
1070: 0a 20 20 7d 20 0a 7d 20 7b 68 65 6c 6c 6f 7d 0a  .  } .} {hello}.
1080: 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 35  do_test e_fkey-5
1090: 33 2e 32 20 7b 0a 20 20 65 78 65 63 73 71 6c 20  3.2 {.  execsql 
10a0: 7b 0a 20 20 20 20 44 45 4c 45 54 45 20 46 52 4f  {.    DELETE FRO
10b0: 4d 20 63 3b 0a 20 20 20 20 44 45 4c 45 54 45 20  M c;.    DELETE 
10c0: 46 52 4f 4d 20 70 3b 0a 20 20 20 20 50 52 41 47  FROM p;.    PRAG
10d0: 4d 41 20 66 6f 72 65 69 67 6e 5f 6b 65 79 73 20  MA foreign_keys 
10e0: 3d 20 4f 4e 3b 0a 20 20 20 20 49 4e 53 45 52 54  = ON;.    INSERT
10f0: 20 49 4e 54 4f 20 70 20 56 41 4c 55 45 53 28 27   INTO p VALUES('
1100: 68 65 6c 6c 6f 27 29 3b 0a 20 20 20 20 49 4e 53  hello');.    INS
1110: 45 52 54 20 49 4e 54 4f 20 63 20 56 41 4c 55 45  ERT INTO c VALUE
1120: 53 28 27 68 65 6c 6c 6f 27 29 3b 0a 20 20 20 20  S('hello');.    
1130: 55 50 44 41 54 45 20 70 20 53 45 54 20 69 20 3d  UPDATE p SET i =
1140: 20 27 77 6f 72 6c 64 27 3b 0a 20 20 20 20 53 45   'world';.    SE
1150: 4c 45 43 54 20 2a 20 46 52 4f 4d 20 63 3b 0a 20  LECT * FROM c;. 
1160: 20 7d 20 0a 7d 20 7b 77 6f 72 6c 64 7d 0a 0a 23   } .} {world}..#
1170: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1180: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1190: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
11a0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
11b0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20 2f 2a 20 45  ---------.# /* E
11c0: 56 3a 20 52 2d 31 35 32 37 38 2d 35 34 34 35 36  V: R-15278-54456
11d0: 20 2a 2f 0a 23 20 2f 2a 20 45 56 3a 20 52 2d 31   */.# /* EV: R-1
11e0: 31 32 35 35 2d 31 39 39 30 37 20 2a 2f 0a 23 0a  1255-19907 */.#.
11f0: 23 20 54 65 73 74 20 74 68 61 74 20 74 68 65 20  # Test that the 
1200: 61 70 70 6c 69 63 61 74 69 6f 6e 20 63 61 6e 20  application can 
1210: 75 73 65 20 22 50 52 41 47 4d 41 20 66 6f 72 65  use "PRAGMA fore
1220: 69 67 6e 5f 6b 65 79 73 22 20 74 6f 20 71 75 65  ign_keys" to que
1230: 72 79 20 66 6f 72 0a 23 20 77 68 65 74 68 65 72  ry for.# whether
1240: 20 6f 72 20 6e 6f 74 20 66 6f 72 65 69 67 6e 20   or not foreign 
1250: 6b 65 79 73 20 61 72 65 20 63 75 72 72 65 6e 74  keys are current
1260: 6c 79 20 65 6e 61 62 6c 65 64 2e 20 54 68 69 73  ly enabled. This
1270: 20 61 6c 73 6f 20 74 65 73 74 73 0a 23 20 74 68   also tests.# th
1280: 65 20 65 78 61 6d 70 6c 65 20 63 6f 64 65 20 69  e example code i
1290: 6e 20 73 65 63 74 69 6f 6e 20 32 20 6f 66 20 66  n section 2 of f
12a0: 6f 72 65 69 67 6e 6b 65 79 73 2e 69 6e 2e 0a 23  oreignkeys.in..#
12b0: 0a 72 65 73 65 74 5f 64 62 0a 64 6f 5f 74 65 73  .reset_db.do_tes
12c0: 74 20 65 5f 66 6b 65 79 2d 35 34 2e 31 20 7b 0a  t e_fkey-54.1 {.
12d0: 20 20 65 78 65 63 73 71 6c 20 7b 20 50 52 41 47    execsql { PRAG
12e0: 4d 41 20 66 6f 72 65 69 67 6e 5f 6b 65 79 73 20  MA foreign_keys 
12f0: 7d 0a 7d 20 7b 30 7d 0a 64 6f 5f 74 65 73 74 20  }.} {0}.do_test 
1300: 65 5f 66 6b 65 79 2d 35 34 2e 32 20 7b 0a 20 20  e_fkey-54.2 {.  
1310: 65 78 65 63 73 71 6c 20 7b 20 0a 20 20 20 20 50  execsql { .    P
1320: 52 41 47 4d 41 20 66 6f 72 65 69 67 6e 5f 6b 65  RAGMA foreign_ke
1330: 79 73 20 3d 20 4f 4e 3b 0a 20 20 20 20 50 52 41  ys = ON;.    PRA
1340: 47 4d 41 20 66 6f 72 65 69 67 6e 5f 6b 65 79 73  GMA foreign_keys
1350: 3b 0a 20 20 7d 0a 7d 20 7b 31 7d 0a 64 6f 5f 74  ;.  }.} {1}.do_t
1360: 65 73 74 20 65 5f 66 6b 65 79 2d 35 34 2e 33 20  est e_fkey-54.3 
1370: 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 20 0a 20  {.  execsql { . 
1380: 20 20 20 50 52 41 47 4d 41 20 66 6f 72 65 69 67     PRAGMA foreig
1390: 6e 5f 6b 65 79 73 20 3d 20 4f 46 46 3b 0a 20 20  n_keys = OFF;.  
13a0: 20 20 50 52 41 47 4d 41 20 66 6f 72 65 69 67 6e    PRAGMA foreign
13b0: 5f 6b 65 79 73 3b 0a 20 20 7d 0a 7d 20 7b 30 7d  _keys;.  }.} {0}
13c0: 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ..#-------------
13d0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
13e0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
13f0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1400: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20 2f  ------------.# /
1410: 2a 20 45 56 3a 20 52 2d 34 36 36 34 39 2d 35 38  * EV: R-46649-58
1420: 35 33 37 20 2a 2f 0a 23 0a 23 20 54 65 73 74 20  537 */.#.# Test 
1430: 74 68 61 74 20 69 74 20 69 73 20 6e 6f 74 20 70  that it is not p
1440: 6f 73 73 69 62 6c 65 20 74 6f 20 65 6e 61 62 6c  ossible to enabl
1450: 65 20 6f 72 20 64 69 73 61 62 6c 65 20 66 6f 72  e or disable for
1460: 65 69 67 6e 20 6b 65 79 20 73 75 70 70 6f 72 74  eign key support
1470: 0a 23 20 77 68 69 6c 65 20 6e 6f 74 20 69 6e 20  .# while not in 
1480: 61 75 74 6f 2d 63 6f 6d 6d 69 74 20 6d 6f 64 65  auto-commit mode
1490: 2e 0a 23 0a 72 65 73 65 74 5f 64 62 0a 64 6f 5f  ..#.reset_db.do_
14a0: 74 65 73 74 20 65 5f 66 6b 65 79 2d 35 35 2e 31  test e_fkey-55.1
14b0: 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20   {.  execsql {. 
14c0: 20 20 20 50 52 41 47 4d 41 20 66 6f 72 65 69 67     PRAGMA foreig
14d0: 6e 5f 6b 65 79 73 20 3d 20 4f 4e 3b 0a 20 20 20  n_keys = ON;.   
14e0: 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 74 31   CREATE TABLE t1
14f0: 28 61 20 55 4e 49 51 55 45 2c 20 62 29 3b 0a 20  (a UNIQUE, b);. 
1500: 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20     CREATE TABLE 
1510: 74 32 28 63 2c 20 64 20 52 45 46 45 52 45 4e 43  t2(c, d REFERENC
1520: 45 53 20 74 31 28 61 29 29 3b 0a 20 20 20 20 49  ES t1(a));.    I
1530: 4e 53 45 52 54 20 49 4e 54 4f 20 74 31 20 56 41  NSERT INTO t1 VA
1540: 4c 55 45 53 28 31 2c 20 32 29 3b 0a 20 20 20 20  LUES(1, 2);.    
1550: 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 32 20 56  INSERT INTO t2 V
1560: 41 4c 55 45 53 28 32 2c 20 31 29 3b 0a 20 20 20  ALUES(2, 1);.   
1570: 20 42 45 47 49 4e 3b 0a 20 20 20 20 20 20 50 52   BEGIN;.      PR
1580: 41 47 4d 41 20 66 6f 72 65 69 67 6e 5f 6b 65 79  AGMA foreign_key
1590: 73 20 3d 20 4f 46 46 3b 0a 20 20 7d 0a 20 20 63  s = OFF;.  }.  c
15a0: 61 74 63 68 73 71 6c 20 7b 0a 20 20 20 20 20 20  atchsql {.      
15b0: 44 45 4c 45 54 45 20 46 52 4f 4d 20 74 31 0a 20  DELETE FROM t1. 
15c0: 20 7d 0a 7d 20 7b 31 20 7b 66 6f 72 65 69 67 6e   }.} {1 {foreign
15d0: 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 20   key constraint 
15e0: 66 61 69 6c 65 64 7d 7d 0a 64 6f 5f 74 65 73 74  failed}}.do_test
15f0: 20 65 5f 66 6b 65 79 2d 35 35 2e 32 20 7b 0a 20   e_fkey-55.2 {. 
1600: 20 65 78 65 63 73 71 6c 20 7b 20 50 52 41 47 4d   execsql { PRAGM
1610: 41 20 66 6f 72 65 69 67 6e 5f 6b 65 79 73 20 7d  A foreign_keys }
1620: 0a 7d 20 7b 31 7d 0a 64 6f 5f 74 65 73 74 20 65  .} {1}.do_test e
1630: 5f 66 6b 65 79 2d 35 35 2e 33 20 7b 0a 20 20 65  _fkey-55.3 {.  e
1640: 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 43 4f 4d  xecsql {.    COM
1650: 4d 49 54 3b 0a 20 20 20 20 50 52 41 47 4d 41 20  MIT;.    PRAGMA 
1660: 66 6f 72 65 69 67 6e 5f 6b 65 79 73 20 3d 20 4f  foreign_keys = O
1670: 46 46 3b 0a 20 20 20 20 42 45 47 49 4e 3b 0a 20  FF;.    BEGIN;. 
1680: 20 20 20 20 20 50 52 41 47 4d 41 20 66 6f 72 65       PRAGMA fore
1690: 69 67 6e 5f 6b 65 79 73 20 3d 20 4f 4e 3b 0a 20  ign_keys = ON;. 
16a0: 20 20 20 20 20 44 45 4c 45 54 45 20 46 52 4f 4d       DELETE FROM
16b0: 20 74 31 3b 0a 20 20 20 20 20 20 50 52 41 47 4d   t1;.      PRAGM
16c0: 41 20 66 6f 72 65 69 67 6e 5f 6b 65 79 73 3b 0a  A foreign_keys;.
16d0: 20 20 7d 0a 7d 20 7b 30 7d 0a 64 6f 5f 74 65 73    }.} {0}.do_tes
16e0: 74 20 65 5f 66 6b 65 79 2d 35 35 2e 34 20 7b 0a  t e_fkey-55.4 {.
16f0: 20 20 65 78 65 63 73 71 6c 20 43 4f 4d 4d 49 54    execsql COMMIT
1700: 0a 7d 20 7b 7d 0a 0a 23 23 23 23 23 23 23 23 23  .} {}..#########
1710: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
1720: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
1730: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
1740: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
1750: 23 23 0a 23 23 23 20 53 45 43 54 49 4f 4e 20 31  ##.### SECTION 1
1760: 3a 20 49 6e 74 72 6f 64 75 63 74 69 6f 6e 20 74  : Introduction t
1770: 6f 20 46 6f 72 65 69 67 6e 20 4b 65 79 20 43 6f  o Foreign Key Co
1780: 6e 73 74 72 61 69 6e 74 73 0a 23 23 23 23 23 23  nstraints.######
1790: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
17a0: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
17b0: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
17c0: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
17d0: 23 23 23 23 23 0a 65 78 65 63 73 71 6c 20 22 50  #####.execsql "P
17e0: 52 41 47 4d 41 20 66 6f 72 65 69 67 6e 5f 6b 65  RAGMA foreign_ke
17f0: 79 73 20 3d 20 4f 4e 22 0a 0a 23 2d 2d 2d 2d 2d  ys = ON"..#-----
1800: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1810: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1820: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1830: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1840: 2d 2d 2d 2d 0a 23 20 2f 2a 20 45 56 3a 20 52 2d  ----.# /* EV: R-
1850: 30 34 30 34 32 2d 32 34 38 32 35 20 2a 2f 0a 23  04042-24825 */.#
1860: 0a 23 20 56 65 72 69 66 79 20 74 68 61 74 20 74  .# Verify that t
1870: 68 65 20 73 79 6e 74 61 78 20 69 6e 20 74 68 65  he syntax in the
1880: 20 66 69 72 73 74 20 65 78 61 6d 70 6c 65 20 69   first example i
1890: 6e 20 73 65 63 74 69 6f 6e 20 31 20 69 73 20 76  n section 1 is v
18a0: 61 6c 69 64 2e 0a 23 0a 64 6f 5f 74 65 73 74 20  alid..#.do_test 
18b0: 65 5f 66 6b 65 79 2d 33 38 2e 31 20 7b 0a 20 20  e_fkey-38.1 {.  
18c0: 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 43 52  execsql {.    CR
18d0: 45 41 54 45 20 54 41 42 4c 45 20 61 72 74 69 73  EATE TABLE artis
18e0: 74 28 0a 20 20 20 20 20 20 61 72 74 69 73 74 69  t(.      artisti
18f0: 64 20 20 20 20 49 4e 54 45 47 45 52 20 50 52 49  d    INTEGER PRI
1900: 4d 41 52 59 20 4b 45 59 2c 20 0a 20 20 20 20 20  MARY KEY, .     
1910: 20 61 72 74 69 73 74 6e 61 6d 65 20 20 54 45 58   artistname  TEX
1920: 54 0a 20 20 20 20 29 3b 0a 20 20 20 20 43 52 45  T.    );.    CRE
1930: 41 54 45 20 54 41 42 4c 45 20 74 72 61 63 6b 28  ATE TABLE track(
1940: 0a 20 20 20 20 20 20 74 72 61 63 6b 69 64 20 20  .      trackid  
1950: 20 20 20 49 4e 54 45 47 45 52 2c 20 0a 20 20 20     INTEGER, .   
1960: 20 20 20 74 72 61 63 6b 6e 61 6d 65 20 20 20 54     trackname   T
1970: 45 58 54 2c 20 0a 20 20 20 20 20 20 74 72 61 63  EXT, .      trac
1980: 6b 61 72 74 69 73 74 20 49 4e 54 45 47 45 52 2c  kartist INTEGER,
1990: 0a 20 20 20 20 20 20 46 4f 52 45 49 47 4e 20 4b  .      FOREIGN K
19a0: 45 59 28 74 72 61 63 6b 61 72 74 69 73 74 29 20  EY(trackartist) 
19b0: 52 45 46 45 52 45 4e 43 45 53 20 61 72 74 69 73  REFERENCES artis
19c0: 74 28 61 72 74 69 73 74 69 64 29 0a 20 20 20 20  t(artistid).    
19d0: 29 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a 0a 23 2d 2d  );.  }.} {}..#--
19e0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
19f0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1a00: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1a10: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1a20: 2d 2d 2d 2d 2d 2d 2d 0a 23 20 2f 2a 20 45 56 3a  -------.# /* EV:
1a30: 20 52 2d 36 31 33 36 32 2d 33 32 30 38 37 20 2a   R-61362-32087 *
1a40: 2f 0a 23 0a 23 20 41 74 74 65 6d 70 74 69 6e 67  /.#.# Attempting
1a50: 20 74 6f 20 69 6e 73 65 72 74 20 61 20 72 6f 77   to insert a row
1a60: 20 69 6e 74 6f 20 74 68 65 20 27 74 72 61 63 6b   into the 'track
1a70: 27 20 74 61 62 6c 65 20 74 68 61 74 20 63 6f 72  ' table that cor
1a80: 72 65 73 70 6f 6e 64 73 0a 23 20 74 6f 20 6e 6f  responds.# to no
1a90: 20 72 6f 77 20 69 6e 20 74 68 65 20 27 61 72 74   row in the 'art
1aa0: 69 73 74 27 20 74 61 62 6c 65 20 66 61 69 6c 73  ist' table fails
1ab0: 2e 0a 23 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b  ..#.do_test e_fk
1ac0: 65 79 2d 33 39 2e 31 20 7b 0a 20 20 63 61 74 63  ey-39.1 {.  catc
1ad0: 68 73 71 6c 20 7b 20 49 4e 53 45 52 54 20 49 4e  hsql { INSERT IN
1ae0: 54 4f 20 74 72 61 63 6b 20 56 41 4c 55 45 53 28  TO track VALUES(
1af0: 31 2c 20 27 74 72 61 63 6b 20 31 27 2c 20 31 29  1, 'track 1', 1)
1b00: 20 7d 0a 7d 20 7b 31 20 7b 66 6f 72 65 69 67 6e   }.} {1 {foreign
1b10: 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 20   key constraint 
1b20: 66 61 69 6c 65 64 7d 7d 0a 64 6f 5f 74 65 73 74  failed}}.do_test
1b30: 20 65 5f 66 6b 65 79 2d 33 39 2e 32 20 7b 0a 20   e_fkey-39.2 {. 
1b40: 20 65 78 65 63 73 71 6c 20 7b 20 49 4e 53 45 52   execsql { INSER
1b50: 54 20 49 4e 54 4f 20 61 72 74 69 73 74 20 56 41  T INTO artist VA
1b60: 4c 55 45 53 28 32 2c 20 27 61 72 74 69 73 74 20  LUES(2, 'artist 
1b70: 31 27 29 20 7d 0a 20 20 63 61 74 63 68 73 71 6c  1') }.  catchsql
1b80: 20 7b 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74   { INSERT INTO t
1b90: 72 61 63 6b 20 56 41 4c 55 45 53 28 31 2c 20 27  rack VALUES(1, '
1ba0: 74 72 61 63 6b 20 31 27 2c 20 31 29 20 7d 0a 7d  track 1', 1) }.}
1bb0: 20 7b 31 20 7b 66 6f 72 65 69 67 6e 20 6b 65 79   {1 {foreign key
1bc0: 20 63 6f 6e 73 74 72 61 69 6e 74 20 66 61 69 6c   constraint fail
1bd0: 65 64 7d 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66  ed}}.do_test e_f
1be0: 6b 65 79 2d 33 39 2e 32 20 7b 0a 20 20 65 78 65  key-39.2 {.  exe
1bf0: 63 73 71 6c 20 7b 20 49 4e 53 45 52 54 20 49 4e  csql { INSERT IN
1c00: 54 4f 20 74 72 61 63 6b 20 56 41 4c 55 45 53 28  TO track VALUES(
1c10: 31 2c 20 27 74 72 61 63 6b 20 31 27 2c 20 32 29  1, 'track 1', 2)
1c20: 20 7d 0a 7d 20 7b 7d 0a 0a 23 2d 2d 2d 2d 2d 2d   }.} {}..#------
1c30: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1c40: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1c50: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1c60: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1c70: 2d 2d 2d 0a 23 20 2f 2a 20 45 56 3a 20 52 2d 32  ---.# /* EV: R-2
1c80: 34 34 30 31 2d 35 32 34 30 30 20 2a 2f 0a 23 0a  4401-52400 */.#.
1c90: 23 20 41 74 74 65 6d 70 74 69 6e 67 20 74 6f 20  # Attempting to 
1ca0: 64 65 6c 65 74 65 20 61 20 72 6f 77 20 66 72 6f  delete a row fro
1cb0: 6d 20 74 68 65 20 27 61 72 74 69 73 74 27 20 74  m the 'artist' t
1cc0: 61 62 6c 65 20 77 68 69 6c 65 20 74 68 65 72 65  able while there
1cd0: 20 61 72 65 20 0a 23 20 64 65 70 65 6e 64 65 6e   are .# dependen
1ce0: 74 20 72 6f 77 73 20 69 6e 20 74 68 65 20 74 72  t rows in the tr
1cf0: 61 63 6b 20 74 61 62 6c 65 20 61 6c 73 6f 20 66  ack table also f
1d00: 61 69 6c 73 2e 0a 23 0a 64 6f 5f 74 65 73 74 20  ails..#.do_test 
1d10: 65 5f 66 6b 65 79 2d 34 30 2e 31 20 7b 0a 20 20  e_fkey-40.1 {.  
1d20: 63 61 74 63 68 73 71 6c 20 7b 20 44 45 4c 45 54  catchsql { DELET
1d30: 45 20 46 52 4f 4d 20 61 72 74 69 73 74 20 57 48  E FROM artist WH
1d40: 45 52 45 20 61 72 74 69 73 74 69 64 20 3d 20 32  ERE artistid = 2
1d50: 20 7d 0a 7d 20 7b 31 20 7b 66 6f 72 65 69 67 6e   }.} {1 {foreign
1d60: 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 20   key constraint 
1d70: 66 61 69 6c 65 64 7d 7d 0a 64 6f 5f 74 65 73 74  failed}}.do_test
1d80: 20 65 5f 66 6b 65 79 2d 34 30 2e 32 20 7b 0a 20   e_fkey-40.2 {. 
1d90: 20 65 78 65 63 73 71 6c 20 7b 20 0a 20 20 20 20   execsql { .    
1da0: 44 45 4c 45 54 45 20 46 52 4f 4d 20 74 72 61 63  DELETE FROM trac
1db0: 6b 20 57 48 45 52 45 20 74 72 61 63 6b 61 72 74  k WHERE trackart
1dc0: 69 73 74 20 3d 20 32 3b 0a 20 20 20 20 44 45 4c  ist = 2;.    DEL
1dd0: 45 54 45 20 46 52 4f 4d 20 61 72 74 69 73 74 20  ETE FROM artist 
1de0: 57 48 45 52 45 20 61 72 74 69 73 74 69 64 20 3d  WHERE artistid =
1df0: 20 32 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a 0a 23 2d   2;.  }.} {}..#-
1e00: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1e10: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1e20: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1e30: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1e40: 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20 2f 2a 20 45 56  --------.# /* EV
1e50: 3a 20 52 2d 32 33 39 38 30 2d 34 38 38 35 39 20  : R-23980-48859 
1e60: 2a 2f 0a 23 0a 23 20 49 66 20 74 68 65 20 66 6f  */.#.# If the fo
1e70: 72 65 69 67 6e 20 6b 65 79 20 63 6f 6c 75 6d 6e  reign key column
1e80: 20 28 74 72 61 63 6b 61 72 74 69 73 74 29 20 69   (trackartist) i
1e90: 6e 20 74 61 62 6c 65 20 27 74 72 61 63 6b 27 20  n table 'track' 
1ea0: 69 73 20 73 65 74 20 74 6f 20 4e 55 4c 4c 2c 0a  is set to NULL,.
1eb0: 23 20 74 68 65 72 65 20 69 73 20 6e 6f 20 72 65  # there is no re
1ec0: 71 75 69 72 65 6d 65 6e 74 20 66 6f 72 20 61 20  quirement for a 
1ed0: 6d 61 74 63 68 69 6e 67 20 72 6f 77 20 69 6e 20  matching row in 
1ee0: 74 68 65 20 27 61 72 74 69 73 74 27 20 74 61 62  the 'artist' tab
1ef0: 6c 65 2e 0a 23 0a 64 6f 5f 74 65 73 74 20 65 5f  le..#.do_test e_
1f00: 66 6b 65 79 2d 34 31 2e 31 20 7b 0a 20 20 65 78  fkey-41.1 {.  ex
1f10: 65 63 73 71 6c 20 7b 0a 20 20 20 20 49 4e 53 45  ecsql {.    INSE
1f20: 52 54 20 49 4e 54 4f 20 74 72 61 63 6b 20 56 41  RT INTO track VA
1f30: 4c 55 45 53 28 31 2c 20 27 74 72 61 63 6b 20 31  LUES(1, 'track 1
1f40: 27 2c 20 4e 55 4c 4c 29 3b 0a 20 20 20 20 49 4e  ', NULL);.    IN
1f50: 53 45 52 54 20 49 4e 54 4f 20 74 72 61 63 6b 20  SERT INTO track 
1f60: 56 41 4c 55 45 53 28 32 2c 20 27 74 72 61 63 6b  VALUES(2, 'track
1f70: 20 32 27 2c 20 4e 55 4c 4c 29 3b 0a 20 20 7d 0a   2', NULL);.  }.
1f80: 7d 20 7b 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66  } {}.do_test e_f
1f90: 6b 65 79 2d 34 31 2e 32 20 7b 0a 20 20 65 78 65  key-41.2 {.  exe
1fa0: 63 73 71 6c 20 7b 20 53 45 4c 45 43 54 20 2a 20  csql { SELECT * 
1fb0: 46 52 4f 4d 20 61 72 74 69 73 74 20 7d 0a 7d 20  FROM artist }.} 
1fc0: 7b 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65  {}.do_test e_fke
1fd0: 79 2d 34 31 2e 33 20 7b 0a 20 20 23 20 53 65 74  y-41.3 {.  # Set
1fe0: 74 69 6e 67 20 74 68 65 20 74 72 61 63 6b 69 64  ting the trackid
1ff0: 20 74 6f 20 61 20 6e 6f 6e 2d 4e 55 4c 4c 20 76   to a non-NULL v
2000: 61 6c 75 65 20 66 61 69 6c 73 2c 20 6f 66 20 63  alue fails, of c
2010: 6f 75 72 73 65 2e 0a 20 20 63 61 74 63 68 73 71  ourse..  catchsq
2020: 6c 20 7b 20 55 50 44 41 54 45 20 74 72 61 63 6b  l { UPDATE track
2030: 20 53 45 54 20 74 72 61 63 6b 61 72 74 69 73 74   SET trackartist
2040: 20 3d 20 35 20 57 48 45 52 45 20 74 72 61 63 6b   = 5 WHERE track
2050: 69 64 20 3d 20 31 20 7d 0a 7d 20 7b 31 20 7b 66  id = 1 }.} {1 {f
2060: 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74  oreign key const
2070: 72 61 69 6e 74 20 66 61 69 6c 65 64 7d 7d 0a 64  raint failed}}.d
2080: 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 34 31  o_test e_fkey-41
2090: 2e 34 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b  .4 {.  execsql {
20a0: 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f  .    INSERT INTO
20b0: 20 61 72 74 69 73 74 20 56 41 4c 55 45 53 28 35   artist VALUES(5
20c0: 2c 20 27 61 72 74 69 73 74 20 35 27 29 3b 0a 20  , 'artist 5');. 
20d0: 20 20 20 55 50 44 41 54 45 20 74 72 61 63 6b 20     UPDATE track 
20e0: 53 45 54 20 74 72 61 63 6b 61 72 74 69 73 74 20  SET trackartist 
20f0: 3d 20 35 20 57 48 45 52 45 20 74 72 61 63 6b 69  = 5 WHERE tracki
2100: 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 63 61 74  d = 1;.  }.  cat
2110: 63 68 73 71 6c 20 7b 20 44 45 4c 45 54 45 20 46  chsql { DELETE F
2120: 52 4f 4d 20 61 72 74 69 73 74 20 57 48 45 52 45  ROM artist WHERE
2130: 20 61 72 74 69 73 74 69 64 20 3d 20 35 7d 0a 7d   artistid = 5}.}
2140: 20 7b 31 20 7b 66 6f 72 65 69 67 6e 20 6b 65 79   {1 {foreign key
2150: 20 63 6f 6e 73 74 72 61 69 6e 74 20 66 61 69 6c   constraint fail
2160: 65 64 7d 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66  ed}}.do_test e_f
2170: 6b 65 79 2d 34 31 2e 35 20 7b 0a 20 20 65 78 65  key-41.5 {.  exe
2180: 63 73 71 6c 20 7b 20 0a 20 20 20 20 55 50 44 41  csql { .    UPDA
2190: 54 45 20 74 72 61 63 6b 20 53 45 54 20 74 72 61  TE track SET tra
21a0: 63 6b 61 72 74 69 73 74 20 3d 20 4e 55 4c 4c 20  ckartist = NULL 
21b0: 57 48 45 52 45 20 74 72 61 63 6b 69 64 20 3d 20  WHERE trackid = 
21c0: 31 3b 0a 20 20 20 20 44 45 4c 45 54 45 20 46 52  1;.    DELETE FR
21d0: 4f 4d 20 61 72 74 69 73 74 20 57 48 45 52 45 20  OM artist WHERE 
21e0: 61 72 74 69 73 74 69 64 20 3d 20 35 3b 0a 20 20  artistid = 5;.  
21f0: 7d 0a 7d 20 7b 7d 0a 0a 23 2d 2d 2d 2d 2d 2d 2d  }.} {}..#-------
2200: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2210: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2220: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2230: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2240: 2d 2d 0a 23 20 2f 2a 20 45 56 3a 20 52 2d 35 32  --.# /* EV: R-52
2250: 34 38 36 2d 32 31 33 35 32 20 2a 2f 0a 23 0a 23  486-21352 */.#.#
2260: 20 54 65 73 74 20 74 68 61 74 20 74 68 65 20 66   Test that the f
2270: 6f 6c 6c 6f 77 69 6e 67 20 69 73 20 74 72 75 65  ollowing is true
2280: 20 66 6f 20 61 6c 6c 20 72 6f 77 73 20 69 6e 20   fo all rows in 
2290: 74 68 65 20 74 72 61 63 6b 20 74 61 62 6c 65 3a  the track table:
22a0: 0a 23 0a 23 20 20 20 74 72 61 63 6b 61 72 74 69  .#.#   trackarti
22b0: 73 74 20 49 53 20 4e 55 4c 4c 20 4f 52 20 0a 23  st IS NULL OR .#
22c0: 20 20 20 45 58 49 53 54 53 28 53 45 4c 45 43 54     EXISTS(SELECT
22d0: 20 31 20 46 52 4f 4d 20 61 72 74 69 73 74 20 57   1 FROM artist W
22e0: 48 45 52 45 20 61 72 74 69 73 74 69 64 3d 74 72  HERE artistid=tr
22f0: 61 63 6b 61 72 74 69 73 74 29 0a 23 0a 0a 23 20  ackartist).#..# 
2300: 54 68 69 73 20 70 72 6f 63 65 64 75 72 65 20 65  This procedure e
2310: 78 65 63 75 74 65 73 20 61 20 74 65 73 74 20 63  xecutes a test c
2320: 61 73 65 20 74 6f 20 63 68 65 63 6b 20 74 68 61  ase to check tha
2330: 74 20 73 74 61 74 65 6d 65 6e 74 20 0a 23 20 52  t statement .# R
2340: 2d 35 32 34 38 36 2d 32 31 33 35 32 20 69 73 20  -52486-21352 is 
2350: 74 72 75 65 20 61 66 74 65 72 20 65 78 65 63 75  true after execu
2360: 74 69 6e 67 20 74 68 65 20 53 51 4c 20 73 74 61  ting the SQL sta
2370: 74 65 6d 65 6e 74 20 70 61 73 73 65 64 2e 0a 23  tement passed..#
2380: 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 61   as the second a
2390: 72 67 75 6d 65 6e 74 2e 0a 70 72 6f 63 20 74 65  rgument..proc te
23a0: 73 74 5f 72 35 32 34 38 36 5f 32 31 33 35 32 20  st_r52486_21352 
23b0: 7b 74 6e 20 73 71 6c 7d 20 7b 0a 20 20 73 65 74  {tn sql} {.  set
23c0: 20 72 65 73 20 5b 63 61 74 63 68 73 71 6c 20 24   res [catchsql $
23d0: 73 71 6c 5d 0a 20 20 73 65 74 20 72 65 73 75 6c  sql].  set resul
23e0: 74 73 20 7b 0a 20 20 20 20 7b 30 20 7b 7d 7d 20  ts {.    {0 {}} 
23f0: 0a 20 20 20 20 7b 31 20 7b 50 52 49 4d 41 52 59  .    {1 {PRIMARY
2400: 20 4b 45 59 20 6d 75 73 74 20 62 65 20 75 6e 69   KEY must be uni
2410: 71 75 65 7d 7d 20 0a 20 20 20 20 7b 31 20 7b 66  que}} .    {1 {f
2420: 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74  oreign key const
2430: 72 61 69 6e 74 20 66 61 69 6c 65 64 7d 7d 0a 20  raint failed}}. 
2440: 20 7d 0a 20 20 69 66 20 7b 5b 6c 73 65 61 72 63   }.  if {[lsearc
2450: 68 20 24 72 65 73 75 6c 74 73 20 24 72 65 73 5d  h $results $res]
2460: 3c 30 7d 20 7b 0a 20 20 20 20 65 72 72 6f 72 20  <0} {.    error 
2470: 24 72 65 73 0a 20 20 7d 0a 0a 20 20 64 6f 5f 74  $res.  }..  do_t
2480: 65 73 74 20 65 5f 66 6b 65 79 2d 34 32 2e 24 74  est e_fkey-42.$t
2490: 6e 20 7b 0a 20 20 20 20 65 78 65 63 73 71 6c 20  n {.    execsql 
24a0: 7b 0a 20 20 20 20 20 20 53 45 4c 45 43 54 20 63  {.      SELECT c
24b0: 6f 75 6e 74 28 2a 29 20 46 52 4f 4d 20 74 72 61  ount(*) FROM tra
24c0: 63 6b 20 57 48 45 52 45 20 4e 4f 54 20 28 0a 20  ck WHERE NOT (. 
24d0: 20 20 20 20 20 20 20 74 72 61 63 6b 61 72 74 69         trackarti
24e0: 73 74 20 49 53 20 4e 55 4c 4c 20 4f 52 20 0a 20  st IS NULL OR . 
24f0: 20 20 20 20 20 20 20 45 58 49 53 54 53 28 53 45         EXISTS(SE
2500: 4c 45 43 54 20 31 20 46 52 4f 4d 20 61 72 74 69  LECT 1 FROM arti
2510: 73 74 20 57 48 45 52 45 20 61 72 74 69 73 74 69  st WHERE artisti
2520: 64 3d 74 72 61 63 6b 61 72 74 69 73 74 29 0a 20  d=trackartist). 
2530: 20 20 20 20 20 29 0a 20 20 20 20 7d 0a 20 20 7d       ).    }.  }
2540: 20 7b 30 7d 0a 7d 0a 0a 23 20 45 78 65 63 75 74   {0}.}..# Execut
2550: 65 20 61 20 73 65 72 69 65 73 20 6f 66 20 72 61  e a series of ra
2560: 6e 64 6f 6d 20 49 4e 53 45 52 54 2c 20 55 50 44  ndom INSERT, UPD
2570: 41 54 45 20 61 6e 64 20 44 45 4c 45 54 45 20 6f  ATE and DELETE o
2580: 70 65 72 61 74 69 6f 6e 73 0a 23 20 28 73 6f 6d  perations.# (som
2590: 65 20 6f 66 20 77 68 69 63 68 20 6d 61 79 20 66  e of which may f
25a0: 61 69 6c 20 64 75 65 20 74 6f 20 46 4b 20 6f 72  ail due to FK or
25b0: 20 50 4b 20 63 6f 6e 73 74 72 61 69 6e 74 20 76   PK constraint v
25c0: 69 6f 6c 61 74 69 6f 6e 73 29 20 6f 6e 20 0a 23  iolations) on .#
25d0: 20 74 68 65 20 74 77 6f 20 74 61 62 6c 65 73 20   the two tables 
25e0: 69 6e 20 74 68 65 20 65 78 61 6d 70 6c 65 20 73  in the example s
25f0: 63 68 65 6d 61 2e 20 54 65 73 74 20 74 68 61 74  chema. Test that
2600: 20 52 2d 35 32 34 38 36 2d 32 31 33 35 32 0a 23   R-52486-21352.#
2610: 20 69 73 20 74 72 75 65 20 61 66 74 65 72 20 65   is true after e
2620: 78 65 63 75 74 69 6e 67 20 65 61 63 68 20 6f 70  xecuting each op
2630: 65 72 61 74 69 6f 6e 2e 0a 23 0a 73 65 74 20 54  eration..#.set T
2640: 65 6d 70 6c 61 74 65 20 7b 0a 20 20 7b 49 4e 53  emplate {.  {INS
2650: 45 52 54 20 49 4e 54 4f 20 74 72 61 63 6b 20 56  ERT INTO track V
2660: 41 4c 55 45 53 28 24 74 2c 20 27 74 72 61 63 6b  ALUES($t, 'track
2670: 20 24 74 27 2c 20 24 61 29 7d 0a 20 20 7b 44 45   $t', $a)}.  {DE
2680: 4c 45 54 45 20 46 52 4f 4d 20 74 72 61 63 6b 20  LETE FROM track 
2690: 57 48 45 52 45 20 74 72 61 63 6b 69 64 20 3d 20  WHERE trackid = 
26a0: 24 74 7d 0a 20 20 7b 55 50 44 41 54 45 20 74 72  $t}.  {UPDATE tr
26b0: 61 63 6b 20 53 45 54 20 74 72 61 63 6b 61 72 74  ack SET trackart
26c0: 69 73 74 20 3d 20 24 61 20 57 48 45 52 45 20 74  ist = $a WHERE t
26d0: 72 61 63 6b 69 64 20 3d 20 24 74 7d 0a 20 20 7b  rackid = $t}.  {
26e0: 49 4e 53 45 52 54 20 49 4e 54 4f 20 61 72 74 69  INSERT INTO arti
26f0: 73 74 20 56 41 4c 55 45 53 28 24 61 2c 20 27 61  st VALUES($a, 'a
2700: 72 74 69 73 74 20 24 61 27 29 7d 0a 20 20 7b 44  rtist $a')}.  {D
2710: 45 4c 45 54 45 20 46 52 4f 4d 20 61 72 74 69 73  ELETE FROM artis
2720: 74 20 57 48 45 52 45 20 61 72 74 69 73 74 69 64  t WHERE artistid
2730: 20 3d 20 24 61 7d 0a 20 20 7b 55 50 44 41 54 45   = $a}.  {UPDATE
2740: 20 61 72 74 69 73 74 20 53 45 54 20 61 72 74 69   artist SET arti
2750: 73 74 69 64 20 3d 20 24 61 32 20 57 48 45 52 45  stid = $a2 WHERE
2760: 20 61 72 74 69 73 74 69 64 20 3d 20 24 61 7d 0a   artistid = $a}.
2770: 7d 0a 66 6f 72 20 7b 73 65 74 20 69 20 30 7d 20  }.for {set i 0} 
2780: 7b 24 69 20 3c 20 35 30 30 7d 20 7b 69 6e 63 72  {$i < 500} {incr
2790: 20 69 7d 20 7b 0a 20 20 73 65 74 20 61 20 20 20   i} {.  set a   
27a0: 5b 65 78 70 72 20 69 6e 74 28 72 61 6e 64 28 29  [expr int(rand()
27b0: 2a 31 30 29 5d 0a 20 20 73 65 74 20 61 32 20 20  *10)].  set a2  
27c0: 5b 65 78 70 72 20 69 6e 74 28 72 61 6e 64 28 29  [expr int(rand()
27d0: 2a 31 30 29 5d 0a 20 20 73 65 74 20 74 20 20 20  *10)].  set t   
27e0: 5b 65 78 70 72 20 69 6e 74 28 72 61 6e 64 28 29  [expr int(rand()
27f0: 2a 35 30 29 5d 0a 20 20 73 65 74 20 73 71 6c 20  *50)].  set sql 
2800: 5b 73 75 62 73 74 20 5b 6c 69 6e 64 65 78 20 24  [subst [lindex $
2810: 54 65 6d 70 6c 61 74 65 20 5b 65 78 70 72 20 69  Template [expr i
2820: 6e 74 28 72 61 6e 64 28 29 2a 36 29 5d 5d 5d 0a  nt(rand()*6)]]].
2830: 0a 20 20 74 65 73 74 5f 72 35 32 34 38 36 5f 32  .  test_r52486_2
2840: 31 33 35 32 20 24 69 20 24 73 71 6c 0a 7d 0a 0a  1352 $i $sql.}..
2850: 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  #---------------
2860: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2870: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2880: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2890: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20 2f 2a 20  ----------.# /* 
28a0: 45 56 3a 20 52 2d 34 32 34 31 32 2d 35 39 33 32  EV: R-42412-5932
28b0: 31 20 2a 2f 0a 23 0a 23 20 43 68 65 63 6b 20 74  1 */.#.# Check t
28c0: 68 61 74 20 61 20 4e 4f 54 20 4e 55 4c 4c 20 63  hat a NOT NULL c
28d0: 6f 6e 73 74 72 61 69 6e 74 20 63 61 6e 20 62 65  onstraint can be
28e0: 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 65 78   added to the ex
28f0: 61 6d 70 6c 65 20 73 63 68 65 6d 61 0a 23 20 74  ample schema.# t
2900: 6f 20 70 72 6f 68 69 62 69 74 20 4e 55 4c 4c 20  o prohibit NULL 
2910: 63 68 69 6c 64 20 6b 65 79 73 20 66 72 6f 6d 20  child keys from 
2920: 62 65 69 6e 67 20 69 6e 73 65 72 74 65 64 2e 0a  being inserted..
2930: 23 0a 64 72 6f 70 5f 61 6c 6c 5f 74 61 62 6c 65  #.drop_all_table
2940: 73 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79  s.do_test e_fkey
2950: 2d 34 38 2e 31 20 7b 0a 20 20 65 78 65 63 73 71  -48.1 {.  execsq
2960: 6c 20 7b 0a 20 20 20 20 43 52 45 41 54 45 20 54  l {.    CREATE T
2970: 41 42 4c 45 20 61 72 74 69 73 74 28 0a 20 20 20  ABLE artist(.   
2980: 20 20 20 61 72 74 69 73 74 69 64 20 20 20 20 49     artistid    I
2990: 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b  NTEGER PRIMARY K
29a0: 45 59 2c 20 0a 20 20 20 20 20 20 61 72 74 69 73  EY, .      artis
29b0: 74 6e 61 6d 65 20 20 54 45 58 54 0a 20 20 20 20  tname  TEXT.    
29c0: 29 3b 0a 20 20 20 20 43 52 45 41 54 45 20 54 41  );.    CREATE TA
29d0: 42 4c 45 20 74 72 61 63 6b 28 0a 20 20 20 20 20  BLE track(.     
29e0: 20 74 72 61 63 6b 69 64 20 20 20 20 20 49 4e 54   trackid     INT
29f0: 45 47 45 52 2c 20 0a 20 20 20 20 20 20 74 72 61  EGER, .      tra
2a00: 63 6b 6e 61 6d 65 20 20 20 54 45 58 54 2c 20 0a  ckname   TEXT, .
2a10: 20 20 20 20 20 20 74 72 61 63 6b 61 72 74 69 73        trackartis
2a20: 74 20 49 4e 54 45 47 45 52 20 4e 4f 54 20 4e 55  t INTEGER NOT NU
2a30: 4c 4c 2c 0a 20 20 20 20 20 20 46 4f 52 45 49 47  LL,.      FOREIG
2a40: 4e 20 4b 45 59 28 74 72 61 63 6b 61 72 74 69 73  N KEY(trackartis
2a50: 74 29 20 52 45 46 45 52 45 4e 43 45 53 20 61 72  t) REFERENCES ar
2a60: 74 69 73 74 28 61 72 74 69 73 74 69 64 29 0a 20  tist(artistid). 
2a70: 20 20 20 29 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a 64     );.  }.} {}.d
2a80: 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 34 38  o_test e_fkey-48
2a90: 2e 32 20 7b 0a 20 20 63 61 74 63 68 73 71 6c 20  .2 {.  catchsql 
2aa0: 7b 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 72  { INSERT INTO tr
2ab0: 61 63 6b 20 56 41 4c 55 45 53 28 31 34 2c 20 27  ack VALUES(14, '
2ac0: 4d 72 2e 20 42 6f 6a 61 6e 67 6c 65 73 27 2c 20  Mr. Bojangles', 
2ad0: 4e 55 4c 4c 29 20 7d 0a 7d 20 7b 31 20 7b 74 72  NULL) }.} {1 {tr
2ae0: 61 63 6b 2e 74 72 61 63 6b 61 72 74 69 73 74 20  ack.trackartist 
2af0: 6d 61 79 20 6e 6f 74 20 62 65 20 4e 55 4c 4c 7d  may not be NULL}
2b00: 7d 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  }..#------------
2b10: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2b20: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2b30: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2b40: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20  -------------.# 
2b50: 2f 2a 20 45 56 3a 20 52 2d 31 37 39 30 32 2d 35  /* EV: R-17902-5
2b60: 39 32 35 30 20 2a 2f 0a 23 0a 23 20 54 65 73 74  9250 */.#.# Test
2b70: 20 61 6e 20 65 78 61 6d 70 6c 65 20 66 72 6f 6d   an example from
2b80: 20 66 6f 72 65 69 67 6e 6b 65 79 73 2e 68 74 6d   foreignkeys.htm
2b90: 6c 2e 0a 23 0a 64 72 6f 70 5f 61 6c 6c 5f 74 61  l..#.drop_all_ta
2ba0: 62 6c 65 73 0a 64 6f 5f 74 65 73 74 20 65 5f 66  bles.do_test e_f
2bb0: 6b 65 79 2d 34 33 2e 31 20 7b 0a 20 20 65 78 65  key-43.1 {.  exe
2bc0: 63 73 71 6c 20 7b 0a 20 20 20 20 43 52 45 41 54  csql {.    CREAT
2bd0: 45 20 54 41 42 4c 45 20 61 72 74 69 73 74 28 0a  E TABLE artist(.
2be0: 20 20 20 20 20 20 61 72 74 69 73 74 69 64 20 20        artistid  
2bf0: 20 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52    INTEGER PRIMAR
2c00: 59 20 4b 45 59 2c 20 0a 20 20 20 20 20 20 61 72  Y KEY, .      ar
2c10: 74 69 73 74 6e 61 6d 65 20 20 54 45 58 54 0a 20  tistname  TEXT. 
2c20: 20 20 20 29 3b 0a 20 20 20 20 43 52 45 41 54 45     );.    CREATE
2c30: 20 54 41 42 4c 45 20 74 72 61 63 6b 28 0a 20 20   TABLE track(.  
2c40: 20 20 20 20 74 72 61 63 6b 69 64 20 20 20 20 20      trackid     
2c50: 49 4e 54 45 47 45 52 2c 20 0a 20 20 20 20 20 20  INTEGER, .      
2c60: 74 72 61 63 6b 6e 61 6d 65 20 20 20 54 45 58 54  trackname   TEXT
2c70: 2c 20 0a 20 20 20 20 20 20 74 72 61 63 6b 61 72  , .      trackar
2c80: 74 69 73 74 20 49 4e 54 45 47 45 52 2c 0a 20 20  tist INTEGER,.  
2c90: 20 20 20 20 46 4f 52 45 49 47 4e 20 4b 45 59 28      FOREIGN KEY(
2ca0: 74 72 61 63 6b 61 72 74 69 73 74 29 20 52 45 46  trackartist) REF
2cb0: 45 52 45 4e 43 45 53 20 61 72 74 69 73 74 28 61  ERENCES artist(a
2cc0: 72 74 69 73 74 69 64 29 0a 20 20 20 20 29 3b 0a  rtistid).    );.
2cd0: 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20      INSERT INTO 
2ce0: 61 72 74 69 73 74 20 56 41 4c 55 45 53 28 31 2c  artist VALUES(1,
2cf0: 20 27 44 65 61 6e 20 4d 61 72 74 69 6e 27 29 3b   'Dean Martin');
2d00: 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f  .    INSERT INTO
2d10: 20 61 72 74 69 73 74 20 56 41 4c 55 45 53 28 32   artist VALUES(2
2d20: 2c 20 27 46 72 61 6e 6b 20 53 69 6e 61 74 72 61  , 'Frank Sinatra
2d30: 27 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49  ');.    INSERT I
2d40: 4e 54 4f 20 74 72 61 63 6b 20 56 41 4c 55 45 53  NTO track VALUES
2d50: 28 31 31 2c 20 27 54 68 61 74 27 27 73 20 41 6d  (11, 'That''s Am
2d60: 6f 72 65 27 2c 20 31 29 3b 0a 20 20 20 20 49 4e  ore', 1);.    IN
2d70: 53 45 52 54 20 49 4e 54 4f 20 74 72 61 63 6b 20  SERT INTO track 
2d80: 56 41 4c 55 45 53 28 31 32 2c 20 27 43 68 72 69  VALUES(12, 'Chri
2d90: 73 74 6d 61 73 20 42 6c 75 65 73 27 2c 20 31 29  stmas Blues', 1)
2da0: 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54  ;.    INSERT INT
2db0: 4f 20 74 72 61 63 6b 20 56 41 4c 55 45 53 28 31  O track VALUES(1
2dc0: 33 2c 20 27 4d 79 20 57 61 79 27 2c 20 32 29 3b  3, 'My Way', 2);
2dd0: 0a 20 20 7d 0a 7d 20 7b 7d 0a 64 6f 5f 74 65 73  .  }.} {}.do_tes
2de0: 74 20 65 5f 66 6b 65 79 2d 34 33 2e 32 20 7b 0a  t e_fkey-43.2 {.
2df0: 20 20 63 61 74 63 68 73 71 6c 20 7b 20 49 4e 53    catchsql { INS
2e00: 45 52 54 20 49 4e 54 4f 20 74 72 61 63 6b 20 56  ERT INTO track V
2e10: 41 4c 55 45 53 28 31 34 2c 20 27 4d 72 2e 20 42  ALUES(14, 'Mr. B
2e20: 6f 6a 61 6e 67 6c 65 73 27 2c 20 33 29 20 7d 0a  ojangles', 3) }.
2e30: 7d 20 7b 31 20 7b 66 6f 72 65 69 67 6e 20 6b 65  } {1 {foreign ke
2e40: 79 20 63 6f 6e 73 74 72 61 69 6e 74 20 66 61 69  y constraint fai
2e50: 6c 65 64 7d 7d 0a 64 6f 5f 74 65 73 74 20 65 5f  led}}.do_test e_
2e60: 66 6b 65 79 2d 34 33 2e 33 20 7b 0a 20 20 65 78  fkey-43.3 {.  ex
2e70: 65 63 73 71 6c 20 7b 20 49 4e 53 45 52 54 20 49  ecsql { INSERT I
2e80: 4e 54 4f 20 74 72 61 63 6b 20 56 41 4c 55 45 53  NTO track VALUES
2e90: 28 31 34 2c 20 27 4d 72 2e 20 42 6f 6a 61 6e 67  (14, 'Mr. Bojang
2ea0: 6c 65 73 27 2c 20 4e 55 4c 4c 29 20 7d 0a 7d 20  les', NULL) }.} 
2eb0: 7b 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65  {}.do_test e_fke
2ec0: 79 2d 34 33 2e 34 20 7b 0a 20 20 63 61 74 63 68  y-43.4 {.  catch
2ed0: 73 71 6c 20 7b 20 0a 20 20 20 20 55 50 44 41 54  sql { .    UPDAT
2ee0: 45 20 74 72 61 63 6b 20 53 45 54 20 74 72 61 63  E track SET trac
2ef0: 6b 61 72 74 69 73 74 20 3d 20 33 20 57 48 45 52  kartist = 3 WHER
2f00: 45 20 74 72 61 63 6b 6e 61 6d 65 20 3d 20 27 4d  E trackname = 'M
2f10: 72 2e 20 42 6f 6a 61 6e 67 6c 65 73 27 3b 0a 20  r. Bojangles';. 
2f20: 20 7d 0a 7d 20 7b 31 20 7b 66 6f 72 65 69 67 6e   }.} {1 {foreign
2f30: 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 20   key constraint 
2f40: 66 61 69 6c 65 64 7d 7d 0a 64 6f 5f 74 65 73 74  failed}}.do_test
2f50: 20 65 5f 66 6b 65 79 2d 34 33 2e 35 20 7b 0a 20   e_fkey-43.5 {. 
2f60: 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 49   execsql {.    I
2f70: 4e 53 45 52 54 20 49 4e 54 4f 20 61 72 74 69 73  NSERT INTO artis
2f80: 74 20 56 41 4c 55 45 53 28 33 2c 20 27 53 61 6d  t VALUES(3, 'Sam
2f90: 6d 79 20 44 61 76 69 73 20 4a 72 2e 27 29 3b 0a  my Davis Jr.');.
2fa0: 20 20 20 20 55 50 44 41 54 45 20 74 72 61 63 6b      UPDATE track
2fb0: 20 53 45 54 20 74 72 61 63 6b 61 72 74 69 73 74   SET trackartist
2fc0: 20 3d 20 33 20 57 48 45 52 45 20 74 72 61 63 6b   = 3 WHERE track
2fd0: 6e 61 6d 65 20 3d 20 27 4d 72 2e 20 42 6f 6a 61  name = 'Mr. Boja
2fe0: 6e 67 6c 65 73 27 3b 0a 20 20 20 20 49 4e 53 45  ngles';.    INSE
2ff0: 52 54 20 49 4e 54 4f 20 74 72 61 63 6b 20 56 41  RT INTO track VA
3000: 4c 55 45 53 28 31 35 2c 20 27 42 6f 6f 67 69 65  LUES(15, 'Boogie
3010: 20 57 6f 6f 67 69 65 27 2c 20 33 29 3b 0a 20 20   Woogie', 3);.  
3020: 7d 0a 7d 20 7b 7d 0a 0a 23 2d 2d 2d 2d 2d 2d 2d  }.} {}..#-------
3030: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
3040: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
3050: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
3060: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
3070: 2d 2d 0a 23 20 2f 2a 20 45 56 3a 20 52 2d 31 35  --.# /* EV: R-15
3080: 30 33 34 2d 36 34 33 33 31 20 2a 2f 0a 23 0a 23  034-64331 */.#.#
3090: 20 54 65 73 74 20 74 68 65 20 73 65 63 6f 6e 64   Test the second
30a0: 20 65 78 61 6d 70 6c 65 20 66 72 6f 6d 20 74 68   example from th
30b0: 65 20 66 69 72 73 74 20 73 65 63 74 69 6f 6e 20  e first section 
30c0: 6f 66 20 66 6f 72 65 69 67 6e 6b 65 79 73 2e 68  of foreignkeys.h
30d0: 74 6d 6c 2e 0a 23 0a 64 6f 5f 74 65 73 74 20 65  tml..#.do_test e
30e0: 5f 66 6b 65 79 2d 34 34 2e 31 20 7b 0a 20 20 63  _fkey-44.1 {.  c
30f0: 61 74 63 68 73 71 6c 20 7b 0a 20 20 20 20 44 45  atchsql {.    DE
3100: 4c 45 54 45 20 46 52 4f 4d 20 61 72 74 69 73 74  LETE FROM artist
3110: 20 57 48 45 52 45 20 61 72 74 69 73 74 6e 61 6d   WHERE artistnam
3120: 65 20 3d 20 27 46 72 61 6e 6b 20 53 69 6e 61 74  e = 'Frank Sinat
3130: 72 61 27 3b 0a 20 20 7d 0a 7d 20 7b 31 20 7b 66  ra';.  }.} {1 {f
3140: 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74  oreign key const
3150: 72 61 69 6e 74 20 66 61 69 6c 65 64 7d 7d 0a 64  raint failed}}.d
3160: 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 34 34  o_test e_fkey-44
3170: 2e 32 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b  .2 {.  execsql {
3180: 0a 20 20 20 20 44 45 4c 45 54 45 20 46 52 4f 4d  .    DELETE FROM
3190: 20 74 72 61 63 6b 20 57 48 45 52 45 20 74 72 61   track WHERE tra
31a0: 63 6b 6e 61 6d 65 20 3d 20 27 4d 79 20 57 61 79  ckname = 'My Way
31b0: 27 3b 0a 20 20 20 20 44 45 4c 45 54 45 20 46 52  ';.    DELETE FR
31c0: 4f 4d 20 61 72 74 69 73 74 20 57 48 45 52 45 20  OM artist WHERE 
31d0: 61 72 74 69 73 74 6e 61 6d 65 20 3d 20 27 46 72  artistname = 'Fr
31e0: 61 6e 6b 20 53 69 6e 61 74 72 61 27 3b 0a 20 20  ank Sinatra';.  
31f0: 7d 0a 7d 20 7b 7d 0a 64 6f 5f 74 65 73 74 20 65  }.} {}.do_test e
3200: 5f 66 6b 65 79 2d 34 34 2e 33 20 7b 0a 20 20 63  _fkey-44.3 {.  c
3210: 61 74 63 68 73 71 6c 20 7b 0a 20 20 20 20 55 50  atchsql {.    UP
3220: 44 41 54 45 20 61 72 74 69 73 74 20 53 45 54 20  DATE artist SET 
3230: 61 72 74 69 73 74 69 64 3d 34 20 57 48 45 52 45  artistid=4 WHERE
3240: 20 61 72 74 69 73 74 6e 61 6d 65 20 3d 20 27 44   artistname = 'D
3250: 65 61 6e 20 4d 61 72 74 69 6e 27 3b 0a 20 20 7d  ean Martin';.  }
3260: 0a 7d 20 7b 31 20 7b 66 6f 72 65 69 67 6e 20 6b  .} {1 {foreign k
3270: 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 20 66 61  ey constraint fa
3280: 69 6c 65 64 7d 7d 0a 64 6f 5f 74 65 73 74 20 65  iled}}.do_test e
3290: 5f 66 6b 65 79 2d 34 34 2e 34 20 7b 0a 20 20 65  _fkey-44.4 {.  e
32a0: 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 44 45 4c  xecsql {.    DEL
32b0: 45 54 45 20 46 52 4f 4d 20 74 72 61 63 6b 20 57  ETE FROM track W
32c0: 48 45 52 45 20 74 72 61 63 6b 6e 61 6d 65 20 49  HERE trackname I
32d0: 4e 28 27 54 68 61 74 27 27 73 20 41 6d 6f 72 65  N('That''s Amore
32e0: 27 2c 20 27 43 68 72 69 73 74 6d 61 73 20 42 6c  ', 'Christmas Bl
32f0: 75 65 73 27 29 3b 0a 20 20 20 20 55 50 44 41 54  ues');.    UPDAT
3300: 45 20 61 72 74 69 73 74 20 53 45 54 20 61 72 74  E artist SET art
3310: 69 73 74 69 64 3d 34 20 57 48 45 52 45 20 61 72  istid=4 WHERE ar
3320: 74 69 73 74 6e 61 6d 65 20 3d 20 27 44 65 61 6e  tistname = 'Dean
3330: 20 4d 61 72 74 69 6e 27 3b 0a 20 20 7d 0a 7d 20   Martin';.  }.} 
3340: 7b 7d 0a 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  {}...#----------
3350: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
3360: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
3370: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
3380: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a  ---------------.
3390: 23 20 2f 2a 20 45 56 3a 20 52 2d 35 36 30 33 32  # /* EV: R-56032
33a0: 2d 32 34 39 32 33 20 2a 2f 0a 23 0a 23 20 54 65  -24923 */.#.# Te
33b0: 73 74 20 74 68 61 74 20 61 20 66 6f 72 65 69 67  st that a foreig
33c0: 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74  n key constraint
33d0: 20 69 73 20 73 61 74 69 73 69 66 65 64 20 69 66   is satisifed if
33e0: 20 22 66 6f 72 20 65 61 63 68 20 72 6f 77 20 69   "for each row i
33f0: 6e 20 74 68 65 20 63 68 69 6c 64 0a 23 20 74 61  n the child.# ta
3400: 62 6c 65 20 65 69 74 68 65 72 20 6f 6e 65 20 6f  ble either one o
3410: 72 20 6d 6f 72 65 20 6f 66 20 74 68 65 20 63 68  r more of the ch
3420: 69 6c 64 20 6b 65 79 20 63 6f 6c 75 6d 6e 73 20  ild key columns 
3430: 61 72 65 20 4e 55 4c 4c 2c 20 6f 72 20 74 68 65  are NULL, or the
3440: 72 65 20 65 78 69 73 74 73 20 61 0a 23 20 72 6f  re exists a.# ro
3450: 77 20 69 6e 20 74 68 65 20 70 61 72 65 6e 74 20  w in the parent 
3460: 74 61 62 6c 65 20 66 6f 72 20 77 68 69 63 68 20  table for which 
3470: 65 61 63 68 20 70 61 72 65 6e 74 20 6b 65 79 20  each parent key 
3480: 63 6f 6c 75 6d 6e 20 63 6f 6e 74 61 69 6e 73 20  column contains 
3490: 61 20 76 61 6c 75 65 0a 23 20 65 71 75 61 6c 20  a value.# equal 
34a0: 74 6f 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20  to the value in 
34b0: 69 74 73 20 61 73 73 6f 63 69 61 74 65 64 20 63  its associated c
34c0: 68 69 6c 64 20 6b 65 79 20 63 6f 6c 75 6d 6e 22  hild key column"
34d0: 2e 0a 23 0a 23 20 2f 2a 20 45 56 3a 20 52 2d 35  ..#.# /* EV: R-5
34e0: 37 37 36 35 2d 31 32 33 38 30 20 2a 2f 0a 23 0a  7765-12380 */.#.
34f0: 23 20 54 65 73 74 20 61 6c 73 6f 20 74 68 61 74  # Test also that
3500: 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20   the comparison 
3510: 72 75 6c 65 73 20 61 72 65 20 75 73 65 64 20 77  rules are used w
3520: 68 65 6e 20 74 65 73 74 69 6e 67 20 69 66 20 74  hen testing if t
3530: 68 65 72 65 20 0a 23 20 69 73 20 61 20 6d 61 74  here .# is a mat
3540: 63 68 69 6e 67 20 72 6f 77 20 69 6e 20 74 68 65  ching row in the
3550: 20 70 61 72 65 6e 74 20 74 61 62 6c 65 20 6f 66   parent table of
3560: 20 61 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 63   a foreign key c
3570: 6f 6e 73 74 72 61 69 6e 74 2e 0a 23 0a 64 72 6f  onstraint..#.dro
3580: 70 5f 61 6c 6c 5f 74 61 62 6c 65 73 0a 64 6f 5f  p_all_tables.do_
3590: 74 65 73 74 20 65 5f 66 6b 65 79 2d 34 35 2e 31  test e_fkey-45.1
35a0: 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20   {.  execsql {. 
35b0: 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20     CREATE TABLE 
35c0: 70 61 72 28 70 20 50 52 49 4d 41 52 59 20 4b 45  par(p PRIMARY KE
35d0: 59 29 3b 0a 20 20 20 20 43 52 45 41 54 45 20 54  Y);.    CREATE T
35e0: 41 42 4c 45 20 63 68 69 28 63 20 52 45 46 45 52  ABLE chi(c REFER
35f0: 45 4e 43 45 53 20 70 61 72 29 3b 0a 0a 20 20 20  ENCES par);..   
3600: 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 70 61 72   INSERT INTO par
3610: 20 56 41 4c 55 45 53 28 31 29 3b 0a 20 20 20 20   VALUES(1);.    
3620: 49 4e 53 45 52 54 20 49 4e 54 4f 20 70 61 72 20  INSERT INTO par 
3630: 56 41 4c 55 45 53 28 27 31 27 29 3b 0a 20 20 20  VALUES('1');.   
3640: 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 70 61 72   INSERT INTO par
3650: 20 56 41 4c 55 45 53 28 58 27 33 31 27 29 3b 0a   VALUES(X'31');.
3660: 20 20 20 20 53 45 4c 45 43 54 20 74 79 70 65 6f      SELECT typeo
3670: 66 28 70 29 20 46 52 4f 4d 20 70 61 72 3b 0a 20  f(p) FROM par;. 
3680: 20 7d 0a 7d 20 7b 69 6e 74 65 67 65 72 20 74 65   }.} {integer te
3690: 78 74 20 62 6c 6f 62 7d 0a 0a 70 72 6f 63 20 74  xt blob}..proc t
36a0: 65 73 74 5f 65 66 6b 65 79 5f 34 35 20 7b 74 6e  est_efkey_45 {tn
36b0: 20 69 73 45 72 72 6f 72 20 73 71 6c 7d 20 7b 0a   isError sql} {.
36c0: 20 20 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79    do_test e_fkey
36d0: 2d 34 35 2e 24 74 6e 2e 31 20 22 0a 20 20 20 20  -45.$tn.1 ".    
36e0: 63 61 74 63 68 73 71 6c 20 7b 24 73 71 6c 7d 0a  catchsql {$sql}.
36f0: 20 20 22 20 5b 6c 69 6e 64 65 78 20 7b 7b 30 20    " [lindex {{0 
3700: 7b 7d 7d 20 7b 31 20 7b 66 6f 72 65 69 67 6e 20  {}} {1 {foreign 
3710: 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 20 66  key constraint f
3720: 61 69 6c 65 64 7d 7d 7d 20 24 69 73 45 72 72 6f  ailed}}} $isErro
3730: 72 5d 0a 0a 20 20 64 6f 5f 74 65 73 74 20 65 5f  r]..  do_test e_
3740: 66 6b 65 79 2d 34 35 2e 24 74 6e 2e 32 20 7b 0a  fkey-45.$tn.2 {.
3750: 20 20 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20      execsql {.  
3760: 20 20 20 20 53 45 4c 45 43 54 20 2a 20 46 52 4f      SELECT * FRO
3770: 4d 20 63 68 69 20 57 48 45 52 45 20 63 20 49 53  M chi WHERE c IS
3780: 20 4e 4f 54 20 4e 55 4c 4c 20 41 4e 44 20 63 20   NOT NULL AND c 
3790: 4e 4f 54 20 49 4e 20 28 53 45 4c 45 43 54 20 70  NOT IN (SELECT p
37a0: 20 46 52 4f 4d 20 70 61 72 29 0a 20 20 20 20 7d   FROM par).    }
37b0: 0a 20 20 7d 20 7b 7d 0a 7d 0a 0a 74 65 73 74 5f  .  } {}.}..test_
37c0: 65 66 6b 65 79 5f 34 35 20 31 20 30 20 22 49 4e  efkey_45 1 0 "IN
37d0: 53 45 52 54 20 49 4e 54 4f 20 63 68 69 20 56 41  SERT INTO chi VA
37e0: 4c 55 45 53 28 31 29 22 0a 74 65 73 74 5f 65 66  LUES(1)".test_ef
37f0: 6b 65 79 5f 34 35 20 32 20 31 20 22 49 4e 53 45  key_45 2 1 "INSE
3800: 52 54 20 49 4e 54 4f 20 63 68 69 20 56 41 4c 55  RT INTO chi VALU
3810: 45 53 28 27 31 2e 30 27 29 22 0a 74 65 73 74 5f  ES('1.0')".test_
3820: 65 66 6b 65 79 5f 34 35 20 33 20 30 20 22 49 4e  efkey_45 3 0 "IN
3830: 53 45 52 54 20 49 4e 54 4f 20 63 68 69 20 56 41  SERT INTO chi VA
3840: 4c 55 45 53 28 27 31 27 29 22 0a 74 65 73 74 5f  LUES('1')".test_
3850: 65 66 6b 65 79 5f 34 35 20 34 20 31 20 22 44 45  efkey_45 4 1 "DE
3860: 4c 45 54 45 20 46 52 4f 4d 20 70 61 72 20 57 48  LETE FROM par WH
3870: 45 52 45 20 70 20 3d 20 27 31 27 22 0a 74 65 73  ERE p = '1'".tes
3880: 74 5f 65 66 6b 65 79 5f 34 35 20 35 20 30 20 22  t_efkey_45 5 0 "
3890: 44 45 4c 45 54 45 20 46 52 4f 4d 20 63 68 69 20  DELETE FROM chi 
38a0: 57 48 45 52 45 20 63 20 3d 20 27 31 27 22 0a 74  WHERE c = '1'".t
38b0: 65 73 74 5f 65 66 6b 65 79 5f 34 35 20 36 20 30  est_efkey_45 6 0
38c0: 20 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 70 61   "DELETE FROM pa
38d0: 72 20 57 48 45 52 45 20 70 20 3d 20 27 31 27 22  r WHERE p = '1'"
38e0: 0a 74 65 73 74 5f 65 66 6b 65 79 5f 34 35 20 37  .test_efkey_45 7
38f0: 20 31 20 22 49 4e 53 45 52 54 20 49 4e 54 4f 20   1 "INSERT INTO 
3900: 63 68 69 20 56 41 4c 55 45 53 28 27 31 27 29 22  chi VALUES('1')"
3910: 0a 74 65 73 74 5f 65 66 6b 65 79 5f 34 35 20 38  .test_efkey_45 8
3920: 20 30 20 22 49 4e 53 45 52 54 20 49 4e 54 4f 20   0 "INSERT INTO 
3930: 63 68 69 20 56 41 4c 55 45 53 28 58 27 33 31 27  chi VALUES(X'31'
3940: 29 22 0a 74 65 73 74 5f 65 66 6b 65 79 5f 34 35  )".test_efkey_45
3950: 20 39 20 31 20 22 49 4e 53 45 52 54 20 49 4e 54   9 1 "INSERT INT
3960: 4f 20 63 68 69 20 56 41 4c 55 45 53 28 58 27 33  O chi VALUES(X'3
3970: 32 27 29 22 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d  2')"..#---------
3980: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
3990: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
39a0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
39b0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
39c0: 0a 23 20 2f 2a 20 45 56 3a 20 52 2d 31 35 37 39  .# /* EV: R-1579
39d0: 36 2d 34 37 35 31 33 20 2a 2f 0a 23 0a 23 20 53  6-47513 */.#.# S
39e0: 70 65 63 69 66 69 63 61 6c 6c 79 2c 20 74 65 73  pecifically, tes
39f0: 74 20 74 68 61 74 20 77 68 65 6e 20 63 6f 6d 70  t that when comp
3a00: 61 72 69 6e 67 20 63 68 69 6c 64 20 61 6e 64 20  aring child and 
3a10: 70 61 72 65 6e 74 20 6b 65 79 20 76 61 6c 75 65  parent key value
3a20: 73 20 74 68 65 0a 23 20 64 65 66 61 75 6c 74 20  s the.# default 
3a30: 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e  collation sequen
3a40: 63 65 20 6f 66 20 74 68 65 20 70 61 72 65 6e 74  ce of the parent
3a50: 20 6b 65 79 20 63 6f 6c 75 6d 6e 20 69 73 20 75   key column is u
3a60: 73 65 64 2e 0a 23 0a 64 72 6f 70 5f 61 6c 6c 5f  sed..#.drop_all_
3a70: 74 61 62 6c 65 73 0a 64 6f 5f 74 65 73 74 20 65  tables.do_test e
3a80: 5f 66 6b 65 79 2d 34 36 2e 31 20 7b 0a 20 20 65  _fkey-46.1 {.  e
3a90: 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 43 52 45  xecsql {.    CRE
3aa0: 41 54 45 20 54 41 42 4c 45 20 74 31 28 61 20 43  ATE TABLE t1(a C
3ab0: 4f 4c 4c 41 54 45 20 6e 6f 63 61 73 65 20 50 52  OLLATE nocase PR
3ac0: 49 4d 41 52 59 20 4b 45 59 29 3b 0a 20 20 20 20  IMARY KEY);.    
3ad0: 43 52 45 41 54 45 20 54 41 42 4c 45 20 74 32 28  CREATE TABLE t2(
3ae0: 62 20 52 45 46 45 52 45 4e 43 45 53 20 74 31 29  b REFERENCES t1)
3af0: 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a 64 6f 5f 74 65  ;.  }.} {}.do_te
3b00: 73 74 20 65 5f 66 6b 65 79 2d 34 36 2e 32 20 7b  st e_fkey-46.2 {
3b10: 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20  .  execsql {.   
3b20: 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 31 20   INSERT INTO t1 
3b30: 56 41 4c 55 45 53 28 27 6f 4e 65 27 29 3b 0a 20  VALUES('oNe');. 
3b40: 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74     INSERT INTO t
3b50: 32 20 56 41 4c 55 45 53 28 27 6f 6e 65 27 29 3b  2 VALUES('one');
3b60: 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f  .    INSERT INTO
3b70: 20 74 32 20 56 41 4c 55 45 53 28 27 4f 4e 45 27   t2 VALUES('ONE'
3b80: 29 3b 0a 20 20 20 20 55 50 44 41 54 45 20 74 32  );.    UPDATE t2
3b90: 20 53 45 54 20 62 20 3d 20 27 4f 6e 45 27 3b 0a   SET b = 'OnE';.
3ba0: 20 20 20 20 55 50 44 41 54 45 20 74 31 20 53 45      UPDATE t1 SE
3bb0: 54 20 61 20 3d 20 27 4f 4e 45 27 3b 0a 20 20 7d  T a = 'ONE';.  }
3bc0: 0a 7d 20 7b 7d 0a 64 6f 5f 74 65 73 74 20 65 5f  .} {}.do_test e_
3bd0: 66 6b 65 79 2d 34 36 2e 33 20 7b 0a 20 20 63 61  fkey-46.3 {.  ca
3be0: 74 63 68 73 71 6c 20 7b 20 55 50 44 41 54 45 20  tchsql { UPDATE 
3bf0: 74 32 20 53 45 54 20 62 20 3d 20 27 74 77 6f 27  t2 SET b = 'two'
3c00: 20 57 48 45 52 45 20 72 6f 77 69 64 20 3d 20 31   WHERE rowid = 1
3c10: 20 7d 0a 7d 20 7b 31 20 7b 66 6f 72 65 69 67 6e   }.} {1 {foreign
3c20: 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 20   key constraint 
3c30: 66 61 69 6c 65 64 7d 7d 0a 64 6f 5f 74 65 73 74  failed}}.do_test
3c40: 20 65 5f 66 6b 65 79 2d 34 36 2e 34 20 7b 0a 20   e_fkey-46.4 {. 
3c50: 20 63 61 74 63 68 73 71 6c 20 7b 20 44 45 4c 45   catchsql { DELE
3c60: 54 45 20 46 52 4f 4d 20 74 31 20 57 48 45 52 45  TE FROM t1 WHERE
3c70: 20 72 6f 77 69 64 20 3d 20 31 20 7d 0a 7d 20 7b   rowid = 1 }.} {
3c80: 31 20 7b 66 6f 72 65 69 67 6e 20 6b 65 79 20 63  1 {foreign key c
3c90: 6f 6e 73 74 72 61 69 6e 74 20 66 61 69 6c 65 64  onstraint failed
3ca0: 7d 7d 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  }}..#-----------
3cb0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
3cc0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
3cd0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
3ce0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23  --------------.#
3cf0: 20 2f 2a 20 45 56 3a 20 52 2d 30 34 32 34 30 2d   /* EV: R-04240-
3d00: 31 33 38 36 30 20 2a 2f 0a 23 0a 23 20 53 70 65  13860 */.#.# Spe
3d10: 63 69 66 69 63 61 6c 6c 79 2c 20 74 65 73 74 20  cifically, test 
3d20: 74 68 61 74 20 77 68 65 6e 20 63 6f 6d 70 61 72  that when compar
3d30: 69 6e 67 20 63 68 69 6c 64 20 61 6e 64 20 70 61  ing child and pa
3d40: 72 65 6e 74 20 6b 65 79 20 76 61 6c 75 65 73 20  rent key values 
3d50: 74 68 65 0a 23 20 61 66 66 69 6e 69 74 79 20 6f  the.# affinity o
3d60: 66 20 74 68 65 20 70 61 72 65 6e 74 20 6b 65 79  f the parent key
3d70: 20 63 6f 6c 75 6d 6e 20 69 73 20 61 70 70 6c 69   column is appli
3d80: 65 64 20 74 6f 20 74 68 65 20 63 68 69 6c 64 20  ed to the child 
3d90: 6b 65 79 20 76 61 6c 75 65 0a 23 20 62 65 66 6f  key value.# befo
3da0: 72 65 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f  re the compariso
3db0: 6e 20 74 61 6b 65 73 20 70 6c 61 63 65 2e 0a 23  n takes place..#
3dc0: 0a 64 72 6f 70 5f 61 6c 6c 5f 74 61 62 6c 65 73  .drop_all_tables
3dd0: 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d  .do_test e_fkey-
3de0: 34 37 2e 31 20 7b 0a 20 20 65 78 65 63 73 71 6c  47.1 {.  execsql
3df0: 20 7b 0a 20 20 20 20 43 52 45 41 54 45 20 54 41   {.    CREATE TA
3e00: 42 4c 45 20 74 31 28 61 20 4e 55 4d 45 52 49 43  BLE t1(a NUMERIC
3e10: 20 50 52 49 4d 41 52 59 20 4b 45 59 29 3b 0a 20   PRIMARY KEY);. 
3e20: 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20     CREATE TABLE 
3e30: 74 32 28 62 20 54 45 58 54 20 52 45 46 45 52 45  t2(b TEXT REFERE
3e40: 4e 43 45 53 20 74 31 29 3b 0a 20 20 7d 0a 7d 20  NCES t1);.  }.} 
3e50: 7b 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65  {}.do_test e_fke
3e60: 79 2d 34 37 2e 32 20 7b 0a 20 20 65 78 65 63 73  y-47.2 {.  execs
3e70: 71 6c 20 7b 0a 20 20 20 20 49 4e 53 45 52 54 20  ql {.    INSERT 
3e80: 49 4e 54 4f 20 74 31 20 56 41 4c 55 45 53 28 31  INTO t1 VALUES(1
3e90: 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e  );.    INSERT IN
3ea0: 54 4f 20 74 31 20 56 41 4c 55 45 53 28 32 29 3b  TO t1 VALUES(2);
3eb0: 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f  .    INSERT INTO
3ec0: 20 74 31 20 56 41 4c 55 45 53 28 27 74 68 72 65   t1 VALUES('thre
3ed0: 65 27 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20  e');.    INSERT 
3ee0: 49 4e 54 4f 20 74 32 20 56 41 4c 55 45 53 28 27  INTO t2 VALUES('
3ef0: 32 2e 30 27 29 3b 0a 20 20 20 20 53 45 4c 45 43  2.0');.    SELEC
3f00: 54 20 62 2c 20 74 79 70 65 6f 66 28 62 29 20 46  T b, typeof(b) F
3f10: 52 4f 4d 20 74 32 3b 0a 20 20 7d 0a 7d 20 7b 32  ROM t2;.  }.} {2
3f20: 2e 30 20 74 65 78 74 7d 0a 64 6f 5f 74 65 73 74  .0 text}.do_test
3f30: 20 65 5f 66 6b 65 79 2d 34 37 2e 33 20 7b 0a 20   e_fkey-47.3 {. 
3f40: 20 65 78 65 63 73 71 6c 20 7b 20 53 45 4c 45 43   execsql { SELEC
3f50: 54 20 74 79 70 65 6f 66 28 61 29 20 46 52 4f 4d  T typeof(a) FROM
3f60: 20 74 31 20 7d 0a 7d 20 7b 69 6e 74 65 67 65 72   t1 }.} {integer
3f70: 20 69 6e 74 65 67 65 72 20 74 65 78 74 7d 0a 64   integer text}.d
3f80: 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 34 37  o_test e_fkey-47
3f90: 2e 34 20 7b 0a 20 20 63 61 74 63 68 73 71 6c 20  .4 {.  catchsql 
3fa0: 7b 20 44 45 4c 45 54 45 20 46 52 4f 4d 20 74 31  { DELETE FROM t1
3fb0: 20 57 48 45 52 45 20 72 6f 77 69 64 20 3d 20 32   WHERE rowid = 2
3fc0: 20 7d 0a 7d 20 7b 31 20 7b 66 6f 72 65 69 67 6e   }.} {1 {foreign
3fd0: 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 20   key constraint 
3fe0: 66 61 69 6c 65 64 7d 7d 0a 0a 23 23 23 23 23 23  failed}}..######
3ff0: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
4000: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
4010: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
4020: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
4030: 23 23 23 23 23 0a 23 23 23 20 53 45 43 54 49 4f  #####.### SECTIO
4040: 4e 20 33 3a 20 52 65 71 75 69 72 65 64 20 61 6e  N 3: Required an
4050: 64 20 53 75 67 67 65 73 74 65 64 20 44 61 74 61  d Suggested Data
4060: 62 61 73 65 20 49 6e 64 65 78 65 73 0a 23 23 23  base Indexes.###
4070: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
4080: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
4090: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
40a0: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
40b0: 23 23 23 23 23 23 23 23 0a 0a 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 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
4100: 23 23 23 23 23 0a 23 23 23 20 53 45 43 54 49 4f  #####.### SECTIO
4110: 4e 20 34 2e 31 3a 20 43 6f 6d 70 6f 73 69 74 65  N 4.1: Composite
4120: 20 46 6f 72 65 69 67 6e 20 4b 65 79 20 43 6f 6e   Foreign Key Con
4130: 73 74 72 61 69 6e 74 73 0a 23 23 23 23 23 23 23  straints.#######
4140: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
4150: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
4160: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
4170: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
4180: 23 23 23 23 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d  ####..#---------
4190: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
41a0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
41b0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
41c0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
41d0: 0a 23 20 2f 2a 20 45 56 3a 20 52 2d 32 34 36 37  .# /* EV: R-2467
41e0: 36 2d 30 39 38 35 39 20 2a 2f 0a 23 0a 23 20 54  6-09859 */.#.# T
41f0: 65 73 74 20 74 68 65 20 65 78 61 6d 70 6c 65 20  est the example 
4200: 73 63 68 65 6d 61 20 69 6e 20 74 68 65 20 22 43  schema in the "C
4210: 6f 6d 70 6f 73 69 74 65 20 46 6f 72 65 69 67 6e  omposite Foreign
4220: 20 4b 65 79 20 43 6f 6e 73 74 72 61 69 6e 74 73   Key Constraints
4230: 22 20 0a 23 20 73 65 63 74 69 6f 6e 2e 0a 23 0a  " .# section..#.
4240: 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 33  do_test e_fkey-3
4250: 36 2e 31 20 7b 0a 20 20 65 78 65 63 73 71 6c 20  6.1 {.  execsql 
4260: 7b 0a 20 20 20 20 43 52 45 41 54 45 20 54 41 42  {.    CREATE TAB
4270: 4c 45 20 61 6c 62 75 6d 28 0a 20 20 20 20 20 20  LE album(.      
4280: 61 6c 62 75 6d 61 72 74 69 73 74 20 54 45 58 54  albumartist TEXT
4290: 2c 0a 20 20 20 20 20 20 61 6c 62 75 6d 6e 61 6d  ,.      albumnam
42a0: 65 20 54 45 58 54 2c 0a 20 20 20 20 20 20 61 6c  e TEXT,.      al
42b0: 62 75 6d 63 6f 76 65 72 20 42 49 4e 41 52 59 2c  bumcover BINARY,
42c0: 0a 20 20 20 20 20 20 50 52 49 4d 41 52 59 20 4b  .      PRIMARY K
42d0: 45 59 28 61 6c 62 75 6d 61 72 74 69 73 74 2c 20  EY(albumartist, 
42e0: 61 6c 62 75 6d 6e 61 6d 65 29 0a 20 20 20 20 29  albumname).    )
42f0: 3b 0a 20 20 20 20 43 52 45 41 54 45 20 54 41 42  ;.    CREATE TAB
4300: 4c 45 20 73 6f 6e 67 28 0a 20 20 20 20 20 20 73  LE song(.      s
4310: 6f 6e 67 69 64 20 49 4e 54 45 47 45 52 2c 0a 20  ongid INTEGER,. 
4320: 20 20 20 20 20 73 6f 6e 67 61 72 74 69 73 74 20       songartist 
4330: 54 45 58 54 2c 0a 20 20 20 20 20 20 73 6f 6e 67  TEXT,.      song
4340: 61 6c 62 75 6d 20 54 45 58 54 2c 0a 20 20 20 20  album TEXT,.    
4350: 20 20 73 6f 6e 67 6e 61 6d 65 20 54 45 58 54 2c    songname TEXT,
4360: 0a 20 20 20 20 20 20 46 4f 52 45 49 47 4e 20 4b  .      FOREIGN K
4370: 45 59 28 73 6f 6e 67 61 72 74 69 73 74 2c 20 73  EY(songartist, s
4380: 6f 6e 67 61 6c 62 75 6d 29 20 52 45 46 45 52 45  ongalbum) REFERE
4390: 4e 43 45 53 20 61 6c 62 75 6d 28 61 6c 62 75 6d  NCES album(album
43a0: 61 72 74 69 73 74 2c 61 6c 62 75 6d 6e 61 6d 65  artist,albumname
43b0: 29 0a 20 20 20 20 29 3b 0a 20 20 7d 0a 7d 20 7b  ).    );.  }.} {
43c0: 7d 0a 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65  }..do_test e_fke
43d0: 79 2d 33 36 2e 32 20 7b 0a 20 20 65 78 65 63 73  y-36.2 {.  execs
43e0: 71 6c 20 7b 0a 20 20 20 20 49 4e 53 45 52 54 20  ql {.    INSERT 
43f0: 49 4e 54 4f 20 61 6c 62 75 6d 20 56 41 4c 55 45  INTO album VALUE
4400: 53 28 27 45 6c 76 69 73 20 50 72 65 73 6c 65 79  S('Elvis Presley
4410: 27 2c 20 27 45 6c 76 69 73 27 27 20 43 68 72 69  ', 'Elvis'' Chri
4420: 73 74 6d 61 73 20 41 6c 62 75 6d 27 2c 20 4e 55  stmas Album', NU
4430: 4c 4c 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20  LL);.    INSERT 
4440: 49 4e 54 4f 20 73 6f 6e 67 20 56 41 4c 55 45 53  INTO song VALUES
4450: 28 0a 20 20 20 20 20 20 31 2c 20 27 45 6c 76 69  (.      1, 'Elvi
4460: 73 20 50 72 65 73 6c 65 79 27 2c 20 27 45 6c 76  s Presley', 'Elv
4470: 69 73 27 27 20 43 68 72 69 73 74 6d 61 73 20 41  is'' Christmas A
4480: 6c 62 75 6d 27 2c 20 27 48 65 72 65 20 43 6f 6d  lbum', 'Here Com
4490: 65 73 20 53 61 6e 74 61 20 43 6c 61 75 73 65 27  es Santa Clause'
44a0: 0a 20 20 20 20 29 3b 0a 20 20 7d 0a 7d 20 7b 7d  .    );.  }.} {}
44b0: 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d  .do_test e_fkey-
44c0: 33 36 2e 33 20 7b 0a 20 20 63 61 74 63 68 73 71  36.3 {.  catchsq
44d0: 6c 20 7b 0a 20 20 20 20 49 4e 53 45 52 54 20 49  l {.    INSERT I
44e0: 4e 54 4f 20 73 6f 6e 67 20 56 41 4c 55 45 53 28  NTO song VALUES(
44f0: 32 2c 20 27 45 6c 76 69 73 20 50 72 65 73 6c 65  2, 'Elvis Presle
4500: 79 27 2c 20 27 45 6c 76 69 73 20 49 73 20 42 61  y', 'Elvis Is Ba
4510: 63 6b 21 27 2c 20 27 46 65 76 65 72 27 29 3b 0a  ck!', 'Fever');.
4520: 20 20 7d 0a 7d 20 7b 31 20 7b 66 6f 72 65 69 67    }.} {1 {foreig
4530: 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74  n key constraint
4540: 20 66 61 69 6c 65 64 7d 7d 0a 0a 23 2d 2d 2d 2d   failed}}..#----
4550: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
4560: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
4570: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
4580: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
4590: 2d 2d 2d 2d 2d 0a 23 20 2f 2a 20 45 56 3a 20 52  -----.# /* EV: R
45a0: 2d 33 33 36 32 36 2d 34 38 34 31 38 20 2a 2f 0a  -33626-48418 */.
45b0: 23 0a 23 20 43 68 65 63 6b 20 74 68 61 74 20 69  #.# Check that i
45c0: 66 20 61 6e 79 20 6f 66 20 74 68 65 20 63 68 69  f any of the chi
45d0: 6c 64 20 6b 65 79 20 63 6f 6c 75 6d 6e 73 20 69  ld key columns i
45e0: 6e 20 74 68 65 20 61 62 6f 76 65 20 73 63 68 65  n the above sche
45f0: 6d 61 20 61 72 65 20 4e 55 4c 4c 2c 0a 23 20 74  ma are NULL,.# t
4600: 68 65 72 65 20 69 73 20 6e 6f 20 72 65 71 75 69  here is no requi
4610: 72 65 6d 65 6e 74 20 66 6f 72 20 61 20 63 6f 72  rement for a cor
4620: 72 65 73 70 6f 6e 64 69 6e 67 20 70 61 72 65 6e  responding paren
4630: 74 20 6b 65 79 2e 0a 23 0a 64 6f 5f 74 65 73 74  t key..#.do_test
4640: 20 65 5f 66 6b 65 79 2d 33 37 2e 31 20 7b 0a 20   e_fkey-37.1 {. 
4650: 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 49   execsql {.    I
4660: 4e 53 45 52 54 20 49 4e 54 4f 20 73 6f 6e 67 20  NSERT INTO song 
4670: 56 41 4c 55 45 53 28 32 2c 20 27 45 6c 76 69 73  VALUES(2, 'Elvis
4680: 20 50 72 65 73 6c 65 79 27 2c 20 4e 55 4c 4c 2c   Presley', NULL,
4690: 20 27 46 65 76 65 72 27 29 3b 0a 20 20 20 20 49   'Fever');.    I
46a0: 4e 53 45 52 54 20 49 4e 54 4f 20 73 6f 6e 67 20  NSERT INTO song 
46b0: 56 41 4c 55 45 53 28 33 2c 20 4e 55 4c 4c 2c 20  VALUES(3, NULL, 
46c0: 27 45 6c 76 69 73 20 49 73 20 42 61 63 6b 27 2c  'Elvis Is Back',
46d0: 20 27 53 6f 6c 64 69 65 72 20 42 6f 79 27 29 3b   'Soldier Boy');
46e0: 0a 20 20 7d 0a 7d 20 7b 7d 0a 0a 23 23 23 23 23  .  }.} {}..#####
46f0: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
4700: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
4710: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
4720: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
4730: 23 23 23 23 23 23 0a 23 23 23 20 53 45 43 54 49  ######.### SECTI
4740: 4f 4e 20 34 2e 32 3a 20 44 65 66 65 72 72 65 64  ON 4.2: Deferred
4750: 20 46 6f 72 65 69 67 6e 20 4b 65 79 20 43 6f 6e   Foreign Key Con
4760: 73 74 72 61 69 6e 74 73 0a 23 23 23 23 23 23 23  straints.#######
4770: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
4780: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
4790: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
47a0: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
47b0: 23 23 23 23 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d  ####..#---------
47c0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
47d0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
47e0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
47f0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
4800: 0a 23 20 4e 6f 74 65 3a 20 52 2d 33 35 32 39 30  .# Note: R-35290
4810: 2d 31 36 34 36 30 20 69 73 20 74 65 73 74 65 64  -16460 is tested
4820: 20 62 65 6c 6f 77 2e 0a 23 0a 23 20 54 4f 44 4f   below..#.# TODO
4830: 3a 20 52 2d 33 30 33 32 33 2d 32 31 39 31 37 0a  : R-30323-21917.
4840: 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  .#--------------
4850: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
4860: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
4870: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
4880: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20 2f 2a  -----------.# /*
4890: 20 45 56 3a 20 52 2d 30 39 33 32 33 2d 33 30 34   EV: R-09323-304
48a0: 37 30 20 2a 2f 0a 23 0a 23 20 54 65 73 74 20 74  70 */.#.# Test t
48b0: 68 61 74 20 69 66 20 61 20 73 74 61 74 65 6d 65  hat if a stateme
48c0: 6e 74 20 76 69 6f 6c 61 74 65 73 20 61 6e 20 69  nt violates an i
48d0: 6d 6d 65 64 69 61 74 65 20 46 4b 20 63 6f 6e 73  mmediate FK cons
48e0: 74 72 61 69 6e 74 2c 20 61 6e 64 20 74 68 65 0a  traint, and the.
48f0: 23 20 64 61 74 61 62 61 73 65 20 64 6f 65 73 20  # database does 
4900: 6e 6f 74 20 73 61 74 69 73 66 79 20 74 68 65 20  not satisfy the 
4910: 46 4b 20 63 6f 6e 73 74 72 61 69 6e 74 20 6f 6e  FK constraint on
4920: 63 65 20 61 6c 6c 20 65 66 66 65 63 74 73 20 6f  ce all effects o
4930: 66 20 74 68 65 0a 23 20 73 74 61 74 65 6d 65 6e  f the.# statemen
4940: 74 20 68 61 76 65 20 62 65 65 6e 20 61 70 70 6c  t have been appl
4950: 69 65 64 2c 20 61 6e 20 65 72 72 6f 72 20 69 73  ied, an error is
4960: 20 72 65 70 6f 72 74 65 64 20 61 6e 64 20 74 68   reported and th
4970: 65 20 65 66 66 65 63 74 73 20 6f 66 0a 23 20 74  e effects of.# t
4980: 68 65 20 73 74 61 74 65 6d 65 6e 74 20 72 6f 6c  he statement rol
4990: 6c 65 64 20 62 61 63 6b 2e 0a 23 0a 64 72 6f 70  led back..#.drop
49a0: 5f 61 6c 6c 5f 74 61 62 6c 65 73 0a 64 6f 5f 74  _all_tables.do_t
49b0: 65 73 74 20 65 5f 66 6b 65 79 2d 33 33 2e 31 20  est e_fkey-33.1 
49c0: 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20  {.  execsql {.  
49d0: 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 6b    CREATE TABLE k
49e0: 69 6e 67 28 61 2c 20 62 2c 20 50 52 49 4d 41 52  ing(a, b, PRIMAR
49f0: 59 20 4b 45 59 28 61 29 29 3b 0a 20 20 20 20 43  Y KEY(a));.    C
4a00: 52 45 41 54 45 20 54 41 42 4c 45 20 70 72 69 6e  REATE TABLE prin
4a10: 63 65 28 63 20 52 45 46 45 52 45 4e 43 45 53 20  ce(c REFERENCES 
4a20: 6b 69 6e 67 2c 20 64 29 3b 0a 20 20 7d 0a 7d 20  king, d);.  }.} 
4a30: 7b 7d 0a 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b  {}..do_test e_fk
4a40: 65 79 2d 33 33 2e 32 20 7b 0a 20 20 23 20 45 78  ey-33.2 {.  # Ex
4a50: 65 63 75 74 65 20 61 20 73 74 61 74 65 6d 65 6e  ecute a statemen
4a60: 74 20 74 68 61 74 20 76 69 6f 6c 61 74 65 73 20  t that violates 
4a70: 74 68 65 20 69 6d 6d 65 64 69 61 74 65 20 46 4b  the immediate FK
4a80: 20 63 6f 6e 73 74 72 61 69 6e 74 2e 0a 20 20 63   constraint..  c
4a90: 61 74 63 68 73 71 6c 20 7b 20 49 4e 53 45 52 54  atchsql { INSERT
4aa0: 20 49 4e 54 4f 20 70 72 69 6e 63 65 20 56 41 4c   INTO prince VAL
4ab0: 55 45 53 28 31 2c 20 32 29 20 7d 0a 7d 20 7b 31  UES(1, 2) }.} {1
4ac0: 20 7b 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f   {foreign key co
4ad0: 6e 73 74 72 61 69 6e 74 20 66 61 69 6c 65 64 7d  nstraint failed}
4ae0: 7d 0a 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65  }..do_test e_fke
4af0: 79 2d 33 33 2e 33 20 7b 0a 20 20 23 20 54 68 69  y-33.3 {.  # Thi
4b00: 73 20 74 69 6d 65 2c 20 75 73 65 20 61 20 74 72  s time, use a tr
4b10: 69 67 67 65 72 20 74 6f 20 66 69 78 20 74 68 65  igger to fix the
4b20: 20 63 6f 6e 73 74 72 61 69 6e 74 20 76 69 6f 6c   constraint viol
4b30: 61 74 69 6f 6e 20 62 65 66 6f 72 65 20 74 68 65  ation before the
4b40: 0a 20 20 23 20 73 74 61 74 65 6d 65 6e 74 20 68  .  # statement h
4b50: 61 73 20 66 69 6e 69 73 68 65 64 20 65 78 65 63  as finished exec
4b60: 75 74 69 6e 67 2e 20 54 68 65 6e 20 65 78 65 63  uting. Then exec
4b70: 75 74 65 20 74 68 65 20 73 61 6d 65 20 73 74 61  ute the same sta
4b80: 74 65 6d 65 6e 74 20 61 73 0a 20 20 23 20 69 6e  tement as.  # in
4b90: 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 74 65   the previous te
4ba0: 73 74 20 63 61 73 65 2e 20 54 68 69 73 20 74 69  st case. This ti
4bb0: 6d 65 2c 20 6e 6f 20 65 72 72 6f 72 2e 0a 20 20  me, no error..  
4bc0: 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 43 52  execsql {.    CR
4bd0: 45 41 54 45 20 54 52 49 47 47 45 52 20 6b 74 20  EATE TRIGGER kt 
4be0: 41 46 54 45 52 20 49 4e 53 45 52 54 20 4f 4e 20  AFTER INSERT ON 
4bf0: 70 72 69 6e 63 65 20 57 48 45 4e 0a 20 20 20 20  prince WHEN.    
4c00: 20 20 4e 4f 54 20 45 58 49 53 54 53 20 28 53 45    NOT EXISTS (SE
4c10: 4c 45 43 54 20 61 20 46 52 4f 4d 20 6b 69 6e 67  LECT a FROM king
4c20: 20 57 48 45 52 45 20 61 20 3d 20 6e 65 77 2e 63   WHERE a = new.c
4c30: 29 0a 20 20 20 20 42 45 47 49 4e 0a 20 20 20 20  ).    BEGIN.    
4c40: 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 6b 69    INSERT INTO ki
4c50: 6e 67 20 56 41 4c 55 45 53 28 6e 65 77 2e 63 2c  ng VALUES(new.c,
4c60: 20 4e 55 4c 4c 29 3b 0a 20 20 20 20 45 4e 44 0a   NULL);.    END.
4c70: 20 20 7d 0a 20 20 65 78 65 63 73 71 6c 20 7b 20    }.  execsql { 
4c80: 49 4e 53 45 52 54 20 49 4e 54 4f 20 70 72 69 6e  INSERT INTO prin
4c90: 63 65 20 56 41 4c 55 45 53 28 31 2c 20 32 29 20  ce VALUES(1, 2) 
4ca0: 7d 0a 7d 20 7b 7d 0a 0a 23 20 54 65 73 74 20 74  }.} {}..# Test t
4cb0: 68 61 74 20 6f 70 65 72 61 74 69 6e 67 20 69 6e  hat operating in
4cc0: 73 69 64 65 20 61 20 74 72 61 6e 73 61 63 74 69  side a transacti
4cd0: 6f 6e 20 6d 61 6b 65 73 20 6e 6f 20 64 69 66 66  on makes no diff
4ce0: 65 72 65 6e 63 65 20 74 6f 20 0a 23 20 69 6d 6d  erence to .# imm
4cf0: 65 64 69 61 74 65 20 63 6f 6e 73 74 72 61 69 6e  ediate constrain
4d00: 74 20 76 69 6f 6c 61 74 69 6f 6e 20 68 61 6e 64  t violation hand
4d10: 6c 69 6e 67 2e 0a 64 6f 5f 74 65 73 74 20 65 5f  ling..do_test e_
4d20: 66 6b 65 79 2d 33 33 2e 34 20 7b 0a 20 20 65 78  fkey-33.4 {.  ex
4d30: 65 63 73 71 6c 20 7b 0a 20 20 20 20 42 45 47 49  ecsql {.    BEGI
4d40: 4e 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e  N;.    INSERT IN
4d50: 54 4f 20 70 72 69 6e 63 65 20 56 41 4c 55 45 53  TO prince VALUES
4d60: 28 32 2c 20 33 29 3b 0a 20 20 20 20 44 52 4f 50  (2, 3);.    DROP
4d70: 20 54 52 49 47 47 45 52 20 6b 74 3b 0a 20 20 7d   TRIGGER kt;.  }
4d80: 0a 20 20 63 61 74 63 68 73 71 6c 20 7b 20 49 4e  .  catchsql { IN
4d90: 53 45 52 54 20 49 4e 54 4f 20 70 72 69 6e 63 65  SERT INTO prince
4da0: 20 56 41 4c 55 45 53 28 33 2c 20 34 29 20 7d 0a   VALUES(3, 4) }.
4db0: 7d 20 7b 31 20 7b 66 6f 72 65 69 67 6e 20 6b 65  } {1 {foreign ke
4dc0: 79 20 63 6f 6e 73 74 72 61 69 6e 74 20 66 61 69  y constraint fai
4dd0: 6c 65 64 7d 7d 0a 64 6f 5f 74 65 73 74 20 65 5f  led}}.do_test e_
4de0: 66 6b 65 79 2d 33 33 2e 35 20 7b 0a 20 20 65 78  fkey-33.5 {.  ex
4df0: 65 63 73 71 6c 20 7b 0a 20 20 20 20 43 4f 4d 4d  ecsql {.    COMM
4e00: 49 54 3b 0a 20 20 20 20 53 45 4c 45 43 54 20 2a  IT;.    SELECT *
4e10: 20 46 52 4f 4d 20 6b 69 6e 67 3b 0a 20 20 7d 0a   FROM king;.  }.
4e20: 7d 20 7b 31 20 7b 7d 20 32 20 7b 7d 7d 0a 0a 23  } {1 {} 2 {}}..#
4e30: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
4e40: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
4e50: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
4e60: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
4e70: 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20 2f 2a 20 45  ---------.# /* E
4e80: 56 3a 20 52 2d 34 39 31 37 38 2d 32 31 33 35 38  V: R-49178-21358
4e90: 20 2a 2f 0a 23 20 2f 2a 20 45 56 3a 20 52 2d 33   */.# /* EV: R-3
4ea0: 39 36 39 32 2d 31 32 34 38 38 20 2a 2f 0a 23 20  9692-12488 */.# 
4eb0: 2f 2a 20 45 56 3a 20 52 2d 35 35 31 34 37 2d 34  /* EV: R-55147-4
4ec0: 37 36 36 34 20 2a 2f 0a 23 20 2f 2a 20 45 56 3a  7664 */.# /* EV:
4ed0: 20 52 2d 32 39 36 30 34 2d 33 30 33 39 35 20 2a   R-29604-30395 *
4ee0: 2f 0a 23 0a 23 20 54 65 73 74 20 74 68 61 74 20  /.#.# Test that 
4ef0: 69 66 20 61 20 64 65 66 65 72 72 65 64 20 63 6f  if a deferred co
4f00: 6e 73 74 72 61 69 6e 74 20 69 73 20 76 69 6f 6c  nstraint is viol
4f10: 61 74 65 64 20 77 69 74 68 69 6e 20 61 20 74 72  ated within a tr
4f20: 61 6e 73 61 63 74 69 6f 6e 2c 0a 23 20 6e 6f 74  ansaction,.# not
4f30: 68 69 6e 67 20 68 61 70 70 65 6e 73 20 69 6d 6d  hing happens imm
4f40: 65 64 69 61 74 65 6c 79 20 61 6e 64 20 74 68 65  ediately and the
4f50: 20 64 61 74 61 62 61 73 65 20 69 73 20 61 6c 6c   database is all
4f60: 6f 77 65 64 20 74 6f 20 70 65 72 73 69 73 74 0a  owed to persist.
4f70: 23 20 69 6e 20 61 20 73 74 61 74 65 20 74 68 61  # in a state tha
4f80: 74 20 64 6f 65 73 20 6e 6f 74 20 73 61 74 69 73  t does not satis
4f90: 66 79 20 74 68 65 20 46 4b 20 63 6f 6e 73 74 72  fy the FK constr
4fa0: 61 69 6e 74 2e 20 48 6f 77 65 76 65 72 20 61 74  aint. However at
4fb0: 74 65 6d 70 74 73 0a 23 20 74 6f 20 43 4f 4d 4d  tempts.# to COMM
4fc0: 49 54 20 74 68 65 20 74 72 61 6e 73 61 63 74 69  IT the transacti
4fd0: 6f 6e 20 66 61 69 6c 20 75 6e 74 69 6c 20 74 68  on fail until th
4fe0: 65 20 46 4b 20 63 6f 6e 73 74 72 61 69 6e 74 20  e FK constraint 
4ff0: 69 73 20 73 61 74 69 73 66 69 65 64 2e 0a 23 0a  is satisfied..#.
5000: 70 72 6f 63 20 74 65 73 74 5f 65 66 6b 65 79 5f  proc test_efkey_
5010: 33 34 20 7b 74 6e 20 69 73 45 72 72 6f 72 20 73  34 {tn isError s
5020: 71 6c 7d 20 7b 0a 20 20 64 6f 5f 74 65 73 74 20  ql} {.  do_test 
5030: 65 5f 66 6b 65 79 2d 33 34 2e 24 74 6e 20 22 0a  e_fkey-34.$tn ".
5040: 20 20 20 20 63 61 74 63 68 73 71 6c 20 7b 24 73      catchsql {$s
5050: 71 6c 7d 0a 20 20 22 20 5b 6c 69 6e 64 65 78 20  ql}.  " [lindex 
5060: 7b 7b 30 20 7b 7d 7d 20 7b 31 20 7b 66 6f 72 65  {{0 {}} {1 {fore
5070: 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69  ign key constrai
5080: 6e 74 20 66 61 69 6c 65 64 7d 7d 7d 20 24 69 73  nt failed}}} $is
5090: 45 72 72 6f 72 5d 0a 7d 0a 64 72 6f 70 5f 61 6c  Error].}.drop_al
50a0: 6c 5f 74 61 62 6c 65 73 0a 0a 74 65 73 74 5f 65  l_tables..test_e
50b0: 66 6b 65 79 5f 33 34 20 20 31 20 30 20 7b 0a 20  fkey_34  1 0 {. 
50c0: 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 6c 6c   CREATE TABLE ll
50d0: 28 6b 20 50 52 49 4d 41 52 59 20 4b 45 59 29 3b  (k PRIMARY KEY);
50e0: 0a 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20  .  CREATE TABLE 
50f0: 6b 6b 28 63 20 52 45 46 45 52 45 4e 43 45 53 20  kk(c REFERENCES 
5100: 6c 6c 20 44 45 46 45 52 52 41 42 4c 45 20 49 4e  ll DEFERRABLE IN
5110: 49 54 49 41 4c 4c 59 20 44 45 46 45 52 52 45 44  ITIALLY DEFERRED
5120: 29 3b 0a 7d 0a 74 65 73 74 5f 65 66 6b 65 79 5f  );.}.test_efkey_
5130: 33 34 20 20 32 20 30 20 22 42 45 47 49 4e 22 0a  34  2 0 "BEGIN".
5140: 74 65 73 74 5f 65 66 6b 65 79 5f 33 34 20 20 33  test_efkey_34  3
5150: 20 30 20 20 20 22 49 4e 53 45 52 54 20 49 4e 54   0   "INSERT INT
5160: 4f 20 6b 6b 20 56 41 4c 55 45 53 28 35 29 22 0a  O kk VALUES(5)".
5170: 74 65 73 74 5f 65 66 6b 65 79 5f 33 34 20 20 34  test_efkey_34  4
5180: 20 30 20 20 20 22 49 4e 53 45 52 54 20 49 4e 54   0   "INSERT INT
5190: 4f 20 6b 6b 20 56 41 4c 55 45 53 28 31 30 29 22  O kk VALUES(10)"
51a0: 0a 74 65 73 74 5f 65 66 6b 65 79 5f 33 34 20 20  .test_efkey_34  
51b0: 35 20 31 20 22 43 4f 4d 4d 49 54 22 0a 74 65 73  5 1 "COMMIT".tes
51c0: 74 5f 65 66 6b 65 79 5f 33 34 20 20 36 20 30 20  t_efkey_34  6 0 
51d0: 20 20 22 49 4e 53 45 52 54 20 49 4e 54 4f 20 6c    "INSERT INTO l
51e0: 6c 20 56 41 4c 55 45 53 28 31 30 29 22 0a 74 65  l VALUES(10)".te
51f0: 73 74 5f 65 66 6b 65 79 5f 33 34 20 20 37 20 31  st_efkey_34  7 1
5200: 20 22 43 4f 4d 4d 49 54 22 0a 74 65 73 74 5f 65   "COMMIT".test_e
5210: 66 6b 65 79 5f 33 34 20 20 38 20 30 20 20 20 22  fkey_34  8 0   "
5220: 49 4e 53 45 52 54 20 49 4e 54 4f 20 6c 6c 20 56  INSERT INTO ll V
5230: 41 4c 55 45 53 28 35 29 22 0a 74 65 73 74 5f 65  ALUES(5)".test_e
5240: 66 6b 65 79 5f 33 34 20 20 39 20 30 20 22 43 4f  fkey_34  9 0 "CO
5250: 4d 4d 49 54 22 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d  MMIT"..#--------
5260: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
5270: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
5280: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
5290: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
52a0: 2d 0a 23 20 2f 2a 20 45 56 3a 20 52 2d 35 36 38  -.# /* EV: R-568
52b0: 34 34 2d 36 31 37 30 35 20 2a 2f 0a 23 0a 23 20  44-61705 */.#.# 
52c0: 57 68 65 6e 20 6e 6f 74 20 72 75 6e 6e 69 6e 67  When not running
52d0: 20 69 6e 73 69 64 65 20 61 20 74 72 61 6e 73 61   inside a transa
52e0: 63 74 69 6f 6e 2c 20 61 20 64 65 66 65 72 72 65  ction, a deferre
52f0: 64 20 63 6f 6e 73 74 72 61 69 6e 74 20 69 73 20  d constraint is 
5300: 73 69 6d 69 6c 61 72 0a 23 20 74 6f 20 61 6e 20  similar.# to an 
5310: 69 6d 6d 65 64 69 61 74 65 20 63 6f 6e 73 74 72  immediate constr
5320: 61 69 6e 74 20 28 76 69 6f 6c 61 74 69 6f 6e 73  aint (violations
5330: 20 61 72 65 20 72 65 70 6f 72 74 65 64 20 69 6d   are reported im
5340: 6d 65 64 69 61 74 65 6c 79 29 2e 0a 23 0a 64 72  mediately)..#.dr
5350: 6f 70 5f 61 6c 6c 5f 74 61 62 6c 65 73 0a 70 72  op_all_tables.pr
5360: 6f 63 20 74 65 73 74 5f 65 66 6b 65 79 5f 33 35  oc test_efkey_35
5370: 20 7b 74 6e 20 69 73 45 72 72 6f 72 20 73 71 6c   {tn isError sql
5380: 7d 20 7b 0a 20 20 64 6f 5f 74 65 73 74 20 65 5f  } {.  do_test e_
5390: 66 6b 65 79 2d 33 35 2e 24 74 6e 20 22 0a 20 20  fkey-35.$tn ".  
53a0: 20 20 63 61 74 63 68 73 71 6c 20 7b 24 73 71 6c    catchsql {$sql
53b0: 7d 0a 20 20 22 20 5b 6c 69 6e 64 65 78 20 7b 7b  }.  " [lindex {{
53c0: 30 20 7b 7d 7d 20 7b 31 20 7b 66 6f 72 65 69 67  0 {}} {1 {foreig
53d0: 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74  n key constraint
53e0: 20 66 61 69 6c 65 64 7d 7d 7d 20 24 69 73 45 72   failed}}} $isEr
53f0: 72 6f 72 5d 0a 7d 0a 64 6f 5f 74 65 73 74 20 65  ror].}.do_test e
5400: 5f 66 6b 65 79 2d 33 35 2e 31 20 7b 0a 20 20 65  _fkey-35.1 {.  e
5410: 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 43 52 45  xecsql {.    CRE
5420: 41 54 45 20 54 41 42 4c 45 20 70 61 72 65 6e 74  ATE TABLE parent
5430: 28 78 2c 20 79 29 3b 0a 20 20 20 20 43 52 45 41  (x, y);.    CREA
5440: 54 45 20 55 4e 49 51 55 45 20 49 4e 44 45 58 20  TE UNIQUE INDEX 
5450: 70 69 20 4f 4e 20 70 61 72 65 6e 74 28 78 2c 20  pi ON parent(x, 
5460: 79 29 3b 0a 20 20 20 20 43 52 45 41 54 45 20 54  y);.    CREATE T
5470: 41 42 4c 45 20 63 68 69 6c 64 28 61 2c 20 62 2c  ABLE child(a, b,
5480: 0a 20 20 20 20 20 20 46 4f 52 45 49 47 4e 20 4b  .      FOREIGN K
5490: 45 59 28 61 2c 20 62 29 20 52 45 46 45 52 45 4e  EY(a, b) REFEREN
54a0: 43 45 53 20 70 61 72 65 6e 74 28 78 2c 20 79 29  CES parent(x, y)
54b0: 20 44 45 46 45 52 52 41 42 4c 45 20 49 4e 49 54   DEFERRABLE INIT
54c0: 49 41 4c 4c 59 20 44 45 46 45 52 52 45 44 0a 20  IALLY DEFERRED. 
54d0: 20 20 20 29 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a 74     );.  }.} {}.t
54e0: 65 73 74 5f 65 66 6b 65 79 5f 33 35 20 32 20 31  est_efkey_35 2 1
54f0: 20 22 49 4e 53 45 52 54 20 49 4e 54 4f 20 63 68   "INSERT INTO ch
5500: 69 6c 64 20 20 56 41 4c 55 45 53 28 27 78 27 2c  ild  VALUES('x',
5510: 20 27 79 27 29 22 0a 74 65 73 74 5f 65 66 6b 65   'y')".test_efke
5520: 79 5f 33 35 20 33 20 30 20 22 49 4e 53 45 52 54  y_35 3 0 "INSERT
5530: 20 49 4e 54 4f 20 70 61 72 65 6e 74 20 56 41 4c   INTO parent VAL
5540: 55 45 53 28 27 78 27 2c 20 27 79 27 29 22 0a 74  UES('x', 'y')".t
5550: 65 73 74 5f 65 66 6b 65 79 5f 33 35 20 34 20 30  est_efkey_35 4 0
5560: 20 22 49 4e 53 45 52 54 20 49 4e 54 4f 20 63 68   "INSERT INTO ch
5570: 69 6c 64 20 20 56 41 4c 55 45 53 28 27 78 27 2c  ild  VALUES('x',
5580: 20 27 79 27 29 22 0a 0a 0a 23 2d 2d 2d 2d 2d 2d   'y')"...#------
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 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
55c0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
55d0: 2d 2d 2d 0a 23 20 2f 2a 20 45 56 3a 20 52 2d 31  ---.# /* EV: R-1
55e0: 32 37 38 32 2d 36 31 38 34 31 20 2a 2f 0a 23 0a  2782-61841 */.#.
55f0: 23 20 54 65 73 74 20 74 68 61 74 20 61 6e 20 46  # Test that an F
5600: 4b 20 63 6f 6e 73 74 72 61 69 6e 74 20 69 73 20  K constraint is 
5610: 6d 61 64 65 20 64 65 66 65 72 72 65 64 20 62 79  made deferred by
5620: 20 61 64 64 69 6e 67 20 74 68 65 20 66 6f 6c 6c   adding the foll
5630: 6f 77 69 6e 67 0a 23 20 74 6f 20 74 68 65 20 64  owing.# to the d
5640: 65 66 69 6e 69 74 69 6f 6e 3a 0a 23 0a 23 20 20  efinition:.#.#  
5650: 20 44 45 46 45 52 52 41 42 4c 45 20 49 4e 49 54   DEFERRABLE INIT
5660: 49 41 4c 4c 59 20 44 45 46 45 52 52 45 44 0a 23  IALLY DEFERRED.#
5670: 0a 23 20 2f 2a 20 45 56 3a 20 52 2d 30 39 30 30  .# /* EV: R-0900
5680: 35 2d 32 38 37 39 31 20 2a 2f 0a 23 0a 23 20 41  5-28791 */.#.# A
5690: 6c 73 6f 20 74 65 73 74 20 74 68 61 74 20 61 64  lso test that ad
56a0: 64 69 6e 67 20 61 6e 79 20 6f 66 20 74 68 65 20  ding any of the 
56b0: 66 6f 6c 6c 6f 77 69 6e 67 20 74 6f 20 61 20 66  following to a f
56c0: 6f 72 65 69 67 6e 20 6b 65 79 20 64 65 66 69 6e  oreign key defin
56d0: 69 74 69 6f 6e 20 0a 23 20 6d 61 6b 65 73 20 74  ition .# makes t
56e0: 68 65 20 63 6f 6e 73 74 72 61 69 6e 74 20 49 4d  he constraint IM
56f0: 4d 45 44 49 41 54 45 3a 0a 23 0a 23 20 20 20 4e  MEDIATE:.#.#   N
5700: 4f 54 20 44 45 46 45 52 52 41 42 4c 45 20 49 4e  OT DEFERRABLE IN
5710: 49 54 49 41 4c 4c 59 20 44 45 46 45 52 52 45 44  ITIALLY DEFERRED
5720: 0a 23 20 20 20 4e 4f 54 20 44 45 46 45 52 52 41  .#   NOT DEFERRA
5730: 42 4c 45 20 49 4e 49 54 49 41 4c 4c 59 20 49 4d  BLE INITIALLY IM
5740: 4d 45 44 49 41 54 45 0a 23 20 20 20 4e 4f 54 20  MEDIATE.#   NOT 
5750: 44 45 46 45 52 52 41 42 4c 45 0a 23 20 20 20 44  DEFERRABLE.#   D
5760: 45 46 45 52 52 41 42 4c 45 20 49 4e 49 54 49 41  EFERRABLE INITIA
5770: 4c 4c 59 20 49 4d 4d 45 44 49 41 54 45 0a 23 20  LLY IMMEDIATE.# 
5780: 20 20 44 45 46 45 52 52 41 42 4c 45 0a 23 0a 23    DEFERRABLE.#.#
5790: 20 2f 2a 20 45 56 3a 20 52 2d 33 35 32 39 30 2d   /* EV: R-35290-
57a0: 31 36 34 36 30 20 2a 2f 0a 23 0a 23 20 46 6f 72  16460 */.#.# For
57b0: 65 69 67 6e 20 6b 65 79 73 20 61 72 65 20 49 4d  eign keys are IM
57c0: 4d 45 44 49 41 54 45 20 62 79 20 64 65 66 61 75  MEDIATE by defau
57d0: 6c 74 20 28 69 66 20 74 68 65 72 65 20 69 73 20  lt (if there is 
57e0: 6e 6f 20 44 45 46 45 52 52 41 42 4c 45 20 6f 72  no DEFERRABLE or
57f0: 20 4e 4f 54 0a 23 20 44 45 46 45 52 52 41 42 4c   NOT.# DEFERRABL
5800: 45 20 63 6c 61 75 73 65 29 2e 0a 23 0a 64 72 6f  E clause)..#.dro
5810: 70 5f 61 6c 6c 5f 74 61 62 6c 65 73 0a 64 6f 5f  p_all_tables.do_
5820: 74 65 73 74 20 65 5f 66 6b 65 79 2d 32 39 2e 31  test e_fkey-29.1
5830: 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20   {.  execsql {. 
5840: 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20     CREATE TABLE 
5850: 70 61 72 65 6e 74 28 78 2c 20 79 2c 20 7a 2c 20  parent(x, y, z, 
5860: 50 52 49 4d 41 52 59 20 4b 45 59 28 78 2c 79 2c  PRIMARY KEY(x,y,
5870: 7a 29 29 3b 0a 20 20 20 20 43 52 45 41 54 45 20  z));.    CREATE 
5880: 54 41 42 4c 45 20 63 31 28 61 2c 20 62 2c 20 63  TABLE c1(a, b, c
5890: 2c 0a 20 20 20 20 20 20 46 4f 52 45 49 47 4e 20  ,.      FOREIGN 
58a0: 4b 45 59 28 61 2c 20 62 2c 20 63 29 20 52 45 46  KEY(a, b, c) REF
58b0: 45 52 45 4e 43 45 53 20 70 61 72 65 6e 74 20 4e  ERENCES parent N
58c0: 4f 54 20 44 45 46 45 52 52 41 42 4c 45 20 49 4e  OT DEFERRABLE IN
58d0: 49 54 49 41 4c 4c 59 20 44 45 46 45 52 52 45 44  ITIALLY DEFERRED
58e0: 0a 20 20 20 20 29 3b 0a 20 20 20 20 43 52 45 41  .    );.    CREA
58f0: 54 45 20 54 41 42 4c 45 20 63 32 28 61 2c 20 62  TE TABLE c2(a, b
5900: 2c 20 63 2c 0a 20 20 20 20 20 20 46 4f 52 45 49  , c,.      FOREI
5910: 47 4e 20 4b 45 59 28 61 2c 20 62 2c 20 63 29 20  GN KEY(a, b, c) 
5920: 52 45 46 45 52 45 4e 43 45 53 20 70 61 72 65 6e  REFERENCES paren
5930: 74 20 4e 4f 54 20 44 45 46 45 52 52 41 42 4c 45  t NOT DEFERRABLE
5940: 20 49 4e 49 54 49 41 4c 4c 59 20 49 4d 4d 45 44   INITIALLY IMMED
5950: 49 41 54 45 0a 20 20 20 20 29 3b 0a 20 20 20 20  IATE.    );.    
5960: 43 52 45 41 54 45 20 54 41 42 4c 45 20 63 33 28  CREATE TABLE c3(
5970: 61 2c 20 62 2c 20 63 2c 0a 20 20 20 20 20 20 46  a, b, c,.      F
5980: 4f 52 45 49 47 4e 20 4b 45 59 28 61 2c 20 62 2c  OREIGN KEY(a, b,
5990: 20 63 29 20 52 45 46 45 52 45 4e 43 45 53 20 70   c) REFERENCES p
59a0: 61 72 65 6e 74 20 4e 4f 54 20 44 45 46 45 52 52  arent NOT DEFERR
59b0: 41 42 4c 45 0a 20 20 20 20 29 3b 0a 20 20 20 20  ABLE.    );.    
59c0: 43 52 45 41 54 45 20 54 41 42 4c 45 20 63 34 28  CREATE TABLE c4(
59d0: 61 2c 20 62 2c 20 63 2c 0a 20 20 20 20 20 20 46  a, b, c,.      F
59e0: 4f 52 45 49 47 4e 20 4b 45 59 28 61 2c 20 62 2c  OREIGN KEY(a, b,
59f0: 20 63 29 20 52 45 46 45 52 45 4e 43 45 53 20 70   c) REFERENCES p
5a00: 61 72 65 6e 74 20 44 45 46 45 52 52 41 42 4c 45  arent DEFERRABLE
5a10: 20 49 4e 49 54 49 41 4c 4c 59 20 49 4d 4d 45 44   INITIALLY IMMED
5a20: 49 41 54 45 0a 20 20 20 20 29 3b 0a 20 20 20 20  IATE.    );.    
5a30: 43 52 45 41 54 45 20 54 41 42 4c 45 20 63 35 28  CREATE TABLE c5(
5a40: 61 2c 20 62 2c 20 63 2c 0a 20 20 20 20 20 20 46  a, b, c,.      F
5a50: 4f 52 45 49 47 4e 20 4b 45 59 28 61 2c 20 62 2c  OREIGN KEY(a, b,
5a60: 20 63 29 20 52 45 46 45 52 45 4e 43 45 53 20 70   c) REFERENCES p
5a70: 61 72 65 6e 74 20 44 45 46 45 52 52 41 42 4c 45  arent DEFERRABLE
5a80: 0a 20 20 20 20 29 3b 0a 20 20 20 20 43 52 45 41  .    );.    CREA
5a90: 54 45 20 54 41 42 4c 45 20 63 36 28 61 2c 20 62  TE TABLE c6(a, b
5aa0: 2c 20 63 2c 20 46 4f 52 45 49 47 4e 20 4b 45 59  , c, FOREIGN KEY
5ab0: 28 61 2c 20 62 2c 20 63 29 20 52 45 46 45 52 45  (a, b, c) REFERE
5ac0: 4e 43 45 53 20 70 61 72 65 6e 74 29 3b 0a 0a 20  NCES parent);.. 
5ad0: 20 20 20 2d 2d 20 54 68 69 73 20 46 4b 20 63 6f     -- This FK co
5ae0: 6e 73 74 72 61 69 6e 74 20 69 73 20 74 68 65 20  nstraint is the 
5af0: 6f 6e 6c 79 20 64 65 66 65 72 72 61 62 6c 65 20  only deferrable 
5b00: 6f 6e 65 2e 0a 20 20 20 20 43 52 45 41 54 45 20  one..    CREATE 
5b10: 54 41 42 4c 45 20 63 37 28 61 2c 20 62 2c 20 63  TABLE c7(a, b, c
5b20: 2c 0a 20 20 20 20 20 20 46 4f 52 45 49 47 4e 20  ,.      FOREIGN 
5b30: 4b 45 59 28 61 2c 20 62 2c 20 63 29 20 52 45 46  KEY(a, b, c) REF
5b40: 45 52 45 4e 43 45 53 20 70 61 72 65 6e 74 20 44  ERENCES parent D
5b50: 45 46 45 52 52 41 42 4c 45 20 49 4e 49 54 49 41  EFERRABLE INITIA
5b60: 4c 4c 59 20 44 45 46 45 52 52 45 44 0a 20 20 20  LLY DEFERRED.   
5b70: 20 29 3b 0a 0a 20 20 20 20 49 4e 53 45 52 54 20   );..    INSERT 
5b80: 49 4e 54 4f 20 70 61 72 65 6e 74 20 56 41 4c 55  INTO parent VALU
5b90: 45 53 28 27 61 27 2c 20 27 62 27 2c 20 27 63 27  ES('a', 'b', 'c'
5ba0: 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e  );.    INSERT IN
5bb0: 54 4f 20 70 61 72 65 6e 74 20 56 41 4c 55 45 53  TO parent VALUES
5bc0: 28 27 64 27 2c 20 27 65 27 2c 20 27 66 27 29 3b  ('d', 'e', 'f');
5bd0: 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f  .    INSERT INTO
5be0: 20 70 61 72 65 6e 74 20 56 41 4c 55 45 53 28 27   parent VALUES('
5bf0: 67 27 2c 20 27 68 27 2c 20 27 69 27 29 3b 0a 20  g', 'h', 'i');. 
5c00: 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 70     INSERT INTO p
5c10: 61 72 65 6e 74 20 56 41 4c 55 45 53 28 27 6a 27  arent VALUES('j'
5c20: 2c 20 27 6b 27 2c 20 27 6c 27 29 3b 0a 20 20 20  , 'k', 'l');.   
5c30: 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 70 61 72   INSERT INTO par
5c40: 65 6e 74 20 56 41 4c 55 45 53 28 27 6d 27 2c 20  ent VALUES('m', 
5c50: 27 6e 27 2c 20 27 6f 27 29 3b 0a 20 20 20 20 49  'n', 'o');.    I
5c60: 4e 53 45 52 54 20 49 4e 54 4f 20 70 61 72 65 6e  NSERT INTO paren
5c70: 74 20 56 41 4c 55 45 53 28 27 70 27 2c 20 27 71  t VALUES('p', 'q
5c80: 27 2c 20 27 72 27 29 3b 0a 20 20 20 20 49 4e 53  ', 'r');.    INS
5c90: 45 52 54 20 49 4e 54 4f 20 70 61 72 65 6e 74 20  ERT INTO parent 
5ca0: 56 41 4c 55 45 53 28 27 73 27 2c 20 27 74 27 2c  VALUES('s', 't',
5cb0: 20 27 75 27 29 3b 0a 0a 20 20 20 20 49 4e 53 45   'u');..    INSE
5cc0: 52 54 20 49 4e 54 4f 20 63 31 20 56 41 4c 55 45  RT INTO c1 VALUE
5cd0: 53 28 27 61 27 2c 20 27 62 27 2c 20 27 63 27 29  S('a', 'b', 'c')
5ce0: 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54  ;.    INSERT INT
5cf0: 4f 20 63 32 20 56 41 4c 55 45 53 28 27 64 27 2c  O c2 VALUES('d',
5d00: 20 27 65 27 2c 20 27 66 27 29 3b 0a 20 20 20 20   'e', 'f');.    
5d10: 49 4e 53 45 52 54 20 49 4e 54 4f 20 63 33 20 56  INSERT INTO c3 V
5d20: 41 4c 55 45 53 28 27 67 27 2c 20 27 68 27 2c 20  ALUES('g', 'h', 
5d30: 27 69 27 29 3b 0a 20 20 20 20 49 4e 53 45 52 54  'i');.    INSERT
5d40: 20 49 4e 54 4f 20 63 34 20 56 41 4c 55 45 53 28   INTO c4 VALUES(
5d50: 27 6a 27 2c 20 27 6b 27 2c 20 27 6c 27 29 3b 0a  'j', 'k', 'l');.
5d60: 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20      INSERT INTO 
5d70: 63 35 20 56 41 4c 55 45 53 28 27 6d 27 2c 20 27  c5 VALUES('m', '
5d80: 6e 27 2c 20 27 6f 27 29 3b 0a 20 20 20 20 49 4e  n', 'o');.    IN
5d90: 53 45 52 54 20 49 4e 54 4f 20 63 36 20 56 41 4c  SERT INTO c6 VAL
5da0: 55 45 53 28 27 70 27 2c 20 27 71 27 2c 20 27 72  UES('p', 'q', 'r
5db0: 27 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49  ');.    INSERT I
5dc0: 4e 54 4f 20 63 37 20 56 41 4c 55 45 53 28 27 73  NTO c7 VALUES('s
5dd0: 27 2c 20 27 74 27 2c 20 27 75 27 29 3b 0a 20 20  ', 't', 'u');.  
5de0: 7d 0a 7d 20 7b 7d 0a 0a 70 72 6f 63 20 74 65 73  }.} {}..proc tes
5df0: 74 5f 65 66 6b 65 79 5f 32 39 20 7b 74 6e 20 73  t_efkey_29 {tn s
5e00: 71 6c 20 69 73 45 72 72 6f 72 7d 20 7b 0a 20 20  ql isError} {.  
5e10: 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 32  do_test e_fkey-2
5e20: 39 2e 24 74 6e 20 22 63 61 74 63 68 73 71 6c 20  9.$tn "catchsql 
5e30: 7b 24 73 71 6c 7d 22 20 5b 0a 20 20 20 20 6c 69  {$sql}" [.    li
5e40: 6e 64 65 78 20 7b 7b 30 20 7b 7d 7d 20 7b 31 20  ndex {{0 {}} {1 
5e50: 7b 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e  {foreign key con
5e60: 73 74 72 61 69 6e 74 20 66 61 69 6c 65 64 7d 7d  straint failed}}
5e70: 7d 20 24 69 73 45 72 72 6f 72 0a 20 20 5d 0a 7d  } $isError.  ].}
5e80: 0a 74 65 73 74 5f 65 66 6b 65 79 5f 32 39 20 20  .test_efkey_29  
5e90: 32 20 22 42 45 47 49 4e 22 20 20 20 20 20 20 20  2 "BEGIN"       
5ea0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5eb0: 20 20 20 20 20 20 20 20 20 20 20 20 30 0a 74 65              0.te
5ec0: 73 74 5f 65 66 6b 65 79 5f 32 39 20 20 33 20 22  st_efkey_29  3 "
5ed0: 44 45 4c 45 54 45 20 46 52 4f 4d 20 70 61 72 65  DELETE FROM pare
5ee0: 6e 74 20 57 48 45 52 45 20 78 20 3d 20 27 61 27  nt WHERE x = 'a'
5ef0: 22 20 20 20 20 20 20 20 20 31 0a 74 65 73 74 5f  "        1.test_
5f00: 65 66 6b 65 79 5f 32 39 20 20 34 20 22 44 45 4c  efkey_29  4 "DEL
5f10: 45 54 45 20 46 52 4f 4d 20 70 61 72 65 6e 74 20  ETE FROM parent 
5f20: 57 48 45 52 45 20 78 20 3d 20 27 64 27 22 20 20  WHERE x = 'd'"  
5f30: 20 20 20 20 20 20 31 0a 74 65 73 74 5f 65 66 6b        1.test_efk
5f40: 65 79 5f 32 39 20 20 35 20 22 44 45 4c 45 54 45  ey_29  5 "DELETE
5f50: 20 46 52 4f 4d 20 70 61 72 65 6e 74 20 57 48 45   FROM parent WHE
5f60: 52 45 20 78 20 3d 20 27 67 27 22 20 20 20 20 20  RE x = 'g'"     
5f70: 20 20 20 31 0a 74 65 73 74 5f 65 66 6b 65 79 5f     1.test_efkey_
5f80: 32 39 20 20 36 20 22 44 45 4c 45 54 45 20 46 52  29  6 "DELETE FR
5f90: 4f 4d 20 70 61 72 65 6e 74 20 57 48 45 52 45 20  OM parent WHERE 
5fa0: 78 20 3d 20 27 6a 27 22 20 20 20 20 20 20 20 20  x = 'j'"        
5fb0: 31 0a 74 65 73 74 5f 65 66 6b 65 79 5f 32 39 20  1.test_efkey_29 
5fc0: 20 37 20 22 44 45 4c 45 54 45 20 46 52 4f 4d 20   7 "DELETE FROM 
5fd0: 70 61 72 65 6e 74 20 57 48 45 52 45 20 78 20 3d  parent WHERE x =
5fe0: 20 27 6d 27 22 20 20 20 20 20 20 20 20 31 0a 74   'm'"        1.t
5ff0: 65 73 74 5f 65 66 6b 65 79 5f 32 39 20 20 38 20  est_efkey_29  8 
6000: 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 70 61 72  "DELETE FROM par
6010: 65 6e 74 20 57 48 45 52 45 20 78 20 3d 20 27 70  ent WHERE x = 'p
6020: 27 22 20 20 20 20 20 20 20 20 31 0a 74 65 73 74  '"        1.test
6030: 5f 65 66 6b 65 79 5f 32 39 20 20 39 20 22 44 45  _efkey_29  9 "DE
6040: 4c 45 54 45 20 46 52 4f 4d 20 70 61 72 65 6e 74  LETE FROM parent
6050: 20 57 48 45 52 45 20 78 20 3d 20 27 73 27 22 20   WHERE x = 's'" 
6060: 20 20 20 20 20 20 20 30 0a 74 65 73 74 5f 65 66         0.test_ef
6070: 6b 65 79 5f 32 39 20 31 30 20 22 43 4f 4d 4d 49  key_29 10 "COMMI
6080: 54 22 20 20 20 20 20 20 20 20 20 20 20 20 20 20  T"              
6090: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
60a0: 20 20 20 20 31 0a 74 65 73 74 5f 65 66 6b 65 79      1.test_efkey
60b0: 5f 32 39 20 31 31 20 22 52 4f 4c 4c 42 41 43 4b  _29 11 "ROLLBACK
60c0: 22 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  "               
60d0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
60e0: 20 30 0a 0a 74 65 73 74 5f 65 66 6b 65 79 5f 32   0..test_efkey_2
60f0: 39 20 20 39 20 22 42 45 47 49 4e 22 20 20 20 20  9  9 "BEGIN"    
6100: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6110: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30                 0
6120: 0a 74 65 73 74 5f 65 66 6b 65 79 5f 32 39 20 31  .test_efkey_29 1
6130: 30 20 22 55 50 44 41 54 45 20 70 61 72 65 6e 74  0 "UPDATE parent
6140: 20 53 45 54 20 7a 20 3d 20 27 7a 27 20 57 48 45   SET z = 'z' WHE
6150: 52 45 20 7a 20 3d 20 27 63 27 22 20 31 0a 74 65  RE z = 'c'" 1.te
6160: 73 74 5f 65 66 6b 65 79 5f 32 39 20 31 31 20 22  st_efkey_29 11 "
6170: 55 50 44 41 54 45 20 70 61 72 65 6e 74 20 53 45  UPDATE parent SE
6180: 54 20 7a 20 3d 20 27 7a 27 20 57 48 45 52 45 20  T z = 'z' WHERE 
6190: 7a 20 3d 20 27 66 27 22 20 31 0a 74 65 73 74 5f  z = 'f'" 1.test_
61a0: 65 66 6b 65 79 5f 32 39 20 31 32 20 22 55 50 44  efkey_29 12 "UPD
61b0: 41 54 45 20 70 61 72 65 6e 74 20 53 45 54 20 7a  ATE parent SET z
61c0: 20 3d 20 27 7a 27 20 57 48 45 52 45 20 7a 20 3d   = 'z' WHERE z =
61d0: 20 27 69 27 22 20 31 0a 74 65 73 74 5f 65 66 6b   'i'" 1.test_efk
61e0: 65 79 5f 32 39 20 31 33 20 22 55 50 44 41 54 45  ey_29 13 "UPDATE
61f0: 20 70 61 72 65 6e 74 20 53 45 54 20 7a 20 3d 20   parent SET z = 
6200: 27 7a 27 20 57 48 45 52 45 20 7a 20 3d 20 27 6c  'z' WHERE z = 'l
6210: 27 22 20 31 0a 74 65 73 74 5f 65 66 6b 65 79 5f  '" 1.test_efkey_
6220: 32 39 20 31 34 20 22 55 50 44 41 54 45 20 70 61  29 14 "UPDATE pa
6230: 72 65 6e 74 20 53 45 54 20 7a 20 3d 20 27 7a 27  rent SET z = 'z'
6240: 20 57 48 45 52 45 20 7a 20 3d 20 27 6f 27 22 20   WHERE z = 'o'" 
6250: 31 0a 74 65 73 74 5f 65 66 6b 65 79 5f 32 39 20  1.test_efkey_29 
6260: 31 35 20 22 55 50 44 41 54 45 20 70 61 72 65 6e  15 "UPDATE paren
6270: 74 20 53 45 54 20 7a 20 3d 20 27 7a 27 20 57 48  t SET z = 'z' WH
6280: 45 52 45 20 7a 20 3d 20 27 72 27 22 20 31 0a 74  ERE z = 'r'" 1.t
6290: 65 73 74 5f 65 66 6b 65 79 5f 32 39 20 31 36 20  est_efkey_29 16 
62a0: 22 55 50 44 41 54 45 20 70 61 72 65 6e 74 20 53  "UPDATE parent S
62b0: 45 54 20 7a 20 3d 20 27 7a 27 20 57 48 45 52 45  ET z = 'z' WHERE
62c0: 20 7a 20 3d 20 27 75 27 22 20 30 0a 74 65 73 74   z = 'u'" 0.test
62d0: 5f 65 66 6b 65 79 5f 32 39 20 31 37 20 22 43 4f  _efkey_29 17 "CO
62e0: 4d 4d 49 54 22 20 20 20 20 20 20 20 20 20 20 20  MMIT"           
62f0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6300: 20 20 20 20 20 20 20 31 0a 74 65 73 74 5f 65 66         1.test_ef
6310: 6b 65 79 5f 32 39 20 31 38 20 22 52 4f 4c 4c 42  key_29 18 "ROLLB
6320: 41 43 4b 22 20 20 20 20 20 20 20 20 20 20 20 20  ACK"            
6330: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6340: 20 20 20 20 30 0a 0a 74 65 73 74 5f 65 66 6b 65      0..test_efke
6350: 79 5f 32 39 20 31 37 20 22 42 45 47 49 4e 22 20  y_29 17 "BEGIN" 
6360: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6370: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6380: 20 20 30 0a 74 65 73 74 5f 65 66 6b 65 79 5f 32    0.test_efkey_2
6390: 39 20 31 38 20 22 49 4e 53 45 52 54 20 49 4e 54  9 18 "INSERT INT
63a0: 4f 20 63 31 20 56 41 4c 55 45 53 28 31 2c 20 32  O c1 VALUES(1, 2
63b0: 2c 20 33 29 22 20 20 20 20 20 20 20 20 20 20 31  , 3)"          1
63c0: 0a 74 65 73 74 5f 65 66 6b 65 79 5f 32 39 20 31  .test_efkey_29 1
63d0: 39 20 22 49 4e 53 45 52 54 20 49 4e 54 4f 20 63  9 "INSERT INTO c
63e0: 32 20 56 41 4c 55 45 53 28 31 2c 20 32 2c 20 33  2 VALUES(1, 2, 3
63f0: 29 22 20 20 20 20 20 20 20 20 20 20 31 0a 74 65  )"          1.te
6400: 73 74 5f 65 66 6b 65 79 5f 32 39 20 32 30 20 22  st_efkey_29 20 "
6410: 49 4e 53 45 52 54 20 49 4e 54 4f 20 63 33 20 56  INSERT INTO c3 V
6420: 41 4c 55 45 53 28 31 2c 20 32 2c 20 33 29 22 20  ALUES(1, 2, 3)" 
6430: 20 20 20 20 20 20 20 20 20 31 0a 74 65 73 74 5f           1.test_
6440: 65 66 6b 65 79 5f 32 39 20 32 31 20 22 49 4e 53  efkey_29 21 "INS
6450: 45 52 54 20 49 4e 54 4f 20 63 34 20 56 41 4c 55  ERT INTO c4 VALU
6460: 45 53 28 31 2c 20 32 2c 20 33 29 22 20 20 20 20  ES(1, 2, 3)"    
6470: 20 20 20 20 20 20 31 0a 74 65 73 74 5f 65 66 6b        1.test_efk
6480: 65 79 5f 32 39 20 32 32 20 22 49 4e 53 45 52 54  ey_29 22 "INSERT
6490: 20 49 4e 54 4f 20 63 35 20 56 41 4c 55 45 53 28   INTO c5 VALUES(
64a0: 31 2c 20 32 2c 20 33 29 22 20 20 20 20 20 20 20  1, 2, 3)"       
64b0: 20 20 20 31 0a 74 65 73 74 5f 65 66 6b 65 79 5f     1.test_efkey_
64c0: 32 39 20 32 32 20 22 49 4e 53 45 52 54 20 49 4e  29 22 "INSERT IN
64d0: 54 4f 20 63 36 20 56 41 4c 55 45 53 28 31 2c 20  TO c6 VALUES(1, 
64e0: 32 2c 20 33 29 22 20 20 20 20 20 20 20 20 20 20  2, 3)"          
64f0: 31 0a 74 65 73 74 5f 65 66 6b 65 79 5f 32 39 20  1.test_efkey_29 
6500: 32 32 20 22 49 4e 53 45 52 54 20 49 4e 54 4f 20  22 "INSERT INTO 
6510: 63 37 20 56 41 4c 55 45 53 28 31 2c 20 32 2c 20  c7 VALUES(1, 2, 
6520: 33 29 22 20 20 20 20 20 20 20 20 20 20 30 0a 74  3)"          0.t
6530: 65 73 74 5f 65 66 6b 65 79 5f 32 39 20 32 33 20  est_efkey_29 23 
6540: 22 43 4f 4d 4d 49 54 22 20 20 20 20 20 20 20 20  "COMMIT"        
6550: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6560: 20 20 20 20 20 20 20 20 20 20 31 0a 74 65 73 74            1.test
6570: 5f 65 66 6b 65 79 5f 32 39 20 32 34 20 22 49 4e  _efkey_29 24 "IN
6580: 53 45 52 54 20 49 4e 54 4f 20 70 61 72 65 6e 74  SERT INTO parent
6590: 20 56 41 4c 55 45 53 28 31 2c 20 32 2c 20 33 29   VALUES(1, 2, 3)
65a0: 22 20 20 20 20 20 20 30 0a 74 65 73 74 5f 65 66  "      0.test_ef
65b0: 6b 65 79 5f 32 39 20 32 35 20 22 43 4f 4d 4d 49  key_29 25 "COMMI
65c0: 54 22 20 20 20 20 20 20 20 20 20 20 20 20 20 20  T"              
65d0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
65e0: 20 20 20 20 30 0a 0a 74 65 73 74 5f 65 66 6b 65      0..test_efke
65f0: 79 5f 32 39 20 32 36 20 22 42 45 47 49 4e 22 20  y_29 26 "BEGIN" 
6600: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6610: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6620: 20 20 30 0a 74 65 73 74 5f 65 66 6b 65 79 5f 32    0.test_efkey_2
6630: 39 20 32 37 20 22 55 50 44 41 54 45 20 63 31 20  9 27 "UPDATE c1 
6640: 53 45 54 20 61 20 3d 20 31 30 22 20 20 20 20 20  SET a = 10"     
6650: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
6660: 0a 74 65 73 74 5f 65 66 6b 65 79 5f 32 39 20 32  .test_efkey_29 2
6670: 38 20 22 55 50 44 41 54 45 20 63 32 20 53 45 54  8 "UPDATE c2 SET
6680: 20 61 20 3d 20 31 30 22 20 20 20 20 20 20 20 20   a = 10"        
6690: 20 20 20 20 20 20 20 20 20 20 20 20 31 0a 74 65              1.te
66a0: 73 74 5f 65 66 6b 65 79 5f 32 39 20 32 39 20 22  st_efkey_29 29 "
66b0: 55 50 44 41 54 45 20 63 33 20 53 45 54 20 61 20  UPDATE c3 SET a 
66c0: 3d 20 31 30 22 20 20 20 20 20 20 20 20 20 20 20  = 10"           
66d0: 20 20 20 20 20 20 20 20 20 31 0a 74 65 73 74 5f           1.test_
66e0: 65 66 6b 65 79 5f 32 39 20 33 30 20 22 55 50 44  efkey_29 30 "UPD
66f0: 41 54 45 20 63 34 20 53 45 54 20 61 20 3d 20 31  ATE c4 SET a = 1
6700: 30 22 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0"              
6710: 20 20 20 20 20 20 31 0a 74 65 73 74 5f 65 66 6b        1.test_efk
6720: 65 79 5f 32 39 20 33 31 20 22 55 50 44 41 54 45  ey_29 31 "UPDATE
6730: 20 63 35 20 53 45 54 20 61 20 3d 20 31 30 22 20   c5 SET a = 10" 
6740: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6750: 20 20 20 31 0a 74 65 73 74 5f 65 66 6b 65 79 5f     1.test_efkey_
6760: 32 39 20 33 31 20 22 55 50 44 41 54 45 20 63 36  29 31 "UPDATE c6
6770: 20 53 45 54 20 61 20 3d 20 31 30 22 20 20 20 20   SET a = 10"    
6780: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6790: 31 0a 74 65 73 74 5f 65 66 6b 65 79 5f 32 39 20  1.test_efkey_29 
67a0: 33 31 20 22 55 50 44 41 54 45 20 63 37 20 53 45  31 "UPDATE c7 SE
67b0: 54 20 61 20 3d 20 31 30 22 20 20 20 20 20 20 20  T a = 10"       
67c0: 20 20 20 20 20 20 20 20 20 20 20 20 20 30 0a 74               0.t
67d0: 65 73 74 5f 65 66 6b 65 79 5f 32 39 20 33 32 20  est_efkey_29 32 
67e0: 22 43 4f 4d 4d 49 54 22 20 20 20 20 20 20 20 20  "COMMIT"        
67f0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6800: 20 20 20 20 20 20 20 20 20 20 31 0a 74 65 73 74            1.test
6810: 5f 65 66 6b 65 79 5f 32 39 20 33 33 20 22 52 4f  _efkey_29 33 "RO
6820: 4c 4c 42 41 43 4b 22 20 20 20 20 20 20 20 20 20  LLBACK"         
6830: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6840: 20 20 20 20 20 20 20 30 0a 0a 23 2d 2d 2d 2d 2d         0..#-----
6850: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
6860: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
6870: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
6880: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
6890: 2d 2d 2d 2d 0a 23 20 2f 2a 20 45 56 3a 20 52 2d  ----.# /* EV: R-
68a0: 33 35 30 34 33 2d 30 31 35 34 36 20 2a 2f 0a 23  35043-01546 */.#
68b0: 0a 23 20 54 65 73 74 20 61 6e 20 65 78 61 6d 70  .# Test an examp
68c0: 6c 65 20 66 72 6f 6d 20 66 6f 72 65 69 67 6e 6b  le from foreignk
68d0: 65 79 73 2e 68 74 6d 6c 20 64 65 61 6c 69 6e 67  eys.html dealing
68e0: 20 77 69 74 68 20 61 20 64 65 66 65 72 72 65 64   with a deferred
68f0: 20 66 6f 72 65 69 67 6e 20 0a 23 20 6b 65 79 20   foreign .# key 
6900: 63 6f 6e 73 74 72 61 69 6e 74 2e 0a 23 0a 64 6f  constraint..#.do
6910: 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 32 38 2e  _test e_fkey-28.
6920: 31 20 7b 0a 20 20 64 72 6f 70 5f 61 6c 6c 5f 74  1 {.  drop_all_t
6930: 61 62 6c 65 73 0a 20 20 65 78 65 63 73 71 6c 20  ables.  execsql 
6940: 7b 0a 20 20 20 20 43 52 45 41 54 45 20 54 41 42  {.    CREATE TAB
6950: 4c 45 20 61 72 74 69 73 74 28 0a 20 20 20 20 20  LE artist(.     
6960: 20 61 72 74 69 73 74 69 64 20 20 20 20 49 4e 54   artistid    INT
6970: 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59  EGER PRIMARY KEY
6980: 2c 20 0a 20 20 20 20 20 20 61 72 74 69 73 74 6e  , .      artistn
6990: 61 6d 65 20 20 54 45 58 54 0a 20 20 20 20 29 3b  ame  TEXT.    );
69a0: 0a 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c  .    CREATE TABL
69b0: 45 20 74 72 61 63 6b 28 0a 20 20 20 20 20 20 74  E track(.      t
69c0: 72 61 63 6b 69 64 20 20 20 20 20 49 4e 54 45 47  rackid     INTEG
69d0: 45 52 2c 0a 20 20 20 20 20 20 74 72 61 63 6b 6e  ER,.      trackn
69e0: 61 6d 65 20 20 20 54 45 58 54 2c 20 0a 20 20 20  ame   TEXT, .   
69f0: 20 20 20 74 72 61 63 6b 61 72 74 69 73 74 20 49     trackartist I
6a00: 4e 54 45 47 45 52 20 52 45 46 45 52 45 4e 43 45  NTEGER REFERENCE
6a10: 53 20 61 72 74 69 73 74 28 61 72 74 69 73 74 69  S artist(artisti
6a20: 64 29 20 44 45 46 45 52 52 41 42 4c 45 20 49 4e  d) DEFERRABLE IN
6a30: 49 54 49 41 4c 4c 59 20 44 45 46 45 52 52 45 44  ITIALLY DEFERRED
6a40: 0a 20 20 20 20 29 3b 0a 20 20 7d 0a 7d 20 7b 7d  .    );.  }.} {}
6a50: 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d  .do_test e_fkey-
6a60: 32 38 2e 32 20 7b 0a 20 20 65 78 65 63 73 71 6c  28.2 {.  execsql
6a70: 20 7b 0a 20 20 20 20 42 45 47 49 4e 3b 0a 20 20   {.    BEGIN;.  
6a80: 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20      INSERT INTO 
6a90: 74 72 61 63 6b 20 56 41 4c 55 45 53 28 31 2c 20  track VALUES(1, 
6aa0: 27 57 68 69 74 65 20 43 68 72 69 73 74 6d 61 73  'White Christmas
6ab0: 27 2c 20 35 29 3b 0a 20 20 7d 0a 20 20 63 61 74  ', 5);.  }.  cat
6ac0: 63 68 73 71 6c 20 43 4f 4d 4d 49 54 0a 7d 20 7b  chsql COMMIT.} {
6ad0: 31 20 7b 66 6f 72 65 69 67 6e 20 6b 65 79 20 63  1 {foreign key c
6ae0: 6f 6e 73 74 72 61 69 6e 74 20 66 61 69 6c 65 64  onstraint failed
6af0: 7d 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65  }}.do_test e_fke
6b00: 79 2d 32 38 2e 33 20 7b 0a 20 20 65 78 65 63 73  y-28.3 {.  execs
6b10: 71 6c 20 7b 0a 20 20 20 20 49 4e 53 45 52 54 20  ql {.    INSERT 
6b20: 49 4e 54 4f 20 61 72 74 69 73 74 20 56 41 4c 55  INTO artist VALU
6b30: 45 53 28 35 2c 20 27 42 69 6e 67 20 43 72 6f 73  ES(5, 'Bing Cros
6b40: 62 79 27 29 3b 0a 20 20 20 20 43 4f 4d 4d 49 54  by');.    COMMIT
6b50: 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a 0a 23 2d 2d 2d  ;.  }.} {}..#---
6b60: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
6b70: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
6b80: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
6b90: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
6ba0: 2d 2d 2d 2d 2d 2d 0a 23 20 2f 2a 20 45 56 3a 20  ------.# /* EV: 
6bb0: 52 2d 30 37 32 32 33 2d 34 38 33 32 33 20 2a 2f  R-07223-48323 */
6bc0: 0a 23 0a 23 20 56 65 72 69 66 79 20 74 68 61 74  .#.# Verify that
6bd0: 20 61 20 6e 65 73 74 65 64 20 73 61 76 65 70 6f   a nested savepo
6be0: 69 6e 74 20 6d 61 79 20 62 65 20 72 65 6c 65 61  int may be relea
6bf0: 73 65 64 20 77 69 74 68 6f 75 74 20 73 61 74 69  sed without sati
6c00: 73 66 79 69 6e 67 20 0a 23 20 64 65 66 65 72 72  sfying .# deferr
6c10: 65 64 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 63  ed foreign key c
6c20: 6f 6e 73 74 72 61 69 6e 74 73 2e 0a 23 0a 64 72  onstraints..#.dr
6c30: 6f 70 5f 61 6c 6c 5f 74 61 62 6c 65 73 0a 64 6f  op_all_tables.do
6c40: 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 33 30 2e  _test e_fkey-30.
6c50: 31 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a  1 {.  execsql {.
6c60: 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45      CREATE TABLE
6c70: 20 74 31 28 61 20 50 52 49 4d 41 52 59 20 4b 45   t1(a PRIMARY KE
6c80: 59 2c 0a 20 20 20 20 20 20 62 20 52 45 46 45 52  Y,.      b REFER
6c90: 45 4e 43 45 53 20 74 31 20 44 45 46 45 52 52 41  ENCES t1 DEFERRA
6ca0: 42 4c 45 20 49 4e 49 54 49 41 4c 4c 59 20 44 45  BLE INITIALLY DE
6cb0: 46 45 52 52 45 44 0a 20 20 20 20 29 3b 0a 20 20  FERRED.    );.  
6cc0: 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 31    INSERT INTO t1
6cd0: 20 56 41 4c 55 45 53 28 31 2c 20 31 29 3b 0a 20   VALUES(1, 1);. 
6ce0: 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74     INSERT INTO t
6cf0: 31 20 56 41 4c 55 45 53 28 32 2c 20 32 29 3b 0a  1 VALUES(2, 2);.
6d00: 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20      INSERT INTO 
6d10: 74 31 20 56 41 4c 55 45 53 28 33 2c 20 33 29 3b  t1 VALUES(3, 3);
6d20: 0a 20 20 7d 0a 7d 20 7b 7d 0a 64 6f 5f 74 65 73  .  }.} {}.do_tes
6d30: 74 20 65 5f 66 6b 65 79 2d 33 30 2e 32 20 7b 0a  t e_fkey-30.2 {.
6d40: 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20    execsql {.    
6d50: 42 45 47 49 4e 3b 0a 20 20 20 20 20 20 53 41 56  BEGIN;.      SAV
6d60: 45 50 4f 49 4e 54 20 6f 6e 65 3b 0a 20 20 20 20  EPOINT one;.    
6d70: 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20      INSERT INTO 
6d80: 74 31 20 56 41 4c 55 45 53 28 34 2c 20 35 29 3b  t1 VALUES(4, 5);
6d90: 0a 20 20 20 20 20 20 52 45 4c 45 41 53 45 20 6f  .      RELEASE o
6da0: 6e 65 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a 64 6f 5f  ne;.  }.} {}.do_
6db0: 74 65 73 74 20 65 5f 66 6b 65 79 2d 33 30 2e 33  test e_fkey-30.3
6dc0: 20 7b 0a 20 20 63 61 74 63 68 73 71 6c 20 43 4f   {.  catchsql CO
6dd0: 4d 4d 49 54 0a 7d 20 7b 31 20 7b 66 6f 72 65 69  MMIT.} {1 {forei
6de0: 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e  gn key constrain
6df0: 74 20 66 61 69 6c 65 64 7d 7d 0a 64 6f 5f 74 65  t failed}}.do_te
6e00: 73 74 20 65 5f 66 6b 65 79 2d 33 30 2e 34 20 7b  st e_fkey-30.4 {
6e10: 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20  .  execsql {.   
6e20: 20 55 50 44 41 54 45 20 74 31 20 53 45 54 20 61   UPDATE t1 SET a
6e30: 20 3d 20 35 20 57 48 45 52 45 20 61 20 3d 20 34   = 5 WHERE a = 4
6e40: 3b 0a 20 20 20 20 43 4f 4d 4d 49 54 3b 0a 20 20  ;.    COMMIT;.  
6e50: 7d 0a 7d 20 7b 7d 0a 0a 0a 23 2d 2d 2d 2d 2d 2d  }.} {}...#------
6e60: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
6e70: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
6e80: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
6e90: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
6ea0: 2d 2d 2d 0a 23 20 2f 2a 20 45 56 3a 20 52 2d 34  ---.# /* EV: R-4
6eb0: 34 32 39 35 2d 31 33 38 32 33 20 2a 2f 0a 23 0a  4295-13823 */.#.
6ec0: 23 20 43 68 65 63 6b 20 74 68 61 74 20 61 20 74  # Check that a t
6ed0: 72 61 6e 73 61 63 74 69 6f 6e 20 73 61 76 65 70  ransaction savep
6ee0: 6f 69 6e 74 20 28 61 6e 20 6f 75 74 65 72 6d 6f  oint (an outermo
6ef0: 73 74 20 73 61 76 65 70 6f 69 6e 74 20 6f 70 65  st savepoint ope
6f00: 6e 65 64 20 77 68 65 6e 0a 23 20 74 68 65 20 64  ned when.# the d
6f10: 61 74 61 62 61 73 65 20 77 61 73 20 69 6e 20 61  atabase was in a
6f20: 75 74 6f 2d 63 6f 6d 6d 69 74 20 6d 6f 64 65 29  uto-commit mode)
6f30: 20 63 61 6e 6e 6f 74 20 62 65 20 72 65 6c 65 61   cannot be relea
6f40: 73 65 64 20 77 69 74 68 6f 75 74 0a 23 20 73 61  sed without.# sa
6f50: 74 69 73 66 79 69 6e 67 20 64 65 66 65 72 72 65  tisfying deferre
6f60: 64 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f  d foreign key co
6f70: 6e 73 74 72 61 69 6e 74 73 2e 20 49 74 20 6d 61  nstraints. It ma
6f80: 79 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b  y be rolled back
6f90: 2e 0a 23 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b  ..#.do_test e_fk
6fa0: 65 79 2d 33 31 2e 31 20 7b 0a 20 20 65 78 65 63  ey-31.1 {.  exec
6fb0: 73 71 6c 20 7b 0a 20 20 20 20 53 41 56 45 50 4f  sql {.    SAVEPO
6fc0: 49 4e 54 20 6f 6e 65 3b 0a 20 20 20 20 20 20 53  INT one;.      S
6fd0: 41 56 45 50 4f 49 4e 54 20 74 77 6f 3b 0a 20 20  AVEPOINT two;.  
6fe0: 20 20 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54        INSERT INT
6ff0: 4f 20 74 31 20 56 41 4c 55 45 53 28 36 2c 20 37  O t1 VALUES(6, 7
7000: 29 3b 0a 20 20 20 20 20 20 52 45 4c 45 41 53 45  );.      RELEASE
7010: 20 74 77 6f 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a 64   two;.  }.} {}.d
7020: 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 33 31  o_test e_fkey-31
7030: 2e 32 20 7b 0a 20 20 63 61 74 63 68 73 71 6c 20  .2 {.  catchsql 
7040: 7b 52 45 4c 45 41 53 45 20 6f 6e 65 7d 0a 7d 20  {RELEASE one}.} 
7050: 7b 31 20 7b 66 6f 72 65 69 67 6e 20 6b 65 79 20  {1 {foreign key 
7060: 63 6f 6e 73 74 72 61 69 6e 74 20 66 61 69 6c 65  constraint faile
7070: 64 7d 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b  d}}.do_test e_fk
7080: 65 79 2d 33 31 2e 33 20 7b 0a 20 20 65 78 65 63  ey-31.3 {.  exec
7090: 73 71 6c 20 7b 0a 20 20 20 20 20 20 55 50 44 41  sql {.      UPDA
70a0: 54 45 20 74 31 20 53 45 54 20 61 20 3d 20 37 20  TE t1 SET a = 7 
70b0: 57 48 45 52 45 20 61 20 3d 20 36 3b 0a 20 20 20  WHERE a = 6;.   
70c0: 20 52 45 4c 45 41 53 45 20 6f 6e 65 3b 0a 20 20   RELEASE one;.  
70d0: 7d 0a 7d 20 7b 7d 0a 64 6f 5f 74 65 73 74 20 65  }.} {}.do_test e
70e0: 5f 66 6b 65 79 2d 33 31 2e 34 20 7b 0a 20 20 65  _fkey-31.4 {.  e
70f0: 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 53 41 56  xecsql {.    SAV
7100: 45 50 4f 49 4e 54 20 6f 6e 65 3b 0a 20 20 20 20  EPOINT one;.    
7110: 20 20 53 41 56 45 50 4f 49 4e 54 20 74 77 6f 3b    SAVEPOINT two;
7120: 0a 20 20 20 20 20 20 20 20 49 4e 53 45 52 54 20  .        INSERT 
7130: 49 4e 54 4f 20 74 31 20 56 41 4c 55 45 53 28 39  INTO t1 VALUES(9
7140: 2c 20 31 30 29 3b 0a 20 20 20 20 20 20 52 45 4c  , 10);.      REL
7150: 45 41 53 45 20 74 77 6f 3b 0a 20 20 7d 0a 7d 20  EASE two;.  }.} 
7160: 7b 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65  {}.do_test e_fke
7170: 79 2d 33 31 2e 35 20 7b 0a 20 20 63 61 74 63 68  y-31.5 {.  catch
7180: 73 71 6c 20 7b 52 45 4c 45 41 53 45 20 6f 6e 65  sql {RELEASE one
7190: 7d 0a 7d 20 7b 31 20 7b 66 6f 72 65 69 67 6e 20  }.} {1 {foreign 
71a0: 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 20 66  key constraint f
71b0: 61 69 6c 65 64 7d 7d 0a 64 6f 5f 74 65 73 74 20  ailed}}.do_test 
71c0: 65 5f 66 6b 65 79 2d 33 31 2e 36 20 7b 0a 20 20  e_fkey-31.6 {.  
71d0: 65 78 65 63 73 71 6c 20 7b 52 4f 4c 4c 42 41 43  execsql {ROLLBAC
71e0: 4b 20 54 4f 20 6f 6e 65 20 3b 20 52 45 4c 45 41  K TO one ; RELEA
71f0: 53 45 20 6f 6e 65 7d 0a 7d 20 7b 7d 0a 0a 23 2d  SE one}.} {}..#-
7200: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
7210: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
7220: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
7230: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
7240: 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20 2f 2a 20 45 56  --------.# /* EV
7250: 3a 20 52 2d 33 37 37 33 36 2d 34 32 36 31 36 20  : R-37736-42616 
7260: 2a 2f 0a 23 0a 23 20 54 65 73 74 20 74 68 61 74  */.#.# Test that
7270: 20 69 66 20 61 20 43 4f 4d 4d 49 54 20 6f 70 65   if a COMMIT ope
7280: 72 61 74 69 6f 6e 20 66 61 69 6c 73 20 64 75 65  ration fails due
7290: 20 74 6f 20 64 65 66 65 72 72 65 64 20 66 6f 72   to deferred for
72a0: 65 69 67 6e 20 6b 65 79 20 0a 23 20 63 6f 6e 73  eign key .# cons
72b0: 74 72 61 69 6e 74 73 2c 20 61 6e 79 20 6e 65 73  traints, any nes
72c0: 74 65 64 20 73 61 76 65 70 6f 69 6e 74 73 20 72  ted savepoints r
72d0: 65 6d 61 69 6e 20 6f 70 65 6e 2e 0a 23 0a 64 6f  emain open..#.do
72e0: 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 33 32 2e  _test e_fkey-32.
72f0: 31 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a  1 {.  execsql {.
7300: 20 20 20 20 44 45 4c 45 54 45 20 46 52 4f 4d 20      DELETE FROM 
7310: 74 31 20 57 48 45 52 45 20 61 3e 33 3b 0a 20 20  t1 WHERE a>3;.  
7320: 20 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20    SELECT * FROM 
7330: 74 31 3b 0a 20 20 7d 0a 7d 20 7b 31 20 31 20 32  t1;.  }.} {1 1 2
7340: 20 32 20 33 20 33 7d 0a 64 6f 5f 74 65 73 74 20   2 3 3}.do_test 
7350: 65 5f 66 6b 65 79 2d 33 32 2e 32 20 7b 0a 20 20  e_fkey-32.2 {.  
7360: 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 42 45  execsql {.    BE
7370: 47 49 4e 3b 0a 20 20 20 20 20 20 49 4e 53 45 52  GIN;.      INSER
7380: 54 20 49 4e 54 4f 20 74 31 20 56 41 4c 55 45 53  T INTO t1 VALUES
7390: 28 34 2c 20 34 29 3b 0a 20 20 20 20 20 20 53 41  (4, 4);.      SA
73a0: 56 45 50 4f 49 4e 54 20 6f 6e 65 3b 0a 20 20 20  VEPOINT one;.   
73b0: 20 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f       INSERT INTO
73c0: 20 74 31 20 56 41 4c 55 45 53 28 35 2c 20 36 29   t1 VALUES(5, 6)
73d0: 3b 0a 20 20 20 20 20 20 20 20 53 45 4c 45 43 54  ;.        SELECT
73e0: 20 2a 20 46 52 4f 4d 20 74 31 3b 0a 20 20 7d 0a   * FROM t1;.  }.
73f0: 7d 20 7b 31 20 31 20 32 20 32 20 33 20 33 20 34  } {1 1 2 2 3 3 4
7400: 20 34 20 35 20 36 7d 0a 64 6f 5f 74 65 73 74 20   4 5 6}.do_test 
7410: 65 5f 66 6b 65 79 2d 33 32 2e 33 20 7b 0a 20 20  e_fkey-32.3 {.  
7420: 63 61 74 63 68 73 71 6c 20 43 4f 4d 4d 49 54 0a  catchsql COMMIT.
7430: 7d 20 7b 31 20 7b 66 6f 72 65 69 67 6e 20 6b 65  } {1 {foreign ke
7440: 79 20 63 6f 6e 73 74 72 61 69 6e 74 20 66 61 69  y constraint fai
7450: 6c 65 64 7d 7d 0a 64 6f 5f 74 65 73 74 20 65 5f  led}}.do_test e_
7460: 66 6b 65 79 2d 33 32 2e 34 20 7b 0a 20 20 65 78  fkey-32.4 {.  ex
7470: 65 63 73 71 6c 20 7b 0a 20 20 20 20 52 4f 4c 4c  ecsql {.    ROLL
7480: 42 41 43 4b 20 54 4f 20 6f 6e 65 3b 0a 20 20 20  BACK TO one;.   
7490: 20 43 4f 4d 4d 49 54 3b 0a 20 20 20 20 53 45 4c   COMMIT;.    SEL
74a0: 45 43 54 20 2a 20 46 52 4f 4d 20 74 31 3b 0a 20  ECT * FROM t1;. 
74b0: 20 7d 0a 7d 20 7b 31 20 31 20 32 20 32 20 33 20   }.} {1 1 2 2 3 
74c0: 33 20 34 20 34 7d 0a 0a 64 6f 5f 74 65 73 74 20  3 4 4}..do_test 
74d0: 65 5f 66 6b 65 79 2d 33 32 2e 35 20 7b 0a 20 20  e_fkey-32.5 {.  
74e0: 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 53 41  execsql {.    SA
74f0: 56 45 50 4f 49 4e 54 20 61 3b 0a 20 20 20 20 20  VEPOINT a;.     
7500: 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 31 20   INSERT INTO t1 
7510: 56 41 4c 55 45 53 28 35 2c 20 35 29 3b 0a 20 20  VALUES(5, 5);.  
7520: 20 20 20 20 53 41 56 45 50 4f 49 4e 54 20 62 3b      SAVEPOINT b;
7530: 0a 20 20 20 20 20 20 20 20 49 4e 53 45 52 54 20  .        INSERT 
7540: 49 4e 54 4f 20 74 31 20 56 41 4c 55 45 53 28 36  INTO t1 VALUES(6
7550: 2c 20 37 29 3b 0a 20 20 20 20 20 20 20 20 53 41  , 7);.        SA
7560: 56 45 50 4f 49 4e 54 20 63 3b 0a 20 20 20 20 20  VEPOINT c;.     
7570: 20 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f       INSERT INTO
7580: 20 74 31 20 56 41 4c 55 45 53 28 37 2c 20 38 29   t1 VALUES(7, 8)
7590: 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a 64 6f 5f 74 65  ;.  }.} {}.do_te
75a0: 73 74 20 65 5f 66 6b 65 79 2d 33 32 2e 36 20 7b  st e_fkey-32.6 {
75b0: 0a 20 20 63 61 74 63 68 73 71 6c 20 7b 52 45 4c  .  catchsql {REL
75c0: 45 41 53 45 20 61 7d 0a 7d 20 7b 31 20 7b 66 6f  EASE a}.} {1 {fo
75d0: 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72  reign key constr
75e0: 61 69 6e 74 20 66 61 69 6c 65 64 7d 7d 0a 64 6f  aint failed}}.do
75f0: 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 33 32 2e  _test e_fkey-32.
7600: 37 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 20 7b  7 {.  execsql  {
7610: 52 4f 4c 4c 42 41 43 4b 20 54 4f 20 63 7d 0a 20  ROLLBACK TO c}. 
7620: 20 63 61 74 63 68 73 71 6c 20 7b 52 45 4c 45 41   catchsql {RELEA
7630: 53 45 20 61 7d 0a 7d 20 7b 31 20 7b 66 6f 72 65  SE a}.} {1 {fore
7640: 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69  ign key constrai
7650: 6e 74 20 66 61 69 6c 65 64 7d 7d 0a 64 6f 5f 74  nt failed}}.do_t
7660: 65 73 74 20 65 5f 66 6b 65 79 2d 33 32 2e 38 20  est e_fkey-32.8 
7670: 7b 0a 20 20 65 78 65 63 73 71 6c 20 20 7b 0a 20  {.  execsql  {. 
7680: 20 20 20 52 4f 4c 4c 42 41 43 4b 20 54 4f 20 62     ROLLBACK TO b
7690: 3b 0a 20 20 20 20 52 45 4c 45 41 53 45 20 61 3b  ;.    RELEASE a;
76a0: 0a 20 20 20 20 53 45 4c 45 43 54 20 2a 20 46 52  .    SELECT * FR
76b0: 4f 4d 20 74 31 3b 0a 20 20 7d 0a 7d 20 7b 31 20  OM t1;.  }.} {1 
76c0: 31 20 32 20 32 20 33 20 33 20 34 20 34 20 35 20  1 2 2 3 3 4 4 5 
76d0: 35 7d 0a 0a 23 23 23 23 23 23 23 23 23 23 23 23  5}..############
76e0: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
76f0: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
7700: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
7710: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 0a  ###############.
7720: 23 23 23 20 53 45 43 54 49 4f 4e 20 34 2e 33 3a  ### SECTION 4.3:
7730: 20 4f 4e 20 44 45 4c 45 54 45 20 61 6e 64 20 4f   ON DELETE and O
7740: 4e 20 55 50 44 41 54 45 20 41 63 74 69 6f 6e 73  N UPDATE Actions
7750: 0a 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  .###############
7760: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
7770: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
7780: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
7790: 23 23 23 23 23 23 23 23 23 23 23 23 0a 0a 23 2d  ############..#-
77a0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
77b0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
77c0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
77d0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
77e0: 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20 2f 2a 20 45 56  --------.# /* EV
77f0: 3a 20 52 2d 34 38 32 37 30 2d 34 34 32 38 32 20  : R-48270-44282 
7800: 2a 2f 0a 23 0a 23 20 54 65 73 74 20 74 68 61 74  */.#.# Test that
7810: 20 63 6f 6e 66 69 67 75 72 65 64 20 4f 4e 20 44   configured ON D
7820: 45 4c 45 54 45 20 61 6e 64 20 4f 4e 20 55 50 44  ELETE and ON UPD
7830: 41 54 45 20 61 63 74 69 6f 6e 73 20 74 61 6b 65  ATE actions take
7840: 20 70 6c 61 63 65 20 77 68 65 6e 0a 23 20 64 65   place when.# de
7850: 6c 65 74 69 6e 67 20 6f 72 20 6d 6f 64 69 66 79  leting or modify
7860: 69 6e 67 20 72 6f 77 73 20 6f 66 20 74 68 65 20  ing rows of the 
7870: 70 61 72 65 6e 74 20 74 61 62 6c 65 2c 20 72 65  parent table, re
7880: 73 70 65 63 74 69 76 65 6c 79 2e 0a 23 0a 23 20  spectively..#.# 
7890: 2f 2a 20 45 56 3a 20 52 2d 34 38 31 32 34 2d 36  /* EV: R-48124-6
78a0: 33 32 32 35 20 2a 2f 0a 23 0a 23 20 54 65 73 74  3225 */.#.# Test
78b0: 20 74 68 61 74 20 61 20 73 69 6e 67 6c 65 20 46   that a single F
78c0: 4b 20 63 6f 6e 73 74 72 61 69 6e 74 20 6d 61 79  K constraint may
78d0: 20 68 61 76 65 20 64 69 66 66 65 72 65 6e 74 20   have different 
78e0: 61 63 74 69 6f 6e 73 20 63 6f 6e 66 69 67 75 72  actions configur
78f0: 65 64 0a 23 20 66 6f 72 20 4f 4e 20 44 45 4c 45  ed.# for ON DELE
7900: 54 45 20 61 6e 64 20 4f 4e 20 55 50 44 41 54 45  TE and ON UPDATE
7910: 2e 0a 23 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b  ..#.do_test e_fk
7920: 65 79 2d 31 36 2e 31 20 7b 0a 20 20 65 78 65 63  ey-16.1 {.  exec
7930: 73 71 6c 20 7b 0a 20 20 20 20 43 52 45 41 54 45  sql {.    CREATE
7940: 20 54 41 42 4c 45 20 70 28 61 2c 20 62 20 50 52   TABLE p(a, b PR
7950: 49 4d 41 52 59 20 4b 45 59 2c 20 63 29 3b 0a 20  IMARY KEY, c);. 
7960: 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20     CREATE TABLE 
7970: 63 31 28 64 2c 20 65 2c 20 66 20 44 45 46 41 55  c1(d, e, f DEFAU
7980: 4c 54 20 27 6b 30 27 20 52 45 46 45 52 45 4e 43  LT 'k0' REFERENC
7990: 45 53 20 70 20 0a 20 20 20 20 20 20 4f 4e 20 55  ES p .      ON U
79a0: 50 44 41 54 45 20 53 45 54 20 44 45 46 41 55 4c  PDATE SET DEFAUL
79b0: 54 0a 20 20 20 20 20 20 4f 4e 20 44 45 4c 45 54  T.      ON DELET
79c0: 45 20 53 45 54 20 4e 55 4c 4c 0a 20 20 20 20 29  E SET NULL.    )
79d0: 3b 0a 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e  ;..    INSERT IN
79e0: 54 4f 20 70 20 56 41 4c 55 45 53 28 30 2c 20 27  TO p VALUES(0, '
79f0: 6b 30 27 2c 20 27 27 29 3b 0a 20 20 20 20 49 4e  k0', '');.    IN
7a00: 53 45 52 54 20 49 4e 54 4f 20 70 20 56 41 4c 55  SERT INTO p VALU
7a10: 45 53 28 31 2c 20 27 6b 31 27 2c 20 27 49 27 29  ES(1, 'k1', 'I')
7a20: 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54  ;.    INSERT INT
7a30: 4f 20 70 20 56 41 4c 55 45 53 28 32 2c 20 27 6b  O p VALUES(2, 'k
7a40: 32 27 2c 20 27 49 49 27 29 3b 0a 20 20 20 20 49  2', 'II');.    I
7a50: 4e 53 45 52 54 20 49 4e 54 4f 20 70 20 56 41 4c  NSERT INTO p VAL
7a60: 55 45 53 28 33 2c 20 27 6b 33 27 2c 20 27 49 49  UES(3, 'k3', 'II
7a70: 49 27 29 3b 0a 0a 20 20 20 20 49 4e 53 45 52 54  I');..    INSERT
7a80: 20 49 4e 54 4f 20 63 31 20 56 41 4c 55 45 53 28   INTO c1 VALUES(
7a90: 31 2c 20 27 78 78 27 2c 20 27 6b 31 27 29 3b 0a  1, 'xx', 'k1');.
7aa0: 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20      INSERT INTO 
7ab0: 63 31 20 56 41 4c 55 45 53 28 32 2c 20 27 78 78  c1 VALUES(2, 'xx
7ac0: 27 2c 20 27 6b 32 27 29 3b 0a 20 20 20 20 49 4e  ', 'k2');.    IN
7ad0: 53 45 52 54 20 49 4e 54 4f 20 63 31 20 56 41 4c  SERT INTO c1 VAL
7ae0: 55 45 53 28 33 2c 20 27 78 78 27 2c 20 27 6b 33  UES(3, 'xx', 'k3
7af0: 27 29 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a 64 6f 5f  ');.  }.} {}.do_
7b00: 74 65 73 74 20 65 5f 66 6b 65 79 2d 31 36 2e 32  test e_fkey-16.2
7b10: 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20   {.  execsql {. 
7b20: 20 20 20 55 50 44 41 54 45 20 70 20 53 45 54 20     UPDATE p SET 
7b30: 62 20 3d 20 27 6b 34 27 20 57 48 45 52 45 20 61  b = 'k4' WHERE a
7b40: 20 3d 20 31 3b 0a 20 20 20 20 53 45 4c 45 43 54   = 1;.    SELECT
7b50: 20 2a 20 46 52 4f 4d 20 63 31 3b 0a 20 20 7d 0a   * FROM c1;.  }.
7b60: 7d 20 7b 31 20 78 78 20 6b 30 20 32 20 78 78 20  } {1 xx k0 2 xx 
7b70: 6b 32 20 33 20 78 78 20 6b 33 7d 0a 64 6f 5f 74  k2 3 xx k3}.do_t
7b80: 65 73 74 20 65 5f 66 6b 65 79 2d 31 36 2e 33 20  est e_fkey-16.3 
7b90: 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20  {.  execsql {.  
7ba0: 20 20 44 45 4c 45 54 45 20 46 52 4f 4d 20 70 20    DELETE FROM p 
7bb0: 57 48 45 52 45 20 61 20 3d 20 32 3b 0a 20 20 20  WHERE a = 2;.   
7bc0: 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 63   SELECT * FROM c
7bd0: 31 3b 0a 20 20 7d 0a 7d 20 7b 31 20 78 78 20 6b  1;.  }.} {1 xx k
7be0: 30 20 32 20 78 78 20 7b 7d 20 33 20 78 78 20 6b  0 2 xx {} 3 xx k
7bf0: 33 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65  3}.do_test e_fke
7c00: 79 2d 31 36 2e 34 20 7b 0a 20 20 65 78 65 63 73  y-16.4 {.  execs
7c10: 71 6c 20 7b 0a 20 20 20 20 43 52 45 41 54 45 20  ql {.    CREATE 
7c20: 55 4e 49 51 55 45 20 49 4e 44 45 58 20 70 69 20  UNIQUE INDEX pi 
7c30: 4f 4e 20 70 28 63 29 3b 0a 20 20 20 20 52 45 50  ON p(c);.    REP
7c40: 4c 41 43 45 20 49 4e 54 4f 20 70 20 56 41 4c 55  LACE INTO p VALU
7c50: 45 53 28 35 2c 20 27 6b 35 27 2c 20 27 49 49 49  ES(5, 'k5', 'III
7c60: 27 29 3b 0a 20 20 20 20 53 45 4c 45 43 54 20 2a  ');.    SELECT *
7c70: 20 46 52 4f 4d 20 63 31 3b 0a 20 20 7d 0a 7d 20   FROM c1;.  }.} 
7c80: 7b 31 20 78 78 20 6b 30 20 32 20 78 78 20 7b 7d  {1 xx k0 2 xx {}
7c90: 20 33 20 78 78 20 7b 7d 7d 0a 0a 23 2d 2d 2d 2d   3 xx {}}..#----
7ca0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
7cb0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
7cc0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
7cd0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
7ce0: 2d 2d 2d 2d 2d 0a 23 20 2f 2a 20 45 56 3a 20 52  -----.# /* EV: R
7cf0: 2d 33 33 33 32 36 2d 34 35 32 35 32 20 2a 2f 0a  -33326-45252 */.
7d00: 23 0a 23 20 45 61 63 68 20 66 6f 72 65 69 67 6e  #.# Each foreign
7d10: 20 6b 65 79 20 69 6e 20 74 68 65 20 73 79 73 74   key in the syst
7d20: 65 6d 20 68 61 73 20 61 6e 20 4f 4e 20 55 50 44  em has an ON UPD
7d30: 41 54 45 20 61 6e 64 20 4f 4e 20 44 45 4c 45 54  ATE and ON DELET
7d40: 45 20 61 63 74 69 6f 6e 2c 0a 23 20 65 69 74 68  E action,.# eith
7d50: 65 72 20 22 4e 4f 20 41 43 54 49 4f 4e 22 2c 20  er "NO ACTION", 
7d60: 22 52 45 53 54 52 49 43 54 22 2c 20 22 53 45 54  "RESTRICT", "SET
7d70: 20 4e 55 4c 4c 22 2c 20 22 53 45 54 20 44 45 46   NULL", "SET DEF
7d80: 41 55 4c 54 22 20 6f 72 20 22 43 41 53 43 41 44  AULT" or "CASCAD
7d90: 45 22 2e 0a 23 0a 23 20 2f 2a 20 45 56 3a 20 52  E"..#.# /* EV: R
7da0: 2d 31 39 38 30 33 2d 34 35 38 38 34 20 2a 2f 0a  -19803-45884 */.
7db0: 23 0a 23 20 49 66 20 6e 6f 6e 65 20 69 73 20 73  #.# If none is s
7dc0: 70 65 63 69 66 69 65 64 20 65 78 70 6c 69 63 69  pecified explici
7dd0: 74 6c 79 2c 20 22 4e 4f 20 41 43 54 49 4f 4e 22  tly, "NO ACTION"
7de0: 20 69 73 20 74 68 65 20 64 65 66 61 75 6c 74 2e   is the default.
7df0: 0a 23 20 0a 64 72 6f 70 5f 61 6c 6c 5f 74 61 62  .# .drop_all_tab
7e00: 6c 65 73 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b  les.do_test e_fk
7e10: 65 79 2d 31 37 2e 31 20 7b 0a 20 20 65 78 65 63  ey-17.1 {.  exec
7e20: 73 71 6c 20 7b 0a 20 20 20 20 43 52 45 41 54 45  sql {.    CREATE
7e30: 20 54 41 42 4c 45 20 70 61 72 65 6e 74 28 78 20   TABLE parent(x 
7e40: 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 79 29 3b  PRIMARY KEY, y);
7e50: 0a 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c  .    CREATE TABL
7e60: 45 20 63 68 69 6c 64 31 28 61 2c 20 0a 20 20 20  E child1(a, .   
7e70: 20 20 20 62 20 52 45 46 45 52 45 4e 43 45 53 20     b REFERENCES 
7e80: 70 61 72 65 6e 74 20 4f 4e 20 55 50 44 41 54 45  parent ON UPDATE
7e90: 20 4e 4f 20 41 43 54 49 4f 4e 20 4f 4e 20 44 45   NO ACTION ON DE
7ea0: 4c 45 54 45 20 52 45 53 54 52 49 43 54 0a 20 20  LETE RESTRICT.  
7eb0: 20 20 29 3b 0a 20 20 20 20 43 52 45 41 54 45 20    );.    CREATE 
7ec0: 54 41 42 4c 45 20 63 68 69 6c 64 32 28 61 2c 20  TABLE child2(a, 
7ed0: 0a 20 20 20 20 20 20 62 20 52 45 46 45 52 45 4e  .      b REFEREN
7ee0: 43 45 53 20 70 61 72 65 6e 74 20 4f 4e 20 55 50  CES parent ON UP
7ef0: 44 41 54 45 20 52 45 53 54 52 49 43 54 20 4f 4e  DATE RESTRICT ON
7f00: 20 44 45 4c 45 54 45 20 53 45 54 20 4e 55 4c 4c   DELETE SET NULL
7f10: 0a 20 20 20 20 29 3b 0a 20 20 20 20 43 52 45 41  .    );.    CREA
7f20: 54 45 20 54 41 42 4c 45 20 63 68 69 6c 64 33 28  TE TABLE child3(
7f30: 61 2c 20 0a 20 20 20 20 20 20 62 20 52 45 46 45  a, .      b REFE
7f40: 52 45 4e 43 45 53 20 70 61 72 65 6e 74 20 4f 4e  RENCES parent ON
7f50: 20 55 50 44 41 54 45 20 53 45 54 20 4e 55 4c 4c   UPDATE SET NULL
7f60: 20 4f 4e 20 44 45 4c 45 54 45 20 53 45 54 20 44   ON DELETE SET D
7f70: 45 46 41 55 4c 54 0a 20 20 20 20 29 3b 0a 20 20  EFAULT.    );.  
7f80: 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 63    CREATE TABLE c
7f90: 68 69 6c 64 34 28 61 2c 20 0a 20 20 20 20 20 20  hild4(a, .      
7fa0: 62 20 52 45 46 45 52 45 4e 43 45 53 20 70 61 72  b REFERENCES par
7fb0: 65 6e 74 20 4f 4e 20 55 50 44 41 54 45 20 53 45  ent ON UPDATE SE
7fc0: 54 20 44 45 46 41 55 4c 54 20 4f 4e 20 44 45 4c  T DEFAULT ON DEL
7fd0: 45 54 45 20 43 41 53 43 41 44 45 0a 20 20 20 20  ETE CASCADE.    
7fe0: 29 3b 0a 0a 20 20 20 20 2d 2d 20 43 72 65 61 74  );..    -- Creat
7ff0: 65 20 73 6f 6d 65 20 66 6f 72 65 69 67 6e 20 6b  e some foreign k
8000: 65 79 73 20 74 68 61 74 20 75 73 65 20 74 68 65  eys that use the
8010: 20 64 65 66 61 75 6c 74 20 61 63 74 69 6f 6e 20   default action 
8020: 2d 20 22 4e 4f 20 41 43 54 49 4f 4e 22 0a 20 20  - "NO ACTION".  
8030: 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 63    CREATE TABLE c
8040: 68 69 6c 64 35 28 61 2c 20 62 20 52 45 46 45 52  hild5(a, b REFER
8050: 45 4e 43 45 53 20 70 61 72 65 6e 74 20 4f 4e 20  ENCES parent ON 
8060: 55 50 44 41 54 45 20 43 41 53 43 41 44 45 29 3b  UPDATE CASCADE);
8070: 0a 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c  .    CREATE TABL
8080: 45 20 63 68 69 6c 64 36 28 61 2c 20 62 20 52 45  E child6(a, b RE
8090: 46 45 52 45 4e 43 45 53 20 70 61 72 65 6e 74 20  FERENCES parent 
80a0: 4f 4e 20 44 45 4c 45 54 45 20 52 45 53 54 52 49  ON DELETE RESTRI
80b0: 43 54 29 3b 0a 20 20 20 20 43 52 45 41 54 45 20  CT);.    CREATE 
80c0: 54 41 42 4c 45 20 63 68 69 6c 64 37 28 61 2c 20  TABLE child7(a, 
80d0: 62 20 52 45 46 45 52 45 4e 43 45 53 20 70 61 72  b REFERENCES par
80e0: 65 6e 74 20 4f 4e 20 44 45 4c 45 54 45 20 4e 4f  ent ON DELETE NO
80f0: 20 41 43 54 49 4f 4e 29 3b 0a 20 20 20 20 43 52   ACTION);.    CR
8100: 45 41 54 45 20 54 41 42 4c 45 20 63 68 69 6c 64  EATE TABLE child
8110: 38 28 61 2c 20 62 20 52 45 46 45 52 45 4e 43 45  8(a, b REFERENCE
8120: 53 20 70 61 72 65 6e 74 20 4f 4e 20 55 50 44 41  S parent ON UPDA
8130: 54 45 20 4e 4f 20 41 43 54 49 4f 4e 29 3b 0a 20  TE NO ACTION);. 
8140: 20 7d 0a 7d 20 7b 7d 0a 0a 66 6f 72 65 61 63 68   }.} {}..foreach
8150: 20 7b 74 6e 20 7a 54 61 62 20 6c 52 65 73 7d 20   {tn zTab lRes} 
8160: 7b 0a 20 20 32 20 63 68 69 6c 64 31 20 7b 30 20  {.  2 child1 {0 
8170: 30 20 70 61 72 65 6e 74 20 62 20 7b 7d 20 7b 4e  0 parent b {} {N
8180: 4f 20 41 43 54 49 4f 4e 7d 20 52 45 53 54 52 49  O ACTION} RESTRI
8190: 43 54 20 4e 4f 4e 45 7d 0a 20 20 33 20 63 68 69  CT NONE}.  3 chi
81a0: 6c 64 32 20 7b 30 20 30 20 70 61 72 65 6e 74 20  ld2 {0 0 parent 
81b0: 62 20 7b 7d 20 52 45 53 54 52 49 43 54 20 7b 53  b {} RESTRICT {S
81c0: 45 54 20 4e 55 4c 4c 7d 20 4e 4f 4e 45 7d 0a 20  ET NULL} NONE}. 
81d0: 20 34 20 63 68 69 6c 64 33 20 7b 30 20 30 20 70   4 child3 {0 0 p
81e0: 61 72 65 6e 74 20 62 20 7b 7d 20 7b 53 45 54 20  arent b {} {SET 
81f0: 4e 55 4c 4c 7d 20 7b 53 45 54 20 44 45 46 41 55  NULL} {SET DEFAU
8200: 4c 54 7d 20 4e 4f 4e 45 7d 0a 20 20 35 20 63 68  LT} NONE}.  5 ch
8210: 69 6c 64 34 20 7b 30 20 30 20 70 61 72 65 6e 74  ild4 {0 0 parent
8220: 20 62 20 7b 7d 20 7b 53 45 54 20 44 45 46 41 55   b {} {SET DEFAU
8230: 4c 54 7d 20 43 41 53 43 41 44 45 20 4e 4f 4e 45  LT} CASCADE NONE
8240: 7d 0a 20 20 36 20 63 68 69 6c 64 35 20 7b 30 20  }.  6 child5 {0 
8250: 30 20 70 61 72 65 6e 74 20 62 20 7b 7d 20 43 41  0 parent b {} CA
8260: 53 43 41 44 45 20 7b 4e 4f 20 41 43 54 49 4f 4e  SCADE {NO ACTION
8270: 7d 20 4e 4f 4e 45 7d 0a 20 20 37 20 63 68 69 6c  } NONE}.  7 chil
8280: 64 36 20 7b 30 20 30 20 70 61 72 65 6e 74 20 62  d6 {0 0 parent b
8290: 20 7b 7d 20 7b 4e 4f 20 41 43 54 49 4f 4e 7d 20   {} {NO ACTION} 
82a0: 52 45 53 54 52 49 43 54 20 4e 4f 4e 45 7d 0a 20  RESTRICT NONE}. 
82b0: 20 38 20 63 68 69 6c 64 37 20 7b 30 20 30 20 70   8 child7 {0 0 p
82c0: 61 72 65 6e 74 20 62 20 7b 7d 20 7b 4e 4f 20 41  arent b {} {NO A
82d0: 43 54 49 4f 4e 7d 20 7b 4e 4f 20 41 43 54 49 4f  CTION} {NO ACTIO
82e0: 4e 7d 20 4e 4f 4e 45 7d 0a 20 20 39 20 63 68 69  N} NONE}.  9 chi
82f0: 6c 64 38 20 7b 30 20 30 20 70 61 72 65 6e 74 20  ld8 {0 0 parent 
8300: 62 20 7b 7d 20 7b 4e 4f 20 41 43 54 49 4f 4e 7d  b {} {NO ACTION}
8310: 20 7b 4e 4f 20 41 43 54 49 4f 4e 7d 20 4e 4f 4e   {NO ACTION} NON
8320: 45 7d 0a 7d 20 7b 0a 20 20 64 6f 5f 74 65 73 74  E}.} {.  do_test
8330: 20 65 5f 66 6b 65 79 2d 31 37 2e 24 74 6e 20 7b   e_fkey-17.$tn {
8340: 20 65 78 65 63 73 71 6c 20 22 50 52 41 47 4d 41   execsql "PRAGMA
8350: 20 66 6f 72 65 69 67 6e 5f 6b 65 79 5f 6c 69 73   foreign_key_lis
8360: 74 28 24 7a 54 61 62 29 22 20 7d 20 24 6c 52 65  t($zTab)" } $lRe
8370: 73 0a 7d 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  s.}..#----------
8380: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
8390: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
83a0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
83b0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a  ---------------.
83c0: 23 20 2f 2a 20 45 56 3a 20 52 2d 31 39 39 37 31  # /* EV: R-19971
83d0: 2d 35 34 39 37 36 20 2a 2f 0a 23 0a 23 20 54 65  -54976 */.#.# Te
83e0: 73 74 20 74 68 61 74 20 22 4e 4f 20 41 43 54 49  st that "NO ACTI
83f0: 4f 4e 22 20 6d 65 61 6e 73 20 74 68 61 74 20 6e  ON" means that n
8400: 6f 74 68 69 6e 67 20 68 61 70 70 65 6e 73 20 74  othing happens t
8410: 6f 20 61 20 63 68 69 6c 64 20 72 6f 77 20 77 68  o a child row wh
8420: 65 6e 0a 23 20 69 74 27 73 20 70 61 72 65 6e 74  en.# it's parent
8430: 20 72 6f 77 20 69 73 20 75 70 64 61 74 65 64 20   row is updated 
8440: 6f 72 20 64 65 6c 65 74 65 64 2e 0a 23 0a 64 72  or deleted..#.dr
8450: 6f 70 5f 61 6c 6c 5f 74 61 62 6c 65 73 0a 64 6f  op_all_tables.do
8460: 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 31 38 2e  _test e_fkey-18.
8470: 31 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a  1 {.  execsql {.
8480: 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45      CREATE TABLE
8490: 20 70 61 72 65 6e 74 28 70 31 2c 20 70 32 2c 20   parent(p1, p2, 
84a0: 50 52 49 4d 41 52 59 20 4b 45 59 28 70 31 2c 20  PRIMARY KEY(p1, 
84b0: 70 32 29 29 3b 0a 20 20 20 20 43 52 45 41 54 45  p2));.    CREATE
84c0: 20 54 41 42 4c 45 20 63 68 69 6c 64 28 63 31 2c   TABLE child(c1,
84d0: 20 63 32 2c 20 0a 20 20 20 20 20 20 46 4f 52 45   c2, .      FORE
84e0: 49 47 4e 20 4b 45 59 28 63 31 2c 20 63 32 29 20  IGN KEY(c1, c2) 
84f0: 52 45 46 45 52 45 4e 43 45 53 20 70 61 72 65 6e  REFERENCES paren
8500: 74 0a 20 20 20 20 20 20 4f 4e 20 55 50 44 41 54  t.      ON UPDAT
8510: 45 20 4e 4f 20 41 43 54 49 4f 4e 0a 20 20 20 20  E NO ACTION.    
8520: 20 20 4f 4e 20 44 45 4c 45 54 45 20 4e 4f 20 41    ON DELETE NO A
8530: 43 54 49 4f 4e 0a 20 20 20 20 20 20 44 45 46 45  CTION.      DEFE
8540: 52 52 41 42 4c 45 20 49 4e 49 54 49 41 4c 4c 59  RRABLE INITIALLY
8550: 20 44 45 46 45 52 52 45 44 0a 20 20 20 20 29 3b   DEFERRED.    );
8560: 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f  .    INSERT INTO
8570: 20 70 61 72 65 6e 74 20 56 41 4c 55 45 53 28 27   parent VALUES('
8580: 6a 27 2c 20 27 6b 27 29 3b 0a 20 20 20 20 49 4e  j', 'k');.    IN
8590: 53 45 52 54 20 49 4e 54 4f 20 70 61 72 65 6e 74  SERT INTO parent
85a0: 20 56 41 4c 55 45 53 28 27 6c 27 2c 20 27 6d 27   VALUES('l', 'm'
85b0: 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e  );.    INSERT IN
85c0: 54 4f 20 63 68 69 6c 64 20 56 41 4c 55 45 53 28  TO child VALUES(
85d0: 27 6a 27 2c 20 27 6b 27 29 3b 0a 20 20 20 20 49  'j', 'k');.    I
85e0: 4e 53 45 52 54 20 49 4e 54 4f 20 63 68 69 6c 64  NSERT INTO child
85f0: 20 56 41 4c 55 45 53 28 27 6c 27 2c 20 27 6d 27   VALUES('l', 'm'
8600: 29 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a 64 6f 5f 74  );.  }.} {}.do_t
8610: 65 73 74 20 65 5f 66 6b 65 79 2d 31 38 2e 32 20  est e_fkey-18.2 
8620: 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20  {.  execsql {.  
8630: 20 20 42 45 47 49 4e 3b 0a 20 20 20 20 20 20 55    BEGIN;.      U
8640: 50 44 41 54 45 20 70 61 72 65 6e 74 20 53 45 54  PDATE parent SET
8650: 20 70 31 3d 27 6b 27 20 57 48 45 52 45 20 70 31   p1='k' WHERE p1
8660: 3d 27 6a 27 3b 0a 20 20 20 20 20 20 44 45 4c 45  ='j';.      DELE
8670: 54 45 20 46 52 4f 4d 20 70 61 72 65 6e 74 20 57  TE FROM parent W
8680: 48 45 52 45 20 70 31 3d 27 6c 27 3b 0a 20 20 20  HERE p1='l';.   
8690: 20 20 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d     SELECT * FROM
86a0: 20 63 68 69 6c 64 3b 0a 20 20 7d 0a 7d 20 7b 6a   child;.  }.} {j
86b0: 20 6b 20 6c 20 6d 7d 0a 64 6f 5f 74 65 73 74 20   k l m}.do_test 
86c0: 65 5f 66 6b 65 79 2d 31 38 2e 33 20 7b 0a 20 20  e_fkey-18.3 {.  
86d0: 63 61 74 63 68 73 71 6c 20 43 4f 4d 4d 49 54 0a  catchsql COMMIT.
86e0: 7d 20 7b 31 20 7b 66 6f 72 65 69 67 6e 20 6b 65  } {1 {foreign ke
86f0: 79 20 63 6f 6e 73 74 72 61 69 6e 74 20 66 61 69  y constraint fai
8700: 6c 65 64 7d 7d 0a 64 6f 5f 74 65 73 74 20 65 5f  led}}.do_test e_
8710: 66 6b 65 79 2d 31 38 2e 34 20 7b 0a 20 20 65 78  fkey-18.4 {.  ex
8720: 65 63 73 71 6c 20 52 4f 4c 4c 42 41 43 4b 0a 7d  ecsql ROLLBACK.}
8730: 20 7b 7d 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d   {}..#----------
8740: 2d 2d 2d 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 0a  ---------------.
8780: 23 20 2f 2a 20 45 56 3a 20 52 2d 30 34 32 37 32  # /* EV: R-04272
8790: 2d 33 38 36 35 33 20 2a 2f 0a 23 0a 23 20 54 65  -38653 */.#.# Te
87a0: 73 74 20 74 68 61 74 20 22 52 45 53 54 52 49 43  st that "RESTRIC
87b0: 54 22 20 6d 65 61 6e 73 20 74 68 65 20 61 70 70  T" means the app
87c0: 6c 69 63 61 74 69 6f 6e 20 69 73 20 70 72 6f 68  lication is proh
87d0: 69 62 69 74 65 64 20 66 72 6f 6d 20 64 65 6c 65  ibited from dele
87e0: 74 69 6e 67 0a 23 20 6f 72 20 75 70 64 61 74 69  ting.# or updati
87f0: 6e 67 20 61 20 70 61 72 65 6e 74 20 74 61 62 6c  ng a parent tabl
8800: 65 20 72 6f 77 20 77 68 65 6e 20 74 68 65 72 65  e row when there
8810: 20 65 78 69 73 74 73 20 6f 6e 65 20 6f 72 20 6d   exists one or m
8820: 6f 72 65 20 63 68 69 6c 64 20 6b 65 79 73 0a 23  ore child keys.#
8830: 20 6d 61 70 70 65 64 20 74 6f 20 69 74 2e 0a 23   mapped to it..#
8840: 0a 64 72 6f 70 5f 61 6c 6c 5f 74 61 62 6c 65 73  .drop_all_tables
8850: 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d  .do_test e_fkey-
8860: 31 38 2e 31 20 7b 0a 20 20 65 78 65 63 73 71 6c  18.1 {.  execsql
8870: 20 7b 0a 20 20 20 20 43 52 45 41 54 45 20 54 41   {.    CREATE TA
8880: 42 4c 45 20 70 61 72 65 6e 74 28 70 31 2c 20 70  BLE parent(p1, p
8890: 32 29 3b 0a 20 20 20 20 43 52 45 41 54 45 20 55  2);.    CREATE U
88a0: 4e 49 51 55 45 20 49 4e 44 45 58 20 70 61 72 65  NIQUE INDEX pare
88b0: 6e 74 5f 69 20 4f 4e 20 70 61 72 65 6e 74 28 70  nt_i ON parent(p
88c0: 31 2c 20 70 32 29 3b 0a 20 20 20 20 43 52 45 41  1, p2);.    CREA
88d0: 54 45 20 54 41 42 4c 45 20 63 68 69 6c 64 31 28  TE TABLE child1(
88e0: 63 31 2c 20 63 32 2c 20 0a 20 20 20 20 20 20 46  c1, c2, .      F
88f0: 4f 52 45 49 47 4e 20 4b 45 59 28 63 32 2c 20 63  OREIGN KEY(c2, c
8900: 31 29 20 52 45 46 45 52 45 4e 43 45 53 20 70 61  1) REFERENCES pa
8910: 72 65 6e 74 28 70 31 2c 20 70 32 29 20 4f 4e 20  rent(p1, p2) ON 
8920: 44 45 4c 45 54 45 20 52 45 53 54 52 49 43 54 0a  DELETE RESTRICT.
8930: 20 20 20 20 29 3b 0a 20 20 20 20 43 52 45 41 54      );.    CREAT
8940: 45 20 54 41 42 4c 45 20 63 68 69 6c 64 32 28 63  E TABLE child2(c
8950: 31 2c 20 63 32 2c 20 0a 20 20 20 20 20 20 46 4f  1, c2, .      FO
8960: 52 45 49 47 4e 20 4b 45 59 28 63 32 2c 20 63 31  REIGN KEY(c2, c1
8970: 29 20 52 45 46 45 52 45 4e 43 45 53 20 70 61 72  ) REFERENCES par
8980: 65 6e 74 28 70 31 2c 20 70 32 29 20 4f 4e 20 55  ent(p1, p2) ON U
8990: 50 44 41 54 45 20 52 45 53 54 52 49 43 54 0a 20  PDATE RESTRICT. 
89a0: 20 20 20 29 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a 64     );.  }.} {}.d
89b0: 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 31 38  o_test e_fkey-18
89c0: 2e 32 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b  .2 {.  execsql {
89d0: 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f  .    INSERT INTO
89e0: 20 70 61 72 65 6e 74 20 56 41 4c 55 45 53 28 27   parent VALUES('
89f0: 61 27 2c 20 27 62 27 29 3b 0a 20 20 20 20 49 4e  a', 'b');.    IN
8a00: 53 45 52 54 20 49 4e 54 4f 20 70 61 72 65 6e 74  SERT INTO parent
8a10: 20 56 41 4c 55 45 53 28 27 63 27 2c 20 27 64 27   VALUES('c', 'd'
8a20: 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e  );.    INSERT IN
8a30: 54 4f 20 63 68 69 6c 64 31 20 56 41 4c 55 45 53  TO child1 VALUES
8a40: 28 27 62 27 2c 20 27 61 27 29 3b 0a 20 20 20 20  ('b', 'a');.    
8a50: 49 4e 53 45 52 54 20 49 4e 54 4f 20 63 68 69 6c  INSERT INTO chil
8a60: 64 32 20 56 41 4c 55 45 53 28 27 64 27 2c 20 27  d2 VALUES('d', '
8a70: 63 27 29 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a 64 6f  c');.  }.} {}.do
8a80: 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 31 38 2e  _test e_fkey-18.
8a90: 33 20 7b 0a 20 20 63 61 74 63 68 73 71 6c 20 7b  3 {.  catchsql {
8aa0: 20 44 45 4c 45 54 45 20 46 52 4f 4d 20 70 61 72   DELETE FROM par
8ab0: 65 6e 74 20 57 48 45 52 45 20 70 31 20 3d 20 27  ent WHERE p1 = '
8ac0: 61 27 20 7d 0a 7d 20 7b 31 20 7b 66 6f 72 65 69  a' }.} {1 {forei
8ad0: 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e  gn key constrain
8ae0: 74 20 66 61 69 6c 65 64 7d 7d 0a 64 6f 5f 74 65  t failed}}.do_te
8af0: 73 74 20 65 5f 66 6b 65 79 2d 31 38 2e 34 20 7b  st e_fkey-18.4 {
8b00: 0a 20 20 63 61 74 63 68 73 71 6c 20 7b 20 55 50  .  catchsql { UP
8b10: 44 41 54 45 20 70 61 72 65 6e 74 20 53 45 54 20  DATE parent SET 
8b20: 70 32 20 3d 20 27 65 27 20 57 48 45 52 45 20 70  p2 = 'e' WHERE p
8b30: 31 20 3d 20 27 63 27 20 7d 0a 7d 20 7b 31 20 7b  1 = 'c' }.} {1 {
8b40: 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73  foreign key cons
8b50: 74 72 61 69 6e 74 20 66 61 69 6c 65 64 7d 7d 0a  traint failed}}.
8b60: 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  .#--------------
8b70: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
8b80: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
8b90: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
8ba0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20 2f 2a  -----------.# /*
8bb0: 20 45 56 3a 20 52 2d 33 37 39 39 37 2d 34 32 31   EV: R-37997-421
8bc0: 38 37 20 2a 2f 0a 23 20 0a 23 20 54 65 73 74 20  87 */.# .# Test 
8bd0: 74 68 61 74 20 52 45 53 54 52 49 43 54 20 69 73  that RESTRICT is
8be0: 20 73 6c 69 67 68 74 6c 79 20 64 69 66 66 65 72   slightly differ
8bf0: 65 6e 74 20 66 72 6f 6d 20 4e 4f 20 41 43 54 49  ent from NO ACTI
8c00: 4f 4e 20 66 6f 72 20 49 4d 4d 45 44 49 41 54 45  ON for IMMEDIATE
8c10: 0a 23 20 63 6f 6e 73 74 72 61 69 6e 74 73 2c 20  .# constraints, 
8c20: 69 6e 20 74 68 61 74 20 69 74 20 69 73 20 65 6e  in that it is en
8c30: 66 6f 72 63 65 64 20 69 6d 6d 65 64 69 61 74 65  forced immediate
8c40: 6c 79 2c 20 6e 6f 74 20 61 74 20 74 68 65 20 65  ly, not at the e
8c50: 6e 64 20 6f 66 20 74 68 65 20 0a 23 20 73 74 61  nd of the .# sta
8c60: 74 65 6d 65 6e 74 2e 0a 23 0a 64 72 6f 70 5f 61  tement..#.drop_a
8c70: 6c 6c 5f 74 61 62 6c 65 73 0a 64 6f 5f 74 65 73  ll_tables.do_tes
8c80: 74 20 65 5f 66 6b 65 79 2d 31 39 2e 31 20 7b 0a  t e_fkey-19.1 {.
8c90: 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20    execsql {.    
8ca0: 43 52 45 41 54 45 20 54 41 42 4c 45 20 70 61 72  CREATE TABLE par
8cb0: 65 6e 74 28 78 20 50 52 49 4d 41 52 59 20 4b 45  ent(x PRIMARY KE
8cc0: 59 29 3b 0a 20 20 20 20 43 52 45 41 54 45 20 54  Y);.    CREATE T
8cd0: 41 42 4c 45 20 63 68 69 6c 64 31 28 63 20 52 45  ABLE child1(c RE
8ce0: 46 45 52 45 4e 43 45 53 20 70 61 72 65 6e 74 20  FERENCES parent 
8cf0: 4f 4e 20 55 50 44 41 54 45 20 52 45 53 54 52 49  ON UPDATE RESTRI
8d00: 43 54 29 3b 0a 20 20 20 20 43 52 45 41 54 45 20  CT);.    CREATE 
8d10: 54 41 42 4c 45 20 63 68 69 6c 64 32 28 63 20 52  TABLE child2(c R
8d20: 45 46 45 52 45 4e 43 45 53 20 70 61 72 65 6e 74  EFERENCES parent
8d30: 20 4f 4e 20 55 50 44 41 54 45 20 4e 4f 20 41 43   ON UPDATE NO AC
8d40: 54 49 4f 4e 29 3b 0a 0a 20 20 20 20 49 4e 53 45  TION);..    INSE
8d50: 52 54 20 49 4e 54 4f 20 70 61 72 65 6e 74 20 56  RT INTO parent V
8d60: 41 4c 55 45 53 28 27 6b 65 79 31 27 29 3b 0a 20  ALUES('key1');. 
8d70: 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 70     INSERT INTO p
8d80: 61 72 65 6e 74 20 56 41 4c 55 45 53 28 27 6b 65  arent VALUES('ke
8d90: 79 32 27 29 3b 0a 20 20 20 20 49 4e 53 45 52 54  y2');.    INSERT
8da0: 20 49 4e 54 4f 20 63 68 69 6c 64 31 20 56 41 4c   INTO child1 VAL
8db0: 55 45 53 28 27 6b 65 79 31 27 29 3b 0a 20 20 20  UES('key1');.   
8dc0: 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 63 68 69   INSERT INTO chi
8dd0: 6c 64 32 20 56 41 4c 55 45 53 28 27 6b 65 79 32  ld2 VALUES('key2
8de0: 27 29 3b 0a 0a 20 20 20 20 43 52 45 41 54 45 20  ');..    CREATE 
8df0: 54 52 49 47 47 45 52 20 70 61 72 65 6e 74 5f 74  TRIGGER parent_t
8e00: 20 41 46 54 45 52 20 55 50 44 41 54 45 20 4f 4e   AFTER UPDATE ON
8e10: 20 70 61 72 65 6e 74 20 42 45 47 49 4e 0a 20 20   parent BEGIN.  
8e20: 20 20 20 20 55 50 44 41 54 45 20 63 68 69 6c 64      UPDATE child
8e30: 31 20 73 65 74 20 63 20 3d 20 6e 65 77 2e 78 20  1 set c = new.x 
8e40: 57 48 45 52 45 20 63 20 3d 20 6f 6c 64 2e 78 3b  WHERE c = old.x;
8e50: 0a 20 20 20 20 20 20 55 50 44 41 54 45 20 63 68  .      UPDATE ch
8e60: 69 6c 64 32 20 73 65 74 20 63 20 3d 20 6e 65 77  ild2 set c = new
8e70: 2e 78 20 57 48 45 52 45 20 63 20 3d 20 6f 6c 64  .x WHERE c = old
8e80: 2e 78 3b 0a 20 20 20 20 45 4e 44 3b 0a 20 20 7d  .x;.    END;.  }
8e90: 0a 7d 20 7b 7d 0a 64 6f 5f 74 65 73 74 20 65 5f  .} {}.do_test e_
8ea0: 66 6b 65 79 2d 31 39 2e 32 20 7b 0a 20 20 63 61  fkey-19.2 {.  ca
8eb0: 74 63 68 73 71 6c 20 7b 20 55 50 44 41 54 45 20  tchsql { UPDATE 
8ec0: 70 61 72 65 6e 74 20 53 45 54 20 78 20 3d 20 27  parent SET x = '
8ed0: 6b 65 79 20 6f 6e 65 27 20 57 48 45 52 45 20 78  key one' WHERE x
8ee0: 20 3d 20 27 6b 65 79 31 27 20 7d 0a 7d 20 7b 31   = 'key1' }.} {1
8ef0: 20 7b 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f   {foreign key co
8f00: 6e 73 74 72 61 69 6e 74 20 66 61 69 6c 65 64 7d  nstraint failed}
8f10: 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79  }.do_test e_fkey
8f20: 2d 31 39 2e 33 20 7b 0a 20 20 65 78 65 63 73 71  -19.3 {.  execsq
8f30: 6c 20 7b 20 0a 20 20 20 20 55 50 44 41 54 45 20  l { .    UPDATE 
8f40: 70 61 72 65 6e 74 20 53 45 54 20 78 20 3d 20 27  parent SET x = '
8f50: 6b 65 79 20 74 77 6f 27 20 57 48 45 52 45 20 78  key two' WHERE x
8f60: 20 3d 20 27 6b 65 79 32 27 3b 0a 20 20 20 20 53   = 'key2';.    S
8f70: 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 63 68 69  ELECT * FROM chi
8f80: 6c 64 32 3b 0a 20 20 7d 0a 7d 20 7b 7b 6b 65 79  ld2;.  }.} {{key
8f90: 20 74 77 6f 7d 7d 0a 0a 64 72 6f 70 5f 61 6c 6c   two}}..drop_all
8fa0: 5f 74 61 62 6c 65 73 0a 64 6f 5f 74 65 73 74 20  _tables.do_test 
8fb0: 65 5f 66 6b 65 79 2d 31 39 2e 34 20 7b 0a 20 20  e_fkey-19.4 {.  
8fc0: 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 43 52  execsql {.    CR
8fd0: 45 41 54 45 20 54 41 42 4c 45 20 70 61 72 65 6e  EATE TABLE paren
8fe0: 74 28 78 20 50 52 49 4d 41 52 59 20 4b 45 59 29  t(x PRIMARY KEY)
8ff0: 3b 0a 20 20 20 20 43 52 45 41 54 45 20 54 41 42  ;.    CREATE TAB
9000: 4c 45 20 63 68 69 6c 64 31 28 63 20 52 45 46 45  LE child1(c REFE
9010: 52 45 4e 43 45 53 20 70 61 72 65 6e 74 20 4f 4e  RENCES parent ON
9020: 20 44 45 4c 45 54 45 20 52 45 53 54 52 49 43 54   DELETE RESTRICT
9030: 29 3b 0a 20 20 20 20 43 52 45 41 54 45 20 54 41  );.    CREATE TA
9040: 42 4c 45 20 63 68 69 6c 64 32 28 63 20 52 45 46  BLE child2(c REF
9050: 45 52 45 4e 43 45 53 20 70 61 72 65 6e 74 20 4f  ERENCES parent O
9060: 4e 20 44 45 4c 45 54 45 20 4e 4f 20 41 43 54 49  N DELETE NO ACTI
9070: 4f 4e 29 3b 0a 0a 20 20 20 20 49 4e 53 45 52 54  ON);..    INSERT
9080: 20 49 4e 54 4f 20 70 61 72 65 6e 74 20 56 41 4c   INTO parent VAL
9090: 55 45 53 28 27 6b 65 79 31 27 29 3b 0a 20 20 20  UES('key1');.   
90a0: 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 70 61 72   INSERT INTO par
90b0: 65 6e 74 20 56 41 4c 55 45 53 28 27 6b 65 79 32  ent VALUES('key2
90c0: 27 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49  ');.    INSERT I
90d0: 4e 54 4f 20 63 68 69 6c 64 31 20 56 41 4c 55 45  NTO child1 VALUE
90e0: 53 28 27 6b 65 79 31 27 29 3b 0a 20 20 20 20 49  S('key1');.    I
90f0: 4e 53 45 52 54 20 49 4e 54 4f 20 63 68 69 6c 64  NSERT INTO child
9100: 32 20 56 41 4c 55 45 53 28 27 6b 65 79 32 27 29  2 VALUES('key2')
9110: 3b 0a 0a 20 20 20 20 43 52 45 41 54 45 20 54 52  ;..    CREATE TR
9120: 49 47 47 45 52 20 70 61 72 65 6e 74 5f 74 20 41  IGGER parent_t A
9130: 46 54 45 52 20 44 45 4c 45 54 45 20 4f 4e 20 70  FTER DELETE ON p
9140: 61 72 65 6e 74 20 42 45 47 49 4e 0a 20 20 20 20  arent BEGIN.    
9150: 20 20 55 50 44 41 54 45 20 63 68 69 6c 64 31 20    UPDATE child1 
9160: 53 45 54 20 63 20 3d 20 4e 55 4c 4c 20 57 48 45  SET c = NULL WHE
9170: 52 45 20 63 20 3d 20 6f 6c 64 2e 78 3b 0a 20 20  RE c = old.x;.  
9180: 20 20 20 20 55 50 44 41 54 45 20 63 68 69 6c 64      UPDATE child
9190: 32 20 53 45 54 20 63 20 3d 20 4e 55 4c 4c 20 57  2 SET c = NULL W
91a0: 48 45 52 45 20 63 20 3d 20 6f 6c 64 2e 78 3b 0a  HERE c = old.x;.
91b0: 20 20 20 20 45 4e 44 3b 0a 20 20 7d 0a 7d 20 7b      END;.  }.} {
91c0: 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79  }.do_test e_fkey
91d0: 2d 31 39 2e 35 20 7b 0a 20 20 63 61 74 63 68 73  -19.5 {.  catchs
91e0: 71 6c 20 7b 20 44 45 4c 45 54 45 20 46 52 4f 4d  ql { DELETE FROM
91f0: 20 70 61 72 65 6e 74 20 57 48 45 52 45 20 78 20   parent WHERE x 
9200: 3d 20 27 6b 65 79 31 27 20 7d 0a 7d 20 7b 31 20  = 'key1' }.} {1 
9210: 7b 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e  {foreign key con
9220: 73 74 72 61 69 6e 74 20 66 61 69 6c 65 64 7d 7d  straint failed}}
9230: 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d  .do_test e_fkey-
9240: 31 39 2e 36 20 7b 0a 20 20 65 78 65 63 73 71 6c  19.6 {.  execsql
9250: 20 7b 20 0a 20 20 20 20 44 45 4c 45 54 45 20 46   { .    DELETE F
9260: 52 4f 4d 20 70 61 72 65 6e 74 20 57 48 45 52 45  ROM parent WHERE
9270: 20 78 20 3d 20 27 6b 65 79 32 27 3b 0a 20 20 20   x = 'key2';.   
9280: 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 63   SELECT * FROM c
9290: 68 69 6c 64 32 3b 0a 20 20 7d 0a 7d 20 7b 7b 7d  hild2;.  }.} {{}
92a0: 7d 0a 0a 64 72 6f 70 5f 61 6c 6c 5f 74 61 62 6c  }..drop_all_tabl
92b0: 65 73 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65  es.do_test e_fke
92c0: 79 2d 31 39 2e 37 20 7b 0a 20 20 65 78 65 63 73  y-19.7 {.  execs
92d0: 71 6c 20 7b 0a 20 20 20 20 43 52 45 41 54 45 20  ql {.    CREATE 
92e0: 54 41 42 4c 45 20 70 61 72 65 6e 74 28 78 20 50  TABLE parent(x P
92f0: 52 49 4d 41 52 59 20 4b 45 59 29 3b 0a 20 20 20  RIMARY KEY);.   
9300: 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 63 68   CREATE TABLE ch
9310: 69 6c 64 31 28 63 20 52 45 46 45 52 45 4e 43 45  ild1(c REFERENCE
9320: 53 20 70 61 72 65 6e 74 20 4f 4e 20 44 45 4c 45  S parent ON DELE
9330: 54 45 20 52 45 53 54 52 49 43 54 29 3b 0a 20 20  TE RESTRICT);.  
9340: 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 63    CREATE TABLE c
9350: 68 69 6c 64 32 28 63 20 52 45 46 45 52 45 4e 43  hild2(c REFERENC
9360: 45 53 20 70 61 72 65 6e 74 20 4f 4e 20 44 45 4c  ES parent ON DEL
9370: 45 54 45 20 4e 4f 20 41 43 54 49 4f 4e 29 3b 0a  ETE NO ACTION);.
9380: 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f  .    INSERT INTO
9390: 20 70 61 72 65 6e 74 20 56 41 4c 55 45 53 28 27   parent VALUES('
93a0: 6b 65 79 31 27 29 3b 0a 20 20 20 20 49 4e 53 45  key1');.    INSE
93b0: 52 54 20 49 4e 54 4f 20 70 61 72 65 6e 74 20 56  RT INTO parent V
93c0: 41 4c 55 45 53 28 27 6b 65 79 32 27 29 3b 0a 20  ALUES('key2');. 
93d0: 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 63     INSERT INTO c
93e0: 68 69 6c 64 31 20 56 41 4c 55 45 53 28 27 6b 65  hild1 VALUES('ke
93f0: 79 31 27 29 3b 0a 20 20 20 20 49 4e 53 45 52 54  y1');.    INSERT
9400: 20 49 4e 54 4f 20 63 68 69 6c 64 32 20 56 41 4c   INTO child2 VAL
9410: 55 45 53 28 27 6b 65 79 32 27 29 3b 0a 20 20 7d  UES('key2');.  }
9420: 0a 7d 20 7b 7d 0a 64 6f 5f 74 65 73 74 20 65 5f  .} {}.do_test e_
9430: 66 6b 65 79 2d 31 39 2e 38 20 7b 0a 20 20 63 61  fkey-19.8 {.  ca
9440: 74 63 68 73 71 6c 20 7b 20 52 45 50 4c 41 43 45  tchsql { REPLACE
9450: 20 49 4e 54 4f 20 70 61 72 65 6e 74 20 56 41 4c   INTO parent VAL
9460: 55 45 53 28 27 6b 65 79 31 27 29 20 7d 0a 7d 20  UES('key1') }.} 
9470: 7b 31 20 7b 66 6f 72 65 69 67 6e 20 6b 65 79 20  {1 {foreign key 
9480: 63 6f 6e 73 74 72 61 69 6e 74 20 66 61 69 6c 65  constraint faile
9490: 64 7d 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b  d}}.do_test e_fk
94a0: 65 79 2d 31 39 2e 39 20 7b 0a 20 20 65 78 65 63  ey-19.9 {.  exec
94b0: 73 71 6c 20 7b 20 0a 20 20 20 20 52 45 50 4c 41  sql { .    REPLA
94c0: 43 45 20 49 4e 54 4f 20 70 61 72 65 6e 74 20 56  CE INTO parent V
94d0: 41 4c 55 45 53 28 27 6b 65 79 32 27 29 3b 0a 20  ALUES('key2');. 
94e0: 20 20 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d     SELECT * FROM
94f0: 20 63 68 69 6c 64 32 3b 0a 20 20 7d 0a 7d 20 7b   child2;.  }.} {
9500: 6b 65 79 32 7d 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d  key2}..#--------
9510: 2d 2d 2d 2d 2d 2d 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 0a 23 20 2f 2a 20 45 56 3a 20 52 2d 32 34 31  -.# /* EV: R-241
9560: 37 39 2d 36 30 35 32 33 20 2a 2f 0a 23 20 0a 23  79-60523 */.# .#
9570: 20 54 65 73 74 20 74 68 61 74 20 52 45 53 54 52   Test that RESTR
9580: 49 43 54 20 69 73 20 65 6e 66 6f 72 63 65 64 20  ICT is enforced 
9590: 69 6d 6d 65 64 69 61 74 65 6c 79 2c 20 65 76 65  immediately, eve
95a0: 6e 20 66 6f 72 20 61 20 44 45 46 45 52 52 45 44  n for a DEFERRED
95b0: 20 63 6f 6e 73 74 72 61 69 6e 74 2e 0a 23 0a 64   constraint..#.d
95c0: 72 6f 70 5f 61 6c 6c 5f 74 61 62 6c 65 73 0a 64  rop_all_tables.d
95d0: 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 32 30  o_test e_fkey-20
95e0: 2e 31 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b  .1 {.  execsql {
95f0: 0a 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c  .    CREATE TABL
9600: 45 20 70 61 72 65 6e 74 28 78 20 50 52 49 4d 41  E parent(x PRIMA
9610: 52 59 20 4b 45 59 29 3b 0a 20 20 20 20 43 52 45  RY KEY);.    CRE
9620: 41 54 45 20 54 41 42 4c 45 20 63 68 69 6c 64 31  ATE TABLE child1
9630: 28 63 20 52 45 46 45 52 45 4e 43 45 53 20 70 61  (c REFERENCES pa
9640: 72 65 6e 74 20 4f 4e 20 55 50 44 41 54 45 20 52  rent ON UPDATE R
9650: 45 53 54 52 49 43 54 0a 20 20 20 20 20 20 44 45  ESTRICT.      DE
9660: 46 45 52 52 41 42 4c 45 20 49 4e 49 54 49 41 4c  FERRABLE INITIAL
9670: 4c 59 20 44 45 46 45 52 52 45 44 0a 20 20 20 20  LY DEFERRED.    
9680: 29 3b 0a 20 20 20 20 43 52 45 41 54 45 20 54 41  );.    CREATE TA
9690: 42 4c 45 20 63 68 69 6c 64 32 28 63 20 52 45 46  BLE child2(c REF
96a0: 45 52 45 4e 43 45 53 20 70 61 72 65 6e 74 20 4f  ERENCES parent O
96b0: 4e 20 55 50 44 41 54 45 20 4e 4f 20 41 43 54 49  N UPDATE NO ACTI
96c0: 4f 4e 0a 20 20 20 20 20 20 44 45 46 45 52 52 41  ON.      DEFERRA
96d0: 42 4c 45 20 49 4e 49 54 49 41 4c 4c 59 20 44 45  BLE INITIALLY DE
96e0: 46 45 52 52 45 44 0a 20 20 20 20 29 3b 0a 0a 20  FERRED.    );.. 
96f0: 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 70     INSERT INTO p
9700: 61 72 65 6e 74 20 56 41 4c 55 45 53 28 27 6b 65  arent VALUES('ke
9710: 79 31 27 29 3b 0a 20 20 20 20 49 4e 53 45 52 54  y1');.    INSERT
9720: 20 49 4e 54 4f 20 70 61 72 65 6e 74 20 56 41 4c   INTO parent VAL
9730: 55 45 53 28 27 6b 65 79 32 27 29 3b 0a 20 20 20  UES('key2');.   
9740: 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 63 68 69   INSERT INTO chi
9750: 6c 64 31 20 56 41 4c 55 45 53 28 27 6b 65 79 31  ld1 VALUES('key1
9760: 27 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49  ');.    INSERT I
9770: 4e 54 4f 20 63 68 69 6c 64 32 20 56 41 4c 55 45  NTO child2 VALUE
9780: 53 28 27 6b 65 79 32 27 29 3b 0a 20 20 20 20 42  S('key2');.    B
9790: 45 47 49 4e 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a 64  EGIN;.  }.} {}.d
97a0: 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 32 30  o_test e_fkey-20
97b0: 2e 32 20 7b 0a 20 20 63 61 74 63 68 73 71 6c 20  .2 {.  catchsql 
97c0: 7b 20 55 50 44 41 54 45 20 70 61 72 65 6e 74 20  { UPDATE parent 
97d0: 53 45 54 20 78 20 3d 20 27 6b 65 79 20 6f 6e 65  SET x = 'key one
97e0: 27 20 57 48 45 52 45 20 78 20 3d 20 27 6b 65 79  ' WHERE x = 'key
97f0: 31 27 20 7d 0a 7d 20 7b 31 20 7b 66 6f 72 65 69  1' }.} {1 {forei
9800: 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e  gn key constrain
9810: 74 20 66 61 69 6c 65 64 7d 7d 0a 64 6f 5f 74 65  t failed}}.do_te
9820: 73 74 20 65 5f 66 6b 65 79 2d 32 30 2e 33 20 7b  st e_fkey-20.3 {
9830: 0a 20 20 65 78 65 63 73 71 6c 20 7b 20 55 50 44  .  execsql { UPD
9840: 41 54 45 20 70 61 72 65 6e 74 20 53 45 54 20 78  ATE parent SET x
9850: 20 3d 20 27 6b 65 79 20 74 77 6f 27 20 57 48 45   = 'key two' WHE
9860: 52 45 20 78 20 3d 20 27 6b 65 79 32 27 20 7d 0a  RE x = 'key2' }.
9870: 7d 20 7b 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66  } {}.do_test e_f
9880: 6b 65 79 2d 32 30 2e 34 20 7b 0a 20 20 63 61 74  key-20.4 {.  cat
9890: 63 68 73 71 6c 20 43 4f 4d 4d 49 54 0a 7d 20 7b  chsql COMMIT.} {
98a0: 31 20 7b 66 6f 72 65 69 67 6e 20 6b 65 79 20 63  1 {foreign key c
98b0: 6f 6e 73 74 72 61 69 6e 74 20 66 61 69 6c 65 64  onstraint failed
98c0: 7d 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65  }}.do_test e_fke
98d0: 79 2d 32 30 2e 35 20 7b 0a 20 20 65 78 65 63 73  y-20.5 {.  execs
98e0: 71 6c 20 7b 0a 20 20 20 20 55 50 44 41 54 45 20  ql {.    UPDATE 
98f0: 63 68 69 6c 64 32 20 53 45 54 20 63 20 3d 20 27  child2 SET c = '
9900: 6b 65 79 20 74 77 6f 27 3b 0a 20 20 20 20 43 4f  key two';.    CO
9910: 4d 4d 49 54 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a 0a  MMIT;.  }.} {}..
9920: 64 72 6f 70 5f 61 6c 6c 5f 74 61 62 6c 65 73 0a  drop_all_tables.
9930: 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 32  do_test e_fkey-2
9940: 30 2e 36 20 7b 0a 20 20 65 78 65 63 73 71 6c 20  0.6 {.  execsql 
9950: 7b 0a 20 20 20 20 43 52 45 41 54 45 20 54 41 42  {.    CREATE TAB
9960: 4c 45 20 70 61 72 65 6e 74 28 78 20 50 52 49 4d  LE parent(x PRIM
9970: 41 52 59 20 4b 45 59 29 3b 0a 20 20 20 20 43 52  ARY KEY);.    CR
9980: 45 41 54 45 20 54 41 42 4c 45 20 63 68 69 6c 64  EATE TABLE child
9990: 31 28 63 20 52 45 46 45 52 45 4e 43 45 53 20 70  1(c REFERENCES p
99a0: 61 72 65 6e 74 20 4f 4e 20 44 45 4c 45 54 45 20  arent ON DELETE 
99b0: 52 45 53 54 52 49 43 54 0a 20 20 20 20 20 20 44  RESTRICT.      D
99c0: 45 46 45 52 52 41 42 4c 45 20 49 4e 49 54 49 41  EFERRABLE INITIA
99d0: 4c 4c 59 20 44 45 46 45 52 52 45 44 0a 20 20 20  LLY DEFERRED.   
99e0: 20 29 3b 0a 20 20 20 20 43 52 45 41 54 45 20 54   );.    CREATE T
99f0: 41 42 4c 45 20 63 68 69 6c 64 32 28 63 20 52 45  ABLE child2(c RE
9a00: 46 45 52 45 4e 43 45 53 20 70 61 72 65 6e 74 20  FERENCES parent 
9a10: 4f 4e 20 44 45 4c 45 54 45 20 4e 4f 20 41 43 54  ON DELETE NO ACT
9a20: 49 4f 4e 0a 20 20 20 20 20 20 44 45 46 45 52 52  ION.      DEFERR
9a30: 41 42 4c 45 20 49 4e 49 54 49 41 4c 4c 59 20 44  ABLE INITIALLY D
9a40: 45 46 45 52 52 45 44 0a 20 20 20 20 29 3b 0a 0a  EFERRED.    );..
9a50: 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20      INSERT INTO 
9a60: 70 61 72 65 6e 74 20 56 41 4c 55 45 53 28 27 6b  parent VALUES('k
9a70: 65 79 31 27 29 3b 0a 20 20 20 20 49 4e 53 45 52  ey1');.    INSER
9a80: 54 20 49 4e 54 4f 20 70 61 72 65 6e 74 20 56 41  T INTO parent VA
9a90: 4c 55 45 53 28 27 6b 65 79 32 27 29 3b 0a 20 20  LUES('key2');.  
9aa0: 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 63 68    INSERT INTO ch
9ab0: 69 6c 64 31 20 56 41 4c 55 45 53 28 27 6b 65 79  ild1 VALUES('key
9ac0: 31 27 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20  1');.    INSERT 
9ad0: 49 4e 54 4f 20 63 68 69 6c 64 32 20 56 41 4c 55  INTO child2 VALU
9ae0: 45 53 28 27 6b 65 79 32 27 29 3b 0a 20 20 20 20  ES('key2');.    
9af0: 42 45 47 49 4e 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a  BEGIN;.  }.} {}.
9b00: 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 32  do_test e_fkey-2
9b10: 30 2e 37 20 7b 0a 20 20 63 61 74 63 68 73 71 6c  0.7 {.  catchsql
9b20: 20 7b 20 44 45 4c 45 54 45 20 46 52 4f 4d 20 70   { DELETE FROM p
9b30: 61 72 65 6e 74 20 57 48 45 52 45 20 78 20 3d 20  arent WHERE x = 
9b40: 27 6b 65 79 31 27 20 7d 0a 7d 20 7b 31 20 7b 66  'key1' }.} {1 {f
9b50: 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74  oreign key const
9b60: 72 61 69 6e 74 20 66 61 69 6c 65 64 7d 7d 0a 64  raint failed}}.d
9b70: 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 32 30  o_test e_fkey-20
9b80: 2e 38 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b  .8 {.  execsql {
9b90: 20 44 45 4c 45 54 45 20 46 52 4f 4d 20 70 61 72   DELETE FROM par
9ba0: 65 6e 74 20 57 48 45 52 45 20 78 20 3d 20 27 6b  ent WHERE x = 'k
9bb0: 65 79 32 27 20 7d 0a 7d 20 7b 7d 0a 64 6f 5f 74  ey2' }.} {}.do_t
9bc0: 65 73 74 20 65 5f 66 6b 65 79 2d 32 30 2e 39 20  est e_fkey-20.9 
9bd0: 7b 0a 20 20 63 61 74 63 68 73 71 6c 20 43 4f 4d  {.  catchsql COM
9be0: 4d 49 54 0a 7d 20 7b 31 20 7b 66 6f 72 65 69 67  MIT.} {1 {foreig
9bf0: 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74  n key constraint
9c00: 20 66 61 69 6c 65 64 7d 7d 0a 64 6f 5f 74 65 73   failed}}.do_tes
9c10: 74 20 65 5f 66 6b 65 79 2d 32 30 2e 31 30 20 7b  t e_fkey-20.10 {
9c20: 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20  .  execsql {.   
9c30: 20 55 50 44 41 54 45 20 63 68 69 6c 64 32 20 53   UPDATE child2 S
9c40: 45 54 20 63 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20  ET c = NULL;.   
9c50: 20 43 4f 4d 4d 49 54 3b 0a 20 20 7d 0a 7d 20 7b   COMMIT;.  }.} {
9c60: 7d 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  }..#------------
9c70: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
9c80: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
9c90: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
9ca0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20  -------------.# 
9cb0: 2f 2a 20 45 56 3a 20 52 2d 30 33 33 35 33 2d 30  /* EV: R-03353-0
9cc0: 35 33 32 37 20 2a 2f 0a 23 0a 23 20 54 65 73 74  5327 */.#.# Test
9cd0: 20 53 45 54 20 4e 55 4c 4c 20 61 63 74 69 6f 6e   SET NULL action
9ce0: 73 2e 0a 23 0a 64 72 6f 70 5f 61 6c 6c 5f 74 61  s..#.drop_all_ta
9cf0: 62 6c 65 73 0a 64 6f 5f 74 65 73 74 20 65 5f 66  bles.do_test e_f
9d00: 6b 65 79 2d 32 31 2e 31 20 7b 0a 20 20 65 78 65  key-21.1 {.  exe
9d10: 63 73 71 6c 20 7b 0a 20 20 20 20 43 52 45 41 54  csql {.    CREAT
9d20: 45 20 54 41 42 4c 45 20 70 41 28 78 20 50 52 49  E TABLE pA(x PRI
9d30: 4d 41 52 59 20 4b 45 59 29 3b 0a 20 20 20 20 43  MARY KEY);.    C
9d40: 52 45 41 54 45 20 54 41 42 4c 45 20 63 41 28 63  REATE TABLE cA(c
9d50: 20 52 45 46 45 52 45 4e 43 45 53 20 70 41 20 4f   REFERENCES pA O
9d60: 4e 20 44 45 4c 45 54 45 20 53 45 54 20 4e 55 4c  N DELETE SET NUL
9d70: 4c 29 3b 0a 20 20 20 20 43 52 45 41 54 45 20 54  L);.    CREATE T
9d80: 41 42 4c 45 20 63 42 28 63 20 52 45 46 45 52 45  ABLE cB(c REFERE
9d90: 4e 43 45 53 20 70 41 20 4f 4e 20 55 50 44 41 54  NCES pA ON UPDAT
9da0: 45 20 53 45 54 20 4e 55 4c 4c 29 3b 0a 0a 20 20  E SET NULL);..  
9db0: 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 70 41    INSERT INTO pA
9dc0: 20 56 41 4c 55 45 53 28 58 27 41 42 43 44 27 29   VALUES(X'ABCD')
9dd0: 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54  ;.    INSERT INT
9de0: 4f 20 70 41 20 56 41 4c 55 45 53 28 58 27 31 32  O pA VALUES(X'12
9df0: 33 34 27 29 3b 0a 20 20 20 20 49 4e 53 45 52 54  34');.    INSERT
9e00: 20 49 4e 54 4f 20 63 41 20 56 41 4c 55 45 53 28   INTO cA VALUES(
9e10: 58 27 41 42 43 44 27 29 3b 0a 20 20 20 20 49 4e  X'ABCD');.    IN
9e20: 53 45 52 54 20 49 4e 54 4f 20 63 42 20 56 41 4c  SERT INTO cB VAL
9e30: 55 45 53 28 58 27 31 32 33 34 27 29 3b 0a 20 20  UES(X'1234');.  
9e40: 7d 0a 7d 20 7b 7d 0a 64 6f 5f 74 65 73 74 20 65  }.} {}.do_test e
9e50: 5f 66 6b 65 79 2d 32 31 2e 32 20 7b 0a 20 20 65  _fkey-21.2 {.  e
9e60: 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 44 45 4c  xecsql {.    DEL
9e70: 45 54 45 20 46 52 4f 4d 20 70 41 20 57 48 45 52  ETE FROM pA WHER
9e80: 45 20 72 6f 77 69 64 20 3d 20 31 3b 0a 20 20 20  E rowid = 1;.   
9e90: 20 53 45 4c 45 43 54 20 71 75 6f 74 65 28 78 29   SELECT quote(x)
9ea0: 20 46 52 4f 4d 20 70 41 3b 0a 20 20 7d 0a 7d 20   FROM pA;.  }.} 
9eb0: 7b 58 27 31 32 33 34 27 7d 0a 64 6f 5f 74 65 73  {X'1234'}.do_tes
9ec0: 74 20 65 5f 66 6b 65 79 2d 32 31 2e 33 20 7b 0a  t e_fkey-21.3 {.
9ed0: 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20    execsql {.    
9ee0: 53 45 4c 45 43 54 20 71 75 6f 74 65 28 63 29 20  SELECT quote(c) 
9ef0: 46 52 4f 4d 20 63 41 3b 0a 20 20 7d 0a 7d 20 7b  FROM cA;.  }.} {
9f00: 4e 55 4c 4c 7d 0a 64 6f 5f 74 65 73 74 20 65 5f  NULL}.do_test e_
9f10: 66 6b 65 79 2d 32 31 2e 34 20 7b 0a 20 20 65 78  fkey-21.4 {.  ex
9f20: 65 63 73 71 6c 20 7b 0a 20 20 20 20 55 50 44 41  ecsql {.    UPDA
9f30: 54 45 20 70 41 20 53 45 54 20 78 20 3d 20 58 27  TE pA SET x = X'
9f40: 38 37 36 35 27 20 57 48 45 52 45 20 72 6f 77 69  8765' WHERE rowi
9f50: 64 20 3d 20 32 3b 0a 20 20 20 20 53 45 4c 45 43  d = 2;.    SELEC
9f60: 54 20 71 75 6f 74 65 28 78 29 20 46 52 4f 4d 20  T quote(x) FROM 
9f70: 70 41 3b 0a 20 20 7d 0a 7d 20 7b 58 27 38 37 36  pA;.  }.} {X'876
9f80: 35 27 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b  5'}.do_test e_fk
9f90: 65 79 2d 32 31 2e 35 20 7b 0a 20 20 65 78 65 63  ey-21.5 {.  exec
9fa0: 73 71 6c 20 7b 20 53 45 4c 45 43 54 20 71 75 6f  sql { SELECT quo
9fb0: 74 65 28 63 29 20 46 52 4f 4d 20 63 42 20 7d 0a  te(c) FROM cB }.
9fc0: 7d 20 7b 4e 55 4c 4c 7d 0a 0a 23 2d 2d 2d 2d 2d  } {NULL}..#-----
9fd0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
9fe0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
9ff0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
a000: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
a010: 2d 2d 2d 2d 0a 23 20 2f 2a 20 45 56 3a 20 52 2d  ----.# /* EV: R-
a020: 34 33 30 35 34 2d 35 34 38 33 32 20 2a 2f 0a 23  43054-54832 */.#
a030: 0a 23 20 54 65 73 74 20 53 45 54 20 44 45 46 41  .# Test SET DEFA
a040: 55 4c 54 20 61 63 74 69 6f 6e 73 2e 0a 23 0a 64  ULT actions..#.d
a050: 72 6f 70 5f 61 6c 6c 5f 74 61 62 6c 65 73 0a 64  rop_all_tables.d
a060: 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 32 32  o_test e_fkey-22
a070: 2e 31 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b  .1 {.  execsql {
a080: 0a 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c  .    CREATE TABL
a090: 45 20 70 41 28 78 20 50 52 49 4d 41 52 59 20 4b  E pA(x PRIMARY K
a0a0: 45 59 29 3b 0a 20 20 20 20 43 52 45 41 54 45 20  EY);.    CREATE 
a0b0: 54 41 42 4c 45 20 63 41 28 63 20 44 45 46 41 55  TABLE cA(c DEFAU
a0c0: 4c 54 20 58 27 30 30 30 30 27 20 52 45 46 45 52  LT X'0000' REFER
a0d0: 45 4e 43 45 53 20 70 41 20 4f 4e 20 44 45 4c 45  ENCES pA ON DELE
a0e0: 54 45 20 53 45 54 20 44 45 46 41 55 4c 54 29 3b  TE SET DEFAULT);
a0f0: 0a 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c  .    CREATE TABL
a100: 45 20 63 42 28 63 20 44 45 46 41 55 4c 54 20 58  E cB(c DEFAULT X
a110: 27 39 39 39 39 27 20 52 45 46 45 52 45 4e 43 45  '9999' REFERENCE
a120: 53 20 70 41 20 4f 4e 20 55 50 44 41 54 45 20 53  S pA ON UPDATE S
a130: 45 54 20 44 45 46 41 55 4c 54 29 3b 0a 0a 20 20  ET DEFAULT);..  
a140: 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 70 41    INSERT INTO pA
a150: 28 72 6f 77 69 64 2c 20 78 29 20 56 41 4c 55 45  (rowid, x) VALUE
a160: 53 28 31 2c 20 58 27 30 30 30 30 27 29 3b 0a 20  S(1, X'0000');. 
a170: 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 70     INSERT INTO p
a180: 41 28 72 6f 77 69 64 2c 20 78 29 20 56 41 4c 55  A(rowid, x) VALU
a190: 45 53 28 32 2c 20 58 27 39 39 39 39 27 29 3b 0a  ES(2, X'9999');.
a1a0: 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20      INSERT INTO 
a1b0: 70 41 28 72 6f 77 69 64 2c 20 78 29 20 56 41 4c  pA(rowid, x) VAL
a1c0: 55 45 53 28 33 2c 20 58 27 41 42 43 44 27 29 3b  UES(3, X'ABCD');
a1d0: 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f  .    INSERT INTO
a1e0: 20 70 41 28 72 6f 77 69 64 2c 20 78 29 20 56 41   pA(rowid, x) VA
a1f0: 4c 55 45 53 28 34 2c 20 58 27 31 32 33 34 27 29  LUES(4, X'1234')
a200: 3b 0a 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e  ;..    INSERT IN
a210: 54 4f 20 63 41 20 56 41 4c 55 45 53 28 58 27 41  TO cA VALUES(X'A
a220: 42 43 44 27 29 3b 0a 20 20 20 20 49 4e 53 45 52  BCD');.    INSER
a230: 54 20 49 4e 54 4f 20 63 42 20 56 41 4c 55 45 53  T INTO cB VALUES
a240: 28 58 27 31 32 33 34 27 29 3b 0a 20 20 7d 0a 7d  (X'1234');.  }.}
a250: 20 7b 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b   {}.do_test e_fk
a260: 65 79 2d 32 32 2e 32 20 7b 0a 20 20 65 78 65 63  ey-22.2 {.  exec
a270: 73 71 6c 20 7b 0a 20 20 20 20 44 45 4c 45 54 45  sql {.    DELETE
a280: 20 46 52 4f 4d 20 70 41 20 57 48 45 52 45 20 72   FROM pA WHERE r
a290: 6f 77 69 64 20 3d 20 33 3b 0a 20 20 20 20 53 45  owid = 3;.    SE
a2a0: 4c 45 43 54 20 71 75 6f 74 65 28 78 29 20 46 52  LECT quote(x) FR
a2b0: 4f 4d 20 70 41 3b 0a 20 20 7d 0a 7d 20 7b 58 27  OM pA;.  }.} {X'
a2c0: 30 30 30 30 27 20 58 27 39 39 39 39 27 20 58 27  0000' X'9999' X'
a2d0: 31 32 33 34 27 7d 0a 64 6f 5f 74 65 73 74 20 65  1234'}.do_test e
a2e0: 5f 66 6b 65 79 2d 32 32 2e 33 20 7b 0a 20 20 65  _fkey-22.3 {.  e
a2f0: 78 65 63 73 71 6c 20 7b 20 53 45 4c 45 43 54 20  xecsql { SELECT 
a300: 71 75 6f 74 65 28 63 29 20 46 52 4f 4d 20 63 41  quote(c) FROM cA
a310: 20 7d 0a 7d 20 7b 58 27 30 30 30 30 27 7d 0a 64   }.} {X'0000'}.d
a320: 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 32 32  o_test e_fkey-22
a330: 2e 34 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b  .4 {.  execsql {
a340: 0a 20 20 20 20 55 50 44 41 54 45 20 70 41 20 53  .    UPDATE pA S
a350: 45 54 20 78 20 3d 20 58 27 38 37 36 35 27 20 57  ET x = X'8765' W
a360: 48 45 52 45 20 72 6f 77 69 64 20 3d 20 34 3b 0a  HERE rowid = 4;.
a370: 20 20 20 20 53 45 4c 45 43 54 20 71 75 6f 74 65      SELECT quote
a380: 28 78 29 20 46 52 4f 4d 20 70 41 3b 0a 20 20 7d  (x) FROM pA;.  }
a390: 0a 7d 20 7b 58 27 30 30 30 30 27 20 58 27 39 39  .} {X'0000' X'99
a3a0: 39 39 27 20 58 27 38 37 36 35 27 7d 0a 64 6f 5f  99' X'8765'}.do_
a3b0: 74 65 73 74 20 65 5f 66 6b 65 79 2d 32 32 2e 35  test e_fkey-22.5
a3c0: 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 20 53   {.  execsql { S
a3d0: 45 4c 45 43 54 20 71 75 6f 74 65 28 63 29 20 46  ELECT quote(c) F
a3e0: 52 4f 4d 20 63 42 20 7d 0a 7d 20 7b 58 27 39 39  ROM cB }.} {X'99
a3f0: 39 39 27 7d 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d  99'}..#---------
a400: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
a410: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
a420: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
a430: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
a440: 0a 23 20 2f 2a 20 45 56 3a 20 52 2d 36 31 33 37  .# /* EV: R-6137
a450: 36 2d 35 37 32 36 37 20 2a 2f 0a 23 20 2f 2a 20  6-57267 */.# /* 
a460: 45 56 3a 20 52 2d 36 31 38 30 39 2d 36 32 32 30  EV: R-61809-6220
a470: 37 20 2a 2f 0a 23 0a 23 20 54 65 73 74 20 4f 4e  7 */.#.# Test ON
a480: 20 44 45 4c 45 54 45 20 43 41 53 43 41 44 45 20   DELETE CASCADE 
a490: 61 63 74 69 6f 6e 73 2e 0a 23 0a 64 72 6f 70 5f  actions..#.drop_
a4a0: 61 6c 6c 5f 74 61 62 6c 65 73 0a 64 6f 5f 74 65  all_tables.do_te
a4b0: 73 74 20 65 5f 66 6b 65 79 2d 32 33 2e 31 20 7b  st e_fkey-23.1 {
a4c0: 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20  .  execsql {.   
a4d0: 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 70 31   CREATE TABLE p1
a4e0: 28 61 2c 20 62 20 55 4e 49 51 55 45 29 3b 0a 20  (a, b UNIQUE);. 
a4f0: 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20     CREATE TABLE 
a500: 63 31 28 63 20 52 45 46 45 52 45 4e 43 45 53 20  c1(c REFERENCES 
a510: 70 31 28 62 29 20 4f 4e 20 44 45 4c 45 54 45 20  p1(b) ON DELETE 
a520: 43 41 53 43 41 44 45 2c 20 64 29 3b 0a 20 20 20  CASCADE, d);.   
a530: 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 70 31 20   INSERT INTO p1 
a540: 56 41 4c 55 45 53 28 4e 55 4c 4c 2c 20 4e 55 4c  VALUES(NULL, NUL
a550: 4c 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49  L);.    INSERT I
a560: 4e 54 4f 20 70 31 20 56 41 4c 55 45 53 28 34 2c  NTO p1 VALUES(4,
a570: 20 34 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20   4);.    INSERT 
a580: 49 4e 54 4f 20 70 31 20 56 41 4c 55 45 53 28 35  INTO p1 VALUES(5
a590: 2c 20 35 29 3b 0a 20 20 20 20 49 4e 53 45 52 54  , 5);.    INSERT
a5a0: 20 49 4e 54 4f 20 63 31 20 56 41 4c 55 45 53 28   INTO c1 VALUES(
a5b0: 4e 55 4c 4c 2c 20 4e 55 4c 4c 29 3b 0a 20 20 20  NULL, NULL);.   
a5c0: 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 63 31 20   INSERT INTO c1 
a5d0: 56 41 4c 55 45 53 28 34 2c 20 34 29 3b 0a 20 20  VALUES(4, 4);.  
a5e0: 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 63 31    INSERT INTO c1
a5f0: 20 56 41 4c 55 45 53 28 35 2c 20 35 29 3b 0a 20   VALUES(5, 5);. 
a600: 20 20 20 53 45 4c 45 43 54 20 63 6f 75 6e 74 28     SELECT count(
a610: 2a 29 20 46 52 4f 4d 20 63 31 3b 0a 20 20 7d 0a  *) FROM c1;.  }.
a620: 7d 20 7b 33 7d 0a 64 6f 5f 74 65 73 74 20 65 5f  } {3}.do_test e_
a630: 66 6b 65 79 2d 32 33 2e 32 20 7b 0a 20 20 65 78  fkey-23.2 {.  ex
a640: 65 63 73 71 6c 20 7b 0a 20 20 20 20 44 45 4c 45  ecsql {.    DELE
a650: 54 45 20 46 52 4f 4d 20 70 31 20 57 48 45 52 45  TE FROM p1 WHERE
a660: 20 61 20 3d 20 34 3b 0a 20 20 20 20 53 45 4c 45   a = 4;.    SELE
a670: 43 54 20 64 2c 20 63 20 46 52 4f 4d 20 63 31 3b  CT d, c FROM c1;
a680: 0a 20 20 7d 0a 7d 20 7b 7b 7d 20 7b 7d 20 35 20  .  }.} {{} {} 5 
a690: 35 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65  5}.do_test e_fke
a6a0: 79 2d 32 33 2e 33 20 7b 0a 20 20 65 78 65 63 73  y-23.3 {.  execs
a6b0: 71 6c 20 7b 0a 20 20 20 20 44 45 4c 45 54 45 20  ql {.    DELETE 
a6c0: 46 52 4f 4d 20 70 31 3b 0a 20 20 20 20 53 45 4c  FROM p1;.    SEL
a6d0: 45 43 54 20 64 2c 20 63 20 46 52 4f 4d 20 63 31  ECT d, c FROM c1
a6e0: 3b 0a 20 20 7d 0a 7d 20 7b 7b 7d 20 7b 7d 7d 0a  ;.  }.} {{} {}}.
a6f0: 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 32  do_test e_fkey-2
a700: 33 2e 34 20 7b 0a 20 20 65 78 65 63 73 71 6c 20  3.4 {.  execsql 
a710: 7b 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20  { SELECT * FROM 
a720: 70 31 20 7d 0a 7d 20 7b 7d 0a 0a 0a 23 2d 2d 2d  p1 }.} {}...#---
a730: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
a740: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
a750: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
a760: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
a770: 2d 2d 2d 2d 2d 2d 0a 23 20 2f 2a 20 45 56 3a 20  ------.# /* EV: 
a780: 52 2d 36 31 33 37 36 2d 35 37 32 36 37 20 2a 2f  R-61376-57267 */
a790: 0a 23 20 2f 2a 20 45 56 3a 20 52 2d 31 33 38 37  .# /* EV: R-1387
a7a0: 37 2d 36 34 35 34 32 20 2a 2f 0a 23 0a 23 20 54  7-64542 */.#.# T
a7b0: 65 73 74 20 4f 4e 20 55 50 44 41 54 45 20 43 41  est ON UPDATE CA
a7c0: 53 43 41 44 45 20 61 63 74 69 6f 6e 73 2e 0a 23  SCADE actions..#
a7d0: 0a 64 72 6f 70 5f 61 6c 6c 5f 74 61 62 6c 65 73  .drop_all_tables
a7e0: 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d  .do_test e_fkey-
a7f0: 32 34 2e 31 20 7b 0a 20 20 65 78 65 63 73 71 6c  24.1 {.  execsql
a800: 20 7b 0a 20 20 20 20 43 52 45 41 54 45 20 54 41   {.    CREATE TA
a810: 42 4c 45 20 70 31 28 61 2c 20 62 20 55 4e 49 51  BLE p1(a, b UNIQ
a820: 55 45 29 3b 0a 20 20 20 20 43 52 45 41 54 45 20  UE);.    CREATE 
a830: 54 41 42 4c 45 20 63 31 28 63 20 52 45 46 45 52  TABLE c1(c REFER
a840: 45 4e 43 45 53 20 70 31 28 62 29 20 4f 4e 20 55  ENCES p1(b) ON U
a850: 50 44 41 54 45 20 43 41 53 43 41 44 45 2c 20 64  PDATE CASCADE, d
a860: 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e  );.    INSERT IN
a870: 54 4f 20 70 31 20 56 41 4c 55 45 53 28 4e 55 4c  TO p1 VALUES(NUL
a880: 4c 2c 20 4e 55 4c 4c 29 3b 0a 20 20 20 20 49 4e  L, NULL);.    IN
a890: 53 45 52 54 20 49 4e 54 4f 20 70 31 20 56 41 4c  SERT INTO p1 VAL
a8a0: 55 45 53 28 34 2c 20 34 29 3b 0a 20 20 20 20 49  UES(4, 4);.    I
a8b0: 4e 53 45 52 54 20 49 4e 54 4f 20 70 31 20 56 41  NSERT INTO p1 VA
a8c0: 4c 55 45 53 28 35 2c 20 35 29 3b 0a 20 20 20 20  LUES(5, 5);.    
a8d0: 49 4e 53 45 52 54 20 49 4e 54 4f 20 63 31 20 56  INSERT INTO c1 V
a8e0: 41 4c 55 45 53 28 4e 55 4c 4c 2c 20 4e 55 4c 4c  ALUES(NULL, NULL
a8f0: 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e  );.    INSERT IN
a900: 54 4f 20 63 31 20 56 41 4c 55 45 53 28 34 2c 20  TO c1 VALUES(4, 
a910: 34 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49  4);.    INSERT I
a920: 4e 54 4f 20 63 31 20 56 41 4c 55 45 53 28 35 2c  NTO c1 VALUES(5,
a930: 20 35 29 3b 0a 20 20 20 20 53 45 4c 45 43 54 20   5);.    SELECT 
a940: 63 6f 75 6e 74 28 2a 29 20 46 52 4f 4d 20 63 31  count(*) FROM c1
a950: 3b 0a 20 20 7d 0a 7d 20 7b 33 7d 0a 64 6f 5f 74  ;.  }.} {3}.do_t
a960: 65 73 74 20 65 5f 66 6b 65 79 2d 32 34 2e 32 20  est e_fkey-24.2 
a970: 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20  {.  execsql {.  
a980: 20 20 55 50 44 41 54 45 20 70 31 20 53 45 54 20    UPDATE p1 SET 
a990: 62 20 3d 20 31 30 20 57 48 45 52 45 20 62 20 3d  b = 10 WHERE b =
a9a0: 20 35 3b 0a 20 20 20 20 53 45 4c 45 43 54 20 64   5;.    SELECT d
a9b0: 2c 20 63 20 46 52 4f 4d 20 63 31 3b 0a 20 20 7d  , c FROM c1;.  }
a9c0: 0a 7d 20 7b 7b 7d 20 7b 7d 20 34 20 34 20 35 20  .} {{} {} 4 4 5 
a9d0: 31 30 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b  10}.do_test e_fk
a9e0: 65 79 2d 32 34 2e 33 20 7b 0a 20 20 65 78 65 63  ey-24.3 {.  exec
a9f0: 73 71 6c 20 7b 0a 20 20 20 20 55 50 44 41 54 45  sql {.    UPDATE
aa00: 20 70 31 20 53 45 54 20 62 20 3d 20 31 31 20 57   p1 SET b = 11 W
aa10: 48 45 52 45 20 62 20 3d 20 34 3b 0a 20 20 20 20  HERE b = 4;.    
aa20: 53 45 4c 45 43 54 20 64 2c 20 63 20 46 52 4f 4d  SELECT d, c FROM
aa30: 20 63 31 3b 0a 20 20 7d 0a 7d 20 7b 7b 7d 20 7b   c1;.  }.} {{} {
aa40: 7d 20 34 20 31 31 20 35 20 31 30 7d 0a 64 6f 5f  } 4 11 5 10}.do_
aa50: 74 65 73 74 20 65 5f 66 6b 65 79 2d 32 34 2e 34  test e_fkey-24.4
aa60: 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 20 0a   {.  execsql { .
aa70: 20 20 20 20 55 50 44 41 54 45 20 70 31 20 53 45      UPDATE p1 SE
aa80: 54 20 62 20 3d 20 36 20 57 48 45 52 45 20 62 20  T b = 6 WHERE b 
aa90: 49 53 20 4e 55 4c 4c 3b 0a 20 20 20 20 53 45 4c  IS NULL;.    SEL
aaa0: 45 43 54 20 64 2c 20 63 20 46 52 4f 4d 20 63 31  ECT d, c FROM c1
aab0: 3b 0a 20 20 7d 0a 7d 20 7b 7b 7d 20 7b 7d 20 34  ;.  }.} {{} {} 4
aac0: 20 31 31 20 35 20 31 30 7d 0a 64 6f 5f 74 65 73   11 5 10}.do_tes
aad0: 74 20 65 5f 66 6b 65 79 2d 32 33 2e 35 20 7b 0a  t e_fkey-23.5 {.
aae0: 20 20 65 78 65 63 73 71 6c 20 7b 20 53 45 4c 45    execsql { SELE
aaf0: 43 54 20 2a 20 46 52 4f 4d 20 70 31 20 7d 0a 7d  CT * FROM p1 }.}
ab00: 20 7b 7b 7d 20 36 20 34 20 31 31 20 35 20 31 30   {{} 6 4 11 5 10
ab10: 7d 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  }..#------------
ab20: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 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 0a 23 20  -------------.# 
ab60: 2f 2a 20 45 56 3a 20 52 2d 35 31 33 32 39 2d 33  /* EV: R-51329-3
ab70: 33 34 33 38 20 2a 2f 0a 23 0a 23 20 54 65 73 74  3438 */.#.# Test
ab80: 20 61 6e 20 65 78 61 6d 70 6c 65 20 66 72 6f 6d   an example from
ab90: 20 74 68 65 20 22 4f 4e 20 44 45 4c 45 54 45 20   the "ON DELETE 
aba0: 61 6e 64 20 4f 4e 20 55 50 44 41 54 45 20 41 63  and ON UPDATE Ac
abb0: 74 69 6f 6e 73 22 20 73 65 63 74 69 6f 6e 20 0a  tions" section .
abc0: 23 20 6f 66 20 66 6f 72 65 69 67 6e 6b 65 79 73  # of foreignkeys
abd0: 2e 68 74 6d 6c 2e 0a 23 0a 64 72 6f 70 5f 61 6c  .html..#.drop_al
abe0: 6c 5f 74 61 62 6c 65 73 0a 64 6f 5f 74 65 73 74  l_tables.do_test
abf0: 20 65 5f 66 6b 65 79 2d 31 35 2e 31 20 7b 0a 20   e_fkey-15.1 {. 
ac00: 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 43   execsql {.    C
ac10: 52 45 41 54 45 20 54 41 42 4c 45 20 61 72 74 69  REATE TABLE arti
ac20: 73 74 28 0a 20 20 20 20 20 20 61 72 74 69 73 74  st(.      artist
ac30: 69 64 20 20 20 20 49 4e 54 45 47 45 52 20 50 52  id    INTEGER PR
ac40: 49 4d 41 52 59 20 4b 45 59 2c 20 0a 20 20 20 20  IMARY KEY, .    
ac50: 20 20 61 72 74 69 73 74 6e 61 6d 65 20 20 54 45    artistname  TE
ac60: 58 54 0a 20 20 20 20 29 3b 0a 20 20 20 20 43 52  XT.    );.    CR
ac70: 45 41 54 45 20 54 41 42 4c 45 20 74 72 61 63 6b  EATE TABLE track
ac80: 28 0a 20 20 20 20 20 20 74 72 61 63 6b 69 64 20  (.      trackid 
ac90: 20 20 20 20 49 4e 54 45 47 45 52 2c 0a 20 20 20      INTEGER,.   
aca0: 20 20 20 74 72 61 63 6b 6e 61 6d 65 20 20 20 54     trackname   T
acb0: 45 58 54 2c 20 0a 20 20 20 20 20 20 74 72 61 63  EXT, .      trac
acc0: 6b 61 72 74 69 73 74 20 49 4e 54 45 47 45 52 20  kartist INTEGER 
acd0: 52 45 46 45 52 45 4e 43 45 53 20 61 72 74 69 73  REFERENCES artis
ace0: 74 28 61 72 74 69 73 74 69 64 29 20 4f 4e 20 55  t(artistid) ON U
acf0: 50 44 41 54 45 20 43 41 53 43 41 44 45 0a 20 20  PDATE CASCADE.  
ad00: 20 20 29 3b 0a 0a 20 20 20 20 49 4e 53 45 52 54    );..    INSERT
ad10: 20 49 4e 54 4f 20 61 72 74 69 73 74 20 56 41 4c   INTO artist VAL
ad20: 55 45 53 28 31 2c 20 27 44 65 61 6e 20 4d 61 72  UES(1, 'Dean Mar
ad30: 74 69 6e 27 29 3b 0a 20 20 20 20 49 4e 53 45 52  tin');.    INSER
ad40: 54 20 49 4e 54 4f 20 61 72 74 69 73 74 20 56 41  T INTO artist VA
ad50: 4c 55 45 53 28 32 2c 20 27 46 72 61 6e 6b 20 53  LUES(2, 'Frank S
ad60: 69 6e 61 74 72 61 27 29 3b 0a 20 20 20 20 49 4e  inatra');.    IN
ad70: 53 45 52 54 20 49 4e 54 4f 20 74 72 61 63 6b 20  SERT INTO track 
ad80: 56 41 4c 55 45 53 28 31 31 2c 20 27 54 68 61 74  VALUES(11, 'That
ad90: 27 27 73 20 41 6d 6f 72 65 27 2c 20 31 29 3b 0a  ''s Amore', 1);.
ada0: 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20      INSERT INTO 
adb0: 74 72 61 63 6b 20 56 41 4c 55 45 53 28 31 32 2c  track VALUES(12,
adc0: 20 27 43 68 72 69 73 74 6d 61 73 20 42 6c 75 65   'Christmas Blue
add0: 73 27 2c 20 31 29 3b 0a 20 20 20 20 49 4e 53 45  s', 1);.    INSE
ade0: 52 54 20 49 4e 54 4f 20 74 72 61 63 6b 20 56 41  RT INTO track VA
adf0: 4c 55 45 53 28 31 33 2c 20 27 4d 79 20 57 61 79  LUES(13, 'My Way
ae00: 27 2c 20 32 29 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a  ', 2);.  }.} {}.
ae10: 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 31  do_test e_fkey-1
ae20: 35 2e 32 20 7b 0a 20 20 65 78 65 63 73 71 6c 20  5.2 {.  execsql 
ae30: 7b 0a 20 20 20 20 55 50 44 41 54 45 20 61 72 74  {.    UPDATE art
ae40: 69 73 74 20 53 45 54 20 61 72 74 69 73 74 69 64  ist SET artistid
ae50: 20 3d 20 31 30 30 20 57 48 45 52 45 20 61 72 74   = 100 WHERE art
ae60: 69 73 74 6e 61 6d 65 20 3d 20 27 44 65 61 6e 20  istname = 'Dean 
ae70: 4d 61 72 74 69 6e 27 3b 0a 20 20 7d 0a 7d 20 7b  Martin';.  }.} {
ae80: 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79  }.do_test e_fkey
ae90: 2d 31 35 2e 33 20 7b 0a 20 20 65 78 65 63 73 71  -15.3 {.  execsq
aea0: 6c 20 7b 20 53 45 4c 45 43 54 20 2a 20 46 52 4f  l { SELECT * FRO
aeb0: 4d 20 61 72 74 69 73 74 20 7d 0a 7d 20 7b 32 20  M artist }.} {2 
aec0: 7b 46 72 61 6e 6b 20 53 69 6e 61 74 72 61 7d 20  {Frank Sinatra} 
aed0: 31 30 30 20 7b 44 65 61 6e 20 4d 61 72 74 69 6e  100 {Dean Martin
aee0: 7d 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65  }}.do_test e_fke
aef0: 79 2d 31 35 2e 34 20 7b 0a 20 20 65 78 65 63 73  y-15.4 {.  execs
af00: 71 6c 20 7b 20 53 45 4c 45 43 54 20 2a 20 46 52  ql { SELECT * FR
af10: 4f 4d 20 74 72 61 63 6b 20 7d 0a 7d 20 7b 31 31  OM track }.} {11
af20: 20 7b 54 68 61 74 27 73 20 41 6d 6f 72 65 7d 20   {That's Amore} 
af30: 31 30 30 20 31 32 20 7b 43 68 72 69 73 74 6d 61  100 12 {Christma
af40: 73 20 42 6c 75 65 73 7d 20 31 30 30 20 31 33 20  s Blues} 100 13 
af50: 7b 4d 79 20 57 61 79 7d 20 32 7d 0a 0a 0a 23 2d  {My Way} 2}...#-
af60: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
af70: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
af80: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
af90: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
afa0: 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20 2f 2a 20 45 56  --------.# /* EV
afb0: 3a 20 52 2d 35 33 39 36 38 2d 35 31 36 34 32 20  : R-53968-51642 
afc0: 2a 2f 0a 23 0a 23 20 56 65 72 69 66 79 20 74 68  */.#.# Verify th
afd0: 61 74 20 61 64 64 69 6e 67 20 61 6e 20 46 4b 20  at adding an FK 
afe0: 61 63 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20  action does not 
aff0: 61 62 73 6f 6c 76 65 20 74 68 65 20 75 73 65 72  absolve the user
b000: 20 6f 66 20 74 68 65 20 0a 23 20 72 65 71 75 69   of the .# requi
b010: 72 65 6d 65 6e 74 20 6e 6f 74 20 74 6f 20 76 69  rement not to vi
b020: 6f 6c 61 74 65 20 74 68 65 20 66 6f 72 65 69 67  olate the foreig
b030: 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74  n key constraint
b040: 2e 0a 23 0a 64 72 6f 70 5f 61 6c 6c 5f 74 61 62  ..#.drop_all_tab
b050: 6c 65 73 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b  les.do_test e_fk
b060: 65 79 2d 32 35 2e 31 20 7b 0a 20 20 65 78 65 63  ey-25.1 {.  exec
b070: 73 71 6c 20 7b 0a 20 20 20 20 43 52 45 41 54 45  sql {.    CREATE
b080: 20 54 41 42 4c 45 20 70 61 72 65 6e 74 28 61 20   TABLE parent(a 
b090: 43 4f 4c 4c 41 54 45 20 6e 6f 63 61 73 65 2c 20  COLLATE nocase, 
b0a0: 62 2c 20 63 2c 20 50 52 49 4d 41 52 59 20 4b 45  b, c, PRIMARY KE
b0b0: 59 28 63 2c 20 61 29 29 3b 0a 20 20 20 20 43 52  Y(c, a));.    CR
b0c0: 45 41 54 45 20 54 41 42 4c 45 20 63 68 69 6c 64  EATE TABLE child
b0d0: 28 64 20 44 45 46 41 55 4c 54 20 27 61 27 2c 20  (d DEFAULT 'a', 
b0e0: 65 2c 20 66 20 44 45 46 41 55 4c 54 20 27 63 27  e, f DEFAULT 'c'
b0f0: 2c 0a 20 20 20 20 20 20 46 4f 52 45 49 47 4e 20  ,.      FOREIGN 
b100: 4b 45 59 28 66 2c 20 64 29 20 52 45 46 45 52 45  KEY(f, d) REFERE
b110: 4e 43 45 53 20 70 61 72 65 6e 74 20 4f 4e 20 55  NCES parent ON U
b120: 50 44 41 54 45 20 53 45 54 20 44 45 46 41 55 4c  PDATE SET DEFAUL
b130: 54 0a 20 20 20 20 29 3b 0a 0a 20 20 20 20 49 4e  T.    );..    IN
b140: 53 45 52 54 20 49 4e 54 4f 20 70 61 72 65 6e 74  SERT INTO parent
b150: 20 56 41 4c 55 45 53 28 27 41 27 2c 20 27 62 27   VALUES('A', 'b'
b160: 2c 20 27 63 27 29 3b 0a 20 20 20 20 49 4e 53 45  , 'c');.    INSE
b170: 52 54 20 49 4e 54 4f 20 70 61 72 65 6e 74 20 56  RT INTO parent V
b180: 41 4c 55 45 53 28 27 4f 4e 45 27 2c 20 27 74 77  ALUES('ONE', 'tw
b190: 6f 27 2c 20 27 74 68 72 65 65 27 29 3b 0a 20 20  o', 'three');.  
b1a0: 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 63 68    INSERT INTO ch
b1b0: 69 6c 64 20 56 41 4c 55 45 53 28 27 6f 6e 65 27  ild VALUES('one'
b1c0: 2c 20 27 74 77 6f 27 2c 20 27 74 68 72 65 65 27  , 'two', 'three'
b1d0: 29 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a 64 6f 5f 74  );.  }.} {}.do_t
b1e0: 65 73 74 20 65 5f 66 6b 65 79 2d 32 35 2e 32 20  est e_fkey-25.2 
b1f0: 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20  {.  execsql {.  
b200: 20 20 42 45 47 49 4e 3b 0a 20 20 20 20 20 20 55    BEGIN;.      U
b210: 50 44 41 54 45 20 70 61 72 65 6e 74 20 53 45 54  PDATE parent SET
b220: 20 61 20 3d 20 27 27 20 57 48 45 52 45 20 61 20   a = '' WHERE a 
b230: 3d 20 27 6f 4e 65 27 3b 0a 20 20 20 20 20 20 53  = 'oNe';.      S
b240: 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 63 68 69  ELECT * FROM chi
b250: 6c 64 3b 0a 20 20 7d 0a 7d 20 7b 61 20 74 77 6f  ld;.  }.} {a two
b260: 20 63 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b   c}.do_test e_fk
b270: 65 79 2d 32 35 2e 33 20 7b 0a 20 20 65 78 65 63  ey-25.3 {.  exec
b280: 73 71 6c 20 7b 0a 20 20 20 20 52 4f 4c 4c 42 41  sql {.    ROLLBA
b290: 43 4b 3b 0a 20 20 20 20 44 45 4c 45 54 45 20 46  CK;.    DELETE F
b2a0: 52 4f 4d 20 70 61 72 65 6e 74 20 57 48 45 52 45  ROM parent WHERE
b2b0: 20 61 20 3d 20 27 41 27 3b 0a 20 20 20 20 53 45   a = 'A';.    SE
b2c0: 4c 45 43 54 20 2a 20 46 52 4f 4d 20 70 61 72 65  LECT * FROM pare
b2d0: 6e 74 3b 0a 20 20 7d 0a 7d 20 7b 4f 4e 45 20 74  nt;.  }.} {ONE t
b2e0: 77 6f 20 74 68 72 65 65 7d 0a 64 6f 5f 74 65 73  wo three}.do_tes
b2f0: 74 20 65 5f 66 6b 65 79 2d 32 35 2e 34 20 7b 0a  t e_fkey-25.4 {.
b300: 20 20 63 61 74 63 68 73 71 6c 20 7b 20 55 50 44    catchsql { UPD
b310: 41 54 45 20 70 61 72 65 6e 74 20 53 45 54 20 61  ATE parent SET a
b320: 20 3d 20 27 27 20 57 48 45 52 45 20 61 20 3d 20   = '' WHERE a = 
b330: 27 6f 4e 65 27 20 7d 0a 7d 20 7b 31 20 7b 66 6f  'oNe' }.} {1 {fo
b340: 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72  reign key constr
b350: 61 69 6e 74 20 66 61 69 6c 65 64 7d 7d 0a 0a 0a  aint failed}}...
b360: 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  #---------------
b370: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
b380: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
b390: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
b3a0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20 2f 2a 20  ----------.# /* 
b3b0: 45 56 3a 20 52 2d 30 37 30 36 35 2d 35 39 35 38  EV: R-07065-5958
b3c0: 38 20 2a 2f 0a 23 20 2f 2a 20 45 56 3a 20 52 2d  8 */.# /* EV: R-
b3d0: 32 38 32 32 30 2d 34 36 36 39 34 20 2a 2f 0a 23  28220-46694 */.#
b3e0: 0a 23 20 54 65 73 74 20 61 6e 20 65 78 61 6d 70  .# Test an examp
b3f0: 6c 65 20 66 72 6f 6d 20 74 68 65 20 22 4f 4e 20  le from the "ON 
b400: 44 45 4c 45 54 45 20 61 6e 64 20 4f 4e 20 55 50  DELETE and ON UP
b410: 44 41 54 45 20 41 63 74 69 6f 6e 73 22 20 73 65  DATE Actions" se
b420: 63 74 69 6f 6e 20 0a 23 20 6f 66 20 66 6f 72 65  ction .# of fore
b430: 69 67 6e 6b 65 79 73 2e 68 74 6d 6c 2e 20 54 68  ignkeys.html. Th
b440: 69 73 20 65 78 61 6d 70 6c 65 20 73 68 6f 77 73  is example shows
b450: 20 74 68 61 74 20 61 64 64 69 6e 67 20 61 6e 20   that adding an 
b460: 22 4f 4e 20 44 45 4c 45 54 45 20 44 45 46 41 55  "ON DELETE DEFAU
b470: 4c 54 22 0a 23 20 63 6c 61 75 73 65 20 64 6f 65  LT".# clause doe
b480: 73 20 6e 6f 74 20 61 62 72 6f 67 61 74 65 20 74  s not abrogate t
b490: 68 65 20 6e 65 65 64 20 74 6f 20 73 61 74 69 73  he need to satis
b4a0: 66 79 20 74 68 65 20 66 6f 72 65 69 67 6e 20 6b  fy the foreign k
b4b0: 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 0a 23 20  ey constraint.# 
b4c0: 28 52 2d 32 38 32 32 30 2d 34 36 36 39 34 29 2e  (R-28220-46694).
b4d0: 0a 23 0a 64 72 6f 70 5f 61 6c 6c 5f 74 61 62 6c  .#.drop_all_tabl
b4e0: 65 73 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65  es.do_test e_fke
b4f0: 79 2d 31 34 2e 31 20 7b 0a 20 20 65 78 65 63 73  y-14.1 {.  execs
b500: 71 6c 20 7b 0a 20 20 20 20 43 52 45 41 54 45 20  ql {.    CREATE 
b510: 54 41 42 4c 45 20 61 72 74 69 73 74 28 0a 20 20  TABLE artist(.  
b520: 20 20 20 20 61 72 74 69 73 74 69 64 20 20 20 20      artistid    
b530: 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20  INTEGER PRIMARY 
b540: 4b 45 59 2c 20 0a 20 20 20 20 20 20 61 72 74 69  KEY, .      arti
b550: 73 74 6e 61 6d 65 20 20 54 45 58 54 0a 20 20 20  stname  TEXT.   
b560: 20 29 3b 0a 20 20 20 20 43 52 45 41 54 45 20 54   );.    CREATE T
b570: 41 42 4c 45 20 74 72 61 63 6b 28 0a 20 20 20 20  ABLE track(.    
b580: 20 20 74 72 61 63 6b 69 64 20 20 20 20 20 49 4e    trackid     IN
b590: 54 45 47 45 52 2c 0a 20 20 20 20 20 20 74 72 61  TEGER,.      tra
b5a0: 63 6b 6e 61 6d 65 20 20 20 54 45 58 54 2c 20 0a  ckname   TEXT, .
b5b0: 20 20 20 20 20 20 74 72 61 63 6b 61 72 74 69 73        trackartis
b5c0: 74 20 49 4e 54 45 47 45 52 20 44 45 46 41 55 4c  t INTEGER DEFAUL
b5d0: 54 20 30 20 52 45 46 45 52 45 4e 43 45 53 20 61  T 0 REFERENCES a
b5e0: 72 74 69 73 74 28 61 72 74 69 73 74 69 64 29 20  rtist(artistid) 
b5f0: 4f 4e 20 44 45 4c 45 54 45 20 53 45 54 20 44 45  ON DELETE SET DE
b600: 46 41 55 4c 54 0a 20 20 20 20 29 3b 0a 20 20 20  FAULT.    );.   
b610: 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 61 72 74   INSERT INTO art
b620: 69 73 74 20 56 41 4c 55 45 53 28 33 2c 20 27 53  ist VALUES(3, 'S
b630: 61 6d 6d 79 20 44 61 76 69 73 20 4a 72 2e 27 29  ammy Davis Jr.')
b640: 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54  ;.    INSERT INT
b650: 4f 20 74 72 61 63 6b 20 56 41 4c 55 45 53 28 31  O track VALUES(1
b660: 34 2c 20 27 4d 72 2e 20 42 6f 6a 61 6e 67 6c 65  4, 'Mr. Bojangle
b670: 73 27 2c 20 33 29 3b 0a 20 20 7d 0a 7d 20 7b 7d  s', 3);.  }.} {}
b680: 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d  .do_test e_fkey-
b690: 31 34 2e 32 20 7b 0a 20 20 63 61 74 63 68 73 71  14.2 {.  catchsq
b6a0: 6c 20 7b 20 44 45 4c 45 54 45 20 46 52 4f 4d 20  l { DELETE FROM 
b6b0: 61 72 74 69 73 74 20 57 48 45 52 45 20 61 72 74  artist WHERE art
b6c0: 69 73 74 6e 61 6d 65 20 3d 20 27 53 61 6d 6d 79  istname = 'Sammy
b6d0: 20 44 61 76 69 73 20 4a 72 2e 27 20 7d 0a 7d 20   Davis Jr.' }.} 
b6e0: 7b 31 20 7b 66 6f 72 65 69 67 6e 20 6b 65 79 20  {1 {foreign key 
b6f0: 63 6f 6e 73 74 72 61 69 6e 74 20 66 61 69 6c 65  constraint faile
b700: 64 7d 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b  d}}.do_test e_fk
b710: 65 79 2d 31 34 2e 33 20 7b 0a 20 20 65 78 65 63  ey-14.3 {.  exec
b720: 73 71 6c 20 7b 0a 20 20 20 20 49 4e 53 45 52 54  sql {.    INSERT
b730: 20 49 4e 54 4f 20 61 72 74 69 73 74 20 56 41 4c   INTO artist VAL
b740: 55 45 53 28 30 2c 20 27 55 6e 6b 6e 6f 77 6e 20  UES(0, 'Unknown 
b750: 41 72 74 69 73 74 27 29 3b 0a 20 20 20 20 44 45  Artist');.    DE
b760: 4c 45 54 45 20 46 52 4f 4d 20 61 72 74 69 73 74  LETE FROM artist
b770: 20 57 48 45 52 45 20 61 72 74 69 73 74 6e 61 6d   WHERE artistnam
b780: 65 20 3d 20 27 53 61 6d 6d 79 20 44 61 76 69 73  e = 'Sammy Davis
b790: 20 4a 72 2e 27 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a   Jr.';.  }.} {}.
b7a0: 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 31  do_test e_fkey-1
b7b0: 34 2e 34 20 7b 0a 20 20 65 78 65 63 73 71 6c 20  4.4 {.  execsql 
b7c0: 7b 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20  { SELECT * FROM 
b7d0: 61 72 74 69 73 74 20 7d 0a 7d 20 7b 30 20 7b 55  artist }.} {0 {U
b7e0: 6e 6b 6e 6f 77 6e 20 41 72 74 69 73 74 7d 7d 0a  nknown Artist}}.
b7f0: 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 31  do_test e_fkey-1
b800: 34 2e 35 20 7b 0a 20 20 65 78 65 63 73 71 6c 20  4.5 {.  execsql 
b810: 7b 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20  { SELECT * FROM 
b820: 74 72 61 63 6b 20 7d 0a 7d 20 7b 31 34 20 7b 4d  track }.} {14 {M
b830: 72 2e 20 42 6f 6a 61 6e 67 6c 65 73 7d 20 30 7d  r. Bojangles} 0}
b840: 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ..#-------------
b850: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
b860: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
b870: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
b880: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20 2f  ------------.# /
b890: 2a 20 45 56 3a 20 52 2d 30 39 35 36 34 2d 32 32  * EV: R-09564-22
b8a0: 31 37 30 20 2a 2f 0a 23 0a 23 20 43 68 65 63 6b  170 */.#.# Check
b8b0: 20 74 68 61 74 20 74 68 65 20 6f 72 64 65 72 20   that the order 
b8c0: 6f 66 20 73 74 65 70 73 20 69 6e 20 61 6e 20 55  of steps in an U
b8d0: 50 44 41 54 45 20 6f 72 20 44 45 4c 45 54 45 20  PDATE or DELETE 
b8e0: 6f 6e 20 61 20 70 61 72 65 6e 74 20 0a 23 20 74  on a parent .# t
b8f0: 61 62 6c 65 20 69 73 20 61 73 20 66 6f 6c 6c 6f  able is as follo
b900: 77 73 3a 0a 23 0a 23 20 20 20 31 2e 20 45 78 65  ws:.#.#   1. Exe
b910: 63 75 74 65 20 61 70 70 6c 69 63 61 62 6c 65 20  cute applicable 
b920: 42 45 46 4f 52 45 20 74 72 69 67 67 65 72 20 70  BEFORE trigger p
b930: 72 6f 67 72 61 6d 73 2c 0a 23 20 20 20 32 2e 20  rograms,.#   2. 
b940: 43 68 65 63 6b 20 6c 6f 63 61 6c 20 28 6e 6f 6e  Check local (non
b950: 20 66 6f 72 65 69 67 6e 20 6b 65 79 29 20 63 6f   foreign key) co
b960: 6e 73 74 72 61 69 6e 74 73 2c 0a 23 20 20 20 33  nstraints,.#   3
b970: 2e 20 55 70 64 61 74 65 20 6f 72 20 64 65 6c 65  . Update or dele
b980: 74 65 20 74 68 65 20 72 6f 77 20 69 6e 20 74 68  te the row in th
b990: 65 20 70 61 72 65 6e 74 20 74 61 62 6c 65 2c 0a  e parent table,.
b9a0: 23 20 20 20 34 2e 20 50 65 72 66 6f 72 6d 20 61  #   4. Perform a
b9b0: 6e 79 20 72 65 71 75 69 72 65 64 20 66 6f 72 65  ny required fore
b9c0: 69 67 6e 20 6b 65 79 20 61 63 74 69 6f 6e 73 2c  ign key actions,
b9d0: 0a 23 20 20 20 35 2e 20 45 78 65 63 75 74 65 20  .#   5. Execute 
b9e0: 61 70 70 6c 69 63 61 62 6c 65 20 41 46 54 45 52  applicable AFTER
b9f0: 20 74 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d   trigger program
ba00: 73 2e 20 0a 23 0a 64 72 6f 70 5f 61 6c 6c 5f 74  s. .#.drop_all_t
ba10: 61 62 6c 65 73 0a 64 6f 5f 74 65 73 74 20 65 5f  ables.do_test e_
ba20: 66 6b 65 79 2d 32 37 2e 31 20 7b 0a 20 20 70 72  fkey-27.1 {.  pr
ba30: 6f 63 20 6d 61 78 70 61 72 65 6e 74 20 7b 61 72  oc maxparent {ar
ba40: 67 73 7d 20 7b 20 64 62 20 6f 6e 65 20 7b 53 45  gs} { db one {SE
ba50: 4c 45 43 54 20 6d 61 78 28 78 29 20 46 52 4f 4d  LECT max(x) FROM
ba60: 20 70 61 72 65 6e 74 7d 20 7d 0a 20 20 64 62 20   parent} }.  db 
ba70: 66 75 6e 63 20 6d 61 78 70 61 72 65 6e 74 20 6d  func maxparent m
ba80: 61 78 70 61 72 65 6e 74 0a 0a 20 20 65 78 65 63  axparent..  exec
ba90: 73 71 6c 20 7b 0a 20 20 20 20 43 52 45 41 54 45  sql {.    CREATE
baa0: 20 54 41 42 4c 45 20 70 61 72 65 6e 74 28 78 20   TABLE parent(x 
bab0: 50 52 49 4d 41 52 59 20 4b 45 59 29 3b 0a 0a 20  PRIMARY KEY);.. 
bac0: 20 20 20 43 52 45 41 54 45 20 54 52 49 47 47 45     CREATE TRIGGE
bad0: 52 20 62 75 20 42 45 46 4f 52 45 20 55 50 44 41  R bu BEFORE UPDA
bae0: 54 45 20 4f 4e 20 70 61 72 65 6e 74 20 42 45 47  TE ON parent BEG
baf0: 49 4e 0a 20 20 20 20 20 20 49 4e 53 45 52 54 20  IN.      INSERT 
bb00: 49 4e 54 4f 20 70 61 72 65 6e 74 20 56 41 4c 55  INTO parent VALU
bb10: 45 53 28 6e 65 77 2e 78 2d 6f 6c 64 2e 78 29 3b  ES(new.x-old.x);
bb20: 0a 20 20 20 20 45 4e 44 3b 0a 20 20 20 20 43 52  .    END;.    CR
bb30: 45 41 54 45 20 54 41 42 4c 45 20 63 68 69 6c 64  EATE TABLE child
bb40: 28 0a 20 20 20 20 20 20 61 20 44 45 46 41 55 4c  (.      a DEFAUL
bb50: 54 20 28 6d 61 78 70 61 72 65 6e 74 28 29 29 20  T (maxparent()) 
bb60: 52 45 46 45 52 45 4e 43 45 53 20 70 61 72 65 6e  REFERENCES paren
bb70: 74 20 4f 4e 20 55 50 44 41 54 45 20 53 45 54 20  t ON UPDATE SET 
bb80: 44 45 46 41 55 4c 54 0a 20 20 20 20 29 3b 0a 20  DEFAULT.    );. 
bb90: 20 20 20 43 52 45 41 54 45 20 54 52 49 47 47 45     CREATE TRIGGE
bba0: 52 20 61 75 20 41 46 54 45 52 20 55 50 44 41 54  R au AFTER UPDAT
bbb0: 45 20 4f 4e 20 70 61 72 65 6e 74 20 42 45 47 49  E ON parent BEGI
bbc0: 4e 0a 20 20 20 20 20 20 49 4e 53 45 52 54 20 49  N.      INSERT I
bbd0: 4e 54 4f 20 70 61 72 65 6e 74 20 56 41 4c 55 45  NTO parent VALUE
bbe0: 53 28 6e 65 77 2e 78 2b 6f 6c 64 2e 78 29 3b 0a  S(new.x+old.x);.
bbf0: 20 20 20 20 45 4e 44 3b 0a 0a 20 20 20 20 49 4e      END;..    IN
bc00: 53 45 52 54 20 49 4e 54 4f 20 70 61 72 65 6e 74  SERT INTO parent
bc10: 20 56 41 4c 55 45 53 28 31 29 3b 0a 20 20 20 20   VALUES(1);.    
bc20: 49 4e 53 45 52 54 20 49 4e 54 4f 20 63 68 69 6c  INSERT INTO chil
bc30: 64 20 56 41 4c 55 45 53 28 31 29 3b 0a 20 20 7d  d VALUES(1);.  }
bc40: 0a 7d 20 7b 7d 0a 64 6f 5f 74 65 73 74 20 65 5f  .} {}.do_test e_
bc50: 66 6b 65 79 2d 32 37 2e 32 20 7b 0a 20 20 65 78  fkey-27.2 {.  ex
bc60: 65 63 73 71 6c 20 7b 0a 20 20 20 20 55 50 44 41  ecsql {.    UPDA
bc70: 54 45 20 70 61 72 65 6e 74 20 53 45 54 20 78 20  TE parent SET x 
bc80: 3d 20 32 32 3b 0a 20 20 20 20 53 45 4c 45 43 54  = 22;.    SELECT
bc90: 20 2a 20 46 52 4f 4d 20 70 61 72 65 6e 74 20 55   * FROM parent U
bca0: 4e 49 4f 4e 20 41 4c 4c 20 53 45 4c 45 43 54 20  NION ALL SELECT 
bcb0: 27 78 78 78 27 20 55 4e 49 4f 4e 20 41 4c 4c 20  'xxx' UNION ALL 
bcc0: 53 45 4c 45 43 54 20 61 20 46 52 4f 4d 20 63 68  SELECT a FROM ch
bcd0: 69 6c 64 3b 0a 20 20 7d 0a 7d 20 7b 32 32 20 32  ild;.  }.} {22 2
bce0: 31 20 32 33 20 78 78 78 20 32 32 7d 0a 64 6f 5f  1 23 xxx 22}.do_
bcf0: 74 65 73 74 20 65 5f 66 6b 65 79 2d 32 37 2e 33  test e_fkey-27.3
bd00: 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20   {.  execsql {. 
bd10: 20 20 20 44 45 4c 45 54 45 20 46 52 4f 4d 20 63     DELETE FROM c
bd20: 68 69 6c 64 3b 0a 20 20 20 20 44 45 4c 45 54 45  hild;.    DELETE
bd30: 20 46 52 4f 4d 20 70 61 72 65 6e 74 3b 0a 20 20   FROM parent;.  
bd40: 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 70 61    INSERT INTO pa
bd50: 72 65 6e 74 20 56 41 4c 55 45 53 28 2d 31 29 3b  rent VALUES(-1);
bd60: 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f  .    INSERT INTO
bd70: 20 63 68 69 6c 64 20 56 41 4c 55 45 53 28 2d 31   child VALUES(-1
bd80: 29 3b 0a 20 20 20 20 55 50 44 41 54 45 20 70 61  );.    UPDATE pa
bd90: 72 65 6e 74 20 53 45 54 20 78 20 3d 20 32 32 3b  rent SET x = 22;
bda0: 0a 20 20 20 20 53 45 4c 45 43 54 20 2a 20 46 52  .    SELECT * FR
bdb0: 4f 4d 20 70 61 72 65 6e 74 20 55 4e 49 4f 4e 20  OM parent UNION 
bdc0: 41 4c 4c 20 53 45 4c 45 43 54 20 27 78 78 78 27  ALL SELECT 'xxx'
bdd0: 20 55 4e 49 4f 4e 20 41 4c 4c 20 53 45 4c 45 43   UNION ALL SELEC
bde0: 54 20 61 20 46 52 4f 4d 20 63 68 69 6c 64 3b 0a  T a FROM child;.
bdf0: 20 20 7d 0a 7d 20 7b 32 32 20 32 33 20 32 31 20    }.} {22 23 21 
be00: 78 78 78 20 32 33 7d 0a 0a 0a 23 2d 2d 2d 2d 2d  xxx 23}...#-----
be10: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
be20: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
be30: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
be40: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
be50: 2d 2d 2d 2d 0a 23 20 2f 2a 20 45 56 3a 20 52 2d  ----.# /* EV: R-
be60: 32 37 33 38 33 2d 31 30 32 34 36 20 2a 2f 0a 23  27383-10246 */.#
be70: 0a 23 20 56 65 72 69 66 79 20 74 68 61 74 20 4f  .# Verify that O
be80: 4e 20 55 50 44 41 54 45 20 61 63 74 69 6f 6e 73  N UPDATE actions
be90: 20 6f 6e 6c 79 20 61 63 74 75 61 6c 6c 79 20 74   only actually t
bea0: 61 6b 65 20 70 6c 61 63 65 20 69 66 20 74 68 65  ake place if the
beb0: 20 70 61 72 65 6e 74 20 6b 65 79 0a 23 20 69 73   parent key.# is
bec0: 20 73 65 74 20 74 6f 20 61 20 6e 65 77 20 76 61   set to a new va
bed0: 6c 75 65 20 74 68 61 74 20 69 73 20 64 69 73 74  lue that is dist
bee0: 69 6e 63 74 20 66 72 6f 6d 20 74 68 65 20 6f 6c  inct from the ol
bef0: 64 20 76 61 6c 75 65 2e 20 54 68 65 20 64 65 66  d value. The def
bf00: 61 75 6c 74 0a 23 20 63 6f 6c 6c 61 74 69 6f 6e  ault.# collation
bf10: 20 73 65 71 75 65 6e 63 65 20 61 6e 64 20 61 66   sequence and af
bf20: 66 69 6e 69 74 79 20 61 72 65 20 75 73 65 64 20  finity are used 
bf30: 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 69 66 20  to determine if 
bf40: 74 68 65 20 6e 65 77 20 76 61 6c 75 65 0a 23 20  the new value.# 
bf50: 69 73 20 27 64 69 73 74 69 6e 63 74 27 20 66 72  is 'distinct' fr
bf60: 6f 6d 20 74 68 65 20 6f 6c 64 20 6f 72 20 6e 6f  om the old or no
bf70: 74 2e 0a 23 0a 64 72 6f 70 5f 61 6c 6c 5f 74 61  t..#.drop_all_ta
bf80: 62 6c 65 73 0a 64 6f 5f 74 65 73 74 20 65 5f 66  bles.do_test e_f
bf90: 6b 65 79 2d 32 36 2e 31 20 7b 0a 20 20 65 78 65  key-26.1 {.  exe
bfa0: 63 73 71 6c 20 7b 0a 20 20 20 20 43 52 45 41 54  csql {.    CREAT
bfb0: 45 20 54 41 42 4c 45 20 7a 65 75 73 28 61 20 49  E TABLE zeus(a I
bfc0: 4e 54 45 47 45 52 20 43 4f 4c 4c 41 54 45 20 4e  NTEGER COLLATE N
bfd0: 4f 43 41 53 45 2c 20 62 2c 20 50 52 49 4d 41 52  OCASE, b, PRIMAR
bfe0: 59 20 4b 45 59 28 61 2c 20 62 29 29 3b 0a 20 20  Y KEY(a, b));.  
bff0: 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 61    CREATE TABLE a
c000: 70 6f 6c 6c 6f 28 63 2c 20 64 2c 20 0a 20 20 20  pollo(c, d, .   
c010: 20 20 20 46 4f 52 45 49 47 4e 20 4b 45 59 28 63     FOREIGN KEY(c
c020: 2c 20 64 29 20 52 45 46 45 52 45 4e 43 45 53 20  , d) REFERENCES 
c030: 7a 65 75 73 20 4f 4e 20 55 50 44 41 54 45 20 43  zeus ON UPDATE C
c040: 41 53 43 41 44 45 0a 20 20 20 20 29 3b 0a 20 20  ASCADE.    );.  
c050: 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 7a 65    INSERT INTO ze
c060: 75 73 20 56 41 4c 55 45 53 28 27 61 62 63 27 2c  us VALUES('abc',
c070: 20 27 78 79 7a 27 29 3b 0a 20 20 20 20 49 4e 53   'xyz');.    INS
c080: 45 52 54 20 49 4e 54 4f 20 61 70 6f 6c 6c 6f 20  ERT INTO apollo 
c090: 56 41 4c 55 45 53 28 27 41 42 43 27 2c 20 27 78  VALUES('ABC', 'x
c0a0: 79 7a 27 29 3b 0a 20 20 7d 0a 20 20 65 78 65 63  yz');.  }.  exec
c0b0: 73 71 6c 20 7b 0a 20 20 20 20 55 50 44 41 54 45  sql {.    UPDATE
c0c0: 20 7a 65 75 73 20 53 45 54 20 61 20 3d 20 27 61   zeus SET a = 'a
c0d0: 42 63 27 3b 0a 20 20 20 20 53 45 4c 45 43 54 20  Bc';.    SELECT 
c0e0: 2a 20 46 52 4f 4d 20 61 70 6f 6c 6c 6f 3b 0a 20  * FROM apollo;. 
c0f0: 20 7d 0a 7d 20 7b 41 42 43 20 78 79 7a 7d 0a 64   }.} {ABC xyz}.d
c100: 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 32 36  o_test e_fkey-26
c110: 2e 32 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b  .2 {.  execsql {
c120: 0a 20 20 20 20 55 50 44 41 54 45 20 7a 65 75 73  .    UPDATE zeus
c130: 20 53 45 54 20 61 20 3d 20 31 2c 20 62 20 3d 20   SET a = 1, b = 
c140: 31 3b 0a 20 20 20 20 53 45 4c 45 43 54 20 2a 20  1;.    SELECT * 
c150: 46 52 4f 4d 20 61 70 6f 6c 6c 6f 3b 0a 20 20 7d  FROM apollo;.  }
c160: 0a 7d 20 7b 31 20 31 7d 0a 64 6f 5f 74 65 73 74  .} {1 1}.do_test
c170: 20 65 5f 66 6b 65 79 2d 32 36 2e 33 20 7b 0a 20   e_fkey-26.3 {. 
c180: 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 55   execsql {.    U
c190: 50 44 41 54 45 20 7a 65 75 73 20 53 45 54 20 61  PDATE zeus SET a
c1a0: 20 3d 20 31 2c 20 62 20 3d 20 31 3b 0a 20 20 20   = 1, b = 1;.   
c1b0: 20 53 45 4c 45 43 54 20 74 79 70 65 6f 66 28 63   SELECT typeof(c
c1c0: 29 2c 20 63 2c 20 74 79 70 65 6f 66 28 64 29 2c  ), c, typeof(d),
c1d0: 20 64 20 46 52 4f 4d 20 61 70 6f 6c 6c 6f 3b 0a   d FROM apollo;.
c1e0: 20 20 7d 0a 7d 20 7b 69 6e 74 65 67 65 72 20 31    }.} {integer 1
c1f0: 20 69 6e 74 65 67 65 72 20 31 7d 0a 64 6f 5f 74   integer 1}.do_t
c200: 65 73 74 20 65 5f 66 6b 65 79 2d 32 36 2e 34 20  est e_fkey-26.4 
c210: 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20  {.  execsql {.  
c220: 20 20 55 50 44 41 54 45 20 7a 65 75 73 20 53 45    UPDATE zeus SE
c230: 54 20 61 20 3d 20 27 31 27 3b 0a 20 20 20 20 53  T a = '1';.    S
c240: 45 4c 45 43 54 20 74 79 70 65 6f 66 28 63 29 2c  ELECT typeof(c),
c250: 20 63 2c 20 74 79 70 65 6f 66 28 64 29 2c 20 64   c, typeof(d), d
c260: 20 46 52 4f 4d 20 61 70 6f 6c 6c 6f 3b 0a 20 20   FROM apollo;.  
c270: 7d 0a 7d 20 7b 69 6e 74 65 67 65 72 20 31 20 69  }.} {integer 1 i
c280: 6e 74 65 67 65 72 20 31 7d 0a 64 6f 5f 74 65 73  nteger 1}.do_tes
c290: 74 20 65 5f 66 6b 65 79 2d 32 36 2e 35 20 7b 0a  t e_fkey-26.5 {.
c2a0: 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20    execsql {.    
c2b0: 55 50 44 41 54 45 20 7a 65 75 73 20 53 45 54 20  UPDATE zeus SET 
c2c0: 62 20 3d 20 27 31 27 3b 0a 20 20 20 20 53 45 4c  b = '1';.    SEL
c2d0: 45 43 54 20 74 79 70 65 6f 66 28 63 29 2c 20 63  ECT typeof(c), c
c2e0: 2c 20 74 79 70 65 6f 66 28 64 29 2c 20 64 20 46  , typeof(d), d F
c2f0: 52 4f 4d 20 61 70 6f 6c 6c 6f 3b 0a 20 20 7d 0a  ROM apollo;.  }.
c300: 7d 20 7b 69 6e 74 65 67 65 72 20 31 20 74 65 78  } {integer 1 tex
c310: 74 20 31 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66  t 1}.do_test e_f
c320: 6b 65 79 2d 32 36 2e 36 20 7b 0a 20 20 65 78 65  key-26.6 {.  exe
c330: 63 73 71 6c 20 7b 0a 20 20 20 20 55 50 44 41 54  csql {.    UPDAT
c340: 45 20 7a 65 75 73 20 53 45 54 20 62 20 3d 20 4e  E zeus SET b = N
c350: 55 4c 4c 3b 0a 20 20 20 20 53 45 4c 45 43 54 20  ULL;.    SELECT 
c360: 74 79 70 65 6f 66 28 63 29 2c 20 63 2c 20 74 79  typeof(c), c, ty
c370: 70 65 6f 66 28 64 29 2c 20 64 20 46 52 4f 4d 20  peof(d), d FROM 
c380: 61 70 6f 6c 6c 6f 3b 0a 20 20 7d 0a 7d 20 7b 69  apollo;.  }.} {i
c390: 6e 74 65 67 65 72 20 31 20 6e 75 6c 6c 20 7b 7d  nteger 1 null {}
c3a0: 7d 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  }..#------------
c3b0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
c3c0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
c3d0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
c3e0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20  -------------.# 
c3f0: 2f 2a 20 45 56 3a 20 52 2d 35 31 34 33 37 2d 33  /* EV: R-51437-3
c400: 39 38 39 31 20 2a 2f 0a 23 0a 23 20 54 65 73 74  9891 */.#.# Test
c410: 20 61 6e 20 65 78 61 6d 70 6c 65 20 66 72 6f 6d   an example from
c420: 20 74 68 65 20 22 4f 4e 20 44 45 4c 45 54 45 20   the "ON DELETE 
c430: 61 6e 64 20 4f 4e 20 55 50 44 41 54 45 20 41 63  and ON UPDATE Ac
c440: 74 69 6f 6e 73 22 20 73 65 63 74 69 6f 6e 20 0a  tions" section .
c450: 23 20 6f 66 20 66 6f 72 65 69 67 6e 6b 65 79 73  # of foreignkeys
c460: 2e 68 74 6d 6c 2e 20 54 68 69 73 20 65 78 61 6d  .html. This exam
c470: 70 6c 65 20 64 65 6d 6f 6e 73 74 72 61 74 65 73  ple demonstrates
c480: 20 74 68 61 74 20 4f 4e 20 55 50 44 41 54 45 20   that ON UPDATE 
c490: 61 63 74 69 6f 6e 73 0a 23 20 6f 6e 6c 79 20 74  actions.# only t
c4a0: 61 6b 65 20 70 6c 61 63 65 20 69 66 20 61 74 20  ake place if at 
c4b0: 6c 65 61 73 74 20 6f 6e 65 20 70 61 72 65 6e 74  least one parent
c4c0: 20 6b 65 79 20 63 6f 6c 75 6d 6e 20 69 73 20 73   key column is s
c4d0: 65 74 20 74 6f 20 61 20 76 61 6c 75 65 20 0a 23  et to a value .#
c4e0: 20 74 68 61 74 20 69 73 20 64 69 73 74 69 6e 63   that is distinc
c4f0: 74 20 66 72 6f 6d 20 69 74 73 20 70 72 65 76 69  t from its previ
c500: 6f 75 73 20 76 61 6c 75 65 2e 0a 23 0a 64 72 6f  ous value..#.dro
c510: 70 5f 61 6c 6c 5f 74 61 62 6c 65 73 0a 64 6f 5f  p_all_tables.do_
c520: 74 65 73 74 20 65 5f 66 6b 65 79 2d 31 33 2e 31  test e_fkey-13.1
c530: 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20   {.  execsql {. 
c540: 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20     CREATE TABLE 
c550: 70 61 72 65 6e 74 28 78 20 50 52 49 4d 41 52 59  parent(x PRIMARY
c560: 20 4b 45 59 29 3b 0a 20 20 20 20 43 52 45 41 54   KEY);.    CREAT
c570: 45 20 54 41 42 4c 45 20 63 68 69 6c 64 28 79 20  E TABLE child(y 
c580: 52 45 46 45 52 45 4e 43 45 53 20 70 61 72 65 6e  REFERENCES paren
c590: 74 20 4f 4e 20 55 50 44 41 54 45 20 53 45 54 20  t ON UPDATE SET 
c5a0: 4e 55 4c 4c 29 3b 0a 20 20 20 20 49 4e 53 45 52  NULL);.    INSER
c5b0: 54 20 49 4e 54 4f 20 70 61 72 65 6e 74 20 56 41  T INTO parent VA
c5c0: 4c 55 45 53 28 27 6b 65 79 27 29 3b 0a 20 20 20  LUES('key');.   
c5d0: 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 63 68 69   INSERT INTO chi
c5e0: 6c 64 20 56 41 4c 55 45 53 28 27 6b 65 79 27 29  ld VALUES('key')
c5f0: 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a 64 6f 5f 74 65  ;.  }.} {}.do_te
c600: 73 74 20 65 5f 66 6b 65 79 2d 31 33 2e 32 20 7b  st e_fkey-13.2 {
c610: 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20  .  execsql {.   
c620: 20 55 50 44 41 54 45 20 70 61 72 65 6e 74 20 53   UPDATE parent S
c630: 45 54 20 78 20 3d 20 27 6b 65 79 27 3b 0a 20 20  ET x = 'key';.  
c640: 20 20 53 45 4c 45 43 54 20 49 46 4e 55 4c 4c 28    SELECT IFNULL(
c650: 79 2c 20 27 6e 75 6c 6c 27 29 20 46 52 4f 4d 20  y, 'null') FROM 
c660: 63 68 69 6c 64 3b 0a 20 20 7d 0a 7d 20 7b 6b 65  child;.  }.} {ke
c670: 79 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65  y}.do_test e_fke
c680: 79 2d 31 33 2e 33 20 7b 0a 20 20 65 78 65 63 73  y-13.3 {.  execs
c690: 71 6c 20 7b 0a 20 20 20 20 55 50 44 41 54 45 20  ql {.    UPDATE 
c6a0: 70 61 72 65 6e 74 20 53 45 54 20 78 20 3d 20 27  parent SET x = '
c6b0: 6b 65 79 32 27 3b 0a 20 20 20 20 53 45 4c 45 43  key2';.    SELEC
c6c0: 54 20 49 46 4e 55 4c 4c 28 79 2c 20 27 6e 75 6c  T IFNULL(y, 'nul
c6d0: 6c 27 29 20 46 52 4f 4d 20 63 68 69 6c 64 3b 0a  l') FROM child;.
c6e0: 20 20 7d 0a 7d 20 7b 6e 75 6c 6c 7d 0a 0a 23 23    }.} {null}..##
c6f0: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
c700: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
c710: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
c720: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
c730: 23 23 23 23 23 23 23 23 23 0a 23 23 23 20 53 45  #########.### SE
c740: 43 54 49 4f 4e 20 35 3a 20 43 52 45 41 54 45 2c  CTION 5: CREATE,
c750: 20 41 4c 54 45 52 20 61 6e 64 20 44 52 4f 50 20   ALTER and DROP 
c760: 54 41 42 4c 45 20 63 6f 6d 6d 61 6e 64 73 0a 23  TABLE commands.#
c770: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
c780: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
c790: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
c7a0: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
c7b0: 23 23 23 23 23 23 23 23 23 23 0a 0a 23 2d 2d 2d  ##########..#---
c7c0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
c7d0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
c7e0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
c7f0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
c800: 2d 2d 2d 2d 2d 2d 0a 23 20 2f 2a 20 45 56 3a 20  ------.# /* EV: 
c810: 52 2d 33 36 30 31 38 2d 32 31 37 35 35 20 2a 2f  R-36018-21755 */
c820: 0a 23 20 2f 2a 20 45 56 3a 20 52 2d 32 35 33 38  .# /* EV: R-2538
c830: 34 2d 33 39 33 33 37 20 2a 2f 0a 23 20 0a 23 20  4-39337 */.# .# 
c840: 54 65 73 74 20 74 68 61 74 20 70 61 72 65 6e 74  Test that parent
c850: 20 6b 65 79 73 20 61 72 65 20 6e 6f 74 20 63 68   keys are not ch
c860: 65 63 6b 65 64 20 77 68 65 6e 20 74 61 62 6c 65  ecked when table
c870: 73 20 61 72 65 20 63 72 65 61 74 65 64 2e 0a 23  s are created..#
c880: 0a 23 20 43 68 69 6c 64 20 6b 65 79 73 20 61 72  .# Child keys ar
c890: 65 20 63 68 65 63 6b 65 64 20 74 6f 20 65 6e 73  e checked to ens
c8a0: 75 72 65 20 61 6c 6c 20 63 6f 6d 70 6f 6e 65 6e  ure all componen
c8b0: 74 20 63 6f 6c 75 6d 6e 73 20 65 78 69 73 74 2e  t columns exist.
c8c0: 20 49 66 20 70 61 72 65 6e 74 0a 23 20 6b 65 79   If parent.# key
c8d0: 20 63 6f 6c 75 6d 6e 73 20 61 72 65 20 65 78 70   columns are exp
c8e0: 6c 69 63 69 74 6c 79 20 73 70 65 63 69 66 69 65  licitly specifie
c8f0: 64 2c 20 53 51 4c 69 74 65 20 63 68 65 63 6b 73  d, SQLite checks
c900: 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68   to make sure th
c910: 65 72 65 0a 23 20 61 72 65 20 74 68 65 20 73 61  ere.# are the sa
c920: 6d 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c  me number of col
c930: 75 6d 6e 73 20 69 6e 20 74 68 65 20 63 68 69 6c  umns in the chil
c940: 64 20 61 6e 64 20 70 61 72 65 6e 74 20 6b 65 79  d and parent key
c950: 73 2e 20 28 54 4f 44 4f 3a 20 54 68 69 73 0a 23  s. (TODO: This.#
c960: 20 69 73 20 74 65 73 74 65 64 20 62 75 74 20 64   is tested but d
c970: 6f 65 73 20 6e 6f 74 20 63 6f 72 72 65 73 70 6f  oes not correspo
c980: 6e 64 20 74 6f 20 61 6e 79 20 74 65 73 74 61 62  nd to any testab
c990: 6c 65 20 73 74 61 74 65 6d 65 6e 74 2e 29 0a 23  le statement.).#
c9a0: 0a 23 20 2f 2a 20 45 56 3a 20 52 2d 30 38 39 30  .# /* EV: R-0890
c9b0: 38 2d 32 33 34 33 39 20 2a 2f 0a 23 0a 23 20 41  8-23439 */.#.# A
c9c0: 6c 73 6f 20 74 65 73 74 20 74 68 61 74 20 74 68  lso test that th
c9d0: 65 20 61 62 6f 76 65 20 73 74 61 74 65 6d 65 6e  e above statemen
c9e0: 74 73 20 61 72 65 20 74 72 75 65 20 72 65 67 61  ts are true rega
c9f0: 72 64 6c 65 73 73 20 6f 66 20 77 68 65 74 68 65  rdless of whethe
ca00: 72 20 6f 72 20 6e 6f 74 0a 23 20 66 6f 72 65 69  r or not.# forei
ca10: 67 6e 20 6b 65 79 73 20 61 72 65 20 65 6e 61 62  gn keys are enab
ca20: 6c 65 64 3a 20 20 22 41 20 43 52 45 41 54 45 20  led:  "A CREATE 
ca30: 54 41 42 4c 45 20 63 6f 6d 6d 61 6e 64 20 6f 70  TABLE command op
ca40: 65 72 61 74 65 73 20 74 68 65 20 73 61 6d 65 20  erates the same 
ca50: 77 68 65 74 68 65 72 0a 23 20 6f 72 20 6e 6f 74  whether.# or not
ca60: 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e   foreign key con
ca70: 73 74 72 61 69 6e 74 73 20 61 72 65 20 65 6e 61  straints are ena
ca80: 62 6c 65 64 2e 22 0a 23 20 0a 66 6f 72 65 61 63  bled.".# .foreac
ca90: 68 20 7b 74 6e 20 7a 43 72 65 61 74 65 54 62 6c  h {tn zCreateTbl
caa0: 20 6c 52 65 73 7d 20 7b 0a 20 20 31 20 22 43 52   lRes} {.  1 "CR
cab0: 45 41 54 45 20 54 41 42 4c 45 20 74 31 28 61 2c  EATE TABLE t1(a,
cac0: 20 62 20 52 45 46 45 52 45 4e 43 45 53 20 74 31   b REFERENCES t1
cad0: 29 22 20 20 20 20 20 20 20 20 20 20 20 20 20 20  )"              
cae0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7b 30                {0
caf0: 20 7b 7d 7d 0a 20 20 32 20 22 43 52 45 41 54 45   {}}.  2 "CREATE
cb00: 20 54 41 42 4c 45 20 74 31 28 61 2c 20 62 20 52   TABLE t1(a, b R
cb10: 45 46 45 52 45 4e 43 45 53 20 74 32 29 22 20 20  EFERENCES t2)"  
cb20: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cb30: 20 20 20 20 20 20 20 20 20 20 7b 30 20 7b 7d 7d            {0 {}}
cb40: 0a 20 20 33 20 22 43 52 45 41 54 45 20 54 41 42  .  3 "CREATE TAB
cb50: 4c 45 20 74 31 28 61 2c 20 62 2c 20 46 4f 52 45  LE t1(a, b, FORE
cb60: 49 47 4e 20 4b 45 59 28 61 2c 62 29 20 52 45 46  IGN KEY(a,b) REF
cb70: 45 52 45 4e 43 45 53 20 74 31 29 22 20 20 20 20  ERENCES t1)"    
cb80: 20 20 20 20 20 20 7b 30 20 7b 7d 7d 0a 20 20 34        {0 {}}.  4
cb90: 20 22 43 52 45 41 54 45 20 54 41 42 4c 45 20 74   "CREATE TABLE t
cba0: 31 28 61 2c 20 62 2c 20 46 4f 52 45 49 47 4e 20  1(a, b, FOREIGN 
cbb0: 4b 45 59 28 61 2c 62 29 20 52 45 46 45 52 45 4e  KEY(a,b) REFEREN
cbc0: 43 45 53 20 74 32 29 22 20 20 20 20 20 20 20 20  CES t2)"        
cbd0: 20 20 7b 30 20 7b 7d 7d 0a 20 20 35 20 22 43 52    {0 {}}.  5 "CR
cbe0: 45 41 54 45 20 54 41 42 4c 45 20 74 31 28 61 2c  EATE TABLE t1(a,
cbf0: 20 62 2c 20 46 4f 52 45 49 47 4e 20 4b 45 59 28   b, FOREIGN KEY(
cc00: 61 2c 62 29 20 52 45 46 45 52 45 4e 43 45 53 20  a,b) REFERENCES 
cc10: 74 32 29 22 20 20 20 20 20 20 20 20 20 20 7b 30  t2)"          {0
cc20: 20 7b 7d 7d 0a 20 20 36 20 22 43 52 45 41 54 45   {}}.  6 "CREATE
cc30: 20 54 41 42 4c 45 20 74 31 28 61 2c 20 62 2c 20   TABLE t1(a, b, 
cc40: 46 4f 52 45 49 47 4e 20 4b 45 59 28 61 2c 62 29  FOREIGN KEY(a,b)
cc50: 20 52 45 46 45 52 45 4e 43 45 53 20 74 32 28 6e   REFERENCES t2(n
cc60: 2c 64 29 29 22 20 20 20 20 20 7b 30 20 7b 7d 7d  ,d))"     {0 {}}
cc70: 0a 20 20 37 20 22 43 52 45 41 54 45 20 54 41 42  .  7 "CREATE TAB
cc80: 4c 45 20 74 31 28 61 2c 20 62 2c 20 46 4f 52 45  LE t1(a, b, FORE
cc90: 49 47 4e 20 4b 45 59 28 61 2c 62 29 20 52 45 46  IGN KEY(a,b) REF
cca0: 45 52 45 4e 43 45 53 20 74 31 28 61 2c 62 29 29  ERENCES t1(a,b))
ccb0: 22 20 20 20 20 20 7b 30 20 7b 7d 7d 0a 0a 20 20  "     {0 {}}..  
ccc0: 41 20 22 43 52 45 41 54 45 20 54 41 42 4c 45 20  A "CREATE TABLE 
ccd0: 74 31 28 61 2c 20 62 2c 20 46 4f 52 45 49 47 4e  t1(a, b, FOREIGN
cce0: 20 4b 45 59 28 63 2c 62 29 20 52 45 46 45 52 45   KEY(c,b) REFERE
ccf0: 4e 43 45 53 20 74 32 29 22 20 20 20 20 20 20 20  NCES t2)"       
cd00: 20 20 20 0a 20 20 20 20 20 7b 31 20 7b 75 6e 6b     .     {1 {unk
cd10: 6e 6f 77 6e 20 63 6f 6c 75 6d 6e 20 22 63 22 20  nown column "c" 
cd20: 69 6e 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 64  in foreign key d
cd30: 65 66 69 6e 69 74 69 6f 6e 7d 7d 0a 20 20 42 20  efinition}}.  B 
cd40: 22 43 52 45 41 54 45 20 54 41 42 4c 45 20 74 31  "CREATE TABLE t1
cd50: 28 61 2c 20 62 2c 20 46 4f 52 45 49 47 4e 20 4b  (a, b, FOREIGN K
cd60: 45 59 28 63 2c 62 29 20 52 45 46 45 52 45 4e 43  EY(c,b) REFERENC
cd70: 45 53 20 74 32 28 64 29 29 22 20 20 20 20 20 20  ES t2(d))"      
cd80: 20 20 20 20 0a 20 20 20 20 20 7b 31 20 7b 6e 75      .     {1 {nu
cd90: 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20  mber of columns 
cda0: 69 6e 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 64  in foreign key d
cdb0: 6f 65 73 20 6e 6f 74 20 6d 61 74 63 68 20 74 68  oes not match th
cdc0: 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75  e number of colu
cdd0: 6d 6e 73 20 69 6e 20 74 68 65 20 72 65 66 65 72  mns in the refer
cde0: 65 6e 63 65 64 20 74 61 62 6c 65 7d 7d 0a 7d 20  enced table}}.} 
cdf0: 7b 0a 20 20 64 6f 5f 74 65 73 74 20 65 5f 66 6b  {.  do_test e_fk
ce00: 65 79 2d 35 2e 24 74 6e 2e 6f 66 66 20 7b 0a 20  ey-5.$tn.off {. 
ce10: 20 20 20 64 72 6f 70 5f 61 6c 6c 5f 74 61 62 6c     drop_all_tabl
ce20: 65 73 0a 20 20 20 20 65 78 65 63 73 71 6c 20 7b  es.    execsql {
ce30: 50 52 41 47 4d 41 20 66 6f 72 65 69 67 6e 5f 6b  PRAGMA foreign_k
ce40: 65 79 73 20 3d 20 4f 46 46 7d 0a 20 20 20 20 63  eys = OFF}.    c
ce50: 61 74 63 68 73 71 6c 20 24 7a 43 72 65 61 74 65  atchsql $zCreate
ce60: 54 62 6c 0a 20 20 7d 20 24 6c 52 65 73 0a 20 20  Tbl.  } $lRes.  
ce70: 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 35  do_test e_fkey-5
ce80: 2e 24 74 6e 2e 6f 6e 20 7b 0a 20 20 20 20 64 72  .$tn.on {.    dr
ce90: 6f 70 5f 61 6c 6c 5f 74 61 62 6c 65 73 0a 20 20  op_all_tables.  
cea0: 20 20 65 78 65 63 73 71 6c 20 7b 50 52 41 47 4d    execsql {PRAGM
ceb0: 41 20 66 6f 72 65 69 67 6e 5f 6b 65 79 73 20 3d  A foreign_keys =
cec0: 20 4f 4e 7d 0a 20 20 20 20 63 61 74 63 68 73 71   ON}.    catchsq
ced0: 6c 20 24 7a 43 72 65 61 74 65 54 62 6c 0a 20 20  l $zCreateTbl.  
cee0: 7d 20 24 6c 52 65 73 0a 7d 0a 0a 23 2d 2d 2d 2d  } $lRes.}..#----
cef0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
cf00: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
cf10: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
cf20: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
cf30: 2d 2d 2d 2d 2d 0a 23 20 2f 2a 20 45 56 3a 20 52  -----.# /* EV: R
cf40: 2d 34 37 39 35 32 2d 36 32 34 39 38 20 2a 2f 0a  -47952-62498 */.
cf50: 23 0a 70 72 6f 63 20 74 65 73 74 5f 65 66 6b 65  #.proc test_efke
cf60: 79 5f 36 20 7b 74 6e 20 7a 41 6c 74 65 72 20 69  y_6 {tn zAlter i
cf70: 73 45 72 72 6f 72 7d 20 7b 0a 20 20 64 72 6f 70  sError} {.  drop
cf80: 5f 61 6c 6c 5f 74 61 62 6c 65 73 20 0a 0a 20 20  _all_tables ..  
cf90: 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 36  do_test e_fkey-6
cfa0: 2e 24 74 6e 2e 31 20 22 0a 20 20 20 20 65 78 65  .$tn.1 ".    exe
cfb0: 63 73 71 6c 20 7b 20 43 52 45 41 54 45 20 54 41  csql { CREATE TA
cfc0: 42 4c 45 20 74 62 6c 28 61 2c 20 62 29 20 7d 0a  BLE tbl(a, b) }.
cfd0: 20 20 20 20 5b 6c 69 73 74 20 63 61 74 63 68 73      [list catchs
cfe0: 71 6c 20 24 7a 41 6c 74 65 72 5d 0a 20 20 22 20  ql $zAlter].  " 
cff0: 5b 6c 69 6e 64 65 78 20 7b 7b 30 20 7b 7d 7d 20  [lindex {{0 {}} 
d000: 7b 31 20 7b 43 61 6e 6e 6f 74 20 61 64 64 20 61  {1 {Cannot add a
d010: 20 52 45 46 45 52 45 4e 43 45 53 20 63 6f 6c 75   REFERENCES colu
d020: 6d 6e 20 77 69 74 68 20 6e 6f 6e 2d 4e 55 4c 4c  mn with non-NULL
d030: 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65 7d 7d   default value}}
d040: 7d 20 24 69 73 45 72 72 6f 72 5d 0a 0a 7d 0a 0a  } $isError]..}..
d050: 74 65 73 74 5f 65 66 6b 65 79 5f 36 20 31 20 22  test_efkey_6 1 "
d060: 41 4c 54 45 52 20 54 41 42 4c 45 20 74 62 6c 20  ALTER TABLE tbl 
d070: 41 44 44 20 43 4f 4c 55 4d 4e 20 63 20 52 45 46  ADD COLUMN c REF
d080: 45 52 45 4e 43 45 53 20 78 78 22 20 30 0a 74 65  ERENCES xx" 0.te
d090: 73 74 5f 65 66 6b 65 79 5f 36 20 32 20 22 41 4c  st_efkey_6 2 "AL
d0a0: 54 45 52 20 54 41 42 4c 45 20 74 62 6c 20 41 44  TER TABLE tbl AD
d0b0: 44 20 43 4f 4c 55 4d 4e 20 63 20 44 45 46 41 55  D COLUMN c DEFAU
d0c0: 4c 54 20 4e 55 4c 4c 20 52 45 46 45 52 45 4e 43  LT NULL REFERENC
d0d0: 45 53 20 78 78 22 20 30 0a 74 65 73 74 5f 65 66  ES xx" 0.test_ef
d0e0: 6b 65 79 5f 36 20 33 20 22 41 4c 54 45 52 20 54  key_6 3 "ALTER T
d0f0: 41 42 4c 45 20 74 62 6c 20 41 44 44 20 43 4f 4c  ABLE tbl ADD COL
d100: 55 4d 4e 20 63 20 44 45 46 41 55 4c 54 20 30 20  UMN c DEFAULT 0 
d110: 52 45 46 45 52 45 4e 43 45 53 20 78 78 22 20 31  REFERENCES xx" 1
d120: 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ..#-------------
d130: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
d140: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
d150: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
d160: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20 2f  ------------.# /
d170: 2a 20 45 56 3a 20 52 2d 34 37 30 38 30 2d 30 32  * EV: R-47080-02
d180: 30 36 39 20 2a 2f 0a 23 0a 23 20 54 65 73 74 20  069 */.#.# Test 
d190: 74 68 61 74 20 41 4c 54 45 52 20 54 41 42 4c 45  that ALTER TABLE
d1a0: 20 61 64 6a 75 73 74 73 20 52 45 46 45 52 45 4e   adjusts REFEREN
d1b0: 43 45 53 20 63 6c 61 75 73 65 73 20 77 68 65 6e  CES clauses when
d1c0: 20 74 68 65 20 70 61 72 65 6e 74 20 74 61 62 6c   the parent tabl
d1d0: 65 0a 23 20 69 73 20 52 45 4e 41 4d 45 44 2e 0a  e.# is RENAMED..
d1e0: 23 0a 23 20 2f 2a 20 45 56 3a 20 52 2d 36 33 38  #.# /* EV: R-638
d1f0: 32 37 2d 35 34 37 37 34 20 2a 2f 0a 23 0a 23 20  27-54774 */.#.# 
d200: 54 65 73 74 20 74 68 61 74 20 74 68 65 73 65 20  Test that these 
d210: 61 64 6a 75 73 74 6d 65 6e 74 73 20 61 72 65 20  adjustments are 
d220: 76 69 73 69 62 6c 65 20 69 6e 20 74 68 65 20 73  visible in the s
d230: 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62  qlite_master tab
d240: 6c 65 2e 0a 23 0a 64 6f 5f 74 65 73 74 20 65 5f  le..#.do_test e_
d250: 66 6b 65 79 2d 37 2e 31 20 7b 0a 20 20 64 72 6f  fkey-7.1 {.  dro
d260: 70 5f 61 6c 6c 5f 74 61 62 6c 65 73 0a 20 20 65  p_all_tables.  e
d270: 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 43 52 45  xecsql {.    CRE
d280: 41 54 45 20 54 41 42 4c 45 20 27 70 20 31 20 22  ATE TABLE 'p 1 "
d290: 70 61 72 65 6e 74 20 6f 6e 65 22 27 28 61 20 52  parent one"'(a R
d2a0: 45 46 45 52 45 4e 43 45 53 20 27 70 20 31 20 22  EFERENCES 'p 1 "
d2b0: 70 61 72 65 6e 74 20 6f 6e 65 22 27 2c 20 62 2c  parent one"', b,
d2c0: 20 50 52 49 4d 41 52 59 20 4b 45 59 28 62 29 29   PRIMARY KEY(b))
d2d0: 3b 0a 0a 20 20 20 20 43 52 45 41 54 45 20 54 41  ;..    CREATE TA
d2e0: 42 4c 45 20 63 31 28 63 2c 20 64 20 52 45 46 45  BLE c1(c, d REFE
d2f0: 52 45 4e 43 45 53 20 27 70 20 31 20 22 70 61 72  RENCES 'p 1 "par
d300: 65 6e 74 20 6f 6e 65 22 27 20 4f 4e 20 55 50 44  ent one"' ON UPD
d310: 41 54 45 20 43 41 53 43 41 44 45 29 3b 0a 20 20  ATE CASCADE);.  
d320: 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 63    CREATE TABLE c
d330: 32 28 65 2c 20 66 2c 20 46 4f 52 45 49 47 4e 20  2(e, f, FOREIGN 
d340: 4b 45 59 28 66 29 20 52 45 46 45 52 45 4e 43 45  KEY(f) REFERENCE
d350: 53 20 27 70 20 31 20 22 70 61 72 65 6e 74 20 6f  S 'p 1 "parent o
d360: 6e 65 22 27 20 4f 4e 20 55 50 44 41 54 45 20 43  ne"' ON UPDATE C
d370: 41 53 43 41 44 45 29 3b 0a 20 20 20 20 43 52 45  ASCADE);.    CRE
d380: 41 54 45 20 54 41 42 4c 45 20 63 33 28 65 2c 20  ATE TABLE c3(e, 
d390: 27 66 20 63 6f 6c 20 32 27 2c 20 46 4f 52 45 49  'f col 2', FOREI
d3a0: 47 4e 20 4b 45 59 28 27 66 20 63 6f 6c 20 32 27  GN KEY('f col 2'
d3b0: 29 20 52 45 46 45 52 45 4e 43 45 53 20 27 70 20  ) REFERENCES 'p 
d3c0: 31 20 22 70 61 72 65 6e 74 20 6f 6e 65 22 27 20  1 "parent one"' 
d3d0: 4f 4e 20 55 50 44 41 54 45 20 43 41 53 43 41 44  ON UPDATE CASCAD
d3e0: 45 29 3b 0a 0a 20 20 20 20 49 4e 53 45 52 54 20  E);..    INSERT 
d3f0: 49 4e 54 4f 20 27 70 20 31 20 22 70 61 72 65 6e  INTO 'p 1 "paren
d400: 74 20 6f 6e 65 22 27 20 56 41 4c 55 45 53 28 31  t one"' VALUES(1
d410: 2c 20 31 29 3b 0a 20 20 20 20 49 4e 53 45 52 54  , 1);.    INSERT
d420: 20 49 4e 54 4f 20 63 31 20 56 41 4c 55 45 53 28   INTO c1 VALUES(
d430: 31 2c 20 31 29 3b 0a 20 20 20 20 49 4e 53 45 52  1, 1);.    INSER
d440: 54 20 49 4e 54 4f 20 63 32 20 56 41 4c 55 45 53  T INTO c2 VALUES
d450: 28 31 2c 20 31 29 3b 0a 20 20 20 20 49 4e 53 45  (1, 1);.    INSE
d460: 52 54 20 49 4e 54 4f 20 63 33 20 56 41 4c 55 45  RT INTO c3 VALUE
d470: 53 28 31 2c 20 31 29 3b 0a 0a 20 20 20 20 2d 2d  S(1, 1);..    --
d480: 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 71 28   CREATE TABLE q(
d490: 61 2c 20 62 2c 20 50 52 49 4d 41 52 59 20 4b 45  a, b, PRIMARY KE
d4a0: 59 28 62 29 29 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a  Y(b));.  }.} {}.
d4b0: 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 37  do_test e_fkey-7
d4c0: 2e 32 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b  .2 {.  execsql {
d4d0: 20 41 4c 54 45 52 20 54 41 42 4c 45 20 27 70 20   ALTER TABLE 'p 
d4e0: 31 20 22 70 61 72 65 6e 74 20 6f 6e 65 22 27 20  1 "parent one"' 
d4f0: 52 45 4e 41 4d 45 20 54 4f 20 70 20 7d 0a 7d 20  RENAME TO p }.} 
d500: 7b 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65  {}.do_test e_fke
d510: 79 2d 37 2e 33 20 7b 0a 20 20 65 78 65 63 73 71  y-7.3 {.  execsq
d520: 6c 20 7b 0a 20 20 20 20 55 50 44 41 54 45 20 70  l {.    UPDATE p
d530: 20 53 45 54 20 61 20 3d 20 27 78 78 78 27 2c 20   SET a = 'xxx', 
d540: 62 20 3d 20 27 78 78 78 27 3b 0a 20 20 20 20 53  b = 'xxx';.    S
d550: 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 70 3b 0a  ELECT * FROM p;.
d560: 20 20 20 20 53 45 4c 45 43 54 20 2a 20 46 52 4f      SELECT * FRO
d570: 4d 20 63 31 3b 0a 20 20 20 20 53 45 4c 45 43 54  M c1;.    SELECT
d580: 20 2a 20 46 52 4f 4d 20 63 32 3b 0a 20 20 20 20   * FROM c2;.    
d590: 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 63 33  SELECT * FROM c3
d5a0: 3b 0a 20 20 7d 0a 7d 20 7b 78 78 78 20 78 78 78  ;.  }.} {xxx xxx
d5b0: 20 31 20 78 78 78 20 31 20 78 78 78 20 31 20 78   1 xxx 1 xxx 1 x
d5c0: 78 78 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b  xx}.do_test e_fk
d5d0: 65 79 2d 37 2e 34 20 7b 0a 20 20 65 78 65 63 73  ey-7.4 {.  execs
d5e0: 71 6c 20 7b 20 53 45 4c 45 43 54 20 73 71 6c 20  ql { SELECT sql 
d5f0: 46 52 4f 4d 20 73 71 6c 69 74 65 5f 6d 61 73 74  FROM sqlite_mast
d600: 65 72 20 57 48 45 52 45 20 74 79 70 65 20 3d 20  er WHERE type = 
d610: 27 74 61 62 6c 65 27 7d 0a 7d 20 5b 6c 69 73 74  'table'}.} [list
d620: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d630: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d640: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d650: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d660: 20 20 20 20 20 5c 0a 20 20 7b 43 52 45 41 54 45       \.  {CREATE
d670: 20 54 41 42 4c 45 20 22 70 22 28 61 20 52 45 46   TABLE "p"(a REF
d680: 45 52 45 4e 43 45 53 20 22 70 22 2c 20 62 2c 20  ERENCES "p", b, 
d690: 50 52 49 4d 41 52 59 20 4b 45 59 28 62 29 29 7d  PRIMARY KEY(b))}
d6a0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d6b0: 20 20 20 5c 0a 20 20 7b 43 52 45 41 54 45 20 54     \.  {CREATE T
d6c0: 41 42 4c 45 20 63 31 28 63 2c 20 64 20 52 45 46  ABLE c1(c, d REF
d6d0: 45 52 45 4e 43 45 53 20 22 70 22 20 4f 4e 20 55  ERENCES "p" ON U
d6e0: 50 44 41 54 45 20 43 41 53 43 41 44 45 29 7d 20  PDATE CASCADE)} 
d6f0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d700: 20 5c 0a 20 20 7b 43 52 45 41 54 45 20 54 41 42   \.  {CREATE TAB
d710: 4c 45 20 63 32 28 65 2c 20 66 2c 20 46 4f 52 45  LE c2(e, f, FORE
d720: 49 47 4e 20 4b 45 59 28 66 29 20 52 45 46 45 52  IGN KEY(f) REFER
d730: 45 4e 43 45 53 20 22 70 22 20 4f 4e 20 55 50 44  ENCES "p" ON UPD
d740: 41 54 45 20 43 41 53 43 41 44 45 29 7d 20 20 5c  ATE CASCADE)}  \
d750: 0a 20 20 7b 43 52 45 41 54 45 20 54 41 42 4c 45  .  {CREATE TABLE
d760: 20 63 33 28 65 2c 20 27 66 20 63 6f 6c 20 32 27   c3(e, 'f col 2'
d770: 2c 20 46 4f 52 45 49 47 4e 20 4b 45 59 28 27 66  , FOREIGN KEY('f
d780: 20 63 6f 6c 20 32 27 29 20 52 45 46 45 52 45 4e   col 2') REFEREN
d790: 43 45 53 20 22 70 22 20 4f 4e 20 55 50 44 41 54  CES "p" ON UPDAT
d7a0: 45 20 43 41 53 43 41 44 45 29 7d 20 5c 0a 5d 0a  E CASCADE)} \.].
d7b0: 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  .#--------------
d7c0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
d7d0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
d7e0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
d7f0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20 2f 2a  -----------.# /*
d800: 20 45 56 3a 20 52 2d 31 34 32 30 38 2d 32 33 39   EV: R-14208-239
d810: 38 36 20 2a 2f 0a 23 20 2f 2a 20 45 56 3a 20 52  86 */.# /* EV: R
d820: 2d 31 31 30 37 38 2d 30 33 39 34 35 20 2a 2f 0a  -11078-03945 */.
d830: 23 0a 23 20 43 68 65 63 6b 20 74 68 61 74 20 61  #.# Check that a
d840: 20 44 52 4f 50 20 54 41 42 4c 45 20 64 6f 65 73   DROP TABLE does
d850: 20 61 6e 20 69 6d 70 6c 69 63 69 74 20 44 45 4c   an implicit DEL
d860: 45 54 45 20 46 52 4f 4d 2e 20 57 68 69 63 68 20  ETE FROM. Which 
d870: 64 6f 65 73 20 6e 6f 74 0a 23 20 63 61 75 73 65  does not.# cause
d880: 20 61 6e 79 20 74 72 69 67 67 65 72 73 20 74 6f   any triggers to
d890: 20 66 69 72 65 2c 20 62 75 74 20 64 6f 65 73 20   fire, but does 
d8a0: 66 69 72 65 20 66 6f 72 65 69 67 6e 20 6b 65 79  fire foreign key
d8b0: 20 61 63 74 69 6f 6e 73 2e 0a 23 0a 64 6f 5f 74   actions..#.do_t
d8c0: 65 73 74 20 65 5f 66 6b 65 79 2d 38 2e 31 20 7b  est e_fkey-8.1 {
d8d0: 0a 20 20 64 72 6f 70 5f 61 6c 6c 5f 74 61 62 6c  .  drop_all_tabl
d8e0: 65 73 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20  es.  execsql {. 
d8f0: 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20     CREATE TABLE 
d900: 70 28 61 2c 20 62 2c 20 50 52 49 4d 41 52 59 20  p(a, b, PRIMARY 
d910: 4b 45 59 28 61 2c 20 62 29 29 3b 0a 0a 20 20 20  KEY(a, b));..   
d920: 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 63 31   CREATE TABLE c1
d930: 28 63 2c 20 64 2c 20 46 4f 52 45 49 47 4e 20 4b  (c, d, FOREIGN K
d940: 45 59 28 63 2c 20 64 29 20 52 45 46 45 52 45 4e  EY(c, d) REFEREN
d950: 43 45 53 20 70 20 4f 4e 20 44 45 4c 45 54 45 20  CES p ON DELETE 
d960: 53 45 54 20 4e 55 4c 4c 29 3b 0a 20 20 20 20 43  SET NULL);.    C
d970: 52 45 41 54 45 20 54 41 42 4c 45 20 63 32 28 63  REATE TABLE c2(c
d980: 2c 20 64 2c 20 46 4f 52 45 49 47 4e 20 4b 45 59  , d, FOREIGN KEY
d990: 28 63 2c 20 64 29 20 52 45 46 45 52 45 4e 43 45  (c, d) REFERENCE
d9a0: 53 20 70 20 4f 4e 20 44 45 4c 45 54 45 20 53 45  S p ON DELETE SE
d9b0: 54 20 44 45 46 41 55 4c 54 29 3b 0a 20 20 20 20  T DEFAULT);.    
d9c0: 43 52 45 41 54 45 20 54 41 42 4c 45 20 63 33 28  CREATE TABLE c3(
d9d0: 63 2c 20 64 2c 20 46 4f 52 45 49 47 4e 20 4b 45  c, d, FOREIGN KE
d9e0: 59 28 63 2c 20 64 29 20 52 45 46 45 52 45 4e 43  Y(c, d) REFERENC
d9f0: 45 53 20 70 20 4f 4e 20 44 45 4c 45 54 45 20 43  ES p ON DELETE C
da00: 41 53 43 41 44 45 29 3b 0a 20 20 20 20 43 52 45  ASCADE);.    CRE
da10: 41 54 45 20 54 41 42 4c 45 20 63 34 28 63 2c 20  ATE TABLE c4(c, 
da20: 64 2c 20 46 4f 52 45 49 47 4e 20 4b 45 59 28 63  d, FOREIGN KEY(c
da30: 2c 20 64 29 20 52 45 46 45 52 45 4e 43 45 53 20  , d) REFERENCES 
da40: 70 20 4f 4e 20 44 45 4c 45 54 45 20 52 45 53 54  p ON DELETE REST
da50: 52 49 43 54 29 3b 0a 20 20 20 20 43 52 45 41 54  RICT);.    CREAT
da60: 45 20 54 41 42 4c 45 20 63 35 28 63 2c 20 64 2c  E TABLE c5(c, d,
da70: 20 46 4f 52 45 49 47 4e 20 4b 45 59 28 63 2c 20   FOREIGN KEY(c, 
da80: 64 29 20 52 45 46 45 52 45 4e 43 45 53 20 70 20  d) REFERENCES p 
da90: 4f 4e 20 44 45 4c 45 54 45 20 4e 4f 20 41 43 54  ON DELETE NO ACT
daa0: 49 4f 4e 29 3b 0a 0a 20 20 20 20 43 52 45 41 54  ION);..    CREAT
dab0: 45 20 54 41 42 4c 45 20 63 36 28 63 2c 20 64 2c  E TABLE c6(c, d,
dac0: 20 0a 20 20 20 20 20 20 46 4f 52 45 49 47 4e 20   .      FOREIGN 
dad0: 4b 45 59 28 63 2c 20 64 29 20 52 45 46 45 52 45  KEY(c, d) REFERE
dae0: 4e 43 45 53 20 70 20 4f 4e 20 44 45 4c 45 54 45  NCES p ON DELETE
daf0: 20 52 45 53 54 52 49 43 54 20 0a 20 20 20 20 20   RESTRICT .     
db00: 20 44 45 46 45 52 52 41 42 4c 45 20 49 4e 49 54   DEFERRABLE INIT
db10: 49 41 4c 4c 59 20 44 45 46 45 52 52 45 44 0a 20  IALLY DEFERRED. 
db20: 20 20 20 29 3b 0a 20 20 20 20 43 52 45 41 54 45     );.    CREATE
db30: 20 54 41 42 4c 45 20 63 37 28 63 2c 20 64 2c 20   TABLE c7(c, d, 
db40: 0a 20 20 20 20 20 20 46 4f 52 45 49 47 4e 20 4b  .      FOREIGN K
db50: 45 59 28 63 2c 20 64 29 20 52 45 46 45 52 45 4e  EY(c, d) REFEREN
db60: 43 45 53 20 70 20 4f 4e 20 44 45 4c 45 54 45 20  CES p ON DELETE 
db70: 4e 4f 20 41 43 54 49 4f 4e 0a 20 20 20 20 20 20  NO ACTION.      
db80: 44 45 46 45 52 52 41 42 4c 45 20 49 4e 49 54 49  DEFERRABLE INITI
db90: 41 4c 4c 59 20 44 45 46 45 52 52 45 44 0a 20 20  ALLY DEFERRED.  
dba0: 20 20 29 3b 0a 0a 20 20 20 20 43 52 45 41 54 45    );..    CREATE
dbb0: 20 54 41 42 4c 45 20 6c 6f 67 28 6d 73 67 29 3b   TABLE log(msg);
dbc0: 0a 20 20 20 20 43 52 45 41 54 45 20 54 52 49 47  .    CREATE TRIG
dbd0: 47 45 52 20 74 74 20 41 46 54 45 52 20 44 45 4c  GER tt AFTER DEL
dbe0: 45 54 45 20 4f 4e 20 70 20 42 45 47 49 4e 0a 20  ETE ON p BEGIN. 
dbf0: 20 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f       INSERT INTO
dc00: 20 6c 6f 67 20 56 41 4c 55 45 53 28 27 64 65 6c   log VALUES('del
dc10: 65 74 65 20 27 20 7c 7c 20 6f 6c 64 2e 72 6f 77  ete ' || old.row
dc20: 69 64 29 3b 0a 20 20 20 20 45 4e 44 3b 0a 20 20  id);.    END;.  
dc30: 7d 0a 7d 20 7b 7d 0a 0a 64 6f 5f 74 65 73 74 20  }.} {}..do_test 
dc40: 65 5f 66 6b 65 79 2d 38 2e 32 20 7b 0a 20 20 65  e_fkey-8.2 {.  e
dc50: 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 49 4e 53  xecsql {.    INS
dc60: 45 52 54 20 49 4e 54 4f 20 70 20 56 41 4c 55 45  ERT INTO p VALUE
dc70: 53 28 27 61 27 2c 20 27 62 27 29 3b 0a 20 20 20  S('a', 'b');.   
dc80: 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 63 31 20   INSERT INTO c1 
dc90: 56 41 4c 55 45 53 28 27 61 27 2c 20 27 62 27 29  VALUES('a', 'b')
dca0: 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54  ;.    INSERT INT
dcb0: 4f 20 63 32 20 56 41 4c 55 45 53 28 27 61 27 2c  O c2 VALUES('a',
dcc0: 20 27 62 27 29 3b 0a 20 20 20 20 49 4e 53 45 52   'b');.    INSER
dcd0: 54 20 49 4e 54 4f 20 63 33 20 56 41 4c 55 45 53  T INTO c3 VALUES
dce0: 28 27 61 27 2c 20 27 62 27 29 3b 0a 20 20 20 20  ('a', 'b');.    
dcf0: 42 45 47 49 4e 3b 0a 20 20 20 20 20 20 44 52 4f  BEGIN;.      DRO
dd00: 50 20 54 41 42 4c 45 20 70 3b 0a 20 20 20 20 20  P TABLE p;.     
dd10: 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 63   SELECT * FROM c
dd20: 31 3b 0a 20 20 7d 0a 7d 20 7b 7b 7d 20 7b 7d 7d  1;.  }.} {{} {}}
dd30: 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d  .do_test e_fkey-
dd40: 38 2e 33 20 7b 0a 20 20 65 78 65 63 73 71 6c 20  8.3 {.  execsql 
dd50: 7b 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20  { SELECT * FROM 
dd60: 63 32 20 7d 0a 7d 20 7b 7b 7d 20 7b 7d 7d 0a 64  c2 }.} {{} {}}.d
dd70: 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 38 2e  o_test e_fkey-8.
dd80: 34 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 20  4 {.  execsql { 
dd90: 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 63 33  SELECT * FROM c3
dda0: 20 7d 0a 7d 20 7b 7d 0a 64 6f 5f 74 65 73 74 20   }.} {}.do_test 
ddb0: 65 5f 66 6b 65 79 2d 38 2e 35 20 7b 0a 20 20 65  e_fkey-8.5 {.  e
ddc0: 78 65 63 73 71 6c 20 7b 20 53 45 4c 45 43 54 20  xecsql { SELECT 
ddd0: 2a 20 46 52 4f 4d 20 6c 6f 67 20 7d 0a 7d 20 7b  * FROM log }.} {
dde0: 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79  }.do_test e_fkey
ddf0: 2d 38 2e 36 20 7b 0a 20 20 65 78 65 63 73 71 6c  -8.6 {.  execsql
de00: 20 52 4f 4c 4c 42 41 43 4b 0a 7d 20 7b 7d 0a 64   ROLLBACK.} {}.d
de10: 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 38 2e  o_test e_fkey-8.
de20: 37 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a  7 {.  execsql {.
de30: 20 20 20 20 42 45 47 49 4e 3b 0a 20 20 20 20 20      BEGIN;.     
de40: 20 44 45 4c 45 54 45 20 46 52 4f 4d 20 70 3b 0a   DELETE FROM p;.
de50: 20 20 20 20 20 20 53 45 4c 45 43 54 20 2a 20 46        SELECT * F
de60: 52 4f 4d 20 6c 6f 67 3b 0a 20 20 20 20 52 4f 4c  ROM log;.    ROL
de70: 4c 42 41 43 4b 3b 0a 20 20 7d 0a 7d 20 7b 7b 64  LBACK;.  }.} {{d
de80: 65 6c 65 74 65 20 31 7d 7d 0a 0a 23 2d 2d 2d 2d  elete 1}}..#----
de90: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
dea0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
deb0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
dec0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
ded0: 2d 2d 2d 2d 2d 0a 23 20 2f 2a 20 45 56 3a 20 52  -----.# /* EV: R
dee0: 2d 33 32 37 36 38 2d 34 37 39 32 35 20 2a 2f 0a  -32768-47925 */.
def0: 23 0a 23 20 49 66 20 61 6e 20 49 4d 4d 45 44 49  #.# If an IMMEDI
df00: 41 54 45 20 66 6f 72 65 69 67 6e 20 6b 65 79 20  ATE foreign key 
df10: 66 61 69 6c 73 20 61 73 20 61 20 72 65 73 75 6c  fails as a resul
df20: 74 20 6f 66 20 61 20 44 52 4f 50 20 54 41 42 4c  t of a DROP TABL
df30: 45 2c 20 74 68 65 0a 23 20 44 52 4f 50 20 54 41  E, the.# DROP TA
df40: 42 4c 45 20 63 6f 6d 6d 61 6e 64 20 66 61 69 6c  BLE command fail
df50: 73 2e 0a 23 0a 64 6f 5f 74 65 73 74 20 65 5f 66  s..#.do_test e_f
df60: 6b 65 79 2d 39 2e 31 20 7b 0a 20 20 65 78 65 63  key-9.1 {.  exec
df70: 73 71 6c 20 7b 20 0a 20 20 20 20 44 45 4c 45 54  sql { .    DELET
df80: 45 20 46 52 4f 4d 20 63 31 3b 0a 20 20 20 20 44  E FROM c1;.    D
df90: 45 4c 45 54 45 20 46 52 4f 4d 20 63 32 3b 0a 20  ELETE FROM c2;. 
dfa0: 20 20 20 44 45 4c 45 54 45 20 46 52 4f 4d 20 63     DELETE FROM c
dfb0: 33 3b 0a 20 20 7d 0a 20 20 65 78 65 63 73 71 6c  3;.  }.  execsql
dfc0: 20 7b 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 63   { INSERT INTO c
dfd0: 35 20 56 41 4c 55 45 53 28 27 61 27 2c 20 27 62  5 VALUES('a', 'b
dfe0: 27 29 20 7d 0a 20 20 63 61 74 63 68 73 71 6c 20  ') }.  catchsql 
dff0: 7b 20 44 52 4f 50 20 54 41 42 4c 45 20 70 20 7d  { DROP TABLE p }
e000: 0a 7d 20 7b 31 20 7b 66 6f 72 65 69 67 6e 20 6b  .} {1 {foreign k
e010: 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 20 66 61  ey constraint fa
e020: 69 6c 65 64 7d 7d 0a 64 6f 5f 74 65 73 74 20 65  iled}}.do_test e
e030: 5f 66 6b 65 79 2d 39 2e 32 20 7b 0a 20 20 65 78  _fkey-9.2 {.  ex
e040: 65 63 73 71 6c 20 7b 20 53 45 4c 45 43 54 20 2a  ecsql { SELECT *
e050: 20 46 52 4f 4d 20 70 20 7d 0a 7d 20 7b 61 20 62   FROM p }.} {a b
e060: 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79  }.do_test e_fkey
e070: 2d 39 2e 33 20 7b 0a 20 20 63 61 74 63 68 73 71  -9.3 {.  catchsq
e080: 6c 20 7b 0a 20 20 20 20 42 45 47 49 4e 3b 0a 20  l {.    BEGIN;. 
e090: 20 20 20 20 20 44 52 4f 50 20 54 41 42 4c 45 20       DROP TABLE 
e0a0: 70 3b 0a 20 20 7d 0a 7d 20 7b 31 20 7b 66 6f 72  p;.  }.} {1 {for
e0b0: 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61  eign key constra
e0c0: 69 6e 74 20 66 61 69 6c 65 64 7d 7d 0a 64 6f 5f  int failed}}.do_
e0d0: 74 65 73 74 20 65 5f 66 6b 65 79 2d 39 2e 34 20  test e_fkey-9.4 
e0e0: 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20  {.  execsql {.  
e0f0: 20 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20    SELECT * FROM 
e100: 70 3b 0a 20 20 20 20 53 45 4c 45 43 54 20 2a 20  p;.    SELECT * 
e110: 46 52 4f 4d 20 63 35 3b 0a 20 20 20 20 52 4f 4c  FROM c5;.    ROL
e120: 4c 42 41 43 4b 3b 0a 20 20 7d 0a 7d 20 7b 61 20  LBACK;.  }.} {a 
e130: 62 20 61 20 62 7d 0a 0a 23 2d 2d 2d 2d 2d 2d 2d  b a b}..#-------
e140: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
e150: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
e160: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
e170: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
e180: 2d 2d 0a 23 20 2f 2a 20 45 56 3a 20 52 2d 30 35  --.# /* EV: R-05
e190: 39 30 33 2d 30 38 34 36 30 20 2a 2f 0a 23 0a 23  903-08460 */.#.#
e1a0: 20 49 66 20 61 20 44 45 46 45 52 52 45 44 20 66   If a DEFERRED f
e1b0: 6f 72 65 69 67 6e 20 6b 65 79 20 66 61 69 6c 73  oreign key fails
e1c0: 20 61 73 20 61 20 72 65 73 75 6c 74 20 6f 66 20   as a result of 
e1d0: 61 20 44 52 4f 50 20 54 41 42 4c 45 2c 20 61 74  a DROP TABLE, at
e1e0: 74 65 6d 70 74 69 6e 67 0a 23 20 74 6f 20 63 6f  tempting.# to co
e1f0: 6d 6d 69 74 20 74 68 65 20 74 72 61 6e 73 61 63  mmit the transac
e200: 74 69 6f 6e 20 66 61 69 6c 73 20 75 6e 6c 65 73  tion fails unles
e210: 73 20 74 68 65 20 76 69 6f 6c 61 74 69 6f 6e 20  s the violation 
e220: 69 73 20 66 69 78 65 64 2e 0a 23 0a 64 6f 5f 74  is fixed..#.do_t
e230: 65 73 74 20 65 5f 66 6b 65 79 2d 31 30 2e 31 20  est e_fkey-10.1 
e240: 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 20 0a 20  {.  execsql { . 
e250: 20 20 20 44 45 4c 45 54 45 20 46 52 4f 4d 20 63     DELETE FROM c
e260: 31 20 3b 20 44 45 4c 45 54 45 20 46 52 4f 4d 20  1 ; DELETE FROM 
e270: 63 32 20 3b 20 44 45 4c 45 54 45 20 46 52 4f 4d  c2 ; DELETE FROM
e280: 20 63 33 20 3b 0a 20 20 20 20 44 45 4c 45 54 45   c3 ;.    DELETE
e290: 20 46 52 4f 4d 20 63 34 20 3b 20 44 45 4c 45 54   FROM c4 ; DELET
e2a0: 45 20 46 52 4f 4d 20 63 35 20 3b 20 44 45 4c 45  E FROM c5 ; DELE
e2b0: 54 45 20 46 52 4f 4d 20 63 36 20 3b 0a 20 20 20  TE FROM c6 ;.   
e2c0: 20 44 45 4c 45 54 45 20 46 52 4f 4d 20 63 37 20   DELETE FROM c7 
e2d0: 0a 20 20 7d 0a 7d 20 7b 7d 0a 64 6f 5f 74 65 73  .  }.} {}.do_tes
e2e0: 74 20 65 5f 66 6b 65 79 2d 31 30 2e 32 20 7b 0a  t e_fkey-10.2 {.
e2f0: 20 20 65 78 65 63 73 71 6c 20 7b 20 49 4e 53 45    execsql { INSE
e300: 52 54 20 49 4e 54 4f 20 63 37 20 56 41 4c 55 45  RT INTO c7 VALUE
e310: 53 28 27 61 27 2c 20 27 62 27 29 20 7d 0a 20 20  S('a', 'b') }.  
e320: 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 42 45  execsql {.    BE
e330: 47 49 4e 3b 0a 20 20 20 20 20 20 44 52 4f 50 20  GIN;.      DROP 
e340: 54 41 42 4c 45 20 70 3b 0a 20 20 7d 0a 7d 20 7b  TABLE p;.  }.} {
e350: 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79  }.do_test e_fkey
e360: 2d 31 30 2e 33 20 7b 0a 20 20 63 61 74 63 68 73  -10.3 {.  catchs
e370: 71 6c 20 43 4f 4d 4d 49 54 0a 7d 20 7b 31 20 7b  ql COMMIT.} {1 {
e380: 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73  foreign key cons
e390: 74 72 61 69 6e 74 20 66 61 69 6c 65 64 7d 7d 0a  traint failed}}.
e3a0: 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 31  do_test e_fkey-1
e3b0: 30 2e 34 20 7b 0a 20 20 65 78 65 63 73 71 6c 20  0.4 {.  execsql 
e3c0: 7b 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 70  { CREATE TABLE p
e3d0: 28 61 2c 20 62 2c 20 50 52 49 4d 41 52 59 20 4b  (a, b, PRIMARY K
e3e0: 45 59 28 61 2c 20 62 29 29 20 7d 0a 20 20 63 61  EY(a, b)) }.  ca
e3f0: 74 63 68 73 71 6c 20 43 4f 4d 4d 49 54 0a 7d 20  tchsql COMMIT.} 
e400: 7b 31 20 7b 66 6f 72 65 69 67 6e 20 6b 65 79 20  {1 {foreign key 
e410: 63 6f 6e 73 74 72 61 69 6e 74 20 66 61 69 6c 65  constraint faile
e420: 64 7d 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b  d}}.do_test e_fk
e430: 65 79 2d 31 30 2e 35 20 7b 0a 20 20 65 78 65 63  ey-10.5 {.  exec
e440: 73 71 6c 20 7b 20 49 4e 53 45 52 54 20 49 4e 54  sql { INSERT INT
e450: 4f 20 70 20 56 41 4c 55 45 53 28 27 61 27 2c 20  O p VALUES('a', 
e460: 27 62 27 29 20 7d 0a 20 20 65 78 65 63 73 71 6c  'b') }.  execsql
e470: 20 43 4f 4d 4d 49 54 0a 7d 20 7b 7d 0a 0a 23 2d   COMMIT.} {}..#-
e480: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
e490: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
e4a0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
e4b0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
e4c0: 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20 2f 2a 20 45 56  --------.# /* EV
e4d0: 3a 20 52 2d 35 37 32 34 32 2d 33 37 30 30 35 20  : R-57242-37005 
e4e0: 2a 2f 0a 23 0a 23 20 41 6e 79 20 22 66 6f 72 65  */.#.# Any "fore
e4f0: 69 67 6e 20 6b 65 79 20 6d 69 73 6d 61 74 63 68  ign key mismatch
e500: 22 20 65 72 72 6f 72 73 20 65 6e 63 6f 75 6e 74  " errors encount
e510: 65 72 65 64 20 77 68 69 6c 65 20 72 75 6e 6e 69  ered while runni
e520: 6e 67 20 61 6e 20 69 6d 70 6c 69 63 69 74 0a 23  ng an implicit.#
e530: 20 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 74 62   "DELETE FROM tb
e540: 6c 22 20 61 72 65 20 69 67 6e 6f 72 65 64 2e 0a  l" are ignored..
e550: 23 0a 64 72 6f 70 5f 61 6c 6c 5f 74 61 62 6c 65  #.drop_all_table
e560: 73 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79  s.do_test e_fkey
e570: 2d 31 31 2e 31 20 7b 0a 20 20 65 78 65 63 73 71  -11.1 {.  execsq
e580: 6c 20 7b 0a 20 20 20 20 50 52 41 47 4d 41 20 66  l {.    PRAGMA f
e590: 6f 72 65 69 67 6e 5f 6b 65 79 73 20 3d 20 4f 46  oreign_keys = OF
e5a0: 46 3b 0a 0a 20 20 20 20 43 52 45 41 54 45 20 54  F;..    CREATE T
e5b0: 41 42 4c 45 20 70 28 61 20 50 52 49 4d 41 52 59  ABLE p(a PRIMARY
e5c0: 20 4b 45 59 2c 20 62 20 52 45 46 45 52 45 4e 43   KEY, b REFERENC
e5d0: 45 53 20 6e 6f 73 75 63 68 74 61 62 6c 65 29 3b  ES nosuchtable);
e5e0: 0a 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c  .    CREATE TABL
e5f0: 45 20 63 31 28 63 2c 20 64 2c 20 46 4f 52 45 49  E c1(c, d, FOREI
e600: 47 4e 20 4b 45 59 28 63 2c 20 64 29 20 52 45 46  GN KEY(c, d) REF
e610: 45 52 45 4e 43 45 53 20 61 29 3b 0a 20 20 20 20  ERENCES a);.    
e620: 43 52 45 41 54 45 20 54 41 42 4c 45 20 63 32 28  CREATE TABLE c2(
e630: 63 20 52 45 46 45 52 45 4e 43 45 53 20 70 28 62  c REFERENCES p(b
e640: 29 2c 20 64 29 3b 0a 20 20 20 20 43 52 45 41 54  ), d);.    CREAT
e650: 45 20 54 41 42 4c 45 20 63 33 28 63 20 52 45 46  E TABLE c3(c REF
e660: 45 52 45 4e 43 45 53 20 70 20 4f 4e 20 44 45 4c  ERENCES p ON DEL
e670: 45 54 45 20 53 45 54 20 4e 55 4c 4c 2c 20 64 29  ETE SET NULL, d)
e680: 3b 0a 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e  ;..    INSERT IN
e690: 54 4f 20 70 20 56 41 4c 55 45 53 28 31 2c 20 32  TO p VALUES(1, 2
e6a0: 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e  );.    INSERT IN
e6b0: 54 4f 20 63 31 20 56 41 4c 55 45 53 28 31 2c 20  TO c1 VALUES(1, 
e6c0: 32 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49  2);.    INSERT I
e6d0: 4e 54 4f 20 63 32 20 56 41 4c 55 45 53 28 31 2c  NTO c2 VALUES(1,
e6e0: 20 32 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20   2);.    INSERT 
e6f0: 49 4e 54 4f 20 63 33 20 56 41 4c 55 45 53 28 31  INTO c3 VALUES(1
e700: 2c 20 32 29 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a 64  , 2);.  }.} {}.d
e710: 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 31 31  o_test e_fkey-11
e720: 2e 32 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b  .2 {.  execsql {
e730: 20 50 52 41 47 4d 41 20 66 6f 72 65 69 67 6e 5f   PRAGMA foreign_
e740: 6b 65 79 73 20 3d 20 4f 4e 20 7d 0a 20 20 63 61  keys = ON }.  ca
e750: 74 63 68 73 71 6c 20 7b 20 44 45 4c 45 54 45 20  tchsql { DELETE 
e760: 46 52 4f 4d 20 70 20 7d 0a 7d 20 7b 31 20 7b 6e  FROM p }.} {1 {n
e770: 6f 20 73 75 63 68 20 74 61 62 6c 65 3a 20 6d 61  o such table: ma
e780: 69 6e 2e 6e 6f 73 75 63 68 74 61 62 6c 65 7d 7d  in.nosuchtable}}
e790: 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d  .do_test e_fkey-
e7a0: 31 31 2e 33 20 7b 0a 20 20 65 78 65 63 73 71 6c  11.3 {.  execsql
e7b0: 20 7b 0a 20 20 20 20 42 45 47 49 4e 3b 0a 20 20   {.    BEGIN;.  
e7c0: 20 20 20 20 44 52 4f 50 20 54 41 42 4c 45 20 70      DROP TABLE p
e7d0: 3b 0a 20 20 20 20 20 20 53 45 4c 45 43 54 20 2a  ;.      SELECT *
e7e0: 20 46 52 4f 4d 20 63 33 3b 0a 20 20 20 20 52 4f   FROM c3;.    RO
e7f0: 4c 4c 42 41 43 4b 3b 0a 20 20 7d 0a 7d 20 7b 7b  LLBACK;.  }.} {{
e800: 7d 20 32 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66  } 2}.do_test e_f
e810: 6b 65 79 2d 31 31 2e 34 20 7b 0a 20 20 65 78 65  key-11.4 {.  exe
e820: 63 73 71 6c 20 7b 20 43 52 45 41 54 45 20 54 41  csql { CREATE TA
e830: 42 4c 45 20 6e 6f 73 75 63 68 74 61 62 6c 65 28  BLE nosuchtable(
e840: 78 20 50 52 49 4d 41 52 59 20 4b 45 59 29 20 7d  x PRIMARY KEY) }
e850: 0a 20 20 63 61 74 63 68 73 71 6c 20 7b 20 44 45  .  catchsql { DE
e860: 4c 45 54 45 20 46 52 4f 4d 20 70 20 7d 0a 7d 20  LETE FROM p }.} 
e870: 7b 31 20 7b 66 6f 72 65 69 67 6e 20 6b 65 79 20  {1 {foreign key 
e880: 6d 69 73 6d 61 74 63 68 7d 7d 0a 64 6f 5f 74 65  mismatch}}.do_te
e890: 73 74 20 65 5f 66 6b 65 79 2d 31 31 2e 35 20 7b  st e_fkey-11.5 {
e8a0: 0a 20 20 65 78 65 63 73 71 6c 20 7b 20 44 52 4f  .  execsql { DRO
e8b0: 50 20 54 41 42 4c 45 20 63 31 20 7d 0a 20 20 63  P TABLE c1 }.  c
e8c0: 61 74 63 68 73 71 6c 20 7b 20 44 45 4c 45 54 45  atchsql { DELETE
e8d0: 20 46 52 4f 4d 20 70 20 7d 0a 7d 20 7b 31 20 7b   FROM p }.} {1 {
e8e0: 66 6f 72 65 69 67 6e 20 6b 65 79 20 6d 69 73 6d  foreign key mism
e8f0: 61 74 63 68 7d 7d 0a 64 6f 5f 74 65 73 74 20 65  atch}}.do_test e
e900: 5f 66 6b 65 79 2d 31 31 2e 36 20 7b 0a 20 20 65  _fkey-11.6 {.  e
e910: 78 65 63 73 71 6c 20 7b 20 44 52 4f 50 20 54 41  xecsql { DROP TA
e920: 42 4c 45 20 63 32 20 7d 0a 20 20 65 78 65 63 73  BLE c2 }.  execs
e930: 71 6c 20 7b 20 44 45 4c 45 54 45 20 46 52 4f 4d  ql { DELETE FROM
e940: 20 70 20 7d 0a 7d 20 7b 7d 0a 0a 23 2d 2d 2d 2d   p }.} {}..#----
e950: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
e960: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
e970: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
e980: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
e990: 2d 2d 2d 2d 2d 0a 23 20 2f 2a 20 45 56 3a 20 52  -----.# /* EV: R
e9a0: 2d 35 34 31 34 32 2d 34 31 33 34 36 20 2a 2f 0a  -54142-41346 */.
e9b0: 23 0a 23 20 54 65 73 74 20 74 68 61 74 20 74 68  #.# Test that th
e9c0: 65 20 73 70 65 63 69 61 6c 20 62 65 68 61 76 69  e special behavi
e9d0: 6f 75 72 73 20 6f 66 20 41 4c 54 45 52 20 61 6e  ours of ALTER an
e9e0: 64 20 44 52 4f 50 20 54 41 42 4c 45 20 61 72 65  d DROP TABLE are
e9f0: 20 6f 6e 6c 79 0a 23 20 61 63 74 69 76 61 74 65   only.# activate
ea00: 64 20 77 68 65 6e 20 66 6f 72 65 69 67 6e 20 6b  d when foreign k
ea10: 65 79 73 20 61 72 65 20 65 6e 61 62 6c 65 64 2e  eys are enabled.
ea20: 20 53 70 65 63 69 61 6c 20 62 65 68 61 76 69 6f   Special behavio
ea30: 75 72 73 20 61 72 65 3a 0a 23 0a 23 20 20 20 31  urs are:.#.#   1
ea40: 2e 20 41 44 44 20 43 4f 4c 55 4d 4e 20 6e 6f 74  . ADD COLUMN not
ea50: 20 61 6c 6c 6f 77 69 6e 67 20 61 20 52 45 46 45   allowing a REFE
ea60: 52 45 4e 43 45 53 20 63 6c 61 75 73 65 20 77 69  RENCES clause wi
ea70: 74 68 20 61 20 6e 6f 6e 2d 4e 55 4c 4c 20 0a 23  th a non-NULL .#
ea80: 20 20 20 20 20 20 64 65 66 61 75 6c 74 20 76 61        default va
ea90: 6c 75 65 2e 0a 23 20 20 20 32 2e 20 4d 6f 64 69  lue..#   2. Modi
eaa0: 66 79 69 6e 67 20 66 6f 72 65 69 67 6e 20 6b 65  fying foreign ke
eab0: 79 20 64 65 66 69 6e 69 74 69 6f 6e 73 20 77 68  y definitions wh
eac0: 65 6e 20 61 20 70 61 72 65 6e 74 20 74 61 62 6c  en a parent tabl
ead0: 65 20 69 73 20 52 45 4e 41 4d 45 64 2e 0a 23 20  e is RENAMEd..# 
eae0: 20 20 33 2e 20 52 75 6e 6e 69 6e 67 20 61 6e 20    3. Running an 
eaf0: 69 6d 70 6c 69 63 69 74 20 44 45 4c 45 54 45 20  implicit DELETE 
eb00: 46 52 4f 4d 20 63 6f 6d 6d 61 6e 64 20 61 73 20  FROM command as 
eb10: 70 61 72 74 20 6f 66 20 44 52 4f 50 20 54 41 42  part of DROP TAB
eb20: 4c 45 2e 0a 23 0a 64 6f 5f 74 65 73 74 20 65 5f  LE..#.do_test e_
eb30: 66 6b 65 79 2d 31 32 2e 31 2e 31 20 7b 0a 20 20  fkey-12.1.1 {.  
eb40: 64 72 6f 70 5f 61 6c 6c 5f 74 61 62 6c 65 73 0a  drop_all_tables.
eb50: 20 20 65 78 65 63 73 71 6c 20 7b 20 43 52 45 41    execsql { CREA
eb60: 54 45 20 54 41 42 4c 45 20 74 31 28 61 2c 20 62  TE TABLE t1(a, b
eb70: 29 20 7d 0a 20 20 63 61 74 63 68 73 71 6c 20 7b  ) }.  catchsql {
eb80: 20 41 4c 54 45 52 20 54 41 42 4c 45 20 74 31 20   ALTER TABLE t1 
eb90: 41 44 44 20 43 4f 4c 55 4d 4e 20 63 20 44 45 46  ADD COLUMN c DEF
eba0: 41 55 4c 54 20 27 78 78 78 27 20 52 45 46 45 52  AULT 'xxx' REFER
ebb0: 45 4e 43 45 53 20 74 32 20 7d 0a 7d 20 7b 31 20  ENCES t2 }.} {1 
ebc0: 7b 43 61 6e 6e 6f 74 20 61 64 64 20 61 20 52 45  {Cannot add a RE
ebd0: 46 45 52 45 4e 43 45 53 20 63 6f 6c 75 6d 6e 20  FERENCES column 
ebe0: 77 69 74 68 20 6e 6f 6e 2d 4e 55 4c 4c 20 64 65  with non-NULL de
ebf0: 66 61 75 6c 74 20 76 61 6c 75 65 7d 7d 0a 64 6f  fault value}}.do
ec00: 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 31 32 2e  _test e_fkey-12.
ec10: 31 2e 32 20 7b 0a 20 20 65 78 65 63 73 71 6c 20  1.2 {.  execsql 
ec20: 7b 20 50 52 41 47 4d 41 20 66 6f 72 65 69 67 6e  { PRAGMA foreign
ec30: 5f 6b 65 79 73 20 3d 20 4f 46 46 20 7d 0a 20 20  _keys = OFF }.  
ec40: 65 78 65 63 73 71 6c 20 7b 20 41 4c 54 45 52 20  execsql { ALTER 
ec50: 54 41 42 4c 45 20 74 31 20 41 44 44 20 43 4f 4c  TABLE t1 ADD COL
ec60: 55 4d 4e 20 63 20 44 45 46 41 55 4c 54 20 27 78  UMN c DEFAULT 'x
ec70: 78 78 27 20 52 45 46 45 52 45 4e 43 45 53 20 74  xx' REFERENCES t
ec80: 32 20 7d 0a 20 20 65 78 65 63 73 71 6c 20 7b 20  2 }.  execsql { 
ec90: 53 45 4c 45 43 54 20 73 71 6c 20 46 52 4f 4d 20  SELECT sql FROM 
eca0: 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 57 48  sqlite_master WH
ecb0: 45 52 45 20 6e 61 6d 65 20 3d 20 27 74 31 27 20  ERE name = 't1' 
ecc0: 7d 0a 7d 20 7b 7b 43 52 45 41 54 45 20 54 41 42  }.} {{CREATE TAB
ecd0: 4c 45 20 74 31 28 61 2c 20 62 2c 20 63 20 44 45  LE t1(a, b, c DE
ece0: 46 41 55 4c 54 20 27 78 78 78 27 20 52 45 46 45  FAULT 'xxx' REFE
ecf0: 52 45 4e 43 45 53 20 74 32 29 7d 7d 0a 64 6f 5f  RENCES t2)}}.do_
ed00: 74 65 73 74 20 65 5f 66 6b 65 79 2d 31 32 2e 31  test e_fkey-12.1
ed10: 2e 33 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b  .3 {.  execsql {
ed20: 20 50 52 41 47 4d 41 20 66 6f 72 65 69 67 6e 5f   PRAGMA foreign_
ed30: 6b 65 79 73 20 3d 20 4f 4e 20 7d 0a 7d 20 7b 7d  keys = ON }.} {}
ed40: 0a 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79  ..do_test e_fkey
ed50: 2d 31 32 2e 32 2e 31 20 7b 0a 20 20 64 72 6f 70  -12.2.1 {.  drop
ed60: 5f 61 6c 6c 5f 74 61 62 6c 65 73 0a 20 20 65 78  _all_tables.  ex
ed70: 65 63 73 71 6c 20 7b 0a 20 20 20 20 43 52 45 41  ecsql {.    CREA
ed80: 54 45 20 54 41 42 4c 45 20 70 28 61 20 55 4e 49  TE TABLE p(a UNI
ed90: 51 55 45 29 3b 0a 20 20 20 20 43 52 45 41 54 45  QUE);.    CREATE
eda0: 20 54 41 42 4c 45 20 63 28 62 20 52 45 46 45 52   TABLE c(b REFER
edb0: 45 4e 43 45 53 20 70 28 61 29 29 3b 0a 20 20 20  ENCES p(a));.   
edc0: 20 42 45 47 49 4e 3b 0a 20 20 20 20 20 20 41 4c   BEGIN;.      AL
edd0: 54 45 52 20 54 41 42 4c 45 20 70 20 52 45 4e 41  TER TABLE p RENA
ede0: 4d 45 20 54 4f 20 70 61 72 65 6e 74 3b 0a 20 20  ME TO parent;.  
edf0: 20 20 20 20 53 45 4c 45 43 54 20 73 71 6c 20 46      SELECT sql F
ee00: 52 4f 4d 20 73 71 6c 69 74 65 5f 6d 61 73 74 65  ROM sqlite_maste
ee10: 72 20 57 48 45 52 45 20 6e 61 6d 65 20 3d 20 27  r WHERE name = '
ee20: 63 27 3b 0a 20 20 20 20 52 4f 4c 4c 42 41 43 4b  c';.    ROLLBACK
ee30: 3b 0a 20 20 7d 0a 7d 20 7b 7b 43 52 45 41 54 45  ;.  }.} {{CREATE
ee40: 20 54 41 42 4c 45 20 63 28 62 20 52 45 46 45 52   TABLE c(b REFER
ee50: 45 4e 43 45 53 20 22 70 61 72 65 6e 74 22 28 61  ENCES "parent"(a
ee60: 29 29 7d 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66  ))}}.do_test e_f
ee70: 6b 65 79 2d 31 32 2e 32 2e 32 20 7b 0a 20 20 65  key-12.2.2 {.  e
ee80: 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 50 52 41  xecsql {.    PRA
ee90: 47 4d 41 20 66 6f 72 65 69 67 6e 5f 6b 65 79 73  GMA foreign_keys
eea0: 20 3d 20 4f 46 46 3b 0a 20 20 20 20 41 4c 54 45   = OFF;.    ALTE
eeb0: 52 20 54 41 42 4c 45 20 70 20 52 45 4e 41 4d 45  R TABLE p RENAME
eec0: 20 54 4f 20 70 61 72 65 6e 74 3b 0a 20 20 20 20   TO parent;.    
eed0: 53 45 4c 45 43 54 20 73 71 6c 20 46 52 4f 4d 20  SELECT sql FROM 
eee0: 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 57 48  sqlite_master WH
eef0: 45 52 45 20 6e 61 6d 65 20 3d 20 27 63 27 3b 0a  ERE name = 'c';.
ef00: 20 20 7d 0a 7d 20 7b 7b 43 52 45 41 54 45 20 54    }.} {{CREATE T
ef10: 41 42 4c 45 20 63 28 62 20 52 45 46 45 52 45 4e  ABLE c(b REFEREN
ef20: 43 45 53 20 70 28 61 29 29 7d 7d 0a 64 6f 5f 74  CES p(a))}}.do_t
ef30: 65 73 74 20 65 5f 66 6b 65 79 2d 31 32 2e 32 2e  est e_fkey-12.2.
ef40: 33 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 20  3 {.  execsql { 
ef50: 50 52 41 47 4d 41 20 66 6f 72 65 69 67 6e 5f 6b  PRAGMA foreign_k
ef60: 65 79 73 20 3d 20 4f 4e 20 7d 0a 7d 20 7b 7d 0a  eys = ON }.} {}.
ef70: 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d  .do_test e_fkey-
ef80: 31 32 2e 33 2e 31 20 7b 0a 20 20 64 72 6f 70 5f  12.3.1 {.  drop_
ef90: 61 6c 6c 5f 74 61 62 6c 65 73 0a 20 20 65 78 65  all_tables.  exe
efa0: 63 73 71 6c 20 7b 0a 20 20 20 20 43 52 45 41 54  csql {.    CREAT
efb0: 45 20 54 41 42 4c 45 20 70 28 61 20 55 4e 49 51  E TABLE p(a UNIQ
efc0: 55 45 29 3b 0a 20 20 20 20 43 52 45 41 54 45 20  UE);.    CREATE 
efd0: 54 41 42 4c 45 20 63 28 62 20 52 45 46 45 52 45  TABLE c(b REFERE
efe0: 4e 43 45 53 20 70 28 61 29 20 4f 4e 20 44 45 4c  NCES p(a) ON DEL
eff0: 45 54 45 20 53 45 54 20 4e 55 4c 4c 29 3b 0a 20  ETE SET NULL);. 
f000: 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 70     INSERT INTO p
f010: 20 56 41 4c 55 45 53 28 27 78 27 29 3b 0a 20 20   VALUES('x');.  
f020: 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 63 20    INSERT INTO c 
f030: 56 41 4c 55 45 53 28 27 78 27 29 3b 0a 20 20 20  VALUES('x');.   
f040: 20 42 45 47 49 4e 3b 0a 20 20 20 20 20 20 44 52   BEGIN;.      DR
f050: 4f 50 20 54 41 42 4c 45 20 70 3b 0a 20 20 20 20  OP TABLE p;.    
f060: 20 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20    SELECT * FROM 
f070: 63 3b 0a 20 20 20 20 52 4f 4c 4c 42 41 43 4b 3b  c;.    ROLLBACK;
f080: 0a 20 20 7d 0a 7d 20 7b 7b 7d 7d 0a 64 6f 5f 74  .  }.} {{}}.do_t
f090: 65 73 74 20 65 5f 66 6b 65 79 2d 31 32 2e 33 2e  est e_fkey-12.3.
f0a0: 32 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a  2 {.  execsql {.
f0b0: 20 20 20 20 50 52 41 47 4d 41 20 66 6f 72 65 69      PRAGMA forei
f0c0: 67 6e 5f 6b 65 79 73 20 3d 20 4f 46 46 3b 0a 20  gn_keys = OFF;. 
f0d0: 20 20 20 44 52 4f 50 20 54 41 42 4c 45 20 70 3b     DROP TABLE p;
f0e0: 0a 20 20 20 20 53 45 4c 45 43 54 20 2a 20 46 52  .    SELECT * FR
f0f0: 4f 4d 20 63 3b 0a 20 20 7d 0a 7d 20 7b 78 7d 0a  OM c;.  }.} {x}.
f100: 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 31  do_test e_fkey-1
f110: 32 2e 33 2e 33 20 7b 0a 20 20 65 78 65 63 73 71  2.3.3 {.  execsq
f120: 6c 20 7b 20 50 52 41 47 4d 41 20 66 6f 72 65 69  l { PRAGMA forei
f130: 67 6e 5f 6b 65 79 73 20 3d 20 4f 4e 20 7d 0a 7d  gn_keys = ON }.}
f140: 20 7b 7d 0a 0a 23 23 23 23 23 23 23 23 23 23 23   {}..###########
f150: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
f160: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
f170: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
f180: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
f190: 0a 23 23 23 20 53 45 43 54 49 4f 4e 20 36 3a 20  .### SECTION 6: 
f1a0: 4c 69 6d 69 74 73 20 61 6e 64 20 55 6e 73 75 70  Limits and Unsup
f1b0: 70 6f 72 74 65 64 20 46 65 61 74 75 72 65 73 0a  ported Features.
f1c0: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
f1d0: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
f1e0: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
f1f0: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
f200: 23 23 23 23 23 23 23 23 23 23 23 0a 0a 23 2d 2d  ###########..#--
f210: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
f220: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
f230: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
f240: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
f250: 2d 2d 2d 2d 2d 2d 2d 0a 23 20 2f 2a 20 45 56 3a  -------.# /* EV:
f260: 20 52 2d 32 34 37 32 38 2d 31 33 32 33 30 20 2a   R-24728-13230 *
f270: 2f 0a 23 20 2f 2a 20 45 56 3a 20 52 2d 32 34 34  /.# /* EV: R-244
f280: 35 30 2d 34 36 31 37 34 20 2a 2f 0a 23 0a 23 20  50-46174 */.#.# 
f290: 54 65 73 74 20 74 68 61 74 20 4d 41 54 43 48 20  Test that MATCH 
f2a0: 63 6c 61 75 73 65 73 20 61 72 65 20 70 61 72 73  clauses are pars
f2b0: 65 64 2c 20 62 75 74 20 53 51 4c 69 74 65 20 74  ed, but SQLite t
f2c0: 72 65 61 74 73 20 65 76 65 72 79 20 66 6f 72 65  reats every fore
f2d0: 69 67 6e 20 6b 65 79 0a 23 20 63 6f 6e 73 74 72  ign key.# constr
f2e0: 61 69 6e 74 20 61 73 20 69 66 20 69 74 20 77 65  aint as if it we
f2f0: 72 65 20 22 4d 41 54 43 48 20 53 49 4d 50 4c 45  re "MATCH SIMPLE
f300: 22 2e 0a 23 0a 66 6f 72 65 61 63 68 20 7a 4d 61  "..#.foreach zMa
f310: 74 63 68 20 5b 6c 69 73 74 20 53 49 4d 50 4c 45  tch [list SIMPLE
f320: 20 50 41 52 54 49 41 4c 20 46 55 4c 4c 20 53 69   PARTIAL FULL Si
f330: 6d 70 6c 65 20 70 61 72 54 49 41 4c 20 46 75 4c  mple parTIAL FuL
f340: 4c 20 5d 20 7b 0a 20 20 64 72 6f 70 5f 61 6c 6c  L ] {.  drop_all
f350: 5f 74 61 62 6c 65 73 0a 20 20 64 6f 5f 74 65 73  _tables.  do_tes
f360: 74 20 65 5f 66 6b 65 79 2d 31 2e 24 7a 4d 61 74  t e_fkey-1.$zMat
f370: 63 68 2e 31 20 7b 0a 20 20 20 20 65 78 65 63 73  ch.1 {.    execs
f380: 71 6c 20 22 0a 20 20 20 20 20 20 43 52 45 41 54  ql ".      CREAT
f390: 45 20 54 41 42 4c 45 20 70 28 61 2c 20 62 2c 20  E TABLE p(a, b, 
f3a0: 63 2c 20 50 52 49 4d 41 52 59 20 4b 45 59 28 62  c, PRIMARY KEY(b
f3b0: 2c 20 63 29 29 3b 0a 20 20 20 20 20 20 43 52 45  , c));.      CRE
f3c0: 41 54 45 20 54 41 42 4c 45 20 63 28 64 2c 20 65  ATE TABLE c(d, e
f3d0: 2c 20 66 2c 20 46 4f 52 45 49 47 4e 20 4b 45 59  , f, FOREIGN KEY
f3e0: 28 65 2c 20 66 29 20 52 45 46 45 52 45 4e 43 45  (e, f) REFERENCE
f3f0: 53 20 70 20 4d 41 54 43 48 20 24 7a 4d 61 74 63  S p MATCH $zMatc
f400: 68 29 3b 0a 20 20 20 20 22 0a 20 20 7d 20 7b 7d  h);.    ".  } {}
f410: 0a 20 20 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65  .  do_test e_fke
f420: 79 2d 31 2e 24 7a 4d 61 74 63 68 2e 32 20 7b 0a  y-1.$zMatch.2 {.
f430: 20 20 20 20 65 78 65 63 73 71 6c 20 7b 20 49 4e      execsql { IN
f440: 53 45 52 54 20 49 4e 54 4f 20 70 20 56 41 4c 55  SERT INTO p VALU
f450: 45 53 28 31 2c 20 32 2c 20 33 29 20 20 20 20 20  ES(1, 2, 3)     
f460: 20 20 20 20 7d 0a 0a 20 20 20 20 23 20 4d 41 54      }..    # MAT
f470: 43 48 20 53 49 4d 50 4c 45 20 62 65 68 61 76 69  CH SIMPLE behavi
f480: 6f 75 72 3a 20 41 6c 6c 6f 77 20 61 6e 79 20 63  our: Allow any c
f490: 68 69 6c 64 20 6b 65 79 20 74 68 61 74 20 63 6f  hild key that co
f4a0: 6e 74 61 69 6e 73 20 6f 6e 65 20 6f 72 20 6d 6f  ntains one or mo
f4b0: 72 65 0a 20 20 20 20 23 20 4e 55 4c 4c 20 76 61  re.    # NULL va
f4c0: 6c 75 65 20 74 6f 20 62 65 20 69 6e 73 65 72 74  lue to be insert
f4d0: 65 64 2e 20 4e 6f 6e 2d 4e 55 4c 4c 20 76 61 6c  ed. Non-NULL val
f4e0: 75 65 73 20 64 6f 20 6e 6f 74 20 68 61 76 65 20  ues do not have 
f4f0: 74 6f 20 6d 61 70 20 74 6f 20 61 6e 79 0a 20 20  to map to any.  
f500: 20 20 23 20 70 61 72 65 6e 74 20 6b 65 79 20 76    # parent key v
f510: 61 6c 75 65 73 2c 20 73 6f 20 6c 6f 6e 67 20 61  alues, so long a
f520: 73 20 61 74 20 6c 65 61 73 74 20 6f 6e 65 20 66  s at least one f
f530: 69 65 6c 64 20 6f 66 20 74 68 65 20 63 68 69 6c  ield of the chil
f540: 64 20 6b 65 79 20 69 73 0a 20 20 20 20 23 20 4e  d key is.    # N
f550: 55 4c 4c 2e 0a 20 20 20 20 65 78 65 63 73 71 6c  ULL..    execsql
f560: 20 7b 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 63   { INSERT INTO c
f570: 20 56 41 4c 55 45 53 28 27 77 27 2c 20 32 2c 20   VALUES('w', 2, 
f580: 33 29 20 20 20 20 20 20 20 7d 0a 20 20 20 20 65  3)       }.    e
f590: 78 65 63 73 71 6c 20 7b 20 49 4e 53 45 52 54 20  xecsql { INSERT 
f5a0: 49 4e 54 4f 20 63 20 56 41 4c 55 45 53 28 27 78  INTO c VALUES('x
f5b0: 27 2c 20 27 78 27 2c 20 4e 55 4c 4c 29 20 20 7d  ', 'x', NULL)  }
f5c0: 0a 20 20 20 20 65 78 65 63 73 71 6c 20 7b 20 49  .    execsql { I
f5d0: 4e 53 45 52 54 20 49 4e 54 4f 20 63 20 56 41 4c  NSERT INTO c VAL
f5e0: 55 45 53 28 27 79 27 2c 20 4e 55 4c 4c 2c 20 27  UES('y', NULL, '
f5f0: 78 27 29 20 20 7d 0a 20 20 20 20 65 78 65 63 73  x')  }.    execs
f600: 71 6c 20 7b 20 49 4e 53 45 52 54 20 49 4e 54 4f  ql { INSERT INTO
f610: 20 63 20 56 41 4c 55 45 53 28 27 7a 27 2c 20 4e   c VALUES('z', N
f620: 55 4c 4c 2c 20 4e 55 4c 4c 29 20 7d 0a 0a 20 20  ULL, NULL) }..  
f630: 20 20 23 20 43 68 65 63 6b 20 74 68 61 74 20 74    # Check that t
f640: 68 65 20 46 4b 20 69 73 20 65 6e 66 6f 72 63 65  he FK is enforce
f650: 64 20 70 72 6f 70 65 72 6c 79 20 69 66 20 74 68  d properly if th
f660: 65 72 65 20 61 72 65 20 6e 6f 20 4e 55 4c 4c 20  ere are no NULL 
f670: 76 61 6c 75 65 73 20 0a 20 20 20 20 23 20 69 6e  values .    # in
f680: 20 74 68 65 20 63 68 69 6c 64 20 6b 65 79 20 63   the child key c
f690: 6f 6c 75 6d 6e 73 2e 0a 20 20 20 20 63 61 74 63  olumns..    catc
f6a0: 68 73 71 6c 20 7b 20 49 4e 53 45 52 54 20 49 4e  hsql { INSERT IN
f6b0: 54 4f 20 63 20 56 41 4c 55 45 53 28 27 61 27 2c  TO c VALUES('a',
f6c0: 20 32 2c 20 34 29 20 7d 0a 20 20 7d 20 7b 31 20   2, 4) }.  } {1 
f6d0: 7b 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e  {foreign key con
f6e0: 73 74 72 61 69 6e 74 20 66 61 69 6c 65 64 7d 7d  straint failed}}
f6f0: 0a 7d 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  .}..#-----------
f700: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
f710: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
f720: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
f730: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23  --------------.#
f740: 20 2f 2a 20 45 56 3a 20 52 2d 32 31 35 39 39 2d   /* EV: R-21599-
f750: 31 36 30 33 38 20 2a 2f 0a 23 0a 23 20 54 65 73  16038 */.#.# Tes
f760: 74 20 74 68 61 74 20 53 51 4c 69 74 65 20 64 6f  t that SQLite do
f770: 65 73 20 6e 6f 74 20 73 75 70 70 6f 72 74 20 74  es not support t
f780: 68 65 20 53 45 54 20 43 4f 4e 53 54 52 41 49 4e  he SET CONSTRAIN
f790: 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 41 6e 64  T statement. And
f7a0: 0a 23 20 74 68 61 74 20 69 74 20 69 73 20 70 6f  .# that it is po
f7b0: 73 73 69 62 6c 65 20 74 6f 20 63 72 65 61 74 65  ssible to create
f7c0: 20 62 6f 74 68 20 69 6d 6d 65 64 69 61 74 65 20   both immediate 
f7d0: 61 6e 64 20 64 65 66 65 72 72 65 64 20 63 6f 6e  and deferred con
f7e0: 73 74 72 61 69 6e 74 73 2e 0a 23 0a 64 72 6f 70  straints..#.drop
f7f0: 5f 61 6c 6c 5f 74 61 62 6c 65 73 0a 64 6f 5f 74  _all_tables.do_t
f800: 65 73 74 20 65 5f 66 6b 65 79 2d 32 2e 31 20 7b  est e_fkey-2.1 {
f810: 0a 20 20 63 61 74 63 68 73 71 6c 20 7b 20 53 45  .  catchsql { SE
f820: 54 20 43 4f 4e 53 54 52 41 49 4e 54 53 20 41 4c  T CONSTRAINTS AL
f830: 4c 20 49 4d 4d 45 44 49 41 54 45 20 7d 0a 7d 20  L IMMEDIATE }.} 
f840: 7b 31 20 7b 6e 65 61 72 20 22 53 45 54 22 3a 20  {1 {near "SET": 
f850: 73 79 6e 74 61 78 20 65 72 72 6f 72 7d 7d 0a 64  syntax error}}.d
f860: 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 32 2e  o_test e_fkey-2.
f870: 32 20 7b 0a 20 20 63 61 74 63 68 73 71 6c 20 7b  2 {.  catchsql {
f880: 20 53 45 54 20 43 4f 4e 53 54 52 41 49 4e 54 53   SET CONSTRAINTS
f890: 20 41 4c 4c 20 44 45 46 45 52 52 45 44 20 7d 0a   ALL DEFERRED }.
f8a0: 7d 20 7b 31 20 7b 6e 65 61 72 20 22 53 45 54 22  } {1 {near "SET"
f8b0: 3a 20 73 79 6e 74 61 78 20 65 72 72 6f 72 7d 7d  : syntax error}}
f8c0: 0a 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79  ..do_test e_fkey
f8d0: 2d 32 2e 33 20 7b 0a 20 20 65 78 65 63 73 71 6c  -2.3 {.  execsql
f8e0: 20 7b 0a 20 20 20 20 43 52 45 41 54 45 20 54 41   {.    CREATE TA
f8f0: 42 4c 45 20 70 28 61 2c 20 62 2c 20 50 52 49 4d  BLE p(a, b, PRIM
f900: 41 52 59 20 4b 45 59 28 61 2c 20 62 29 29 3b 0a  ARY KEY(a, b));.
f910: 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45      CREATE TABLE
f920: 20 63 64 28 63 2c 20 64 2c 20 0a 20 20 20 20 20   cd(c, d, .     
f930: 20 46 4f 52 45 49 47 4e 20 4b 45 59 28 63 2c 20   FOREIGN KEY(c, 
f940: 64 29 20 52 45 46 45 52 45 4e 43 45 53 20 70 20  d) REFERENCES p 
f950: 44 45 46 45 52 52 41 42 4c 45 20 49 4e 49 54 49  DEFERRABLE INITI
f960: 41 4c 4c 59 20 44 45 46 45 52 52 45 44 29 3b 0a  ALLY DEFERRED);.
f970: 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45      CREATE TABLE
f980: 20 63 69 28 63 2c 20 64 2c 20 0a 20 20 20 20 20   ci(c, d, .     
f990: 20 46 4f 52 45 49 47 4e 20 4b 45 59 28 63 2c 20   FOREIGN KEY(c, 
f9a0: 64 29 20 52 45 46 45 52 45 4e 43 45 53 20 70 20  d) REFERENCES p 
f9b0: 44 45 46 45 52 52 41 42 4c 45 20 49 4e 49 54 49  DEFERRABLE INITI
f9c0: 41 4c 4c 59 20 49 4d 4d 45 44 49 41 54 45 29 3b  ALLY IMMEDIATE);
f9d0: 0a 20 20 20 20 42 45 47 49 4e 3b 0a 20 20 7d 0a  .    BEGIN;.  }.
f9e0: 7d 20 7b 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66  } {}.do_test e_f
f9f0: 6b 65 79 2d 32 2e 34 20 7b 0a 20 20 63 61 74 63  key-2.4 {.  catc
fa00: 68 73 71 6c 20 7b 20 49 4e 53 45 52 54 20 49 4e  hsql { INSERT IN
fa10: 54 4f 20 63 69 20 56 41 4c 55 45 53 28 27 78 27  TO ci VALUES('x'
fa20: 2c 20 27 79 27 29 20 7d 0a 7d 20 7b 31 20 7b 66  , 'y') }.} {1 {f
fa30: 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74  oreign key const
fa40: 72 61 69 6e 74 20 66 61 69 6c 65 64 7d 7d 0a 64  raint failed}}.d
fa50: 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 32 2e  o_test e_fkey-2.
fa60: 35 20 7b 0a 20 20 63 61 74 63 68 73 71 6c 20 7b  5 {.  catchsql {
fa70: 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 63 64 20   INSERT INTO cd 
fa80: 56 41 4c 55 45 53 28 27 78 27 2c 20 27 79 27 29  VALUES('x', 'y')
fa90: 20 7d 0a 7d 20 7b 30 20 7b 7d 7d 0a 64 6f 5f 74   }.} {0 {}}.do_t
faa0: 65 73 74 20 65 5f 66 6b 65 79 2d 32 2e 36 20 7b  est e_fkey-2.6 {
fab0: 0a 20 20 63 61 74 63 68 73 71 6c 20 7b 20 43 4f  .  catchsql { CO
fac0: 4d 4d 49 54 20 7d 0a 7d 20 7b 31 20 7b 66 6f 72  MMIT }.} {1 {for
fad0: 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61  eign key constra
fae0: 69 6e 74 20 66 61 69 6c 65 64 7d 7d 0a 64 6f 5f  int failed}}.do_
faf0: 74 65 73 74 20 65 5f 66 6b 65 79 2d 32 2e 37 20  test e_fkey-2.7 
fb00: 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 20 0a 20  {.  execsql { . 
fb10: 20 20 20 44 45 4c 45 54 45 20 46 52 4f 4d 20 63     DELETE FROM c
fb20: 64 3b 0a 20 20 20 20 43 4f 4d 4d 49 54 3b 0a 20  d;.    COMMIT;. 
fb30: 20 7d 0a 7d 20 7b 7d 0a 0a 23 2d 2d 2d 2d 2d 2d   }.} {}..#------
fb40: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
fb50: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
fb60: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
fb70: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
fb80: 2d 2d 2d 0a 23 20 2f 2a 20 45 56 3a 20 52 2d 34  ---.# /* EV: R-4
fb90: 32 32 36 34 2d 33 30 35 30 33 20 2a 2f 0a 23 0a  2264-30503 */.#.
fba0: 23 20 54 65 73 74 20 74 68 61 74 20 74 68 65 20  # Test that the 
fbb0: 6d 61 78 69 6d 75 6d 20 72 65 63 75 72 73 69 6f  maximum recursio
fbc0: 6e 20 64 65 70 74 68 20 6f 66 20 66 6f 72 65 69  n depth of forei
fbd0: 67 6e 20 6b 65 79 20 61 63 74 69 6f 6e 20 70 72  gn key action pr
fbe0: 6f 67 72 61 6d 73 20 69 73 0a 23 20 67 6f 76 65  ograms is.# gove
fbf0: 72 6e 65 64 20 62 79 20 74 68 65 20 53 51 4c 49  rned by the SQLI
fc00: 54 45 5f 4d 41 58 5f 54 52 49 47 47 45 52 5f 44  TE_MAX_TRIGGER_D
fc10: 45 50 54 48 20 61 6e 64 20 53 51 4c 49 54 45 5f  EPTH and SQLITE_
fc20: 4c 49 4d 49 54 5f 54 52 49 47 47 45 52 5f 44 45  LIMIT_TRIGGER_DE
fc30: 50 54 48 0a 23 20 73 65 74 74 69 6e 67 73 2e 0a  PTH.# settings..
fc40: 23 0a 70 72 6f 63 20 74 65 73 74 5f 6f 6e 5f 64  #.proc test_on_d
fc50: 65 6c 65 74 65 5f 72 65 63 75 72 73 69 6f 6e 20  elete_recursion 
fc60: 7b 6c 69 6d 69 74 7d 20 7b 0a 20 20 64 72 6f 70  {limit} {.  drop
fc70: 5f 61 6c 6c 5f 74 61 62 6c 65 73 0a 20 20 65 78  _all_tables.  ex
fc80: 65 63 73 71 6c 20 7b 20 0a 20 20 20 20 42 45 47  ecsql { .    BEG
fc90: 49 4e 3b 0a 20 20 20 20 43 52 45 41 54 45 20 54  IN;.    CREATE T
fca0: 41 42 4c 45 20 74 30 28 61 20 50 52 49 4d 41 52  ABLE t0(a PRIMAR
fcb0: 59 20 4b 45 59 2c 20 62 29 3b 0a 20 20 20 20 49  Y KEY, b);.    I
fcc0: 4e 53 45 52 54 20 49 4e 54 4f 20 74 30 20 56 41  NSERT INTO t0 VA
fcd0: 4c 55 45 53 28 27 78 30 27 2c 20 4e 55 4c 4c 29  LUES('x0', NULL)
fce0: 3b 0a 20 20 7d 0a 20 20 66 6f 72 20 7b 73 65 74  ;.  }.  for {set
fcf0: 20 69 20 31 7d 20 7b 24 69 20 3c 3d 20 24 6c 69   i 1} {$i <= $li
fd00: 6d 69 74 7d 20 7b 69 6e 63 72 20 69 7d 20 7b 0a  mit} {incr i} {.
fd10: 20 20 20 20 65 78 65 63 73 71 6c 20 22 0a 20 20      execsql ".  
fd20: 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45      CREATE TABLE
fd30: 20 74 24 69 20 28 0a 20 20 20 20 20 20 20 20 61   t$i (.        a
fd40: 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 62 20   PRIMARY KEY, b 
fd50: 52 45 46 45 52 45 4e 43 45 53 20 74 5b 65 78 70  REFERENCES t[exp
fd60: 72 20 24 69 2d 31 5d 20 4f 4e 20 44 45 4c 45 54  r $i-1] ON DELET
fd70: 45 20 43 41 53 43 41 44 45 0a 20 20 20 20 20 20  E CASCADE.      
fd80: 29 3b 0a 20 20 20 20 20 20 49 4e 53 45 52 54 20  );.      INSERT 
fd90: 49 4e 54 4f 20 74 24 69 20 56 41 4c 55 45 53 28  INTO t$i VALUES(
fda0: 27 78 24 69 27 2c 20 27 78 5b 65 78 70 72 20 24  'x$i', 'x[expr $
fdb0: 69 2d 31 5d 27 29 3b 0a 20 20 20 20 22 0a 20 20  i-1]');.    ".  
fdc0: 7d 0a 20 20 65 78 65 63 73 71 6c 20 43 4f 4d 4d  }.  execsql COMM
fdd0: 49 54 0a 20 20 63 61 74 63 68 73 71 6c 20 22 0a  IT.  catchsql ".
fde0: 20 20 20 20 44 45 4c 45 54 45 20 46 52 4f 4d 20      DELETE FROM 
fdf0: 74 30 3b 0a 20 20 20 20 53 45 4c 45 43 54 20 63  t0;.    SELECT c
fe00: 6f 75 6e 74 28 2a 29 20 46 52 4f 4d 20 74 24 6c  ount(*) FROM t$l
fe10: 69 6d 69 74 3b 0a 20 20 22 0a 7d 0a 70 72 6f 63  imit;.  ".}.proc
fe20: 20 74 65 73 74 5f 6f 6e 5f 75 70 64 61 74 65 5f   test_on_update_
fe30: 72 65 63 75 72 73 69 6f 6e 20 7b 6c 69 6d 69 74  recursion {limit
fe40: 7d 20 7b 0a 20 20 64 72 6f 70 5f 61 6c 6c 5f 74  } {.  drop_all_t
fe50: 61 62 6c 65 73 0a 20 20 65 78 65 63 73 71 6c 20  ables.  execsql 
fe60: 7b 20 0a 20 20 20 20 42 45 47 49 4e 3b 0a 20 20  { .    BEGIN;.  
fe70: 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 74    CREATE TABLE t
fe80: 30 28 61 20 50 52 49 4d 41 52 59 20 4b 45 59 29  0(a PRIMARY KEY)
fe90: 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54  ;.    INSERT INT
fea0: 4f 20 74 30 20 56 41 4c 55 45 53 28 27 78 78 78  O t0 VALUES('xxx
feb0: 27 29 3b 0a 20 20 7d 0a 20 20 66 6f 72 20 7b 73  ');.  }.  for {s
fec0: 65 74 20 69 20 31 7d 20 7b 24 69 20 3c 3d 20 24  et i 1} {$i <= $
fed0: 6c 69 6d 69 74 7d 20 7b 69 6e 63 72 20 69 7d 20  limit} {incr i} 
fee0: 7b 0a 20 20 20 20 73 65 74 20 6a 20 5b 65 78 70  {.    set j [exp
fef0: 72 20 24 69 2d 31 5d 0a 0a 20 20 20 20 65 78 65  r $i-1]..    exe
ff00: 63 73 71 6c 20 22 0a 20 20 20 20 20 20 43 52 45  csql ".      CRE
ff10: 41 54 45 20 54 41 42 4c 45 20 74 24 69 20 28 61  ATE TABLE t$i (a
ff20: 20 50 52 49 4d 41 52 59 20 4b 45 59 20 52 45 46   PRIMARY KEY REF
ff30: 45 52 45 4e 43 45 53 20 74 24 6a 20 4f 4e 20 55  ERENCES t$j ON U
ff40: 50 44 41 54 45 20 43 41 53 43 41 44 45 29 3b 0a  PDATE CASCADE);.
ff50: 20 20 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54        INSERT INT
ff60: 4f 20 74 24 69 20 56 41 4c 55 45 53 28 27 78 78  O t$i VALUES('xx
ff70: 78 27 29 3b 0a 20 20 20 20 22 0a 20 20 7d 0a 20  x');.    ".  }. 
ff80: 20 65 78 65 63 73 71 6c 20 43 4f 4d 4d 49 54 0a   execsql COMMIT.
ff90: 20 20 63 61 74 63 68 73 71 6c 20 22 0a 20 20 20    catchsql ".   
ffa0: 20 55 50 44 41 54 45 20 74 30 20 53 45 54 20 61   UPDATE t0 SET a
ffb0: 20 3d 20 27 79 79 79 27 3b 0a 20 20 20 20 53 45   = 'yyy';.    SE
ffc0: 4c 45 43 54 20 4e 4f 54 20 28 61 3d 27 79 79 79  LECT NOT (a='yyy
ffd0: 27 29 20 46 52 4f 4d 20 74 24 6c 69 6d 69 74 3b  ') FROM t$limit;
ffe0: 0a 20 20 22 0a 7d 0a 0a 64 6f 5f 74 65 73 74 20  .  ".}..do_test 
fff0: 65 5f 66 6b 65 79 2d 33 2e 31 2e 31 20 7b 0a 20  e_fkey-3.1.1 {. 
10000 20 74 65 73 74 5f 6f 6e 5f 64 65 6c 65 74 65 5f   test_on_delete_
10010 72 65 63 75 72 73 69 6f 6e 20 24 53 51 4c 49 54  recursion $SQLIT
10020 45 5f 4d 41 58 5f 54 52 49 47 47 45 52 5f 44 45  E_MAX_TRIGGER_DE
10030 50 54 48 0a 7d 20 7b 30 20 30 7d 0a 64 6f 5f 74  PTH.} {0 0}.do_t
10040 65 73 74 20 65 5f 66 6b 65 79 2d 33 2e 31 2e 32  est e_fkey-3.1.2
10050 20 7b 0a 20 20 74 65 73 74 5f 6f 6e 5f 64 65 6c   {.  test_on_del
10060 65 74 65 5f 72 65 63 75 72 73 69 6f 6e 20 5b 65  ete_recursion [e
10070 78 70 72 20 24 53 51 4c 49 54 45 5f 4d 41 58 5f  xpr $SQLITE_MAX_
10080 54 52 49 47 47 45 52 5f 44 45 50 54 48 2b 31 5d  TRIGGER_DEPTH+1]
10090 0a 7d 20 7b 31 20 7b 74 6f 6f 20 6d 61 6e 79 20  .} {1 {too many 
100a0 6c 65 76 65 6c 73 20 6f 66 20 74 72 69 67 67 65  levels of trigge
100b0 72 20 72 65 63 75 72 73 69 6f 6e 7d 7d 0a 64 6f  r recursion}}.do
100c0 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 33 2e 31  _test e_fkey-3.1
100d0 2e 33 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6c  .3 {.  sqlite3_l
100e0 69 6d 69 74 20 64 62 20 53 51 4c 49 54 45 5f 4c  imit db SQLITE_L
100f0 49 4d 49 54 5f 54 52 49 47 47 45 52 5f 44 45 50  IMIT_TRIGGER_DEP
10100 54 48 20 35 0a 20 20 74 65 73 74 5f 6f 6e 5f 64  TH 5.  test_on_d
10110 65 6c 65 74 65 5f 72 65 63 75 72 73 69 6f 6e 20  elete_recursion 
10120 35 0a 7d 20 7b 30 20 30 7d 0a 64 6f 5f 74 65 73  5.} {0 0}.do_tes
10130 74 20 65 5f 66 6b 65 79 2d 33 2e 31 2e 34 20 7b  t e_fkey-3.1.4 {
10140 0a 20 20 74 65 73 74 5f 6f 6e 5f 64 65 6c 65 74  .  test_on_delet
10150 65 5f 72 65 63 75 72 73 69 6f 6e 20 36 0a 7d 20  e_recursion 6.} 
10160 7b 31 20 7b 74 6f 6f 20 6d 61 6e 79 20 6c 65 76  {1 {too many lev
10170 65 6c 73 20 6f 66 20 74 72 69 67 67 65 72 20 72  els of trigger r
10180 65 63 75 72 73 69 6f 6e 7d 7d 0a 64 6f 5f 74 65  ecursion}}.do_te
10190 73 74 20 65 5f 66 6b 65 79 2d 33 2e 31 2e 35 20  st e_fkey-3.1.5 
101a0 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6c 69 6d 69  {.  sqlite3_limi
101b0 74 20 64 62 20 53 51 4c 49 54 45 5f 4c 49 4d 49  t db SQLITE_LIMI
101c0 54 5f 54 52 49 47 47 45 52 5f 44 45 50 54 48 20  T_TRIGGER_DEPTH 
101d0 31 30 30 30 30 30 30 0a 7d 20 7b 35 7d 0a 64 6f  1000000.} {5}.do
101e0 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 33 2e 32  _test e_fkey-3.2
101f0 2e 31 20 7b 0a 20 20 74 65 73 74 5f 6f 6e 5f 75  .1 {.  test_on_u
10200 70 64 61 74 65 5f 72 65 63 75 72 73 69 6f 6e 20  pdate_recursion 
10210 24 53 51 4c 49 54 45 5f 4d 41 58 5f 54 52 49 47  $SQLITE_MAX_TRIG
10220 47 45 52 5f 44 45 50 54 48 0a 7d 20 7b 30 20 30  GER_DEPTH.} {0 0
10230 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79  }.do_test e_fkey
10240 2d 33 2e 32 2e 32 20 7b 0a 20 20 74 65 73 74 5f  -3.2.2 {.  test_
10250 6f 6e 5f 75 70 64 61 74 65 5f 72 65 63 75 72 73  on_update_recurs
10260 69 6f 6e 20 5b 65 78 70 72 20 24 53 51 4c 49 54  ion [expr $SQLIT
10270 45 5f 4d 41 58 5f 54 52 49 47 47 45 52 5f 44 45  E_MAX_TRIGGER_DE
10280 50 54 48 2b 31 5d 0a 7d 20 7b 31 20 7b 74 6f 6f  PTH+1].} {1 {too
10290 20 6d 61 6e 79 20 6c 65 76 65 6c 73 20 6f 66 20   many levels of 
102a0 74 72 69 67 67 65 72 20 72 65 63 75 72 73 69 6f  trigger recursio
102b0 6e 7d 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b  n}}.do_test e_fk
102c0 65 79 2d 33 2e 32 2e 33 20 7b 0a 20 20 73 71 6c  ey-3.2.3 {.  sql
102d0 69 74 65 33 5f 6c 69 6d 69 74 20 64 62 20 53 51  ite3_limit db SQ
102e0 4c 49 54 45 5f 4c 49 4d 49 54 5f 54 52 49 47 47  LITE_LIMIT_TRIGG
102f0 45 52 5f 44 45 50 54 48 20 35 0a 20 20 74 65 73  ER_DEPTH 5.  tes
10300 74 5f 6f 6e 5f 75 70 64 61 74 65 5f 72 65 63 75  t_on_update_recu
10310 72 73 69 6f 6e 20 35 0a 7d 20 7b 30 20 30 7d 0a  rsion 5.} {0 0}.
10320 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 33  do_test e_fkey-3
10330 2e 32 2e 34 20 7b 0a 20 20 74 65 73 74 5f 6f 6e  .2.4 {.  test_on
10340 5f 75 70 64 61 74 65 5f 72 65 63 75 72 73 69 6f  _update_recursio
10350 6e 20 36 0a 7d 20 7b 31 20 7b 74 6f 6f 20 6d 61  n 6.} {1 {too ma
10360 6e 79 20 6c 65 76 65 6c 73 20 6f 66 20 74 72 69  ny levels of tri
10370 67 67 65 72 20 72 65 63 75 72 73 69 6f 6e 7d 7d  gger recursion}}
10380 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d  .do_test e_fkey-
10390 33 2e 32 2e 35 20 7b 0a 20 20 73 71 6c 69 74 65  3.2.5 {.  sqlite
103a0 33 5f 6c 69 6d 69 74 20 64 62 20 53 51 4c 49 54  3_limit db SQLIT
103b0 45 5f 4c 49 4d 49 54 5f 54 52 49 47 47 45 52 5f  E_LIMIT_TRIGGER_
103c0 44 45 50 54 48 20 31 30 30 30 30 30 30 0a 7d 20  DEPTH 1000000.} 
103d0 7b 35 7d 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  {5}..#----------
103e0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
103f0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
10400 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
10410 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a  ---------------.
10420 23 20 2f 2a 20 45 56 3a 20 52 2d 35 31 37 36 39  # /* EV: R-51769
10430 2d 33 32 37 33 30 20 2a 2f 0a 23 0a 23 20 54 68  -32730 */.#.# Th
10440 65 20 73 65 74 74 69 6e 67 20 6f 66 20 74 68 65  e setting of the
10450 20 72 65 63 75 72 73 69 76 65 5f 74 72 69 67 67   recursive_trigg
10460 65 72 73 20 70 72 61 67 6d 61 20 64 6f 65 73 20  ers pragma does 
10470 6e 6f 74 20 61 66 66 65 63 74 20 66 6f 72 65 69  not affect forei
10480 67 6e 0a 23 20 6b 65 79 20 61 63 74 69 6f 6e 73  gn.# key actions
10490 2e 0a 23 0a 66 6f 72 65 61 63 68 20 72 65 63 75  ..#.foreach recu
104a0 72 73 69 76 65 5f 74 72 69 67 67 65 72 73 5f 73  rsive_triggers_s
104b0 65 74 74 69 6e 67 20 5b 6c 69 73 74 20 30 20 31  etting [list 0 1
104c0 20 4f 4e 20 4f 46 46 5d 20 7b 0a 20 20 64 72 6f   ON OFF] {.  dro
104d0 70 5f 61 6c 6c 5f 74 61 62 6c 65 73 0a 20 20 65  p_all_tables.  e
104e0 78 65 63 73 71 6c 20 22 50 52 41 47 4d 41 20 72  xecsql "PRAGMA r
104f0 65 63 75 72 73 69 76 65 5f 74 72 69 67 67 65 72  ecursive_trigger
10500 73 20 3d 20 24 72 65 63 75 72 73 69 76 65 5f 74  s = $recursive_t
10510 72 69 67 67 65 72 73 5f 73 65 74 74 69 6e 67 22  riggers_setting"
10520 0a 0a 20 20 64 6f 5f 74 65 73 74 20 65 5f 66 6b  ..  do_test e_fk
10530 65 79 2d 34 2e 24 72 65 63 75 72 73 69 76 65 5f  ey-4.$recursive_
10540 74 72 69 67 67 65 72 73 5f 73 65 74 74 69 6e 67  triggers_setting
10550 2e 31 20 7b 0a 20 20 20 20 65 78 65 63 73 71 6c  .1 {.    execsql
10560 20 7b 0a 20 20 20 20 20 20 43 52 45 41 54 45 20   {.      CREATE 
10570 54 41 42 4c 45 20 74 31 28 61 20 50 52 49 4d 41  TABLE t1(a PRIMA
10580 52 59 20 4b 45 59 2c 20 62 20 52 45 46 45 52 45  RY KEY, b REFERE
10590 4e 43 45 53 20 74 31 20 4f 4e 20 44 45 4c 45 54  NCES t1 ON DELET
105a0 45 20 43 41 53 43 41 44 45 29 3b 0a 20 20 20 20  E CASCADE);.    
105b0 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 31    INSERT INTO t1
105c0 20 56 41 4c 55 45 53 28 31 2c 20 4e 55 4c 4c 29   VALUES(1, NULL)
105d0 3b 0a 20 20 20 20 20 20 49 4e 53 45 52 54 20 49  ;.      INSERT I
105e0 4e 54 4f 20 74 31 20 56 41 4c 55 45 53 28 32 2c  NTO t1 VALUES(2,
105f0 20 31 29 3b 0a 20 20 20 20 20 20 49 4e 53 45 52   1);.      INSER
10600 54 20 49 4e 54 4f 20 74 31 20 56 41 4c 55 45 53  T INTO t1 VALUES
10610 28 33 2c 20 32 29 3b 0a 20 20 20 20 20 20 49 4e  (3, 2);.      IN
10620 53 45 52 54 20 49 4e 54 4f 20 74 31 20 56 41 4c  SERT INTO t1 VAL
10630 55 45 53 28 34 2c 20 33 29 3b 0a 20 20 20 20 20  UES(4, 3);.     
10640 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 31 20   INSERT INTO t1 
10650 56 41 4c 55 45 53 28 35 2c 20 34 29 3b 0a 20 20  VALUES(5, 4);.  
10660 20 20 20 20 53 45 4c 45 43 54 20 63 6f 75 6e 74      SELECT count
10670 28 2a 29 20 46 52 4f 4d 20 74 31 3b 0a 20 20 20  (*) FROM t1;.   
10680 20 7d 0a 20 20 7d 20 7b 35 7d 0a 20 20 64 6f 5f   }.  } {5}.  do_
10690 74 65 73 74 20 65 5f 66 6b 65 79 2d 34 2e 24 72  test e_fkey-4.$r
106a0 65 63 75 72 73 69 76 65 5f 74 72 69 67 67 65 72  ecursive_trigger
106b0 73 5f 73 65 74 74 69 6e 67 2e 32 20 7b 0a 20 20  s_setting.2 {.  
106c0 20 20 65 78 65 63 73 71 6c 20 7b 20 53 45 4c 45    execsql { SELE
106d0 43 54 20 63 6f 75 6e 74 28 2a 29 20 46 52 4f 4d  CT count(*) FROM
106e0 20 74 31 20 57 48 45 52 45 20 61 20 3d 20 31 20   t1 WHERE a = 1 
106f0 7d 0a 20 20 7d 20 7b 31 7d 0a 20 20 64 6f 5f 74  }.  } {1}.  do_t
10700 65 73 74 20 65 5f 66 6b 65 79 2d 34 2e 24 72 65  est e_fkey-4.$re
10710 63 75 72 73 69 76 65 5f 74 72 69 67 67 65 72 73  cursive_triggers
10720 5f 73 65 74 74 69 6e 67 2e 33 20 7b 0a 20 20 20  _setting.3 {.   
10730 20 65 78 65 63 73 71 6c 20 7b 20 0a 20 20 20 20   execsql { .    
10740 20 20 44 45 4c 45 54 45 20 46 52 4f 4d 20 74 31    DELETE FROM t1
10750 20 57 48 45 52 45 20 61 20 3d 20 31 3b 0a 20 20   WHERE a = 1;.  
10760 20 20 20 20 53 45 4c 45 43 54 20 63 6f 75 6e 74      SELECT count
10770 28 2a 29 20 46 52 4f 4d 20 74 31 3b 0a 20 20 20  (*) FROM t1;.   
10780 20 7d 0a 20 20 7d 20 7b 30 7d 0a 7d 0a 0a 66 69   }.  } {0}.}..fi
10790 6e 69 73 68 5f 74 65 73 74 0a                    nish_test.