/ Hex Artifact Content
Login

Artifact af2750eddb48280cfd5ce1d8b2f7948bbd8779ad:


0000: 23 20 32 30 30 39 20 4f 63 74 6f 62 65 72 20 37  # 2009 October 7
0010: 0a 23 0a 23 20 54 68 65 20 61 75 74 68 6f 72 20  .#.# The author 
0020: 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
0030: 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
0040: 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
0050: 63 65 20 6f 66 0a 23 20 61 20 6c 65 67 61 6c 20  ce of.# a legal 
0060: 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
0070: 61 20 62 6c 65 73 73 69 6e 67 3a 0a 23 0a 23 20  a blessing:.#.# 
0080: 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
0090: 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
00a0: 0a 23 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  .#    May you fi
00b0: 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
00c0: 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
00d0: 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
00e0: 23 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  #    May you sha
00f0: 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
0100: 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
0110: 6e 20 79 6f 75 20 67 69 76 65 2e 0a 23 0a 23 2a  n you give..#.#*
0120: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0130: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0140: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0150: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0160: 2a 2a 2a 2a 2a 2a 0a 23 0a 23 20 54 68 69 73 20  ******.#.# This 
0170: 66 69 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20  file implements 
0180: 74 65 73 74 73 20 74 6f 20 76 65 72 69 66 79 20  tests to verify 
0190: 74 68 65 20 22 74 65 73 74 61 62 6c 65 20 73 74  the "testable st
01a0: 61 74 65 6d 65 6e 74 73 22 20 69 6e 20 74 68 65  atements" in the
01b0: 0a 23 20 66 6f 72 65 69 67 6e 6b 65 79 73 2e 69  .# foreignkeys.i
01c0: 6e 20 64 6f 63 75 6d 65 6e 74 2e 0a 23 0a 23 20  n document..#.# 
01d0: 54 68 65 20 74 65 73 74 73 20 69 6e 20 74 68 69  The tests in thi
01e0: 73 20 66 69 6c 65 20 61 72 65 20 61 72 72 61 6e  s file are arran
01f0: 67 65 64 20 74 6f 20 6d 69 72 72 6f 72 20 74 68  ged to mirror th
0200: 65 20 73 74 72 75 63 74 75 72 65 20 6f 66 20 0a  e structure of .
0210: 23 20 66 6f 72 65 69 67 6e 6b 65 79 2e 69 6e 2c  # foreignkey.in,
0220: 20 77 69 74 68 20 6f 6e 65 20 65 78 63 65 70 74   with one except
0230: 69 6f 6e 3a 20 54 68 65 20 73 74 61 74 65 6d 65  ion: The stateme
0240: 6e 74 73 20 69 6e 20 73 65 63 74 69 6f 6e 20 32  nts in section 2
0250: 2c 20 77 68 69 63 68 20 0a 23 20 64 65 61 6c 73  , which .# deals
0260: 20 77 69 74 68 20 65 6e 61 62 6c 69 6e 67 2f 64   with enabling/d
0270: 69 73 61 62 6c 69 6e 67 20 66 6f 72 65 69 67 6e  isabling foreign
0280: 20 6b 65 79 20 73 75 70 70 6f 72 74 2c 20 69 73   key support, is
0290: 20 74 65 73 74 65 64 20 66 69 72 73 74 2c 0a 23   tested first,.#
02a0: 20 62 65 66 6f 72 65 20 73 65 63 74 69 6f 6e 20   before section 
02b0: 31 2e 20 54 68 69 73 20 69 73 20 62 65 63 61 75  1. This is becau
02c0: 73 65 20 73 6f 6d 65 20 73 74 61 74 65 6d 65 6e  se some statemen
02d0: 74 73 20 69 6e 20 73 65 63 74 69 6f 6e 20 32 20  ts in section 2 
02e0: 64 65 61 6c 0a 23 20 77 69 74 68 20 62 75 69 6c  deal.# with buil
02f0: 64 73 20 74 68 61 74 20 64 6f 20 6e 6f 74 20 69  ds that do not i
0300: 6e 63 6c 75 64 65 20 63 6f 6d 70 6c 65 74 65 20  nclude complete 
0310: 66 6f 72 65 69 67 6e 20 6b 65 79 20 73 75 70 70  foreign key supp
0320: 6f 72 74 20 28 62 65 63 61 75 73 65 0a 23 20 65  ort (because.# e
0330: 69 74 68 65 72 20 53 51 4c 49 54 45 5f 4f 4d 49  ither SQLITE_OMI
0340: 54 5f 54 52 49 47 47 45 52 20 6f 72 20 53 51 4c  T_TRIGGER or SQL
0350: 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e  ITE_OMIT_FOREIGN
0360: 5f 4b 45 59 20 77 61 73 20 64 65 66 69 6e 65 64  _KEY was defined
0370: 0a 23 20 61 74 20 62 75 69 6c 64 20 74 69 6d 65  .# at build time
0380: 29 2e 0a 23 0a 0a 73 65 74 20 74 65 73 74 64 69  )..#..set testdi
0390: 72 20 5b 66 69 6c 65 20 64 69 72 6e 61 6d 65 20  r [file dirname 
03a0: 24 61 72 67 76 30 5d 0a 73 6f 75 72 63 65 20 24  $argv0].source $
03b0: 74 65 73 74 64 69 72 2f 74 65 73 74 65 72 2e 74  testdir/tester.t
03c0: 63 6c 0a 0a 70 72 6f 63 20 65 71 70 20 7b 73 71  cl..proc eqp {sq
03d0: 6c 20 7b 64 62 20 64 62 7d 7d 20 7b 20 75 70 6c  l {db db}} { upl
03e0: 65 76 65 6c 20 65 78 65 63 73 71 6c 20 5b 6c 69  evel execsql [li
03f0: 73 74 20 22 45 58 50 4c 41 49 4e 20 51 55 45 52  st "EXPLAIN QUER
0400: 59 20 50 4c 41 4e 20 24 73 71 6c 22 5d 20 24 64  Y PLAN $sql"] $d
0410: 62 20 7d 0a 0a 23 23 23 23 23 23 23 23 23 23 23  b }..###########
0420: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
0430: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
0440: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
0450: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
0460: 0a 23 23 23 20 53 45 43 54 49 4f 4e 20 32 3a 20  .### SECTION 2: 
0470: 45 6e 61 62 6c 69 6e 67 20 46 6f 72 65 69 67 6e  Enabling Foreign
0480: 20 4b 65 79 20 53 75 70 70 6f 72 74 0a 23 23 23   Key Support.###
0490: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
04a0: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
04b0: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
04c0: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
04d0: 23 23 23 23 23 23 23 23 0a 0a 23 2d 2d 2d 2d 2d  ########..#-----
04e0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
04f0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0500: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0510: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0520: 2d 2d 2d 2d 0a 23 20 2f 2a 20 45 56 3a 20 52 2d  ----.# /* EV: R-
0530: 33 33 37 31 30 2d 35 36 33 34 34 20 2a 2f 0a 23  33710-56344 */.#
0540: 0a 23 20 54 65 73 74 20 62 75 69 6c 64 73 20 6e  .# Test builds n
0550: 65 69 74 68 65 72 20 4f 4d 49 54 5f 46 4f 52 45  either OMIT_FORE
0560: 49 47 4e 5f 4b 45 59 20 6f 72 20 4f 4d 49 54 5f  IGN_KEY or OMIT_
0570: 54 52 49 47 47 45 52 20 64 65 66 69 6e 65 64 20  TRIGGER defined 
0580: 68 61 76 65 20 0a 23 20 66 6f 72 65 69 67 6e 20  have .# foreign 
0590: 6b 65 79 20 66 75 6e 63 74 69 6f 6e 61 6c 69 74  key functionalit
05a0: 79 2e 0a 23 0a 69 66 63 61 70 61 62 6c 65 20 74  y..#.ifcapable t
05b0: 72 69 67 67 65 72 26 26 66 6f 72 65 69 67 6e 6b  rigger&&foreignk
05c0: 65 79 20 7b 0a 20 20 64 6f 5f 74 65 73 74 20 65  ey {.  do_test e
05d0: 5f 66 6b 65 79 2d 34 39 20 7b 0a 20 20 20 20 65  _fkey-49 {.    e
05e0: 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 20 20 50  xecsql {.      P
05f0: 52 41 47 4d 41 20 66 6f 72 65 69 67 6e 5f 6b 65  RAGMA foreign_ke
0600: 79 73 20 3d 20 4f 4e 3b 0a 20 20 20 20 20 20 43  ys = ON;.      C
0610: 52 45 41 54 45 20 54 41 42 4c 45 20 70 28 69 20  REATE TABLE p(i 
0620: 50 52 49 4d 41 52 59 20 4b 45 59 29 3b 0a 20 20  PRIMARY KEY);.  
0630: 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45      CREATE TABLE
0640: 20 63 28 6a 20 52 45 46 45 52 45 4e 43 45 53 20   c(j REFERENCES 
0650: 70 20 4f 4e 20 55 50 44 41 54 45 20 43 41 53 43  p ON UPDATE CASC
0660: 41 44 45 29 3b 0a 20 20 20 20 20 20 49 4e 53 45  ADE);.      INSE
0670: 52 54 20 49 4e 54 4f 20 70 20 56 41 4c 55 45 53  RT INTO p VALUES
0680: 28 27 68 65 6c 6c 6f 27 29 3b 0a 20 20 20 20 20  ('hello');.     
0690: 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 63 20 56   INSERT INTO c V
06a0: 41 4c 55 45 53 28 27 68 65 6c 6c 6f 27 29 3b 0a  ALUES('hello');.
06b0: 20 20 20 20 20 20 55 50 44 41 54 45 20 70 20 53        UPDATE p S
06c0: 45 54 20 69 20 3d 20 27 77 6f 72 6c 64 27 3b 0a  ET i = 'world';.
06d0: 20 20 20 20 20 20 53 45 4c 45 43 54 20 2a 20 46        SELECT * F
06e0: 52 4f 4d 20 63 3b 0a 20 20 20 20 7d 0a 20 20 7d  ROM c;.    }.  }
06f0: 20 7b 77 6f 72 6c 64 7d 0a 7d 0a 0a 23 2d 2d 2d   {world}.}..#---
0700: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0710: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0720: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0730: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0740: 2d 2d 2d 2d 2d 2d 0a 23 20 2f 2a 20 45 56 3a 20  ------.# /* EV: 
0750: 52 2d 34 34 36 39 37 2d 36 31 35 34 33 20 2a 2f  R-44697-61543 */
0760: 0a 23 0a 23 20 54 65 73 74 20 74 68 65 20 65 66  .#.# Test the ef
0770: 66 65 63 74 73 20 6f 66 20 64 65 66 69 6e 69 6e  fects of definin
0780: 67 20 4f 4d 49 54 5f 54 52 49 47 47 45 52 20 62  g OMIT_TRIGGER b
0790: 75 74 20 6e 6f 74 20 4f 4d 49 54 5f 46 4f 52 45  ut not OMIT_FORE
07a0: 49 47 4e 5f 4b 45 59 2e 0a 23 0a 23 20 2f 2a 20  IGN_KEY..#.# /* 
07b0: 45 56 3a 20 52 2d 32 32 35 36 37 2d 34 34 30 33  EV: R-22567-4403
07c0: 39 20 2a 2f 0a 23 20 2f 2a 20 45 56 3a 20 52 2d  9 */.# /* EV: R-
07d0: 34 31 37 38 34 2d 31 33 33 33 39 20 2a 2f 0a 23  41784-13339 */.#
07e0: 0a 23 20 53 70 65 63 69 66 69 63 61 6c 6c 79 2c  .# Specifically,
07f0: 20 74 65 73 74 20 74 68 61 74 20 22 50 52 41 47   test that "PRAG
0800: 4d 41 20 66 6f 72 65 69 67 6e 5f 6b 65 79 73 22  MA foreign_keys"
0810: 20 69 73 20 61 20 6e 6f 2d 6f 70 20 69 6e 20 74   is a no-op in t
0820: 68 69 73 20 63 61 73 65 2e 0a 23 20 57 68 65 6e  his case..# When
0830: 20 75 73 69 6e 67 20 74 68 65 20 70 72 61 67 6d   using the pragm
0840: 61 20 74 6f 20 71 75 65 72 79 20 74 68 65 20 63  a to query the c
0850: 75 72 72 65 6e 74 20 73 65 74 74 69 6e 67 2c 20  urrent setting, 
0860: 30 20 72 6f 77 73 20 61 72 65 20 72 65 74 75 72  0 rows are retur
0870: 6e 65 64 2e 0a 23 0a 72 65 73 65 74 5f 64 62 0a  ned..#.reset_db.
0880: 69 66 63 61 70 61 62 6c 65 20 21 74 72 69 67 67  ifcapable !trigg
0890: 65 72 26 26 66 6f 72 65 69 67 6e 6b 65 79 20 7b  er&&foreignkey {
08a0: 0a 20 20 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65  .  do_test e_fke
08b0: 79 2d 35 31 2e 31 20 7b 0a 20 20 20 20 65 78 65  y-51.1 {.    exe
08c0: 63 73 71 6c 20 7b 0a 20 20 20 20 20 20 50 52 41  csql {.      PRA
08d0: 47 4d 41 20 66 6f 72 65 69 67 6e 5f 6b 65 79 73  GMA foreign_keys
08e0: 20 3d 20 4f 4e 3b 0a 20 20 20 20 20 20 43 52 45   = ON;.      CRE
08f0: 41 54 45 20 54 41 42 4c 45 20 70 28 69 20 50 52  ATE TABLE p(i PR
0900: 49 4d 41 52 59 20 4b 45 59 29 3b 0a 20 20 20 20  IMARY KEY);.    
0910: 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 63    CREATE TABLE c
0920: 28 6a 20 52 45 46 45 52 45 4e 43 45 53 20 70 20  (j REFERENCES p 
0930: 4f 4e 20 55 50 44 41 54 45 20 43 41 53 43 41 44  ON UPDATE CASCAD
0940: 45 29 3b 0a 20 20 20 20 20 20 49 4e 53 45 52 54  E);.      INSERT
0950: 20 49 4e 54 4f 20 70 20 56 41 4c 55 45 53 28 27   INTO p VALUES('
0960: 68 65 6c 6c 6f 27 29 3b 0a 20 20 20 20 20 20 49  hello');.      I
0970: 4e 53 45 52 54 20 49 4e 54 4f 20 63 20 56 41 4c  NSERT INTO c VAL
0980: 55 45 53 28 27 68 65 6c 6c 6f 27 29 3b 0a 20 20  UES('hello');.  
0990: 20 20 20 20 55 50 44 41 54 45 20 70 20 53 45 54      UPDATE p SET
09a0: 20 69 20 3d 20 27 77 6f 72 6c 64 27 3b 0a 20 20   i = 'world';.  
09b0: 20 20 20 20 53 45 4c 45 43 54 20 2a 20 46 52 4f      SELECT * FRO
09c0: 4d 20 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 20 7b  M c;.    }.  } {
09d0: 68 65 6c 6c 6f 7d 0a 20 20 64 6f 5f 74 65 73 74  hello}.  do_test
09e0: 20 65 5f 66 6b 65 79 2d 35 31 2e 32 20 7b 0a 20   e_fkey-51.2 {. 
09f0: 20 20 20 65 78 65 63 73 71 6c 20 7b 20 50 52 41     execsql { PRA
0a00: 47 4d 41 20 66 6f 72 65 69 67 6e 5f 6b 65 79 5f  GMA foreign_key_
0a10: 6c 69 73 74 28 63 29 20 7d 0a 20 20 7d 20 7b 30  list(c) }.  } {0
0a20: 20 30 20 70 20 6a 20 7b 7d 20 43 41 53 43 41 44   0 p j {} CASCAD
0a30: 45 20 7b 4e 4f 20 41 43 54 49 4f 4e 7d 20 4e 4f  E {NO ACTION} NO
0a40: 4e 45 7d 0a 20 20 64 6f 5f 74 65 73 74 20 65 5f  NE}.  do_test e_
0a50: 66 6b 65 79 2d 35 31 2e 33 20 7b 0a 20 20 20 20  fkey-51.3 {.    
0a60: 65 78 65 63 73 71 6c 20 7b 20 50 52 41 47 4d 41  execsql { PRAGMA
0a70: 20 66 6f 72 65 69 67 6e 5f 6b 65 79 73 20 7d 0a   foreign_keys }.
0a80: 20 20 7d 20 7b 7d 0a 7d 0a 0a 0a 23 2d 2d 2d 2d    } {}.}...#----
0a90: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0aa0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0ab0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0ac0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0ad0: 2d 2d 2d 2d 2d 0a 23 20 2f 2a 20 45 56 3a 20 52  -----.# /* EV: R
0ae0: 2d 35 38 34 32 38 2d 33 36 36 36 30 20 2a 2f 0a  -58428-36660 */.
0af0: 23 0a 23 20 54 65 73 74 20 74 68 65 20 65 66 66  #.# Test the eff
0b00: 65 63 74 73 20 6f 66 20 64 65 66 69 6e 69 6e 67  ects of defining
0b10: 20 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45   OMIT_FOREIGN_KE
0b20: 59 2e 0a 23 0a 23 20 2f 2a 20 45 56 3a 20 52 2d  Y..#.# /* EV: R-
0b30: 35 38 34 32 38 2d 33 36 36 36 30 20 2a 2f 0a 23  58428-36660 */.#
0b40: 0a 23 20 53 70 65 63 69 66 69 63 61 6c 6c 79 2c  .# Specifically,
0b50: 20 74 65 73 74 20 74 68 61 74 20 66 6f 72 65 69   test that forei
0b60: 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e  gn key constrain
0b70: 74 73 20 63 61 6e 6e 6f 74 20 65 76 65 6e 20 62  ts cannot even b
0b80: 65 20 70 61 72 73 65 64 20 0a 23 20 69 6e 20 73  e parsed .# in s
0b90: 75 63 68 20 61 20 62 75 69 6c 64 2e 0a 23 0a 72  uch a build..#.r
0ba0: 65 73 65 74 5f 64 62 0a 69 66 63 61 70 61 62 6c  eset_db.ifcapabl
0bb0: 65 20 21 66 6f 72 65 69 67 6e 6b 65 79 20 7b 0a  e !foreignkey {.
0bc0: 20 20 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79    do_test e_fkey
0bd0: 2d 35 32 2e 31 20 7b 0a 20 20 20 20 65 78 65 63  -52.1 {.    exec
0be0: 73 71 6c 20 7b 20 43 52 45 41 54 45 20 54 41 42  sql { CREATE TAB
0bf0: 4c 45 20 70 28 69 20 50 52 49 4d 41 52 59 20 4b  LE p(i PRIMARY K
0c00: 45 59 29 20 7d 0a 20 20 20 20 63 61 74 63 68 73  EY) }.    catchs
0c10: 71 6c 20 7b 20 43 52 45 41 54 45 20 54 41 42 4c  ql { CREATE TABL
0c20: 45 20 63 28 6a 20 52 45 46 45 52 45 4e 43 45 53  E c(j REFERENCES
0c30: 20 70 20 4f 4e 20 55 50 44 41 54 45 20 43 41 53   p ON UPDATE CAS
0c40: 43 41 44 45 29 20 7d 0a 20 20 7d 20 7b 31 20 7b  CADE) }.  } {1 {
0c50: 6e 65 61 72 20 22 4f 4e 22 3a 20 73 79 6e 74 61  near "ON": synta
0c60: 78 20 65 72 72 6f 72 7d 7d 0a 20 20 64 6f 5f 74  x error}}.  do_t
0c70: 65 73 74 20 65 5f 66 6b 65 79 2d 35 32 2e 32 20  est e_fkey-52.2 
0c80: 7b 0a 20 20 20 20 23 20 54 68 69 73 20 69 73 20  {.    # This is 
0c90: 61 6c 6c 6f 77 65 64 2c 20 61 73 20 69 6e 20 74  allowed, as in t
0ca0: 68 69 73 20 62 75 69 6c 64 2c 20 22 52 45 46 45  his build, "REFE
0cb0: 52 45 4e 43 45 53 22 20 69 73 20 6e 6f 74 20 61  RENCES" is not a
0cc0: 20 6b 65 79 77 6f 72 64 2e 0a 20 20 20 20 23 20   keyword..    # 
0cd0: 54 68 65 20 64 65 63 6c 61 72 65 64 20 64 61 74  The declared dat
0ce0: 61 74 79 70 65 20 6f 66 20 63 6f 6c 75 6d 6e 20  atype of column 
0cf0: 6a 20 69 73 20 22 52 45 46 45 52 45 4e 43 45 53  j is "REFERENCES
0d00: 20 70 22 2e 0a 20 20 20 20 65 78 65 63 73 71 6c   p"..    execsql
0d10: 20 7b 20 43 52 45 41 54 45 20 54 41 42 4c 45 20   { CREATE TABLE 
0d20: 63 28 6a 20 52 45 46 45 52 45 4e 43 45 53 20 70  c(j REFERENCES p
0d30: 29 20 7d 0a 20 20 7d 20 7b 7d 0a 20 20 64 6f 5f  ) }.  } {}.  do_
0d40: 74 65 73 74 20 65 5f 66 6b 65 79 2d 35 32 2e 33  test e_fkey-52.3
0d50: 20 7b 0a 20 20 20 20 65 78 65 63 73 71 6c 20 7b   {.    execsql {
0d60: 20 50 52 41 47 4d 41 20 74 61 62 6c 65 5f 69 6e   PRAGMA table_in
0d70: 66 6f 28 63 29 20 7d 0a 20 20 7d 20 7b 30 20 6a  fo(c) }.  } {0 j
0d80: 20 7b 52 45 46 45 52 45 4e 43 45 53 20 70 7d 20   {REFERENCES p} 
0d90: 30 20 7b 7d 20 30 7d 0a 20 20 64 6f 5f 74 65 73  0 {} 0}.  do_tes
0da0: 74 20 65 5f 66 6b 65 79 2d 35 32 2e 34 20 7b 0a  t e_fkey-52.4 {.
0db0: 20 20 20 20 65 78 65 63 73 71 6c 20 7b 20 50 52      execsql { PR
0dc0: 41 47 4d 41 20 66 6f 72 65 69 67 6e 5f 6b 65 79  AGMA foreign_key
0dd0: 5f 6c 69 73 74 28 63 29 20 7d 0a 20 20 7d 20 7b  _list(c) }.  } {
0de0: 7d 0a 20 20 64 6f 5f 74 65 73 74 20 65 5f 66 6b  }.  do_test e_fk
0df0: 65 79 2d 35 32 2e 35 20 7b 0a 20 20 20 20 65 78  ey-52.5 {.    ex
0e00: 65 63 73 71 6c 20 7b 20 50 52 41 47 4d 41 20 66  ecsql { PRAGMA f
0e10: 6f 72 65 69 67 6e 5f 6b 65 79 73 20 7d 0a 20 20  oreign_keys }.  
0e20: 7d 20 7b 7d 0a 7d 0a 0a 69 66 63 61 70 61 62 6c  } {}.}..ifcapabl
0e30: 65 20 21 66 6f 72 65 69 67 6e 6b 65 79 7c 7c 21  e !foreignkey||!
0e40: 74 72 69 67 67 65 72 20 7b 20 66 69 6e 69 73 68  trigger { finish
0e50: 5f 74 65 73 74 20 3b 20 72 65 74 75 72 6e 20 7d  _test ; return }
0e60: 0a 72 65 73 65 74 5f 64 62 0a 0a 0a 23 2d 2d 2d  .reset_db...#---
0e70: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0e80: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0e90: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0ea0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0eb0: 2d 2d 2d 2d 2d 2d 0a 23 20 2f 2a 20 45 56 3a 20  ------.# /* EV: 
0ec0: 52 2d 30 37 32 38 30 2d 36 30 35 31 30 20 2a 2f  R-07280-60510 */
0ed0: 0a 23 0a 23 20 54 65 73 74 20 74 68 61 74 20 65  .#.# Test that e
0ee0: 76 65 6e 20 69 66 20 66 6f 72 65 69 67 6e 20 6b  ven if foreign k
0ef0: 65 79 73 20 61 72 65 20 73 75 70 70 6f 72 74 65  eys are supporte
0f00: 64 20 62 79 20 74 68 65 20 62 75 69 6c 64 2c 20  d by the build, 
0f10: 74 68 65 79 20 6d 75 73 74 0a 23 20 62 65 20 65  they must.# be e
0f20: 6e 61 62 6c 65 64 20 75 73 69 6e 67 20 22 50 52  nabled using "PR
0f30: 41 47 4d 41 20 66 6f 72 65 69 67 6e 5f 6b 65 79  AGMA foreign_key
0f40: 73 20 3d 20 4f 4e 22 20 28 6f 72 20 73 69 6d 69  s = ON" (or simi
0f50: 6c 61 72 29 2e 0a 23 0a 23 20 2f 2a 20 45 56 3a  lar)..#.# /* EV:
0f60: 20 52 2d 35 39 35 37 38 2d 30 34 39 39 30 20 2a   R-59578-04990 *
0f70: 2f 0a 23 0a 23 20 54 68 69 73 20 61 6c 73 6f 20  /.#.# This also 
0f80: 74 65 73 74 73 20 74 68 61 74 20 66 6f 72 65 69  tests that forei
0f90: 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e  gn key constrain
0fa0: 74 73 20 61 72 65 20 64 69 73 61 62 6c 65 64 20  ts are disabled 
0fb0: 62 79 20 64 65 66 61 75 6c 74 2e 0a 23 0a 64 72  by default..#.dr
0fc0: 6f 70 5f 61 6c 6c 5f 74 61 62 6c 65 73 0a 64 6f  op_all_tables.do
0fd0: 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 35 33 2e  _test e_fkey-53.
0fe0: 31 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a  1 {.  execsql {.
0ff0: 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45      CREATE TABLE
1000: 20 70 28 69 20 50 52 49 4d 41 52 59 20 4b 45 59   p(i PRIMARY KEY
1010: 29 3b 0a 20 20 20 20 43 52 45 41 54 45 20 54 41  );.    CREATE TA
1020: 42 4c 45 20 63 28 6a 20 52 45 46 45 52 45 4e 43  BLE c(j REFERENC
1030: 45 53 20 70 20 4f 4e 20 55 50 44 41 54 45 20 43  ES p ON UPDATE C
1040: 41 53 43 41 44 45 29 3b 0a 20 20 20 20 49 4e 53  ASCADE);.    INS
1050: 45 52 54 20 49 4e 54 4f 20 70 20 56 41 4c 55 45  ERT INTO p VALUE
1060: 53 28 27 68 65 6c 6c 6f 27 29 3b 0a 20 20 20 20  S('hello');.    
1070: 49 4e 53 45 52 54 20 49 4e 54 4f 20 63 20 56 41  INSERT INTO c VA
1080: 4c 55 45 53 28 27 68 65 6c 6c 6f 27 29 3b 0a 20  LUES('hello');. 
1090: 20 20 20 55 50 44 41 54 45 20 70 20 53 45 54 20     UPDATE p SET 
10a0: 69 20 3d 20 27 77 6f 72 6c 64 27 3b 0a 20 20 20  i = 'world';.   
10b0: 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 63   SELECT * FROM c
10c0: 3b 0a 20 20 7d 20 0a 7d 20 7b 68 65 6c 6c 6f 7d  ;.  } .} {hello}
10d0: 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d  .do_test e_fkey-
10e0: 35 33 2e 32 20 7b 0a 20 20 65 78 65 63 73 71 6c  53.2 {.  execsql
10f0: 20 7b 0a 20 20 20 20 44 45 4c 45 54 45 20 46 52   {.    DELETE FR
1100: 4f 4d 20 63 3b 0a 20 20 20 20 44 45 4c 45 54 45  OM c;.    DELETE
1110: 20 46 52 4f 4d 20 70 3b 0a 20 20 20 20 50 52 41   FROM p;.    PRA
1120: 47 4d 41 20 66 6f 72 65 69 67 6e 5f 6b 65 79 73  GMA foreign_keys
1130: 20 3d 20 4f 4e 3b 0a 20 20 20 20 49 4e 53 45 52   = ON;.    INSER
1140: 54 20 49 4e 54 4f 20 70 20 56 41 4c 55 45 53 28  T INTO p VALUES(
1150: 27 68 65 6c 6c 6f 27 29 3b 0a 20 20 20 20 49 4e  'hello');.    IN
1160: 53 45 52 54 20 49 4e 54 4f 20 63 20 56 41 4c 55  SERT INTO c VALU
1170: 45 53 28 27 68 65 6c 6c 6f 27 29 3b 0a 20 20 20  ES('hello');.   
1180: 20 55 50 44 41 54 45 20 70 20 53 45 54 20 69 20   UPDATE p SET i 
1190: 3d 20 27 77 6f 72 6c 64 27 3b 0a 20 20 20 20 53  = 'world';.    S
11a0: 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 63 3b 0a  ELECT * FROM c;.
11b0: 20 20 7d 20 0a 7d 20 7b 77 6f 72 6c 64 7d 0a 0a    } .} {world}..
11c0: 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  #---------------
11d0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
11e0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
11f0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1200: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20 2f 2a 20  ----------.# /* 
1210: 45 56 3a 20 52 2d 31 35 32 37 38 2d 35 34 34 35  EV: R-15278-5445
1220: 36 20 2a 2f 0a 23 20 2f 2a 20 45 56 3a 20 52 2d  6 */.# /* EV: R-
1230: 31 31 32 35 35 2d 31 39 39 30 37 20 2a 2f 0a 23  11255-19907 */.#
1240: 0a 23 20 54 65 73 74 20 74 68 61 74 20 74 68 65  .# Test that the
1250: 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 63 61 6e   application can
1260: 20 75 73 65 20 22 50 52 41 47 4d 41 20 66 6f 72   use "PRAGMA for
1270: 65 69 67 6e 5f 6b 65 79 73 22 20 74 6f 20 71 75  eign_keys" to qu
1280: 65 72 79 20 66 6f 72 0a 23 20 77 68 65 74 68 65  ery for.# whethe
1290: 72 20 6f 72 20 6e 6f 74 20 66 6f 72 65 69 67 6e  r or not foreign
12a0: 20 6b 65 79 73 20 61 72 65 20 63 75 72 72 65 6e   keys are curren
12b0: 74 6c 79 20 65 6e 61 62 6c 65 64 2e 20 54 68 69  tly enabled. Thi
12c0: 73 20 61 6c 73 6f 20 74 65 73 74 73 0a 23 20 74  s also tests.# t
12d0: 68 65 20 65 78 61 6d 70 6c 65 20 63 6f 64 65 20  he example code 
12e0: 69 6e 20 73 65 63 74 69 6f 6e 20 32 20 6f 66 20  in section 2 of 
12f0: 66 6f 72 65 69 67 6e 6b 65 79 73 2e 69 6e 2e 0a  foreignkeys.in..
1300: 23 0a 72 65 73 65 74 5f 64 62 0a 64 6f 5f 74 65  #.reset_db.do_te
1310: 73 74 20 65 5f 66 6b 65 79 2d 35 34 2e 31 20 7b  st e_fkey-54.1 {
1320: 0a 20 20 65 78 65 63 73 71 6c 20 7b 20 50 52 41  .  execsql { PRA
1330: 47 4d 41 20 66 6f 72 65 69 67 6e 5f 6b 65 79 73  GMA foreign_keys
1340: 20 7d 0a 7d 20 7b 30 7d 0a 64 6f 5f 74 65 73 74   }.} {0}.do_test
1350: 20 65 5f 66 6b 65 79 2d 35 34 2e 32 20 7b 0a 20   e_fkey-54.2 {. 
1360: 20 65 78 65 63 73 71 6c 20 7b 20 0a 20 20 20 20   execsql { .    
1370: 50 52 41 47 4d 41 20 66 6f 72 65 69 67 6e 5f 6b  PRAGMA foreign_k
1380: 65 79 73 20 3d 20 4f 4e 3b 0a 20 20 20 20 50 52  eys = ON;.    PR
1390: 41 47 4d 41 20 66 6f 72 65 69 67 6e 5f 6b 65 79  AGMA foreign_key
13a0: 73 3b 0a 20 20 7d 0a 7d 20 7b 31 7d 0a 64 6f 5f  s;.  }.} {1}.do_
13b0: 74 65 73 74 20 65 5f 66 6b 65 79 2d 35 34 2e 33  test e_fkey-54.3
13c0: 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 20 0a   {.  execsql { .
13d0: 20 20 20 20 50 52 41 47 4d 41 20 66 6f 72 65 69      PRAGMA forei
13e0: 67 6e 5f 6b 65 79 73 20 3d 20 4f 46 46 3b 0a 20  gn_keys = OFF;. 
13f0: 20 20 20 50 52 41 47 4d 41 20 66 6f 72 65 69 67     PRAGMA foreig
1400: 6e 5f 6b 65 79 73 3b 0a 20 20 7d 0a 7d 20 7b 30  n_keys;.  }.} {0
1410: 7d 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  }..#------------
1420: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1430: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1440: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1450: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20  -------------.# 
1460: 2f 2a 20 45 56 3a 20 52 2d 34 36 36 34 39 2d 35  /* EV: R-46649-5
1470: 38 35 33 37 20 2a 2f 0a 23 0a 23 20 54 65 73 74  8537 */.#.# Test
1480: 20 74 68 61 74 20 69 74 20 69 73 20 6e 6f 74 20   that it is not 
1490: 70 6f 73 73 69 62 6c 65 20 74 6f 20 65 6e 61 62  possible to enab
14a0: 6c 65 20 6f 72 20 64 69 73 61 62 6c 65 20 66 6f  le or disable fo
14b0: 72 65 69 67 6e 20 6b 65 79 20 73 75 70 70 6f 72  reign key suppor
14c0: 74 0a 23 20 77 68 69 6c 65 20 6e 6f 74 20 69 6e  t.# while not in
14d0: 20 61 75 74 6f 2d 63 6f 6d 6d 69 74 20 6d 6f 64   auto-commit mod
14e0: 65 2e 0a 23 0a 72 65 73 65 74 5f 64 62 0a 64 6f  e..#.reset_db.do
14f0: 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 35 35 2e  _test e_fkey-55.
1500: 31 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a  1 {.  execsql {.
1510: 20 20 20 20 50 52 41 47 4d 41 20 66 6f 72 65 69      PRAGMA forei
1520: 67 6e 5f 6b 65 79 73 20 3d 20 4f 4e 3b 0a 20 20  gn_keys = ON;.  
1530: 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 74    CREATE TABLE t
1540: 31 28 61 20 55 4e 49 51 55 45 2c 20 62 29 3b 0a  1(a UNIQUE, b);.
1550: 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45      CREATE TABLE
1560: 20 74 32 28 63 2c 20 64 20 52 45 46 45 52 45 4e   t2(c, d REFEREN
1570: 43 45 53 20 74 31 28 61 29 29 3b 0a 20 20 20 20  CES t1(a));.    
1580: 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 31 20 56  INSERT INTO t1 V
1590: 41 4c 55 45 53 28 31 2c 20 32 29 3b 0a 20 20 20  ALUES(1, 2);.   
15a0: 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 32 20   INSERT INTO t2 
15b0: 56 41 4c 55 45 53 28 32 2c 20 31 29 3b 0a 20 20  VALUES(2, 1);.  
15c0: 20 20 42 45 47 49 4e 3b 0a 20 20 20 20 20 20 50    BEGIN;.      P
15d0: 52 41 47 4d 41 20 66 6f 72 65 69 67 6e 5f 6b 65  RAGMA foreign_ke
15e0: 79 73 20 3d 20 4f 46 46 3b 0a 20 20 7d 0a 20 20  ys = OFF;.  }.  
15f0: 63 61 74 63 68 73 71 6c 20 7b 0a 20 20 20 20 20  catchsql {.     
1600: 20 44 45 4c 45 54 45 20 46 52 4f 4d 20 74 31 0a   DELETE FROM t1.
1610: 20 20 7d 0a 7d 20 7b 31 20 7b 66 6f 72 65 69 67    }.} {1 {foreig
1620: 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74  n key constraint
1630: 20 66 61 69 6c 65 64 7d 7d 0a 64 6f 5f 74 65 73   failed}}.do_tes
1640: 74 20 65 5f 66 6b 65 79 2d 35 35 2e 32 20 7b 0a  t e_fkey-55.2 {.
1650: 20 20 65 78 65 63 73 71 6c 20 7b 20 50 52 41 47    execsql { PRAG
1660: 4d 41 20 66 6f 72 65 69 67 6e 5f 6b 65 79 73 20  MA foreign_keys 
1670: 7d 0a 7d 20 7b 31 7d 0a 64 6f 5f 74 65 73 74 20  }.} {1}.do_test 
1680: 65 5f 66 6b 65 79 2d 35 35 2e 33 20 7b 0a 20 20  e_fkey-55.3 {.  
1690: 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 43 4f  execsql {.    CO
16a0: 4d 4d 49 54 3b 0a 20 20 20 20 50 52 41 47 4d 41  MMIT;.    PRAGMA
16b0: 20 66 6f 72 65 69 67 6e 5f 6b 65 79 73 20 3d 20   foreign_keys = 
16c0: 4f 46 46 3b 0a 20 20 20 20 42 45 47 49 4e 3b 0a  OFF;.    BEGIN;.
16d0: 20 20 20 20 20 20 50 52 41 47 4d 41 20 66 6f 72        PRAGMA for
16e0: 65 69 67 6e 5f 6b 65 79 73 20 3d 20 4f 4e 3b 0a  eign_keys = ON;.
16f0: 20 20 20 20 20 20 44 45 4c 45 54 45 20 46 52 4f        DELETE FRO
1700: 4d 20 74 31 3b 0a 20 20 20 20 20 20 50 52 41 47  M t1;.      PRAG
1710: 4d 41 20 66 6f 72 65 69 67 6e 5f 6b 65 79 73 3b  MA foreign_keys;
1720: 0a 20 20 7d 0a 7d 20 7b 30 7d 0a 64 6f 5f 74 65  .  }.} {0}.do_te
1730: 73 74 20 65 5f 66 6b 65 79 2d 35 35 2e 34 20 7b  st e_fkey-55.4 {
1740: 0a 20 20 65 78 65 63 73 71 6c 20 43 4f 4d 4d 49  .  execsql COMMI
1750: 54 0a 7d 20 7b 7d 0a 0a 23 23 23 23 23 23 23 23  T.} {}..########
1760: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
1770: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
1780: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
1790: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
17a0: 23 23 23 0a 23 23 23 20 53 45 43 54 49 4f 4e 20  ###.### SECTION 
17b0: 31 3a 20 49 6e 74 72 6f 64 75 63 74 69 6f 6e 20  1: Introduction 
17c0: 74 6f 20 46 6f 72 65 69 67 6e 20 4b 65 79 20 43  to Foreign Key C
17d0: 6f 6e 73 74 72 61 69 6e 74 73 0a 23 23 23 23 23  onstraints.#####
17e0: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
17f0: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
1800: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
1810: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
1820: 23 23 23 23 23 23 0a 65 78 65 63 73 71 6c 20 22  ######.execsql "
1830: 50 52 41 47 4d 41 20 66 6f 72 65 69 67 6e 5f 6b  PRAGMA foreign_k
1840: 65 79 73 20 3d 20 4f 4e 22 0a 0a 23 2d 2d 2d 2d  eys = ON"..#----
1850: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1860: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1870: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1880: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1890: 2d 2d 2d 2d 2d 0a 23 20 2f 2a 20 45 56 3a 20 52  -----.# /* EV: R
18a0: 2d 30 34 30 34 32 2d 32 34 38 32 35 20 2a 2f 0a  -04042-24825 */.
18b0: 23 0a 23 20 56 65 72 69 66 79 20 74 68 61 74 20  #.# Verify that 
18c0: 74 68 65 20 73 79 6e 74 61 78 20 69 6e 20 74 68  the syntax in th
18d0: 65 20 66 69 72 73 74 20 65 78 61 6d 70 6c 65 20  e first example 
18e0: 69 6e 20 73 65 63 74 69 6f 6e 20 31 20 69 73 20  in section 1 is 
18f0: 76 61 6c 69 64 2e 0a 23 0a 64 6f 5f 74 65 73 74  valid..#.do_test
1900: 20 65 5f 66 6b 65 79 2d 33 38 2e 31 20 7b 0a 20   e_fkey-38.1 {. 
1910: 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 43   execsql {.    C
1920: 52 45 41 54 45 20 54 41 42 4c 45 20 61 72 74 69  REATE TABLE arti
1930: 73 74 28 0a 20 20 20 20 20 20 61 72 74 69 73 74  st(.      artist
1940: 69 64 20 20 20 20 49 4e 54 45 47 45 52 20 50 52  id    INTEGER PR
1950: 49 4d 41 52 59 20 4b 45 59 2c 20 0a 20 20 20 20  IMARY KEY, .    
1960: 20 20 61 72 74 69 73 74 6e 61 6d 65 20 20 54 45    artistname  TE
1970: 58 54 0a 20 20 20 20 29 3b 0a 20 20 20 20 43 52  XT.    );.    CR
1980: 45 41 54 45 20 54 41 42 4c 45 20 74 72 61 63 6b  EATE TABLE track
1990: 28 0a 20 20 20 20 20 20 74 72 61 63 6b 69 64 20  (.      trackid 
19a0: 20 20 20 20 49 4e 54 45 47 45 52 2c 20 0a 20 20      INTEGER, .  
19b0: 20 20 20 20 74 72 61 63 6b 6e 61 6d 65 20 20 20      trackname   
19c0: 54 45 58 54 2c 20 0a 20 20 20 20 20 20 74 72 61  TEXT, .      tra
19d0: 63 6b 61 72 74 69 73 74 20 49 4e 54 45 47 45 52  ckartist INTEGER
19e0: 2c 0a 20 20 20 20 20 20 46 4f 52 45 49 47 4e 20  ,.      FOREIGN 
19f0: 4b 45 59 28 74 72 61 63 6b 61 72 74 69 73 74 29  KEY(trackartist)
1a00: 20 52 45 46 45 52 45 4e 43 45 53 20 61 72 74 69   REFERENCES arti
1a10: 73 74 28 61 72 74 69 73 74 69 64 29 0a 20 20 20  st(artistid).   
1a20: 20 29 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a 0a 23 2d   );.  }.} {}..#-
1a30: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1a40: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1a50: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1a60: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1a70: 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20 2f 2a 20 45 56  --------.# /* EV
1a80: 3a 20 52 2d 36 31 33 36 32 2d 33 32 30 38 37 20  : R-61362-32087 
1a90: 2a 2f 0a 23 0a 23 20 41 74 74 65 6d 70 74 69 6e  */.#.# Attemptin
1aa0: 67 20 74 6f 20 69 6e 73 65 72 74 20 61 20 72 6f  g to insert a ro
1ab0: 77 20 69 6e 74 6f 20 74 68 65 20 27 74 72 61 63  w into the 'trac
1ac0: 6b 27 20 74 61 62 6c 65 20 74 68 61 74 20 63 6f  k' table that co
1ad0: 72 72 65 73 70 6f 6e 64 73 0a 23 20 74 6f 20 6e  rresponds.# to n
1ae0: 6f 20 72 6f 77 20 69 6e 20 74 68 65 20 27 61 72  o row in the 'ar
1af0: 74 69 73 74 27 20 74 61 62 6c 65 20 66 61 69 6c  tist' table fail
1b00: 73 2e 0a 23 0a 64 6f 5f 74 65 73 74 20 65 5f 66  s..#.do_test e_f
1b10: 6b 65 79 2d 33 39 2e 31 20 7b 0a 20 20 63 61 74  key-39.1 {.  cat
1b20: 63 68 73 71 6c 20 7b 20 49 4e 53 45 52 54 20 49  chsql { INSERT I
1b30: 4e 54 4f 20 74 72 61 63 6b 20 56 41 4c 55 45 53  NTO track VALUES
1b40: 28 31 2c 20 27 74 72 61 63 6b 20 31 27 2c 20 31  (1, 'track 1', 1
1b50: 29 20 7d 0a 7d 20 7b 31 20 7b 66 6f 72 65 69 67  ) }.} {1 {foreig
1b60: 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74  n key constraint
1b70: 20 66 61 69 6c 65 64 7d 7d 0a 64 6f 5f 74 65 73   failed}}.do_tes
1b80: 74 20 65 5f 66 6b 65 79 2d 33 39 2e 32 20 7b 0a  t e_fkey-39.2 {.
1b90: 20 20 65 78 65 63 73 71 6c 20 7b 20 49 4e 53 45    execsql { INSE
1ba0: 52 54 20 49 4e 54 4f 20 61 72 74 69 73 74 20 56  RT INTO artist V
1bb0: 41 4c 55 45 53 28 32 2c 20 27 61 72 74 69 73 74  ALUES(2, 'artist
1bc0: 20 31 27 29 20 7d 0a 20 20 63 61 74 63 68 73 71   1') }.  catchsq
1bd0: 6c 20 7b 20 49 4e 53 45 52 54 20 49 4e 54 4f 20  l { INSERT INTO 
1be0: 74 72 61 63 6b 20 56 41 4c 55 45 53 28 31 2c 20  track VALUES(1, 
1bf0: 27 74 72 61 63 6b 20 31 27 2c 20 31 29 20 7d 0a  'track 1', 1) }.
1c00: 7d 20 7b 31 20 7b 66 6f 72 65 69 67 6e 20 6b 65  } {1 {foreign ke
1c10: 79 20 63 6f 6e 73 74 72 61 69 6e 74 20 66 61 69  y constraint fai
1c20: 6c 65 64 7d 7d 0a 64 6f 5f 74 65 73 74 20 65 5f  led}}.do_test e_
1c30: 66 6b 65 79 2d 33 39 2e 32 20 7b 0a 20 20 65 78  fkey-39.2 {.  ex
1c40: 65 63 73 71 6c 20 7b 20 49 4e 53 45 52 54 20 49  ecsql { INSERT I
1c50: 4e 54 4f 20 74 72 61 63 6b 20 56 41 4c 55 45 53  NTO track VALUES
1c60: 28 31 2c 20 27 74 72 61 63 6b 20 31 27 2c 20 32  (1, 'track 1', 2
1c70: 29 20 7d 0a 7d 20 7b 7d 0a 0a 23 2d 2d 2d 2d 2d  ) }.} {}..#-----
1c80: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1c90: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1ca0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1cb0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1cc0: 2d 2d 2d 2d 0a 23 20 2f 2a 20 45 56 3a 20 52 2d  ----.# /* EV: R-
1cd0: 32 34 34 30 31 2d 35 32 34 30 30 20 2a 2f 0a 23  24401-52400 */.#
1ce0: 0a 23 20 41 74 74 65 6d 70 74 69 6e 67 20 74 6f  .# Attempting to
1cf0: 20 64 65 6c 65 74 65 20 61 20 72 6f 77 20 66 72   delete a row fr
1d00: 6f 6d 20 74 68 65 20 27 61 72 74 69 73 74 27 20  om the 'artist' 
1d10: 74 61 62 6c 65 20 77 68 69 6c 65 20 74 68 65 72  table while ther
1d20: 65 20 61 72 65 20 0a 23 20 64 65 70 65 6e 64 65  e are .# depende
1d30: 6e 74 20 72 6f 77 73 20 69 6e 20 74 68 65 20 74  nt rows in the t
1d40: 72 61 63 6b 20 74 61 62 6c 65 20 61 6c 73 6f 20  rack table also 
1d50: 66 61 69 6c 73 2e 0a 23 0a 64 6f 5f 74 65 73 74  fails..#.do_test
1d60: 20 65 5f 66 6b 65 79 2d 34 30 2e 31 20 7b 0a 20   e_fkey-40.1 {. 
1d70: 20 63 61 74 63 68 73 71 6c 20 7b 20 44 45 4c 45   catchsql { DELE
1d80: 54 45 20 46 52 4f 4d 20 61 72 74 69 73 74 20 57  TE FROM artist W
1d90: 48 45 52 45 20 61 72 74 69 73 74 69 64 20 3d 20  HERE artistid = 
1da0: 32 20 7d 0a 7d 20 7b 31 20 7b 66 6f 72 65 69 67  2 }.} {1 {foreig
1db0: 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74  n key constraint
1dc0: 20 66 61 69 6c 65 64 7d 7d 0a 64 6f 5f 74 65 73   failed}}.do_tes
1dd0: 74 20 65 5f 66 6b 65 79 2d 34 30 2e 32 20 7b 0a  t e_fkey-40.2 {.
1de0: 20 20 65 78 65 63 73 71 6c 20 7b 20 0a 20 20 20    execsql { .   
1df0: 20 44 45 4c 45 54 45 20 46 52 4f 4d 20 74 72 61   DELETE FROM tra
1e00: 63 6b 20 57 48 45 52 45 20 74 72 61 63 6b 61 72  ck WHERE trackar
1e10: 74 69 73 74 20 3d 20 32 3b 0a 20 20 20 20 44 45  tist = 2;.    DE
1e20: 4c 45 54 45 20 46 52 4f 4d 20 61 72 74 69 73 74  LETE FROM artist
1e30: 20 57 48 45 52 45 20 61 72 74 69 73 74 69 64 20   WHERE artistid 
1e40: 3d 20 32 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a 0a 23  = 2;.  }.} {}..#
1e50: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1e60: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1e70: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1e80: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1e90: 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20 2f 2a 20 45  ---------.# /* E
1ea0: 56 3a 20 52 2d 32 33 39 38 30 2d 34 38 38 35 39  V: R-23980-48859
1eb0: 20 2a 2f 0a 23 0a 23 20 49 66 20 74 68 65 20 66   */.#.# If the f
1ec0: 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6c 75 6d  oreign key colum
1ed0: 6e 20 28 74 72 61 63 6b 61 72 74 69 73 74 29 20  n (trackartist) 
1ee0: 69 6e 20 74 61 62 6c 65 20 27 74 72 61 63 6b 27  in table 'track'
1ef0: 20 69 73 20 73 65 74 20 74 6f 20 4e 55 4c 4c 2c   is set to NULL,
1f00: 0a 23 20 74 68 65 72 65 20 69 73 20 6e 6f 20 72  .# there is no r
1f10: 65 71 75 69 72 65 6d 65 6e 74 20 66 6f 72 20 61  equirement for a
1f20: 20 6d 61 74 63 68 69 6e 67 20 72 6f 77 20 69 6e   matching row in
1f30: 20 74 68 65 20 27 61 72 74 69 73 74 27 20 74 61   the 'artist' ta
1f40: 62 6c 65 2e 0a 23 0a 64 6f 5f 74 65 73 74 20 65  ble..#.do_test e
1f50: 5f 66 6b 65 79 2d 34 31 2e 31 20 7b 0a 20 20 65  _fkey-41.1 {.  e
1f60: 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 49 4e 53  xecsql {.    INS
1f70: 45 52 54 20 49 4e 54 4f 20 74 72 61 63 6b 20 56  ERT INTO track V
1f80: 41 4c 55 45 53 28 31 2c 20 27 74 72 61 63 6b 20  ALUES(1, 'track 
1f90: 31 27 2c 20 4e 55 4c 4c 29 3b 0a 20 20 20 20 49  1', NULL);.    I
1fa0: 4e 53 45 52 54 20 49 4e 54 4f 20 74 72 61 63 6b  NSERT INTO track
1fb0: 20 56 41 4c 55 45 53 28 32 2c 20 27 74 72 61 63   VALUES(2, 'trac
1fc0: 6b 20 32 27 2c 20 4e 55 4c 4c 29 3b 0a 20 20 7d  k 2', NULL);.  }
1fd0: 0a 7d 20 7b 7d 0a 64 6f 5f 74 65 73 74 20 65 5f  .} {}.do_test e_
1fe0: 66 6b 65 79 2d 34 31 2e 32 20 7b 0a 20 20 65 78  fkey-41.2 {.  ex
1ff0: 65 63 73 71 6c 20 7b 20 53 45 4c 45 43 54 20 2a  ecsql { SELECT *
2000: 20 46 52 4f 4d 20 61 72 74 69 73 74 20 7d 0a 7d   FROM artist }.}
2010: 20 7b 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b   {}.do_test e_fk
2020: 65 79 2d 34 31 2e 33 20 7b 0a 20 20 23 20 53 65  ey-41.3 {.  # Se
2030: 74 74 69 6e 67 20 74 68 65 20 74 72 61 63 6b 69  tting the tracki
2040: 64 20 74 6f 20 61 20 6e 6f 6e 2d 4e 55 4c 4c 20  d to a non-NULL 
2050: 76 61 6c 75 65 20 66 61 69 6c 73 2c 20 6f 66 20  value fails, of 
2060: 63 6f 75 72 73 65 2e 0a 20 20 63 61 74 63 68 73  course..  catchs
2070: 71 6c 20 7b 20 55 50 44 41 54 45 20 74 72 61 63  ql { UPDATE trac
2080: 6b 20 53 45 54 20 74 72 61 63 6b 61 72 74 69 73  k SET trackartis
2090: 74 20 3d 20 35 20 57 48 45 52 45 20 74 72 61 63  t = 5 WHERE trac
20a0: 6b 69 64 20 3d 20 31 20 7d 0a 7d 20 7b 31 20 7b  kid = 1 }.} {1 {
20b0: 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73  foreign key cons
20c0: 74 72 61 69 6e 74 20 66 61 69 6c 65 64 7d 7d 0a  traint failed}}.
20d0: 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 34  do_test e_fkey-4
20e0: 31 2e 34 20 7b 0a 20 20 65 78 65 63 73 71 6c 20  1.4 {.  execsql 
20f0: 7b 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54  {.    INSERT INT
2100: 4f 20 61 72 74 69 73 74 20 56 41 4c 55 45 53 28  O artist VALUES(
2110: 35 2c 20 27 61 72 74 69 73 74 20 35 27 29 3b 0a  5, 'artist 5');.
2120: 20 20 20 20 55 50 44 41 54 45 20 74 72 61 63 6b      UPDATE track
2130: 20 53 45 54 20 74 72 61 63 6b 61 72 74 69 73 74   SET trackartist
2140: 20 3d 20 35 20 57 48 45 52 45 20 74 72 61 63 6b   = 5 WHERE track
2150: 69 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 63 61  id = 1;.  }.  ca
2160: 74 63 68 73 71 6c 20 7b 20 44 45 4c 45 54 45 20  tchsql { DELETE 
2170: 46 52 4f 4d 20 61 72 74 69 73 74 20 57 48 45 52  FROM artist WHER
2180: 45 20 61 72 74 69 73 74 69 64 20 3d 20 35 7d 0a  E artistid = 5}.
2190: 7d 20 7b 31 20 7b 66 6f 72 65 69 67 6e 20 6b 65  } {1 {foreign ke
21a0: 79 20 63 6f 6e 73 74 72 61 69 6e 74 20 66 61 69  y constraint fai
21b0: 6c 65 64 7d 7d 0a 64 6f 5f 74 65 73 74 20 65 5f  led}}.do_test e_
21c0: 66 6b 65 79 2d 34 31 2e 35 20 7b 0a 20 20 65 78  fkey-41.5 {.  ex
21d0: 65 63 73 71 6c 20 7b 20 0a 20 20 20 20 55 50 44  ecsql { .    UPD
21e0: 41 54 45 20 74 72 61 63 6b 20 53 45 54 20 74 72  ATE track SET tr
21f0: 61 63 6b 61 72 74 69 73 74 20 3d 20 4e 55 4c 4c  ackartist = NULL
2200: 20 57 48 45 52 45 20 74 72 61 63 6b 69 64 20 3d   WHERE trackid =
2210: 20 31 3b 0a 20 20 20 20 44 45 4c 45 54 45 20 46   1;.    DELETE F
2220: 52 4f 4d 20 61 72 74 69 73 74 20 57 48 45 52 45  ROM artist WHERE
2230: 20 61 72 74 69 73 74 69 64 20 3d 20 35 3b 0a 20   artistid = 5;. 
2240: 20 7d 0a 7d 20 7b 7d 0a 0a 23 2d 2d 2d 2d 2d 2d   }.} {}..#------
2250: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2260: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2270: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2280: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2290: 2d 2d 2d 0a 23 20 2f 2a 20 45 56 3a 20 52 2d 35  ---.# /* EV: R-5
22a0: 32 34 38 36 2d 32 31 33 35 32 20 2a 2f 0a 23 0a  2486-21352 */.#.
22b0: 23 20 54 65 73 74 20 74 68 61 74 20 74 68 65 20  # Test that the 
22c0: 66 6f 6c 6c 6f 77 69 6e 67 20 69 73 20 74 72 75  following is tru
22d0: 65 20 66 6f 20 61 6c 6c 20 72 6f 77 73 20 69 6e  e fo all rows in
22e0: 20 74 68 65 20 74 72 61 63 6b 20 74 61 62 6c 65   the track table
22f0: 3a 0a 23 0a 23 20 20 20 74 72 61 63 6b 61 72 74  :.#.#   trackart
2300: 69 73 74 20 49 53 20 4e 55 4c 4c 20 4f 52 20 0a  ist IS NULL OR .
2310: 23 20 20 20 45 58 49 53 54 53 28 53 45 4c 45 43  #   EXISTS(SELEC
2320: 54 20 31 20 46 52 4f 4d 20 61 72 74 69 73 74 20  T 1 FROM artist 
2330: 57 48 45 52 45 20 61 72 74 69 73 74 69 64 3d 74  WHERE artistid=t
2340: 72 61 63 6b 61 72 74 69 73 74 29 0a 23 0a 0a 23  rackartist).#..#
2350: 20 54 68 69 73 20 70 72 6f 63 65 64 75 72 65 20   This procedure 
2360: 65 78 65 63 75 74 65 73 20 61 20 74 65 73 74 20  executes a test 
2370: 63 61 73 65 20 74 6f 20 63 68 65 63 6b 20 74 68  case to check th
2380: 61 74 20 73 74 61 74 65 6d 65 6e 74 20 0a 23 20  at statement .# 
2390: 52 2d 35 32 34 38 36 2d 32 31 33 35 32 20 69 73  R-52486-21352 is
23a0: 20 74 72 75 65 20 61 66 74 65 72 20 65 78 65 63   true after exec
23b0: 75 74 69 6e 67 20 74 68 65 20 53 51 4c 20 73 74  uting the SQL st
23c0: 61 74 65 6d 65 6e 74 20 70 61 73 73 65 64 2e 0a  atement passed..
23d0: 23 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20  # as the second 
23e0: 61 72 67 75 6d 65 6e 74 2e 0a 70 72 6f 63 20 74  argument..proc t
23f0: 65 73 74 5f 72 35 32 34 38 36 5f 32 31 33 35 32  est_r52486_21352
2400: 20 7b 74 6e 20 73 71 6c 7d 20 7b 0a 20 20 73 65   {tn sql} {.  se
2410: 74 20 72 65 73 20 5b 63 61 74 63 68 73 71 6c 20  t res [catchsql 
2420: 24 73 71 6c 5d 0a 20 20 73 65 74 20 72 65 73 75  $sql].  set resu
2430: 6c 74 73 20 7b 0a 20 20 20 20 7b 30 20 7b 7d 7d  lts {.    {0 {}}
2440: 20 0a 20 20 20 20 7b 31 20 7b 50 52 49 4d 41 52   .    {1 {PRIMAR
2450: 59 20 4b 45 59 20 6d 75 73 74 20 62 65 20 75 6e  Y KEY must be un
2460: 69 71 75 65 7d 7d 20 0a 20 20 20 20 7b 31 20 7b  ique}} .    {1 {
2470: 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73  foreign key cons
2480: 74 72 61 69 6e 74 20 66 61 69 6c 65 64 7d 7d 0a  traint failed}}.
2490: 20 20 7d 0a 20 20 69 66 20 7b 5b 6c 73 65 61 72    }.  if {[lsear
24a0: 63 68 20 24 72 65 73 75 6c 74 73 20 24 72 65 73  ch $results $res
24b0: 5d 3c 30 7d 20 7b 0a 20 20 20 20 65 72 72 6f 72  ]<0} {.    error
24c0: 20 24 72 65 73 0a 20 20 7d 0a 0a 20 20 64 6f 5f   $res.  }..  do_
24d0: 74 65 73 74 20 65 5f 66 6b 65 79 2d 34 32 2e 24  test e_fkey-42.$
24e0: 74 6e 20 7b 0a 20 20 20 20 65 78 65 63 73 71 6c  tn {.    execsql
24f0: 20 7b 0a 20 20 20 20 20 20 53 45 4c 45 43 54 20   {.      SELECT 
2500: 63 6f 75 6e 74 28 2a 29 20 46 52 4f 4d 20 74 72  count(*) FROM tr
2510: 61 63 6b 20 57 48 45 52 45 20 4e 4f 54 20 28 0a  ack WHERE NOT (.
2520: 20 20 20 20 20 20 20 20 74 72 61 63 6b 61 72 74          trackart
2530: 69 73 74 20 49 53 20 4e 55 4c 4c 20 4f 52 20 0a  ist IS NULL OR .
2540: 20 20 20 20 20 20 20 20 45 58 49 53 54 53 28 53          EXISTS(S
2550: 45 4c 45 43 54 20 31 20 46 52 4f 4d 20 61 72 74  ELECT 1 FROM art
2560: 69 73 74 20 57 48 45 52 45 20 61 72 74 69 73 74  ist WHERE artist
2570: 69 64 3d 74 72 61 63 6b 61 72 74 69 73 74 29 0a  id=trackartist).
2580: 20 20 20 20 20 20 29 0a 20 20 20 20 7d 0a 20 20        ).    }.  
2590: 7d 20 7b 30 7d 0a 7d 0a 0a 23 20 45 78 65 63 75  } {0}.}..# Execu
25a0: 74 65 20 61 20 73 65 72 69 65 73 20 6f 66 20 72  te a series of r
25b0: 61 6e 64 6f 6d 20 49 4e 53 45 52 54 2c 20 55 50  andom INSERT, UP
25c0: 44 41 54 45 20 61 6e 64 20 44 45 4c 45 54 45 20  DATE and DELETE 
25d0: 6f 70 65 72 61 74 69 6f 6e 73 0a 23 20 28 73 6f  operations.# (so
25e0: 6d 65 20 6f 66 20 77 68 69 63 68 20 6d 61 79 20  me of which may 
25f0: 66 61 69 6c 20 64 75 65 20 74 6f 20 46 4b 20 6f  fail due to FK o
2600: 72 20 50 4b 20 63 6f 6e 73 74 72 61 69 6e 74 20  r PK constraint 
2610: 76 69 6f 6c 61 74 69 6f 6e 73 29 20 6f 6e 20 0a  violations) on .
2620: 23 20 74 68 65 20 74 77 6f 20 74 61 62 6c 65 73  # the two tables
2630: 20 69 6e 20 74 68 65 20 65 78 61 6d 70 6c 65 20   in the example 
2640: 73 63 68 65 6d 61 2e 20 54 65 73 74 20 74 68 61  schema. Test tha
2650: 74 20 52 2d 35 32 34 38 36 2d 32 31 33 35 32 0a  t R-52486-21352.
2660: 23 20 69 73 20 74 72 75 65 20 61 66 74 65 72 20  # is true after 
2670: 65 78 65 63 75 74 69 6e 67 20 65 61 63 68 20 6f  executing each o
2680: 70 65 72 61 74 69 6f 6e 2e 0a 23 0a 73 65 74 20  peration..#.set 
2690: 54 65 6d 70 6c 61 74 65 20 7b 0a 20 20 7b 49 4e  Template {.  {IN
26a0: 53 45 52 54 20 49 4e 54 4f 20 74 72 61 63 6b 20  SERT INTO track 
26b0: 56 41 4c 55 45 53 28 24 74 2c 20 27 74 72 61 63  VALUES($t, 'trac
26c0: 6b 20 24 74 27 2c 20 24 61 29 7d 0a 20 20 7b 44  k $t', $a)}.  {D
26d0: 45 4c 45 54 45 20 46 52 4f 4d 20 74 72 61 63 6b  ELETE FROM track
26e0: 20 57 48 45 52 45 20 74 72 61 63 6b 69 64 20 3d   WHERE trackid =
26f0: 20 24 74 7d 0a 20 20 7b 55 50 44 41 54 45 20 74   $t}.  {UPDATE t
2700: 72 61 63 6b 20 53 45 54 20 74 72 61 63 6b 61 72  rack SET trackar
2710: 74 69 73 74 20 3d 20 24 61 20 57 48 45 52 45 20  tist = $a WHERE 
2720: 74 72 61 63 6b 69 64 20 3d 20 24 74 7d 0a 20 20  trackid = $t}.  
2730: 7b 49 4e 53 45 52 54 20 49 4e 54 4f 20 61 72 74  {INSERT INTO art
2740: 69 73 74 20 56 41 4c 55 45 53 28 24 61 2c 20 27  ist VALUES($a, '
2750: 61 72 74 69 73 74 20 24 61 27 29 7d 0a 20 20 7b  artist $a')}.  {
2760: 44 45 4c 45 54 45 20 46 52 4f 4d 20 61 72 74 69  DELETE FROM arti
2770: 73 74 20 57 48 45 52 45 20 61 72 74 69 73 74 69  st WHERE artisti
2780: 64 20 3d 20 24 61 7d 0a 20 20 7b 55 50 44 41 54  d = $a}.  {UPDAT
2790: 45 20 61 72 74 69 73 74 20 53 45 54 20 61 72 74  E artist SET art
27a0: 69 73 74 69 64 20 3d 20 24 61 32 20 57 48 45 52  istid = $a2 WHER
27b0: 45 20 61 72 74 69 73 74 69 64 20 3d 20 24 61 7d  E artistid = $a}
27c0: 0a 7d 0a 66 6f 72 20 7b 73 65 74 20 69 20 30 7d  .}.for {set i 0}
27d0: 20 7b 24 69 20 3c 20 35 30 30 7d 20 7b 69 6e 63   {$i < 500} {inc
27e0: 72 20 69 7d 20 7b 0a 20 20 73 65 74 20 61 20 20  r i} {.  set a  
27f0: 20 5b 65 78 70 72 20 69 6e 74 28 72 61 6e 64 28   [expr int(rand(
2800: 29 2a 31 30 29 5d 0a 20 20 73 65 74 20 61 32 20  )*10)].  set a2 
2810: 20 5b 65 78 70 72 20 69 6e 74 28 72 61 6e 64 28   [expr int(rand(
2820: 29 2a 31 30 29 5d 0a 20 20 73 65 74 20 74 20 20  )*10)].  set t  
2830: 20 5b 65 78 70 72 20 69 6e 74 28 72 61 6e 64 28   [expr int(rand(
2840: 29 2a 35 30 29 5d 0a 20 20 73 65 74 20 73 71 6c  )*50)].  set sql
2850: 20 5b 73 75 62 73 74 20 5b 6c 69 6e 64 65 78 20   [subst [lindex 
2860: 24 54 65 6d 70 6c 61 74 65 20 5b 65 78 70 72 20  $Template [expr 
2870: 69 6e 74 28 72 61 6e 64 28 29 2a 36 29 5d 5d 5d  int(rand()*6)]]]
2880: 0a 0a 20 20 74 65 73 74 5f 72 35 32 34 38 36 5f  ..  test_r52486_
2890: 32 31 33 35 32 20 24 69 20 24 73 71 6c 0a 7d 0a  21352 $i $sql.}.
28a0: 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  .#--------------
28b0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
28c0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
28d0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
28e0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20 2f 2a  -----------.# /*
28f0: 20 45 56 3a 20 52 2d 34 32 34 31 32 2d 35 39 33   EV: R-42412-593
2900: 32 31 20 2a 2f 0a 23 0a 23 20 43 68 65 63 6b 20  21 */.#.# Check 
2910: 74 68 61 74 20 61 20 4e 4f 54 20 4e 55 4c 4c 20  that a NOT NULL 
2920: 63 6f 6e 73 74 72 61 69 6e 74 20 63 61 6e 20 62  constraint can b
2930: 65 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 65  e added to the e
2940: 78 61 6d 70 6c 65 20 73 63 68 65 6d 61 0a 23 20  xample schema.# 
2950: 74 6f 20 70 72 6f 68 69 62 69 74 20 4e 55 4c 4c  to prohibit NULL
2960: 20 63 68 69 6c 64 20 6b 65 79 73 20 66 72 6f 6d   child keys from
2970: 20 62 65 69 6e 67 20 69 6e 73 65 72 74 65 64 2e   being inserted.
2980: 0a 23 0a 64 72 6f 70 5f 61 6c 6c 5f 74 61 62 6c  .#.drop_all_tabl
2990: 65 73 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65  es.do_test e_fke
29a0: 79 2d 34 38 2e 31 20 7b 0a 20 20 65 78 65 63 73  y-48.1 {.  execs
29b0: 71 6c 20 7b 0a 20 20 20 20 43 52 45 41 54 45 20  ql {.    CREATE 
29c0: 54 41 42 4c 45 20 61 72 74 69 73 74 28 0a 20 20  TABLE artist(.  
29d0: 20 20 20 20 61 72 74 69 73 74 69 64 20 20 20 20      artistid    
29e0: 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20  INTEGER PRIMARY 
29f0: 4b 45 59 2c 20 0a 20 20 20 20 20 20 61 72 74 69  KEY, .      arti
2a00: 73 74 6e 61 6d 65 20 20 54 45 58 54 0a 20 20 20  stname  TEXT.   
2a10: 20 29 3b 0a 20 20 20 20 43 52 45 41 54 45 20 54   );.    CREATE T
2a20: 41 42 4c 45 20 74 72 61 63 6b 28 0a 20 20 20 20  ABLE track(.    
2a30: 20 20 74 72 61 63 6b 69 64 20 20 20 20 20 49 4e    trackid     IN
2a40: 54 45 47 45 52 2c 20 0a 20 20 20 20 20 20 74 72  TEGER, .      tr
2a50: 61 63 6b 6e 61 6d 65 20 20 20 54 45 58 54 2c 20  ackname   TEXT, 
2a60: 0a 20 20 20 20 20 20 74 72 61 63 6b 61 72 74 69  .      trackarti
2a70: 73 74 20 49 4e 54 45 47 45 52 20 4e 4f 54 20 4e  st INTEGER NOT N
2a80: 55 4c 4c 2c 0a 20 20 20 20 20 20 46 4f 52 45 49  ULL,.      FOREI
2a90: 47 4e 20 4b 45 59 28 74 72 61 63 6b 61 72 74 69  GN KEY(trackarti
2aa0: 73 74 29 20 52 45 46 45 52 45 4e 43 45 53 20 61  st) REFERENCES a
2ab0: 72 74 69 73 74 28 61 72 74 69 73 74 69 64 29 0a  rtist(artistid).
2ac0: 20 20 20 20 29 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a      );.  }.} {}.
2ad0: 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 34  do_test e_fkey-4
2ae0: 38 2e 32 20 7b 0a 20 20 63 61 74 63 68 73 71 6c  8.2 {.  catchsql
2af0: 20 7b 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74   { INSERT INTO t
2b00: 72 61 63 6b 20 56 41 4c 55 45 53 28 31 34 2c 20  rack VALUES(14, 
2b10: 27 4d 72 2e 20 42 6f 6a 61 6e 67 6c 65 73 27 2c  'Mr. Bojangles',
2b20: 20 4e 55 4c 4c 29 20 7d 0a 7d 20 7b 31 20 7b 74   NULL) }.} {1 {t
2b30: 72 61 63 6b 2e 74 72 61 63 6b 61 72 74 69 73 74  rack.trackartist
2b40: 20 6d 61 79 20 6e 6f 74 20 62 65 20 4e 55 4c 4c   may not be NULL
2b50: 7d 7d 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  }}..#-----------
2b60: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2b70: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2b80: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2b90: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23  --------------.#
2ba0: 20 2f 2a 20 45 56 3a 20 52 2d 31 37 39 30 32 2d   /* EV: R-17902-
2bb0: 35 39 32 35 30 20 2a 2f 0a 23 0a 23 20 54 65 73  59250 */.#.# Tes
2bc0: 74 20 61 6e 20 65 78 61 6d 70 6c 65 20 66 72 6f  t an example fro
2bd0: 6d 20 66 6f 72 65 69 67 6e 6b 65 79 73 2e 68 74  m foreignkeys.ht
2be0: 6d 6c 2e 0a 23 0a 64 72 6f 70 5f 61 6c 6c 5f 74  ml..#.drop_all_t
2bf0: 61 62 6c 65 73 0a 64 6f 5f 74 65 73 74 20 65 5f  ables.do_test e_
2c00: 66 6b 65 79 2d 34 33 2e 31 20 7b 0a 20 20 65 78  fkey-43.1 {.  ex
2c10: 65 63 73 71 6c 20 7b 0a 20 20 20 20 43 52 45 41  ecsql {.    CREA
2c20: 54 45 20 54 41 42 4c 45 20 61 72 74 69 73 74 28  TE TABLE artist(
2c30: 0a 20 20 20 20 20 20 61 72 74 69 73 74 69 64 20  .      artistid 
2c40: 20 20 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41     INTEGER PRIMA
2c50: 52 59 20 4b 45 59 2c 20 0a 20 20 20 20 20 20 61  RY KEY, .      a
2c60: 72 74 69 73 74 6e 61 6d 65 20 20 54 45 58 54 0a  rtistname  TEXT.
2c70: 20 20 20 20 29 3b 0a 20 20 20 20 43 52 45 41 54      );.    CREAT
2c80: 45 20 54 41 42 4c 45 20 74 72 61 63 6b 28 0a 20  E TABLE track(. 
2c90: 20 20 20 20 20 74 72 61 63 6b 69 64 20 20 20 20       trackid    
2ca0: 20 49 4e 54 45 47 45 52 2c 20 0a 20 20 20 20 20   INTEGER, .     
2cb0: 20 74 72 61 63 6b 6e 61 6d 65 20 20 20 54 45 58   trackname   TEX
2cc0: 54 2c 20 0a 20 20 20 20 20 20 74 72 61 63 6b 61  T, .      tracka
2cd0: 72 74 69 73 74 20 49 4e 54 45 47 45 52 2c 0a 20  rtist INTEGER,. 
2ce0: 20 20 20 20 20 46 4f 52 45 49 47 4e 20 4b 45 59       FOREIGN KEY
2cf0: 28 74 72 61 63 6b 61 72 74 69 73 74 29 20 52 45  (trackartist) RE
2d00: 46 45 52 45 4e 43 45 53 20 61 72 74 69 73 74 28  FERENCES artist(
2d10: 61 72 74 69 73 74 69 64 29 0a 20 20 20 20 29 3b  artistid).    );
2d20: 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f  .    INSERT INTO
2d30: 20 61 72 74 69 73 74 20 56 41 4c 55 45 53 28 31   artist VALUES(1
2d40: 2c 20 27 44 65 61 6e 20 4d 61 72 74 69 6e 27 29  , 'Dean Martin')
2d50: 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54  ;.    INSERT INT
2d60: 4f 20 61 72 74 69 73 74 20 56 41 4c 55 45 53 28  O artist VALUES(
2d70: 32 2c 20 27 46 72 61 6e 6b 20 53 69 6e 61 74 72  2, 'Frank Sinatr
2d80: 61 27 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20  a');.    INSERT 
2d90: 49 4e 54 4f 20 74 72 61 63 6b 20 56 41 4c 55 45  INTO track VALUE
2da0: 53 28 31 31 2c 20 27 54 68 61 74 27 27 73 20 41  S(11, 'That''s A
2db0: 6d 6f 72 65 27 2c 20 31 29 3b 0a 20 20 20 20 49  more', 1);.    I
2dc0: 4e 53 45 52 54 20 49 4e 54 4f 20 74 72 61 63 6b  NSERT INTO track
2dd0: 20 56 41 4c 55 45 53 28 31 32 2c 20 27 43 68 72   VALUES(12, 'Chr
2de0: 69 73 74 6d 61 73 20 42 6c 75 65 73 27 2c 20 31  istmas Blues', 1
2df0: 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e  );.    INSERT IN
2e00: 54 4f 20 74 72 61 63 6b 20 56 41 4c 55 45 53 28  TO track VALUES(
2e10: 31 33 2c 20 27 4d 79 20 57 61 79 27 2c 20 32 29  13, 'My Way', 2)
2e20: 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a 64 6f 5f 74 65  ;.  }.} {}.do_te
2e30: 73 74 20 65 5f 66 6b 65 79 2d 34 33 2e 32 20 7b  st e_fkey-43.2 {
2e40: 0a 20 20 63 61 74 63 68 73 71 6c 20 7b 20 49 4e  .  catchsql { IN
2e50: 53 45 52 54 20 49 4e 54 4f 20 74 72 61 63 6b 20  SERT INTO track 
2e60: 56 41 4c 55 45 53 28 31 34 2c 20 27 4d 72 2e 20  VALUES(14, 'Mr. 
2e70: 42 6f 6a 61 6e 67 6c 65 73 27 2c 20 33 29 20 7d  Bojangles', 3) }
2e80: 0a 7d 20 7b 31 20 7b 66 6f 72 65 69 67 6e 20 6b  .} {1 {foreign k
2e90: 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 20 66 61  ey constraint fa
2ea0: 69 6c 65 64 7d 7d 0a 64 6f 5f 74 65 73 74 20 65  iled}}.do_test e
2eb0: 5f 66 6b 65 79 2d 34 33 2e 33 20 7b 0a 20 20 65  _fkey-43.3 {.  e
2ec0: 78 65 63 73 71 6c 20 7b 20 49 4e 53 45 52 54 20  xecsql { INSERT 
2ed0: 49 4e 54 4f 20 74 72 61 63 6b 20 56 41 4c 55 45  INTO track VALUE
2ee0: 53 28 31 34 2c 20 27 4d 72 2e 20 42 6f 6a 61 6e  S(14, 'Mr. Bojan
2ef0: 67 6c 65 73 27 2c 20 4e 55 4c 4c 29 20 7d 0a 7d  gles', NULL) }.}
2f00: 20 7b 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b   {}.do_test e_fk
2f10: 65 79 2d 34 33 2e 34 20 7b 0a 20 20 63 61 74 63  ey-43.4 {.  catc
2f20: 68 73 71 6c 20 7b 20 0a 20 20 20 20 55 50 44 41  hsql { .    UPDA
2f30: 54 45 20 74 72 61 63 6b 20 53 45 54 20 74 72 61  TE track SET tra
2f40: 63 6b 61 72 74 69 73 74 20 3d 20 33 20 57 48 45  ckartist = 3 WHE
2f50: 52 45 20 74 72 61 63 6b 6e 61 6d 65 20 3d 20 27  RE trackname = '
2f60: 4d 72 2e 20 42 6f 6a 61 6e 67 6c 65 73 27 3b 0a  Mr. Bojangles';.
2f70: 20 20 7d 0a 7d 20 7b 31 20 7b 66 6f 72 65 69 67    }.} {1 {foreig
2f80: 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74  n key constraint
2f90: 20 66 61 69 6c 65 64 7d 7d 0a 64 6f 5f 74 65 73   failed}}.do_tes
2fa0: 74 20 65 5f 66 6b 65 79 2d 34 33 2e 35 20 7b 0a  t e_fkey-43.5 {.
2fb0: 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20    execsql {.    
2fc0: 49 4e 53 45 52 54 20 49 4e 54 4f 20 61 72 74 69  INSERT INTO arti
2fd0: 73 74 20 56 41 4c 55 45 53 28 33 2c 20 27 53 61  st VALUES(3, 'Sa
2fe0: 6d 6d 79 20 44 61 76 69 73 20 4a 72 2e 27 29 3b  mmy Davis Jr.');
2ff0: 0a 20 20 20 20 55 50 44 41 54 45 20 74 72 61 63  .    UPDATE trac
3000: 6b 20 53 45 54 20 74 72 61 63 6b 61 72 74 69 73  k SET trackartis
3010: 74 20 3d 20 33 20 57 48 45 52 45 20 74 72 61 63  t = 3 WHERE trac
3020: 6b 6e 61 6d 65 20 3d 20 27 4d 72 2e 20 42 6f 6a  kname = 'Mr. Boj
3030: 61 6e 67 6c 65 73 27 3b 0a 20 20 20 20 49 4e 53  angles';.    INS
3040: 45 52 54 20 49 4e 54 4f 20 74 72 61 63 6b 20 56  ERT INTO track V
3050: 41 4c 55 45 53 28 31 35 2c 20 27 42 6f 6f 67 69  ALUES(15, 'Boogi
3060: 65 20 57 6f 6f 67 69 65 27 2c 20 33 29 3b 0a 20  e Woogie', 3);. 
3070: 20 7d 0a 7d 20 7b 7d 0a 0a 23 2d 2d 2d 2d 2d 2d   }.} {}..#------
3080: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
3090: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
30a0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
30b0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
30c0: 2d 2d 2d 0a 23 20 2f 2a 20 45 56 3a 20 52 2d 31  ---.# /* EV: R-1
30d0: 35 30 33 34 2d 36 34 33 33 31 20 2a 2f 0a 23 0a  5034-64331 */.#.
30e0: 23 20 54 65 73 74 20 74 68 65 20 73 65 63 6f 6e  # Test the secon
30f0: 64 20 65 78 61 6d 70 6c 65 20 66 72 6f 6d 20 74  d example from t
3100: 68 65 20 66 69 72 73 74 20 73 65 63 74 69 6f 6e  he first section
3110: 20 6f 66 20 66 6f 72 65 69 67 6e 6b 65 79 73 2e   of foreignkeys.
3120: 68 74 6d 6c 2e 0a 23 0a 64 6f 5f 74 65 73 74 20  html..#.do_test 
3130: 65 5f 66 6b 65 79 2d 34 34 2e 31 20 7b 0a 20 20  e_fkey-44.1 {.  
3140: 63 61 74 63 68 73 71 6c 20 7b 0a 20 20 20 20 44  catchsql {.    D
3150: 45 4c 45 54 45 20 46 52 4f 4d 20 61 72 74 69 73  ELETE FROM artis
3160: 74 20 57 48 45 52 45 20 61 72 74 69 73 74 6e 61  t WHERE artistna
3170: 6d 65 20 3d 20 27 46 72 61 6e 6b 20 53 69 6e 61  me = 'Frank Sina
3180: 74 72 61 27 3b 0a 20 20 7d 0a 7d 20 7b 31 20 7b  tra';.  }.} {1 {
3190: 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73  foreign key cons
31a0: 74 72 61 69 6e 74 20 66 61 69 6c 65 64 7d 7d 0a  traint failed}}.
31b0: 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 34  do_test e_fkey-4
31c0: 34 2e 32 20 7b 0a 20 20 65 78 65 63 73 71 6c 20  4.2 {.  execsql 
31d0: 7b 0a 20 20 20 20 44 45 4c 45 54 45 20 46 52 4f  {.    DELETE FRO
31e0: 4d 20 74 72 61 63 6b 20 57 48 45 52 45 20 74 72  M track WHERE tr
31f0: 61 63 6b 6e 61 6d 65 20 3d 20 27 4d 79 20 57 61  ackname = 'My Wa
3200: 79 27 3b 0a 20 20 20 20 44 45 4c 45 54 45 20 46  y';.    DELETE F
3210: 52 4f 4d 20 61 72 74 69 73 74 20 57 48 45 52 45  ROM artist WHERE
3220: 20 61 72 74 69 73 74 6e 61 6d 65 20 3d 20 27 46   artistname = 'F
3230: 72 61 6e 6b 20 53 69 6e 61 74 72 61 27 3b 0a 20  rank Sinatra';. 
3240: 20 7d 0a 7d 20 7b 7d 0a 64 6f 5f 74 65 73 74 20   }.} {}.do_test 
3250: 65 5f 66 6b 65 79 2d 34 34 2e 33 20 7b 0a 20 20  e_fkey-44.3 {.  
3260: 63 61 74 63 68 73 71 6c 20 7b 0a 20 20 20 20 55  catchsql {.    U
3270: 50 44 41 54 45 20 61 72 74 69 73 74 20 53 45 54  PDATE artist SET
3280: 20 61 72 74 69 73 74 69 64 3d 34 20 57 48 45 52   artistid=4 WHER
3290: 45 20 61 72 74 69 73 74 6e 61 6d 65 20 3d 20 27  E artistname = '
32a0: 44 65 61 6e 20 4d 61 72 74 69 6e 27 3b 0a 20 20  Dean Martin';.  
32b0: 7d 0a 7d 20 7b 31 20 7b 66 6f 72 65 69 67 6e 20  }.} {1 {foreign 
32c0: 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 20 66  key constraint f
32d0: 61 69 6c 65 64 7d 7d 0a 64 6f 5f 74 65 73 74 20  ailed}}.do_test 
32e0: 65 5f 66 6b 65 79 2d 34 34 2e 34 20 7b 0a 20 20  e_fkey-44.4 {.  
32f0: 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 44 45  execsql {.    DE
3300: 4c 45 54 45 20 46 52 4f 4d 20 74 72 61 63 6b 20  LETE FROM track 
3310: 57 48 45 52 45 20 74 72 61 63 6b 6e 61 6d 65 20  WHERE trackname 
3320: 49 4e 28 27 54 68 61 74 27 27 73 20 41 6d 6f 72  IN('That''s Amor
3330: 65 27 2c 20 27 43 68 72 69 73 74 6d 61 73 20 42  e', 'Christmas B
3340: 6c 75 65 73 27 29 3b 0a 20 20 20 20 55 50 44 41  lues');.    UPDA
3350: 54 45 20 61 72 74 69 73 74 20 53 45 54 20 61 72  TE artist SET ar
3360: 74 69 73 74 69 64 3d 34 20 57 48 45 52 45 20 61  tistid=4 WHERE a
3370: 72 74 69 73 74 6e 61 6d 65 20 3d 20 27 44 65 61  rtistname = 'Dea
3380: 6e 20 4d 61 72 74 69 6e 27 3b 0a 20 20 7d 0a 7d  n Martin';.  }.}
3390: 20 7b 7d 0a 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d   {}...#---------
33a0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
33b0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
33c0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
33d0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
33e0: 0a 23 20 2f 2a 20 45 56 3a 20 52 2d 35 36 30 33  .# /* EV: R-5603
33f0: 32 2d 32 34 39 32 33 20 2a 2f 0a 23 0a 23 20 54  2-24923 */.#.# T
3400: 65 73 74 20 74 68 61 74 20 61 20 66 6f 72 65 69  est that a forei
3410: 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e  gn key constrain
3420: 74 20 69 73 20 73 61 74 69 73 69 66 65 64 20 69  t is satisifed i
3430: 66 20 22 66 6f 72 20 65 61 63 68 20 72 6f 77 20  f "for each row 
3440: 69 6e 20 74 68 65 20 63 68 69 6c 64 0a 23 20 74  in the child.# t
3450: 61 62 6c 65 20 65 69 74 68 65 72 20 6f 6e 65 20  able either one 
3460: 6f 72 20 6d 6f 72 65 20 6f 66 20 74 68 65 20 63  or more of the c
3470: 68 69 6c 64 20 6b 65 79 20 63 6f 6c 75 6d 6e 73  hild key columns
3480: 20 61 72 65 20 4e 55 4c 4c 2c 20 6f 72 20 74 68   are NULL, or th
3490: 65 72 65 20 65 78 69 73 74 73 20 61 0a 23 20 72  ere exists a.# r
34a0: 6f 77 20 69 6e 20 74 68 65 20 70 61 72 65 6e 74  ow in the parent
34b0: 20 74 61 62 6c 65 20 66 6f 72 20 77 68 69 63 68   table for which
34c0: 20 65 61 63 68 20 70 61 72 65 6e 74 20 6b 65 79   each parent key
34d0: 20 63 6f 6c 75 6d 6e 20 63 6f 6e 74 61 69 6e 73   column contains
34e0: 20 61 20 76 61 6c 75 65 0a 23 20 65 71 75 61 6c   a value.# equal
34f0: 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 69 6e   to the value in
3500: 20 69 74 73 20 61 73 73 6f 63 69 61 74 65 64 20   its associated 
3510: 63 68 69 6c 64 20 6b 65 79 20 63 6f 6c 75 6d 6e  child key column
3520: 22 2e 0a 23 0a 23 20 2f 2a 20 45 56 3a 20 52 2d  "..#.# /* EV: R-
3530: 35 37 37 36 35 2d 31 32 33 38 30 20 2a 2f 0a 23  57765-12380 */.#
3540: 0a 23 20 54 65 73 74 20 61 6c 73 6f 20 74 68 61  .# Test also tha
3550: 74 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e  t the comparison
3560: 20 72 75 6c 65 73 20 61 72 65 20 75 73 65 64 20   rules are used 
3570: 77 68 65 6e 20 74 65 73 74 69 6e 67 20 69 66 20  when testing if 
3580: 74 68 65 72 65 20 0a 23 20 69 73 20 61 20 6d 61  there .# is a ma
3590: 74 63 68 69 6e 67 20 72 6f 77 20 69 6e 20 74 68  tching row in th
35a0: 65 20 70 61 72 65 6e 74 20 74 61 62 6c 65 20 6f  e parent table o
35b0: 66 20 61 20 66 6f 72 65 69 67 6e 20 6b 65 79 20  f a foreign key 
35c0: 63 6f 6e 73 74 72 61 69 6e 74 2e 0a 23 0a 64 72  constraint..#.dr
35d0: 6f 70 5f 61 6c 6c 5f 74 61 62 6c 65 73 0a 64 6f  op_all_tables.do
35e0: 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 34 35 2e  _test e_fkey-45.
35f0: 31 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a  1 {.  execsql {.
3600: 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45      CREATE TABLE
3610: 20 70 61 72 28 70 20 50 52 49 4d 41 52 59 20 4b   par(p PRIMARY K
3620: 45 59 29 3b 0a 20 20 20 20 43 52 45 41 54 45 20  EY);.    CREATE 
3630: 54 41 42 4c 45 20 63 68 69 28 63 20 52 45 46 45  TABLE chi(c REFE
3640: 52 45 4e 43 45 53 20 70 61 72 29 3b 0a 0a 20 20  RENCES par);..  
3650: 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 70 61    INSERT INTO pa
3660: 72 20 56 41 4c 55 45 53 28 31 29 3b 0a 20 20 20  r VALUES(1);.   
3670: 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 70 61 72   INSERT INTO par
3680: 20 56 41 4c 55 45 53 28 27 31 27 29 3b 0a 20 20   VALUES('1');.  
3690: 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 70 61    INSERT INTO pa
36a0: 72 20 56 41 4c 55 45 53 28 58 27 33 31 27 29 3b  r VALUES(X'31');
36b0: 0a 20 20 20 20 53 45 4c 45 43 54 20 74 79 70 65  .    SELECT type
36c0: 6f 66 28 70 29 20 46 52 4f 4d 20 70 61 72 3b 0a  of(p) FROM par;.
36d0: 20 20 7d 0a 7d 20 7b 69 6e 74 65 67 65 72 20 74    }.} {integer t
36e0: 65 78 74 20 62 6c 6f 62 7d 0a 0a 70 72 6f 63 20  ext blob}..proc 
36f0: 74 65 73 74 5f 65 66 6b 65 79 5f 34 35 20 7b 74  test_efkey_45 {t
3700: 6e 20 69 73 45 72 72 6f 72 20 73 71 6c 7d 20 7b  n isError sql} {
3710: 0a 20 20 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65  .  do_test e_fke
3720: 79 2d 34 35 2e 24 74 6e 2e 31 20 22 0a 20 20 20  y-45.$tn.1 ".   
3730: 20 63 61 74 63 68 73 71 6c 20 7b 24 73 71 6c 7d   catchsql {$sql}
3740: 0a 20 20 22 20 5b 6c 69 6e 64 65 78 20 7b 7b 30  .  " [lindex {{0
3750: 20 7b 7d 7d 20 7b 31 20 7b 66 6f 72 65 69 67 6e   {}} {1 {foreign
3760: 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 20   key constraint 
3770: 66 61 69 6c 65 64 7d 7d 7d 20 24 69 73 45 72 72  failed}}} $isErr
3780: 6f 72 5d 0a 0a 20 20 64 6f 5f 74 65 73 74 20 65  or]..  do_test e
3790: 5f 66 6b 65 79 2d 34 35 2e 24 74 6e 2e 32 20 7b  _fkey-45.$tn.2 {
37a0: 0a 20 20 20 20 65 78 65 63 73 71 6c 20 7b 0a 20  .    execsql {. 
37b0: 20 20 20 20 20 53 45 4c 45 43 54 20 2a 20 46 52       SELECT * FR
37c0: 4f 4d 20 63 68 69 20 57 48 45 52 45 20 63 20 49  OM chi WHERE c I
37d0: 53 20 4e 4f 54 20 4e 55 4c 4c 20 41 4e 44 20 63  S NOT NULL AND c
37e0: 20 4e 4f 54 20 49 4e 20 28 53 45 4c 45 43 54 20   NOT IN (SELECT 
37f0: 70 20 46 52 4f 4d 20 70 61 72 29 0a 20 20 20 20  p FROM par).    
3800: 7d 0a 20 20 7d 20 7b 7d 0a 7d 0a 0a 74 65 73 74  }.  } {}.}..test
3810: 5f 65 66 6b 65 79 5f 34 35 20 31 20 30 20 22 49  _efkey_45 1 0 "I
3820: 4e 53 45 52 54 20 49 4e 54 4f 20 63 68 69 20 56  NSERT INTO chi V
3830: 41 4c 55 45 53 28 31 29 22 0a 74 65 73 74 5f 65  ALUES(1)".test_e
3840: 66 6b 65 79 5f 34 35 20 32 20 31 20 22 49 4e 53  fkey_45 2 1 "INS
3850: 45 52 54 20 49 4e 54 4f 20 63 68 69 20 56 41 4c  ERT INTO chi VAL
3860: 55 45 53 28 27 31 2e 30 27 29 22 0a 74 65 73 74  UES('1.0')".test
3870: 5f 65 66 6b 65 79 5f 34 35 20 33 20 30 20 22 49  _efkey_45 3 0 "I
3880: 4e 53 45 52 54 20 49 4e 54 4f 20 63 68 69 20 56  NSERT INTO chi V
3890: 41 4c 55 45 53 28 27 31 27 29 22 0a 74 65 73 74  ALUES('1')".test
38a0: 5f 65 66 6b 65 79 5f 34 35 20 34 20 31 20 22 44  _efkey_45 4 1 "D
38b0: 45 4c 45 54 45 20 46 52 4f 4d 20 70 61 72 20 57  ELETE FROM par W
38c0: 48 45 52 45 20 70 20 3d 20 27 31 27 22 0a 74 65  HERE p = '1'".te
38d0: 73 74 5f 65 66 6b 65 79 5f 34 35 20 35 20 30 20  st_efkey_45 5 0 
38e0: 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 63 68 69  "DELETE FROM chi
38f0: 20 57 48 45 52 45 20 63 20 3d 20 27 31 27 22 0a   WHERE c = '1'".
3900: 74 65 73 74 5f 65 66 6b 65 79 5f 34 35 20 36 20  test_efkey_45 6 
3910: 30 20 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 70  0 "DELETE FROM p
3920: 61 72 20 57 48 45 52 45 20 70 20 3d 20 27 31 27  ar WHERE p = '1'
3930: 22 0a 74 65 73 74 5f 65 66 6b 65 79 5f 34 35 20  ".test_efkey_45 
3940: 37 20 31 20 22 49 4e 53 45 52 54 20 49 4e 54 4f  7 1 "INSERT INTO
3950: 20 63 68 69 20 56 41 4c 55 45 53 28 27 31 27 29   chi VALUES('1')
3960: 22 0a 74 65 73 74 5f 65 66 6b 65 79 5f 34 35 20  ".test_efkey_45 
3970: 38 20 30 20 22 49 4e 53 45 52 54 20 49 4e 54 4f  8 0 "INSERT INTO
3980: 20 63 68 69 20 56 41 4c 55 45 53 28 58 27 33 31   chi VALUES(X'31
3990: 27 29 22 0a 74 65 73 74 5f 65 66 6b 65 79 5f 34  ')".test_efkey_4
39a0: 35 20 39 20 31 20 22 49 4e 53 45 52 54 20 49 4e  5 9 1 "INSERT IN
39b0: 54 4f 20 63 68 69 20 56 41 4c 55 45 53 28 58 27  TO chi VALUES(X'
39c0: 33 32 27 29 22 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d  32')"..#--------
39d0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
39e0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
39f0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
3a00: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
3a10: 2d 0a 23 20 2f 2a 20 45 56 3a 20 52 2d 31 35 37  -.# /* EV: R-157
3a20: 39 36 2d 34 37 35 31 33 20 2a 2f 0a 23 0a 23 20  96-47513 */.#.# 
3a30: 53 70 65 63 69 66 69 63 61 6c 6c 79 2c 20 74 65  Specifically, te
3a40: 73 74 20 74 68 61 74 20 77 68 65 6e 20 63 6f 6d  st that when com
3a50: 70 61 72 69 6e 67 20 63 68 69 6c 64 20 61 6e 64  paring child and
3a60: 20 70 61 72 65 6e 74 20 6b 65 79 20 76 61 6c 75   parent key valu
3a70: 65 73 20 74 68 65 0a 23 20 64 65 66 61 75 6c 74  es the.# default
3a80: 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65   collation seque
3a90: 6e 63 65 20 6f 66 20 74 68 65 20 70 61 72 65 6e  nce of the paren
3aa0: 74 20 6b 65 79 20 63 6f 6c 75 6d 6e 20 69 73 20  t key column is 
3ab0: 75 73 65 64 2e 0a 23 0a 64 72 6f 70 5f 61 6c 6c  used..#.drop_all
3ac0: 5f 74 61 62 6c 65 73 0a 64 6f 5f 74 65 73 74 20  _tables.do_test 
3ad0: 65 5f 66 6b 65 79 2d 34 36 2e 31 20 7b 0a 20 20  e_fkey-46.1 {.  
3ae0: 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 43 52  execsql {.    CR
3af0: 45 41 54 45 20 54 41 42 4c 45 20 74 31 28 61 20  EATE TABLE t1(a 
3b00: 43 4f 4c 4c 41 54 45 20 6e 6f 63 61 73 65 20 50  COLLATE nocase P
3b10: 52 49 4d 41 52 59 20 4b 45 59 29 3b 0a 20 20 20  RIMARY KEY);.   
3b20: 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 74 32   CREATE TABLE t2
3b30: 28 62 20 52 45 46 45 52 45 4e 43 45 53 20 74 31  (b REFERENCES t1
3b40: 29 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a 64 6f 5f 74  );.  }.} {}.do_t
3b50: 65 73 74 20 65 5f 66 6b 65 79 2d 34 36 2e 32 20  est e_fkey-46.2 
3b60: 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20  {.  execsql {.  
3b70: 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 31    INSERT INTO t1
3b80: 20 56 41 4c 55 45 53 28 27 6f 4e 65 27 29 3b 0a   VALUES('oNe');.
3b90: 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20      INSERT INTO 
3ba0: 74 32 20 56 41 4c 55 45 53 28 27 6f 6e 65 27 29  t2 VALUES('one')
3bb0: 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54  ;.    INSERT INT
3bc0: 4f 20 74 32 20 56 41 4c 55 45 53 28 27 4f 4e 45  O t2 VALUES('ONE
3bd0: 27 29 3b 0a 20 20 20 20 55 50 44 41 54 45 20 74  ');.    UPDATE t
3be0: 32 20 53 45 54 20 62 20 3d 20 27 4f 6e 45 27 3b  2 SET b = 'OnE';
3bf0: 0a 20 20 20 20 55 50 44 41 54 45 20 74 31 20 53  .    UPDATE t1 S
3c00: 45 54 20 61 20 3d 20 27 4f 4e 45 27 3b 0a 20 20  ET a = 'ONE';.  
3c10: 7d 0a 7d 20 7b 7d 0a 64 6f 5f 74 65 73 74 20 65  }.} {}.do_test e
3c20: 5f 66 6b 65 79 2d 34 36 2e 33 20 7b 0a 20 20 63  _fkey-46.3 {.  c
3c30: 61 74 63 68 73 71 6c 20 7b 20 55 50 44 41 54 45  atchsql { UPDATE
3c40: 20 74 32 20 53 45 54 20 62 20 3d 20 27 74 77 6f   t2 SET b = 'two
3c50: 27 20 57 48 45 52 45 20 72 6f 77 69 64 20 3d 20  ' WHERE rowid = 
3c60: 31 20 7d 0a 7d 20 7b 31 20 7b 66 6f 72 65 69 67  1 }.} {1 {foreig
3c70: 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74  n key constraint
3c80: 20 66 61 69 6c 65 64 7d 7d 0a 64 6f 5f 74 65 73   failed}}.do_tes
3c90: 74 20 65 5f 66 6b 65 79 2d 34 36 2e 34 20 7b 0a  t e_fkey-46.4 {.
3ca0: 20 20 63 61 74 63 68 73 71 6c 20 7b 20 44 45 4c    catchsql { DEL
3cb0: 45 54 45 20 46 52 4f 4d 20 74 31 20 57 48 45 52  ETE FROM t1 WHER
3cc0: 45 20 72 6f 77 69 64 20 3d 20 31 20 7d 0a 7d 20  E rowid = 1 }.} 
3cd0: 7b 31 20 7b 66 6f 72 65 69 67 6e 20 6b 65 79 20  {1 {foreign key 
3ce0: 63 6f 6e 73 74 72 61 69 6e 74 20 66 61 69 6c 65  constraint faile
3cf0: 64 7d 7d 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  d}}..#----------
3d00: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
3d10: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
3d20: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
3d30: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a  ---------------.
3d40: 23 20 2f 2a 20 45 56 3a 20 52 2d 30 34 32 34 30  # /* EV: R-04240
3d50: 2d 31 33 38 36 30 20 2a 2f 0a 23 0a 23 20 53 70  -13860 */.#.# Sp
3d60: 65 63 69 66 69 63 61 6c 6c 79 2c 20 74 65 73 74  ecifically, test
3d70: 20 74 68 61 74 20 77 68 65 6e 20 63 6f 6d 70 61   that when compa
3d80: 72 69 6e 67 20 63 68 69 6c 64 20 61 6e 64 20 70  ring child and p
3d90: 61 72 65 6e 74 20 6b 65 79 20 76 61 6c 75 65 73  arent key values
3da0: 20 74 68 65 0a 23 20 61 66 66 69 6e 69 74 79 20   the.# affinity 
3db0: 6f 66 20 74 68 65 20 70 61 72 65 6e 74 20 6b 65  of the parent ke
3dc0: 79 20 63 6f 6c 75 6d 6e 20 69 73 20 61 70 70 6c  y column is appl
3dd0: 69 65 64 20 74 6f 20 74 68 65 20 63 68 69 6c 64  ied to the child
3de0: 20 6b 65 79 20 76 61 6c 75 65 0a 23 20 62 65 66   key value.# bef
3df0: 6f 72 65 20 74 68 65 20 63 6f 6d 70 61 72 69 73  ore the comparis
3e00: 6f 6e 20 74 61 6b 65 73 20 70 6c 61 63 65 2e 0a  on takes place..
3e10: 23 0a 64 72 6f 70 5f 61 6c 6c 5f 74 61 62 6c 65  #.drop_all_table
3e20: 73 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79  s.do_test e_fkey
3e30: 2d 34 37 2e 31 20 7b 0a 20 20 65 78 65 63 73 71  -47.1 {.  execsq
3e40: 6c 20 7b 0a 20 20 20 20 43 52 45 41 54 45 20 54  l {.    CREATE T
3e50: 41 42 4c 45 20 74 31 28 61 20 4e 55 4d 45 52 49  ABLE t1(a NUMERI
3e60: 43 20 50 52 49 4d 41 52 59 20 4b 45 59 29 3b 0a  C PRIMARY KEY);.
3e70: 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45      CREATE TABLE
3e80: 20 74 32 28 62 20 54 45 58 54 20 52 45 46 45 52   t2(b TEXT REFER
3e90: 45 4e 43 45 53 20 74 31 29 3b 0a 20 20 7d 0a 7d  ENCES t1);.  }.}
3ea0: 20 7b 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b   {}.do_test e_fk
3eb0: 65 79 2d 34 37 2e 32 20 7b 0a 20 20 65 78 65 63  ey-47.2 {.  exec
3ec0: 73 71 6c 20 7b 0a 20 20 20 20 49 4e 53 45 52 54  sql {.    INSERT
3ed0: 20 49 4e 54 4f 20 74 31 20 56 41 4c 55 45 53 28   INTO t1 VALUES(
3ee0: 31 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49  1);.    INSERT I
3ef0: 4e 54 4f 20 74 31 20 56 41 4c 55 45 53 28 32 29  NTO t1 VALUES(2)
3f00: 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54  ;.    INSERT INT
3f10: 4f 20 74 31 20 56 41 4c 55 45 53 28 27 74 68 72  O t1 VALUES('thr
3f20: 65 65 27 29 3b 0a 20 20 20 20 49 4e 53 45 52 54  ee');.    INSERT
3f30: 20 49 4e 54 4f 20 74 32 20 56 41 4c 55 45 53 28   INTO t2 VALUES(
3f40: 27 32 2e 30 27 29 3b 0a 20 20 20 20 53 45 4c 45  '2.0');.    SELE
3f50: 43 54 20 62 2c 20 74 79 70 65 6f 66 28 62 29 20  CT b, typeof(b) 
3f60: 46 52 4f 4d 20 74 32 3b 0a 20 20 7d 0a 7d 20 7b  FROM t2;.  }.} {
3f70: 32 2e 30 20 74 65 78 74 7d 0a 64 6f 5f 74 65 73  2.0 text}.do_tes
3f80: 74 20 65 5f 66 6b 65 79 2d 34 37 2e 33 20 7b 0a  t e_fkey-47.3 {.
3f90: 20 20 65 78 65 63 73 71 6c 20 7b 20 53 45 4c 45    execsql { SELE
3fa0: 43 54 20 74 79 70 65 6f 66 28 61 29 20 46 52 4f  CT typeof(a) FRO
3fb0: 4d 20 74 31 20 7d 0a 7d 20 7b 69 6e 74 65 67 65  M t1 }.} {intege
3fc0: 72 20 69 6e 74 65 67 65 72 20 74 65 78 74 7d 0a  r integer text}.
3fd0: 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 34  do_test e_fkey-4
3fe0: 37 2e 34 20 7b 0a 20 20 63 61 74 63 68 73 71 6c  7.4 {.  catchsql
3ff0: 20 7b 20 44 45 4c 45 54 45 20 46 52 4f 4d 20 74   { DELETE FROM t
4000: 31 20 57 48 45 52 45 20 72 6f 77 69 64 20 3d 20  1 WHERE rowid = 
4010: 32 20 7d 0a 7d 20 7b 31 20 7b 66 6f 72 65 69 67  2 }.} {1 {foreig
4020: 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74  n key constraint
4030: 20 66 61 69 6c 65 64 7d 7d 0a 0a 23 23 23 23 23   failed}}..#####
4040: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
4050: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
4060: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
4070: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
4080: 23 23 23 23 23 23 0a 23 23 23 20 53 45 43 54 49  ######.### SECTI
4090: 4f 4e 20 33 3a 20 52 65 71 75 69 72 65 64 20 61  ON 3: Required a
40a0: 6e 64 20 53 75 67 67 65 73 74 65 64 20 44 61 74  nd Suggested Dat
40b0: 61 62 61 73 65 20 49 6e 64 65 78 65 73 0a 23 23  abase Indexes.##
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 23 23 23 23 0a 0a 23 2d 2d 2d 2d  #########..#----
4110: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
4120: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
4130: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
4140: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
4150: 2d 2d 2d 2d 2d 0a 23 20 2f 2a 20 45 56 3a 20 52  -----.# /* EV: R
4160: 2d 31 33 34 33 35 2d 32 36 33 31 31 20 2a 2f 0a  -13435-26311 */.
4170: 23 0a 23 20 41 20 70 61 72 65 6e 74 20 6b 65 79  #.# A parent key
4180: 20 6d 75 73 74 20 62 65 20 65 69 74 68 65 72 20   must be either 
4190: 61 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 73  a PRIMARY KEY, s
41a0: 75 62 6a 65 63 74 20 74 6f 20 61 20 55 4e 49 51  ubject to a UNIQ
41b0: 55 45 20 0a 23 20 63 6f 6e 73 74 72 61 69 6e 74  UE .# constraint
41c0: 2c 20 6f 72 20 68 61 76 65 20 61 20 55 4e 49 51  , or have a UNIQ
41d0: 55 45 20 69 6e 64 65 78 20 63 72 65 61 74 65 64  UE index created
41e0: 20 6f 6e 20 69 74 2e 0a 23 20 0a 23 20 2f 2a 20   on it..# .# /* 
41f0: 45 56 3a 20 52 2d 30 30 33 37 36 2d 33 39 32 31  EV: R-00376-3921
4200: 32 20 2a 2f 0a 23 0a 23 20 41 6c 73 6f 20 74 65  2 */.#.# Also te
4210: 73 74 20 74 68 61 74 20 69 66 20 61 20 70 61 72  st that if a par
4220: 65 6e 74 20 6b 65 79 20 69 73 20 6e 6f 74 20 73  ent key is not s
4230: 75 62 6a 65 63 74 20 74 6f 20 61 20 50 52 49 4d  ubject to a PRIM
4240: 41 52 59 20 4b 45 59 20 6f 72 20 55 4e 49 51 55  ARY KEY or UNIQU
4250: 45 0a 23 20 63 6f 6e 73 74 72 61 69 6e 74 2c 20  E.# constraint, 
4260: 62 75 74 20 64 6f 65 73 20 68 61 76 65 20 61 20  but does have a 
4270: 55 4e 49 51 55 45 20 69 6e 64 65 78 20 63 72 65  UNIQUE index cre
4280: 61 74 65 64 20 6f 6e 20 69 74 2c 20 74 68 65 6e  ated on it, then
4290: 20 74 68 65 20 55 4e 49 51 55 45 20 69 6e 64 65   the UNIQUE inde
42a0: 78 0a 23 20 6d 75 73 74 20 75 73 65 20 74 68 65  x.# must use the
42b0: 20 64 65 66 61 75 6c 74 20 63 6f 6c 6c 61 74 69   default collati
42c0: 6f 6e 20 73 65 71 75 65 6e 63 65 73 20 61 73 73  on sequences ass
42d0: 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65  ociated with the
42e0: 20 70 61 72 65 6e 74 20 6b 65 79 0a 23 20 63 6f   parent key.# co
42f0: 6c 75 6d 6e 73 2e 0a 23 0a 64 72 6f 70 5f 61 6c  lumns..#.drop_al
4300: 6c 5f 74 61 62 6c 65 73 0a 64 6f 5f 74 65 73 74  l_tables.do_test
4310: 20 65 5f 66 6b 65 79 2d 35 37 2e 31 20 7b 0a 20   e_fkey-57.1 {. 
4320: 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 43   execsql {.    C
4330: 52 45 41 54 45 20 54 41 42 4c 45 20 74 32 28 61  REATE TABLE t2(a
4340: 20 52 45 46 45 52 45 4e 43 45 53 20 74 31 28 78   REFERENCES t1(x
4350: 29 29 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a 70 72 6f  ));.  }.} {}.pro
4360: 63 20 74 65 73 74 5f 65 66 6b 65 79 5f 35 37 20  c test_efkey_57 
4370: 7b 74 6e 20 69 73 45 72 72 6f 72 20 73 71 6c 7d  {tn isError sql}
4380: 20 7b 0a 20 20 63 61 74 63 68 73 71 6c 20 7b 20   {.  catchsql { 
4390: 44 52 4f 50 20 54 41 42 4c 45 20 74 31 20 7d 0a  DROP TABLE t1 }.
43a0: 20 20 65 78 65 63 73 71 6c 20 24 73 71 6c 0a 20    execsql $sql. 
43b0: 20 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d   do_test e_fkey-
43c0: 35 37 2e 24 74 6e 20 7b 0a 20 20 20 20 63 61 74  57.$tn {.    cat
43d0: 63 68 73 71 6c 20 7b 20 49 4e 53 45 52 54 20 49  chsql { INSERT I
43e0: 4e 54 4f 20 74 32 20 56 41 4c 55 45 53 28 4e 55  NTO t2 VALUES(NU
43f0: 4c 4c 29 20 7d 0a 20 20 7d 20 5b 6c 69 6e 64 65  LL) }.  } [linde
4400: 78 20 7b 7b 30 20 7b 7d 7d 20 7b 31 20 7b 66 6f  x {{0 {}} {1 {fo
4410: 72 65 69 67 6e 20 6b 65 79 20 6d 69 73 6d 61 74  reign key mismat
4420: 63 68 7d 7d 7d 20 24 69 73 45 72 72 6f 72 5d 0a  ch}}} $isError].
4430: 7d 0a 74 65 73 74 5f 65 66 6b 65 79 5f 35 37 20  }.test_efkey_57 
4440: 32 20 30 20 7b 20 43 52 45 41 54 45 20 54 41 42  2 0 { CREATE TAB
4450: 4c 45 20 74 31 28 78 20 50 52 49 4d 41 52 59 20  LE t1(x PRIMARY 
4460: 4b 45 59 29 20 7d 0a 74 65 73 74 5f 65 66 6b 65  KEY) }.test_efke
4470: 79 5f 35 37 20 33 20 30 20 7b 20 43 52 45 41 54  y_57 3 0 { CREAT
4480: 45 20 54 41 42 4c 45 20 74 31 28 78 20 55 4e 49  E TABLE t1(x UNI
4490: 51 55 45 29 20 7d 0a 74 65 73 74 5f 65 66 6b 65  QUE) }.test_efke
44a0: 79 5f 35 37 20 34 20 30 20 7b 20 43 52 45 41 54  y_57 4 0 { CREAT
44b0: 45 20 54 41 42 4c 45 20 74 31 28 78 29 3b 20 43  E TABLE t1(x); C
44c0: 52 45 41 54 45 20 55 4e 49 51 55 45 20 49 4e 44  REATE UNIQUE IND
44d0: 45 58 20 74 31 69 20 4f 4e 20 74 31 28 78 29 20  EX t1i ON t1(x) 
44e0: 7d 0a 74 65 73 74 5f 65 66 6b 65 79 5f 35 37 20  }.test_efkey_57 
44f0: 35 20 31 20 7b 20 0a 20 20 43 52 45 41 54 45 20  5 1 { .  CREATE 
4500: 54 41 42 4c 45 20 74 31 28 78 29 3b 20 0a 20 20  TABLE t1(x); .  
4510: 43 52 45 41 54 45 20 55 4e 49 51 55 45 20 49 4e  CREATE UNIQUE IN
4520: 44 45 58 20 74 31 69 20 4f 4e 20 74 31 28 78 20  DEX t1i ON t1(x 
4530: 43 4f 4c 4c 41 54 45 20 6e 6f 63 61 73 65 29 3b  COLLATE nocase);
4540: 0a 7d 0a 74 65 73 74 5f 65 66 6b 65 79 5f 35 37  .}.test_efkey_57
4550: 20 36 20 31 20 7b 20 43 52 45 41 54 45 20 54 41   6 1 { CREATE TA
4560: 42 4c 45 20 74 31 28 78 29 20 7d 0a 74 65 73 74  BLE t1(x) }.test
4570: 5f 65 66 6b 65 79 5f 35 37 20 37 20 31 20 7b 20  _efkey_57 7 1 { 
4580: 43 52 45 41 54 45 20 54 41 42 4c 45 20 74 31 28  CREATE TABLE t1(
4590: 78 2c 20 79 2c 20 50 52 49 4d 41 52 59 20 4b 45  x, y, PRIMARY KE
45a0: 59 28 78 2c 20 79 29 29 20 7d 0a 74 65 73 74 5f  Y(x, y)) }.test_
45b0: 65 66 6b 65 79 5f 35 37 20 38 20 31 20 7b 20 43  efkey_57 8 1 { C
45c0: 52 45 41 54 45 20 54 41 42 4c 45 20 74 31 28 78  REATE TABLE t1(x
45d0: 2c 20 79 2c 20 55 4e 49 51 55 45 28 78 2c 20 79  , y, UNIQUE(x, y
45e0: 29 29 20 7d 0a 74 65 73 74 5f 65 66 6b 65 79 5f  )) }.test_efkey_
45f0: 35 37 20 39 20 31 20 7b 20 0a 20 20 43 52 45 41  57 9 1 { .  CREA
4600: 54 45 20 54 41 42 4c 45 20 74 31 28 78 2c 20 79  TE TABLE t1(x, y
4610: 29 3b 20 0a 20 20 43 52 45 41 54 45 20 55 4e 49  ); .  CREATE UNI
4620: 51 55 45 20 49 4e 44 45 58 20 74 31 69 20 4f 4e  QUE INDEX t1i ON
4630: 20 74 31 28 78 2c 20 79 29 3b 0a 7d 0a 0a 0a 23   t1(x, y);.}...#
4640: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
4650: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
4660: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
4670: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
4680: 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20 54 68 69 73  ---------.# This
4690: 20 62 6c 6f 63 6b 20 74 65 73 74 73 20 61 6e 20   block tests an 
46a0: 65 78 61 6d 70 6c 65 20 69 6e 20 66 6f 72 65 69  example in forei
46b0: 67 6e 6b 65 79 73 2e 68 74 6d 6c 2e 20 53 65 76  gnkeys.html. Sev
46c0: 65 72 61 6c 20 74 65 73 74 61 62 6c 65 0a 23 20  eral testable.# 
46d0: 73 74 61 74 65 6d 65 6e 74 73 20 72 65 66 65 72  statements refer
46e0: 20 74 6f 20 74 68 69 73 20 65 78 61 6d 70 6c 65   to this example
46f0: 2c 20 61 73 20 66 6f 6c 6c 6f 77 73 0a 23 0a 23  , as follows.#.#
4700: 20 2f 2a 20 45 56 3a 20 52 2d 32 37 34 38 34 2d   /* EV: R-27484-
4710: 30 31 34 36 37 20 2a 2f 0a 23 0a 23 20 46 4b 20  01467 */.#.# FK 
4720: 43 6f 6e 73 74 72 61 69 6e 74 73 20 6f 6e 20 63  Constraints on c
4730: 68 69 6c 64 31 2c 20 63 68 69 6c 64 32 20 61 6e  hild1, child2 an
4740: 64 20 63 68 69 6c 64 33 20 61 72 65 20 4f 6b 2e  d child3 are Ok.
4750: 0a 23 0a 23 20 2f 2a 20 45 56 3a 20 52 2d 35 31  .#.# /* EV: R-51
4760: 30 33 39 2d 34 34 38 34 30 20 2a 2f 0a 23 0a 23  039-44840 */.#.#
4770: 20 50 72 6f 62 6c 65 6d 20 77 69 74 68 20 46 4b   Problem with FK
4780: 20 6f 6e 20 63 68 69 6c 64 34 2e 0a 23 0a 23 20   on child4..#.# 
4790: 2f 2a 20 45 56 3a 20 52 2d 30 31 30 36 30 2d 34  /* EV: R-01060-4
47a0: 38 37 38 38 20 2a 2f 0a 23 0a 23 20 50 72 6f 62  8788 */.#.# Prob
47b0: 6c 65 6d 20 77 69 74 68 20 46 4b 20 6f 6e 20 63  lem with FK on c
47c0: 68 69 6c 64 35 2e 0a 23 0a 23 20 2f 2a 20 45 56  hild5..#.# /* EV
47d0: 3a 20 52 2d 36 33 30 38 38 2d 33 37 34 36 39 20  : R-63088-37469 
47e0: 2a 2f 0a 23 0a 23 20 50 72 6f 62 6c 65 6d 20 77  */.#.# Problem w
47f0: 69 74 68 20 46 4b 20 6f 6e 20 63 68 69 6c 64 36  ith FK on child6
4800: 20 61 6e 64 20 63 68 69 6c 64 37 2e 0a 23 0a 64   and child7..#.d
4810: 72 6f 70 5f 61 6c 6c 5f 74 61 62 6c 65 73 0a 64  rop_all_tables.d
4820: 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 35 36  o_test e_fkey-56
4830: 2e 31 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b  .1 {.  execsql {
4840: 0a 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c  .    CREATE TABL
4850: 45 20 70 61 72 65 6e 74 28 61 20 50 52 49 4d 41  E parent(a PRIMA
4860: 52 59 20 4b 45 59 2c 20 62 20 55 4e 49 51 55 45  RY KEY, b UNIQUE
4870: 2c 20 63 2c 20 64 2c 20 65 2c 20 66 29 3b 0a 20  , c, d, e, f);. 
4880: 20 20 20 43 52 45 41 54 45 20 55 4e 49 51 55 45     CREATE UNIQUE
4890: 20 49 4e 44 45 58 20 69 31 20 4f 4e 20 70 61 72   INDEX i1 ON par
48a0: 65 6e 74 28 63 2c 20 64 29 3b 0a 20 20 20 20 43  ent(c, d);.    C
48b0: 52 45 41 54 45 20 49 4e 44 45 58 20 69 32 20 4f  REATE INDEX i2 O
48c0: 4e 20 70 61 72 65 6e 74 28 65 29 3b 0a 20 20 20  N parent(e);.   
48d0: 20 43 52 45 41 54 45 20 55 4e 49 51 55 45 20 49   CREATE UNIQUE I
48e0: 4e 44 45 58 20 69 33 20 4f 4e 20 70 61 72 65 6e  NDEX i3 ON paren
48f0: 74 28 66 20 43 4f 4c 4c 41 54 45 20 6e 6f 63 61  t(f COLLATE noca
4900: 73 65 29 3b 0a 0a 20 20 20 20 43 52 45 41 54 45  se);..    CREATE
4910: 20 54 41 42 4c 45 20 63 68 69 6c 64 31 28 66 2c   TABLE child1(f,
4920: 20 67 20 52 45 46 45 52 45 4e 43 45 53 20 70 61   g REFERENCES pa
4930: 72 65 6e 74 28 61 29 29 3b 20 20 20 20 20 20 20  rent(a));       
4940: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4950: 2d 2d 20 4f 6b 0a 20 20 20 20 43 52 45 41 54 45  -- Ok.    CREATE
4960: 20 54 41 42 4c 45 20 63 68 69 6c 64 32 28 68 2c   TABLE child2(h,
4970: 20 69 20 52 45 46 45 52 45 4e 43 45 53 20 70 61   i REFERENCES pa
4980: 72 65 6e 74 28 62 29 29 3b 20 20 20 20 20 20 20  rent(b));       
4990: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
49a0: 2d 2d 20 4f 6b 0a 20 20 20 20 43 52 45 41 54 45  -- Ok.    CREATE
49b0: 20 54 41 42 4c 45 20 63 68 69 6c 64 33 28 6a 2c   TABLE child3(j,
49c0: 20 6b 2c 20 46 4f 52 45 49 47 4e 20 4b 45 59 28   k, FOREIGN KEY(
49d0: 6a 2c 20 6b 29 20 52 45 46 45 52 45 4e 43 45 53  j, k) REFERENCES
49e0: 20 70 61 72 65 6e 74 28 63 2c 20 64 29 29 3b 20   parent(c, d)); 
49f0: 2d 2d 20 4f 6b 0a 20 20 20 20 43 52 45 41 54 45  -- Ok.    CREATE
4a00: 20 54 41 42 4c 45 20 63 68 69 6c 64 34 28 6c 2c   TABLE child4(l,
4a10: 20 6d 20 52 45 46 45 52 45 4e 43 45 53 20 70 61   m REFERENCES pa
4a20: 72 65 6e 74 28 65 29 29 3b 20 20 20 20 20 20 20  rent(e));       
4a30: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4a40: 2d 2d 20 45 72 72 0a 20 20 20 20 43 52 45 41 54  -- Err.    CREAT
4a50: 45 20 54 41 42 4c 45 20 63 68 69 6c 64 35 28 6e  E TABLE child5(n
4a60: 2c 20 6f 20 52 45 46 45 52 45 4e 43 45 53 20 70  , o REFERENCES p
4a70: 61 72 65 6e 74 28 66 29 29 3b 20 20 20 20 20 20  arent(f));      
4a80: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4a90: 20 2d 2d 20 45 72 72 0a 20 20 20 20 43 52 45 41   -- Err.    CREA
4aa0: 54 45 20 54 41 42 4c 45 20 63 68 69 6c 64 36 28  TE TABLE child6(
4ab0: 70 2c 20 71 2c 20 46 4f 52 45 49 47 4e 20 4b 45  p, q, FOREIGN KE
4ac0: 59 28 70 2c 71 29 20 52 45 46 45 52 45 4e 43 45  Y(p,q) REFERENCE
4ad0: 53 20 70 61 72 65 6e 74 28 62 2c 20 63 29 29 3b  S parent(b, c));
4ae0: 20 20 2d 2d 20 45 72 72 0a 20 20 20 20 43 52 45    -- Err.    CRE
4af0: 41 54 45 20 54 41 42 4c 45 20 63 68 69 6c 64 37  ATE TABLE child7
4b00: 28 72 20 52 45 46 45 52 45 4e 43 45 53 20 70 61  (r REFERENCES pa
4b10: 72 65 6e 74 28 63 29 29 3b 20 20 20 20 20 20 20  rent(c));       
4b20: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4b30: 20 20 20 2d 2d 20 45 72 72 0a 20 20 7d 0a 7d 20     -- Err.  }.} 
4b40: 7b 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65  {}.do_test e_fke
4b50: 79 2d 35 36 2e 32 20 7b 0a 20 20 65 78 65 63 73  y-56.2 {.  execs
4b60: 71 6c 20 7b 0a 20 20 20 20 49 4e 53 45 52 54 20  ql {.    INSERT 
4b70: 49 4e 54 4f 20 70 61 72 65 6e 74 20 56 41 4c 55  INTO parent VALU
4b80: 45 53 28 31 2c 20 32 2c 20 33 2c 20 34 2c 20 35  ES(1, 2, 3, 4, 5
4b90: 2c 20 36 29 3b 0a 20 20 20 20 49 4e 53 45 52 54  , 6);.    INSERT
4ba0: 20 49 4e 54 4f 20 63 68 69 6c 64 31 20 56 41 4c   INTO child1 VAL
4bb0: 55 45 53 28 27 78 78 78 27 2c 20 31 29 3b 0a 20  UES('xxx', 1);. 
4bc0: 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 63     INSERT INTO c
4bd0: 68 69 6c 64 32 20 56 41 4c 55 45 53 28 27 78 78  hild2 VALUES('xx
4be0: 78 27 2c 20 32 29 3b 0a 20 20 20 20 49 4e 53 45  x', 2);.    INSE
4bf0: 52 54 20 49 4e 54 4f 20 63 68 69 6c 64 33 20 56  RT INTO child3 V
4c00: 41 4c 55 45 53 28 33 2c 20 34 29 3b 0a 20 20 7d  ALUES(3, 4);.  }
4c10: 0a 7d 20 7b 7d 0a 64 6f 5f 74 65 73 74 20 65 5f  .} {}.do_test e_
4c20: 66 6b 65 79 2d 35 36 2e 32 20 7b 0a 20 20 63 61  fkey-56.2 {.  ca
4c30: 74 63 68 73 71 6c 20 7b 20 49 4e 53 45 52 54 20  tchsql { INSERT 
4c40: 49 4e 54 4f 20 63 68 69 6c 64 34 20 56 41 4c 55  INTO child4 VALU
4c50: 45 53 28 27 78 78 78 27 2c 20 35 29 20 7d 0a 7d  ES('xxx', 5) }.}
4c60: 20 7b 31 20 7b 66 6f 72 65 69 67 6e 20 6b 65 79   {1 {foreign key
4c70: 20 6d 69 73 6d 61 74 63 68 7d 7d 0a 64 6f 5f 74   mismatch}}.do_t
4c80: 65 73 74 20 65 5f 66 6b 65 79 2d 35 36 2e 33 20  est e_fkey-56.3 
4c90: 7b 0a 20 20 63 61 74 63 68 73 71 6c 20 7b 20 49  {.  catchsql { I
4ca0: 4e 53 45 52 54 20 49 4e 54 4f 20 63 68 69 6c 64  NSERT INTO child
4cb0: 35 20 56 41 4c 55 45 53 28 27 78 78 78 27 2c 20  5 VALUES('xxx', 
4cc0: 36 29 20 7d 0a 7d 20 7b 31 20 7b 66 6f 72 65 69  6) }.} {1 {forei
4cd0: 67 6e 20 6b 65 79 20 6d 69 73 6d 61 74 63 68 7d  gn key mismatch}
4ce0: 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79  }.do_test e_fkey
4cf0: 2d 35 36 2e 34 20 7b 0a 20 20 63 61 74 63 68 73  -56.4 {.  catchs
4d00: 71 6c 20 7b 20 49 4e 53 45 52 54 20 49 4e 54 4f  ql { INSERT INTO
4d10: 20 63 68 69 6c 64 36 20 56 41 4c 55 45 53 28 32   child6 VALUES(2
4d20: 2c 20 33 29 20 7d 0a 7d 20 7b 31 20 7b 66 6f 72  , 3) }.} {1 {for
4d30: 65 69 67 6e 20 6b 65 79 20 6d 69 73 6d 61 74 63  eign key mismatc
4d40: 68 7d 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b  h}}.do_test e_fk
4d50: 65 79 2d 35 36 2e 35 20 7b 0a 20 20 63 61 74 63  ey-56.5 {.  catc
4d60: 68 73 71 6c 20 7b 20 49 4e 53 45 52 54 20 49 4e  hsql { INSERT IN
4d70: 54 4f 20 63 68 69 6c 64 37 20 56 41 4c 55 45 53  TO child7 VALUES
4d80: 28 33 29 20 7d 0a 7d 20 7b 31 20 7b 66 6f 72 65  (3) }.} {1 {fore
4d90: 69 67 6e 20 6b 65 79 20 6d 69 73 6d 61 74 63 68  ign key mismatch
4da0: 7d 7d 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  }}..#-----------
4db0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
4dc0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
4dd0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
4de0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23  --------------.#
4df0: 20 2f 2a 20 45 56 3a 20 52 2d 34 35 34 38 38 2d   /* EV: R-45488-
4e00: 30 38 35 30 34 20 2a 2f 0a 23 20 2f 2a 20 45 56  08504 */.# /* EV
4e10: 3a 20 52 2d 34 38 33 39 31 2d 33 38 34 37 32 20  : R-48391-38472 
4e20: 2a 2f 0a 23 20 2f 2a 20 45 56 3a 20 52 2d 30 33  */.# /* EV: R-03
4e30: 31 30 38 2d 36 33 36 35 39 20 2a 2f 0a 23 20 2f  108-63659 */.# /
4e40: 2a 20 45 56 3a 20 52 2d 36 30 37 38 31 2d 32 36  * EV: R-60781-26
4e50: 35 37 36 20 2a 2f 0a 23 0a 23 20 54 65 73 74 20  576 */.#.# Test 
4e60: 65 72 72 6f 72 73 20 69 6e 20 74 68 65 20 64 61  errors in the da
4e70: 74 61 62 61 73 65 20 73 63 68 65 6d 61 20 74 68  tabase schema th
4e80: 61 74 20 61 72 65 20 64 65 74 65 63 74 65 64 20  at are detected 
4e90: 77 68 69 6c 65 20 70 72 65 70 61 72 69 6e 67 0a  while preparing.
4ea0: 23 20 44 4d 4c 20 73 74 61 74 65 6d 65 6e 74 73  # DML statements
4eb0: 2e 20 54 68 65 20 65 72 72 6f 72 20 74 65 78 74  . The error text
4ec0: 20 66 6f 72 20 74 68 65 73 65 20 6d 65 73 73 61   for these messa
4ed0: 67 65 73 20 61 6c 77 61 79 73 20 6d 61 74 63 68  ges always match
4ee0: 65 73 20 0a 23 20 65 69 74 68 65 72 20 22 66 6f  es .# either "fo
4ef0: 72 65 69 67 6e 20 6b 65 79 20 6d 69 73 6d 61 74  reign key mismat
4f00: 63 68 22 20 6f 72 20 22 6e 6f 20 73 75 63 68 20  ch" or "no such 
4f10: 74 61 62 6c 65 2a 22 20 28 75 73 69 6e 67 20 5b  table*" (using [
4f20: 73 74 72 69 6e 67 20 6d 61 74 63 68 5d 29 2e 0a  string match])..
4f30: 23 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79  #.do_test e_fkey
4f40: 2d 36 36 2e 31 20 7b 0a 20 20 65 78 65 63 73 71  -66.1 {.  execsq
4f50: 6c 20 7b 0a 20 20 20 20 43 52 45 41 54 45 20 54  l {.    CREATE T
4f60: 41 42 4c 45 20 63 31 28 63 20 52 45 46 45 52 45  ABLE c1(c REFERE
4f70: 4e 43 45 53 20 6e 6f 73 75 63 68 74 61 62 6c 65  NCES nosuchtable
4f80: 2c 20 64 29 3b 0a 0a 20 20 20 20 43 52 45 41 54  , d);..    CREAT
4f90: 45 20 54 41 42 4c 45 20 70 32 28 61 2c 20 62 2c  E TABLE p2(a, b,
4fa0: 20 55 4e 49 51 55 45 28 61 2c 20 62 29 29 3b 0a   UNIQUE(a, b));.
4fb0: 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45      CREATE TABLE
4fc0: 20 63 32 28 63 2c 20 64 2c 20 46 4f 52 45 49 47   c2(c, d, FOREIG
4fd0: 4e 20 4b 45 59 28 63 2c 20 64 29 20 52 45 46 45  N KEY(c, d) REFE
4fe0: 52 45 4e 43 45 53 20 70 32 28 61 2c 20 78 29 29  RENCES p2(a, x))
4ff0: 3b 0a 0a 20 20 20 20 43 52 45 41 54 45 20 54 41  ;..    CREATE TA
5000: 42 4c 45 20 70 33 28 61 20 50 52 49 4d 41 52 59  BLE p3(a PRIMARY
5010: 20 4b 45 59 2c 20 62 29 3b 0a 20 20 20 20 43 52   KEY, b);.    CR
5020: 45 41 54 45 20 54 41 42 4c 45 20 63 33 28 63 20  EATE TABLE c3(c 
5030: 52 45 46 45 52 45 4e 43 45 53 20 70 33 28 62 29  REFERENCES p3(b)
5040: 2c 20 64 29 3b 0a 0a 20 20 20 20 43 52 45 41 54  , d);..    CREAT
5050: 45 20 54 41 42 4c 45 20 70 34 28 61 20 50 52 49  E TABLE p4(a PRI
5060: 4d 41 52 59 20 4b 45 59 2c 20 62 29 3b 0a 20 20  MARY KEY, b);.  
5070: 20 20 43 52 45 41 54 45 20 55 4e 49 51 55 45 20    CREATE UNIQUE 
5080: 49 4e 44 45 58 20 70 34 69 20 4f 4e 20 70 34 28  INDEX p4i ON p4(
5090: 62 20 43 4f 4c 4c 41 54 45 20 6e 6f 63 61 73 65  b COLLATE nocase
50a0: 29 3b 0a 20 20 20 20 43 52 45 41 54 45 20 54 41  );.    CREATE TA
50b0: 42 4c 45 20 63 34 28 63 20 52 45 46 45 52 45 4e  BLE c4(c REFEREN
50c0: 43 45 53 20 70 34 28 62 29 2c 20 64 29 3b 0a 0a  CES p4(b), d);..
50d0: 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45      CREATE TABLE
50e0: 20 70 35 28 61 20 50 52 49 4d 41 52 59 20 4b 45   p5(a PRIMARY KE
50f0: 59 2c 20 62 20 43 4f 4c 4c 41 54 45 20 6e 6f 63  Y, b COLLATE noc
5100: 61 73 65 29 3b 0a 20 20 20 20 43 52 45 41 54 45  ase);.    CREATE
5110: 20 55 4e 49 51 55 45 20 49 4e 44 45 58 20 70 35   UNIQUE INDEX p5
5120: 69 20 4f 4e 20 70 35 28 62 20 43 4f 4c 4c 41 54  i ON p5(b COLLAT
5130: 45 20 62 69 6e 61 72 79 29 3b 0a 20 20 20 20 43  E binary);.    C
5140: 52 45 41 54 45 20 54 41 42 4c 45 20 63 35 28 63  REATE TABLE c5(c
5150: 20 52 45 46 45 52 45 4e 43 45 53 20 70 35 28 62   REFERENCES p5(b
5160: 29 2c 20 64 29 3b 0a 0a 20 20 20 20 43 52 45 41  ), d);..    CREA
5170: 54 45 20 54 41 42 4c 45 20 70 36 28 61 20 50 52  TE TABLE p6(a PR
5180: 49 4d 41 52 59 20 4b 45 59 2c 20 62 29 3b 0a 20  IMARY KEY, b);. 
5190: 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20     CREATE TABLE 
51a0: 63 36 28 63 2c 20 64 2c 20 46 4f 52 45 49 47 4e  c6(c, d, FOREIGN
51b0: 20 4b 45 59 28 63 2c 20 64 29 20 52 45 46 45 52   KEY(c, d) REFER
51c0: 45 4e 43 45 53 20 70 36 29 3b 0a 0a 20 20 20 20  ENCES p6);..    
51d0: 43 52 45 41 54 45 20 54 41 42 4c 45 20 70 37 28  CREATE TABLE p7(
51e0: 61 2c 20 62 2c 20 50 52 49 4d 41 52 59 20 4b 45  a, b, PRIMARY KE
51f0: 59 28 61 2c 20 62 29 29 3b 0a 20 20 20 20 43 52  Y(a, b));.    CR
5200: 45 41 54 45 20 54 41 42 4c 45 20 63 37 28 63 2c  EATE TABLE c7(c,
5210: 20 64 20 52 45 46 45 52 45 4e 43 45 53 20 70 37   d REFERENCES p7
5220: 29 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a 0a 66 6f 72  );.  }.} {}..for
5230: 65 61 63 68 20 7b 74 6e 20 74 62 6c 20 70 74 62  each {tn tbl ptb
5240: 6c 20 65 72 72 7d 20 7b 0a 20 20 32 20 63 31 20  l err} {.  2 c1 
5250: 7b 7d 20 22 6e 6f 20 73 75 63 68 20 74 61 62 6c  {} "no such tabl
5260: 65 3a 20 6d 61 69 6e 2e 6e 6f 73 75 63 68 74 61  e: main.nosuchta
5270: 62 6c 65 22 0a 20 20 33 20 63 32 20 70 32 20 22  ble".  3 c2 p2 "
5280: 66 6f 72 65 69 67 6e 20 6b 65 79 20 6d 69 73 6d  foreign key mism
5290: 61 74 63 68 22 0a 20 20 34 20 63 33 20 70 33 20  atch".  4 c3 p3 
52a0: 22 66 6f 72 65 69 67 6e 20 6b 65 79 20 6d 69 73  "foreign key mis
52b0: 6d 61 74 63 68 22 0a 20 20 35 20 63 34 20 70 34  match".  5 c4 p4
52c0: 20 22 66 6f 72 65 69 67 6e 20 6b 65 79 20 6d 69   "foreign key mi
52d0: 73 6d 61 74 63 68 22 0a 20 20 36 20 63 35 20 70  smatch".  6 c5 p
52e0: 35 20 22 66 6f 72 65 69 67 6e 20 6b 65 79 20 6d  5 "foreign key m
52f0: 69 73 6d 61 74 63 68 22 0a 20 20 37 20 63 36 20  ismatch".  7 c6 
5300: 70 36 20 22 66 6f 72 65 69 67 6e 20 6b 65 79 20  p6 "foreign key 
5310: 6d 69 73 6d 61 74 63 68 22 0a 20 20 38 20 63 37  mismatch".  8 c7
5320: 20 70 37 20 22 66 6f 72 65 69 67 6e 20 6b 65 79   p7 "foreign key
5330: 20 6d 69 73 6d 61 74 63 68 22 0a 7d 20 7b 0a 20   mismatch".} {. 
5340: 20 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d   do_test e_fkey-
5350: 36 36 2e 24 74 6e 2e 31 20 7b 0a 20 20 20 20 63  66.$tn.1 {.    c
5360: 61 74 63 68 73 71 6c 20 22 49 4e 53 45 52 54 20  atchsql "INSERT 
5370: 49 4e 54 4f 20 24 74 62 6c 20 56 41 4c 55 45 53  INTO $tbl VALUES
5380: 28 27 61 27 2c 20 27 62 27 29 22 0a 20 20 7d 20  ('a', 'b')".  } 
5390: 5b 6c 69 73 74 20 31 20 24 65 72 72 5d 0a 20 20  [list 1 $err].  
53a0: 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 36  do_test e_fkey-6
53b0: 36 2e 24 74 6e 2e 32 20 7b 0a 20 20 20 20 63 61  6.$tn.2 {.    ca
53c0: 74 63 68 73 71 6c 20 22 55 50 44 41 54 45 20 24  tchsql "UPDATE $
53d0: 74 62 6c 20 53 45 54 20 63 20 3d 20 3f 2c 20 64  tbl SET c = ?, d
53e0: 20 3d 20 3f 22 0a 20 20 7d 20 5b 6c 69 73 74 20   = ?".  } [list 
53f0: 31 20 24 65 72 72 5d 0a 20 20 64 6f 5f 74 65 73  1 $err].  do_tes
5400: 74 20 65 5f 66 6b 65 79 2d 36 36 2e 24 74 6e 2e  t e_fkey-66.$tn.
5410: 33 20 7b 0a 20 20 20 20 63 61 74 63 68 73 71 6c  3 {.    catchsql
5420: 20 22 49 4e 53 45 52 54 20 49 4e 54 4f 20 24 74   "INSERT INTO $t
5430: 62 6c 20 53 45 4c 45 43 54 20 3f 2c 20 3f 22 0a  bl SELECT ?, ?".
5440: 20 20 7d 20 5b 6c 69 73 74 20 31 20 24 65 72 72    } [list 1 $err
5450: 5d 0a 0a 20 20 69 66 20 7b 24 70 74 62 6c 20 6e  ]..  if {$ptbl n
5460: 65 20 22 22 7d 20 7b 0a 20 20 20 20 64 6f 5f 74  e ""} {.    do_t
5470: 65 73 74 20 65 5f 66 6b 65 79 2d 36 36 2e 24 74  est e_fkey-66.$t
5480: 6e 2e 34 20 7b 0a 20 20 20 20 20 20 63 61 74 63  n.4 {.      catc
5490: 68 73 71 6c 20 22 44 45 4c 45 54 45 20 46 52 4f  hsql "DELETE FRO
54a0: 4d 20 24 70 74 62 6c 22 0a 20 20 20 20 7d 20 5b  M $ptbl".    } [
54b0: 6c 69 73 74 20 31 20 24 65 72 72 5d 0a 20 20 20  list 1 $err].   
54c0: 20 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d   do_test e_fkey-
54d0: 36 36 2e 24 74 6e 2e 35 20 7b 0a 20 20 20 20 20  66.$tn.5 {.     
54e0: 20 63 61 74 63 68 73 71 6c 20 22 55 50 44 41 54   catchsql "UPDAT
54f0: 45 20 24 70 74 62 6c 20 53 45 54 20 61 20 3d 20  E $ptbl SET a = 
5500: 3f 2c 20 62 20 3d 20 3f 22 0a 20 20 20 20 7d 20  ?, b = ?".    } 
5510: 5b 6c 69 73 74 20 31 20 24 65 72 72 5d 0a 20 20  [list 1 $err].  
5520: 20 20 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79    do_test e_fkey
5530: 2d 36 36 2e 24 74 6e 2e 36 20 7b 0a 20 20 20 20  -66.$tn.6 {.    
5540: 20 20 63 61 74 63 68 73 71 6c 20 22 49 4e 53 45    catchsql "INSE
5550: 52 54 20 49 4e 54 4f 20 24 70 74 62 6c 20 53 45  RT INTO $ptbl SE
5560: 4c 45 43 54 20 3f 2c 20 3f 22 0a 20 20 20 20 7d  LECT ?, ?".    }
5570: 20 5b 6c 69 73 74 20 31 20 24 65 72 72 5d 0a 20   [list 1 $err]. 
5580: 20 7d 0a 7d 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d   }.}..#---------
5590: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
55a0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
55b0: 2d 2d 2d 2d 2d 2d 2d 2d 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: 0a 23 20 2f 2a 20 45 56 3a 20 52 2d 31 39 33 35  .# /* EV: R-1935
55e0: 33 2d 34 33 36 34 33 20 2a 2f 0a 23 0a 23 20 54  3-43643 */.#.# T
55f0: 65 73 74 20 74 68 65 20 65 78 61 6d 70 6c 65 20  est the example 
5600: 6f 66 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 6d  of foreign key m
5610: 69 73 6d 61 74 63 68 20 65 72 72 6f 72 73 20 63  ismatch errors c
5620: 61 75 73 65 64 20 62 79 20 69 6d 70 6c 69 63 69  aused by implici
5630: 74 6c 79 0a 23 20 6d 61 70 70 69 6e 67 20 61 20  tly.# mapping a 
5640: 63 68 69 6c 64 20 6b 65 79 20 74 6f 20 74 68 65  child key to the
5650: 20 70 72 69 6d 61 72 79 20 6b 65 79 20 6f 66 20   primary key of 
5660: 74 68 65 20 70 61 72 65 6e 74 20 74 61 62 6c 65  the parent table
5670: 20 77 68 65 6e 20 74 68 65 0a 23 20 63 68 69 6c   when the.# chil
5680: 64 20 6b 65 79 20 63 6f 6e 73 69 73 74 73 20 6f  d key consists o
5690: 66 20 61 20 64 69 66 66 65 72 65 6e 74 20 6e 75  f a different nu
56a0: 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20  mber of columns 
56b0: 74 6f 20 74 68 61 74 20 70 72 69 6d 61 72 79 20  to that primary 
56c0: 6b 65 79 2e 0a 23 20 0a 64 72 6f 70 5f 61 6c 6c  key..# .drop_all
56d0: 5f 74 61 62 6c 65 73 0a 64 6f 5f 74 65 73 74 20  _tables.do_test 
56e0: 65 5f 66 6b 65 79 2d 35 38 2e 31 20 7b 0a 20 20  e_fkey-58.1 {.  
56f0: 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 43 52  execsql {.    CR
5700: 45 41 54 45 20 54 41 42 4c 45 20 70 61 72 65 6e  EATE TABLE paren
5710: 74 32 28 61 2c 20 62 2c 20 50 52 49 4d 41 52 59  t2(a, b, PRIMARY
5720: 20 4b 45 59 28 61 2c 62 29 29 3b 0a 0a 20 20 20   KEY(a,b));..   
5730: 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 63 68   CREATE TABLE ch
5740: 69 6c 64 38 28 78 2c 20 79 2c 20 46 4f 52 45 49  ild8(x, y, FOREI
5750: 47 4e 20 4b 45 59 28 78 2c 79 29 20 52 45 46 45  GN KEY(x,y) REFE
5760: 52 45 4e 43 45 53 20 70 61 72 65 6e 74 32 29 3b  RENCES parent2);
5770: 20 20 20 20 20 2d 2d 20 4f 6b 0a 20 20 20 20 43       -- Ok.    C
5780: 52 45 41 54 45 20 54 41 42 4c 45 20 63 68 69 6c  REATE TABLE chil
5790: 64 39 28 78 20 52 45 46 45 52 45 4e 43 45 53 20  d9(x REFERENCES 
57a0: 70 61 72 65 6e 74 32 29 3b 20 20 20 20 20 20 20  parent2);       
57b0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
57c0: 20 20 20 2d 2d 20 45 72 72 0a 20 20 20 20 43 52     -- Err.    CR
57d0: 45 41 54 45 20 54 41 42 4c 45 20 63 68 69 6c 64  EATE TABLE child
57e0: 31 30 28 78 2c 79 2c 7a 2c 20 46 4f 52 45 49 47  10(x,y,z, FOREIG
57f0: 4e 20 4b 45 59 28 78 2c 79 2c 7a 29 20 52 45 46  N KEY(x,y,z) REF
5800: 45 52 45 4e 43 45 53 20 70 61 72 65 6e 74 32 29  ERENCES parent2)
5810: 3b 20 2d 2d 20 45 72 72 0a 20 20 7d 0a 7d 20 7b  ; -- Err.  }.} {
5820: 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79  }.do_test e_fkey
5830: 2d 35 38 2e 32 20 7b 0a 20 20 65 78 65 63 73 71  -58.2 {.  execsq
5840: 6c 20 7b 0a 20 20 20 20 49 4e 53 45 52 54 20 49  l {.    INSERT I
5850: 4e 54 4f 20 70 61 72 65 6e 74 32 20 56 41 4c 55  NTO parent2 VALU
5860: 45 53 28 27 49 27 2c 20 27 49 49 27 29 3b 0a 20  ES('I', 'II');. 
5870: 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 63     INSERT INTO c
5880: 68 69 6c 64 38 20 56 41 4c 55 45 53 28 27 49 27  hild8 VALUES('I'
5890: 2c 20 27 49 49 27 29 3b 0a 20 20 7d 0a 7d 20 7b  , 'II');.  }.} {
58a0: 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79  }.do_test e_fkey
58b0: 2d 35 38 2e 33 20 7b 0a 20 20 63 61 74 63 68 73  -58.3 {.  catchs
58c0: 71 6c 20 7b 20 49 4e 53 45 52 54 20 49 4e 54 4f  ql { INSERT INTO
58d0: 20 63 68 69 6c 64 39 20 56 41 4c 55 45 53 28 27   child9 VALUES('
58e0: 49 27 29 20 7d 0a 7d 20 7b 31 20 7b 66 6f 72 65  I') }.} {1 {fore
58f0: 69 67 6e 20 6b 65 79 20 6d 69 73 6d 61 74 63 68  ign key mismatch
5900: 7d 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65  }}.do_test e_fke
5910: 79 2d 35 38 2e 34 20 7b 0a 20 20 63 61 74 63 68  y-58.4 {.  catch
5920: 73 71 6c 20 7b 20 49 4e 53 45 52 54 20 49 4e 54  sql { INSERT INT
5930: 4f 20 63 68 69 6c 64 39 20 56 41 4c 55 45 53 28  O child9 VALUES(
5940: 27 49 49 27 29 20 7d 0a 7d 20 7b 31 20 7b 66 6f  'II') }.} {1 {fo
5950: 72 65 69 67 6e 20 6b 65 79 20 6d 69 73 6d 61 74  reign key mismat
5960: 63 68 7d 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66  ch}}.do_test e_f
5970: 6b 65 79 2d 35 38 2e 35 20 7b 0a 20 20 63 61 74  key-58.5 {.  cat
5980: 63 68 73 71 6c 20 7b 20 49 4e 53 45 52 54 20 49  chsql { INSERT I
5990: 4e 54 4f 20 63 68 69 6c 64 39 20 56 41 4c 55 45  NTO child9 VALUE
59a0: 53 28 4e 55 4c 4c 29 20 7d 0a 7d 20 7b 31 20 7b  S(NULL) }.} {1 {
59b0: 66 6f 72 65 69 67 6e 20 6b 65 79 20 6d 69 73 6d  foreign key mism
59c0: 61 74 63 68 7d 7d 0a 64 6f 5f 74 65 73 74 20 65  atch}}.do_test e
59d0: 5f 66 6b 65 79 2d 35 38 2e 36 20 7b 0a 20 20 63  _fkey-58.6 {.  c
59e0: 61 74 63 68 73 71 6c 20 7b 20 49 4e 53 45 52 54  atchsql { INSERT
59f0: 20 49 4e 54 4f 20 63 68 69 6c 64 31 30 20 56 41   INTO child10 VA
5a00: 4c 55 45 53 28 27 49 27 2c 20 27 49 49 27 2c 20  LUES('I', 'II', 
5a10: 27 49 49 49 27 29 20 7d 0a 7d 20 7b 31 20 7b 66  'III') }.} {1 {f
5a20: 6f 72 65 69 67 6e 20 6b 65 79 20 6d 69 73 6d 61  oreign key misma
5a30: 74 63 68 7d 7d 0a 64 6f 5f 74 65 73 74 20 65 5f  tch}}.do_test e_
5a40: 66 6b 65 79 2d 35 38 2e 37 20 7b 0a 20 20 63 61  fkey-58.7 {.  ca
5a50: 74 63 68 73 71 6c 20 7b 20 49 4e 53 45 52 54 20  tchsql { INSERT 
5a60: 49 4e 54 4f 20 63 68 69 6c 64 31 30 20 56 41 4c  INTO child10 VAL
5a70: 55 45 53 28 31 2c 20 32 2c 20 33 29 20 7d 0a 7d  UES(1, 2, 3) }.}
5a80: 20 7b 31 20 7b 66 6f 72 65 69 67 6e 20 6b 65 79   {1 {foreign key
5a90: 20 6d 69 73 6d 61 74 63 68 7d 7d 0a 64 6f 5f 74   mismatch}}.do_t
5aa0: 65 73 74 20 65 5f 66 6b 65 79 2d 35 38 2e 38 20  est e_fkey-58.8 
5ab0: 7b 0a 20 20 63 61 74 63 68 73 71 6c 20 7b 20 49  {.  catchsql { I
5ac0: 4e 53 45 52 54 20 49 4e 54 4f 20 63 68 69 6c 64  NSERT INTO child
5ad0: 31 30 20 56 41 4c 55 45 53 28 4e 55 4c 4c 2c 20  10 VALUES(NULL, 
5ae0: 4e 55 4c 4c 2c 20 4e 55 4c 4c 29 20 7d 0a 7d 20  NULL, NULL) }.} 
5af0: 7b 31 20 7b 66 6f 72 65 69 67 6e 20 6b 65 79 20  {1 {foreign key 
5b00: 6d 69 73 6d 61 74 63 68 7d 7d 0a 0a 23 2d 2d 2d  mismatch}}..#---
5b10: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
5b20: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
5b30: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
5b40: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
5b50: 2d 2d 2d 2d 2d 2d 0a 23 20 2f 2a 20 45 56 3a 20  ------.# /* EV: 
5b60: 52 2d 32 33 36 38 32 2d 35 39 38 32 30 20 2a 2f  R-23682-59820 */
5b70: 0a 23 0a 23 20 54 65 73 74 20 65 72 72 6f 72 73  .#.# Test errors
5b80: 20 74 68 61 74 20 61 72 65 20 72 65 70 6f 72 74   that are report
5b90: 65 64 20 77 68 65 6e 20 63 72 65 61 74 69 6e 67  ed when creating
5ba0: 20 74 68 65 20 63 68 69 6c 64 20 74 61 62 6c 65   the child table
5bb0: 2e 20 0a 23 20 53 70 65 63 69 66 69 63 61 6c 6c  . .# Specificall
5bc0: 79 3a 0a 23 0a 23 20 20 20 2a 20 64 69 66 66 65  y:.#.#   * diffe
5bd0: 72 65 6e 74 20 6e 75 6d 62 65 72 20 6f 66 20 63  rent number of c
5be0: 68 69 6c 64 20 61 6e 64 20 70 61 72 65 6e 74 20  hild and parent 
5bf0: 6b 65 79 20 63 6f 6c 75 6d 6e 73 2c 20 61 6e 64  key columns, and
5c00: 0a 23 20 20 20 2a 20 63 68 69 6c 64 20 63 6f 6c  .#   * child col
5c10: 75 6d 6e 73 20 74 68 61 74 20 64 6f 20 6e 6f 74  umns that do not
5c20: 20 65 78 69 73 74 2e 0a 23 0a 23 20 2f 2a 20 45   exist..#.# /* E
5c30: 56 3a 20 52 2d 33 33 38 38 33 2d 32 38 38 33 33  V: R-33883-28833
5c40: 20 2a 2f 0a 23 0a 23 20 54 68 65 73 65 20 65 72   */.#.# These er
5c50: 72 6f 72 73 20 61 72 65 20 72 65 70 6f 72 74 65  rors are reporte
5c60: 64 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74  d whether or not
5c70: 20 46 4b 20 73 75 70 70 6f 72 74 20 69 73 20 65   FK support is e
5c80: 6e 61 62 6c 65 64 2e 0a 23 0a 64 72 6f 70 5f 61  nabled..#.drop_a
5c90: 6c 6c 5f 74 61 62 6c 65 73 0a 66 6f 72 65 61 63  ll_tables.foreac
5ca0: 68 20 66 6b 20 5b 6c 69 73 74 20 4f 46 46 20 4f  h fk [list OFF O
5cb0: 4e 5d 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 22  N] {.  execsql "
5cc0: 50 52 41 47 4d 41 20 66 6f 72 65 69 67 6e 5f 6b  PRAGMA foreign_k
5cd0: 65 79 73 20 3d 20 24 66 6b 22 0a 20 20 73 65 74  eys = $fk".  set
5ce0: 20 69 20 30 0a 20 20 66 6f 72 65 61 63 68 20 7b   i 0.  foreach {
5cf0: 73 71 6c 20 65 72 72 6f 72 7d 20 7b 0a 20 20 20  sql error} {.   
5d00: 20 22 43 52 45 41 54 45 20 54 41 42 4c 45 20 63   "CREATE TABLE c
5d10: 68 69 6c 64 31 28 61 2c 20 62 2c 20 46 4f 52 45  hild1(a, b, FORE
5d20: 49 47 4e 20 4b 45 59 28 61 2c 20 62 29 20 52 45  IGN KEY(a, b) RE
5d30: 46 45 52 45 4e 43 45 53 20 70 28 63 29 29 22 0a  FERENCES p(c))".
5d40: 20 20 20 20 20 20 7b 6e 75 6d 62 65 72 20 6f 66        {number of
5d50: 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 66 6f 72 65   columns in fore
5d60: 69 67 6e 20 6b 65 79 20 64 6f 65 73 20 6e 6f 74  ign key does not
5d70: 20 6d 61 74 63 68 20 74 68 65 20 6e 75 6d 62 65   match the numbe
5d80: 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20  r of columns in 
5d90: 74 68 65 20 72 65 66 65 72 65 6e 63 65 64 20 74  the referenced t
5da0: 61 62 6c 65 7d 0a 20 20 20 20 22 43 52 45 41 54  able}.    "CREAT
5db0: 45 20 54 41 42 4c 45 20 63 68 69 6c 64 32 28 61  E TABLE child2(a
5dc0: 2c 20 62 2c 20 46 4f 52 45 49 47 4e 20 4b 45 59  , b, FOREIGN KEY
5dd0: 28 61 2c 20 62 29 20 52 45 46 45 52 45 4e 43 45  (a, b) REFERENCE
5de0: 53 20 70 28 63 2c 20 64 2c 20 65 29 29 22 0a 20  S p(c, d, e))". 
5df0: 20 20 20 20 20 7b 6e 75 6d 62 65 72 20 6f 66 20       {number of 
5e00: 63 6f 6c 75 6d 6e 73 20 69 6e 20 66 6f 72 65 69  columns in forei
5e10: 67 6e 20 6b 65 79 20 64 6f 65 73 20 6e 6f 74 20  gn key does not 
5e20: 6d 61 74 63 68 20 74 68 65 20 6e 75 6d 62 65 72  match the number
5e30: 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74   of columns in t
5e40: 68 65 20 72 65 66 65 72 65 6e 63 65 64 20 74 61  he referenced ta
5e50: 62 6c 65 7d 0a 20 20 20 20 22 43 52 45 41 54 45  ble}.    "CREATE
5e60: 20 54 41 42 4c 45 20 63 68 69 6c 64 32 28 61 2c   TABLE child2(a,
5e70: 20 62 2c 20 46 4f 52 45 49 47 4e 20 4b 45 59 28   b, FOREIGN KEY(
5e80: 61 2c 20 63 29 20 52 45 46 45 52 45 4e 43 45 53  a, c) REFERENCES
5e90: 20 70 28 63 2c 20 64 29 29 22 0a 20 20 20 20 20   p(c, d))".     
5ea0: 20 7b 75 6e 6b 6e 6f 77 6e 20 63 6f 6c 75 6d 6e   {unknown column
5eb0: 20 22 63 22 20 69 6e 20 66 6f 72 65 69 67 6e 20   "c" in foreign 
5ec0: 6b 65 79 20 64 65 66 69 6e 69 74 69 6f 6e 7d 0a  key definition}.
5ed0: 20 20 20 20 22 43 52 45 41 54 45 20 54 41 42 4c      "CREATE TABL
5ee0: 45 20 63 68 69 6c 64 32 28 61 2c 20 62 2c 20 46  E child2(a, b, F
5ef0: 4f 52 45 49 47 4e 20 4b 45 59 28 63 2c 20 62 29  OREIGN KEY(c, b)
5f00: 20 52 45 46 45 52 45 4e 43 45 53 20 70 28 63 2c   REFERENCES p(c,
5f10: 20 64 29 29 22 0a 20 20 20 20 20 20 7b 75 6e 6b   d))".      {unk
5f20: 6e 6f 77 6e 20 63 6f 6c 75 6d 6e 20 22 63 22 20  nown column "c" 
5f30: 69 6e 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 64  in foreign key d
5f40: 65 66 69 6e 69 74 69 6f 6e 7d 0a 20 20 7d 20 7b  efinition}.  } {
5f50: 0a 20 20 20 20 64 6f 5f 74 65 73 74 20 65 5f 66  .    do_test e_f
5f60: 6b 65 79 2d 35 39 2e 24 66 6b 2e 5b 69 6e 63 72  key-59.$fk.[incr
5f70: 20 69 5d 20 7b 0a 20 20 20 20 20 20 63 61 74 63   i] {.      catc
5f80: 68 73 71 6c 20 24 73 71 6c 0a 20 20 20 20 7d 20  hsql $sql.    } 
5f90: 5b 6c 69 73 74 20 31 20 24 65 72 72 6f 72 5d 0a  [list 1 $error].
5fa0: 20 20 7d 0a 7d 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d    }.}..#--------
5fb0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
5fc0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
5fd0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
5fe0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
5ff0: 2d 0a 23 20 2f 2a 20 45 56 3a 20 52 2d 34 37 31  -.# /* EV: R-471
6000: 30 39 2d 34 30 35 38 31 20 2a 2f 0a 23 0a 23 20  09-40581 */.#.# 
6010: 54 65 73 74 20 74 68 61 74 20 61 20 52 45 46 45  Test that a REFE
6020: 52 45 4e 43 49 4e 47 20 63 6c 61 75 73 65 20 74  RENCING clause t
6030: 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 73 70 65  hat does not spe
6040: 63 69 66 79 20 70 61 72 65 6e 74 20 6b 65 79 20  cify parent key 
6050: 63 6f 6c 75 6d 6e 73 0a 23 20 69 6d 70 6c 69 63  columns.# implic
6060: 69 74 6c 79 20 6d 61 70 73 20 74 6f 20 74 68 65  itly maps to the
6070: 20 70 72 69 6d 61 72 79 20 6b 65 79 20 6f 66 20   primary key of 
6080: 74 68 65 20 70 61 72 65 6e 74 20 74 61 62 6c 65  the parent table
6090: 2e 0a 23 20 0a 64 6f 5f 74 65 73 74 20 65 5f 66  ..# .do_test e_f
60a0: 6b 65 79 2d 36 30 2e 31 20 7b 0a 20 20 65 78 65  key-60.1 {.  exe
60b0: 63 73 71 6c 20 7b 0a 20 20 20 20 43 52 45 41 54  csql {.    CREAT
60c0: 45 20 54 41 42 4c 45 20 70 31 28 61 2c 20 62 2c  E TABLE p1(a, b,
60d0: 20 50 52 49 4d 41 52 59 20 4b 45 59 28 61 2c 20   PRIMARY KEY(a, 
60e0: 62 29 29 3b 0a 20 20 20 20 43 52 45 41 54 45 20  b));.    CREATE 
60f0: 54 41 42 4c 45 20 70 32 28 61 2c 20 62 20 50 52  TABLE p2(a, b PR
6100: 49 4d 41 52 59 20 4b 45 59 29 3b 0a 20 20 20 20  IMARY KEY);.    
6110: 43 52 45 41 54 45 20 54 41 42 4c 45 20 63 31 28  CREATE TABLE c1(
6120: 63 2c 20 64 2c 20 46 4f 52 45 49 47 4e 20 4b 45  c, d, FOREIGN KE
6130: 59 28 63 2c 20 64 29 20 52 45 46 45 52 45 4e 43  Y(c, d) REFERENC
6140: 45 53 20 70 31 29 3b 0a 20 20 20 20 43 52 45 41  ES p1);.    CREA
6150: 54 45 20 54 41 42 4c 45 20 63 32 28 61 2c 20 62  TE TABLE c2(a, b
6160: 20 52 45 46 45 52 45 4e 43 45 53 20 70 32 29 3b   REFERENCES p2);
6170: 0a 20 20 7d 0a 7d 20 7b 7d 0a 70 72 6f 63 20 74  .  }.} {}.proc t
6180: 65 73 74 5f 65 66 6b 65 79 5f 36 30 20 7b 74 6e  est_efkey_60 {tn
6190: 20 69 73 45 72 72 6f 72 20 73 71 6c 7d 20 7b 0a   isError sql} {.
61a0: 20 20 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79    do_test e_fkey
61b0: 2d 36 30 2e 24 74 6e 20 22 0a 20 20 20 20 63 61  -60.$tn ".    ca
61c0: 74 63 68 73 71 6c 20 7b 24 73 71 6c 7d 0a 20 20  tchsql {$sql}.  
61d0: 22 20 5b 6c 69 6e 64 65 78 20 7b 7b 30 20 7b 7d  " [lindex {{0 {}
61e0: 7d 20 7b 31 20 7b 66 6f 72 65 69 67 6e 20 6b 65  } {1 {foreign ke
61f0: 79 20 63 6f 6e 73 74 72 61 69 6e 74 20 66 61 69  y constraint fai
6200: 6c 65 64 7d 7d 7d 20 24 69 73 45 72 72 6f 72 5d  led}}} $isError]
6210: 0a 7d 0a 0a 74 65 73 74 5f 65 66 6b 65 79 5f 36  .}..test_efkey_6
6220: 30 20 32 20 31 20 22 49 4e 53 45 52 54 20 49 4e  0 2 1 "INSERT IN
6230: 54 4f 20 63 31 20 56 41 4c 55 45 53 28 32 33 39  TO c1 VALUES(239
6240: 2c 20 32 33 31 29 22 0a 74 65 73 74 5f 65 66 6b  , 231)".test_efk
6250: 65 79 5f 36 30 20 33 20 30 20 22 49 4e 53 45 52  ey_60 3 0 "INSER
6260: 54 20 49 4e 54 4f 20 70 31 20 56 41 4c 55 45 53  T INTO p1 VALUES
6270: 28 32 33 39 2c 20 32 33 31 29 22 0a 74 65 73 74  (239, 231)".test
6280: 5f 65 66 6b 65 79 5f 36 30 20 34 20 30 20 22 49  _efkey_60 4 0 "I
6290: 4e 53 45 52 54 20 49 4e 54 4f 20 63 31 20 56 41  NSERT INTO c1 VA
62a0: 4c 55 45 53 28 32 33 39 2c 20 32 33 31 29 22 0a  LUES(239, 231)".
62b0: 74 65 73 74 5f 65 66 6b 65 79 5f 36 30 20 35 20  test_efkey_60 5 
62c0: 31 20 22 49 4e 53 45 52 54 20 49 4e 54 4f 20 63  1 "INSERT INTO c
62d0: 32 20 56 41 4c 55 45 53 28 32 33 39 2c 20 32 33  2 VALUES(239, 23
62e0: 31 29 22 0a 74 65 73 74 5f 65 66 6b 65 79 5f 36  1)".test_efkey_6
62f0: 30 20 36 20 30 20 22 49 4e 53 45 52 54 20 49 4e  0 6 0 "INSERT IN
6300: 54 4f 20 70 32 20 56 41 4c 55 45 53 28 32 33 39  TO p2 VALUES(239
6310: 2c 20 32 33 31 29 22 0a 74 65 73 74 5f 65 66 6b  , 231)".test_efk
6320: 65 79 5f 36 30 20 37 20 30 20 22 49 4e 53 45 52  ey_60 7 0 "INSER
6330: 54 20 49 4e 54 4f 20 63 32 20 56 41 4c 55 45 53  T INTO c2 VALUES
6340: 28 32 33 39 2c 20 32 33 31 29 22 0a 0a 23 2d 2d  (239, 231)"..#--
6350: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
6360: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
6370: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
6380: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
6390: 2d 2d 2d 2d 2d 2d 2d 0a 23 20 2f 2a 20 45 56 3a  -------.# /* EV:
63a0: 20 52 2d 31 35 34 31 37 2d 32 38 30 31 34 20 2a   R-15417-28014 *
63b0: 2f 0a 23 0a 23 20 54 65 73 74 20 74 68 61 74 20  /.#.# Test that 
63c0: 61 6e 20 69 6e 64 65 78 20 6f 6e 20 6f 6e 20 74  an index on on t
63d0: 68 65 20 63 68 69 6c 64 20 6b 65 79 20 63 6f 6c  he child key col
63e0: 75 6d 6e 73 20 6f 66 20 61 6e 20 46 4b 20 63 6f  umns of an FK co
63f0: 6e 73 74 72 61 69 6e 74 0a 23 20 69 73 20 6f 70  nstraint.# is op
6400: 74 69 6f 6e 61 6c 2e 0a 23 0a 23 20 2f 2a 20 45  tional..#.# /* E
6410: 56 3a 20 52 2d 31 35 37 34 31 2d 35 30 38 39 33  V: R-15741-50893
6420: 20 2a 2f 0a 23 0a 23 20 41 6c 73 6f 20 74 65 73   */.#.# Also tes
6430: 74 20 74 68 61 74 20 69 66 20 61 6e 20 69 6e 64  t that if an ind
6440: 65 78 20 69 73 20 63 72 65 61 74 65 64 20 6f 6e  ex is created on
6450: 20 74 68 65 20 63 68 69 6c 64 20 6b 65 79 20 63   the child key c
6460: 6f 6c 75 6d 6e 73 2c 20 69 74 20 64 6f 65 73 0a  olumns, it does.
6470: 23 20 6e 6f 74 20 6d 61 6b 65 20 61 20 64 69 66  # not make a dif
6480: 66 65 72 65 6e 63 65 20 77 68 65 74 68 65 72 20  ference whether 
6490: 6f 72 20 6e 6f 74 20 69 74 20 69 73 20 61 20 55  or not it is a U
64a0: 4e 49 51 55 45 20 69 6e 64 65 78 2e 0a 23 0a 64  NIQUE index..#.d
64b0: 72 6f 70 5f 61 6c 6c 5f 74 61 62 6c 65 73 0a 64  rop_all_tables.d
64c0: 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 36 31  o_test e_fkey-61
64d0: 2e 31 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b  .1 {.  execsql {
64e0: 0a 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c  .    CREATE TABL
64f0: 45 20 70 61 72 65 6e 74 28 78 2c 20 79 2c 20 55  E parent(x, y, U
6500: 4e 49 51 55 45 28 79 2c 20 78 29 29 3b 0a 20 20  NIQUE(y, x));.  
6510: 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 63    CREATE TABLE c
6520: 31 28 61 2c 20 62 2c 20 46 4f 52 45 49 47 4e 20  1(a, b, FOREIGN 
6530: 4b 45 59 28 61 2c 20 62 29 20 52 45 46 45 52 45  KEY(a, b) REFERE
6540: 4e 43 45 53 20 70 61 72 65 6e 74 28 78 2c 20 79  NCES parent(x, y
6550: 29 29 3b 0a 20 20 20 20 43 52 45 41 54 45 20 54  ));.    CREATE T
6560: 41 42 4c 45 20 63 32 28 61 2c 20 62 2c 20 46 4f  ABLE c2(a, b, FO
6570: 52 45 49 47 4e 20 4b 45 59 28 61 2c 20 62 29 20  REIGN KEY(a, b) 
6580: 52 45 46 45 52 45 4e 43 45 53 20 70 61 72 65 6e  REFERENCES paren
6590: 74 28 78 2c 20 79 29 29 3b 0a 20 20 20 20 43 52  t(x, y));.    CR
65a0: 45 41 54 45 20 54 41 42 4c 45 20 63 33 28 61 2c  EATE TABLE c3(a,
65b0: 20 62 2c 20 46 4f 52 45 49 47 4e 20 4b 45 59 28   b, FOREIGN KEY(
65c0: 61 2c 20 62 29 20 52 45 46 45 52 45 4e 43 45 53  a, b) REFERENCES
65d0: 20 70 61 72 65 6e 74 28 78 2c 20 79 29 29 3b 0a   parent(x, y));.
65e0: 20 20 20 20 43 52 45 41 54 45 20 49 4e 44 45 58      CREATE INDEX
65f0: 20 63 32 69 20 4f 4e 20 63 32 28 61 2c 20 62 29   c2i ON c2(a, b)
6600: 3b 0a 20 20 20 20 43 52 45 41 54 45 20 55 4e 49  ;.    CREATE UNI
6610: 51 55 45 20 49 4e 44 45 58 20 63 33 69 20 4f 4e  QUE INDEX c3i ON
6620: 20 63 32 28 62 2c 20 61 29 3b 0a 20 20 7d 0a 7d   c2(b, a);.  }.}
6630: 20 7b 7d 0a 70 72 6f 63 20 74 65 73 74 5f 65 66   {}.proc test_ef
6640: 6b 65 79 5f 36 31 20 7b 74 6e 20 69 73 45 72 72  key_61 {tn isErr
6650: 6f 72 20 73 71 6c 7d 20 7b 0a 20 20 64 6f 5f 74  or sql} {.  do_t
6660: 65 73 74 20 65 5f 66 6b 65 79 2d 36 31 2e 24 74  est e_fkey-61.$t
6670: 6e 20 22 0a 20 20 20 20 63 61 74 63 68 73 71 6c  n ".    catchsql
6680: 20 7b 24 73 71 6c 7d 0a 20 20 22 20 5b 6c 69 6e   {$sql}.  " [lin
6690: 64 65 78 20 7b 7b 30 20 7b 7d 7d 20 7b 31 20 7b  dex {{0 {}} {1 {
66a0: 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73  foreign key cons
66b0: 74 72 61 69 6e 74 20 66 61 69 6c 65 64 7d 7d 7d  traint failed}}}
66c0: 20 24 69 73 45 72 72 6f 72 5d 0a 7d 0a 66 6f 72   $isError].}.for
66d0: 65 61 63 68 20 7b 74 6e 20 63 7d 20 5b 6c 69 73  each {tn c} [lis
66e0: 74 20 32 20 63 31 20 33 20 63 32 20 34 20 63 33  t 2 c1 3 c2 4 c3
66f0: 5d 20 7b 0a 20 20 74 65 73 74 5f 65 66 6b 65 79  ] {.  test_efkey
6700: 5f 36 31 20 24 74 6e 2e 31 20 31 20 22 49 4e 53  _61 $tn.1 1 "INS
6710: 45 52 54 20 49 4e 54 4f 20 24 63 20 56 41 4c 55  ERT INTO $c VALU
6720: 45 53 28 31 2c 20 32 29 22 0a 20 20 74 65 73 74  ES(1, 2)".  test
6730: 5f 65 66 6b 65 79 5f 36 31 20 24 74 6e 2e 32 20  _efkey_61 $tn.2 
6740: 30 20 22 49 4e 53 45 52 54 20 49 4e 54 4f 20 70  0 "INSERT INTO p
6750: 61 72 65 6e 74 20 56 41 4c 55 45 53 28 31 2c 20  arent VALUES(1, 
6760: 32 29 22 0a 20 20 74 65 73 74 5f 65 66 6b 65 79  2)".  test_efkey
6770: 5f 36 31 20 24 74 6e 2e 33 20 30 20 22 49 4e 53  _61 $tn.3 0 "INS
6780: 45 52 54 20 49 4e 54 4f 20 24 63 20 56 41 4c 55  ERT INTO $c VALU
6790: 45 53 28 31 2c 20 32 29 22 0a 0a 20 20 65 78 65  ES(1, 2)"..  exe
67a0: 63 73 71 6c 20 22 44 45 4c 45 54 45 20 46 52 4f  csql "DELETE FRO
67b0: 4d 20 24 63 20 3b 20 44 45 4c 45 54 45 20 46 52  M $c ; DELETE FR
67c0: 4f 4d 20 70 61 72 65 6e 74 22 0a 7d 0a 0a 23 2d  OM parent".}..#-
67d0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
67e0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
67f0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
6800: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
6810: 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20 2f 2a 20 45 56  --------.# /* EV
6820: 3a 20 52 2d 30 30 32 37 39 2d 35 32 32 38 33 20  : R-00279-52283 
6830: 2a 2f 0a 23 0a 23 20 54 65 73 74 20 61 6e 20 65  */.#.# Test an e
6840: 78 61 6d 70 6c 65 20 73 68 6f 77 69 6e 67 20 74  xample showing t
6850: 68 61 74 20 77 68 65 6e 20 61 20 72 6f 77 20 69  hat when a row i
6860: 73 20 64 65 6c 65 74 65 64 20 66 72 6f 6d 20 74  s deleted from t
6870: 68 65 20 70 61 72 65 6e 74 20 0a 23 20 74 61 62  he parent .# tab
6880: 6c 65 2c 20 74 68 65 20 63 68 69 6c 64 20 74 61  le, the child ta
6890: 62 6c 65 20 69 73 20 71 75 65 72 69 65 64 20 66  ble is queried f
68a0: 6f 72 20 6f 72 70 68 61 6e 65 64 20 72 6f 77 73  or orphaned rows
68b0: 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 23 0a 23   as follows:.#.#
68c0: 20 20 20 53 45 4c 45 43 54 20 72 6f 77 69 64 20     SELECT rowid 
68d0: 46 52 4f 4d 20 74 72 61 63 6b 20 57 48 45 52 45  FROM track WHERE
68e0: 20 74 72 61 63 6b 61 72 74 69 73 74 20 3d 20 3f   trackartist = ?
68f0: 0a 23 0a 23 20 2f 2a 20 45 56 3a 20 52 2d 32 33  .#.# /* EV: R-23
6900: 33 30 32 2d 33 30 39 35 36 20 2a 2f 0a 23 0a 23  302-30956 */.#.#
6910: 20 41 6c 73 6f 20 74 65 73 74 20 74 68 61 74 20   Also test that 
6920: 69 66 20 74 68 65 20 53 45 4c 45 43 54 20 61 62  if the SELECT ab
6930: 6f 76 65 20 77 6f 75 6c 64 20 72 65 74 75 72 6e  ove would return
6940: 20 61 6e 79 20 72 6f 77 73 2c 20 61 20 66 6f 72   any rows, a for
6950: 65 69 67 6e 0a 23 20 6b 65 79 20 63 6f 6e 73 74  eign.# key const
6960: 72 61 69 6e 74 20 69 73 20 76 69 6f 6c 61 74 65  raint is violate
6970: 64 2e 0a 23 0a 64 6f 5f 74 65 73 74 20 65 5f 66  d..#.do_test e_f
6980: 6b 65 79 2d 36 32 2e 31 20 7b 0a 20 20 65 78 65  key-62.1 {.  exe
6990: 63 73 71 6c 20 7b 0a 20 20 20 20 43 52 45 41 54  csql {.    CREAT
69a0: 45 20 54 41 42 4c 45 20 61 72 74 69 73 74 28 0a  E TABLE artist(.
69b0: 20 20 20 20 20 20 61 72 74 69 73 74 69 64 20 20        artistid  
69c0: 20 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52    INTEGER PRIMAR
69d0: 59 20 4b 45 59 2c 20 0a 20 20 20 20 20 20 61 72  Y KEY, .      ar
69e0: 74 69 73 74 6e 61 6d 65 20 20 54 45 58 54 0a 20  tistname  TEXT. 
69f0: 20 20 20 29 3b 0a 20 20 20 20 43 52 45 41 54 45     );.    CREATE
6a00: 20 54 41 42 4c 45 20 74 72 61 63 6b 28 0a 20 20   TABLE track(.  
6a10: 20 20 20 20 74 72 61 63 6b 69 64 20 20 20 20 20      trackid     
6a20: 49 4e 54 45 47 45 52 2c 20 0a 20 20 20 20 20 20  INTEGER, .      
6a30: 74 72 61 63 6b 6e 61 6d 65 20 20 20 54 45 58 54  trackname   TEXT
6a40: 2c 20 0a 20 20 20 20 20 20 74 72 61 63 6b 61 72  , .      trackar
6a50: 74 69 73 74 20 49 4e 54 45 47 45 52 2c 0a 20 20  tist INTEGER,.  
6a60: 20 20 20 20 46 4f 52 45 49 47 4e 20 4b 45 59 28      FOREIGN KEY(
6a70: 74 72 61 63 6b 61 72 74 69 73 74 29 20 52 45 46  trackartist) REF
6a80: 45 52 45 4e 43 45 53 20 61 72 74 69 73 74 28 61  ERENCES artist(a
6a90: 72 74 69 73 74 69 64 29 0a 20 20 20 20 29 3b 0a  rtistid).    );.
6aa0: 20 20 7d 0a 7d 20 7b 7d 0a 64 6f 5f 74 65 73 74    }.} {}.do_test
6ab0: 20 65 5f 66 6b 65 79 2d 36 32 2e 32 20 7b 0a 20   e_fkey-62.2 {. 
6ac0: 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 50   execsql {.    P
6ad0: 52 41 47 4d 41 20 66 6f 72 65 69 67 6e 5f 6b 65  RAGMA foreign_ke
6ae0: 79 73 20 3d 20 4f 46 46 3b 0a 20 20 20 20 45 58  ys = OFF;.    EX
6af0: 50 4c 41 49 4e 20 51 55 45 52 59 20 50 4c 41 4e  PLAIN QUERY PLAN
6b00: 20 44 45 4c 45 54 45 20 46 52 4f 4d 20 61 72 74   DELETE FROM art
6b10: 69 73 74 20 57 48 45 52 45 20 31 3b 0a 20 20 20  ist WHERE 1;.   
6b20: 20 45 58 50 4c 41 49 4e 20 51 55 45 52 59 20 50   EXPLAIN QUERY P
6b30: 4c 41 4e 20 53 45 4c 45 43 54 20 72 6f 77 69 64  LAN SELECT rowid
6b40: 20 46 52 4f 4d 20 74 72 61 63 6b 20 57 48 45 52   FROM track WHER
6b50: 45 20 74 72 61 63 6b 61 72 74 69 73 74 20 3d 20  E trackartist = 
6b60: 3f 3b 0a 20 20 7d 0a 7d 20 7b 30 20 30 20 7b 54  ?;.  }.} {0 0 {T
6b70: 41 42 4c 45 20 61 72 74 69 73 74 7d 20 30 20 30  ABLE artist} 0 0
6b80: 20 7b 54 41 42 4c 45 20 74 72 61 63 6b 7d 7d 0a   {TABLE track}}.
6b90: 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 36  do_test e_fkey-6
6ba0: 32 2e 33 20 7b 0a 20 20 65 78 65 63 73 71 6c 20  2.3 {.  execsql 
6bb0: 7b 20 0a 20 20 20 20 50 52 41 47 4d 41 20 66 6f  { .    PRAGMA fo
6bc0: 72 65 69 67 6e 5f 6b 65 79 73 20 3d 20 4f 4e 3b  reign_keys = ON;
6bd0: 0a 20 20 20 20 45 58 50 4c 41 49 4e 20 51 55 45  .    EXPLAIN QUE
6be0: 52 59 20 50 4c 41 4e 20 44 45 4c 45 54 45 20 46  RY PLAN DELETE F
6bf0: 52 4f 4d 20 61 72 74 69 73 74 20 57 48 45 52 45  ROM artist WHERE
6c00: 20 31 3b 0a 20 20 7d 0a 7d 20 7b 30 20 30 20 7b   1;.  }.} {0 0 {
6c10: 54 41 42 4c 45 20 61 72 74 69 73 74 7d 20 30 20  TABLE artist} 0 
6c20: 30 20 7b 54 41 42 4c 45 20 74 72 61 63 6b 7d 7d  0 {TABLE track}}
6c30: 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d  .do_test e_fkey-
6c40: 36 32 2e 34 20 7b 0a 20 20 65 78 65 63 73 71 6c  62.4 {.  execsql
6c50: 20 7b 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e   {.    INSERT IN
6c60: 54 4f 20 61 72 74 69 73 74 20 56 41 4c 55 45 53  TO artist VALUES
6c70: 28 35 2c 20 27 61 72 74 69 73 74 20 35 27 29 3b  (5, 'artist 5');
6c80: 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f  .    INSERT INTO
6c90: 20 61 72 74 69 73 74 20 56 41 4c 55 45 53 28 36   artist VALUES(6
6ca0: 2c 20 27 61 72 74 69 73 74 20 36 27 29 3b 0a 20  , 'artist 6');. 
6cb0: 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 61     INSERT INTO a
6cc0: 72 74 69 73 74 20 56 41 4c 55 45 53 28 37 2c 20  rtist VALUES(7, 
6cd0: 27 61 72 74 69 73 74 20 37 27 29 3b 0a 20 20 20  'artist 7');.   
6ce0: 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 72 61   INSERT INTO tra
6cf0: 63 6b 20 56 41 4c 55 45 53 28 31 2c 20 27 74 72  ck VALUES(1, 'tr
6d00: 61 63 6b 20 31 27 2c 20 35 29 3b 0a 20 20 20 20  ack 1', 5);.    
6d10: 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 72 61 63  INSERT INTO trac
6d20: 6b 20 56 41 4c 55 45 53 28 32 2c 20 27 74 72 61  k VALUES(2, 'tra
6d30: 63 6b 20 32 27 2c 20 36 29 3b 0a 20 20 7d 0a 7d  ck 2', 6);.  }.}
6d40: 20 7b 7d 0a 0a 64 6f 5f 74 65 73 74 20 65 5f 66   {}..do_test e_f
6d50: 6b 65 79 2d 36 32 2e 35 20 7b 0a 20 20 63 6f 6e  key-62.5 {.  con
6d60: 63 61 74 20 5c 0a 20 20 20 20 5b 65 78 65 63 73  cat \.    [execs
6d70: 71 6c 20 7b 20 53 45 4c 45 43 54 20 72 6f 77 69  ql { SELECT rowi
6d80: 64 20 46 52 4f 4d 20 74 72 61 63 6b 20 57 48 45  d FROM track WHE
6d90: 52 45 20 74 72 61 63 6b 61 72 74 69 73 74 20 3d  RE trackartist =
6da0: 20 35 20 7d 5d 20 20 20 5c 0a 20 20 20 20 5b 63   5 }]   \.    [c
6db0: 61 74 63 68 73 71 6c 20 7b 20 44 45 4c 45 54 45  atchsql { DELETE
6dc0: 20 46 52 4f 4d 20 61 72 74 69 73 74 20 57 48 45   FROM artist WHE
6dd0: 52 45 20 61 72 74 69 73 74 69 64 20 3d 20 35 20  RE artistid = 5 
6de0: 7d 5d 0a 7d 20 7b 31 20 31 20 7b 66 6f 72 65 69  }].} {1 1 {forei
6df0: 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e  gn key constrain
6e00: 74 20 66 61 69 6c 65 64 7d 7d 0a 0a 64 6f 5f 74  t failed}}..do_t
6e10: 65 73 74 20 65 5f 66 6b 65 79 2d 36 32 2e 36 20  est e_fkey-62.6 
6e20: 7b 0a 20 20 63 6f 6e 63 61 74 20 5c 0a 20 20 20  {.  concat \.   
6e30: 20 5b 65 78 65 63 73 71 6c 20 7b 20 53 45 4c 45   [execsql { SELE
6e40: 43 54 20 72 6f 77 69 64 20 46 52 4f 4d 20 74 72  CT rowid FROM tr
6e50: 61 63 6b 20 57 48 45 52 45 20 74 72 61 63 6b 61  ack WHERE tracka
6e60: 72 74 69 73 74 20 3d 20 37 20 7d 5d 20 20 20 5c  rtist = 7 }]   \
6e70: 0a 20 20 20 20 5b 63 61 74 63 68 73 71 6c 20 7b  .    [catchsql {
6e80: 20 44 45 4c 45 54 45 20 46 52 4f 4d 20 61 72 74   DELETE FROM art
6e90: 69 73 74 20 57 48 45 52 45 20 61 72 74 69 73 74  ist WHERE artist
6ea0: 69 64 20 3d 20 37 20 7d 5d 0a 7d 20 7b 30 20 7b  id = 7 }].} {0 {
6eb0: 7d 7d 0a 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b  }}..do_test e_fk
6ec0: 65 79 2d 36 32 2e 37 20 7b 0a 20 20 63 6f 6e 63  ey-62.7 {.  conc
6ed0: 61 74 20 5c 0a 20 20 20 20 5b 65 78 65 63 73 71  at \.    [execsq
6ee0: 6c 20 7b 20 53 45 4c 45 43 54 20 72 6f 77 69 64  l { SELECT rowid
6ef0: 20 46 52 4f 4d 20 74 72 61 63 6b 20 57 48 45 52   FROM track WHER
6f00: 45 20 74 72 61 63 6b 61 72 74 69 73 74 20 3d 20  E trackartist = 
6f10: 36 20 7d 5d 20 20 20 5c 0a 20 20 20 20 5b 63 61  6 }]   \.    [ca
6f20: 74 63 68 73 71 6c 20 7b 20 44 45 4c 45 54 45 20  tchsql { DELETE 
6f30: 46 52 4f 4d 20 61 72 74 69 73 74 20 57 48 45 52  FROM artist WHER
6f40: 45 20 61 72 74 69 73 74 69 64 20 3d 20 36 20 7d  E artistid = 6 }
6f50: 5d 0a 7d 20 7b 32 20 31 20 7b 66 6f 72 65 69 67  ].} {2 1 {foreig
6f60: 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74  n key constraint
6f70: 20 66 61 69 6c 65 64 7d 7d 0a 0a 23 2d 2d 2d 2d   failed}}..#----
6f80: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
6f90: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
6fa0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
6fb0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
6fc0: 2d 2d 2d 2d 2d 0a 23 20 2f 2a 20 45 56 3a 20 52  -----.# /* EV: R
6fd0: 2d 35 34 31 37 32 2d 35 35 38 34 38 20 2a 2f 0a  -54172-55848 */.
6fe0: 23 0a 23 20 54 65 73 74 20 74 68 61 74 20 77 68  #.# Test that wh
6ff0: 65 6e 20 61 20 72 6f 77 20 69 73 20 64 65 6c 65  en a row is dele
7000: 74 65 64 20 66 72 6f 6d 20 74 68 65 20 70 61 72  ted from the par
7010: 65 6e 74 20 74 61 62 6c 65 20 6f 66 20 61 6e 20  ent table of an 
7020: 46 4b 20 0a 23 20 63 6f 6e 73 74 72 61 69 6e 74  FK .# constraint
7030: 2c 20 74 68 65 20 63 68 69 6c 64 20 74 61 62 6c  , the child tabl
7040: 65 20 69 73 20 71 75 65 72 69 65 64 20 66 6f 72  e is queried for
7050: 20 6f 72 70 68 61 6e 65 64 20 72 6f 77 73 2e 20   orphaned rows. 
7060: 54 68 65 0a 23 20 71 75 65 72 79 20 69 73 20 65  The.# query is e
7070: 71 75 69 76 61 6c 65 6e 74 20 74 6f 3a 0a 23 0a  quivalent to:.#.
7080: 23 20 20 20 53 45 4c 45 43 54 20 72 6f 77 69 64  #   SELECT rowid
7090: 20 46 52 4f 4d 20 3c 63 68 69 6c 64 2d 74 61 62   FROM <child-tab
70a0: 6c 65 3e 20 57 48 45 52 45 20 3c 63 68 69 6c 64  le> WHERE <child
70b0: 2d 6b 65 79 3e 20 3d 20 3a 70 61 72 65 6e 74 5f  -key> = :parent_
70c0: 6b 65 79 5f 76 61 6c 75 65 0a 23 0a 23 20 2f 2a  key_value.#.# /*
70d0: 20 45 56 3a 20 52 2d 36 31 36 31 36 2d 34 36 37   EV: R-61616-467
70e0: 30 30 20 2a 2f 0a 23 0a 23 20 41 6c 73 6f 20 74  00 */.#.# Also t
70f0: 65 73 74 20 74 68 61 74 20 77 68 65 6e 20 61 20  est that when a 
7100: 72 6f 77 20 69 73 20 69 6e 73 65 72 74 65 64 20  row is inserted 
7110: 69 6e 74 6f 20 74 68 65 20 70 61 72 65 6e 74 20  into the parent 
7120: 74 61 62 6c 65 2c 20 6f 72 20 77 68 65 6e 20 74  table, or when t
7130: 68 65 20 0a 23 20 70 61 72 65 6e 74 20 6b 65 79  he .# parent key
7140: 20 76 61 6c 75 65 73 20 6f 66 20 61 6e 20 65 78   values of an ex
7150: 69 73 74 69 6e 67 20 72 6f 77 20 61 72 65 20 6d  isting row are m
7160: 6f 64 69 66 69 65 64 2c 20 61 20 71 75 65 72 79  odified, a query
7170: 20 65 71 75 69 76 61 6c 65 6e 74 0a 23 20 74 6f   equivalent.# to
7180: 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69   the following i
7190: 73 20 70 6c 61 6e 6e 65 64 2e 20 49 6e 20 73 6f  s planned. In so
71a0: 6d 65 20 63 61 73 65 73 20 69 74 20 69 73 20 6e  me cases it is n
71b0: 6f 74 20 65 78 65 63 75 74 65 64 2c 20 62 75 74  ot executed, but
71c0: 20 69 74 0a 23 20 69 73 20 61 6c 77 61 79 73 20   it.# is always 
71d0: 70 6c 61 6e 6e 65 64 2e 0a 23 0a 23 20 20 20 53  planned..#.#   S
71e0: 45 4c 45 43 54 20 72 6f 77 69 64 20 46 52 4f 4d  ELECT rowid FROM
71f0: 20 3c 63 68 69 6c 64 2d 74 61 62 6c 65 3e 20 57   <child-table> W
7200: 48 45 52 45 20 3c 63 68 69 6c 64 2d 6b 65 79 3e  HERE <child-key>
7210: 20 3d 20 3a 70 61 72 65 6e 74 5f 6b 65 79 5f 76   = :parent_key_v
7220: 61 6c 75 65 0a 23 0a 23 0a 64 72 6f 70 5f 61 6c  alue.#.#.drop_al
7230: 6c 5f 74 61 62 6c 65 73 0a 64 6f 5f 74 65 73 74  l_tables.do_test
7240: 20 65 5f 66 6b 65 79 2d 36 34 2e 31 20 7b 0a 20   e_fkey-64.1 {. 
7250: 20 65 78 65 63 73 71 6c 20 7b 20 43 52 45 41 54   execsql { CREAT
7260: 45 20 54 41 42 4c 45 20 70 61 72 65 6e 74 28 78  E TABLE parent(x
7270: 2c 20 79 2c 20 55 4e 49 51 55 45 28 79 2c 20 78  , y, UNIQUE(y, x
7280: 29 29 20 7d 0a 7d 20 7b 7d 0a 66 6f 72 65 61 63  )) }.} {}.foreac
7290: 68 20 7b 74 6e 20 73 71 6c 7d 20 7b 0a 20 20 32  h {tn sql} {.  2
72a0: 20 7b 20 0a 20 20 20 20 43 52 45 41 54 45 20 54   { .    CREATE T
72b0: 41 42 4c 45 20 63 68 69 6c 64 28 61 2c 20 62 2c  ABLE child(a, b,
72c0: 20 46 4f 52 45 49 47 4e 20 4b 45 59 28 61 2c 20   FOREIGN KEY(a, 
72d0: 62 29 20 52 45 46 45 52 45 4e 43 45 53 20 70 61  b) REFERENCES pa
72e0: 72 65 6e 74 28 78 2c 20 79 29 29 0a 20 20 7d 0a  rent(x, y)).  }.
72f0: 20 20 33 20 7b 20 0a 20 20 20 20 43 52 45 41 54    3 { .    CREAT
7300: 45 20 54 41 42 4c 45 20 63 68 69 6c 64 28 61 2c  E TABLE child(a,
7310: 20 62 2c 20 46 4f 52 45 49 47 4e 20 4b 45 59 28   b, FOREIGN KEY(
7320: 61 2c 20 62 29 20 52 45 46 45 52 45 4e 43 45 53  a, b) REFERENCES
7330: 20 70 61 72 65 6e 74 28 78 2c 20 79 29 29 3b 0a   parent(x, y));.
7340: 20 20 20 20 43 52 45 41 54 45 20 49 4e 44 45 58      CREATE INDEX
7350: 20 63 68 69 6c 64 69 20 4f 4e 20 63 68 69 6c 64   childi ON child
7360: 28 61 2c 20 62 29 3b 0a 20 20 7d 0a 20 20 34 20  (a, b);.  }.  4 
7370: 7b 20 0a 20 20 20 20 43 52 45 41 54 45 20 54 41  { .    CREATE TA
7380: 42 4c 45 20 63 68 69 6c 64 28 61 2c 20 62 2c 20  BLE child(a, b, 
7390: 46 4f 52 45 49 47 4e 20 4b 45 59 28 61 2c 20 62  FOREIGN KEY(a, b
73a0: 29 20 52 45 46 45 52 45 4e 43 45 53 20 70 61 72  ) REFERENCES par
73b0: 65 6e 74 28 78 2c 20 79 29 29 3b 0a 20 20 20 20  ent(x, y));.    
73c0: 43 52 45 41 54 45 20 55 4e 49 51 55 45 20 49 4e  CREATE UNIQUE IN
73d0: 44 45 58 20 63 68 69 6c 64 69 20 4f 4e 20 63 68  DEX childi ON ch
73e0: 69 6c 64 28 62 2c 20 61 29 3b 0a 20 20 7d 0a 7d  ild(b, a);.  }.}
73f0: 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 24 73 71   {.  execsql $sq
7400: 6c 0a 0a 20 20 65 78 65 63 73 71 6c 20 7b 50 52  l..  execsql {PR
7410: 41 47 4d 41 20 66 6f 72 65 69 67 6e 5f 6b 65 79  AGMA foreign_key
7420: 73 20 3d 20 4f 46 46 7d 0a 20 20 73 65 74 20 64  s = OFF}.  set d
7430: 65 6c 65 74 65 20 5b 63 6f 6e 63 61 74 20 5c 0a  elete [concat \.
7440: 20 20 20 20 20 20 5b 65 71 70 20 22 44 45 4c 45        [eqp "DELE
7450: 54 45 20 46 52 4f 4d 20 70 61 72 65 6e 74 20 57  TE FROM parent W
7460: 48 45 52 45 20 31 22 5d 20 5c 0a 20 20 20 20 20  HERE 1"] \.     
7470: 20 5b 65 71 70 20 22 53 45 4c 45 43 54 20 72 6f   [eqp "SELECT ro
7480: 77 69 64 20 46 52 4f 4d 20 63 68 69 6c 64 20 57  wid FROM child W
7490: 48 45 52 45 20 61 20 3d 20 3f 20 41 4e 44 20 62  HERE a = ? AND b
74a0: 20 3d 20 3f 22 5d 0a 20 20 5d 0a 20 20 73 65 74   = ?"].  ].  set
74b0: 20 75 70 64 61 74 65 20 5b 63 6f 6e 63 61 74 20   update [concat 
74c0: 5c 0a 20 20 20 20 20 20 5b 65 71 70 20 22 55 50  \.      [eqp "UP
74d0: 44 41 54 45 20 70 61 72 65 6e 74 20 53 45 54 20  DATE parent SET 
74e0: 78 3d 3f 2c 20 79 3d 3f 22 5d 20 5c 0a 20 20 20  x=?, y=?"] \.   
74f0: 20 20 20 5b 65 71 70 20 22 53 45 4c 45 43 54 20     [eqp "SELECT 
7500: 72 6f 77 69 64 20 46 52 4f 4d 20 63 68 69 6c 64  rowid FROM child
7510: 20 57 48 45 52 45 20 61 20 3d 20 3f 20 41 4e 44   WHERE a = ? AND
7520: 20 62 20 3d 20 3f 22 5d 20 5c 0a 20 20 20 20 20   b = ?"] \.     
7530: 20 5b 65 71 70 20 22 53 45 4c 45 43 54 20 72 6f   [eqp "SELECT ro
7540: 77 69 64 20 46 52 4f 4d 20 63 68 69 6c 64 20 57  wid FROM child W
7550: 48 45 52 45 20 61 20 3d 20 3f 20 41 4e 44 20 62  HERE a = ? AND b
7560: 20 3d 20 3f 22 5d 0a 20 20 5d 0a 20 20 65 78 65   = ?"].  ].  exe
7570: 63 73 71 6c 20 7b 50 52 41 47 4d 41 20 66 6f 72  csql {PRAGMA for
7580: 65 69 67 6e 5f 6b 65 79 73 20 3d 20 4f 4e 7d 0a  eign_keys = ON}.
7590: 0a 20 20 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65  .  do_test e_fke
75a0: 79 2d 36 34 2e 24 74 6e 2e 31 20 7b 20 65 71 70  y-64.$tn.1 { eqp
75b0: 20 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 70 61   "DELETE FROM pa
75c0: 72 65 6e 74 20 57 48 45 52 45 20 31 22 20 7d 20  rent WHERE 1" } 
75d0: 24 64 65 6c 65 74 65 0a 20 20 64 6f 5f 74 65 73  $delete.  do_tes
75e0: 74 20 65 5f 66 6b 65 79 2d 36 34 2e 24 74 6e 2e  t e_fkey-64.$tn.
75f0: 32 20 7b 20 65 71 70 20 22 55 50 44 41 54 45 20  2 { eqp "UPDATE 
7600: 70 61 72 65 6e 74 20 73 65 74 20 78 3d 3f 2c 20  parent set x=?, 
7610: 79 3d 3f 22 20 7d 20 24 75 70 64 61 74 65 0a 0a  y=?" } $update..
7620: 20 20 65 78 65 63 73 71 6c 20 7b 44 52 4f 50 20    execsql {DROP 
7630: 54 41 42 4c 45 20 63 68 69 6c 64 7d 0a 7d 0a 0a  TABLE child}.}..
7640: 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  #---------------
7650: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
7660: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
7670: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
7680: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20 2f 2a 20  ----------.# /* 
7690: 45 56 3a 20 52 2d 31 34 35 35 33 2d 33 34 30 31  EV: R-14553-3401
76a0: 33 20 2a 2f 0a 23 0a 23 20 54 65 73 74 20 74 68  3 */.#.# Test th
76b0: 65 20 65 78 61 6d 70 6c 65 20 73 63 68 65 6d 61  e example schema
76c0: 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 73   at the end of s
76d0: 65 63 74 69 6f 6e 20 33 2e 20 41 6c 73 6f 20 74  ection 3. Also t
76e0: 65 73 74 20 74 68 61 74 20 69 73 0a 23 20 69 73  est that is.# is
76f0: 20 22 65 66 66 69 63 69 65 6e 74 22 2e 20 49 6e   "efficient". In
7700: 20 74 68 69 73 20 63 61 73 65 20 22 65 66 66 69   this case "effi
7710: 63 69 65 6e 74 22 20 6d 65 61 6e 73 20 74 68 61  cient" means tha
7720: 74 20 66 6f 72 65 69 67 6e 20 6b 65 79 0a 23 20  t foreign key.# 
7730: 72 65 6c 61 74 65 64 20 6f 70 65 72 61 74 69 6f  related operatio
7740: 6e 73 20 6f 6e 20 74 68 65 20 70 61 72 65 6e 74  ns on the parent
7750: 20 74 61 62 6c 65 20 64 6f 20 6e 6f 74 20 70 72   table do not pr
7760: 6f 76 6f 6b 65 20 6c 69 6e 65 61 72 20 73 63 61  ovoke linear sca
7770: 6e 73 2e 0a 23 0a 64 72 6f 70 5f 61 6c 6c 5f 74  ns..#.drop_all_t
7780: 61 62 6c 65 73 0a 64 6f 5f 74 65 73 74 20 65 5f  ables.do_test e_
7790: 66 6b 65 79 2d 36 33 2e 31 20 7b 0a 20 20 65 78  fkey-63.1 {.  ex
77a0: 65 63 73 71 6c 20 7b 0a 20 20 20 20 43 52 45 41  ecsql {.    CREA
77b0: 54 45 20 54 41 42 4c 45 20 61 72 74 69 73 74 28  TE TABLE artist(
77c0: 0a 20 20 20 20 20 20 61 72 74 69 73 74 69 64 20  .      artistid 
77d0: 20 20 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41     INTEGER PRIMA
77e0: 52 59 20 4b 45 59 2c 20 0a 20 20 20 20 20 20 61  RY KEY, .      a
77f0: 72 74 69 73 74 6e 61 6d 65 20 20 54 45 58 54 0a  rtistname  TEXT.
7800: 20 20 20 20 29 3b 0a 20 20 20 20 43 52 45 41 54      );.    CREAT
7810: 45 20 54 41 42 4c 45 20 74 72 61 63 6b 28 0a 20  E TABLE track(. 
7820: 20 20 20 20 20 74 72 61 63 6b 69 64 20 20 20 20       trackid    
7830: 20 49 4e 54 45 47 45 52 2c 0a 20 20 20 20 20 20   INTEGER,.      
7840: 74 72 61 63 6b 6e 61 6d 65 20 20 20 54 45 58 54  trackname   TEXT
7850: 2c 20 0a 20 20 20 20 20 20 74 72 61 63 6b 61 72  , .      trackar
7860: 74 69 73 74 20 49 4e 54 45 47 45 52 20 52 45 46  tist INTEGER REF
7870: 45 52 45 4e 43 45 53 20 61 72 74 69 73 74 0a 20  ERENCES artist. 
7880: 20 20 20 29 3b 0a 20 20 20 20 43 52 45 41 54 45     );.    CREATE
7890: 20 49 4e 44 45 58 20 74 72 61 63 6b 69 6e 64 65   INDEX trackinde
78a0: 78 20 4f 4e 20 74 72 61 63 6b 28 74 72 61 63 6b  x ON track(track
78b0: 61 72 74 69 73 74 29 3b 0a 20 20 7d 0a 7d 20 7b  artist);.  }.} {
78c0: 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79  }.do_test e_fkey
78d0: 2d 36 33 2e 32 20 7b 0a 20 20 65 71 70 20 7b 20  -63.2 {.  eqp { 
78e0: 49 4e 53 45 52 54 20 49 4e 54 4f 20 61 72 74 69  INSERT INTO arti
78f0: 73 74 20 56 41 4c 55 45 53 28 3f 2c 20 3f 29 20  st VALUES(?, ?) 
7900: 7d 0a 7d 20 7b 7d 0a 64 6f 5f 74 65 73 74 20 65  }.} {}.do_test e
7910: 5f 66 6b 65 79 2d 36 33 2e 33 20 7b 0a 20 20 65  _fkey-63.3 {.  e
7920: 71 70 20 7b 20 55 50 44 41 54 45 20 61 72 74 69  qp { UPDATE arti
7930: 73 74 20 53 45 54 20 61 72 74 69 73 74 69 64 20  st SET artistid 
7940: 3d 20 3f 2c 20 61 72 74 69 73 74 6e 61 6d 65 20  = ?, artistname 
7950: 3d 20 3f 20 7d 0a 7d 20 5b 6c 69 73 74 20 5c 0a  = ? }.} [list \.
7960: 20 20 30 20 30 20 7b 54 41 42 4c 45 20 61 72 74    0 0 {TABLE art
7970: 69 73 74 7d 20 5c 0a 20 20 30 20 30 20 7b 54 41  ist} \.  0 0 {TA
7980: 42 4c 45 20 74 72 61 63 6b 20 57 49 54 48 20 49  BLE track WITH I
7990: 4e 44 45 58 20 74 72 61 63 6b 69 6e 64 65 78 7d  NDEX trackindex}
79a0: 20 5c 0a 20 20 30 20 30 20 7b 54 41 42 4c 45 20   \.  0 0 {TABLE 
79b0: 74 72 61 63 6b 20 57 49 54 48 20 49 4e 44 45 58  track WITH INDEX
79c0: 20 74 72 61 63 6b 69 6e 64 65 78 7d 0a 5d 0a 64   trackindex}.].d
79d0: 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 36 33  o_test e_fkey-63
79e0: 2e 34 20 7b 0a 20 20 65 71 70 20 7b 20 44 45 4c  .4 {.  eqp { DEL
79f0: 45 54 45 20 46 52 4f 4d 20 61 72 74 69 73 74 20  ETE FROM artist 
7a00: 7d 0a 7d 20 5b 6c 69 73 74 20 5c 0a 20 20 30 20  }.} [list \.  0 
7a10: 30 20 7b 54 41 42 4c 45 20 61 72 74 69 73 74 7d  0 {TABLE artist}
7a20: 20 5c 0a 20 20 30 20 30 20 7b 54 41 42 4c 45 20   \.  0 0 {TABLE 
7a30: 74 72 61 63 6b 20 57 49 54 48 20 49 4e 44 45 58  track WITH INDEX
7a40: 20 74 72 61 63 6b 69 6e 64 65 78 7d 0a 5d 0a 0a   trackindex}.]..
7a50: 0a 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  .###############
7a60: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
7a70: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
7a80: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
7a90: 23 23 23 23 23 23 23 23 23 23 23 23 0a 23 23 23  ############.###
7aa0: 20 53 45 43 54 49 4f 4e 20 34 2e 31 3a 20 43 6f   SECTION 4.1: Co
7ab0: 6d 70 6f 73 69 74 65 20 46 6f 72 65 69 67 6e 20  mposite Foreign 
7ac0: 4b 65 79 20 43 6f 6e 73 74 72 61 69 6e 74 73 0a  Key Constraints.
7ad0: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
7ae0: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
7af0: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
7b00: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
7b10: 23 23 23 23 23 23 23 23 23 23 23 0a 0a 23 2d 2d  ###########..#--
7b20: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
7b30: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
7b40: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
7b50: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
7b60: 2d 2d 2d 2d 2d 2d 2d 0a 23 20 2f 2a 20 45 56 3a  -------.# /* EV:
7b70: 20 52 2d 34 31 30 36 32 2d 33 34 34 33 31 20 2a   R-41062-34431 *
7b80: 2f 0a 23 0a 23 20 43 68 65 63 6b 20 74 68 61 74  /.#.# Check that
7b90: 20 70 61 72 65 6e 74 20 61 6e 64 20 63 68 69 6c   parent and chil
7ba0: 64 20 6b 65 79 73 20 6d 75 73 74 20 68 61 76 65  d keys must have
7bb0: 20 74 68 65 20 73 61 6d 65 20 6e 75 6d 62 65 72   the same number
7bc0: 20 6f 66 20 63 6f 6c 75 6d 6e 73 2e 0a 23 0a 66   of columns..#.f
7bd0: 6f 72 65 61 63 68 20 7b 74 6e 20 73 71 6c 20 65  oreach {tn sql e
7be0: 72 72 7d 20 7b 0a 20 20 31 20 22 43 52 45 41 54  rr} {.  1 "CREAT
7bf0: 45 20 54 41 42 4c 45 20 63 28 6a 6a 20 52 45 46  E TABLE c(jj REF
7c00: 45 52 45 4e 43 45 53 20 70 28 78 2c 20 79 29 29  ERENCES p(x, y))
7c10: 22 20 0a 20 20 20 20 7b 66 6f 72 65 69 67 6e 20  " .    {foreign 
7c20: 6b 65 79 20 6f 6e 20 6a 6a 20 73 68 6f 75 6c 64  key on jj should
7c30: 20 72 65 66 65 72 65 6e 63 65 20 6f 6e 6c 79 20   reference only 
7c40: 6f 6e 65 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 61  one column of ta
7c50: 62 6c 65 20 70 7d 0a 0a 20 20 32 20 22 43 52 45  ble p}..  2 "CRE
7c60: 41 54 45 20 54 41 42 4c 45 20 63 28 6a 6a 20 52  ATE TABLE c(jj R
7c70: 45 46 45 52 45 4e 43 45 53 20 70 28 29 29 22 20  EFERENCES p())" 
7c80: 7b 6e 65 61 72 20 22 29 22 3a 20 73 79 6e 74 61  {near ")": synta
7c90: 78 20 65 72 72 6f 72 7d 0a 0a 20 20 33 20 22 43  x error}..  3 "C
7ca0: 52 45 41 54 45 20 54 41 42 4c 45 20 63 28 6a 6a  REATE TABLE c(jj
7cb0: 2c 20 46 4f 52 45 49 47 4e 20 4b 45 59 28 6a 6a  , FOREIGN KEY(jj
7cc0: 29 20 52 45 46 45 52 45 4e 43 45 53 20 70 28 78  ) REFERENCES p(x
7cd0: 2c 20 79 29 29 22 20 0a 20 20 20 20 7b 6e 75 6d  , y))" .    {num
7ce0: 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69  ber of columns i
7cf0: 6e 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 64 6f  n foreign key do
7d00: 65 73 20 6e 6f 74 20 6d 61 74 63 68 20 74 68 65  es not match the
7d10: 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d   number of colum
7d20: 6e 73 20 69 6e 20 74 68 65 20 72 65 66 65 72 65  ns in the refere
7d30: 6e 63 65 64 20 74 61 62 6c 65 7d 0a 0a 20 20 34  nced table}..  4
7d40: 20 22 43 52 45 41 54 45 20 54 41 42 4c 45 20 63   "CREATE TABLE c
7d50: 28 6a 6a 2c 20 46 4f 52 45 49 47 4e 20 4b 45 59  (jj, FOREIGN KEY
7d60: 28 6a 6a 29 20 52 45 46 45 52 45 4e 43 45 53 20  (jj) REFERENCES 
7d70: 70 28 29 29 22 20 0a 20 20 20 20 7b 6e 65 61 72  p())" .    {near
7d80: 20 22 29 22 3a 20 73 79 6e 74 61 78 20 65 72 72   ")": syntax err
7d90: 6f 72 7d 0a 0a 20 20 35 20 22 43 52 45 41 54 45  or}..  5 "CREATE
7da0: 20 54 41 42 4c 45 20 63 28 69 69 2c 20 6a 6a 2c   TABLE c(ii, jj,
7db0: 20 46 4f 52 45 49 47 4e 20 4b 45 59 28 6a 6a 2c   FOREIGN KEY(jj,
7dc0: 20 69 69 29 20 52 45 46 45 52 45 4e 43 45 53 20   ii) REFERENCES 
7dd0: 70 28 29 29 22 20 0a 20 20 20 20 7b 6e 65 61 72  p())" .    {near
7de0: 20 22 29 22 3a 20 73 79 6e 74 61 78 20 65 72 72   ")": syntax err
7df0: 6f 72 7d 0a 0a 20 20 36 20 22 43 52 45 41 54 45  or}..  6 "CREATE
7e00: 20 54 41 42 4c 45 20 63 28 69 69 2c 20 6a 6a 2c   TABLE c(ii, jj,
7e10: 20 46 4f 52 45 49 47 4e 20 4b 45 59 28 6a 6a 2c   FOREIGN KEY(jj,
7e20: 20 69 69 29 20 52 45 46 45 52 45 4e 43 45 53 20   ii) REFERENCES 
7e30: 70 28 78 29 29 22 20 0a 20 20 20 20 7b 6e 75 6d  p(x))" .    {num
7e40: 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69  ber of columns i
7e50: 6e 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 64 6f  n foreign key do
7e60: 65 73 20 6e 6f 74 20 6d 61 74 63 68 20 74 68 65  es not match the
7e70: 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d   number of colum
7e80: 6e 73 20 69 6e 20 74 68 65 20 72 65 66 65 72 65  ns in the refere
7e90: 6e 63 65 64 20 74 61 62 6c 65 7d 0a 0a 20 20 37  nced table}..  7
7ea0: 20 22 43 52 45 41 54 45 20 54 41 42 4c 45 20 63   "CREATE TABLE c
7eb0: 28 69 69 2c 20 6a 6a 2c 20 46 4f 52 45 49 47 4e  (ii, jj, FOREIGN
7ec0: 20 4b 45 59 28 6a 6a 2c 20 69 69 29 20 52 45 46   KEY(jj, ii) REF
7ed0: 45 52 45 4e 43 45 53 20 70 28 78 2c 79 2c 7a 29  ERENCES p(x,y,z)
7ee0: 29 22 20 0a 20 20 20 20 7b 6e 75 6d 62 65 72 20  )" .    {number 
7ef0: 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 66 6f  of columns in fo
7f00: 72 65 69 67 6e 20 6b 65 79 20 64 6f 65 73 20 6e  reign key does n
7f10: 6f 74 20 6d 61 74 63 68 20 74 68 65 20 6e 75 6d  ot match the num
7f20: 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69  ber of columns i
7f30: 6e 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 64  n the referenced
7f40: 20 74 61 62 6c 65 7d 0a 7d 20 7b 0a 20 20 64 72   table}.} {.  dr
7f50: 6f 70 5f 61 6c 6c 5f 74 61 62 6c 65 73 0a 20 20  op_all_tables.  
7f60: 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 36  do_test e_fkey-6
7f70: 35 2e 24 74 6e 20 5b 6c 69 73 74 20 63 61 74 63  5.$tn [list catc
7f80: 68 73 71 6c 20 24 73 71 6c 5d 20 5b 6c 69 73 74  hsql $sql] [list
7f90: 20 31 20 24 65 72 72 5d 0a 7d 0a 64 6f 5f 74 65   1 $err].}.do_te
7fa0: 73 74 20 65 5f 66 6b 65 79 2d 36 35 2e 38 20 7b  st e_fkey-65.8 {
7fb0: 0a 20 20 64 72 6f 70 5f 61 6c 6c 5f 74 61 62 6c  .  drop_all_tabl
7fc0: 65 73 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20  es.  execsql {. 
7fd0: 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20     CREATE TABLE 
7fe0: 70 28 78 20 50 52 49 4d 41 52 59 20 4b 45 59 29  p(x PRIMARY KEY)
7ff0: 3b 0a 20 20 20 20 43 52 45 41 54 45 20 54 41 42  ;.    CREATE TAB
8000: 4c 45 20 63 28 61 2c 20 62 2c 20 46 4f 52 45 49  LE c(a, b, FOREI
8010: 47 4e 20 4b 45 59 28 61 2c 62 29 20 52 45 46 45  GN KEY(a,b) REFE
8020: 52 45 4e 43 45 53 20 70 29 3b 0a 20 20 7d 0a 20  RENCES p);.  }. 
8030: 20 63 61 74 63 68 73 71 6c 20 7b 44 45 4c 45 54   catchsql {DELET
8040: 45 20 46 52 4f 4d 20 70 7d 0a 7d 20 7b 31 20 7b  E FROM p}.} {1 {
8050: 66 6f 72 65 69 67 6e 20 6b 65 79 20 6d 69 73 6d  foreign key mism
8060: 61 74 63 68 7d 7d 0a 64 6f 5f 74 65 73 74 20 65  atch}}.do_test e
8070: 5f 66 6b 65 79 2d 36 35 2e 39 20 7b 0a 20 20 64  _fkey-65.9 {.  d
8080: 72 6f 70 5f 61 6c 6c 5f 74 61 62 6c 65 73 0a 20  rop_all_tables. 
8090: 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 43   execsql {.    C
80a0: 52 45 41 54 45 20 54 41 42 4c 45 20 70 28 78 2c  REATE TABLE p(x,
80b0: 20 79 2c 20 50 52 49 4d 41 52 59 20 4b 45 59 28   y, PRIMARY KEY(
80c0: 78 2c 79 29 29 3b 0a 20 20 20 20 43 52 45 41 54  x,y));.    CREAT
80d0: 45 20 54 41 42 4c 45 20 63 28 61 20 52 45 46 45  E TABLE c(a REFE
80e0: 52 45 4e 43 45 53 20 70 29 3b 0a 20 20 7d 0a 20  RENCES p);.  }. 
80f0: 20 63 61 74 63 68 73 71 6c 20 7b 44 45 4c 45 54   catchsql {DELET
8100: 45 20 46 52 4f 4d 20 70 7d 0a 7d 20 7b 31 20 7b  E FROM p}.} {1 {
8110: 66 6f 72 65 69 67 6e 20 6b 65 79 20 6d 69 73 6d  foreign key mism
8120: 61 74 63 68 7d 7d 0a 0a 0a 23 2d 2d 2d 2d 2d 2d  atch}}...#------
8130: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
8140: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
8150: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
8160: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
8170: 2d 2d 2d 0a 23 20 2f 2a 20 45 56 3a 20 52 2d 32  ---.# /* EV: R-2
8180: 34 36 37 36 2d 30 39 38 35 39 20 2a 2f 0a 23 0a  4676-09859 */.#.
8190: 23 20 54 65 73 74 20 74 68 65 20 65 78 61 6d 70  # Test the examp
81a0: 6c 65 20 73 63 68 65 6d 61 20 69 6e 20 74 68 65  le schema in the
81b0: 20 22 43 6f 6d 70 6f 73 69 74 65 20 46 6f 72 65   "Composite Fore
81c0: 69 67 6e 20 4b 65 79 20 43 6f 6e 73 74 72 61 69  ign Key Constrai
81d0: 6e 74 73 22 20 0a 23 20 73 65 63 74 69 6f 6e 2e  nts" .# section.
81e0: 0a 23 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65  .#.do_test e_fke
81f0: 79 2d 33 36 2e 31 20 7b 0a 20 20 65 78 65 63 73  y-36.1 {.  execs
8200: 71 6c 20 7b 0a 20 20 20 20 43 52 45 41 54 45 20  ql {.    CREATE 
8210: 54 41 42 4c 45 20 61 6c 62 75 6d 28 0a 20 20 20  TABLE album(.   
8220: 20 20 20 61 6c 62 75 6d 61 72 74 69 73 74 20 54     albumartist T
8230: 45 58 54 2c 0a 20 20 20 20 20 20 61 6c 62 75 6d  EXT,.      album
8240: 6e 61 6d 65 20 54 45 58 54 2c 0a 20 20 20 20 20  name TEXT,.     
8250: 20 61 6c 62 75 6d 63 6f 76 65 72 20 42 49 4e 41   albumcover BINA
8260: 52 59 2c 0a 20 20 20 20 20 20 50 52 49 4d 41 52  RY,.      PRIMAR
8270: 59 20 4b 45 59 28 61 6c 62 75 6d 61 72 74 69 73  Y KEY(albumartis
8280: 74 2c 20 61 6c 62 75 6d 6e 61 6d 65 29 0a 20 20  t, albumname).  
8290: 20 20 29 3b 0a 20 20 20 20 43 52 45 41 54 45 20    );.    CREATE 
82a0: 54 41 42 4c 45 20 73 6f 6e 67 28 0a 20 20 20 20  TABLE song(.    
82b0: 20 20 73 6f 6e 67 69 64 20 49 4e 54 45 47 45 52    songid INTEGER
82c0: 2c 0a 20 20 20 20 20 20 73 6f 6e 67 61 72 74 69  ,.      songarti
82d0: 73 74 20 54 45 58 54 2c 0a 20 20 20 20 20 20 73  st TEXT,.      s
82e0: 6f 6e 67 61 6c 62 75 6d 20 54 45 58 54 2c 0a 20  ongalbum TEXT,. 
82f0: 20 20 20 20 20 73 6f 6e 67 6e 61 6d 65 20 54 45       songname TE
8300: 58 54 2c 0a 20 20 20 20 20 20 46 4f 52 45 49 47  XT,.      FOREIG
8310: 4e 20 4b 45 59 28 73 6f 6e 67 61 72 74 69 73 74  N KEY(songartist
8320: 2c 20 73 6f 6e 67 61 6c 62 75 6d 29 20 52 45 46  , songalbum) REF
8330: 45 52 45 4e 43 45 53 20 61 6c 62 75 6d 28 61 6c  ERENCES album(al
8340: 62 75 6d 61 72 74 69 73 74 2c 61 6c 62 75 6d 6e  bumartist,albumn
8350: 61 6d 65 29 0a 20 20 20 20 29 3b 0a 20 20 7d 0a  ame).    );.  }.
8360: 7d 20 7b 7d 0a 0a 64 6f 5f 74 65 73 74 20 65 5f  } {}..do_test e_
8370: 66 6b 65 79 2d 33 36 2e 32 20 7b 0a 20 20 65 78  fkey-36.2 {.  ex
8380: 65 63 73 71 6c 20 7b 0a 20 20 20 20 49 4e 53 45  ecsql {.    INSE
8390: 52 54 20 49 4e 54 4f 20 61 6c 62 75 6d 20 56 41  RT INTO album VA
83a0: 4c 55 45 53 28 27 45 6c 76 69 73 20 50 72 65 73  LUES('Elvis Pres
83b0: 6c 65 79 27 2c 20 27 45 6c 76 69 73 27 27 20 43  ley', 'Elvis'' C
83c0: 68 72 69 73 74 6d 61 73 20 41 6c 62 75 6d 27 2c  hristmas Album',
83d0: 20 4e 55 4c 4c 29 3b 0a 20 20 20 20 49 4e 53 45   NULL);.    INSE
83e0: 52 54 20 49 4e 54 4f 20 73 6f 6e 67 20 56 41 4c  RT INTO song VAL
83f0: 55 45 53 28 0a 20 20 20 20 20 20 31 2c 20 27 45  UES(.      1, 'E
8400: 6c 76 69 73 20 50 72 65 73 6c 65 79 27 2c 20 27  lvis Presley', '
8410: 45 6c 76 69 73 27 27 20 43 68 72 69 73 74 6d 61  Elvis'' Christma
8420: 73 20 41 6c 62 75 6d 27 2c 20 27 48 65 72 65 20  s Album', 'Here 
8430: 43 6f 6d 65 73 20 53 61 6e 74 61 20 43 6c 61 75  Comes Santa Clau
8440: 73 65 27 0a 20 20 20 20 29 3b 0a 20 20 7d 0a 7d  se'.    );.  }.}
8450: 20 7b 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b   {}.do_test e_fk
8460: 65 79 2d 33 36 2e 33 20 7b 0a 20 20 63 61 74 63  ey-36.3 {.  catc
8470: 68 73 71 6c 20 7b 0a 20 20 20 20 49 4e 53 45 52  hsql {.    INSER
8480: 54 20 49 4e 54 4f 20 73 6f 6e 67 20 56 41 4c 55  T INTO song VALU
8490: 45 53 28 32 2c 20 27 45 6c 76 69 73 20 50 72 65  ES(2, 'Elvis Pre
84a0: 73 6c 65 79 27 2c 20 27 45 6c 76 69 73 20 49 73  sley', 'Elvis Is
84b0: 20 42 61 63 6b 21 27 2c 20 27 46 65 76 65 72 27   Back!', 'Fever'
84c0: 29 3b 0a 20 20 7d 0a 7d 20 7b 31 20 7b 66 6f 72  );.  }.} {1 {for
84d0: 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61  eign key constra
84e0: 69 6e 74 20 66 61 69 6c 65 64 7d 7d 0a 0a 0a 23  int failed}}...#
84f0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
8500: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
8510: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
8520: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
8530: 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20 2f 2a 20 45  ---------.# /* E
8540: 56 3a 20 52 2d 33 33 36 32 36 2d 34 38 34 31 38  V: R-33626-48418
8550: 20 2a 2f 0a 23 0a 23 20 43 68 65 63 6b 20 74 68   */.#.# Check th
8560: 61 74 20 69 66 20 61 6e 79 20 6f 66 20 74 68 65  at if any of the
8570: 20 63 68 69 6c 64 20 6b 65 79 20 63 6f 6c 75 6d   child key colum
8580: 6e 73 20 69 6e 20 74 68 65 20 61 62 6f 76 65 20  ns in the above 
8590: 73 63 68 65 6d 61 20 61 72 65 20 4e 55 4c 4c 2c  schema are NULL,
85a0: 0a 23 20 74 68 65 72 65 20 69 73 20 6e 6f 20 72  .# there is no r
85b0: 65 71 75 69 72 65 6d 65 6e 74 20 66 6f 72 20 61  equirement for a
85c0: 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 70   corresponding p
85d0: 61 72 65 6e 74 20 6b 65 79 2e 0a 23 0a 64 6f 5f  arent key..#.do_
85e0: 74 65 73 74 20 65 5f 66 6b 65 79 2d 33 37 2e 31  test e_fkey-37.1
85f0: 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20   {.  execsql {. 
8600: 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 73     INSERT INTO s
8610: 6f 6e 67 20 56 41 4c 55 45 53 28 32 2c 20 27 45  ong VALUES(2, 'E
8620: 6c 76 69 73 20 50 72 65 73 6c 65 79 27 2c 20 4e  lvis Presley', N
8630: 55 4c 4c 2c 20 27 46 65 76 65 72 27 29 3b 0a 20  ULL, 'Fever');. 
8640: 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 73     INSERT INTO s
8650: 6f 6e 67 20 56 41 4c 55 45 53 28 33 2c 20 4e 55  ong VALUES(3, NU
8660: 4c 4c 2c 20 27 45 6c 76 69 73 20 49 73 20 42 61  LL, 'Elvis Is Ba
8670: 63 6b 27 2c 20 27 53 6f 6c 64 69 65 72 20 42 6f  ck', 'Soldier Bo
8680: 79 27 29 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a 0a 23  y');.  }.} {}..#
8690: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
86a0: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
86b0: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
86c0: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
86d0: 23 23 23 23 23 23 23 23 23 23 0a 23 23 23 20 53  ##########.### S
86e0: 45 43 54 49 4f 4e 20 34 2e 32 3a 20 44 65 66 65  ECTION 4.2: Defe
86f0: 72 72 65 64 20 46 6f 72 65 69 67 6e 20 4b 65 79  rred Foreign Key
8700: 20 43 6f 6e 73 74 72 61 69 6e 74 73 0a 23 23 23   Constraints.###
8710: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
8720: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
8730: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
8740: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
8750: 23 23 23 23 23 23 23 23 0a 0a 23 2d 2d 2d 2d 2d  ########..#-----
8760: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
8770: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
8780: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
8790: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
87a0: 2d 2d 2d 2d 0a 23 20 4e 6f 74 65 3a 20 52 2d 33  ----.# Note: R-3
87b0: 35 32 39 30 2d 31 36 34 36 30 20 69 73 20 74 65  5290-16460 is te
87c0: 73 74 65 64 20 62 65 6c 6f 77 2e 0a 23 0a 23 20  sted below..#.# 
87d0: 54 4f 44 4f 3a 20 52 2d 33 30 33 32 33 2d 32 31  TODO: R-30323-21
87e0: 39 31 37 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  917..#----------
87f0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
8800: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
8810: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
8820: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a  ---------------.
8830: 23 20 2f 2a 20 45 56 3a 20 52 2d 30 39 33 32 33  # /* EV: R-09323
8840: 2d 33 30 34 37 30 20 2a 2f 0a 23 0a 23 20 54 65  -30470 */.#.# Te
8850: 73 74 20 74 68 61 74 20 69 66 20 61 20 73 74 61  st that if a sta
8860: 74 65 6d 65 6e 74 20 76 69 6f 6c 61 74 65 73 20  tement violates 
8870: 61 6e 20 69 6d 6d 65 64 69 61 74 65 20 46 4b 20  an immediate FK 
8880: 63 6f 6e 73 74 72 61 69 6e 74 2c 20 61 6e 64 20  constraint, and 
8890: 74 68 65 0a 23 20 64 61 74 61 62 61 73 65 20 64  the.# database d
88a0: 6f 65 73 20 6e 6f 74 20 73 61 74 69 73 66 79 20  oes not satisfy 
88b0: 74 68 65 20 46 4b 20 63 6f 6e 73 74 72 61 69 6e  the FK constrain
88c0: 74 20 6f 6e 63 65 20 61 6c 6c 20 65 66 66 65 63  t once all effec
88d0: 74 73 20 6f 66 20 74 68 65 0a 23 20 73 74 61 74  ts of the.# stat
88e0: 65 6d 65 6e 74 20 68 61 76 65 20 62 65 65 6e 20  ement have been 
88f0: 61 70 70 6c 69 65 64 2c 20 61 6e 20 65 72 72 6f  applied, an erro
8900: 72 20 69 73 20 72 65 70 6f 72 74 65 64 20 61 6e  r is reported an
8910: 64 20 74 68 65 20 65 66 66 65 63 74 73 20 6f 66  d the effects of
8920: 0a 23 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74  .# the statement
8930: 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a 23 0a   rolled back..#.
8940: 64 72 6f 70 5f 61 6c 6c 5f 74 61 62 6c 65 73 0a  drop_all_tables.
8950: 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 33  do_test e_fkey-3
8960: 33 2e 31 20 7b 0a 20 20 65 78 65 63 73 71 6c 20  3.1 {.  execsql 
8970: 7b 0a 20 20 20 20 43 52 45 41 54 45 20 54 41 42  {.    CREATE TAB
8980: 4c 45 20 6b 69 6e 67 28 61 2c 20 62 2c 20 50 52  LE king(a, b, PR
8990: 49 4d 41 52 59 20 4b 45 59 28 61 29 29 3b 0a 20  IMARY KEY(a));. 
89a0: 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20     CREATE TABLE 
89b0: 70 72 69 6e 63 65 28 63 20 52 45 46 45 52 45 4e  prince(c REFEREN
89c0: 43 45 53 20 6b 69 6e 67 2c 20 64 29 3b 0a 20 20  CES king, d);.  
89d0: 7d 0a 7d 20 7b 7d 0a 0a 64 6f 5f 74 65 73 74 20  }.} {}..do_test 
89e0: 65 5f 66 6b 65 79 2d 33 33 2e 32 20 7b 0a 20 20  e_fkey-33.2 {.  
89f0: 23 20 45 78 65 63 75 74 65 20 61 20 73 74 61 74  # Execute a stat
8a00: 65 6d 65 6e 74 20 74 68 61 74 20 76 69 6f 6c 61  ement that viola
8a10: 74 65 73 20 74 68 65 20 69 6d 6d 65 64 69 61 74  tes the immediat
8a20: 65 20 46 4b 20 63 6f 6e 73 74 72 61 69 6e 74 2e  e FK constraint.
8a30: 0a 20 20 63 61 74 63 68 73 71 6c 20 7b 20 49 4e  .  catchsql { IN
8a40: 53 45 52 54 20 49 4e 54 4f 20 70 72 69 6e 63 65  SERT INTO prince
8a50: 20 56 41 4c 55 45 53 28 31 2c 20 32 29 20 7d 0a   VALUES(1, 2) }.
8a60: 7d 20 7b 31 20 7b 66 6f 72 65 69 67 6e 20 6b 65  } {1 {foreign ke
8a70: 79 20 63 6f 6e 73 74 72 61 69 6e 74 20 66 61 69  y constraint fai
8a80: 6c 65 64 7d 7d 0a 0a 64 6f 5f 74 65 73 74 20 65  led}}..do_test e
8a90: 5f 66 6b 65 79 2d 33 33 2e 33 20 7b 0a 20 20 23  _fkey-33.3 {.  #
8aa0: 20 54 68 69 73 20 74 69 6d 65 2c 20 75 73 65 20   This time, use 
8ab0: 61 20 74 72 69 67 67 65 72 20 74 6f 20 66 69 78  a trigger to fix
8ac0: 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e 74 20   the constraint 
8ad0: 76 69 6f 6c 61 74 69 6f 6e 20 62 65 66 6f 72 65  violation before
8ae0: 20 74 68 65 0a 20 20 23 20 73 74 61 74 65 6d 65   the.  # stateme
8af0: 6e 74 20 68 61 73 20 66 69 6e 69 73 68 65 64 20  nt has finished 
8b00: 65 78 65 63 75 74 69 6e 67 2e 20 54 68 65 6e 20  executing. Then 
8b10: 65 78 65 63 75 74 65 20 74 68 65 20 73 61 6d 65  execute the same
8b20: 20 73 74 61 74 65 6d 65 6e 74 20 61 73 0a 20 20   statement as.  
8b30: 23 20 69 6e 20 74 68 65 20 70 72 65 76 69 6f 75  # in the previou
8b40: 73 20 74 65 73 74 20 63 61 73 65 2e 20 54 68 69  s test case. Thi
8b50: 73 20 74 69 6d 65 2c 20 6e 6f 20 65 72 72 6f 72  s time, no error
8b60: 2e 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20  ..  execsql {.  
8b70: 20 20 43 52 45 41 54 45 20 54 52 49 47 47 45 52    CREATE TRIGGER
8b80: 20 6b 74 20 41 46 54 45 52 20 49 4e 53 45 52 54   kt AFTER INSERT
8b90: 20 4f 4e 20 70 72 69 6e 63 65 20 57 48 45 4e 0a   ON prince WHEN.
8ba0: 20 20 20 20 20 20 4e 4f 54 20 45 58 49 53 54 53        NOT EXISTS
8bb0: 20 28 53 45 4c 45 43 54 20 61 20 46 52 4f 4d 20   (SELECT a FROM 
8bc0: 6b 69 6e 67 20 57 48 45 52 45 20 61 20 3d 20 6e  king WHERE a = n
8bd0: 65 77 2e 63 29 0a 20 20 20 20 42 45 47 49 4e 0a  ew.c).    BEGIN.
8be0: 20 20 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54        INSERT INT
8bf0: 4f 20 6b 69 6e 67 20 56 41 4c 55 45 53 28 6e 65  O king VALUES(ne
8c00: 77 2e 63 2c 20 4e 55 4c 4c 29 3b 0a 20 20 20 20  w.c, NULL);.    
8c10: 45 4e 44 0a 20 20 7d 0a 20 20 65 78 65 63 73 71  END.  }.  execsq
8c20: 6c 20 7b 20 49 4e 53 45 52 54 20 49 4e 54 4f 20  l { INSERT INTO 
8c30: 70 72 69 6e 63 65 20 56 41 4c 55 45 53 28 31 2c  prince VALUES(1,
8c40: 20 32 29 20 7d 0a 7d 20 7b 7d 0a 0a 23 20 54 65   2) }.} {}..# Te
8c50: 73 74 20 74 68 61 74 20 6f 70 65 72 61 74 69 6e  st that operatin
8c60: 67 20 69 6e 73 69 64 65 20 61 20 74 72 61 6e 73  g inside a trans
8c70: 61 63 74 69 6f 6e 20 6d 61 6b 65 73 20 6e 6f 20  action makes no 
8c80: 64 69 66 66 65 72 65 6e 63 65 20 74 6f 20 0a 23  difference to .#
8c90: 20 69 6d 6d 65 64 69 61 74 65 20 63 6f 6e 73 74   immediate const
8ca0: 72 61 69 6e 74 20 76 69 6f 6c 61 74 69 6f 6e 20  raint violation 
8cb0: 68 61 6e 64 6c 69 6e 67 2e 0a 64 6f 5f 74 65 73  handling..do_tes
8cc0: 74 20 65 5f 66 6b 65 79 2d 33 33 2e 34 20 7b 0a  t e_fkey-33.4 {.
8cd0: 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20    execsql {.    
8ce0: 42 45 47 49 4e 3b 0a 20 20 20 20 49 4e 53 45 52  BEGIN;.    INSER
8cf0: 54 20 49 4e 54 4f 20 70 72 69 6e 63 65 20 56 41  T INTO prince VA
8d00: 4c 55 45 53 28 32 2c 20 33 29 3b 0a 20 20 20 20  LUES(2, 3);.    
8d10: 44 52 4f 50 20 54 52 49 47 47 45 52 20 6b 74 3b  DROP TRIGGER kt;
8d20: 0a 20 20 7d 0a 20 20 63 61 74 63 68 73 71 6c 20  .  }.  catchsql 
8d30: 7b 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 70 72  { INSERT INTO pr
8d40: 69 6e 63 65 20 56 41 4c 55 45 53 28 33 2c 20 34  ince VALUES(3, 4
8d50: 29 20 7d 0a 7d 20 7b 31 20 7b 66 6f 72 65 69 67  ) }.} {1 {foreig
8d60: 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74  n key constraint
8d70: 20 66 61 69 6c 65 64 7d 7d 0a 64 6f 5f 74 65 73   failed}}.do_tes
8d80: 74 20 65 5f 66 6b 65 79 2d 33 33 2e 35 20 7b 0a  t e_fkey-33.5 {.
8d90: 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20    execsql {.    
8da0: 43 4f 4d 4d 49 54 3b 0a 20 20 20 20 53 45 4c 45  COMMIT;.    SELE
8db0: 43 54 20 2a 20 46 52 4f 4d 20 6b 69 6e 67 3b 0a  CT * FROM king;.
8dc0: 20 20 7d 0a 7d 20 7b 31 20 7b 7d 20 32 20 7b 7d    }.} {1 {} 2 {}
8dd0: 7d 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  }..#------------
8de0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
8df0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
8e00: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
8e10: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20  -------------.# 
8e20: 2f 2a 20 45 56 3a 20 52 2d 34 39 31 37 38 2d 32  /* EV: R-49178-2
8e30: 31 33 35 38 20 2a 2f 0a 23 20 2f 2a 20 45 56 3a  1358 */.# /* EV:
8e40: 20 52 2d 33 39 36 39 32 2d 31 32 34 38 38 20 2a   R-39692-12488 *
8e50: 2f 0a 23 20 2f 2a 20 45 56 3a 20 52 2d 35 35 31  /.# /* EV: R-551
8e60: 34 37 2d 34 37 36 36 34 20 2a 2f 0a 23 20 2f 2a  47-47664 */.# /*
8e70: 20 45 56 3a 20 52 2d 32 39 36 30 34 2d 33 30 33   EV: R-29604-303
8e80: 39 35 20 2a 2f 0a 23 0a 23 20 54 65 73 74 20 74  95 */.#.# Test t
8e90: 68 61 74 20 69 66 20 61 20 64 65 66 65 72 72 65  hat if a deferre
8ea0: 64 20 63 6f 6e 73 74 72 61 69 6e 74 20 69 73 20  d constraint is 
8eb0: 76 69 6f 6c 61 74 65 64 20 77 69 74 68 69 6e 20  violated within 
8ec0: 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 0a 23  a transaction,.#
8ed0: 20 6e 6f 74 68 69 6e 67 20 68 61 70 70 65 6e 73   nothing happens
8ee0: 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 61 6e 64   immediately and
8ef0: 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73   the database is
8f00: 20 61 6c 6c 6f 77 65 64 20 74 6f 20 70 65 72 73   allowed to pers
8f10: 69 73 74 0a 23 20 69 6e 20 61 20 73 74 61 74 65  ist.# in a state
8f20: 20 74 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 73   that does not s
8f30: 61 74 69 73 66 79 20 74 68 65 20 46 4b 20 63 6f  atisfy the FK co
8f40: 6e 73 74 72 61 69 6e 74 2e 20 48 6f 77 65 76 65  nstraint. Howeve
8f50: 72 20 61 74 74 65 6d 70 74 73 0a 23 20 74 6f 20  r attempts.# to 
8f60: 43 4f 4d 4d 49 54 20 74 68 65 20 74 72 61 6e 73  COMMIT the trans
8f70: 61 63 74 69 6f 6e 20 66 61 69 6c 20 75 6e 74 69  action fail unti
8f80: 6c 20 74 68 65 20 46 4b 20 63 6f 6e 73 74 72 61  l the FK constra
8f90: 69 6e 74 20 69 73 20 73 61 74 69 73 66 69 65 64  int is satisfied
8fa0: 2e 0a 23 0a 70 72 6f 63 20 74 65 73 74 5f 65 66  ..#.proc test_ef
8fb0: 6b 65 79 5f 33 34 20 7b 74 6e 20 69 73 45 72 72  key_34 {tn isErr
8fc0: 6f 72 20 73 71 6c 7d 20 7b 0a 20 20 64 6f 5f 74  or sql} {.  do_t
8fd0: 65 73 74 20 65 5f 66 6b 65 79 2d 33 34 2e 24 74  est e_fkey-34.$t
8fe0: 6e 20 22 0a 20 20 20 20 63 61 74 63 68 73 71 6c  n ".    catchsql
8ff0: 20 7b 24 73 71 6c 7d 0a 20 20 22 20 5b 6c 69 6e   {$sql}.  " [lin
9000: 64 65 78 20 7b 7b 30 20 7b 7d 7d 20 7b 31 20 7b  dex {{0 {}} {1 {
9010: 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73  foreign key cons
9020: 74 72 61 69 6e 74 20 66 61 69 6c 65 64 7d 7d 7d  traint failed}}}
9030: 20 24 69 73 45 72 72 6f 72 5d 0a 7d 0a 64 72 6f   $isError].}.dro
9040: 70 5f 61 6c 6c 5f 74 61 62 6c 65 73 0a 0a 74 65  p_all_tables..te
9050: 73 74 5f 65 66 6b 65 79 5f 33 34 20 20 31 20 30  st_efkey_34  1 0
9060: 20 7b 0a 20 20 43 52 45 41 54 45 20 54 41 42 4c   {.  CREATE TABL
9070: 45 20 6c 6c 28 6b 20 50 52 49 4d 41 52 59 20 4b  E ll(k PRIMARY K
9080: 45 59 29 3b 0a 20 20 43 52 45 41 54 45 20 54 41  EY);.  CREATE TA
9090: 42 4c 45 20 6b 6b 28 63 20 52 45 46 45 52 45 4e  BLE kk(c REFEREN
90a0: 43 45 53 20 6c 6c 20 44 45 46 45 52 52 41 42 4c  CES ll DEFERRABL
90b0: 45 20 49 4e 49 54 49 41 4c 4c 59 20 44 45 46 45  E INITIALLY DEFE
90c0: 52 52 45 44 29 3b 0a 7d 0a 74 65 73 74 5f 65 66  RRED);.}.test_ef
90d0: 6b 65 79 5f 33 34 20 20 32 20 30 20 22 42 45 47  key_34  2 0 "BEG
90e0: 49 4e 22 0a 74 65 73 74 5f 65 66 6b 65 79 5f 33  IN".test_efkey_3
90f0: 34 20 20 33 20 30 20 20 20 22 49 4e 53 45 52 54  4  3 0   "INSERT
9100: 20 49 4e 54 4f 20 6b 6b 20 56 41 4c 55 45 53 28   INTO kk VALUES(
9110: 35 29 22 0a 74 65 73 74 5f 65 66 6b 65 79 5f 33  5)".test_efkey_3
9120: 34 20 20 34 20 30 20 20 20 22 49 4e 53 45 52 54  4  4 0   "INSERT
9130: 20 49 4e 54 4f 20 6b 6b 20 56 41 4c 55 45 53 28   INTO kk VALUES(
9140: 31 30 29 22 0a 74 65 73 74 5f 65 66 6b 65 79 5f  10)".test_efkey_
9150: 33 34 20 20 35 20 31 20 22 43 4f 4d 4d 49 54 22  34  5 1 "COMMIT"
9160: 0a 74 65 73 74 5f 65 66 6b 65 79 5f 33 34 20 20  .test_efkey_34  
9170: 36 20 30 20 20 20 22 49 4e 53 45 52 54 20 49 4e  6 0   "INSERT IN
9180: 54 4f 20 6c 6c 20 56 41 4c 55 45 53 28 31 30 29  TO ll VALUES(10)
9190: 22 0a 74 65 73 74 5f 65 66 6b 65 79 5f 33 34 20  ".test_efkey_34 
91a0: 20 37 20 31 20 22 43 4f 4d 4d 49 54 22 0a 74 65   7 1 "COMMIT".te
91b0: 73 74 5f 65 66 6b 65 79 5f 33 34 20 20 38 20 30  st_efkey_34  8 0
91c0: 20 20 20 22 49 4e 53 45 52 54 20 49 4e 54 4f 20     "INSERT INTO 
91d0: 6c 6c 20 56 41 4c 55 45 53 28 35 29 22 0a 74 65  ll VALUES(5)".te
91e0: 73 74 5f 65 66 6b 65 79 5f 33 34 20 20 39 20 30  st_efkey_34  9 0
91f0: 20 22 43 4f 4d 4d 49 54 22 0a 0a 23 2d 2d 2d 2d   "COMMIT"..#----
9200: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
9210: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
9220: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
9230: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
9240: 2d 2d 2d 2d 2d 0a 23 20 2f 2a 20 45 56 3a 20 52  -----.# /* EV: R
9250: 2d 35 36 38 34 34 2d 36 31 37 30 35 20 2a 2f 0a  -56844-61705 */.
9260: 23 0a 23 20 57 68 65 6e 20 6e 6f 74 20 72 75 6e  #.# When not run
9270: 6e 69 6e 67 20 69 6e 73 69 64 65 20 61 20 74 72  ning inside a tr
9280: 61 6e 73 61 63 74 69 6f 6e 2c 20 61 20 64 65 66  ansaction, a def
9290: 65 72 72 65 64 20 63 6f 6e 73 74 72 61 69 6e 74  erred constraint
92a0: 20 69 73 20 73 69 6d 69 6c 61 72 0a 23 20 74 6f   is similar.# to
92b0: 20 61 6e 20 69 6d 6d 65 64 69 61 74 65 20 63 6f   an immediate co
92c0: 6e 73 74 72 61 69 6e 74 20 28 76 69 6f 6c 61 74  nstraint (violat
92d0: 69 6f 6e 73 20 61 72 65 20 72 65 70 6f 72 74 65  ions are reporte
92e0: 64 20 69 6d 6d 65 64 69 61 74 65 6c 79 29 2e 0a  d immediately)..
92f0: 23 0a 64 72 6f 70 5f 61 6c 6c 5f 74 61 62 6c 65  #.drop_all_table
9300: 73 0a 70 72 6f 63 20 74 65 73 74 5f 65 66 6b 65  s.proc test_efke
9310: 79 5f 33 35 20 7b 74 6e 20 69 73 45 72 72 6f 72  y_35 {tn isError
9320: 20 73 71 6c 7d 20 7b 0a 20 20 64 6f 5f 74 65 73   sql} {.  do_tes
9330: 74 20 65 5f 66 6b 65 79 2d 33 35 2e 24 74 6e 20  t e_fkey-35.$tn 
9340: 22 0a 20 20 20 20 63 61 74 63 68 73 71 6c 20 7b  ".    catchsql {
9350: 24 73 71 6c 7d 0a 20 20 22 20 5b 6c 69 6e 64 65  $sql}.  " [linde
9360: 78 20 7b 7b 30 20 7b 7d 7d 20 7b 31 20 7b 66 6f  x {{0 {}} {1 {fo
9370: 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72  reign key constr
9380: 61 69 6e 74 20 66 61 69 6c 65 64 7d 7d 7d 20 24  aint failed}}} $
9390: 69 73 45 72 72 6f 72 5d 0a 7d 0a 64 6f 5f 74 65  isError].}.do_te
93a0: 73 74 20 65 5f 66 6b 65 79 2d 33 35 2e 31 20 7b  st e_fkey-35.1 {
93b0: 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20  .  execsql {.   
93c0: 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 70 61   CREATE TABLE pa
93d0: 72 65 6e 74 28 78 2c 20 79 29 3b 0a 20 20 20 20  rent(x, y);.    
93e0: 43 52 45 41 54 45 20 55 4e 49 51 55 45 20 49 4e  CREATE UNIQUE IN
93f0: 44 45 58 20 70 69 20 4f 4e 20 70 61 72 65 6e 74  DEX pi ON parent
9400: 28 78 2c 20 79 29 3b 0a 20 20 20 20 43 52 45 41  (x, y);.    CREA
9410: 54 45 20 54 41 42 4c 45 20 63 68 69 6c 64 28 61  TE TABLE child(a
9420: 2c 20 62 2c 0a 20 20 20 20 20 20 46 4f 52 45 49  , b,.      FOREI
9430: 47 4e 20 4b 45 59 28 61 2c 20 62 29 20 52 45 46  GN KEY(a, b) REF
9440: 45 52 45 4e 43 45 53 20 70 61 72 65 6e 74 28 78  ERENCES parent(x
9450: 2c 20 79 29 20 44 45 46 45 52 52 41 42 4c 45 20  , y) DEFERRABLE 
9460: 49 4e 49 54 49 41 4c 4c 59 20 44 45 46 45 52 52  INITIALLY DEFERR
9470: 45 44 0a 20 20 20 20 29 3b 0a 20 20 7d 0a 7d 20  ED.    );.  }.} 
9480: 7b 7d 0a 74 65 73 74 5f 65 66 6b 65 79 5f 33 35  {}.test_efkey_35
9490: 20 32 20 31 20 22 49 4e 53 45 52 54 20 49 4e 54   2 1 "INSERT INT
94a0: 4f 20 63 68 69 6c 64 20 20 56 41 4c 55 45 53 28  O child  VALUES(
94b0: 27 78 27 2c 20 27 79 27 29 22 0a 74 65 73 74 5f  'x', 'y')".test_
94c0: 65 66 6b 65 79 5f 33 35 20 33 20 30 20 22 49 4e  efkey_35 3 0 "IN
94d0: 53 45 52 54 20 49 4e 54 4f 20 70 61 72 65 6e 74  SERT INTO parent
94e0: 20 56 41 4c 55 45 53 28 27 78 27 2c 20 27 79 27   VALUES('x', 'y'
94f0: 29 22 0a 74 65 73 74 5f 65 66 6b 65 79 5f 33 35  )".test_efkey_35
9500: 20 34 20 30 20 22 49 4e 53 45 52 54 20 49 4e 54   4 0 "INSERT INT
9510: 4f 20 63 68 69 6c 64 20 20 56 41 4c 55 45 53 28  O child  VALUES(
9520: 27 78 27 2c 20 27 79 27 29 22 0a 0a 0a 23 2d 2d  'x', 'y')"...#--
9530: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
9540: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
9550: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
9560: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
9570: 2d 2d 2d 2d 2d 2d 2d 0a 23 20 2f 2a 20 45 56 3a  -------.# /* EV:
9580: 20 52 2d 31 32 37 38 32 2d 36 31 38 34 31 20 2a   R-12782-61841 *
9590: 2f 0a 23 0a 23 20 54 65 73 74 20 74 68 61 74 20  /.#.# Test that 
95a0: 61 6e 20 46 4b 20 63 6f 6e 73 74 72 61 69 6e 74  an FK constraint
95b0: 20 69 73 20 6d 61 64 65 20 64 65 66 65 72 72 65   is made deferre
95c0: 64 20 62 79 20 61 64 64 69 6e 67 20 74 68 65 20  d by adding the 
95d0: 66 6f 6c 6c 6f 77 69 6e 67 0a 23 20 74 6f 20 74  following.# to t
95e0: 68 65 20 64 65 66 69 6e 69 74 69 6f 6e 3a 0a 23  he definition:.#
95f0: 0a 23 20 20 20 44 45 46 45 52 52 41 42 4c 45 20  .#   DEFERRABLE 
9600: 49 4e 49 54 49 41 4c 4c 59 20 44 45 46 45 52 52  INITIALLY DEFERR
9610: 45 44 0a 23 0a 23 20 2f 2a 20 45 56 3a 20 52 2d  ED.#.# /* EV: R-
9620: 30 39 30 30 35 2d 32 38 37 39 31 20 2a 2f 0a 23  09005-28791 */.#
9630: 0a 23 20 41 6c 73 6f 20 74 65 73 74 20 74 68 61  .# Also test tha
9640: 74 20 61 64 64 69 6e 67 20 61 6e 79 20 6f 66 20  t adding any of 
9650: 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 6f  the following to
9660: 20 61 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 64   a foreign key d
9670: 65 66 69 6e 69 74 69 6f 6e 20 0a 23 20 6d 61 6b  efinition .# mak
9680: 65 73 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e  es the constrain
9690: 74 20 49 4d 4d 45 44 49 41 54 45 3a 0a 23 0a 23  t IMMEDIATE:.#.#
96a0: 20 20 20 4e 4f 54 20 44 45 46 45 52 52 41 42 4c     NOT DEFERRABL
96b0: 45 20 49 4e 49 54 49 41 4c 4c 59 20 44 45 46 45  E INITIALLY DEFE
96c0: 52 52 45 44 0a 23 20 20 20 4e 4f 54 20 44 45 46  RRED.#   NOT DEF
96d0: 45 52 52 41 42 4c 45 20 49 4e 49 54 49 41 4c 4c  ERRABLE INITIALL
96e0: 59 20 49 4d 4d 45 44 49 41 54 45 0a 23 20 20 20  Y IMMEDIATE.#   
96f0: 4e 4f 54 20 44 45 46 45 52 52 41 42 4c 45 0a 23  NOT DEFERRABLE.#
9700: 20 20 20 44 45 46 45 52 52 41 42 4c 45 20 49 4e     DEFERRABLE IN
9710: 49 54 49 41 4c 4c 59 20 49 4d 4d 45 44 49 41 54  ITIALLY IMMEDIAT
9720: 45 0a 23 20 20 20 44 45 46 45 52 52 41 42 4c 45  E.#   DEFERRABLE
9730: 0a 23 0a 23 20 2f 2a 20 45 56 3a 20 52 2d 33 35  .#.# /* EV: R-35
9740: 32 39 30 2d 31 36 34 36 30 20 2a 2f 0a 23 0a 23  290-16460 */.#.#
9750: 20 46 6f 72 65 69 67 6e 20 6b 65 79 73 20 61 72   Foreign keys ar
9760: 65 20 49 4d 4d 45 44 49 41 54 45 20 62 79 20 64  e IMMEDIATE by d
9770: 65 66 61 75 6c 74 20 28 69 66 20 74 68 65 72 65  efault (if there
9780: 20 69 73 20 6e 6f 20 44 45 46 45 52 52 41 42 4c   is no DEFERRABL
9790: 45 20 6f 72 20 4e 4f 54 0a 23 20 44 45 46 45 52  E or NOT.# DEFER
97a0: 52 41 42 4c 45 20 63 6c 61 75 73 65 29 2e 0a 23  RABLE clause)..#
97b0: 0a 23 20 2f 2a 20 45 56 3a 20 52 2d 33 30 33 32  .# /* EV: R-3032
97c0: 33 2d 32 31 39 31 37 20 2a 2f 20 20 46 4b 73 20  3-21917 */  FKs 
97d0: 61 72 65 20 65 69 74 68 65 72 20 49 4d 4d 45 44  are either IMMED
97e0: 49 41 54 45 20 6f 72 20 44 45 46 45 52 52 45 44  IATE or DEFERRED
97f0: 2e 0a 23 0a 64 72 6f 70 5f 61 6c 6c 5f 74 61 62  ..#.drop_all_tab
9800: 6c 65 73 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b  les.do_test e_fk
9810: 65 79 2d 32 39 2e 31 20 7b 0a 20 20 65 78 65 63  ey-29.1 {.  exec
9820: 73 71 6c 20 7b 0a 20 20 20 20 43 52 45 41 54 45  sql {.    CREATE
9830: 20 54 41 42 4c 45 20 70 61 72 65 6e 74 28 78 2c   TABLE parent(x,
9840: 20 79 2c 20 7a 2c 20 50 52 49 4d 41 52 59 20 4b   y, z, PRIMARY K
9850: 45 59 28 78 2c 79 2c 7a 29 29 3b 0a 20 20 20 20  EY(x,y,z));.    
9860: 43 52 45 41 54 45 20 54 41 42 4c 45 20 63 31 28  CREATE TABLE c1(
9870: 61 2c 20 62 2c 20 63 2c 0a 20 20 20 20 20 20 46  a, b, c,.      F
9880: 4f 52 45 49 47 4e 20 4b 45 59 28 61 2c 20 62 2c  OREIGN KEY(a, b,
9890: 20 63 29 20 52 45 46 45 52 45 4e 43 45 53 20 70   c) REFERENCES p
98a0: 61 72 65 6e 74 20 4e 4f 54 20 44 45 46 45 52 52  arent NOT DEFERR
98b0: 41 42 4c 45 20 49 4e 49 54 49 41 4c 4c 59 20 44  ABLE INITIALLY D
98c0: 45 46 45 52 52 45 44 0a 20 20 20 20 29 3b 0a 20  EFERRED.    );. 
98d0: 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20     CREATE TABLE 
98e0: 63 32 28 61 2c 20 62 2c 20 63 2c 0a 20 20 20 20  c2(a, b, c,.    
98f0: 20 20 46 4f 52 45 49 47 4e 20 4b 45 59 28 61 2c    FOREIGN KEY(a,
9900: 20 62 2c 20 63 29 20 52 45 46 45 52 45 4e 43 45   b, c) REFERENCE
9910: 53 20 70 61 72 65 6e 74 20 4e 4f 54 20 44 45 46  S parent NOT DEF
9920: 45 52 52 41 42 4c 45 20 49 4e 49 54 49 41 4c 4c  ERRABLE INITIALL
9930: 59 20 49 4d 4d 45 44 49 41 54 45 0a 20 20 20 20  Y IMMEDIATE.    
9940: 29 3b 0a 20 20 20 20 43 52 45 41 54 45 20 54 41  );.    CREATE TA
9950: 42 4c 45 20 63 33 28 61 2c 20 62 2c 20 63 2c 0a  BLE c3(a, b, c,.
9960: 20 20 20 20 20 20 46 4f 52 45 49 47 4e 20 4b 45        FOREIGN KE
9970: 59 28 61 2c 20 62 2c 20 63 29 20 52 45 46 45 52  Y(a, b, c) REFER
9980: 45 4e 43 45 53 20 70 61 72 65 6e 74 20 4e 4f 54  ENCES parent NOT
9990: 20 44 45 46 45 52 52 41 42 4c 45 0a 20 20 20 20   DEFERRABLE.    
99a0: 29 3b 0a 20 20 20 20 43 52 45 41 54 45 20 54 41  );.    CREATE TA
99b0: 42 4c 45 20 63 34 28 61 2c 20 62 2c 20 63 2c 0a  BLE c4(a, b, c,.
99c0: 20 20 20 20 20 20 46 4f 52 45 49 47 4e 20 4b 45        FOREIGN KE
99d0: 59 28 61 2c 20 62 2c 20 63 29 20 52 45 46 45 52  Y(a, b, c) REFER
99e0: 45 4e 43 45 53 20 70 61 72 65 6e 74 20 44 45 46  ENCES parent DEF
99f0: 45 52 52 41 42 4c 45 20 49 4e 49 54 49 41 4c 4c  ERRABLE INITIALL
9a00: 59 20 49 4d 4d 45 44 49 41 54 45 0a 20 20 20 20  Y IMMEDIATE.    
9a10: 29 3b 0a 20 20 20 20 43 52 45 41 54 45 20 54 41  );.    CREATE TA
9a20: 42 4c 45 20 63 35 28 61 2c 20 62 2c 20 63 2c 0a  BLE c5(a, b, c,.
9a30: 20 20 20 20 20 20 46 4f 52 45 49 47 4e 20 4b 45        FOREIGN KE
9a40: 59 28 61 2c 20 62 2c 20 63 29 20 52 45 46 45 52  Y(a, b, c) REFER
9a50: 45 4e 43 45 53 20 70 61 72 65 6e 74 20 44 45 46  ENCES parent DEF
9a60: 45 52 52 41 42 4c 45 0a 20 20 20 20 29 3b 0a 20  ERRABLE.    );. 
9a70: 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20     CREATE TABLE 
9a80: 63 36 28 61 2c 20 62 2c 20 63 2c 20 46 4f 52 45  c6(a, b, c, FORE
9a90: 49 47 4e 20 4b 45 59 28 61 2c 20 62 2c 20 63 29  IGN KEY(a, b, c)
9aa0: 20 52 45 46 45 52 45 4e 43 45 53 20 70 61 72 65   REFERENCES pare
9ab0: 6e 74 29 3b 0a 0a 20 20 20 20 2d 2d 20 54 68 69  nt);..    -- Thi
9ac0: 73 20 46 4b 20 63 6f 6e 73 74 72 61 69 6e 74 20  s FK constraint 
9ad0: 69 73 20 74 68 65 20 6f 6e 6c 79 20 64 65 66 65  is the only defe
9ae0: 72 72 61 62 6c 65 20 6f 6e 65 2e 0a 20 20 20 20  rrable one..    
9af0: 43 52 45 41 54 45 20 54 41 42 4c 45 20 63 37 28  CREATE TABLE c7(
9b00: 61 2c 20 62 2c 20 63 2c 0a 20 20 20 20 20 20 46  a, b, c,.      F
9b10: 4f 52 45 49 47 4e 20 4b 45 59 28 61 2c 20 62 2c  OREIGN KEY(a, b,
9b20: 20 63 29 20 52 45 46 45 52 45 4e 43 45 53 20 70   c) REFERENCES p
9b30: 61 72 65 6e 74 20 44 45 46 45 52 52 41 42 4c 45  arent DEFERRABLE
9b40: 20 49 4e 49 54 49 41 4c 4c 59 20 44 45 46 45 52   INITIALLY DEFER
9b50: 52 45 44 0a 20 20 20 20 29 3b 0a 0a 20 20 20 20  RED.    );..    
9b60: 49 4e 53 45 52 54 20 49 4e 54 4f 20 70 61 72 65  INSERT INTO pare
9b70: 6e 74 20 56 41 4c 55 45 53 28 27 61 27 2c 20 27  nt VALUES('a', '
9b80: 62 27 2c 20 27 63 27 29 3b 0a 20 20 20 20 49 4e  b', 'c');.    IN
9b90: 53 45 52 54 20 49 4e 54 4f 20 70 61 72 65 6e 74  SERT INTO parent
9ba0: 20 56 41 4c 55 45 53 28 27 64 27 2c 20 27 65 27   VALUES('d', 'e'
9bb0: 2c 20 27 66 27 29 3b 0a 20 20 20 20 49 4e 53 45  , 'f');.    INSE
9bc0: 52 54 20 49 4e 54 4f 20 70 61 72 65 6e 74 20 56  RT INTO parent V
9bd0: 41 4c 55 45 53 28 27 67 27 2c 20 27 68 27 2c 20  ALUES('g', 'h', 
9be0: 27 69 27 29 3b 0a 20 20 20 20 49 4e 53 45 52 54  'i');.    INSERT
9bf0: 20 49 4e 54 4f 20 70 61 72 65 6e 74 20 56 41 4c   INTO parent VAL
9c00: 55 45 53 28 27 6a 27 2c 20 27 6b 27 2c 20 27 6c  UES('j', 'k', 'l
9c10: 27 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49  ');.    INSERT I
9c20: 4e 54 4f 20 70 61 72 65 6e 74 20 56 41 4c 55 45  NTO parent VALUE
9c30: 53 28 27 6d 27 2c 20 27 6e 27 2c 20 27 6f 27 29  S('m', 'n', 'o')
9c40: 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54  ;.    INSERT INT
9c50: 4f 20 70 61 72 65 6e 74 20 56 41 4c 55 45 53 28  O parent VALUES(
9c60: 27 70 27 2c 20 27 71 27 2c 20 27 72 27 29 3b 0a  'p', 'q', 'r');.
9c70: 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20      INSERT INTO 
9c80: 70 61 72 65 6e 74 20 56 41 4c 55 45 53 28 27 73  parent VALUES('s
9c90: 27 2c 20 27 74 27 2c 20 27 75 27 29 3b 0a 0a 20  ', 't', 'u');.. 
9ca0: 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 63     INSERT INTO c
9cb0: 31 20 56 41 4c 55 45 53 28 27 61 27 2c 20 27 62  1 VALUES('a', 'b
9cc0: 27 2c 20 27 63 27 29 3b 0a 20 20 20 20 49 4e 53  ', 'c');.    INS
9cd0: 45 52 54 20 49 4e 54 4f 20 63 32 20 56 41 4c 55  ERT INTO c2 VALU
9ce0: 45 53 28 27 64 27 2c 20 27 65 27 2c 20 27 66 27  ES('d', 'e', 'f'
9cf0: 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e  );.    INSERT IN
9d00: 54 4f 20 63 33 20 56 41 4c 55 45 53 28 27 67 27  TO c3 VALUES('g'
9d10: 2c 20 27 68 27 2c 20 27 69 27 29 3b 0a 20 20 20  , 'h', 'i');.   
9d20: 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 63 34 20   INSERT INTO c4 
9d30: 56 41 4c 55 45 53 28 27 6a 27 2c 20 27 6b 27 2c  VALUES('j', 'k',
9d40: 20 27 6c 27 29 3b 0a 20 20 20 20 49 4e 53 45 52   'l');.    INSER
9d50: 54 20 49 4e 54 4f 20 63 35 20 56 41 4c 55 45 53  T INTO c5 VALUES
9d60: 28 27 6d 27 2c 20 27 6e 27 2c 20 27 6f 27 29 3b  ('m', 'n', 'o');
9d70: 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f  .    INSERT INTO
9d80: 20 63 36 20 56 41 4c 55 45 53 28 27 70 27 2c 20   c6 VALUES('p', 
9d90: 27 71 27 2c 20 27 72 27 29 3b 0a 20 20 20 20 49  'q', 'r');.    I
9da0: 4e 53 45 52 54 20 49 4e 54 4f 20 63 37 20 56 41  NSERT INTO c7 VA
9db0: 4c 55 45 53 28 27 73 27 2c 20 27 74 27 2c 20 27  LUES('s', 't', '
9dc0: 75 27 29 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a 0a 70  u');.  }.} {}..p
9dd0: 72 6f 63 20 74 65 73 74 5f 65 66 6b 65 79 5f 32  roc test_efkey_2
9de0: 39 20 7b 74 6e 20 73 71 6c 20 69 73 45 72 72 6f  9 {tn sql isErro
9df0: 72 7d 20 7b 0a 20 20 64 6f 5f 74 65 73 74 20 65  r} {.  do_test e
9e00: 5f 66 6b 65 79 2d 32 39 2e 24 74 6e 20 22 63 61  _fkey-29.$tn "ca
9e10: 74 63 68 73 71 6c 20 7b 24 73 71 6c 7d 22 20 5b  tchsql {$sql}" [
9e20: 0a 20 20 20 20 6c 69 6e 64 65 78 20 7b 7b 30 20  .    lindex {{0 
9e30: 7b 7d 7d 20 7b 31 20 7b 66 6f 72 65 69 67 6e 20  {}} {1 {foreign 
9e40: 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 20 66  key constraint f
9e50: 61 69 6c 65 64 7d 7d 7d 20 24 69 73 45 72 72 6f  ailed}}} $isErro
9e60: 72 0a 20 20 5d 0a 7d 0a 74 65 73 74 5f 65 66 6b  r.  ].}.test_efk
9e70: 65 79 5f 32 39 20 20 32 20 22 42 45 47 49 4e 22  ey_29  2 "BEGIN"
9e80: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9e90: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9ea0: 20 20 20 30 0a 74 65 73 74 5f 65 66 6b 65 79 5f     0.test_efkey_
9eb0: 32 39 20 20 33 20 22 44 45 4c 45 54 45 20 46 52  29  3 "DELETE FR
9ec0: 4f 4d 20 70 61 72 65 6e 74 20 57 48 45 52 45 20  OM parent WHERE 
9ed0: 78 20 3d 20 27 61 27 22 20 20 20 20 20 20 20 20  x = 'a'"        
9ee0: 31 0a 74 65 73 74 5f 65 66 6b 65 79 5f 32 39 20  1.test_efkey_29 
9ef0: 20 34 20 22 44 45 4c 45 54 45 20 46 52 4f 4d 20   4 "DELETE FROM 
9f00: 70 61 72 65 6e 74 20 57 48 45 52 45 20 78 20 3d  parent WHERE x =
9f10: 20 27 64 27 22 20 20 20 20 20 20 20 20 31 0a 74   'd'"        1.t
9f20: 65 73 74 5f 65 66 6b 65 79 5f 32 39 20 20 35 20  est_efkey_29  5 
9f30: 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 70 61 72  "DELETE FROM par
9f40: 65 6e 74 20 57 48 45 52 45 20 78 20 3d 20 27 67  ent WHERE x = 'g
9f50: 27 22 20 20 20 20 20 20 20 20 31 0a 74 65 73 74  '"        1.test
9f60: 5f 65 66 6b 65 79 5f 32 39 20 20 36 20 22 44 45  _efkey_29  6 "DE
9f70: 4c 45 54 45 20 46 52 4f 4d 20 70 61 72 65 6e 74  LETE FROM parent
9f80: 20 57 48 45 52 45 20 78 20 3d 20 27 6a 27 22 20   WHERE x = 'j'" 
9f90: 20 20 20 20 20 20 20 31 0a 74 65 73 74 5f 65 66         1.test_ef
9fa0: 6b 65 79 5f 32 39 20 20 37 20 22 44 45 4c 45 54  key_29  7 "DELET
9fb0: 45 20 46 52 4f 4d 20 70 61 72 65 6e 74 20 57 48  E FROM parent WH
9fc0: 45 52 45 20 78 20 3d 20 27 6d 27 22 20 20 20 20  ERE x = 'm'"    
9fd0: 20 20 20 20 31 0a 74 65 73 74 5f 65 66 6b 65 79      1.test_efkey
9fe0: 5f 32 39 20 20 38 20 22 44 45 4c 45 54 45 20 46  _29  8 "DELETE F
9ff0: 52 4f 4d 20 70 61 72 65 6e 74 20 57 48 45 52 45  ROM parent WHERE
a000: 20 78 20 3d 20 27 70 27 22 20 20 20 20 20 20 20   x = 'p'"       
a010: 20 31 0a 74 65 73 74 5f 65 66 6b 65 79 5f 32 39   1.test_efkey_29
a020: 20 20 39 20 22 44 45 4c 45 54 45 20 46 52 4f 4d    9 "DELETE FROM
a030: 20 70 61 72 65 6e 74 20 57 48 45 52 45 20 78 20   parent WHERE x 
a040: 3d 20 27 73 27 22 20 20 20 20 20 20 20 20 30 0a  = 's'"        0.
a050: 74 65 73 74 5f 65 66 6b 65 79 5f 32 39 20 31 30  test_efkey_29 10
a060: 20 22 43 4f 4d 4d 49 54 22 20 20 20 20 20 20 20   "COMMIT"       
a070: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a080: 20 20 20 20 20 20 20 20 20 20 20 31 0a 74 65 73             1.tes
a090: 74 5f 65 66 6b 65 79 5f 32 39 20 31 31 20 22 52  t_efkey_29 11 "R
a0a0: 4f 4c 4c 42 41 43 4b 22 20 20 20 20 20 20 20 20  OLLBACK"        
a0b0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a0c0: 20 20 20 20 20 20 20 20 30 0a 0a 74 65 73 74 5f          0..test_
a0d0: 65 66 6b 65 79 5f 32 39 20 20 39 20 22 42 45 47  efkey_29  9 "BEG
a0e0: 49 4e 22 20 20 20 20 20 20 20 20 20 20 20 20 20  IN"             
a0f0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a100: 20 20 20 20 20 20 30 0a 74 65 73 74 5f 65 66 6b        0.test_efk
a110: 65 79 5f 32 39 20 31 30 20 22 55 50 44 41 54 45  ey_29 10 "UPDATE
a120: 20 70 61 72 65 6e 74 20 53 45 54 20 7a 20 3d 20   parent SET z = 
a130: 27 7a 27 20 57 48 45 52 45 20 7a 20 3d 20 27 63  'z' WHERE z = 'c
a140: 27 22 20 31 0a 74 65 73 74 5f 65 66 6b 65 79 5f  '" 1.test_efkey_
a150: 32 39 20 31 31 20 22 55 50 44 41 54 45 20 70 61  29 11 "UPDATE pa
a160: 72 65 6e 74 20 53 45 54 20 7a 20 3d 20 27 7a 27  rent SET z = 'z'
a170: 20 57 48 45 52 45 20 7a 20 3d 20 27 66 27 22 20   WHERE z = 'f'" 
a180: 31 0a 74 65 73 74 5f 65 66 6b 65 79 5f 32 39 20  1.test_efkey_29 
a190: 31 32 20 22 55 50 44 41 54 45 20 70 61 72 65 6e  12 "UPDATE paren
a1a0: 74 20 53 45 54 20 7a 20 3d 20 27 7a 27 20 57 48  t SET z = 'z' WH
a1b0: 45 52 45 20 7a 20 3d 20 27 69 27 22 20 31 0a 74  ERE z = 'i'" 1.t
a1c0: 65 73 74 5f 65 66 6b 65 79 5f 32 39 20 31 33 20  est_efkey_29 13 
a1d0: 22 55 50 44 41 54 45 20 70 61 72 65 6e 74 20 53  "UPDATE parent S
a1e0: 45 54 20 7a 20 3d 20 27 7a 27 20 57 48 45 52 45  ET z = 'z' WHERE
a1f0: 20 7a 20 3d 20 27 6c 27 22 20 31 0a 74 65 73 74   z = 'l'" 1.test
a200: 5f 65 66 6b 65 79 5f 32 39 20 31 34 20 22 55 50  _efkey_29 14 "UP
a210: 44 41 54 45 20 70 61 72 65 6e 74 20 53 45 54 20  DATE parent SET 
a220: 7a 20 3d 20 27 7a 27 20 57 48 45 52 45 20 7a 20  z = 'z' WHERE z 
a230: 3d 20 27 6f 27 22 20 31 0a 74 65 73 74 5f 65 66  = 'o'" 1.test_ef
a240: 6b 65 79 5f 32 39 20 31 35 20 22 55 50 44 41 54  key_29 15 "UPDAT
a250: 45 20 70 61 72 65 6e 74 20 53 45 54 20 7a 20 3d  E parent SET z =
a260: 20 27 7a 27 20 57 48 45 52 45 20 7a 20 3d 20 27   'z' WHERE z = '
a270: 72 27 22 20 31 0a 74 65 73 74 5f 65 66 6b 65 79  r'" 1.test_efkey
a280: 5f 32 39 20 31 36 20 22 55 50 44 41 54 45 20 70  _29 16 "UPDATE p
a290: 61 72 65 6e 74 20 53 45 54 20 7a 20 3d 20 27 7a  arent SET z = 'z
a2a0: 27 20 57 48 45 52 45 20 7a 20 3d 20 27 75 27 22  ' WHERE z = 'u'"
a2b0: 20 30 0a 74 65 73 74 5f 65 66 6b 65 79 5f 32 39   0.test_efkey_29
a2c0: 20 31 37 20 22 43 4f 4d 4d 49 54 22 20 20 20 20   17 "COMMIT"    
a2d0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a2e0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 0a                1.
a2f0: 74 65 73 74 5f 65 66 6b 65 79 5f 32 39 20 31 38  test_efkey_29 18
a300: 20 22 52 4f 4c 4c 42 41 43 4b 22 20 20 20 20 20   "ROLLBACK"     
a310: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a320: 20 20 20 20 20 20 20 20 20 20 20 30 0a 0a 74 65             0..te
a330: 73 74 5f 65 66 6b 65 79 5f 32 39 20 31 37 20 22  st_efkey_29 17 "
a340: 42 45 47 49 4e 22 20 20 20 20 20 20 20 20 20 20  BEGIN"          
a350: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a360: 20 20 20 20 20 20 20 20 20 30 0a 74 65 73 74 5f           0.test_
a370: 65 66 6b 65 79 5f 32 39 20 31 38 20 22 49 4e 53  efkey_29 18 "INS
a380: 45 52 54 20 49 4e 54 4f 20 63 31 20 56 41 4c 55  ERT INTO c1 VALU
a390: 45 53 28 31 2c 20 32 2c 20 33 29 22 20 20 20 20  ES(1, 2, 3)"    
a3a0: 20 20 20 20 20 20 31 0a 74 65 73 74 5f 65 66 6b        1.test_efk
a3b0: 65 79 5f 32 39 20 31 39 20 22 49 4e 53 45 52 54  ey_29 19 "INSERT
a3c0: 20 49 4e 54 4f 20 63 32 20 56 41 4c 55 45 53 28   INTO c2 VALUES(
a3d0: 31 2c 20 32 2c 20 33 29 22 20 20 20 20 20 20 20  1, 2, 3)"       
a3e0: 20 20 20 31 0a 74 65 73 74 5f 65 66 6b 65 79 5f     1.test_efkey_
a3f0: 32 39 20 32 30 20 22 49 4e 53 45 52 54 20 49 4e  29 20 "INSERT IN
a400: 54 4f 20 63 33 20 56 41 4c 55 45 53 28 31 2c 20  TO c3 VALUES(1, 
a410: 32 2c 20 33 29 22 20 20 20 20 20 20 20 20 20 20  2, 3)"          
a420: 31 0a 74 65 73 74 5f 65 66 6b 65 79 5f 32 39 20  1.test_efkey_29 
a430: 32 31 20 22 49 4e 53 45 52 54 20 49 4e 54 4f 20  21 "INSERT INTO 
a440: 63 34 20 56 41 4c 55 45 53 28 31 2c 20 32 2c 20  c4 VALUES(1, 2, 
a450: 33 29 22 20 20 20 20 20 20 20 20 20 20 31 0a 74  3)"          1.t
a460: 65 73 74 5f 65 66 6b 65 79 5f 32 39 20 32 32 20  est_efkey_29 22 
a470: 22 49 4e 53 45 52 54 20 49 4e 54 4f 20 63 35 20  "INSERT INTO c5 
a480: 56 41 4c 55 45 53 28 31 2c 20 32 2c 20 33 29 22  VALUES(1, 2, 3)"
a490: 20 20 20 20 20 20 20 20 20 20 31 0a 74 65 73 74            1.test
a4a0: 5f 65 66 6b 65 79 5f 32 39 20 32 32 20 22 49 4e  _efkey_29 22 "IN
a4b0: 53 45 52 54 20 49 4e 54 4f 20 63 36 20 56 41 4c  SERT INTO c6 VAL
a4c0: 55 45 53 28 31 2c 20 32 2c 20 33 29 22 20 20 20  UES(1, 2, 3)"   
a4d0: 20 20 20 20 20 20 20 31 0a 74 65 73 74 5f 65 66         1.test_ef
a4e0: 6b 65 79 5f 32 39 20 32 32 20 22 49 4e 53 45 52  key_29 22 "INSER
a4f0: 54 20 49 4e 54 4f 20 63 37 20 56 41 4c 55 45 53  T INTO c7 VALUES
a500: 28 31 2c 20 32 2c 20 33 29 22 20 20 20 20 20 20  (1, 2, 3)"      
a510: 20 20 20 20 30 0a 74 65 73 74 5f 65 66 6b 65 79      0.test_efkey
a520: 5f 32 39 20 32 33 20 22 43 4f 4d 4d 49 54 22 20  _29 23 "COMMIT" 
a530: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a540: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a550: 20 31 0a 74 65 73 74 5f 65 66 6b 65 79 5f 32 39   1.test_efkey_29
a560: 20 32 34 20 22 49 4e 53 45 52 54 20 49 4e 54 4f   24 "INSERT INTO
a570: 20 70 61 72 65 6e 74 20 56 41 4c 55 45 53 28 31   parent VALUES(1
a580: 2c 20 32 2c 20 33 29 22 20 20 20 20 20 20 30 0a  , 2, 3)"      0.
a590: 74 65 73 74 5f 65 66 6b 65 79 5f 32 39 20 32 35  test_efkey_29 25
a5a0: 20 22 43 4f 4d 4d 49 54 22 20 20 20 20 20 20 20   "COMMIT"       
a5b0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a5c0: 20 20 20 20 20 20 20 20 20 20 20 30 0a 0a 74 65             0..te
a5d0: 73 74 5f 65 66 6b 65 79 5f 32 39 20 32 36 20 22  st_efkey_29 26 "
a5e0: 42 45 47 49 4e 22 20 20 20 20 20 20 20 20 20 20  BEGIN"          
a5f0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a600: 20 20 20 20 20 20 20 20 20 30 0a 74 65 73 74 5f           0.test_
a610: 65 66 6b 65 79 5f 32 39 20 32 37 20 22 55 50 44  efkey_29 27 "UPD
a620: 41 54 45 20 63 31 20 53 45 54 20 61 20 3d 20 31  ATE c1 SET a = 1
a630: 30 22 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0"              
a640: 20 20 20 20 20 20 31 0a 74 65 73 74 5f 65 66 6b        1.test_efk
a650: 65 79 5f 32 39 20 32 38 20 22 55 50 44 41 54 45  ey_29 28 "UPDATE
a660: 20 63 32 20 53 45 54 20 61 20 3d 20 31 30 22 20   c2 SET a = 10" 
a670: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a680: 20 20 20 31 0a 74 65 73 74 5f 65 66 6b 65 79 5f     1.test_efkey_
a690: 32 39 20 32 39 20 22 55 50 44 41 54 45 20 63 33  29 29 "UPDATE c3
a6a0: 20 53 45 54 20 61 20 3d 20 31 30 22 20 20 20 20   SET a = 10"    
a6b0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6c0: 31 0a 74 65 73 74 5f 65 66 6b 65 79 5f 32 39 20  1.test_efkey_29 
a6d0: 33 30 20 22 55 50 44 41 54 45 20 63 34 20 53 45  30 "UPDATE c4 SE
a6e0: 54 20 61 20 3d 20 31 30 22 20 20 20 20 20 20 20  T a = 10"       
a6f0: 20 20 20 20 20 20 20 20 20 20 20 20 20 31 0a 74               1.t
a700: 65 73 74 5f 65 66 6b 65 79 5f 32 39 20 33 31 20  est_efkey_29 31 
a710: 22 55 50 44 41 54 45 20 63 35 20 53 45 54 20 61  "UPDATE c5 SET a
a720: 20 3d 20 31 30 22 20 20 20 20 20 20 20 20 20 20   = 10"          
a730: 20 20 20 20 20 20 20 20 20 20 31 0a 74 65 73 74            1.test
a740: 5f 65 66 6b 65 79 5f 32 39 20 33 31 20 22 55 50  _efkey_29 31 "UP
a750: 44 41 54 45 20 63 36 20 53 45 54 20 61 20 3d 20  DATE c6 SET a = 
a760: 31 30 22 20 20 20 20 20 20 20 20 20 20 20 20 20  10"             
a770: 20 20 20 20 20 20 20 31 0a 74 65 73 74 5f 65 66         1.test_ef
a780: 6b 65 79 5f 32 39 20 33 31 20 22 55 50 44 41 54  key_29 31 "UPDAT
a790: 45 20 63 37 20 53 45 54 20 61 20 3d 20 31 30 22  E c7 SET a = 10"
a7a0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a7b0: 20 20 20 20 30 0a 74 65 73 74 5f 65 66 6b 65 79      0.test_efkey
a7c0: 5f 32 39 20 33 32 20 22 43 4f 4d 4d 49 54 22 20  _29 32 "COMMIT" 
a7d0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a7e0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a7f0: 20 31 0a 74 65 73 74 5f 65 66 6b 65 79 5f 32 39   1.test_efkey_29
a800: 20 33 33 20 22 52 4f 4c 4c 42 41 43 4b 22 20 20   33 "ROLLBACK"  
a810: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a820: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 0a                0.
a830: 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  .#--------------
a840: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
a850: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
a860: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
a870: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20 2f 2a  -----------.# /*
a880: 20 45 56 3a 20 52 2d 33 35 30 34 33 2d 30 31 35   EV: R-35043-015
a890: 34 36 20 2a 2f 0a 23 0a 23 20 54 65 73 74 20 61  46 */.#.# Test a
a8a0: 6e 20 65 78 61 6d 70 6c 65 20 66 72 6f 6d 20 66  n example from f
a8b0: 6f 72 65 69 67 6e 6b 65 79 73 2e 68 74 6d 6c 20  oreignkeys.html 
a8c0: 64 65 61 6c 69 6e 67 20 77 69 74 68 20 61 20 64  dealing with a d
a8d0: 65 66 65 72 72 65 64 20 66 6f 72 65 69 67 6e 20  eferred foreign 
a8e0: 0a 23 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e  .# key constrain
a8f0: 74 2e 0a 23 0a 64 6f 5f 74 65 73 74 20 65 5f 66  t..#.do_test e_f
a900: 6b 65 79 2d 32 38 2e 31 20 7b 0a 20 20 64 72 6f  key-28.1 {.  dro
a910: 70 5f 61 6c 6c 5f 74 61 62 6c 65 73 0a 20 20 65  p_all_tables.  e
a920: 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 43 52 45  xecsql {.    CRE
a930: 41 54 45 20 54 41 42 4c 45 20 61 72 74 69 73 74  ATE TABLE artist
a940: 28 0a 20 20 20 20 20 20 61 72 74 69 73 74 69 64  (.      artistid
a950: 20 20 20 20 49 4e 54 45 47 45 52 20 50 52 49 4d      INTEGER PRIM
a960: 41 52 59 20 4b 45 59 2c 20 0a 20 20 20 20 20 20  ARY KEY, .      
a970: 61 72 74 69 73 74 6e 61 6d 65 20 20 54 45 58 54  artistname  TEXT
a980: 0a 20 20 20 20 29 3b 0a 20 20 20 20 43 52 45 41  .    );.    CREA
a990: 54 45 20 54 41 42 4c 45 20 74 72 61 63 6b 28 0a  TE TABLE track(.
a9a0: 20 20 20 20 20 20 74 72 61 63 6b 69 64 20 20 20        trackid   
a9b0: 20 20 49 4e 54 45 47 45 52 2c 0a 20 20 20 20 20    INTEGER,.     
a9c0: 20 74 72 61 63 6b 6e 61 6d 65 20 20 20 54 45 58   trackname   TEX
a9d0: 54 2c 20 0a 20 20 20 20 20 20 74 72 61 63 6b 61  T, .      tracka
a9e0: 72 74 69 73 74 20 49 4e 54 45 47 45 52 20 52 45  rtist INTEGER RE
a9f0: 46 45 52 45 4e 43 45 53 20 61 72 74 69 73 74 28  FERENCES artist(
aa00: 61 72 74 69 73 74 69 64 29 20 44 45 46 45 52 52  artistid) DEFERR
aa10: 41 42 4c 45 20 49 4e 49 54 49 41 4c 4c 59 20 44  ABLE INITIALLY D
aa20: 45 46 45 52 52 45 44 0a 20 20 20 20 29 3b 0a 20  EFERRED.    );. 
aa30: 20 7d 0a 7d 20 7b 7d 0a 64 6f 5f 74 65 73 74 20   }.} {}.do_test 
aa40: 65 5f 66 6b 65 79 2d 32 38 2e 32 20 7b 0a 20 20  e_fkey-28.2 {.  
aa50: 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 42 45  execsql {.    BE
aa60: 47 49 4e 3b 0a 20 20 20 20 20 20 49 4e 53 45 52  GIN;.      INSER
aa70: 54 20 49 4e 54 4f 20 74 72 61 63 6b 20 56 41 4c  T INTO track VAL
aa80: 55 45 53 28 31 2c 20 27 57 68 69 74 65 20 43 68  UES(1, 'White Ch
aa90: 72 69 73 74 6d 61 73 27 2c 20 35 29 3b 0a 20 20  ristmas', 5);.  
aaa0: 7d 0a 20 20 63 61 74 63 68 73 71 6c 20 43 4f 4d  }.  catchsql COM
aab0: 4d 49 54 0a 7d 20 7b 31 20 7b 66 6f 72 65 69 67  MIT.} {1 {foreig
aac0: 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74  n key constraint
aad0: 20 66 61 69 6c 65 64 7d 7d 0a 64 6f 5f 74 65 73   failed}}.do_tes
aae0: 74 20 65 5f 66 6b 65 79 2d 32 38 2e 33 20 7b 0a  t e_fkey-28.3 {.
aaf0: 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20    execsql {.    
ab00: 49 4e 53 45 52 54 20 49 4e 54 4f 20 61 72 74 69  INSERT INTO arti
ab10: 73 74 20 56 41 4c 55 45 53 28 35 2c 20 27 42 69  st VALUES(5, 'Bi
ab20: 6e 67 20 43 72 6f 73 62 79 27 29 3b 0a 20 20 20  ng Crosby');.   
ab30: 20 43 4f 4d 4d 49 54 3b 0a 20 20 7d 0a 7d 20 7b   COMMIT;.  }.} {
ab40: 7d 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  }..#------------
ab50: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
ab60: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
ab70: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
ab80: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20  -------------.# 
ab90: 2f 2a 20 45 56 3a 20 52 2d 30 37 32 32 33 2d 34  /* EV: R-07223-4
aba0: 38 33 32 33 20 2a 2f 0a 23 0a 23 20 56 65 72 69  8323 */.#.# Veri
abb0: 66 79 20 74 68 61 74 20 61 20 6e 65 73 74 65 64  fy that a nested
abc0: 20 73 61 76 65 70 6f 69 6e 74 20 6d 61 79 20 62   savepoint may b
abd0: 65 20 72 65 6c 65 61 73 65 64 20 77 69 74 68 6f  e released witho
abe0: 75 74 20 73 61 74 69 73 66 79 69 6e 67 20 0a 23  ut satisfying .#
abf0: 20 64 65 66 65 72 72 65 64 20 66 6f 72 65 69 67   deferred foreig
ac00: 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74  n key constraint
ac10: 73 2e 0a 23 0a 64 72 6f 70 5f 61 6c 6c 5f 74 61  s..#.drop_all_ta
ac20: 62 6c 65 73 0a 64 6f 5f 74 65 73 74 20 65 5f 66  bles.do_test e_f
ac30: 6b 65 79 2d 33 30 2e 31 20 7b 0a 20 20 65 78 65  key-30.1 {.  exe
ac40: 63 73 71 6c 20 7b 0a 20 20 20 20 43 52 45 41 54  csql {.    CREAT
ac50: 45 20 54 41 42 4c 45 20 74 31 28 61 20 50 52 49  E TABLE t1(a PRI
ac60: 4d 41 52 59 20 4b 45 59 2c 0a 20 20 20 20 20 20  MARY KEY,.      
ac70: 62 20 52 45 46 45 52 45 4e 43 45 53 20 74 31 20  b REFERENCES t1 
ac80: 44 45 46 45 52 52 41 42 4c 45 20 49 4e 49 54 49  DEFERRABLE INITI
ac90: 41 4c 4c 59 20 44 45 46 45 52 52 45 44 0a 20 20  ALLY DEFERRED.  
aca0: 20 20 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20    );.    INSERT 
acb0: 49 4e 54 4f 20 74 31 20 56 41 4c 55 45 53 28 31  INTO t1 VALUES(1
acc0: 2c 20 31 29 3b 0a 20 20 20 20 49 4e 53 45 52 54  , 1);.    INSERT
acd0: 20 49 4e 54 4f 20 74 31 20 56 41 4c 55 45 53 28   INTO t1 VALUES(
ace0: 32 2c 20 32 29 3b 0a 20 20 20 20 49 4e 53 45 52  2, 2);.    INSER
acf0: 54 20 49 4e 54 4f 20 74 31 20 56 41 4c 55 45 53  T INTO t1 VALUES
ad00: 28 33 2c 20 33 29 3b 0a 20 20 7d 0a 7d 20 7b 7d  (3, 3);.  }.} {}
ad10: 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d  .do_test e_fkey-
ad20: 33 30 2e 32 20 7b 0a 20 20 65 78 65 63 73 71 6c  30.2 {.  execsql
ad30: 20 7b 0a 20 20 20 20 42 45 47 49 4e 3b 0a 20 20   {.    BEGIN;.  
ad40: 20 20 20 20 53 41 56 45 50 4f 49 4e 54 20 6f 6e      SAVEPOINT on
ad50: 65 3b 0a 20 20 20 20 20 20 20 20 49 4e 53 45 52  e;.        INSER
ad60: 54 20 49 4e 54 4f 20 74 31 20 56 41 4c 55 45 53  T INTO t1 VALUES
ad70: 28 34 2c 20 35 29 3b 0a 20 20 20 20 20 20 52 45  (4, 5);.      RE
ad80: 4c 45 41 53 45 20 6f 6e 65 3b 0a 20 20 7d 0a 7d  LEASE one;.  }.}
ad90: 20 7b 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b   {}.do_test e_fk
ada0: 65 79 2d 33 30 2e 33 20 7b 0a 20 20 63 61 74 63  ey-30.3 {.  catc
adb0: 68 73 71 6c 20 43 4f 4d 4d 49 54 0a 7d 20 7b 31  hsql COMMIT.} {1
adc0: 20 7b 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f   {foreign key co
add0: 6e 73 74 72 61 69 6e 74 20 66 61 69 6c 65 64 7d  nstraint failed}
ade0: 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79  }.do_test e_fkey
adf0: 2d 33 30 2e 34 20 7b 0a 20 20 65 78 65 63 73 71  -30.4 {.  execsq
ae00: 6c 20 7b 0a 20 20 20 20 55 50 44 41 54 45 20 74  l {.    UPDATE t
ae10: 31 20 53 45 54 20 61 20 3d 20 35 20 57 48 45 52  1 SET a = 5 WHER
ae20: 45 20 61 20 3d 20 34 3b 0a 20 20 20 20 43 4f 4d  E a = 4;.    COM
ae30: 4d 49 54 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a 0a 0a  MIT;.  }.} {}...
ae40: 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  #---------------
ae50: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
ae60: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
ae70: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
ae80: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20 2f 2a 20  ----------.# /* 
ae90: 45 56 3a 20 52 2d 34 34 32 39 35 2d 31 33 38 32  EV: R-44295-1382
aea0: 33 20 2a 2f 0a 23 0a 23 20 43 68 65 63 6b 20 74  3 */.#.# Check t
aeb0: 68 61 74 20 61 20 74 72 61 6e 73 61 63 74 69 6f  hat a transactio
aec0: 6e 20 73 61 76 65 70 6f 69 6e 74 20 28 61 6e 20  n savepoint (an 
aed0: 6f 75 74 65 72 6d 6f 73 74 20 73 61 76 65 70 6f  outermost savepo
aee0: 69 6e 74 20 6f 70 65 6e 65 64 20 77 68 65 6e 0a  int opened when.
aef0: 23 20 74 68 65 20 64 61 74 61 62 61 73 65 20 77  # the database w
af00: 61 73 20 69 6e 20 61 75 74 6f 2d 63 6f 6d 6d 69  as in auto-commi
af10: 74 20 6d 6f 64 65 29 20 63 61 6e 6e 6f 74 20 62  t mode) cannot b
af20: 65 20 72 65 6c 65 61 73 65 64 20 77 69 74 68 6f  e released witho
af30: 75 74 0a 23 20 73 61 74 69 73 66 79 69 6e 67 20  ut.# satisfying 
af40: 64 65 66 65 72 72 65 64 20 66 6f 72 65 69 67 6e  deferred foreign
af50: 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 73   key constraints
af60: 2e 20 49 74 20 6d 61 79 20 62 65 20 72 6f 6c 6c  . It may be roll
af70: 65 64 20 62 61 63 6b 2e 0a 23 0a 64 6f 5f 74 65  ed back..#.do_te
af80: 73 74 20 65 5f 66 6b 65 79 2d 33 31 2e 31 20 7b  st e_fkey-31.1 {
af90: 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20  .  execsql {.   
afa0: 20 53 41 56 45 50 4f 49 4e 54 20 6f 6e 65 3b 0a   SAVEPOINT one;.
afb0: 20 20 20 20 20 20 53 41 56 45 50 4f 49 4e 54 20        SAVEPOINT 
afc0: 74 77 6f 3b 0a 20 20 20 20 20 20 20 20 49 4e 53  two;.        INS
afd0: 45 52 54 20 49 4e 54 4f 20 74 31 20 56 41 4c 55  ERT INTO t1 VALU
afe0: 45 53 28 36 2c 20 37 29 3b 0a 20 20 20 20 20 20  ES(6, 7);.      
aff0: 52 45 4c 45 41 53 45 20 74 77 6f 3b 0a 20 20 7d  RELEASE two;.  }
b000: 0a 7d 20 7b 7d 0a 64 6f 5f 74 65 73 74 20 65 5f  .} {}.do_test e_
b010: 66 6b 65 79 2d 33 31 2e 32 20 7b 0a 20 20 63 61  fkey-31.2 {.  ca
b020: 74 63 68 73 71 6c 20 7b 52 45 4c 45 41 53 45 20  tchsql {RELEASE 
b030: 6f 6e 65 7d 0a 7d 20 7b 31 20 7b 66 6f 72 65 69  one}.} {1 {forei
b040: 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e  gn key constrain
b050: 74 20 66 61 69 6c 65 64 7d 7d 0a 64 6f 5f 74 65  t failed}}.do_te
b060: 73 74 20 65 5f 66 6b 65 79 2d 33 31 2e 33 20 7b  st e_fkey-31.3 {
b070: 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20  .  execsql {.   
b080: 20 20 20 55 50 44 41 54 45 20 74 31 20 53 45 54     UPDATE t1 SET
b090: 20 61 20 3d 20 37 20 57 48 45 52 45 20 61 20 3d   a = 7 WHERE a =
b0a0: 20 36 3b 0a 20 20 20 20 52 45 4c 45 41 53 45 20   6;.    RELEASE 
b0b0: 6f 6e 65 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a 64 6f  one;.  }.} {}.do
b0c0: 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 33 31 2e  _test e_fkey-31.
b0d0: 34 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a  4 {.  execsql {.
b0e0: 20 20 20 20 53 41 56 45 50 4f 49 4e 54 20 6f 6e      SAVEPOINT on
b0f0: 65 3b 0a 20 20 20 20 20 20 53 41 56 45 50 4f 49  e;.      SAVEPOI
b100: 4e 54 20 74 77 6f 3b 0a 20 20 20 20 20 20 20 20  NT two;.        
b110: 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 31 20 56  INSERT INTO t1 V
b120: 41 4c 55 45 53 28 39 2c 20 31 30 29 3b 0a 20 20  ALUES(9, 10);.  
b130: 20 20 20 20 52 45 4c 45 41 53 45 20 74 77 6f 3b      RELEASE two;
b140: 0a 20 20 7d 0a 7d 20 7b 7d 0a 64 6f 5f 74 65 73  .  }.} {}.do_tes
b150: 74 20 65 5f 66 6b 65 79 2d 33 31 2e 35 20 7b 0a  t e_fkey-31.5 {.
b160: 20 20 63 61 74 63 68 73 71 6c 20 7b 52 45 4c 45    catchsql {RELE
b170: 41 53 45 20 6f 6e 65 7d 0a 7d 20 7b 31 20 7b 66  ASE one}.} {1 {f
b180: 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74  oreign key const
b190: 72 61 69 6e 74 20 66 61 69 6c 65 64 7d 7d 0a 64  raint failed}}.d
b1a0: 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 33 31  o_test e_fkey-31
b1b0: 2e 36 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b  .6 {.  execsql {
b1c0: 52 4f 4c 4c 42 41 43 4b 20 54 4f 20 6f 6e 65 20  ROLLBACK TO one 
b1d0: 3b 20 52 45 4c 45 41 53 45 20 6f 6e 65 7d 0a 7d  ; RELEASE one}.}
b1e0: 20 7b 7d 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d   {}..#----------
b1f0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
b200: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
b210: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
b220: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a  ---------------.
b230: 23 20 2f 2a 20 45 56 3a 20 52 2d 33 37 37 33 36  # /* EV: R-37736
b240: 2d 34 32 36 31 36 20 2a 2f 0a 23 0a 23 20 54 65  -42616 */.#.# Te
b250: 73 74 20 74 68 61 74 20 69 66 20 61 20 43 4f 4d  st that if a COM
b260: 4d 49 54 20 6f 70 65 72 61 74 69 6f 6e 20 66 61  MIT operation fa
b270: 69 6c 73 20 64 75 65 20 74 6f 20 64 65 66 65 72  ils due to defer
b280: 72 65 64 20 66 6f 72 65 69 67 6e 20 6b 65 79 20  red foreign key 
b290: 0a 23 20 63 6f 6e 73 74 72 61 69 6e 74 73 2c 20  .# constraints, 
b2a0: 61 6e 79 20 6e 65 73 74 65 64 20 73 61 76 65 70  any nested savep
b2b0: 6f 69 6e 74 73 20 72 65 6d 61 69 6e 20 6f 70 65  oints remain ope
b2c0: 6e 2e 0a 23 0a 64 6f 5f 74 65 73 74 20 65 5f 66  n..#.do_test e_f
b2d0: 6b 65 79 2d 33 32 2e 31 20 7b 0a 20 20 65 78 65  key-32.1 {.  exe
b2e0: 63 73 71 6c 20 7b 0a 20 20 20 20 44 45 4c 45 54  csql {.    DELET
b2f0: 45 20 46 52 4f 4d 20 74 31 20 57 48 45 52 45 20  E FROM t1 WHERE 
b300: 61 3e 33 3b 0a 20 20 20 20 53 45 4c 45 43 54 20  a>3;.    SELECT 
b310: 2a 20 46 52 4f 4d 20 74 31 3b 0a 20 20 7d 0a 7d  * FROM t1;.  }.}
b320: 20 7b 31 20 31 20 32 20 32 20 33 20 33 7d 0a 64   {1 1 2 2 3 3}.d
b330: 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 33 32  o_test e_fkey-32
b340: 2e 32 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b  .2 {.  execsql {
b350: 0a 20 20 20 20 42 45 47 49 4e 3b 0a 20 20 20 20  .    BEGIN;.    
b360: 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 31    INSERT INTO t1
b370: 20 56 41 4c 55 45 53 28 34 2c 20 34 29 3b 0a 20   VALUES(4, 4);. 
b380: 20 20 20 20 20 53 41 56 45 50 4f 49 4e 54 20 6f       SAVEPOINT o
b390: 6e 65 3b 0a 20 20 20 20 20 20 20 20 49 4e 53 45  ne;.        INSE
b3a0: 52 54 20 49 4e 54 4f 20 74 31 20 56 41 4c 55 45  RT INTO t1 VALUE
b3b0: 53 28 35 2c 20 36 29 3b 0a 20 20 20 20 20 20 20  S(5, 6);.       
b3c0: 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74   SELECT * FROM t
b3d0: 31 3b 0a 20 20 7d 0a 7d 20 7b 31 20 31 20 32 20  1;.  }.} {1 1 2 
b3e0: 32 20 33 20 33 20 34 20 34 20 35 20 36 7d 0a 64  2 3 3 4 4 5 6}.d
b3f0: 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 33 32  o_test e_fkey-32
b400: 2e 33 20 7b 0a 20 20 63 61 74 63 68 73 71 6c 20  .3 {.  catchsql 
b410: 43 4f 4d 4d 49 54 0a 7d 20 7b 31 20 7b 66 6f 72  COMMIT.} {1 {for
b420: 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61  eign key constra
b430: 69 6e 74 20 66 61 69 6c 65 64 7d 7d 0a 64 6f 5f  int failed}}.do_
b440: 74 65 73 74 20 65 5f 66 6b 65 79 2d 33 32 2e 34  test e_fkey-32.4
b450: 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20   {.  execsql {. 
b460: 20 20 20 52 4f 4c 4c 42 41 43 4b 20 54 4f 20 6f     ROLLBACK TO o
b470: 6e 65 3b 0a 20 20 20 20 43 4f 4d 4d 49 54 3b 0a  ne;.    COMMIT;.
b480: 20 20 20 20 53 45 4c 45 43 54 20 2a 20 46 52 4f      SELECT * FRO
b490: 4d 20 74 31 3b 0a 20 20 7d 0a 7d 20 7b 31 20 31  M t1;.  }.} {1 1
b4a0: 20 32 20 32 20 33 20 33 20 34 20 34 7d 0a 0a 64   2 2 3 3 4 4}..d
b4b0: 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 33 32  o_test e_fkey-32
b4c0: 2e 35 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b  .5 {.  execsql {
b4d0: 0a 20 20 20 20 53 41 56 45 50 4f 49 4e 54 20 61  .    SAVEPOINT a
b4e0: 3b 0a 20 20 20 20 20 20 49 4e 53 45 52 54 20 49  ;.      INSERT I
b4f0: 4e 54 4f 20 74 31 20 56 41 4c 55 45 53 28 35 2c  NTO t1 VALUES(5,
b500: 20 35 29 3b 0a 20 20 20 20 20 20 53 41 56 45 50   5);.      SAVEP
b510: 4f 49 4e 54 20 62 3b 0a 20 20 20 20 20 20 20 20  OINT b;.        
b520: 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 31 20 56  INSERT INTO t1 V
b530: 41 4c 55 45 53 28 36 2c 20 37 29 3b 0a 20 20 20  ALUES(6, 7);.   
b540: 20 20 20 20 20 53 41 56 45 50 4f 49 4e 54 20 63       SAVEPOINT c
b550: 3b 0a 20 20 20 20 20 20 20 20 20 20 49 4e 53 45  ;.          INSE
b560: 52 54 20 49 4e 54 4f 20 74 31 20 56 41 4c 55 45  RT INTO t1 VALUE
b570: 53 28 37 2c 20 38 29 3b 0a 20 20 7d 0a 7d 20 7b  S(7, 8);.  }.} {
b580: 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79  }.do_test e_fkey
b590: 2d 33 32 2e 36 20 7b 0a 20 20 63 61 74 63 68 73  -32.6 {.  catchs
b5a0: 71 6c 20 7b 52 45 4c 45 41 53 45 20 61 7d 0a 7d  ql {RELEASE a}.}
b5b0: 20 7b 31 20 7b 66 6f 72 65 69 67 6e 20 6b 65 79   {1 {foreign key
b5c0: 20 63 6f 6e 73 74 72 61 69 6e 74 20 66 61 69 6c   constraint fail
b5d0: 65 64 7d 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66  ed}}.do_test e_f
b5e0: 6b 65 79 2d 33 32 2e 37 20 7b 0a 20 20 65 78 65  key-32.7 {.  exe
b5f0: 63 73 71 6c 20 20 7b 52 4f 4c 4c 42 41 43 4b 20  csql  {ROLLBACK 
b600: 54 4f 20 63 7d 0a 20 20 63 61 74 63 68 73 71 6c  TO c}.  catchsql
b610: 20 7b 52 45 4c 45 41 53 45 20 61 7d 0a 7d 20 7b   {RELEASE a}.} {
b620: 31 20 7b 66 6f 72 65 69 67 6e 20 6b 65 79 20 63  1 {foreign key c
b630: 6f 6e 73 74 72 61 69 6e 74 20 66 61 69 6c 65 64  onstraint failed
b640: 7d 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65  }}.do_test e_fke
b650: 79 2d 33 32 2e 38 20 7b 0a 20 20 65 78 65 63 73  y-32.8 {.  execs
b660: 71 6c 20 20 7b 0a 20 20 20 20 52 4f 4c 4c 42 41  ql  {.    ROLLBA
b670: 43 4b 20 54 4f 20 62 3b 0a 20 20 20 20 52 45 4c  CK TO b;.    REL
b680: 45 41 53 45 20 61 3b 0a 20 20 20 20 53 45 4c 45  EASE a;.    SELE
b690: 43 54 20 2a 20 46 52 4f 4d 20 74 31 3b 0a 20 20  CT * FROM t1;.  
b6a0: 7d 0a 7d 20 7b 31 20 31 20 32 20 32 20 33 20 33  }.} {1 1 2 2 3 3
b6b0: 20 34 20 34 20 35 20 35 7d 0a 0a 23 23 23 23 23   4 4 5 5}..#####
b6c0: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
b6d0: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
b6e0: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
b6f0: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
b700: 23 23 23 23 23 23 0a 23 23 23 20 53 45 43 54 49  ######.### SECTI
b710: 4f 4e 20 34 2e 33 3a 20 4f 4e 20 44 45 4c 45 54  ON 4.3: ON DELET
b720: 45 20 61 6e 64 20 4f 4e 20 55 50 44 41 54 45 20  E and ON UPDATE 
b730: 41 63 74 69 6f 6e 73 0a 23 23 23 23 23 23 23 23  Actions.########
b740: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
b750: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
b760: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
b770: 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
b780: 23 23 23 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ###..#----------
b790: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
b7a0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
b7b0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
b7c0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a  ---------------.
b7d0: 23 20 2f 2a 20 45 56 3a 20 52 2d 34 38 32 37 30  # /* EV: R-48270
b7e0: 2d 34 34 32 38 32 20 2a 2f 0a 23 0a 23 20 54 65  -44282 */.#.# Te
b7f0: 73 74 20 74 68 61 74 20 63 6f 6e 66 69 67 75 72  st that configur
b800: 65 64 20 4f 4e 20 44 45 4c 45 54 45 20 61 6e 64  ed ON DELETE and
b810: 20 4f 4e 20 55 50 44 41 54 45 20 61 63 74 69 6f   ON UPDATE actio
b820: 6e 73 20 74 61 6b 65 20 70 6c 61 63 65 20 77 68  ns take place wh
b830: 65 6e 0a 23 20 64 65 6c 65 74 69 6e 67 20 6f 72  en.# deleting or
b840: 20 6d 6f 64 69 66 79 69 6e 67 20 72 6f 77 73 20   modifying rows 
b850: 6f 66 20 74 68 65 20 70 61 72 65 6e 74 20 74 61  of the parent ta
b860: 62 6c 65 2c 20 72 65 73 70 65 63 74 69 76 65 6c  ble, respectivel
b870: 79 2e 0a 23 0a 23 20 2f 2a 20 45 56 3a 20 52 2d  y..#.# /* EV: R-
b880: 34 38 31 32 34 2d 36 33 32 32 35 20 2a 2f 0a 23  48124-63225 */.#
b890: 0a 23 20 54 65 73 74 20 74 68 61 74 20 61 20 73  .# Test that a s
b8a0: 69 6e 67 6c 65 20 46 4b 20 63 6f 6e 73 74 72 61  ingle FK constra
b8b0: 69 6e 74 20 6d 61 79 20 68 61 76 65 20 64 69 66  int may have dif
b8c0: 66 65 72 65 6e 74 20 61 63 74 69 6f 6e 73 20 63  ferent actions c
b8d0: 6f 6e 66 69 67 75 72 65 64 0a 23 20 66 6f 72 20  onfigured.# for 
b8e0: 4f 4e 20 44 45 4c 45 54 45 20 61 6e 64 20 4f 4e  ON DELETE and ON
b8f0: 20 55 50 44 41 54 45 2e 0a 23 0a 64 6f 5f 74 65   UPDATE..#.do_te
b900: 73 74 20 65 5f 66 6b 65 79 2d 31 36 2e 31 20 7b  st e_fkey-16.1 {
b910: 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20  .  execsql {.   
b920: 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 70 28   CREATE TABLE p(
b930: 61 2c 20 62 20 50 52 49 4d 41 52 59 20 4b 45 59  a, b PRIMARY KEY
b940: 2c 20 63 29 3b 0a 20 20 20 20 43 52 45 41 54 45  , c);.    CREATE
b950: 20 54 41 42 4c 45 20 63 31 28 64 2c 20 65 2c 20   TABLE c1(d, e, 
b960: 66 20 44 45 46 41 55 4c 54 20 27 6b 30 27 20 52  f DEFAULT 'k0' R
b970: 45 46 45 52 45 4e 43 45 53 20 70 20 0a 20 20 20  EFERENCES p .   
b980: 20 20 20 4f 4e 20 55 50 44 41 54 45 20 53 45 54     ON UPDATE SET
b990: 20 44 45 46 41 55 4c 54 0a 20 20 20 20 20 20 4f   DEFAULT.      O
b9a0: 4e 20 44 45 4c 45 54 45 20 53 45 54 20 4e 55 4c  N DELETE SET NUL
b9b0: 4c 0a 20 20 20 20 29 3b 0a 0a 20 20 20 20 49 4e  L.    );..    IN
b9c0: 53 45 52 54 20 49 4e 54 4f 20 70 20 56 41 4c 55  SERT INTO p VALU
b9d0: 45 53 28 30 2c 20 27 6b 30 27 2c 20 27 27 29 3b  ES(0, 'k0', '');
b9e0: 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f  .    INSERT INTO
b9f0: 20 70 20 56 41 4c 55 45 53 28 31 2c 20 27 6b 31   p VALUES(1, 'k1
ba00: 27 2c 20 27 49 27 29 3b 0a 20 20 20 20 49 4e 53  ', 'I');.    INS
ba10: 45 52 54 20 49 4e 54 4f 20 70 20 56 41 4c 55 45  ERT INTO p VALUE
ba20: 53 28 32 2c 20 27 6b 32 27 2c 20 27 49 49 27 29  S(2, 'k2', 'II')
ba30: 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54  ;.    INSERT INT
ba40: 4f 20 70 20 56 41 4c 55 45 53 28 33 2c 20 27 6b  O p VALUES(3, 'k
ba50: 33 27 2c 20 27 49 49 49 27 29 3b 0a 0a 20 20 20  3', 'III');..   
ba60: 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 63 31 20   INSERT INTO c1 
ba70: 56 41 4c 55 45 53 28 31 2c 20 27 78 78 27 2c 20  VALUES(1, 'xx', 
ba80: 27 6b 31 27 29 3b 0a 20 20 20 20 49 4e 53 45 52  'k1');.    INSER
ba90: 54 20 49 4e 54 4f 20 63 31 20 56 41 4c 55 45 53  T INTO c1 VALUES
baa0: 28 32 2c 20 27 78 78 27 2c 20 27 6b 32 27 29 3b  (2, 'xx', 'k2');
bab0: 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f  .    INSERT INTO
bac0: 20 63 31 20 56 41 4c 55 45 53 28 33 2c 20 27 78   c1 VALUES(3, 'x
bad0: 78 27 2c 20 27 6b 33 27 29 3b 0a 20 20 7d 0a 7d  x', 'k3');.  }.}
bae0: 20 7b 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b   {}.do_test e_fk
baf0: 65 79 2d 31 36 2e 32 20 7b 0a 20 20 65 78 65 63  ey-16.2 {.  exec
bb00: 73 71 6c 20 7b 0a 20 20 20 20 55 50 44 41 54 45  sql {.    UPDATE
bb10: 20 70 20 53 45 54 20 62 20 3d 20 27 6b 34 27 20   p SET b = 'k4' 
bb20: 57 48 45 52 45 20 61 20 3d 20 31 3b 0a 20 20 20  WHERE a = 1;.   
bb30: 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 63   SELECT * FROM c
bb40: 31 3b 0a 20 20 7d 0a 7d 20 7b 31 20 78 78 20 6b  1;.  }.} {1 xx k
bb50: 30 20 32 20 78 78 20 6b 32 20 33 20 78 78 20 6b  0 2 xx k2 3 xx k
bb60: 33 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65  3}.do_test e_fke
bb70: 79 2d 31 36 2e 33 20 7b 0a 20 20 65 78 65 63 73  y-16.3 {.  execs
bb80: 71 6c 20 7b 0a 20 20 20 20 44 45 4c 45 54 45 20  ql {.    DELETE 
bb90: 46 52 4f 4d 20 70 20 57 48 45 52 45 20 61 20 3d  FROM p WHERE a =
bba0: 20 32 3b 0a 20 20 20 20 53 45 4c 45 43 54 20 2a   2;.    SELECT *
bbb0: 20 46 52 4f 4d 20 63 31 3b 0a 20 20 7d 0a 7d 20   FROM c1;.  }.} 
bbc0: 7b 31 20 78 78 20 6b 30 20 32 20 78 78 20 7b 7d  {1 xx k0 2 xx {}
bbd0: 20 33 20 78 78 20 6b 33 7d 0a 64 6f 5f 74 65 73   3 xx k3}.do_tes
bbe0: 74 20 65 5f 66 6b 65 79 2d 31 36 2e 34 20 7b 0a  t e_fkey-16.4 {.
bbf0: 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20    execsql {.    
bc00: 43 52 45 41 54 45 20 55 4e 49 51 55 45 20 49 4e  CREATE UNIQUE IN
bc10: 44 45 58 20 70 69 20 4f 4e 20 70 28 63 29 3b 0a  DEX pi ON p(c);.
bc20: 20 20 20 20 52 45 50 4c 41 43 45 20 49 4e 54 4f      REPLACE INTO
bc30: 20 70 20 56 41 4c 55 45 53 28 35 2c 20 27 6b 35   p VALUES(5, 'k5
bc40: 27 2c 20 27 49 49 49 27 29 3b 0a 20 20 20 20 53  ', 'III');.    S
bc50: 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 63 31 3b  ELECT * FROM c1;
bc60: 0a 20 20 7d 0a 7d 20 7b 31 20 78 78 20 6b 30 20  .  }.} {1 xx k0 
bc70: 32 20 78 78 20 7b 7d 20 33 20 78 78 20 7b 7d 7d  2 xx {} 3 xx {}}
bc80: 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ..#-------------
bc90: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
bca0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
bcb0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
bcc0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20 2f  ------------.# /
bcd0: 2a 20 45 56 3a 20 52 2d 33 33 33 32 36 2d 34 35  * EV: R-33326-45
bce0: 32 35 32 20 2a 2f 0a 23 0a 23 20 45 61 63 68 20  252 */.#.# Each 
bcf0: 66 6f 72 65 69 67 6e 20 6b 65 79 20 69 6e 20 74  foreign key in t
bd00: 68 65 20 73 79 73 74 65 6d 20 68 61 73 20 61 6e  he system has an
bd10: 20 4f 4e 20 55 50 44 41 54 45 20 61 6e 64 20 4f   ON UPDATE and O
bd20: 4e 20 44 45 4c 45 54 45 20 61 63 74 69 6f 6e 2c  N DELETE action,
bd30: 0a 23 20 65 69 74 68 65 72 20 22 4e 4f 20 41 43  .# either "NO AC
bd40: 54 49 4f 4e 22 2c 20 22 52 45 53 54 52 49 43 54  TION", "RESTRICT
bd50: 22 2c 20 22 53 45 54 20 4e 55 4c 4c 22 2c 20 22  ", "SET NULL", "
bd60: 53 45 54 20 44 45 46 41 55 4c 54 22 20 6f 72 20  SET DEFAULT" or 
bd70: 22 43 41 53 43 41 44 45 22 2e 0a 23 0a 23 20 2f  "CASCADE"..#.# /
bd80: 2a 20 45 56 3a 20 52 2d 31 39 38 30 33 2d 34 35  * EV: R-19803-45
bd90: 38 38 34 20 2a 2f 0a 23 0a 23 20 49 66 20 6e 6f  884 */.#.# If no
bda0: 6e 65 20 69 73 20 73 70 65 63 69 66 69 65 64 20  ne is specified 
bdb0: 65 78 70 6c 69 63 69 74 6c 79 2c 20 22 4e 4f 20  explicitly, "NO 
bdc0: 41 43 54 49 4f 4e 22 20 69 73 20 74 68 65 20 64  ACTION" is the d
bdd0: 65 66 61 75 6c 74 2e 0a 23 20 0a 64 72 6f 70 5f  efault..# .drop_
bde0: 61 6c 6c 5f 74 61 62 6c 65 73 0a 64 6f 5f 74 65  all_tables.do_te
bdf0: 73 74 20 65 5f 66 6b 65 79 2d 31 37 2e 31 20 7b  st e_fkey-17.1 {
be00: 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20  .  execsql {.   
be10: 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 70 61   CREATE TABLE pa
be20: 72 65 6e 74 28 78 20 50 52 49 4d 41 52 59 20 4b  rent(x PRIMARY K
be30: 45 59 2c 20 79 29 3b 0a 20 20 20 20 43 52 45 41  EY, y);.    CREA
be40: 54 45 20 54 41 42 4c 45 20 63 68 69 6c 64 31 28  TE TABLE child1(
be50: 61 2c 20 0a 20 20 20 20 20 20 62 20 52 45 46 45  a, .      b REFE
be60: 52 45 4e 43 45 53 20 70 61 72 65 6e 74 20 4f 4e  RENCES parent ON
be70: 20 55 50 44 41 54 45 20 4e 4f 20 41 43 54 49 4f   UPDATE NO ACTIO
be80: 4e 20 4f 4e 20 44 45 4c 45 54 45 20 52 45 53 54  N ON DELETE REST
be90: 52 49 43 54 0a 20 20 20 20 29 3b 0a 20 20 20 20  RICT.    );.    
bea0: 43 52 45 41 54 45 20 54 41 42 4c 45 20 63 68 69  CREATE TABLE chi
beb0: 6c 64 32 28 61 2c 20 0a 20 20 20 20 20 20 62 20  ld2(a, .      b 
bec0: 52 45 46 45 52 45 4e 43 45 53 20 70 61 72 65 6e  REFERENCES paren
bed0: 74 20 4f 4e 20 55 50 44 41 54 45 20 52 45 53 54  t ON UPDATE REST
bee0: 52 49 43 54 20 4f 4e 20 44 45 4c 45 54 45 20 53  RICT ON DELETE S
bef0: 45 54 20 4e 55 4c 4c 0a 20 20 20 20 29 3b 0a 20  ET NULL.    );. 
bf00: 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20     CREATE TABLE 
bf10: 63 68 69 6c 64 33 28 61 2c 20 0a 20 20 20 20 20  child3(a, .     
bf20: 20 62 20 52 45 46 45 52 45 4e 43 45 53 20 70 61   b REFERENCES pa
bf30: 72 65 6e 74 20 4f 4e 20 55 50 44 41 54 45 20 53  rent ON UPDATE S
bf40: 45 54 20 4e 55 4c 4c 20 4f 4e 20 44 45 4c 45 54  ET NULL ON DELET
bf50: 45 20 53 45 54 20 44 45 46 41 55 4c 54 0a 20 20  E SET DEFAULT.  
bf60: 20 20 29 3b 0a 20 20 20 20 43 52 45 41 54 45 20    );.    CREATE 
bf70: 54 41 42 4c 45 20 63 68 69 6c 64 34 28 61 2c 20  TABLE child4(a, 
bf80: 0a 20 20 20 20 20 20 62 20 52 45 46 45 52 45 4e  .      b REFEREN
bf90: 43 45 53 20 70 61 72 65 6e 74 20 4f 4e 20 55 50  CES parent ON UP
bfa0: 44 41 54 45 20 53 45 54 20 44 45 46 41 55 4c 54  DATE SET DEFAULT
bfb0: 20 4f 4e 20 44 45 4c 45 54 45 20 43 41 53 43 41   ON DELETE CASCA
bfc0: 44 45 0a 20 20 20 20 29 3b 0a 0a 20 20 20 20 2d  DE.    );..    -
bfd0: 2d 20 43 72 65 61 74 65 20 73 6f 6d 65 20 66 6f  - Create some fo
bfe0: 72 65 69 67 6e 20 6b 65 79 73 20 74 68 61 74 20  reign keys that 
bff0: 75 73 65 20 74 68 65 20 64 65 66 61 75 6c 74 20  use the default 
c000: 61 63 74 69 6f 6e 20 2d 20 22 4e 4f 20 41 43 54  action - "NO ACT
c010: 49 4f 4e 22 0a 20 20 20 20 43 52 45 41 54 45 20  ION".    CREATE 
c020: 54 41 42 4c 45 20 63 68 69 6c 64 35 28 61 2c 20  TABLE child5(a, 
c030: 62 20 52 45 46 45 52 45 4e 43 45 53 20 70 61 72  b REFERENCES par
c040: 65 6e 74 20 4f 4e 20 55 50 44 41 54 45 20 43 41  ent ON UPDATE CA
c050: 53 43 41 44 45 29 3b 0a 20 20 20 20 43 52 45 41  SCADE);.    CREA
c060: 54 45 20 54 41 42 4c 45 20 63 68 69 6c 64 36 28  TE TABLE child6(
c070: 61 2c 20 62 20 52 45 46 45 52 45 4e 43 45 53 20  a, b REFERENCES 
c080: 70 61 72 65 6e 74 20 4f 4e 20 44 45 4c 45 54 45  parent ON DELETE
c090: 20 52 45 53 54 52 49 43 54 29 3b 0a 20 20 20 20   RESTRICT);.    
c0a0: 43 52 45 41 54 45 20 54 41 42 4c 45 20 63 68 69  CREATE TABLE chi
c0b0: 6c 64 37 28 61 2c 20 62 20 52 45 46 45 52 45 4e  ld7(a, b REFEREN
c0c0: 43 45 53 20 70 61 72 65 6e 74 20 4f 4e 20 44 45  CES parent ON DE
c0d0: 4c 45 54 45 20 4e 4f 20 41 43 54 49 4f 4e 29 3b  LETE NO ACTION);
c0e0: 0a 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c  .    CREATE TABL
c0f0: 45 20 63 68 69 6c 64 38 28 61 2c 20 62 20 52 45  E child8(a, b RE
c100: 46 45 52 45 4e 43 45 53 20 70 61 72 65 6e 74 20  FERENCES parent 
c110: 4f 4e 20 55 50 44 41 54 45 20 4e 4f 20 41 43 54  ON UPDATE NO ACT
c120: 49 4f 4e 29 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a 0a  ION);.  }.} {}..
c130: 66 6f 72 65 61 63 68 20 7b 74 6e 20 7a 54 61 62  foreach {tn zTab
c140: 20 6c 52 65 73 7d 20 7b 0a 20 20 32 20 63 68 69   lRes} {.  2 chi
c150: 6c 64 31 20 7b 30 20 30 20 70 61 72 65 6e 74 20  ld1 {0 0 parent 
c160: 62 20 7b 7d 20 7b 4e 4f 20 41 43 54 49 4f 4e 7d  b {} {NO ACTION}
c170: 20 52 45 53 54 52 49 43 54 20 4e 4f 4e 45 7d 0a   RESTRICT NONE}.
c180: 20 20 33 20 63 68 69 6c 64 32 20 7b 30 20 30 20    3 child2 {0 0 
c190: 70 61 72 65 6e 74 20 62 20 7b 7d 20 52 45 53 54  parent b {} REST
c1a0: 52 49 43 54 20 7b 53 45 54 20 4e 55 4c 4c 7d 20  RICT {SET NULL} 
c1b0: 4e 4f 4e 45 7d 0a 20 20 34 20 63 68 69 6c 64 33  NONE}.  4 child3
c1c0: 20 7b 30 20 30 20 70 61 72 65 6e 74 20 62 20 7b   {0 0 parent b {
c1d0: 7d 20 7b 53 45 54 20 4e 55 4c 4c 7d 20 7b 53 45  } {SET NULL} {SE
c1e0: 54 20 44 45 46 41 55 4c 54 7d 20 4e 4f 4e 45 7d  T DEFAULT} NONE}
c1f0: 0a 20 20 35 20 63 68 69 6c 64 34 20 7b 30 20 30  .  5 child4 {0 0
c200: 20 70 61 72 65 6e 74 20 62 20 7b 7d 20 7b 53 45   parent b {} {SE
c210: 54 20 44 45 46 41 55 4c 54 7d 20 43 41 53 43 41  T DEFAULT} CASCA
c220: 44 45 20 4e 4f 4e 45 7d 0a 20 20 36 20 63 68 69  DE NONE}.  6 chi
c230: 6c 64 35 20 7b 30 20 30 20 70 61 72 65 6e 74 20  ld5 {0 0 parent 
c240: 62 20 7b 7d 20 43 41 53 43 41 44 45 20 7b 4e 4f  b {} CASCADE {NO
c250: 20 41 43 54 49 4f 4e 7d 20 4e 4f 4e 45 7d 0a 20   ACTION} NONE}. 
c260: 20 37 20 63 68 69 6c 64 36 20 7b 30 20 30 20 70   7 child6 {0 0 p
c270: 61 72 65 6e 74 20 62 20 7b 7d 20 7b 4e 4f 20 41  arent b {} {NO A
c280: 43 54 49 4f 4e 7d 20 52 45 53 54 52 49 43 54 20  CTION} RESTRICT 
c290: 4e 4f 4e 45 7d 0a 20 20 38 20 63 68 69 6c 64 37  NONE}.  8 child7
c2a0: 20 7b 30 20 30 20 70 61 72 65 6e 74 20 62 20 7b   {0 0 parent b {
c2b0: 7d 20 7b 4e 4f 20 41 43 54 49 4f 4e 7d 20 7b 4e  } {NO ACTION} {N
c2c0: 4f 20 41 43 54 49 4f 4e 7d 20 4e 4f 4e 45 7d 0a  O ACTION} NONE}.
c2d0: 20 20 39 20 63 68 69 6c 64 38 20 7b 30 20 30 20    9 child8 {0 0 
c2e0: 70 61 72 65 6e 74 20 62 20 7b 7d 20 7b 4e 4f 20  parent b {} {NO 
c2f0: 41 43 54 49 4f 4e 7d 20 7b 4e 4f 20 41 43 54 49  ACTION} {NO ACTI
c300: 4f 4e 7d 20 4e 4f 4e 45 7d 0a 7d 20 7b 0a 20 20  ON} NONE}.} {.  
c310: 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 31  do_test e_fkey-1
c320: 37 2e 24 74 6e 20 7b 20 65 78 65 63 73 71 6c 20  7.$tn { execsql 
c330: 22 50 52 41 47 4d 41 20 66 6f 72 65 69 67 6e 5f  "PRAGMA foreign_
c340: 6b 65 79 5f 6c 69 73 74 28 24 7a 54 61 62 29 22  key_list($zTab)"
c350: 20 7d 20 24 6c 52 65 73 0a 7d 0a 0a 23 2d 2d 2d   } $lRes.}..#---
c360: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
c370: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
c380: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
c390: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
c3a0: 2d 2d 2d 2d 2d 2d 0a 23 20 2f 2a 20 45 56 3a 20  ------.# /* EV: 
c3b0: 52 2d 31 39 39 37 31 2d 35 34 39 37 36 20 2a 2f  R-19971-54976 */
c3c0: 0a 23 0a 23 20 54 65 73 74 20 74 68 61 74 20 22  .#.# Test that "
c3d0: 4e 4f 20 41 43 54 49 4f 4e 22 20 6d 65 61 6e 73  NO ACTION" means
c3e0: 20 74 68 61 74 20 6e 6f 74 68 69 6e 67 20 68 61   that nothing ha
c3f0: 70 70 65 6e 73 20 74 6f 20 61 20 63 68 69 6c 64  ppens to a child
c400: 20 72 6f 77 20 77 68 65 6e 0a 23 20 69 74 27 73   row when.# it's
c410: 20 70 61 72 65 6e 74 20 72 6f 77 20 69 73 20 75   parent row is u
c420: 70 64 61 74 65 64 20 6f 72 20 64 65 6c 65 74 65  pdated or delete
c430: 64 2e 0a 23 0a 64 72 6f 70 5f 61 6c 6c 5f 74 61  d..#.drop_all_ta
c440: 62 6c 65 73 0a 64 6f 5f 74 65 73 74 20 65 5f 66  bles.do_test e_f
c450: 6b 65 79 2d 31 38 2e 31 20 7b 0a 20 20 65 78 65  key-18.1 {.  exe
c460: 63 73 71 6c 20 7b 0a 20 20 20 20 43 52 45 41 54  csql {.    CREAT
c470: 45 20 54 41 42 4c 45 20 70 61 72 65 6e 74 28 70  E TABLE parent(p
c480: 31 2c 20 70 32 2c 20 50 52 49 4d 41 52 59 20 4b  1, p2, PRIMARY K
c490: 45 59 28 70 31 2c 20 70 32 29 29 3b 0a 20 20 20  EY(p1, p2));.   
c4a0: 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 63 68   CREATE TABLE ch
c4b0: 69 6c 64 28 63 31 2c 20 63 32 2c 20 0a 20 20 20  ild(c1, c2, .   
c4c0: 20 20 20 46 4f 52 45 49 47 4e 20 4b 45 59 28 63     FOREIGN KEY(c
c4d0: 31 2c 20 63 32 29 20 52 45 46 45 52 45 4e 43 45  1, c2) REFERENCE
c4e0: 53 20 70 61 72 65 6e 74 0a 20 20 20 20 20 20 4f  S parent.      O
c4f0: 4e 20 55 50 44 41 54 45 20 4e 4f 20 41 43 54 49  N UPDATE NO ACTI
c500: 4f 4e 0a 20 20 20 20 20 20 4f 4e 20 44 45 4c 45  ON.      ON DELE
c510: 54 45 20 4e 4f 20 41 43 54 49 4f 4e 0a 20 20 20  TE NO ACTION.   
c520: 20 20 20 44 45 46 45 52 52 41 42 4c 45 20 49 4e     DEFERRABLE IN
c530: 49 54 49 41 4c 4c 59 20 44 45 46 45 52 52 45 44  ITIALLY DEFERRED
c540: 0a 20 20 20 20 29 3b 0a 20 20 20 20 49 4e 53 45  .    );.    INSE
c550: 52 54 20 49 4e 54 4f 20 70 61 72 65 6e 74 20 56  RT INTO parent V
c560: 41 4c 55 45 53 28 27 6a 27 2c 20 27 6b 27 29 3b  ALUES('j', 'k');
c570: 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f  .    INSERT INTO
c580: 20 70 61 72 65 6e 74 20 56 41 4c 55 45 53 28 27   parent VALUES('
c590: 6c 27 2c 20 27 6d 27 29 3b 0a 20 20 20 20 49 4e  l', 'm');.    IN
c5a0: 53 45 52 54 20 49 4e 54 4f 20 63 68 69 6c 64 20  SERT INTO child 
c5b0: 56 41 4c 55 45 53 28 27 6a 27 2c 20 27 6b 27 29  VALUES('j', 'k')
c5c0: 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54  ;.    INSERT INT
c5d0: 4f 20 63 68 69 6c 64 20 56 41 4c 55 45 53 28 27  O child VALUES('
c5e0: 6c 27 2c 20 27 6d 27 29 3b 0a 20 20 7d 0a 7d 20  l', 'm');.  }.} 
c5f0: 7b 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65  {}.do_test e_fke
c600: 79 2d 31 38 2e 32 20 7b 0a 20 20 65 78 65 63 73  y-18.2 {.  execs
c610: 71 6c 20 7b 0a 20 20 20 20 42 45 47 49 4e 3b 0a  ql {.    BEGIN;.
c620: 20 20 20 20 20 20 55 50 44 41 54 45 20 70 61 72        UPDATE par
c630: 65 6e 74 20 53 45 54 20 70 31 3d 27 6b 27 20 57  ent SET p1='k' W
c640: 48 45 52 45 20 70 31 3d 27 6a 27 3b 0a 20 20 20  HERE p1='j';.   
c650: 20 20 20 44 45 4c 45 54 45 20 46 52 4f 4d 20 70     DELETE FROM p
c660: 61 72 65 6e 74 20 57 48 45 52 45 20 70 31 3d 27  arent WHERE p1='
c670: 6c 27 3b 0a 20 20 20 20 20 20 53 45 4c 45 43 54  l';.      SELECT
c680: 20 2a 20 46 52 4f 4d 20 63 68 69 6c 64 3b 0a 20   * FROM child;. 
c690: 20 7d 0a 7d 20 7b 6a 20 6b 20 6c 20 6d 7d 0a 64   }.} {j k l m}.d
c6a0: 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 31 38  o_test e_fkey-18
c6b0: 2e 33 20 7b 0a 20 20 63 61 74 63 68 73 71 6c 20  .3 {.  catchsql 
c6c0: 43 4f 4d 4d 49 54 0a 7d 20 7b 31 20 7b 66 6f 72  COMMIT.} {1 {for
c6d0: 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61  eign key constra
c6e0: 69 6e 74 20 66 61 69 6c 65 64 7d 7d 0a 64 6f 5f  int failed}}.do_
c6f0: 74 65 73 74 20 65 5f 66 6b 65 79 2d 31 38 2e 34  test e_fkey-18.4
c700: 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 52 4f 4c   {.  execsql ROL
c710: 4c 42 41 43 4b 0a 7d 20 7b 7d 0a 0a 23 2d 2d 2d  LBACK.} {}..#---
c720: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
c730: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
c740: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
c750: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
c760: 2d 2d 2d 2d 2d 2d 0a 23 20 2f 2a 20 45 56 3a 20  ------.# /* EV: 
c770: 52 2d 30 34 32 37 32 2d 33 38 36 35 33 20 2a 2f  R-04272-38653 */
c780: 0a 23 0a 23 20 54 65 73 74 20 74 68 61 74 20 22  .#.# Test that "
c790: 52 45 53 54 52 49 43 54 22 20 6d 65 61 6e 73 20  RESTRICT" means 
c7a0: 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20  the application 
c7b0: 69 73 20 70 72 6f 68 69 62 69 74 65 64 20 66 72  is prohibited fr
c7c0: 6f 6d 20 64 65 6c 65 74 69 6e 67 0a 23 20 6f 72  om deleting.# or
c7d0: 20 75 70 64 61 74 69 6e 67 20 61 20 70 61 72 65   updating a pare
c7e0: 6e 74 20 74 61 62 6c 65 20 72 6f 77 20 77 68 65  nt table row whe
c7f0: 6e 20 74 68 65 72 65 20 65 78 69 73 74 73 20 6f  n there exists o
c800: 6e 65 20 6f 72 20 6d 6f 72 65 20 63 68 69 6c 64  ne or more child
c810: 20 6b 65 79 73 0a 23 20 6d 61 70 70 65 64 20 74   keys.# mapped t
c820: 6f 20 69 74 2e 0a 23 0a 64 72 6f 70 5f 61 6c 6c  o it..#.drop_all
c830: 5f 74 61 62 6c 65 73 0a 64 6f 5f 74 65 73 74 20  _tables.do_test 
c840: 65 5f 66 6b 65 79 2d 31 38 2e 31 20 7b 0a 20 20  e_fkey-18.1 {.  
c850: 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 43 52  execsql {.    CR
c860: 45 41 54 45 20 54 41 42 4c 45 20 70 61 72 65 6e  EATE TABLE paren
c870: 74 28 70 31 2c 20 70 32 29 3b 0a 20 20 20 20 43  t(p1, p2);.    C
c880: 52 45 41 54 45 20 55 4e 49 51 55 45 20 49 4e 44  REATE UNIQUE IND
c890: 45 58 20 70 61 72 65 6e 74 5f 69 20 4f 4e 20 70  EX parent_i ON p
c8a0: 61 72 65 6e 74 28 70 31 2c 20 70 32 29 3b 0a 20  arent(p1, p2);. 
c8b0: 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20     CREATE TABLE 
c8c0: 63 68 69 6c 64 31 28 63 31 2c 20 63 32 2c 20 0a  child1(c1, c2, .
c8d0: 20 20 20 20 20 20 46 4f 52 45 49 47 4e 20 4b 45        FOREIGN KE
c8e0: 59 28 63 32 2c 20 63 31 29 20 52 45 46 45 52 45  Y(c2, c1) REFERE
c8f0: 4e 43 45 53 20 70 61 72 65 6e 74 28 70 31 2c 20  NCES parent(p1, 
c900: 70 32 29 20 4f 4e 20 44 45 4c 45 54 45 20 52 45  p2) ON DELETE RE
c910: 53 54 52 49 43 54 0a 20 20 20 20 29 3b 0a 20 20  STRICT.    );.  
c920: 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 63    CREATE TABLE c
c930: 68 69 6c 64 32 28 63 31 2c 20 63 32 2c 20 0a 20  hild2(c1, c2, . 
c940: 20 20 20 20 20 46 4f 52 45 49 47 4e 20 4b 45 59       FOREIGN KEY
c950: 28 63 32 2c 20 63 31 29 20 52 45 46 45 52 45 4e  (c2, c1) REFEREN
c960: 43 45 53 20 70 61 72 65 6e 74 28 70 31 2c 20 70  CES parent(p1, p
c970: 32 29 20 4f 4e 20 55 50 44 41 54 45 20 52 45 53  2) ON UPDATE RES
c980: 54 52 49 43 54 0a 20 20 20 20 29 3b 0a 20 20 7d  TRICT.    );.  }
c990: 0a 7d 20 7b 7d 0a 64 6f 5f 74 65 73 74 20 65 5f  .} {}.do_test e_
c9a0: 66 6b 65 79 2d 31 38 2e 32 20 7b 0a 20 20 65 78  fkey-18.2 {.  ex
c9b0: 65 63 73 71 6c 20 7b 0a 20 20 20 20 49 4e 53 45  ecsql {.    INSE
c9c0: 52 54 20 49 4e 54 4f 20 70 61 72 65 6e 74 20 56  RT INTO parent V
c9d0: 41 4c 55 45 53 28 27 61 27 2c 20 27 62 27 29 3b  ALUES('a', 'b');
c9e0: 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f  .    INSERT INTO
c9f0: 20 70 61 72 65 6e 74 20 56 41 4c 55 45 53 28 27   parent VALUES('
ca00: 63 27 2c 20 27 64 27 29 3b 0a 20 20 20 20 49 4e  c', 'd');.    IN
ca10: 53 45 52 54 20 49 4e 54 4f 20 63 68 69 6c 64 31  SERT INTO child1
ca20: 20 56 41 4c 55 45 53 28 27 62 27 2c 20 27 61 27   VALUES('b', 'a'
ca30: 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e  );.    INSERT IN
ca40: 54 4f 20 63 68 69 6c 64 32 20 56 41 4c 55 45 53  TO child2 VALUES
ca50: 28 27 64 27 2c 20 27 63 27 29 3b 0a 20 20 7d 0a  ('d', 'c');.  }.
ca60: 7d 20 7b 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66  } {}.do_test e_f
ca70: 6b 65 79 2d 31 38 2e 33 20 7b 0a 20 20 63 61 74  key-18.3 {.  cat
ca80: 63 68 73 71 6c 20 7b 20 44 45 4c 45 54 45 20 46  chsql { DELETE F
ca90: 52 4f 4d 20 70 61 72 65 6e 74 20 57 48 45 52 45  ROM parent WHERE
caa0: 20 70 31 20 3d 20 27 61 27 20 7d 0a 7d 20 7b 31   p1 = 'a' }.} {1
cab0: 20 7b 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f   {foreign key co
cac0: 6e 73 74 72 61 69 6e 74 20 66 61 69 6c 65 64 7d  nstraint failed}
cad0: 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79  }.do_test e_fkey
cae0: 2d 31 38 2e 34 20 7b 0a 20 20 63 61 74 63 68 73  -18.4 {.  catchs
caf0: 71 6c 20 7b 20 55 50 44 41 54 45 20 70 61 72 65  ql { UPDATE pare
cb00: 6e 74 20 53 45 54 20 70 32 20 3d 20 27 65 27 20  nt SET p2 = 'e' 
cb10: 57 48 45 52 45 20 70 31 20 3d 20 27 63 27 20 7d  WHERE p1 = 'c' }
cb20: 0a 7d 20 7b 31 20 7b 66 6f 72 65 69 67 6e 20 6b  .} {1 {foreign k
cb30: 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 20 66 61  ey constraint fa
cb40: 69 6c 65 64 7d 7d 0a 0a 23 2d 2d 2d 2d 2d 2d 2d  iled}}..#-------
cb50: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
cb60: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
cb70: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
cb80: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
cb90: 2d 2d 0a 23 20 2f 2a 20 45 56 3a 20 52 2d 33 37  --.# /* EV: R-37
cba0: 39 39 37 2d 34 32 31 38 37 20 2a 2f 0a 23 20 0a  997-42187 */.# .
cbb0: 23 20 54 65 73 74 20 74 68 61 74 20 52 45 53 54  # Test that REST
cbc0: 52 49 43 54 20 69 73 20 73 6c 69 67 68 74 6c 79  RICT is slightly
cbd0: 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 20   different from 
cbe0: 4e 4f 20 41 43 54 49 4f 4e 20 66 6f 72 20 49 4d  NO ACTION for IM
cbf0: 4d 45 44 49 41 54 45 0a 23 20 63 6f 6e 73 74 72  MEDIATE.# constr
cc00: 61 69 6e 74 73 2c 20 69 6e 20 74 68 61 74 20 69  aints, in that i
cc10: 74 20 69 73 20 65 6e 66 6f 72 63 65 64 20 69 6d  t is enforced im
cc20: 6d 65 64 69 61 74 65 6c 79 2c 20 6e 6f 74 20 61  mediately, not a
cc30: 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  t the end of the
cc40: 20 0a 23 20 73 74 61 74 65 6d 65 6e 74 2e 0a 23   .# statement..#
cc50: 0a 64 72 6f 70 5f 61 6c 6c 5f 74 61 62 6c 65 73  .drop_all_tables
cc60: 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d  .do_test e_fkey-
cc70: 31 39 2e 31 20 7b 0a 20 20 65 78 65 63 73 71 6c  19.1 {.  execsql
cc80: 20 7b 0a 20 20 20 20 43 52 45 41 54 45 20 54 41   {.    CREATE TA
cc90: 42 4c 45 20 70 61 72 65 6e 74 28 78 20 50 52 49  BLE parent(x PRI
cca0: 4d 41 52 59 20 4b 45 59 29 3b 0a 20 20 20 20 43  MARY KEY);.    C
ccb0: 52 45 41 54 45 20 54 41 42 4c 45 20 63 68 69 6c  REATE TABLE chil
ccc0: 64 31 28 63 20 52 45 46 45 52 45 4e 43 45 53 20  d1(c REFERENCES 
ccd0: 70 61 72 65 6e 74 20 4f 4e 20 55 50 44 41 54 45  parent ON UPDATE
cce0: 20 52 45 53 54 52 49 43 54 29 3b 0a 20 20 20 20   RESTRICT);.    
ccf0: 43 52 45 41 54 45 20 54 41 42 4c 45 20 63 68 69  CREATE TABLE chi
cd00: 6c 64 32 28 63 20 52 45 46 45 52 45 4e 43 45 53  ld2(c REFERENCES
cd10: 20 70 61 72 65 6e 74 20 4f 4e 20 55 50 44 41 54   parent ON UPDAT
cd20: 45 20 4e 4f 20 41 43 54 49 4f 4e 29 3b 0a 0a 20  E NO ACTION);.. 
cd30: 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 70     INSERT INTO p
cd40: 61 72 65 6e 74 20 56 41 4c 55 45 53 28 27 6b 65  arent VALUES('ke
cd50: 79 31 27 29 3b 0a 20 20 20 20 49 4e 53 45 52 54  y1');.    INSERT
cd60: 20 49 4e 54 4f 20 70 61 72 65 6e 74 20 56 41 4c   INTO parent VAL
cd70: 55 45 53 28 27 6b 65 79 32 27 29 3b 0a 20 20 20  UES('key2');.   
cd80: 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 63 68 69   INSERT INTO chi
cd90: 6c 64 31 20 56 41 4c 55 45 53 28 27 6b 65 79 31  ld1 VALUES('key1
cda0: 27 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49  ');.    INSERT I
cdb0: 4e 54 4f 20 63 68 69 6c 64 32 20 56 41 4c 55 45  NTO child2 VALUE
cdc0: 53 28 27 6b 65 79 32 27 29 3b 0a 0a 20 20 20 20  S('key2');..    
cdd0: 43 52 45 41 54 45 20 54 52 49 47 47 45 52 20 70  CREATE TRIGGER p
cde0: 61 72 65 6e 74 5f 74 20 41 46 54 45 52 20 55 50  arent_t AFTER UP
cdf0: 44 41 54 45 20 4f 4e 20 70 61 72 65 6e 74 20 42  DATE ON parent B
ce00: 45 47 49 4e 0a 20 20 20 20 20 20 55 50 44 41 54  EGIN.      UPDAT
ce10: 45 20 63 68 69 6c 64 31 20 73 65 74 20 63 20 3d  E child1 set c =
ce20: 20 6e 65 77 2e 78 20 57 48 45 52 45 20 63 20 3d   new.x WHERE c =
ce30: 20 6f 6c 64 2e 78 3b 0a 20 20 20 20 20 20 55 50   old.x;.      UP
ce40: 44 41 54 45 20 63 68 69 6c 64 32 20 73 65 74 20  DATE child2 set 
ce50: 63 20 3d 20 6e 65 77 2e 78 20 57 48 45 52 45 20  c = new.x WHERE 
ce60: 63 20 3d 20 6f 6c 64 2e 78 3b 0a 20 20 20 20 45  c = old.x;.    E
ce70: 4e 44 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a 64 6f 5f  ND;.  }.} {}.do_
ce80: 74 65 73 74 20 65 5f 66 6b 65 79 2d 31 39 2e 32  test e_fkey-19.2
ce90: 20 7b 0a 20 20 63 61 74 63 68 73 71 6c 20 7b 20   {.  catchsql { 
cea0: 55 50 44 41 54 45 20 70 61 72 65 6e 74 20 53 45  UPDATE parent SE
ceb0: 54 20 78 20 3d 20 27 6b 65 79 20 6f 6e 65 27 20  T x = 'key one' 
cec0: 57 48 45 52 45 20 78 20 3d 20 27 6b 65 79 31 27  WHERE x = 'key1'
ced0: 20 7d 0a 7d 20 7b 31 20 7b 66 6f 72 65 69 67 6e   }.} {1 {foreign
cee0: 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 20   key constraint 
cef0: 66 61 69 6c 65 64 7d 7d 0a 64 6f 5f 74 65 73 74  failed}}.do_test
cf00: 20 65 5f 66 6b 65 79 2d 31 39 2e 33 20 7b 0a 20   e_fkey-19.3 {. 
cf10: 20 65 78 65 63 73 71 6c 20 7b 20 0a 20 20 20 20   execsql { .    
cf20: 55 50 44 41 54 45 20 70 61 72 65 6e 74 20 53 45  UPDATE parent SE
cf30: 54 20 78 20 3d 20 27 6b 65 79 20 74 77 6f 27 20  T x = 'key two' 
cf40: 57 48 45 52 45 20 78 20 3d 20 27 6b 65 79 32 27  WHERE x = 'key2'
cf50: 3b 0a 20 20 20 20 53 45 4c 45 43 54 20 2a 20 46  ;.    SELECT * F
cf60: 52 4f 4d 20 63 68 69 6c 64 32 3b 0a 20 20 7d 0a  ROM child2;.  }.
cf70: 7d 20 7b 7b 6b 65 79 20 74 77 6f 7d 7d 0a 0a 64  } {{key two}}..d
cf80: 72 6f 70 5f 61 6c 6c 5f 74 61 62 6c 65 73 0a 64  rop_all_tables.d
cf90: 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 31 39  o_test e_fkey-19
cfa0: 2e 34 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b  .4 {.  execsql {
cfb0: 0a 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c  .    CREATE TABL
cfc0: 45 20 70 61 72 65 6e 74 28 78 20 50 52 49 4d 41  E parent(x PRIMA
cfd0: 52 59 20 4b 45 59 29 3b 0a 20 20 20 20 43 52 45  RY KEY);.    CRE
cfe0: 41 54 45 20 54 41 42 4c 45 20 63 68 69 6c 64 31  ATE TABLE child1
cff0: 28 63 20 52 45 46 45 52 45 4e 43 45 53 20 70 61  (c REFERENCES pa
d000: 72 65 6e 74 20 4f 4e 20 44 45 4c 45 54 45 20 52  rent ON DELETE R
d010: 45 53 54 52 49 43 54 29 3b 0a 20 20 20 20 43 52  ESTRICT);.    CR
d020: 45 41 54 45 20 54 41 42 4c 45 20 63 68 69 6c 64  EATE TABLE child
d030: 32 28 63 20 52 45 46 45 52 45 4e 43 45 53 20 70  2(c REFERENCES p
d040: 61 72 65 6e 74 20 4f 4e 20 44 45 4c 45 54 45 20  arent ON DELETE 
d050: 4e 4f 20 41 43 54 49 4f 4e 29 3b 0a 0a 20 20 20  NO ACTION);..   
d060: 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 70 61 72   INSERT INTO par
d070: 65 6e 74 20 56 41 4c 55 45 53 28 27 6b 65 79 31  ent VALUES('key1
d080: 27 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49  ');.    INSERT I
d090: 4e 54 4f 20 70 61 72 65 6e 74 20 56 41 4c 55 45  NTO parent VALUE
d0a0: 53 28 27 6b 65 79 32 27 29 3b 0a 20 20 20 20 49  S('key2');.    I
d0b0: 4e 53 45 52 54 20 49 4e 54 4f 20 63 68 69 6c 64  NSERT INTO child
d0c0: 31 20 56 41 4c 55 45 53 28 27 6b 65 79 31 27 29  1 VALUES('key1')
d0d0: 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54  ;.    INSERT INT
d0e0: 4f 20 63 68 69 6c 64 32 20 56 41 4c 55 45 53 28  O child2 VALUES(
d0f0: 27 6b 65 79 32 27 29 3b 0a 0a 20 20 20 20 43 52  'key2');..    CR
d100: 45 41 54 45 20 54 52 49 47 47 45 52 20 70 61 72  EATE TRIGGER par
d110: 65 6e 74 5f 74 20 41 46 54 45 52 20 44 45 4c 45  ent_t AFTER DELE
d120: 54 45 20 4f 4e 20 70 61 72 65 6e 74 20 42 45 47  TE ON parent BEG
d130: 49 4e 0a 20 20 20 20 20 20 55 50 44 41 54 45 20  IN.      UPDATE 
d140: 63 68 69 6c 64 31 20 53 45 54 20 63 20 3d 20 4e  child1 SET c = N
d150: 55 4c 4c 20 57 48 45 52 45 20 63 20 3d 20 6f 6c  ULL WHERE c = ol
d160: 64 2e 78 3b 0a 20 20 20 20 20 20 55 50 44 41 54  d.x;.      UPDAT
d170: 45 20 63 68 69 6c 64 32 20 53 45 54 20 63 20 3d  E child2 SET c =
d180: 20 4e 55 4c 4c 20 57 48 45 52 45 20 63 20 3d 20   NULL WHERE c = 
d190: 6f 6c 64 2e 78 3b 0a 20 20 20 20 45 4e 44 3b 0a  old.x;.    END;.
d1a0: 20 20 7d 0a 7d 20 7b 7d 0a 64 6f 5f 74 65 73 74    }.} {}.do_test
d1b0: 20 65 5f 66 6b 65 79 2d 31 39 2e 35 20 7b 0a 20   e_fkey-19.5 {. 
d1c0: 20 63 61 74 63 68 73 71 6c 20 7b 20 44 45 4c 45   catchsql { DELE
d1d0: 54 45 20 46 52 4f 4d 20 70 61 72 65 6e 74 20 57  TE FROM parent W
d1e0: 48 45 52 45 20 78 20 3d 20 27 6b 65 79 31 27 20  HERE x = 'key1' 
d1f0: 7d 0a 7d 20 7b 31 20 7b 66 6f 72 65 69 67 6e 20  }.} {1 {foreign 
d200: 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 20 66  key constraint f
d210: 61 69 6c 65 64 7d 7d 0a 64 6f 5f 74 65 73 74 20  ailed}}.do_test 
d220: 65 5f 66 6b 65 79 2d 31 39 2e 36 20 7b 0a 20 20  e_fkey-19.6 {.  
d230: 65 78 65 63 73 71 6c 20 7b 20 0a 20 20 20 20 44  execsql { .    D
d240: 45 4c 45 54 45 20 46 52 4f 4d 20 70 61 72 65 6e  ELETE FROM paren
d250: 74 20 57 48 45 52 45 20 78 20 3d 20 27 6b 65 79  t WHERE x = 'key
d260: 32 27 3b 0a 20 20 20 20 53 45 4c 45 43 54 20 2a  2';.    SELECT *
d270: 20 46 52 4f 4d 20 63 68 69 6c 64 32 3b 0a 20 20   FROM child2;.  
d280: 7d 0a 7d 20 7b 7b 7d 7d 0a 0a 64 72 6f 70 5f 61  }.} {{}}..drop_a
d290: 6c 6c 5f 74 61 62 6c 65 73 0a 64 6f 5f 74 65 73  ll_tables.do_tes
d2a0: 74 20 65 5f 66 6b 65 79 2d 31 39 2e 37 20 7b 0a  t e_fkey-19.7 {.
d2b0: 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20    execsql {.    
d2c0: 43 52 45 41 54 45 20 54 41 42 4c 45 20 70 61 72  CREATE TABLE par
d2d0: 65 6e 74 28 78 20 50 52 49 4d 41 52 59 20 4b 45  ent(x PRIMARY KE
d2e0: 59 29 3b 0a 20 20 20 20 43 52 45 41 54 45 20 54  Y);.    CREATE T
d2f0: 41 42 4c 45 20 63 68 69 6c 64 31 28 63 20 52 45  ABLE child1(c RE
d300: 46 45 52 45 4e 43 45 53 20 70 61 72 65 6e 74 20  FERENCES parent 
d310: 4f 4e 20 44 45 4c 45 54 45 20 52 45 53 54 52 49  ON DELETE RESTRI
d320: 43 54 29 3b 0a 20 20 20 20 43 52 45 41 54 45 20  CT);.    CREATE 
d330: 54 41 42 4c 45 20 63 68 69 6c 64 32 28 63 20 52  TABLE child2(c R
d340: 45 46 45 52 45 4e 43 45 53 20 70 61 72 65 6e 74  EFERENCES parent
d350: 20 4f 4e 20 44 45 4c 45 54 45 20 4e 4f 20 41 43   ON DELETE NO AC
d360: 54 49 4f 4e 29 3b 0a 0a 20 20 20 20 49 4e 53 45  TION);..    INSE
d370: 52 54 20 49 4e 54 4f 20 70 61 72 65 6e 74 20 56  RT INTO parent V
d380: 41 4c 55 45 53 28 27 6b 65 79 31 27 29 3b 0a 20  ALUES('key1');. 
d390: 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 70     INSERT INTO p
d3a0: 61 72 65 6e 74 20 56 41 4c 55 45 53 28 27 6b 65  arent VALUES('ke
d3b0: 79 32 27 29 3b 0a 20 20 20 20 49 4e 53 45 52 54  y2');.    INSERT
d3c0: 20 49 4e 54 4f 20 63 68 69 6c 64 31 20 56 41 4c   INTO child1 VAL
d3d0: 55 45 53 28 27 6b 65 79 31 27 29 3b 0a 20 20 20  UES('key1');.   
d3e0: 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 63 68 69   INSERT INTO chi
d3f0: 6c 64 32 20 56 41 4c 55 45 53 28 27 6b 65 79 32  ld2 VALUES('key2
d400: 27 29 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a 64 6f 5f  ');.  }.} {}.do_
d410: 74 65 73 74 20 65 5f 66 6b 65 79 2d 31 39 2e 38  test e_fkey-19.8
d420: 20 7b 0a 20 20 63 61 74 63 68 73 71 6c 20 7b 20   {.  catchsql { 
d430: 52 45 50 4c 41 43 45 20 49 4e 54 4f 20 70 61 72  REPLACE INTO par
d440: 65 6e 74 20 56 41 4c 55 45 53 28 27 6b 65 79 31  ent VALUES('key1
d450: 27 29 20 7d 0a 7d 20 7b 31 20 7b 66 6f 72 65 69  ') }.} {1 {forei
d460: 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e  gn key constrain
d470: 74 20 66 61 69 6c 65 64 7d 7d 0a 64 6f 5f 74 65  t failed}}.do_te
d480: 73 74 20 65 5f 66 6b 65 79 2d 31 39 2e 39 20 7b  st e_fkey-19.9 {
d490: 0a 20 20 65 78 65 63 73 71 6c 20 7b 20 0a 20 20  .  execsql { .  
d4a0: 20 20 52 45 50 4c 41 43 45 20 49 4e 54 4f 20 70    REPLACE INTO p
d4b0: 61 72 65 6e 74 20 56 41 4c 55 45 53 28 27 6b 65  arent VALUES('ke
d4c0: 79 32 27 29 3b 0a 20 20 20 20 53 45 4c 45 43 54  y2');.    SELECT
d4d0: 20 2a 20 46 52 4f 4d 20 63 68 69 6c 64 32 3b 0a   * FROM child2;.
d4e0: 20 20 7d 0a 7d 20 7b 6b 65 79 32 7d 0a 0a 23 2d    }.} {key2}..#-
d4f0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
d500: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
d510: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
d520: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
d530: 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20 2f 2a 20 45 56  --------.# /* EV
d540: 3a 20 52 2d 32 34 31 37 39 2d 36 30 35 32 33 20  : R-24179-60523 
d550: 2a 2f 0a 23 20 0a 23 20 54 65 73 74 20 74 68 61  */.# .# Test tha
d560: 74 20 52 45 53 54 52 49 43 54 20 69 73 20 65 6e  t RESTRICT is en
d570: 66 6f 72 63 65 64 20 69 6d 6d 65 64 69 61 74 65  forced immediate
d580: 6c 79 2c 20 65 76 65 6e 20 66 6f 72 20 61 20 44  ly, even for a D
d590: 45 46 45 52 52 45 44 20 63 6f 6e 73 74 72 61 69  EFERRED constrai
d5a0: 6e 74 2e 0a 23 0a 64 72 6f 70 5f 61 6c 6c 5f 74  nt..#.drop_all_t
d5b0: 61 62 6c 65 73 0a 64 6f 5f 74 65 73 74 20 65 5f  ables.do_test e_
d5c0: 66 6b 65 79 2d 32 30 2e 31 20 7b 0a 20 20 65 78  fkey-20.1 {.  ex
d5d0: 65 63 73 71 6c 20 7b 0a 20 20 20 20 43 52 45 41  ecsql {.    CREA
d5e0: 54 45 20 54 41 42 4c 45 20 70 61 72 65 6e 74 28  TE TABLE parent(
d5f0: 78 20 50 52 49 4d 41 52 59 20 4b 45 59 29 3b 0a  x PRIMARY KEY);.
d600: 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45      CREATE TABLE
d610: 20 63 68 69 6c 64 31 28 63 20 52 45 46 45 52 45   child1(c REFERE
d620: 4e 43 45 53 20 70 61 72 65 6e 74 20 4f 4e 20 55  NCES parent ON U
d630: 50 44 41 54 45 20 52 45 53 54 52 49 43 54 0a 20  PDATE RESTRICT. 
d640: 20 20 20 20 20 44 45 46 45 52 52 41 42 4c 45 20       DEFERRABLE 
d650: 49 4e 49 54 49 41 4c 4c 59 20 44 45 46 45 52 52  INITIALLY DEFERR
d660: 45 44 0a 20 20 20 20 29 3b 0a 20 20 20 20 43 52  ED.    );.    CR
d670: 45 41 54 45 20 54 41 42 4c 45 20 63 68 69 6c 64  EATE TABLE child
d680: 32 28 63 20 52 45 46 45 52 45 4e 43 45 53 20 70  2(c REFERENCES p
d690: 61 72 65 6e 74 20 4f 4e 20 55 50 44 41 54 45 20  arent ON UPDATE 
d6a0: 4e 4f 20 41 43 54 49 4f 4e 0a 20 20 20 20 20 20  NO ACTION.      
d6b0: 44 45 46 45 52 52 41 42 4c 45 20 49 4e 49 54 49  DEFERRABLE INITI
d6c0: 41 4c 4c 59 20 44 45 46 45 52 52 45 44 0a 20 20  ALLY DEFERRED.  
d6d0: 20 20 29 3b 0a 0a 20 20 20 20 49 4e 53 45 52 54    );..    INSERT
d6e0: 20 49 4e 54 4f 20 70 61 72 65 6e 74 20 56 41 4c   INTO parent VAL
d6f0: 55 45 53 28 27 6b 65 79 31 27 29 3b 0a 20 20 20  UES('key1');.   
d700: 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 70 61 72   INSERT INTO par
d710: 65 6e 74 20 56 41 4c 55 45 53 28 27 6b 65 79 32  ent VALUES('key2
d720: 27 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49  ');.    INSERT I
d730: 4e 54 4f 20 63 68 69 6c 64 31 20 56 41 4c 55 45  NTO child1 VALUE
d740: 53 28 27 6b 65 79 31 27 29 3b 0a 20 20 20 20 49  S('key1');.    I
d750: 4e 53 45 52 54 20 49 4e 54 4f 20 63 68 69 6c 64  NSERT INTO child
d760: 32 20 56 41 4c 55 45 53 28 27 6b 65 79 32 27 29  2 VALUES('key2')
d770: 3b 0a 20 20 20 20 42 45 47 49 4e 3b 0a 20 20 7d  ;.    BEGIN;.  }
d780: 0a 7d 20 7b 7d 0a 64 6f 5f 74 65 73 74 20 65 5f  .} {}.do_test e_
d790: 66 6b 65 79 2d 32 30 2e 32 20 7b 0a 20 20 63 61  fkey-20.2 {.  ca
d7a0: 74 63 68 73 71 6c 20 7b 20 55 50 44 41 54 45 20  tchsql { UPDATE 
d7b0: 70 61 72 65 6e 74 20 53 45 54 20 78 20 3d 20 27  parent SET x = '
d7c0: 6b 65 79 20 6f 6e 65 27 20 57 48 45 52 45 20 78  key one' WHERE x
d7d0: 20 3d 20 27 6b 65 79 31 27 20 7d 0a 7d 20 7b 31   = 'key1' }.} {1
d7e0: 20 7b 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f   {foreign key co
d7f0: 6e 73 74 72 61 69 6e 74 20 66 61 69 6c 65 64 7d  nstraint failed}
d800: 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79  }.do_test e_fkey
d810: 2d 32 30 2e 33 20 7b 0a 20 20 65 78 65 63 73 71  -20.3 {.  execsq
d820: 6c 20 7b 20 55 50 44 41 54 45 20 70 61 72 65 6e  l { UPDATE paren
d830: 74 20 53 45 54 20 78 20 3d 20 27 6b 65 79 20 74  t SET x = 'key t
d840: 77 6f 27 20 57 48 45 52 45 20 78 20 3d 20 27 6b  wo' WHERE x = 'k
d850: 65 79 32 27 20 7d 0a 7d 20 7b 7d 0a 64 6f 5f 74  ey2' }.} {}.do_t
d860: 65 73 74 20 65 5f 66 6b 65 79 2d 32 30 2e 34 20  est e_fkey-20.4 
d870: 7b 0a 20 20 63 61 74 63 68 73 71 6c 20 43 4f 4d  {.  catchsql COM
d880: 4d 49 54 0a 7d 20 7b 31 20 7b 66 6f 72 65 69 67  MIT.} {1 {foreig
d890: 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74  n key constraint
d8a0: 20 66 61 69 6c 65 64 7d 7d 0a 64 6f 5f 74 65 73   failed}}.do_tes
d8b0: 74 20 65 5f 66 6b 65 79 2d 32 30 2e 35 20 7b 0a  t e_fkey-20.5 {.
d8c0: 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20    execsql {.    
d8d0: 55 50 44 41 54 45 20 63 68 69 6c 64 32 20 53 45  UPDATE child2 SE
d8e0: 54 20 63 20 3d 20 27 6b 65 79 20 74 77 6f 27 3b  T c = 'key two';
d8f0: 0a 20 20 20 20 43 4f 4d 4d 49 54 3b 0a 20 20 7d  .    COMMIT;.  }
d900: 0a 7d 20 7b 7d 0a 0a 64 72 6f 70 5f 61 6c 6c 5f  .} {}..drop_all_
d910: 74 61 62 6c 65 73 0a 64 6f 5f 74 65 73 74 20 65  tables.do_test e
d920: 5f 66 6b 65 79 2d 32 30 2e 36 20 7b 0a 20 20 65  _fkey-20.6 {.  e
d930: 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 43 52 45  xecsql {.    CRE
d940: 41 54 45 20 54 41 42 4c 45 20 70 61 72 65 6e 74  ATE TABLE parent
d950: 28 78 20 50 52 49 4d 41 52 59 20 4b 45 59 29 3b  (x PRIMARY KEY);
d960: 0a 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c  .    CREATE TABL
d970: 45 20 63 68 69 6c 64 31 28 63 20 52 45 46 45 52  E child1(c REFER
d980: 45 4e 43 45 53 20 70 61 72 65 6e 74 20 4f 4e 20  ENCES parent ON 
d990: 44 45 4c 45 54 45 20 52 45 53 54 52 49 43 54 0a  DELETE RESTRICT.
d9a0: 20 20 20 20 20 20 44 45 46 45 52 52 41 42 4c 45        DEFERRABLE
d9b0: 20 49 4e 49 54 49 41 4c 4c 59 20 44 45 46 45 52   INITIALLY DEFER
d9c0: 52 45 44 0a 20 20 20 20 29 3b 0a 20 20 20 20 43  RED.    );.    C
d9d0: 52 45 41 54 45 20 54 41 42 4c 45 20 63 68 69 6c  REATE TABLE chil
d9e0: 64 32 28 63 20 52 45 46 45 52 45 4e 43 45 53 20  d2(c REFERENCES 
d9f0: 70 61 72 65 6e 74 20 4f 4e 20 44 45 4c 45 54 45  parent ON DELETE
da00: 20 4e 4f 20 41 43 54 49 4f 4e 0a 20 20 20 20 20   NO ACTION.     
da10: 20 44 45 46 45 52 52 41 42 4c 45 20 49 4e 49 54   DEFERRABLE INIT
da20: 49 41 4c 4c 59 20 44 45 46 45 52 52 45 44 0a 20  IALLY DEFERRED. 
da30: 20 20 20 29 3b 0a 0a 20 20 20 20 49 4e 53 45 52     );..    INSER
da40: 54 20 49 4e 54 4f 20 70 61 72 65 6e 74 20 56 41  T INTO parent VA
da50: 4c 55 45 53 28 27 6b 65 79 31 27 29 3b 0a 20 20  LUES('key1');.  
da60: 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 70 61    INSERT INTO pa
da70: 72 65 6e 74 20 56 41 4c 55 45 53 28 27 6b 65 79  rent VALUES('key
da80: 32 27 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20  2');.    INSERT 
da90: 49 4e 54 4f 20 63 68 69 6c 64 31 20 56 41 4c 55  INTO child1 VALU
daa0: 45 53 28 27 6b 65 79 31 27 29 3b 0a 20 20 20 20  ES('key1');.    
dab0: 49 4e 53 45 52 54 20 49 4e 54 4f 20 63 68 69 6c  INSERT INTO chil
dac0: 64 32 20 56 41 4c 55 45 53 28 27 6b 65 79 32 27  d2 VALUES('key2'
dad0: 29 3b 0a 20 20 20 20 42 45 47 49 4e 3b 0a 20 20  );.    BEGIN;.  
dae0: 7d 0a 7d 20 7b 7d 0a 64 6f 5f 74 65 73 74 20 65  }.} {}.do_test e
daf0: 5f 66 6b 65 79 2d 32 30 2e 37 20 7b 0a 20 20 63  _fkey-20.7 {.  c
db00: 61 74 63 68 73 71 6c 20 7b 20 44 45 4c 45 54 45  atchsql { DELETE
db10: 20 46 52 4f 4d 20 70 61 72 65 6e 74 20 57 48 45   FROM parent WHE
db20: 52 45 20 78 20 3d 20 27 6b 65 79 31 27 20 7d 0a  RE x = 'key1' }.
db30: 7d 20 7b 31 20 7b 66 6f 72 65 69 67 6e 20 6b 65  } {1 {foreign ke
db40: 79 20 63 6f 6e 73 74 72 61 69 6e 74 20 66 61 69  y constraint fai
db50: 6c 65 64 7d 7d 0a 64 6f 5f 74 65 73 74 20 65 5f  led}}.do_test e_
db60: 66 6b 65 79 2d 32 30 2e 38 20 7b 0a 20 20 65 78  fkey-20.8 {.  ex
db70: 65 63 73 71 6c 20 7b 20 44 45 4c 45 54 45 20 46  ecsql { DELETE F
db80: 52 4f 4d 20 70 61 72 65 6e 74 20 57 48 45 52 45  ROM parent WHERE
db90: 20 78 20 3d 20 27 6b 65 79 32 27 20 7d 0a 7d 20   x = 'key2' }.} 
dba0: 7b 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65  {}.do_test e_fke
dbb0: 79 2d 32 30 2e 39 20 7b 0a 20 20 63 61 74 63 68  y-20.9 {.  catch
dbc0: 73 71 6c 20 43 4f 4d 4d 49 54 0a 7d 20 7b 31 20  sql COMMIT.} {1 
dbd0: 7b 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e  {foreign key con
dbe0: 73 74 72 61 69 6e 74 20 66 61 69 6c 65 64 7d 7d  straint failed}}
dbf0: 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d  .do_test e_fkey-
dc00: 32 30 2e 31 30 20 7b 0a 20 20 65 78 65 63 73 71  20.10 {.  execsq
dc10: 6c 20 7b 0a 20 20 20 20 55 50 44 41 54 45 20 63  l {.    UPDATE c
dc20: 68 69 6c 64 32 20 53 45 54 20 63 20 3d 20 4e 55  hild2 SET c = NU
dc30: 4c 4c 3b 0a 20 20 20 20 43 4f 4d 4d 49 54 3b 0a  LL;.    COMMIT;.
dc40: 20 20 7d 0a 7d 20 7b 7d 0a 0a 23 2d 2d 2d 2d 2d    }.} {}..#-----
dc50: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
dc60: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
dc70: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
dc80: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
dc90: 2d 2d 2d 2d 0a 23 20 2f 2a 20 45 56 3a 20 52 2d  ----.# /* EV: R-
dca0: 30 33 33 35 33 2d 30 35 33 32 37 20 2a 2f 0a 23  03353-05327 */.#
dcb0: 0a 23 20 54 65 73 74 20 53 45 54 20 4e 55 4c 4c  .# Test SET NULL
dcc0: 20 61 63 74 69 6f 6e 73 2e 0a 23 0a 64 72 6f 70   actions..#.drop
dcd0: 5f 61 6c 6c 5f 74 61 62 6c 65 73 0a 64 6f 5f 74  _all_tables.do_t
dce0: 65 73 74 20 65 5f 66 6b 65 79 2d 32 31 2e 31 20  est e_fkey-21.1 
dcf0: 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20  {.  execsql {.  
dd00: 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 70    CREATE TABLE p
dd10: 41 28 78 20 50 52 49 4d 41 52 59 20 4b 45 59 29  A(x PRIMARY KEY)
dd20: 3b 0a 20 20 20 20 43 52 45 41 54 45 20 54 41 42  ;.    CREATE TAB
dd30: 4c 45 20 63 41 28 63 20 52 45 46 45 52 45 4e 43  LE cA(c REFERENC
dd40: 45 53 20 70 41 20 4f 4e 20 44 45 4c 45 54 45 20  ES pA ON DELETE 
dd50: 53 45 54 20 4e 55 4c 4c 29 3b 0a 20 20 20 20 43  SET NULL);.    C
dd60: 52 45 41 54 45 20 54 41 42 4c 45 20 63 42 28 63  REATE TABLE cB(c
dd70: 20 52 45 46 45 52 45 4e 43 45 53 20 70 41 20 4f   REFERENCES pA O
dd80: 4e 20 55 50 44 41 54 45 20 53 45 54 20 4e 55 4c  N UPDATE SET NUL
dd90: 4c 29 3b 0a 0a 20 20 20 20 49 4e 53 45 52 54 20  L);..    INSERT 
dda0: 49 4e 54 4f 20 70 41 20 56 41 4c 55 45 53 28 58  INTO pA VALUES(X
ddb0: 27 41 42 43 44 27 29 3b 0a 20 20 20 20 49 4e 53  'ABCD');.    INS
ddc0: 45 52 54 20 49 4e 54 4f 20 70 41 20 56 41 4c 55  ERT INTO pA VALU
ddd0: 45 53 28 58 27 31 32 33 34 27 29 3b 0a 20 20 20  ES(X'1234');.   
dde0: 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 63 41 20   INSERT INTO cA 
ddf0: 56 41 4c 55 45 53 28 58 27 41 42 43 44 27 29 3b  VALUES(X'ABCD');
de00: 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f  .    INSERT INTO
de10: 20 63 42 20 56 41 4c 55 45 53 28 58 27 31 32 33   cB VALUES(X'123
de20: 34 27 29 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a 64 6f  4');.  }.} {}.do
de30: 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 32 31 2e  _test e_fkey-21.
de40: 32 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a  2 {.  execsql {.
de50: 20 20 20 20 44 45 4c 45 54 45 20 46 52 4f 4d 20      DELETE FROM 
de60: 70 41 20 57 48 45 52 45 20 72 6f 77 69 64 20 3d  pA WHERE rowid =
de70: 20 31 3b 0a 20 20 20 20 53 45 4c 45 43 54 20 71   1;.    SELECT q
de80: 75 6f 74 65 28 78 29 20 46 52 4f 4d 20 70 41 3b  uote(x) FROM pA;
de90: 0a 20 20 7d 0a 7d 20 7b 58 27 31 32 33 34 27 7d  .  }.} {X'1234'}
dea0: 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d  .do_test e_fkey-
deb0: 32 31 2e 33 20 7b 0a 20 20 65 78 65 63 73 71 6c  21.3 {.  execsql
dec0: 20 7b 0a 20 20 20 20 53 45 4c 45 43 54 20 71 75   {.    SELECT qu
ded0: 6f 74 65 28 63 29 20 46 52 4f 4d 20 63 41 3b 0a  ote(c) FROM cA;.
dee0: 20 20 7d 0a 7d 20 7b 4e 55 4c 4c 7d 0a 64 6f 5f    }.} {NULL}.do_
def0: 74 65 73 74 20 65 5f 66 6b 65 79 2d 32 31 2e 34  test e_fkey-21.4
df00: 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20   {.  execsql {. 
df10: 20 20 20 55 50 44 41 54 45 20 70 41 20 53 45 54     UPDATE pA SET
df20: 20 78 20 3d 20 58 27 38 37 36 35 27 20 57 48 45   x = X'8765' WHE
df30: 52 45 20 72 6f 77 69 64 20 3d 20 32 3b 0a 20 20  RE rowid = 2;.  
df40: 20 20 53 45 4c 45 43 54 20 71 75 6f 74 65 28 78    SELECT quote(x
df50: 29 20 46 52 4f 4d 20 70 41 3b 0a 20 20 7d 0a 7d  ) FROM pA;.  }.}
df60: 20 7b 58 27 38 37 36 35 27 7d 0a 64 6f 5f 74 65   {X'8765'}.do_te
df70: 73 74 20 65 5f 66 6b 65 79 2d 32 31 2e 35 20 7b  st e_fkey-21.5 {
df80: 0a 20 20 65 78 65 63 73 71 6c 20 7b 20 53 45 4c  .  execsql { SEL
df90: 45 43 54 20 71 75 6f 74 65 28 63 29 20 46 52 4f  ECT quote(c) FRO
dfa0: 4d 20 63 42 20 7d 0a 7d 20 7b 4e 55 4c 4c 7d 0a  M cB }.} {NULL}.
dfb0: 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  .#--------------
dfc0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
dfd0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
dfe0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
dff0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20 2f 2a  -----------.# /*
e000: 20 45 56 3a 20 52 2d 34 33 30 35 34 2d 35 34 38   EV: R-43054-548
e010: 33 32 20 2a 2f 0a 23 0a 23 20 54 65 73 74 20 53  32 */.#.# Test S
e020: 45 54 20 44 45 46 41 55 4c 54 20 61 63 74 69 6f  ET DEFAULT actio
e030: 6e 73 2e 0a 23 0a 64 72 6f 70 5f 61 6c 6c 5f 74  ns..#.drop_all_t
e040: 61 62 6c 65 73 0a 64 6f 5f 74 65 73 74 20 65 5f  ables.do_test e_
e050: 66 6b 65 79 2d 32 32 2e 31 20 7b 0a 20 20 65 78  fkey-22.1 {.  ex
e060: 65 63 73 71 6c 20 7b 0a 20 20 20 20 43 52 45 41  ecsql {.    CREA
e070: 54 45 20 54 41 42 4c 45 20 70 41 28 78 20 50 52  TE TABLE pA(x PR
e080: 49 4d 41 52 59 20 4b 45 59 29 3b 0a 20 20 20 20  IMARY KEY);.    
e090: 43 52 45 41 54 45 20 54 41 42 4c 45 20 63 41 28  CREATE TABLE cA(
e0a0: 63 20 44 45 46 41 55 4c 54 20 58 27 30 30 30 30  c DEFAULT X'0000
e0b0: 27 20 52 45 46 45 52 45 4e 43 45 53 20 70 41 20  ' REFERENCES pA 
e0c0: 4f 4e 20 44 45 4c 45 54 45 20 53 45 54 20 44 45  ON DELETE SET DE
e0d0: 46 41 55 4c 54 29 3b 0a 20 20 20 20 43 52 45 41  FAULT);.    CREA
e0e0: 54 45 20 54 41 42 4c 45 20 63 42 28 63 20 44 45  TE TABLE cB(c DE
e0f0: 46 41 55 4c 54 20 58 27 39 39 39 39 27 20 52 45  FAULT X'9999' RE
e100: 46 45 52 45 4e 43 45 53 20 70 41 20 4f 4e 20 55  FERENCES pA ON U
e110: 50 44 41 54 45 20 53 45 54 20 44 45 46 41 55 4c  PDATE SET DEFAUL
e120: 54 29 3b 0a 0a 20 20 20 20 49 4e 53 45 52 54 20  T);..    INSERT 
e130: 49 4e 54 4f 20 70 41 28 72 6f 77 69 64 2c 20 78  INTO pA(rowid, x
e140: 29 20 56 41 4c 55 45 53 28 31 2c 20 58 27 30 30  ) VALUES(1, X'00
e150: 30 30 27 29 3b 0a 20 20 20 20 49 4e 53 45 52 54  00');.    INSERT
e160: 20 49 4e 54 4f 20 70 41 28 72 6f 77 69 64 2c 20   INTO pA(rowid, 
e170: 78 29 20 56 41 4c 55 45 53 28 32 2c 20 58 27 39  x) VALUES(2, X'9
e180: 39 39 39 27 29 3b 0a 20 20 20 20 49 4e 53 45 52  999');.    INSER
e190: 54 20 49 4e 54 4f 20 70 41 28 72 6f 77 69 64 2c  T INTO pA(rowid,
e1a0: 20 78 29 20 56 41 4c 55 45 53 28 33 2c 20 58 27   x) VALUES(3, X'
e1b0: 41 42 43 44 27 29 3b 0a 20 20 20 20 49 4e 53 45  ABCD');.    INSE
e1c0: 52 54 20 49 4e 54 4f 20 70 41 28 72 6f 77 69 64  RT INTO pA(rowid
e1d0: 2c 20 78 29 20 56 41 4c 55 45 53 28 34 2c 20 58  , x) VALUES(4, X
e1e0: 27 31 32 33 34 27 29 3b 0a 0a 20 20 20 20 49 4e  '1234');..    IN
e1f0: 53 45 52 54 20 49 4e 54 4f 20 63 41 20 56 41 4c  SERT INTO cA VAL
e200: 55 45 53 28 58 27 41 42 43 44 27 29 3b 0a 20 20  UES(X'ABCD');.  
e210: 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 63 42    INSERT INTO cB
e220: 20 56 41 4c 55 45 53 28 58 27 31 32 33 34 27 29   VALUES(X'1234')
e230: 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a 64 6f 5f 74 65  ;.  }.} {}.do_te
e240: 73 74 20 65 5f 66 6b 65 79 2d 32 32 2e 32 20 7b  st e_fkey-22.2 {
e250: 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20  .  execsql {.   
e260: 20 44 45 4c 45 54 45 20 46 52 4f 4d 20 70 41 20   DELETE FROM pA 
e270: 57 48 45 52 45 20 72 6f 77 69 64 20 3d 20 33 3b  WHERE rowid = 3;
e280: 0a 20 20 20 20 53 45 4c 45 43 54 20 71 75 6f 74  .    SELECT quot
e290: 65 28 78 29 20 46 52 4f 4d 20 70 41 3b 0a 20 20  e(x) FROM pA;.  
e2a0: 7d 0a 7d 20 7b 58 27 30 30 30 30 27 20 58 27 39  }.} {X'0000' X'9
e2b0: 39 39 39 27 20 58 27 31 32 33 34 27 7d 0a 64 6f  999' X'1234'}.do
e2c0: 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 32 32 2e  _test e_fkey-22.
e2d0: 33 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 20  3 {.  execsql { 
e2e0: 53 45 4c 45 43 54 20 71 75 6f 74 65 28 63 29 20  SELECT quote(c) 
e2f0: 46 52 4f 4d 20 63 41 20 7d 0a 7d 20 7b 58 27 30  FROM cA }.} {X'0
e300: 30 30 30 27 7d 0a 64 6f 5f 74 65 73 74 20 65 5f  000'}.do_test e_
e310: 66 6b 65 79 2d 32 32 2e 34 20 7b 0a 20 20 65 78  fkey-22.4 {.  ex
e320: 65 63 73 71 6c 20 7b 0a 20 20 20 20 55 50 44 41  ecsql {.    UPDA
e330: 54 45 20 70 41 20 53 45 54 20 78 20 3d 20 58 27  TE pA SET x = X'
e340: 38 37 36 35 27 20 57 48 45 52 45 20 72 6f 77 69  8765' WHERE rowi
e350: 64 20 3d 20 34 3b 0a 20 20 20 20 53 45 4c 45 43  d = 4;.    SELEC
e360: 54 20 71 75 6f 74 65 28 78 29 20 46 52 4f 4d 20  T quote(x) FROM 
e370: 70 41 3b 0a 20 20 7d 0a 7d 20 7b 58 27 30 30 30  pA;.  }.} {X'000
e380: 30 27 20 58 27 39 39 39 39 27 20 58 27 38 37 36  0' X'9999' X'876
e390: 35 27 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b  5'}.do_test e_fk
e3a0: 65 79 2d 32 32 2e 35 20 7b 0a 20 20 65 78 65 63  ey-22.5 {.  exec
e3b0: 73 71 6c 20 7b 20 53 45 4c 45 43 54 20 71 75 6f  sql { SELECT quo
e3c0: 74 65 28 63 29 20 46 52 4f 4d 20 63 42 20 7d 0a  te(c) FROM cB }.
e3d0: 7d 20 7b 58 27 39 39 39 39 27 7d 0a 0a 23 2d 2d  } {X'9999'}..#--
e3e0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
e3f0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
e400: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
e410: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
e420: 2d 2d 2d 2d 2d 2d 2d 0a 23 20 2f 2a 20 45 56 3a  -------.# /* EV:
e430: 20 52 2d 36 31 33 37 36 2d 35 37 32 36 37 20 2a   R-61376-57267 *
e440: 2f 0a 23 20 2f 2a 20 45 56 3a 20 52 2d 36 31 38  /.# /* EV: R-618
e450: 30 39 2d 36 32 32 30 37 20 2a 2f 0a 23 0a 23 20  09-62207 */.#.# 
e460: 54 65 73 74 20 4f 4e 20 44 45 4c 45 54 45 20 43  Test ON DELETE C
e470: 41 53 43 41 44 45 20 61 63 74 69 6f 6e 73 2e 0a  ASCADE actions..
e480: 23 0a 64 72 6f 70 5f 61 6c 6c 5f 74 61 62 6c 65  #.drop_all_table
e490: 73 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79  s.do_test e_fkey
e4a0: 2d 32 33 2e 31 20 7b 0a 20 20 65 78 65 63 73 71  -23.1 {.  execsq
e4b0: 6c 20 7b 0a 20 20 20 20 43 52 45 41 54 45 20 54  l {.    CREATE T
e4c0: 41 42 4c 45 20 70 31 28 61 2c 20 62 20 55 4e 49  ABLE p1(a, b UNI
e4d0: 51 55 45 29 3b 0a 20 20 20 20 43 52 45 41 54 45  QUE);.    CREATE
e4e0: 20 54 41 42 4c 45 20 63 31 28 63 20 52 45 46 45   TABLE c1(c REFE
e4f0: 52 45 4e 43 45 53 20 70 31 28 62 29 20 4f 4e 20  RENCES p1(b) ON 
e500: 44 45 4c 45 54 45 20 43 41 53 43 41 44 45 2c 20  DELETE CASCADE, 
e510: 64 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49  d);.    INSERT I
e520: 4e 54 4f 20 70 31 20 56 41 4c 55 45 53 28 4e 55  NTO p1 VALUES(NU
e530: 4c 4c 2c 20 4e 55 4c 4c 29 3b 0a 20 20 20 20 49  LL, NULL);.    I
e540: 4e 53 45 52 54 20 49 4e 54 4f 20 70 31 20 56 41  NSERT INTO p1 VA
e550: 4c 55 45 53 28 34 2c 20 34 29 3b 0a 20 20 20 20  LUES(4, 4);.    
e560: 49 4e 53 45 52 54 20 49 4e 54 4f 20 70 31 20 56  INSERT INTO p1 V
e570: 41 4c 55 45 53 28 35 2c 20 35 29 3b 0a 20 20 20  ALUES(5, 5);.   
e580: 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 63 31 20   INSERT INTO c1 
e590: 56 41 4c 55 45 53 28 4e 55 4c 4c 2c 20 4e 55 4c  VALUES(NULL, NUL
e5a0: 4c 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49  L);.    INSERT I
e5b0: 4e 54 4f 20 63 31 20 56 41 4c 55 45 53 28 34 2c  NTO c1 VALUES(4,
e5c0: 20 34 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20   4);.    INSERT 
e5d0: 49 4e 54 4f 20 63 31 20 56 41 4c 55 45 53 28 35  INTO c1 VALUES(5
e5e0: 2c 20 35 29 3b 0a 20 20 20 20 53 45 4c 45 43 54  , 5);.    SELECT
e5f0: 20 63 6f 75 6e 74 28 2a 29 20 46 52 4f 4d 20 63   count(*) FROM c
e600: 31 3b 0a 20 20 7d 0a 7d 20 7b 33 7d 0a 64 6f 5f  1;.  }.} {3}.do_
e610: 74 65 73 74 20 65 5f 66 6b 65 79 2d 32 33 2e 32  test e_fkey-23.2
e620: 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20   {.  execsql {. 
e630: 20 20 20 44 45 4c 45 54 45 20 46 52 4f 4d 20 70     DELETE FROM p
e640: 31 20 57 48 45 52 45 20 61 20 3d 20 34 3b 0a 20  1 WHERE a = 4;. 
e650: 20 20 20 53 45 4c 45 43 54 20 64 2c 20 63 20 46     SELECT d, c F
e660: 52 4f 4d 20 63 31 3b 0a 20 20 7d 0a 7d 20 7b 7b  ROM c1;.  }.} {{
e670: 7d 20 7b 7d 20 35 20 35 7d 0a 64 6f 5f 74 65 73  } {} 5 5}.do_tes
e680: 74 20 65 5f 66 6b 65 79 2d 32 33 2e 33 20 7b 0a  t e_fkey-23.3 {.
e690: 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20    execsql {.    
e6a0: 44 45 4c 45 54 45 20 46 52 4f 4d 20 70 31 3b 0a  DELETE FROM p1;.
e6b0: 20 20 20 20 53 45 4c 45 43 54 20 64 2c 20 63 20      SELECT d, c 
e6c0: 46 52 4f 4d 20 63 31 3b 0a 20 20 7d 0a 7d 20 7b  FROM c1;.  }.} {
e6d0: 7b 7d 20 7b 7d 7d 0a 64 6f 5f 74 65 73 74 20 65  {} {}}.do_test e
e6e0: 5f 66 6b 65 79 2d 32 33 2e 34 20 7b 0a 20 20 65  _fkey-23.4 {.  e
e6f0: 78 65 63 73 71 6c 20 7b 20 53 45 4c 45 43 54 20  xecsql { SELECT 
e700: 2a 20 46 52 4f 4d 20 70 31 20 7d 0a 7d 20 7b 7d  * FROM p1 }.} {}
e710: 0a 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ...#------------
e720: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
e730: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
e740: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
e750: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20  -------------.# 
e760: 2f 2a 20 45 56 3a 20 52 2d 36 31 33 37 36 2d 35  /* EV: R-61376-5
e770: 37 32 36 37 20 2a 2f 0a 23 20 2f 2a 20 45 56 3a  7267 */.# /* EV:
e780: 20 52 2d 31 33 38 37 37 2d 36 34 35 34 32 20 2a   R-13877-64542 *
e790: 2f 0a 23 0a 23 20 54 65 73 74 20 4f 4e 20 55 50  /.#.# Test ON UP
e7a0: 44 41 54 45 20 43 41 53 43 41 44 45 20 61 63 74  DATE CASCADE act
e7b0: 69 6f 6e 73 2e 0a 23 0a 64 72 6f 70 5f 61 6c 6c  ions..#.drop_all
e7c0: 5f 74 61 62 6c 65 73 0a 64 6f 5f 74 65 73 74 20  _tables.do_test 
e7d0: 65 5f 66 6b 65 79 2d 32 34 2e 31 20 7b 0a 20 20  e_fkey-24.1 {.  
e7e0: 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 43 52  execsql {.    CR
e7f0: 45 41 54 45 20 54 41 42 4c 45 20 70 31 28 61 2c  EATE TABLE p1(a,
e800: 20 62 20 55 4e 49 51 55 45 29 3b 0a 20 20 20 20   b UNIQUE);.    
e810: 43 52 45 41 54 45 20 54 41 42 4c 45 20 63 31 28  CREATE TABLE c1(
e820: 63 20 52 45 46 45 52 45 4e 43 45 53 20 70 31 28  c REFERENCES p1(
e830: 62 29 20 4f 4e 20 55 50 44 41 54 45 20 43 41 53  b) ON UPDATE CAS
e840: 43 41 44 45 2c 20 64 29 3b 0a 20 20 20 20 49 4e  CADE, d);.    IN
e850: 53 45 52 54 20 49 4e 54 4f 20 70 31 20 56 41 4c  SERT INTO p1 VAL
e860: 55 45 53 28 4e 55 4c 4c 2c 20 4e 55 4c 4c 29 3b  UES(NULL, NULL);
e870: 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f  .    INSERT INTO
e880: 20 70 31 20 56 41 4c 55 45 53 28 34 2c 20 34 29   p1 VALUES(4, 4)
e890: 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54  ;.    INSERT INT
e8a0: 4f 20 70 31 20 56 41 4c 55 45 53 28 35 2c 20 35  O p1 VALUES(5, 5
e8b0: 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e  );.    INSERT IN
e8c0: 54 4f 20 63 31 20 56 41 4c 55 45 53 28 4e 55 4c  TO c1 VALUES(NUL
e8d0: 4c 2c 20 4e 55 4c 4c 29 3b 0a 20 20 20 20 49 4e  L, NULL);.    IN
e8e0: 53 45 52 54 20 49 4e 54 4f 20 63 31 20 56 41 4c  SERT INTO c1 VAL
e8f0: 55 45 53 28 34 2c 20 34 29 3b 0a 20 20 20 20 49  UES(4, 4);.    I
e900: 4e 53 45 52 54 20 49 4e 54 4f 20 63 31 20 56 41  NSERT INTO c1 VA
e910: 4c 55 45 53 28 35 2c 20 35 29 3b 0a 20 20 20 20  LUES(5, 5);.    
e920: 53 45 4c 45 43 54 20 63 6f 75 6e 74 28 2a 29 20  SELECT count(*) 
e930: 46 52 4f 4d 20 63 31 3b 0a 20 20 7d 0a 7d 20 7b  FROM c1;.  }.} {
e940: 33 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65  3}.do_test e_fke
e950: 79 2d 32 34 2e 32 20 7b 0a 20 20 65 78 65 63 73  y-24.2 {.  execs
e960: 71 6c 20 7b 0a 20 20 20 20 55 50 44 41 54 45 20  ql {.    UPDATE 
e970: 70 31 20 53 45 54 20 62 20 3d 20 31 30 20 57 48  p1 SET b = 10 WH
e980: 45 52 45 20 62 20 3d 20 35 3b 0a 20 20 20 20 53  ERE b = 5;.    S
e990: 45 4c 45 43 54 20 64 2c 20 63 20 46 52 4f 4d 20  ELECT d, c FROM 
e9a0: 63 31 3b 0a 20 20 7d 0a 7d 20 7b 7b 7d 20 7b 7d  c1;.  }.} {{} {}
e9b0: 20 34 20 34 20 35 20 31 30 7d 0a 64 6f 5f 74 65   4 4 5 10}.do_te
e9c0: 73 74 20 65 5f 66 6b 65 79 2d 32 34 2e 33 20 7b  st e_fkey-24.3 {
e9d0: 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20  .  execsql {.   
e9e0: 20 55 50 44 41 54 45 20 70 31 20 53 45 54 20 62   UPDATE p1 SET b
e9f0: 20 3d 20 31 31 20 57 48 45 52 45 20 62 20 3d 20   = 11 WHERE b = 
ea00: 34 3b 0a 20 20 20 20 53 45 4c 45 43 54 20 64 2c  4;.    SELECT d,
ea10: 20 63 20 46 52 4f 4d 20 63 31 3b 0a 20 20 7d 0a   c FROM c1;.  }.
ea20: 7d 20 7b 7b 7d 20 7b 7d 20 34 20 31 31 20 35 20  } {{} {} 4 11 5 
ea30: 31 30 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b  10}.do_test e_fk
ea40: 65 79 2d 32 34 2e 34 20 7b 0a 20 20 65 78 65 63  ey-24.4 {.  exec
ea50: 73 71 6c 20 7b 20 0a 20 20 20 20 55 50 44 41 54  sql { .    UPDAT
ea60: 45 20 70 31 20 53 45 54 20 62 20 3d 20 36 20 57  E p1 SET b = 6 W
ea70: 48 45 52 45 20 62 20 49 53 20 4e 55 4c 4c 3b 0a  HERE b IS NULL;.
ea80: 20 20 20 20 53 45 4c 45 43 54 20 64 2c 20 63 20      SELECT d, c 
ea90: 46 52 4f 4d 20 63 31 3b 0a 20 20 7d 0a 7d 20 7b  FROM c1;.  }.} {
eaa0: 7b 7d 20 7b 7d 20 34 20 31 31 20 35 20 31 30 7d  {} {} 4 11 5 10}
eab0: 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d  .do_test e_fkey-
eac0: 32 33 2e 35 20 7b 0a 20 20 65 78 65 63 73 71 6c  23.5 {.  execsql
ead0: 20 7b 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d   { SELECT * FROM
eae0: 20 70 31 20 7d 0a 7d 20 7b 7b 7d 20 36 20 34 20   p1 }.} {{} 6 4 
eaf0: 31 31 20 35 20 31 30 7d 0a 0a 23 2d 2d 2d 2d 2d  11 5 10}..#-----
eb00: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
eb10: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
eb20: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
eb30: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
eb40: 2d 2d 2d 2d 0a 23 20 2f 2a 20 45 56 3a 20 52 2d  ----.# /* EV: R-
eb50: 35 31 33 32 39 2d 33 33 34 33 38 20 2a 2f 0a 23  51329-33438 */.#
eb60: 0a 23 20 54 65 73 74 20 61 6e 20 65 78 61 6d 70  .# Test an examp
eb70: 6c 65 20 66 72 6f 6d 20 74 68 65 20 22 4f 4e 20  le from the "ON 
eb80: 44 45 4c 45 54 45 20 61 6e 64 20 4f 4e 20 55 50  DELETE and ON UP
eb90: 44 41 54 45 20 41 63 74 69 6f 6e 73 22 20 73 65  DATE Actions" se
eba0: 63 74 69 6f 6e 20 0a 23 20 6f 66 20 66 6f 72 65  ction .# of fore
ebb0: 69 67 6e 6b 65 79 73 2e 68 74 6d 6c 2e 0a 23 0a  ignkeys.html..#.
ebc0: 64 72 6f 70 5f 61 6c 6c 5f 74 61 62 6c 65 73 0a  drop_all_tables.
ebd0: 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 31  do_test e_fkey-1
ebe0: 35 2e 31 20 7b 0a 20 20 65 78 65 63 73 71 6c 20  5.1 {.  execsql 
ebf0: 7b 0a 20 20 20 20 43 52 45 41 54 45 20 54 41 42  {.    CREATE TAB
ec00: 4c 45 20 61 72 74 69 73 74 28 0a 20 20 20 20 20  LE artist(.     
ec10: 20 61 72 74 69 73 74 69 64 20 20 20 20 49 4e 54   artistid    INT
ec20: 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59  EGER PRIMARY KEY
ec30: 2c 20 0a 20 20 20 20 20 20 61 72 74 69 73 74 6e  , .      artistn
ec40: 61 6d 65 20 20 54 45 58 54 0a 20 20 20 20 29 3b  ame  TEXT.    );
ec50: 0a 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c  .    CREATE TABL
ec60: 45 20 74 72 61 63 6b 28 0a 20 20 20 20 20 20 74  E track(.      t
ec70: 72 61 63 6b 69 64 20 20 20 20 20 49 4e 54 45 47  rackid     INTEG
ec80: 45 52 2c 0a 20 20 20 20 20 20 74 72 61 63 6b 6e  ER,.      trackn
ec90: 61 6d 65 20 20 20 54 45 58 54 2c 20 0a 20 20 20  ame   TEXT, .   
eca0: 20 20 20 74 72 61 63 6b 61 72 74 69 73 74 20 49     trackartist I
ecb0: 4e 54 45 47 45 52 20 52 45 46 45 52 45 4e 43 45  NTEGER REFERENCE
ecc0: 53 20 61 72 74 69 73 74 28 61 72 74 69 73 74 69  S artist(artisti
ecd0: 64 29 20 4f 4e 20 55 50 44 41 54 45 20 43 41 53  d) ON UPDATE CAS
ece0: 43 41 44 45 0a 20 20 20 20 29 3b 0a 0a 20 20 20  CADE.    );..   
ecf0: 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 61 72 74   INSERT INTO art
ed00: 69 73 74 20 56 41 4c 55 45 53 28 31 2c 20 27 44  ist VALUES(1, 'D
ed10: 65 61 6e 20 4d 61 72 74 69 6e 27 29 3b 0a 20 20  ean Martin');.  
ed20: 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 61 72    INSERT INTO ar
ed30: 74 69 73 74 20 56 41 4c 55 45 53 28 32 2c 20 27  tist VALUES(2, '
ed40: 46 72 61 6e 6b 20 53 69 6e 61 74 72 61 27 29 3b  Frank Sinatra');
ed50: 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f  .    INSERT INTO
ed60: 20 74 72 61 63 6b 20 56 41 4c 55 45 53 28 31 31   track VALUES(11
ed70: 2c 20 27 54 68 61 74 27 27 73 20 41 6d 6f 72 65  , 'That''s Amore
ed80: 27 2c 20 31 29 3b 0a 20 20 20 20 49 4e 53 45 52  ', 1);.    INSER
ed90: 54 20 49 4e 54 4f 20 74 72 61 63 6b 20 56 41 4c  T INTO track VAL
eda0: 55 45 53 28 31 32 2c 20 27 43 68 72 69 73 74 6d  UES(12, 'Christm
edb0: 61 73 20 42 6c 75 65 73 27 2c 20 31 29 3b 0a 20  as Blues', 1);. 
edc0: 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74     INSERT INTO t
edd0: 72 61 63 6b 20 56 41 4c 55 45 53 28 31 33 2c 20  rack VALUES(13, 
ede0: 27 4d 79 20 57 61 79 27 2c 20 32 29 3b 0a 20 20  'My Way', 2);.  
edf0: 7d 0a 7d 20 7b 7d 0a 64 6f 5f 74 65 73 74 20 65  }.} {}.do_test e
ee00: 5f 66 6b 65 79 2d 31 35 2e 32 20 7b 0a 20 20 65  _fkey-15.2 {.  e
ee10: 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 55 50 44  xecsql {.    UPD
ee20: 41 54 45 20 61 72 74 69 73 74 20 53 45 54 20 61  ATE artist SET a
ee30: 72 74 69 73 74 69 64 20 3d 20 31 30 30 20 57 48  rtistid = 100 WH
ee40: 45 52 45 20 61 72 74 69 73 74 6e 61 6d 65 20 3d  ERE artistname =
ee50: 20 27 44 65 61 6e 20 4d 61 72 74 69 6e 27 3b 0a   'Dean Martin';.
ee60: 20 20 7d 0a 7d 20 7b 7d 0a 64 6f 5f 74 65 73 74    }.} {}.do_test
ee70: 20 65 5f 66 6b 65 79 2d 31 35 2e 33 20 7b 0a 20   e_fkey-15.3 {. 
ee80: 20 65 78 65 63 73 71 6c 20 7b 20 53 45 4c 45 43   execsql { SELEC
ee90: 54 20 2a 20 46 52 4f 4d 20 61 72 74 69 73 74 20  T * FROM artist 
eea0: 7d 0a 7d 20 7b 32 20 7b 46 72 61 6e 6b 20 53 69  }.} {2 {Frank Si
eeb0: 6e 61 74 72 61 7d 20 31 30 30 20 7b 44 65 61 6e  natra} 100 {Dean
eec0: 20 4d 61 72 74 69 6e 7d 7d 0a 64 6f 5f 74 65 73   Martin}}.do_tes
eed0: 74 20 65 5f 66 6b 65 79 2d 31 35 2e 34 20 7b 0a  t e_fkey-15.4 {.
eee0: 20 20 65 78 65 63 73 71 6c 20 7b 20 53 45 4c 45    execsql { SELE
eef0: 43 54 20 2a 20 46 52 4f 4d 20 74 72 61 63 6b 20  CT * FROM track 
ef00: 7d 0a 7d 20 7b 31 31 20 7b 54 68 61 74 27 73 20  }.} {11 {That's 
ef10: 41 6d 6f 72 65 7d 20 31 30 30 20 31 32 20 7b 43  Amore} 100 12 {C
ef20: 68 72 69 73 74 6d 61 73 20 42 6c 75 65 73 7d 20  hristmas Blues} 
ef30: 31 30 30 20 31 33 20 7b 4d 79 20 57 61 79 7d 20  100 13 {My Way} 
ef40: 32 7d 0a 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  2}...#----------
ef50: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
ef60: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
ef70: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
ef80: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a  ---------------.
ef90: 23 20 2f 2a 20 45 56 3a 20 52 2d 35 33 39 36 38  # /* EV: R-53968
efa0: 2d 35 31 36 34 32 20 2a 2f 0a 23 0a 23 20 56 65  -51642 */.#.# Ve
efb0: 72 69 66 79 20 74 68 61 74 20 61 64 64 69 6e 67  rify that adding
efc0: 20 61 6e 20 46 4b 20 61 63 74 69 6f 6e 20 64 6f   an FK action do
efd0: 65 73 20 6e 6f 74 20 61 62 73 6f 6c 76 65 20 74  es not absolve t
efe0: 68 65 20 75 73 65 72 20 6f 66 20 74 68 65 20 0a  he user of the .
eff0: 23 20 72 65 71 75 69 72 65 6d 65 6e 74 20 6e 6f  # requirement no
f000: 74 20 74 6f 20 76 69 6f 6c 61 74 65 20 74 68 65  t to violate the
f010: 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e   foreign key con
f020: 73 74 72 61 69 6e 74 2e 0a 23 0a 64 72 6f 70 5f  straint..#.drop_
f030: 61 6c 6c 5f 74 61 62 6c 65 73 0a 64 6f 5f 74 65  all_tables.do_te
f040: 73 74 20 65 5f 66 6b 65 79 2d 32 35 2e 31 20 7b  st e_fkey-25.1 {
f050: 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20  .  execsql {.   
f060: 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 70 61   CREATE TABLE pa
f070: 72 65 6e 74 28 61 20 43 4f 4c 4c 41 54 45 20 6e  rent(a COLLATE n
f080: 6f 63 61 73 65 2c 20 62 2c 20 63 2c 20 50 52 49  ocase, b, c, PRI
f090: 4d 41 52 59 20 4b 45 59 28 63 2c 20 61 29 29 3b  MARY KEY(c, a));
f0a0: 0a 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c  .    CREATE TABL
f0b0: 45 20 63 68 69 6c 64 28 64 20 44 45 46 41 55 4c  E child(d DEFAUL
f0c0: 54 20 27 61 27 2c 20 65 2c 20 66 20 44 45 46 41  T 'a', e, f DEFA
f0d0: 55 4c 54 20 27 63 27 2c 0a 20 20 20 20 20 20 46  ULT 'c',.      F
f0e0: 4f 52 45 49 47 4e 20 4b 45 59 28 66 2c 20 64 29  OREIGN KEY(f, d)
f0f0: 20 52 45 46 45 52 45 4e 43 45 53 20 70 61 72 65   REFERENCES pare
f100: 6e 74 20 4f 4e 20 55 50 44 41 54 45 20 53 45 54  nt ON UPDATE SET
f110: 20 44 45 46 41 55 4c 54 0a 20 20 20 20 29 3b 0a   DEFAULT.    );.
f120: 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f  .    INSERT INTO
f130: 20 70 61 72 65 6e 74 20 56 41 4c 55 45 53 28 27   parent VALUES('
f140: 41 27 2c 20 27 62 27 2c 20 27 63 27 29 3b 0a 20  A', 'b', 'c');. 
f150: 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 70     INSERT INTO p
f160: 61 72 65 6e 74 20 56 41 4c 55 45 53 28 27 4f 4e  arent VALUES('ON
f170: 45 27 2c 20 27 74 77 6f 27 2c 20 27 74 68 72 65  E', 'two', 'thre
f180: 65 27 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20  e');.    INSERT 
f190: 49 4e 54 4f 20 63 68 69 6c 64 20 56 41 4c 55 45  INTO child VALUE
f1a0: 53 28 27 6f 6e 65 27 2c 20 27 74 77 6f 27 2c 20  S('one', 'two', 
f1b0: 27 74 68 72 65 65 27 29 3b 0a 20 20 7d 0a 7d 20  'three');.  }.} 
f1c0: 7b 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65  {}.do_test e_fke
f1d0: 79 2d 32 35 2e 32 20 7b 0a 20 20 65 78 65 63 73  y-25.2 {.  execs
f1e0: 71 6c 20 7b 0a 20 20 20 20 42 45 47 49 4e 3b 0a  ql {.    BEGIN;.
f1f0: 20 20 20 20 20 20 55 50 44 41 54 45 20 70 61 72        UPDATE par
f200: 65 6e 74 20 53 45 54 20 61 20 3d 20 27 27 20 57  ent SET a = '' W
f210: 48 45 52 45 20 61 20 3d 20 27 6f 4e 65 27 3b 0a  HERE a = 'oNe';.
f220: 20 20 20 20 20 20 53 45 4c 45 43 54 20 2a 20 46        SELECT * F
f230: 52 4f 4d 20 63 68 69 6c 64 3b 0a 20 20 7d 0a 7d  ROM child;.  }.}
f240: 20 7b 61 20 74 77 6f 20 63 7d 0a 64 6f 5f 74 65   {a two c}.do_te
f250: 73 74 20 65 5f 66 6b 65 79 2d 32 35 2e 33 20 7b  st e_fkey-25.3 {
f260: 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20  .  execsql {.   
f270: 20 52 4f 4c 4c 42 41 43 4b 3b 0a 20 20 20 20 44   ROLLBACK;.    D
f280: 45 4c 45 54 45 20 46 52 4f 4d 20 70 61 72 65 6e  ELETE FROM paren
f290: 74 20 57 48 45 52 45 20 61 20 3d 20 27 41 27 3b  t WHERE a = 'A';
f2a0: 0a 20 20 20 20 53 45 4c 45 43 54 20 2a 20 46 52  .    SELECT * FR
f2b0: 4f 4d 20 70 61 72 65 6e 74 3b 0a 20 20 7d 0a 7d  OM parent;.  }.}
f2c0: 20 7b 4f 4e 45 20 74 77 6f 20 74 68 72 65 65 7d   {ONE two three}
f2d0: 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d  .do_test e_fkey-
f2e0: 32 35 2e 34 20 7b 0a 20 20 63 61 74 63 68 73 71  25.4 {.  catchsq
f2f0: 6c 20 7b 20 55 50 44 41 54 45 20 70 61 72 65 6e  l { UPDATE paren
f300: 74 20 53 45 54 20 61 20 3d 20 27 27 20 57 48 45  t SET a = '' WHE
f310: 52 45 20 61 20 3d 20 27 6f 4e 65 27 20 7d 0a 7d  RE a = 'oNe' }.}
f320: 20 7b 31 20 7b 66 6f 72 65 69 67 6e 20 6b 65 79   {1 {foreign key
f330: 20 63 6f 6e 73 74 72 61 69 6e 74 20 66 61 69 6c   constraint fail
f340: 65 64 7d 7d 0a 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d  ed}}...#--------
f350: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
f360: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
f370: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
f380: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
f390: 2d 0a 23 20 2f 2a 20 45 56 3a 20 52 2d 30 37 30  -.# /* EV: R-070
f3a0: 36 35 2d 35 39 35 38 38 20 2a 2f 0a 23 20 2f 2a  65-59588 */.# /*
f3b0: 20 45 56 3a 20 52 2d 32 38 32 32 30 2d 34 36 36   EV: R-28220-466
f3c0: 39 34 20 2a 2f 0a 23 0a 23 20 54 65 73 74 20 61  94 */.#.# Test a
f3d0: 6e 20 65 78 61 6d 70 6c 65 20 66 72 6f 6d 20 74  n example from t
f3e0: 68 65 20 22 4f 4e 20 44 45 4c 45 54 45 20 61 6e  he "ON DELETE an
f3f0: 64 20 4f 4e 20 55 50 44 41 54 45 20 41 63 74 69  d ON UPDATE Acti
f400: 6f 6e 73 22 20 73 65 63 74 69 6f 6e 20 0a 23 20  ons" section .# 
f410: 6f 66 20 66 6f 72 65 69 67 6e 6b 65 79 73 2e 68  of foreignkeys.h
f420: 74 6d 6c 2e 20 54 68 69 73 20 65 78 61 6d 70 6c  tml. This exampl
f430: 65 20 73 68 6f 77 73 20 74 68 61 74 20 61 64 64  e shows that add
f440: 69 6e 67 20 61 6e 20 22 4f 4e 20 44 45 4c 45 54  ing an "ON DELET
f450: 45 20 44 45 46 41 55 4c 54 22 0a 23 20 63 6c 61  E DEFAULT".# cla
f460: 75 73 65 20 64 6f 65 73 20 6e 6f 74 20 61 62 72  use does not abr
f470: 6f 67 61 74 65 20 74 68 65 20 6e 65 65 64 20 74  ogate the need t
f480: 6f 20 73 61 74 69 73 66 79 20 74 68 65 20 66 6f  o satisfy the fo
f490: 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72  reign key constr
f4a0: 61 69 6e 74 0a 23 20 28 52 2d 32 38 32 32 30 2d  aint.# (R-28220-
f4b0: 34 36 36 39 34 29 2e 0a 23 0a 64 72 6f 70 5f 61  46694)..#.drop_a
f4c0: 6c 6c 5f 74 61 62 6c 65 73 0a 64 6f 5f 74 65 73  ll_tables.do_tes
f4d0: 74 20 65 5f 66 6b 65 79 2d 31 34 2e 31 20 7b 0a  t e_fkey-14.1 {.
f4e0: 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20    execsql {.    
f4f0: 43 52 45 41 54 45 20 54 41 42 4c 45 20 61 72 74  CREATE TABLE art
f500: 69 73 74 28 0a 20 20 20 20 20 20 61 72 74 69 73  ist(.      artis
f510: 74 69 64 20 20 20 20 49 4e 54 45 47 45 52 20 50  tid    INTEGER P
f520: 52 49 4d 41 52 59 20 4b 45 59 2c 20 0a 20 20 20  RIMARY KEY, .   
f530: 20 20 20 61 72 74 69 73 74 6e 61 6d 65 20 20 54     artistname  T
f540: 45 58 54 0a 20 20 20 20 29 3b 0a 20 20 20 20 43  EXT.    );.    C
f550: 52 45 41 54 45 20 54 41 42 4c 45 20 74 72 61 63  REATE TABLE trac
f560: 6b 28 0a 20 20 20 20 20 20 74 72 61 63 6b 69 64  k(.      trackid
f570: 20 20 20 20 20 49 4e 54 45 47 45 52 2c 0a 20 20       INTEGER,.  
f580: 20 20 20 20 74 72 61 63 6b 6e 61 6d 65 20 20 20      trackname   
f590: 54 45 58 54 2c 20 0a 20 20 20 20 20 20 74 72 61  TEXT, .      tra
f5a0: 63 6b 61 72 74 69 73 74 20 49 4e 54 45 47 45 52  ckartist INTEGER
f5b0: 20 44 45 46 41 55 4c 54 20 30 20 52 45 46 45 52   DEFAULT 0 REFER
f5c0: 45 4e 43 45 53 20 61 72 74 69 73 74 28 61 72 74  ENCES artist(art
f5d0: 69 73 74 69 64 29 20 4f 4e 20 44 45 4c 45 54 45  istid) ON DELETE
f5e0: 20 53 45 54 20 44 45 46 41 55 4c 54 0a 20 20 20   SET DEFAULT.   
f5f0: 20 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49   );.    INSERT I
f600: 4e 54 4f 20 61 72 74 69 73 74 20 56 41 4c 55 45  NTO artist VALUE
f610: 53 28 33 2c 20 27 53 61 6d 6d 79 20 44 61 76 69  S(3, 'Sammy Davi
f620: 73 20 4a 72 2e 27 29 3b 0a 20 20 20 20 49 4e 53  s Jr.');.    INS
f630: 45 52 54 20 49 4e 54 4f 20 74 72 61 63 6b 20 56  ERT INTO track V
f640: 41 4c 55 45 53 28 31 34 2c 20 27 4d 72 2e 20 42  ALUES(14, 'Mr. B
f650: 6f 6a 61 6e 67 6c 65 73 27 2c 20 33 29 3b 0a 20  ojangles', 3);. 
f660: 20 7d 0a 7d 20 7b 7d 0a 64 6f 5f 74 65 73 74 20   }.} {}.do_test 
f670: 65 5f 66 6b 65 79 2d 31 34 2e 32 20 7b 0a 20 20  e_fkey-14.2 {.  
f680: 63 61 74 63 68 73 71 6c 20 7b 20 44 45 4c 45 54  catchsql { DELET
f690: 45 20 46 52 4f 4d 20 61 72 74 69 73 74 20 57 48  E FROM artist WH
f6a0: 45 52 45 20 61 72 74 69 73 74 6e 61 6d 65 20 3d  ERE artistname =
f6b0: 20 27 53 61 6d 6d 79 20 44 61 76 69 73 20 4a 72   'Sammy Davis Jr
f6c0: 2e 27 20 7d 0a 7d 20 7b 31 20 7b 66 6f 72 65 69  .' }.} {1 {forei
f6d0: 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e  gn key constrain
f6e0: 74 20 66 61 69 6c 65 64 7d 7d 0a 64 6f 5f 74 65  t failed}}.do_te
f6f0: 73 74 20 65 5f 66 6b 65 79 2d 31 34 2e 33 20 7b  st e_fkey-14.3 {
f700: 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20  .  execsql {.   
f710: 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 61 72 74   INSERT INTO art
f720: 69 73 74 20 56 41 4c 55 45 53 28 30 2c 20 27 55  ist VALUES(0, 'U
f730: 6e 6b 6e 6f 77 6e 20 41 72 74 69 73 74 27 29 3b  nknown Artist');
f740: 0a 20 20 20 20 44 45 4c 45 54 45 20 46 52 4f 4d  .    DELETE FROM
f750: 20 61 72 74 69 73 74 20 57 48 45 52 45 20 61 72   artist WHERE ar
f760: 74 69 73 74 6e 61 6d 65 20 3d 20 27 53 61 6d 6d  tistname = 'Samm
f770: 79 20 44 61 76 69 73 20 4a 72 2e 27 3b 0a 20 20  y Davis Jr.';.  
f780: 7d 0a 7d 20 7b 7d 0a 64 6f 5f 74 65 73 74 20 65  }.} {}.do_test e
f790: 5f 66 6b 65 79 2d 31 34 2e 34 20 7b 0a 20 20 65  _fkey-14.4 {.  e
f7a0: 78 65 63 73 71 6c 20 7b 20 53 45 4c 45 43 54 20  xecsql { SELECT 
f7b0: 2a 20 46 52 4f 4d 20 61 72 74 69 73 74 20 7d 0a  * FROM artist }.
f7c0: 7d 20 7b 30 20 7b 55 6e 6b 6e 6f 77 6e 20 41 72  } {0 {Unknown Ar
f7d0: 74 69 73 74 7d 7d 0a 64 6f 5f 74 65 73 74 20 65  tist}}.do_test e
f7e0: 5f 66 6b 65 79 2d 31 34 2e 35 20 7b 0a 20 20 65  _fkey-14.5 {.  e
f7f0: 78 65 63 73 71 6c 20 7b 20 53 45 4c 45 43 54 20  xecsql { SELECT 
f800: 2a 20 46 52 4f 4d 20 74 72 61 63 6b 20 7d 0a 7d  * FROM track }.}
f810: 20 7b 31 34 20 7b 4d 72 2e 20 42 6f 6a 61 6e 67   {14 {Mr. Bojang
f820: 6c 65 73 7d 20 30 7d 0a 0a 23 2d 2d 2d 2d 2d 2d  les} 0}..#------
f830: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
f840: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
f850: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
f860: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
f870: 2d 2d 2d 0a 23 20 2f 2a 20 45 56 3a 20 52 2d 30  ---.# /* EV: R-0
f880: 39 35 36 34 2d 32 32 31 37 30 20 2a 2f 0a 23 0a  9564-22170 */.#.
f890: 23 20 43 68 65 63 6b 20 74 68 61 74 20 74 68 65  # Check that the
f8a0: 20 6f 72 64 65 72 20 6f 66 20 73 74 65 70 73 20   order of steps 
f8b0: 69 6e 20 61 6e 20 55 50 44 41 54 45 20 6f 72 20  in an UPDATE or 
f8c0: 44 45 4c 45 54 45 20 6f 6e 20 61 20 70 61 72 65  DELETE on a pare
f8d0: 6e 74 20 0a 23 20 74 61 62 6c 65 20 69 73 20 61  nt .# table is a
f8e0: 73 20 66 6f 6c 6c 6f 77 73 3a 0a 23 0a 23 20 20  s follows:.#.#  
f8f0: 20 31 2e 20 45 78 65 63 75 74 65 20 61 70 70 6c   1. Execute appl
f900: 69 63 61 62 6c 65 20 42 45 46 4f 52 45 20 74 72  icable BEFORE tr
f910: 69 67 67 65 72 20 70 72 6f 67 72 61 6d 73 2c 0a  igger programs,.
f920: 23 20 20 20 32 2e 20 43 68 65 63 6b 20 6c 6f 63  #   2. Check loc
f930: 61 6c 20 28 6e 6f 6e 20 66 6f 72 65 69 67 6e 20  al (non foreign 
f940: 6b 65 79 29 20 63 6f 6e 73 74 72 61 69 6e 74 73  key) constraints
f950: 2c 0a 23 20 20 20 33 2e 20 55 70 64 61 74 65 20  ,.#   3. Update 
f960: 6f 72 20 64 65 6c 65 74 65 20 74 68 65 20 72 6f  or delete the ro
f970: 77 20 69 6e 20 74 68 65 20 70 61 72 65 6e 74 20  w in the parent 
f980: 74 61 62 6c 65 2c 0a 23 20 20 20 34 2e 20 50 65  table,.#   4. Pe
f990: 72 66 6f 72 6d 20 61 6e 79 20 72 65 71 75 69 72  rform any requir
f9a0: 65 64 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 61  ed foreign key a
f9b0: 63 74 69 6f 6e 73 2c 0a 23 20 20 20 35 2e 20 45  ctions,.#   5. E
f9c0: 78 65 63 75 74 65 20 61 70 70 6c 69 63 61 62 6c  xecute applicabl
f9d0: 65 20 41 46 54 45 52 20 74 72 69 67 67 65 72 20  e AFTER trigger 
f9e0: 70 72 6f 67 72 61 6d 73 2e 20 0a 23 0a 64 72 6f  programs. .#.dro
f9f0: 70 5f 61 6c 6c 5f 74 61 62 6c 65 73 0a 64 6f 5f  p_all_tables.do_
fa00: 74 65 73 74 20 65 5f 66 6b 65 79 2d 32 37 2e 31  test e_fkey-27.1
fa10: 20 7b 0a 20 20 70 72 6f 63 20 6d 61 78 70 61 72   {.  proc maxpar
fa20: 65 6e 74 20 7b 61 72 67 73 7d 20 7b 20 64 62 20  ent {args} { db 
fa30: 6f 6e 65 20 7b 53 45 4c 45 43 54 20 6d 61 78 28  one {SELECT max(
fa40: 78 29 20 46 52 4f 4d 20 70 61 72 65 6e 74 7d 20  x) FROM parent} 
fa50: 7d 0a 20 20 64 62 20 66 75 6e 63 20 6d 61 78 70  }.  db func maxp
fa60: 61 72 65 6e 74 20 6d 61 78 70 61 72 65 6e 74 0a  arent maxparent.
fa70: 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20  .  execsql {.   
fa80: 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 70 61   CREATE TABLE pa
fa90: 72 65 6e 74 28 78 20 50 52 49 4d 41 52 59 20 4b  rent(x PRIMARY K
faa0: 45 59 29 3b 0a 0a 20 20 20 20 43 52 45 41 54 45  EY);..    CREATE
fab0: 20 54 52 49 47 47 45 52 20 62 75 20 42 45 46 4f   TRIGGER bu BEFO
fac0: 52 45 20 55 50 44 41 54 45 20 4f 4e 20 70 61 72  RE UPDATE ON par
fad0: 65 6e 74 20 42 45 47 49 4e 0a 20 20 20 20 20 20  ent BEGIN.      
fae0: 49 4e 53 45 52 54 20 49 4e 54 4f 20 70 61 72 65  INSERT INTO pare
faf0: 6e 74 20 56 41 4c 55 45 53 28 6e 65 77 2e 78 2d  nt VALUES(new.x-
fb00: 6f 6c 64 2e 78 29 3b 0a 20 20 20 20 45 4e 44 3b  old.x);.    END;
fb10: 0a 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c  .    CREATE TABL
fb20: 45 20 63 68 69 6c 64 28 0a 20 20 20 20 20 20 61  E child(.      a
fb30: 20 44 45 46 41 55 4c 54 20 28 6d 61 78 70 61 72   DEFAULT (maxpar
fb40: 65 6e 74 28 29 29 20 52 45 46 45 52 45 4e 43 45  ent()) REFERENCE
fb50: 53 20 70 61 72 65 6e 74 20 4f 4e 20 55 50 44 41  S parent ON UPDA
fb60: 54 45 20 53 45 54 20 44 45 46 41 55 4c 54 0a 20  TE SET DEFAULT. 
fb70: 20 20 20 29 3b 0a 20 20 20 20 43 52 45 41 54 45     );.    CREATE
fb80: 20 54 52 49 47 47 45 52 20 61 75 20 41 46 54 45   TRIGGER au AFTE
fb90: 52 20 55 50 44 41 54 45 20 4f 4e 20 70 61 72 65  R UPDATE ON pare
fba0: 6e 74 20 42 45 47 49 4e 0a 20 20 20 20 20 20 49  nt BEGIN.      I
fbb0: 4e 53 45 52 54 20 49 4e 54 4f 20 70 61 72 65 6e  NSERT INTO paren
fbc0: 74 20 56 41 4c 55 45 53 28 6e 65 77 2e 78 2b 6f  t VALUES(new.x+o
fbd0: 6c 64 2e 78 29 3b 0a 20 20 20 20 45 4e 44 3b 0a  ld.x);.    END;.
fbe0: 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f  .    INSERT INTO
fbf0: 20 70 61 72 65 6e 74 20 56 41 4c 55 45 53 28 31   parent VALUES(1
fc00: 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e  );.    INSERT IN
fc10: 54 4f 20 63 68 69 6c 64 20 56 41 4c 55 45 53 28  TO child VALUES(
fc20: 31 29 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a 64 6f 5f  1);.  }.} {}.do_
fc30: 74 65 73 74 20 65 5f 66 6b 65 79 2d 32 37 2e 32  test e_fkey-27.2
fc40: 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20   {.  execsql {. 
fc50: 20 20 20 55 50 44 41 54 45 20 70 61 72 65 6e 74     UPDATE parent
fc60: 20 53 45 54 20 78 20 3d 20 32 32 3b 0a 20 20 20   SET x = 22;.   
fc70: 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 70   SELECT * FROM p
fc80: 61 72 65 6e 74 20 55 4e 49 4f 4e 20 41 4c 4c 20  arent UNION ALL 
fc90: 53 45 4c 45 43 54 20 27 78 78 78 27 20 55 4e 49  SELECT 'xxx' UNI
fca0: 4f 4e 20 41 4c 4c 20 53 45 4c 45 43 54 20 61 20  ON ALL SELECT a 
fcb0: 46 52 4f 4d 20 63 68 69 6c 64 3b 0a 20 20 7d 0a  FROM child;.  }.
fcc0: 7d 20 7b 32 32 20 32 31 20 32 33 20 78 78 78 20  } {22 21 23 xxx 
fcd0: 32 32 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b  22}.do_test e_fk
fce0: 65 79 2d 32 37 2e 33 20 7b 0a 20 20 65 78 65 63  ey-27.3 {.  exec
fcf0: 73 71 6c 20 7b 0a 20 20 20 20 44 45 4c 45 54 45  sql {.    DELETE
fd00: 20 46 52 4f 4d 20 63 68 69 6c 64 3b 0a 20 20 20   FROM child;.   
fd10: 20 44 45 4c 45 54 45 20 46 52 4f 4d 20 70 61 72   DELETE FROM par
fd20: 65 6e 74 3b 0a 20 20 20 20 49 4e 53 45 52 54 20  ent;.    INSERT 
fd30: 49 4e 54 4f 20 70 61 72 65 6e 74 20 56 41 4c 55  INTO parent VALU
fd40: 45 53 28 2d 31 29 3b 0a 20 20 20 20 49 4e 53 45  ES(-1);.    INSE
fd50: 52 54 20 49 4e 54 4f 20 63 68 69 6c 64 20 56 41  RT INTO child VA
fd60: 4c 55 45 53 28 2d 31 29 3b 0a 20 20 20 20 55 50  LUES(-1);.    UP
fd70: 44 41 54 45 20 70 61 72 65 6e 74 20 53 45 54 20  DATE parent SET 
fd80: 78 20 3d 20 32 32 3b 0a 20 20 20 20 53 45 4c 45  x = 22;.    SELE
fd90: 43 54 20 2a 20 46 52 4f 4d 20 70 61 72 65 6e 74  CT * FROM parent
fda0: 20 55 4e 49 4f 4e 20 41 4c 4c 20 53 45 4c 45 43   UNION ALL SELEC
fdb0: 54 20 27 78 78 78 27 20 55 4e 49 4f 4e 20 41 4c  T 'xxx' UNION AL
fdc0: 4c 20 53 45 4c 45 43 54 20 61 20 46 52 4f 4d 20  L SELECT a FROM 
fdd0: 63 68 69 6c 64 3b 0a 20 20 7d 0a 7d 20 7b 32 32  child;.  }.} {22
fde0: 20 32 33 20 32 31 20 78 78 78 20 32 33 7d 0a 0a   23 21 xxx 23}..
fdf0: 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  .#--------------
fe00: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
fe10: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
fe20: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
fe30: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20 2f 2a  -----------.# /*
fe40: 20 45 56 3a 20 52 2d 32 37 33 38 33 2d 31 30 32   EV: R-27383-102
fe50: 34 36 20 2a 2f 0a 23 0a 23 20 56 65 72 69 66 79  46 */.#.# Verify
fe60: 20 74 68 61 74 20 4f 4e 20 55 50 44 41 54 45 20   that ON UPDATE 
fe70: 61 63 74 69 6f 6e 73 20 6f 6e 6c 79 20 61 63 74  actions only act
fe80: 75 61 6c 6c 79 20 74 61 6b 65 20 70 6c 61 63 65  ually take place
fe90: 20 69 66 20 74 68 65 20 70 61 72 65 6e 74 20 6b   if the parent k
fea0: 65 79 0a 23 20 69 73 20 73 65 74 20 74 6f 20 61  ey.# is set to a
feb0: 20 6e 65 77 20 76 61 6c 75 65 20 74 68 61 74 20   new value that 
fec0: 69 73 20 64 69 73 74 69 6e 63 74 20 66 72 6f 6d  is distinct from
fed0: 20 74 68 65 20 6f 6c 64 20 76 61 6c 75 65 2e 20   the old value. 
fee0: 54 68 65 20 64 65 66 61 75 6c 74 0a 23 20 63 6f  The default.# co
fef0: 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65  llation sequence
ff00: 20 61 6e 64 20 61 66 66 69 6e 69 74 79 20 61 72   and affinity ar
ff10: 65 20 75 73 65 64 20 74 6f 20 64 65 74 65 72 6d  e used to determ
ff20: 69 6e 65 20 69 66 20 74 68 65 20 6e 65 77 20 76  ine if the new v
ff30: 61 6c 75 65 0a 23 20 69 73 20 27 64 69 73 74 69  alue.# is 'disti
ff40: 6e 63 74 27 20 66 72 6f 6d 20 74 68 65 20 6f 6c  nct' from the ol
ff50: 64 20 6f 72 20 6e 6f 74 2e 0a 23 0a 64 72 6f 70  d or not..#.drop
ff60: 5f 61 6c 6c 5f 74 61 62 6c 65 73 0a 64 6f 5f 74  _all_tables.do_t
ff70: 65 73 74 20 65 5f 66 6b 65 79 2d 32 36 2e 31 20  est e_fkey-26.1 
ff80: 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20  {.  execsql {.  
ff90: 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 7a    CREATE TABLE z
ffa0: 65 75 73 28 61 20 49 4e 54 45 47 45 52 20 43 4f  eus(a INTEGER CO
ffb0: 4c 4c 41 54 45 20 4e 4f 43 41 53 45 2c 20 62 2c  LLATE NOCASE, b,
ffc0: 20 50 52 49 4d 41 52 59 20 4b 45 59 28 61 2c 20   PRIMARY KEY(a, 
ffd0: 62 29 29 3b 0a 20 20 20 20 43 52 45 41 54 45 20  b));.    CREATE 
ffe0: 54 41 42 4c 45 20 61 70 6f 6c 6c 6f 28 63 2c 20  TABLE apollo(c, 
fff0: 64 2c 20 0a 20 20 20 20 20 20 46 4f 52 45 49 47  d, .      FOREIG
10000 4e 20 4b 45 59 28 63 2c 20 64 29 20 52 45 46 45  N KEY(c, d) REFE
10010 52 45 4e 43 45 53 20 7a 65 75 73 20 4f 4e 20 55  RENCES zeus ON U
10020 50 44 41 54 45 20 43 41 53 43 41 44 45 0a 20 20  PDATE CASCADE.  
10030 20 20 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20    );.    INSERT 
10040 49 4e 54 4f 20 7a 65 75 73 20 56 41 4c 55 45 53  INTO zeus VALUES
10050 28 27 61 62 63 27 2c 20 27 78 79 7a 27 29 3b 0a  ('abc', 'xyz');.
10060 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20      INSERT INTO 
10070 61 70 6f 6c 6c 6f 20 56 41 4c 55 45 53 28 27 41  apollo VALUES('A
10080 42 43 27 2c 20 27 78 79 7a 27 29 3b 0a 20 20 7d  BC', 'xyz');.  }
10090 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20  .  execsql {.   
100a0 20 55 50 44 41 54 45 20 7a 65 75 73 20 53 45 54   UPDATE zeus SET
100b0 20 61 20 3d 20 27 61 42 63 27 3b 0a 20 20 20 20   a = 'aBc';.    
100c0 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 61 70  SELECT * FROM ap
100d0 6f 6c 6c 6f 3b 0a 20 20 7d 0a 7d 20 7b 41 42 43  ollo;.  }.} {ABC
100e0 20 78 79 7a 7d 0a 64 6f 5f 74 65 73 74 20 65 5f   xyz}.do_test e_
100f0 66 6b 65 79 2d 32 36 2e 32 20 7b 0a 20 20 65 78  fkey-26.2 {.  ex
10100 65 63 73 71 6c 20 7b 0a 20 20 20 20 55 50 44 41  ecsql {.    UPDA
10110 54 45 20 7a 65 75 73 20 53 45 54 20 61 20 3d 20  TE zeus SET a = 
10120 31 2c 20 62 20 3d 20 31 3b 0a 20 20 20 20 53 45  1, b = 1;.    SE
10130 4c 45 43 54 20 2a 20 46 52 4f 4d 20 61 70 6f 6c  LECT * FROM apol
10140 6c 6f 3b 0a 20 20 7d 0a 7d 20 7b 31 20 31 7d 0a  lo;.  }.} {1 1}.
10150 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 32  do_test e_fkey-2
10160 36 2e 33 20 7b 0a 20 20 65 78 65 63 73 71 6c 20  6.3 {.  execsql 
10170 7b 0a 20 20 20 20 55 50 44 41 54 45 20 7a 65 75  {.    UPDATE zeu
10180 73 20 53 45 54 20 61 20 3d 20 31 2c 20 62 20 3d  s SET a = 1, b =
10190 20 31 3b 0a 20 20 20 20 53 45 4c 45 43 54 20 74   1;.    SELECT t
101a0 79 70 65 6f 66 28 63 29 2c 20 63 2c 20 74 79 70  ypeof(c), c, typ
101b0 65 6f 66 28 64 29 2c 20 64 20 46 52 4f 4d 20 61  eof(d), d FROM a
101c0 70 6f 6c 6c 6f 3b 0a 20 20 7d 0a 7d 20 7b 69 6e  pollo;.  }.} {in
101d0 74 65 67 65 72 20 31 20 69 6e 74 65 67 65 72 20  teger 1 integer 
101e0 31 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65  1}.do_test e_fke
101f0 79 2d 32 36 2e 34 20 7b 0a 20 20 65 78 65 63 73  y-26.4 {.  execs
10200 71 6c 20 7b 0a 20 20 20 20 55 50 44 41 54 45 20  ql {.    UPDATE 
10210 7a 65 75 73 20 53 45 54 20 61 20 3d 20 27 31 27  zeus SET a = '1'
10220 3b 0a 20 20 20 20 53 45 4c 45 43 54 20 74 79 70  ;.    SELECT typ
10230 65 6f 66 28 63 29 2c 20 63 2c 20 74 79 70 65 6f  eof(c), c, typeo
10240 66 28 64 29 2c 20 64 20 46 52 4f 4d 20 61 70 6f  f(d), d FROM apo
10250 6c 6c 6f 3b 0a 20 20 7d 0a 7d 20 7b 69 6e 74 65  llo;.  }.} {inte
10260 67 65 72 20 31 20 69 6e 74 65 67 65 72 20 31 7d  ger 1 integer 1}
10270 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d  .do_test e_fkey-
10280 32 36 2e 35 20 7b 0a 20 20 65 78 65 63 73 71 6c  26.5 {.  execsql
10290 20 7b 0a 20 20 20 20 55 50 44 41 54 45 20 7a 65   {.    UPDATE ze
102a0 75 73 20 53 45 54 20 62 20 3d 20 27 31 27 3b 0a  us SET b = '1';.
102b0 20 20 20 20 53 45 4c 45 43 54 20 74 79 70 65 6f      SELECT typeo
102c0 66 28 63 29 2c 20 63 2c 20 74 79 70 65 6f 66 28  f(c), c, typeof(
102d0 64 29 2c 20 64 20 46 52 4f 4d 20 61 70 6f 6c 6c  d), d FROM apoll
102e0 6f 3b 0a 20 20 7d 0a 7d 20 7b 69 6e 74 65 67 65  o;.  }.} {intege
102f0 72 20 31 20 74 65 78 74 20 31 7d 0a 64 6f 5f 74  r 1 text 1}.do_t
10300 65 73 74 20 65 5f 66 6b 65 79 2d 32 36 2e 36 20  est e_fkey-26.6 
10310 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20  {.  execsql {.  
10320 20 20 55 50 44 41 54 45 20 7a 65 75 73 20 53 45    UPDATE zeus SE
10330 54 20 62 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20  T b = NULL;.    
10340 53 45 4c 45 43 54 20 74 79 70 65 6f 66 28 63 29  SELECT typeof(c)
10350 2c 20 63 2c 20 74 79 70 65 6f 66 28 64 29 2c 20  , c, typeof(d), 
10360 64 20 46 52 4f 4d 20 61 70 6f 6c 6c 6f 3b 0a 20  d FROM apollo;. 
10370 20 7d 0a 7d 20 7b 69 6e 74 65 67 65 72 20 31 20   }.} {integer 1 
10380 6e 75 6c 6c 20 7b 7d 7d 0a 0a 23 2d 2d 2d 2d 2d  null {}}..#-----
10390 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
103a0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
103b0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
103c0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
103d0 2d 2d 2d 2d 0a 23 20 2f 2a 20 45 56 3a 20 52 2d  ----.# /* EV: R-
103e0 35 38 35 38 39 2d 35 30 37 38 31 20 2a 2f 0a 23  58589-50781 */.#
103f0 0a 23 20 54 65 73 74 20 61 6e 20 65 78 61 6d 70  .# Test an examp
10400 6c 65 20 66 72 6f 6d 20 74 68 65 20 22 4f 4e 20  le from the "ON 
10410 44 45 4c 45 54 45 20 61 6e 64 20 4f 4e 20 55 50  DELETE and ON UP
10420 44 41 54 45 20 41 63 74 69 6f 6e 73 22 20 73 65  DATE Actions" se
10430 63 74 69 6f 6e 20 0a 23 20 6f 66 20 66 6f 72 65  ction .# of fore
10440 69 67 6e 6b 65 79 73 2e 68 74 6d 6c 2e 20 54 68  ignkeys.html. Th
10450 69 73 20 65 78 61 6d 70 6c 65 20 64 65 6d 6f 6e  is example demon
10460 73 74 72 61 74 65 73 20 74 68 61 74 20 4f 4e 20  strates that ON 
10470 55 50 44 41 54 45 20 61 63 74 69 6f 6e 73 0a 23  UPDATE actions.#
10480 20 6f 6e 6c 79 20 74 61 6b 65 20 70 6c 61 63 65   only take place
10490 20 69 66 20 61 74 20 6c 65 61 73 74 20 6f 6e 65   if at least one
104a0 20 70 61 72 65 6e 74 20 6b 65 79 20 63 6f 6c 75   parent key colu
104b0 6d 6e 20 69 73 20 73 65 74 20 74 6f 20 61 20 76  mn is set to a v
104c0 61 6c 75 65 20 0a 23 20 74 68 61 74 20 69 73 20  alue .# that is 
104d0 64 69 73 74 69 6e 63 74 20 66 72 6f 6d 20 69 74  distinct from it
104e0 73 20 70 72 65 76 69 6f 75 73 20 76 61 6c 75 65  s previous value
104f0 2e 0a 23 0a 64 72 6f 70 5f 61 6c 6c 5f 74 61 62  ..#.drop_all_tab
10500 6c 65 73 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b  les.do_test e_fk
10510 65 79 2d 31 33 2e 31 20 7b 0a 20 20 65 78 65 63  ey-13.1 {.  exec
10520 73 71 6c 20 7b 0a 20 20 20 20 43 52 45 41 54 45  sql {.    CREATE
10530 20 54 41 42 4c 45 20 70 61 72 65 6e 74 28 78 20   TABLE parent(x 
10540 50 52 49 4d 41 52 59 20 4b 45 59 29 3b 0a 20 20  PRIMARY KEY);.  
10550 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 63    CREATE TABLE c
10560 68 69 6c 64 28 79 20 52 45 46 45 52 45 4e 43 45  hild(y REFERENCE
10570 53 20 70 61 72 65 6e 74 20 4f 4e 20 55 50 44 41  S parent ON UPDA
10580 54 45 20 53 45 54 20 4e 55 4c 4c 29 3b 0a 20 20  TE SET NULL);.  
10590 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 70 61    INSERT INTO pa
105a0 72 65 6e 74 20 56 41 4c 55 45 53 28 27 6b 65 79  rent VALUES('key
105b0 27 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49  ');.    INSERT I
105c0 4e 54 4f 20 63 68 69 6c 64 20 56 41 4c 55 45 53  NTO child VALUES
105d0 28 27 6b 65 79 27 29 3b 0a 20 20 7d 0a 7d 20 7b  ('key');.  }.} {
105e0 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79  }.do_test e_fkey
105f0 2d 31 33 2e 32 20 7b 0a 20 20 65 78 65 63 73 71  -13.2 {.  execsq
10600 6c 20 7b 0a 20 20 20 20 55 50 44 41 54 45 20 70  l {.    UPDATE p
10610 61 72 65 6e 74 20 53 45 54 20 78 20 3d 20 27 6b  arent SET x = 'k
10620 65 79 27 3b 0a 20 20 20 20 53 45 4c 45 43 54 20  ey';.    SELECT 
10630 49 46 4e 55 4c 4c 28 79 2c 20 27 6e 75 6c 6c 27  IFNULL(y, 'null'
10640 29 20 46 52 4f 4d 20 63 68 69 6c 64 3b 0a 20 20  ) FROM child;.  
10650 7d 0a 7d 20 7b 6b 65 79 7d 0a 64 6f 5f 74 65 73  }.} {key}.do_tes
10660 74 20 65 5f 66 6b 65 79 2d 31 33 2e 33 20 7b 0a  t e_fkey-13.3 {.
10670 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20    execsql {.    
10680 55 50 44 41 54 45 20 70 61 72 65 6e 74 20 53 45  UPDATE parent SE
10690 54 20 78 20 3d 20 27 6b 65 79 32 27 3b 0a 20 20  T x = 'key2';.  
106a0 20 20 53 45 4c 45 43 54 20 49 46 4e 55 4c 4c 28    SELECT IFNULL(
106b0 79 2c 20 27 6e 75 6c 6c 27 29 20 46 52 4f 4d 20  y, 'null') FROM 
106c0 63 68 69 6c 64 3b 0a 20 20 7d 0a 7d 20 7b 6e 75  child;.  }.} {nu
106d0 6c 6c 7d 0a 0a 23 23 23 23 23 23 23 23 23 23 23  ll}..###########
106e0 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
106f0 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
10700 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
10710 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
10720 0a 23 23 23 20 53 45 43 54 49 4f 4e 20 35 3a 20  .### SECTION 5: 
10730 43 52 45 41 54 45 2c 20 41 4c 54 45 52 20 61 6e  CREATE, ALTER an
10740 64 20 44 52 4f 50 20 54 41 42 4c 45 20 63 6f 6d  d DROP TABLE com
10750 6d 61 6e 64 73 0a 23 23 23 23 23 23 23 23 23 23  mands.##########
10760 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
10770 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
10780 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
10790 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
107a0 23 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  #..#------------
107b0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
107c0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
107d0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
107e0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20  -------------.# 
107f0 2f 2a 20 45 56 3a 20 52 2d 33 36 30 31 38 2d 32  /* EV: R-36018-2
10800 31 37 35 35 20 2a 2f 0a 23 20 2f 2a 20 45 56 3a  1755 */.# /* EV:
10810 20 52 2d 32 35 33 38 34 2d 33 39 33 33 37 20 2a   R-25384-39337 *
10820 2f 0a 23 20 0a 23 20 54 65 73 74 20 74 68 61 74  /.# .# Test that
10830 20 70 61 72 65 6e 74 20 6b 65 79 73 20 61 72 65   parent keys are
10840 20 6e 6f 74 20 63 68 65 63 6b 65 64 20 77 68 65   not checked whe
10850 6e 20 74 61 62 6c 65 73 20 61 72 65 20 63 72 65  n tables are cre
10860 61 74 65 64 2e 0a 23 0a 23 20 43 68 69 6c 64 20  ated..#.# Child 
10870 6b 65 79 73 20 61 72 65 20 63 68 65 63 6b 65 64  keys are checked
10880 20 74 6f 20 65 6e 73 75 72 65 20 61 6c 6c 20 63   to ensure all c
10890 6f 6d 70 6f 6e 65 6e 74 20 63 6f 6c 75 6d 6e 73  omponent columns
108a0 20 65 78 69 73 74 2e 20 49 66 20 70 61 72 65 6e   exist. If paren
108b0 74 0a 23 20 6b 65 79 20 63 6f 6c 75 6d 6e 73 20  t.# key columns 
108c0 61 72 65 20 65 78 70 6c 69 63 69 74 6c 79 20 73  are explicitly s
108d0 70 65 63 69 66 69 65 64 2c 20 53 51 4c 69 74 65  pecified, SQLite
108e0 20 63 68 65 63 6b 73 20 74 6f 20 6d 61 6b 65 20   checks to make 
108f0 73 75 72 65 20 74 68 65 72 65 0a 23 20 61 72 65  sure there.# are
10900 20 74 68 65 20 73 61 6d 65 20 6e 75 6d 62 65 72   the same number
10910 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74   of columns in t
10920 68 65 20 63 68 69 6c 64 20 61 6e 64 20 70 61 72  he child and par
10930 65 6e 74 20 6b 65 79 73 2e 20 28 54 4f 44 4f 3a  ent keys. (TODO:
10940 20 54 68 69 73 0a 23 20 69 73 20 74 65 73 74 65   This.# is teste
10950 64 20 62 75 74 20 64 6f 65 73 20 6e 6f 74 20 63  d but does not c
10960 6f 72 72 65 73 70 6f 6e 64 20 74 6f 20 61 6e 79  orrespond to any
10970 20 74 65 73 74 61 62 6c 65 20 73 74 61 74 65 6d   testable statem
10980 65 6e 74 2e 29 0a 23 0a 23 20 2f 2a 20 45 56 3a  ent.).#.# /* EV:
10990 20 52 2d 30 38 39 30 38 2d 32 33 34 33 39 20 2a   R-08908-23439 *
109a0 2f 0a 23 0a 23 20 41 6c 73 6f 20 74 65 73 74 20  /.#.# Also test 
109b0 74 68 61 74 20 74 68 65 20 61 62 6f 76 65 20 73  that the above s
109c0 74 61 74 65 6d 65 6e 74 73 20 61 72 65 20 74 72  tatements are tr
109d0 75 65 20 72 65 67 61 72 64 6c 65 73 73 20 6f 66  ue regardless of
109e0 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 0a   whether or not.
109f0 23 20 66 6f 72 65 69 67 6e 20 6b 65 79 73 20 61  # foreign keys a
10a00 72 65 20 65 6e 61 62 6c 65 64 3a 20 20 22 41 20  re enabled:  "A 
10a10 43 52 45 41 54 45 20 54 41 42 4c 45 20 63 6f 6d  CREATE TABLE com
10a20 6d 61 6e 64 20 6f 70 65 72 61 74 65 73 20 74 68  mand operates th
10a30 65 20 73 61 6d 65 20 77 68 65 74 68 65 72 0a 23  e same whether.#
10a40 20 6f 72 20 6e 6f 74 20 66 6f 72 65 69 67 6e 20   or not foreign 
10a50 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 73 20  key constraints 
10a60 61 72 65 20 65 6e 61 62 6c 65 64 2e 22 0a 23 20  are enabled.".# 
10a70 0a 66 6f 72 65 61 63 68 20 7b 74 6e 20 7a 43 72  .foreach {tn zCr
10a80 65 61 74 65 54 62 6c 20 6c 52 65 73 7d 20 7b 0a  eateTbl lRes} {.
10a90 20 20 31 20 22 43 52 45 41 54 45 20 54 41 42 4c    1 "CREATE TABL
10aa0 45 20 74 31 28 61 2c 20 62 20 52 45 46 45 52 45  E t1(a, b REFERE
10ab0 4e 43 45 53 20 74 31 29 22 20 20 20 20 20 20 20  NCES t1)"       
10ac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10ad0 20 20 20 20 20 7b 30 20 7b 7d 7d 0a 20 20 32 20       {0 {}}.  2 
10ae0 22 43 52 45 41 54 45 20 54 41 42 4c 45 20 74 31  "CREATE TABLE t1
10af0 28 61 2c 20 62 20 52 45 46 45 52 45 4e 43 45 53  (a, b REFERENCES
10b00 20 74 32 29 22 20 20 20 20 20 20 20 20 20 20 20   t2)"           
10b10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10b20 20 7b 30 20 7b 7d 7d 0a 20 20 33 20 22 43 52 45   {0 {}}.  3 "CRE
10b30 41 54 45 20 54 41 42 4c 45 20 74 31 28 61 2c 20  ATE TABLE t1(a, 
10b40 62 2c 20 46 4f 52 45 49 47 4e 20 4b 45 59 28 61  b, FOREIGN KEY(a
10b50 2c 62 29 20 52 45 46 45 52 45 4e 43 45 53 20 74  ,b) REFERENCES t
10b60 31 29 22 20 20 20 20 20 20 20 20 20 20 7b 30 20  1)"          {0 
10b70 7b 7d 7d 0a 20 20 34 20 22 43 52 45 41 54 45 20  {}}.  4 "CREATE 
10b80 54 41 42 4c 45 20 74 31 28 61 2c 20 62 2c 20 46  TABLE t1(a, b, F
10b90 4f 52 45 49 47 4e 20 4b 45 59 28 61 2c 62 29 20  OREIGN KEY(a,b) 
10ba0 52 45 46 45 52 45 4e 43 45 53 20 74 32 29 22 20  REFERENCES t2)" 
10bb0 20 20 20 20 20 20 20 20 20 7b 30 20 7b 7d 7d 0a           {0 {}}.
10bc0 20 20 35 20 22 43 52 45 41 54 45 20 54 41 42 4c    5 "CREATE TABL
10bd0 45 20 74 31 28 61 2c 20 62 2c 20 46 4f 52 45 49  E t1(a, b, FOREI
10be0 47 4e 20 4b 45 59 28 61 2c 62 29 20 52 45 46 45  GN KEY(a,b) REFE
10bf0 52 45 4e 43 45 53 20 74 32 29 22 20 20 20 20 20  RENCES t2)"     
10c00 20 20 20 20 20 7b 30 20 7b 7d 7d 0a 20 20 36 20       {0 {}}.  6 
10c10 22 43 52 45 41 54 45 20 54 41 42 4c 45 20 74 31  "CREATE TABLE t1
10c20 28 61 2c 20 62 2c 20 46 4f 52 45 49 47 4e 20 4b  (a, b, FOREIGN K
10c30 45 59 28 61 2c 62 29 20 52 45 46 45 52 45 4e 43  EY(a,b) REFERENC
10c40 45 53 20 74 32 28 6e 2c 64 29 29 22 20 20 20 20  ES t2(n,d))"    
10c50 20 7b 30 20 7b 7d 7d 0a 20 20 37 20 22 43 52 45   {0 {}}.  7 "CRE
10c60 41 54 45 20 54 41 42 4c 45 20 74 31 28 61 2c 20  ATE TABLE t1(a, 
10c70 62 2c 20 46 4f 52 45 49 47 4e 20 4b 45 59 28 61  b, FOREIGN KEY(a
10c80 2c 62 29 20 52 45 46 45 52 45 4e 43 45 53 20 74  ,b) REFERENCES t
10c90 31 28 61 2c 62 29 29 22 20 20 20 20 20 7b 30 20  1(a,b))"     {0 
10ca0 7b 7d 7d 0a 0a 20 20 41 20 22 43 52 45 41 54 45  {}}..  A "CREATE
10cb0 20 54 41 42 4c 45 20 74 31 28 61 2c 20 62 2c 20   TABLE t1(a, b, 
10cc0 46 4f 52 45 49 47 4e 20 4b 45 59 28 63 2c 62 29  FOREIGN KEY(c,b)
10cd0 20 52 45 46 45 52 45 4e 43 45 53 20 74 32 29 22   REFERENCES t2)"
10ce0 20 20 20 20 20 20 20 20 20 20 0a 20 20 20 20 20            .     
10cf0 7b 31 20 7b 75 6e 6b 6e 6f 77 6e 20 63 6f 6c 75  {1 {unknown colu
10d00 6d 6e 20 22 63 22 20 69 6e 20 66 6f 72 65 69 67  mn "c" in foreig
10d10 6e 20 6b 65 79 20 64 65 66 69 6e 69 74 69 6f 6e  n key definition
10d20 7d 7d 0a 20 20 42 20 22 43 52 45 41 54 45 20 54  }}.  B "CREATE T
10d30 41 42 4c 45 20 74 31 28 61 2c 20 62 2c 20 46 4f  ABLE t1(a, b, FO
10d40 52 45 49 47 4e 20 4b 45 59 28 63 2c 62 29 20 52  REIGN KEY(c,b) R
10d50 45 46 45 52 45 4e 43 45 53 20 74 32 28 64 29 29  EFERENCES t2(d))
10d60 22 20 20 20 20 20 20 20 20 20 20 0a 20 20 20 20  "          .    
10d70 20 7b 31 20 7b 6e 75 6d 62 65 72 20 6f 66 20 63   {1 {number of c
10d80 6f 6c 75 6d 6e 73 20 69 6e 20 66 6f 72 65 69 67  olumns in foreig
10d90 6e 20 6b 65 79 20 64 6f 65 73 20 6e 6f 74 20 6d  n key does not m
10da0 61 74 63 68 20 74 68 65 20 6e 75 6d 62 65 72 20  atch the number 
10db0 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68  of columns in th
10dc0 65 20 72 65 66 65 72 65 6e 63 65 64 20 74 61 62  e referenced tab
10dd0 6c 65 7d 7d 0a 7d 20 7b 0a 20 20 64 6f 5f 74 65  le}}.} {.  do_te
10de0 73 74 20 65 5f 66 6b 65 79 2d 35 2e 24 74 6e 2e  st e_fkey-5.$tn.
10df0 6f 66 66 20 7b 0a 20 20 20 20 64 72 6f 70 5f 61  off {.    drop_a
10e00 6c 6c 5f 74 61 62 6c 65 73 0a 20 20 20 20 65 78  ll_tables.    ex
10e10 65 63 73 71 6c 20 7b 50 52 41 47 4d 41 20 66 6f  ecsql {PRAGMA fo
10e20 72 65 69 67 6e 5f 6b 65 79 73 20 3d 20 4f 46 46  reign_keys = OFF
10e30 7d 0a 20 20 20 20 63 61 74 63 68 73 71 6c 20 24  }.    catchsql $
10e40 7a 43 72 65 61 74 65 54 62 6c 0a 20 20 7d 20 24  zCreateTbl.  } $
10e50 6c 52 65 73 0a 20 20 64 6f 5f 74 65 73 74 20 65  lRes.  do_test e
10e60 5f 66 6b 65 79 2d 35 2e 24 74 6e 2e 6f 6e 20 7b  _fkey-5.$tn.on {
10e70 0a 20 20 20 20 64 72 6f 70 5f 61 6c 6c 5f 74 61  .    drop_all_ta
10e80 62 6c 65 73 0a 20 20 20 20 65 78 65 63 73 71 6c  bles.    execsql
10e90 20 7b 50 52 41 47 4d 41 20 66 6f 72 65 69 67 6e   {PRAGMA foreign
10ea0 5f 6b 65 79 73 20 3d 20 4f 4e 7d 0a 20 20 20 20  _keys = ON}.    
10eb0 63 61 74 63 68 73 71 6c 20 24 7a 43 72 65 61 74  catchsql $zCreat
10ec0 65 54 62 6c 0a 20 20 7d 20 24 6c 52 65 73 0a 7d  eTbl.  } $lRes.}
10ed0 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ..#-------------
10ee0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
10ef0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
10f00 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
10f10 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20 2f  ------------.# /
10f20 2a 20 45 56 3a 20 52 2d 34 37 39 35 32 2d 36 32  * EV: R-47952-62
10f30 34 39 38 20 2a 2f 0a 23 0a 70 72 6f 63 20 74 65  498 */.#.proc te
10f40 73 74 5f 65 66 6b 65 79 5f 36 20 7b 74 6e 20 7a  st_efkey_6 {tn z
10f50 41 6c 74 65 72 20 69 73 45 72 72 6f 72 7d 20 7b  Alter isError} {
10f60 0a 20 20 64 72 6f 70 5f 61 6c 6c 5f 74 61 62 6c  .  drop_all_tabl
10f70 65 73 20 0a 0a 20 20 64 6f 5f 74 65 73 74 20 65  es ..  do_test e
10f80 5f 66 6b 65 79 2d 36 2e 24 74 6e 2e 31 20 22 0a  _fkey-6.$tn.1 ".
10f90 20 20 20 20 65 78 65 63 73 71 6c 20 7b 20 43 52      execsql { CR
10fa0 45 41 54 45 20 54 41 42 4c 45 20 74 62 6c 28 61  EATE TABLE tbl(a
10fb0 2c 20 62 29 20 7d 0a 20 20 20 20 5b 6c 69 73 74  , b) }.    [list
10fc0 20 63 61 74 63 68 73 71 6c 20 24 7a 41 6c 74 65   catchsql $zAlte
10fd0 72 5d 0a 20 20 22 20 5b 6c 69 6e 64 65 78 20 7b  r].  " [lindex {
10fe0 7b 30 20 7b 7d 7d 20 7b 31 20 7b 43 61 6e 6e 6f  {0 {}} {1 {Canno
10ff0 74 20 61 64 64 20 61 20 52 45 46 45 52 45 4e 43  t add a REFERENC
11000 45 53 20 63 6f 6c 75 6d 6e 20 77 69 74 68 20 6e  ES column with n
11010 6f 6e 2d 4e 55 4c 4c 20 64 65 66 61 75 6c 74 20  on-NULL default 
11020 76 61 6c 75 65 7d 7d 7d 20 24 69 73 45 72 72 6f  value}}} $isErro
11030 72 5d 0a 0a 7d 0a 0a 74 65 73 74 5f 65 66 6b 65  r]..}..test_efke
11040 79 5f 36 20 31 20 22 41 4c 54 45 52 20 54 41 42  y_6 1 "ALTER TAB
11050 4c 45 20 74 62 6c 20 41 44 44 20 43 4f 4c 55 4d  LE tbl ADD COLUM
11060 4e 20 63 20 52 45 46 45 52 45 4e 43 45 53 20 78  N c REFERENCES x
11070 78 22 20 30 0a 74 65 73 74 5f 65 66 6b 65 79 5f  x" 0.test_efkey_
11080 36 20 32 20 22 41 4c 54 45 52 20 54 41 42 4c 45  6 2 "ALTER TABLE
11090 20 74 62 6c 20 41 44 44 20 43 4f 4c 55 4d 4e 20   tbl ADD COLUMN 
110a0 63 20 44 45 46 41 55 4c 54 20 4e 55 4c 4c 20 52  c DEFAULT NULL R
110b0 45 46 45 52 45 4e 43 45 53 20 78 78 22 20 30 0a  EFERENCES xx" 0.
110c0 74 65 73 74 5f 65 66 6b 65 79 5f 36 20 33 20 22  test_efkey_6 3 "
110d0 41 4c 54 45 52 20 54 41 42 4c 45 20 74 62 6c 20  ALTER TABLE tbl 
110e0 41 44 44 20 43 4f 4c 55 4d 4e 20 63 20 44 45 46  ADD COLUMN c DEF
110f0 41 55 4c 54 20 30 20 52 45 46 45 52 45 4e 43 45  AULT 0 REFERENCE
11100 53 20 78 78 22 20 31 0a 0a 23 2d 2d 2d 2d 2d 2d  S xx" 1..#------
11110 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
11120 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
11130 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
11140 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
11150 2d 2d 2d 0a 23 20 2f 2a 20 45 56 3a 20 52 2d 34  ---.# /* EV: R-4
11160 37 30 38 30 2d 30 32 30 36 39 20 2a 2f 0a 23 0a  7080-02069 */.#.
11170 23 20 54 65 73 74 20 74 68 61 74 20 41 4c 54 45  # Test that ALTE
11180 52 20 54 41 42 4c 45 20 61 64 6a 75 73 74 73 20  R TABLE adjusts 
11190 52 45 46 45 52 45 4e 43 45 53 20 63 6c 61 75 73  REFERENCES claus
111a0 65 73 20 77 68 65 6e 20 74 68 65 20 70 61 72 65  es when the pare
111b0 6e 74 20 74 61 62 6c 65 0a 23 20 69 73 20 52 45  nt table.# is RE
111c0 4e 41 4d 45 44 2e 0a 23 0a 23 20 2f 2a 20 45 56  NAMED..#.# /* EV
111d0 3a 20 52 2d 36 33 38 32 37 2d 35 34 37 37 34 20  : R-63827-54774 
111e0 2a 2f 0a 23 0a 23 20 54 65 73 74 20 74 68 61 74  */.#.# Test that
111f0 20 74 68 65 73 65 20 61 64 6a 75 73 74 6d 65 6e   these adjustmen
11200 74 73 20 61 72 65 20 76 69 73 69 62 6c 65 20 69  ts are visible i
11210 6e 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61 73  n the sqlite_mas
11220 74 65 72 20 74 61 62 6c 65 2e 0a 23 0a 64 6f 5f  ter table..#.do_
11230 74 65 73 74 20 65 5f 66 6b 65 79 2d 37 2e 31 20  test e_fkey-7.1 
11240 7b 0a 20 20 64 72 6f 70 5f 61 6c 6c 5f 74 61 62  {.  drop_all_tab
11250 6c 65 73 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a  les.  execsql {.
11260 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45      CREATE TABLE
11270 20 27 70 20 31 20 22 70 61 72 65 6e 74 20 6f 6e   'p 1 "parent on
11280 65 22 27 28 61 20 52 45 46 45 52 45 4e 43 45 53  e"'(a REFERENCES
11290 20 27 70 20 31 20 22 70 61 72 65 6e 74 20 6f 6e   'p 1 "parent on
112a0 65 22 27 2c 20 62 2c 20 50 52 49 4d 41 52 59 20  e"', b, PRIMARY 
112b0 4b 45 59 28 62 29 29 3b 0a 0a 20 20 20 20 43 52  KEY(b));..    CR
112c0 45 41 54 45 20 54 41 42 4c 45 20 63 31 28 63 2c  EATE TABLE c1(c,
112d0 20 64 20 52 45 46 45 52 45 4e 43 45 53 20 27 70   d REFERENCES 'p
112e0 20 31 20 22 70 61 72 65 6e 74 20 6f 6e 65 22 27   1 "parent one"'
112f0 20 4f 4e 20 55 50 44 41 54 45 20 43 41 53 43 41   ON UPDATE CASCA
11300 44 45 29 3b 0a 20 20 20 20 43 52 45 41 54 45 20  DE);.    CREATE 
11310 54 41 42 4c 45 20 63 32 28 65 2c 20 66 2c 20 46  TABLE c2(e, f, F
11320 4f 52 45 49 47 4e 20 4b 45 59 28 66 29 20 52 45  OREIGN KEY(f) RE
11330 46 45 52 45 4e 43 45 53 20 27 70 20 31 20 22 70  FERENCES 'p 1 "p
11340 61 72 65 6e 74 20 6f 6e 65 22 27 20 4f 4e 20 55  arent one"' ON U
11350 50 44 41 54 45 20 43 41 53 43 41 44 45 29 3b 0a  PDATE CASCADE);.
11360 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45      CREATE TABLE
11370 20 63 33 28 65 2c 20 27 66 20 63 6f 6c 20 32 27   c3(e, 'f col 2'
11380 2c 20 46 4f 52 45 49 47 4e 20 4b 45 59 28 27 66  , FOREIGN KEY('f
11390 20 63 6f 6c 20 32 27 29 20 52 45 46 45 52 45 4e   col 2') REFEREN
113a0 43 45 53 20 27 70 20 31 20 22 70 61 72 65 6e 74  CES 'p 1 "parent
113b0 20 6f 6e 65 22 27 20 4f 4e 20 55 50 44 41 54 45   one"' ON UPDATE
113c0 20 43 41 53 43 41 44 45 29 3b 0a 0a 20 20 20 20   CASCADE);..    
113d0 49 4e 53 45 52 54 20 49 4e 54 4f 20 27 70 20 31  INSERT INTO 'p 1
113e0 20 22 70 61 72 65 6e 74 20 6f 6e 65 22 27 20 56   "parent one"' V
113f0 41 4c 55 45 53 28 31 2c 20 31 29 3b 0a 20 20 20  ALUES(1, 1);.   
11400 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 63 31 20   INSERT INTO c1 
11410 56 41 4c 55 45 53 28 31 2c 20 31 29 3b 0a 20 20  VALUES(1, 1);.  
11420 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 63 32    INSERT INTO c2
11430 20 56 41 4c 55 45 53 28 31 2c 20 31 29 3b 0a 20   VALUES(1, 1);. 
11440 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 63     INSERT INTO c
11450 33 20 56 41 4c 55 45 53 28 31 2c 20 31 29 3b 0a  3 VALUES(1, 1);.
11460 0a 20 20 20 20 2d 2d 20 43 52 45 41 54 45 20 54  .    -- CREATE T
11470 41 42 4c 45 20 71 28 61 2c 20 62 2c 20 50 52 49  ABLE q(a, b, PRI
11480 4d 41 52 59 20 4b 45 59 28 62 29 29 3b 0a 20 20  MARY KEY(b));.  
11490 7d 0a 7d 20 7b 7d 0a 64 6f 5f 74 65 73 74 20 65  }.} {}.do_test e
114a0 5f 66 6b 65 79 2d 37 2e 32 20 7b 0a 20 20 65 78  _fkey-7.2 {.  ex
114b0 65 63 73 71 6c 20 7b 20 41 4c 54 45 52 20 54 41  ecsql { ALTER TA
114c0 42 4c 45 20 27 70 20 31 20 22 70 61 72 65 6e 74  BLE 'p 1 "parent
114d0 20 6f 6e 65 22 27 20 52 45 4e 41 4d 45 20 54 4f   one"' RENAME TO
114e0 20 70 20 7d 0a 7d 20 7b 7d 0a 64 6f 5f 74 65 73   p }.} {}.do_tes
114f0 74 20 65 5f 66 6b 65 79 2d 37 2e 33 20 7b 0a 20  t e_fkey-7.3 {. 
11500 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 55   execsql {.    U
11510 50 44 41 54 45 20 70 20 53 45 54 20 61 20 3d 20  PDATE p SET a = 
11520 27 78 78 78 27 2c 20 62 20 3d 20 27 78 78 78 27  'xxx', b = 'xxx'
11530 3b 0a 20 20 20 20 53 45 4c 45 43 54 20 2a 20 46  ;.    SELECT * F
11540 52 4f 4d 20 70 3b 0a 20 20 20 20 53 45 4c 45 43  ROM p;.    SELEC
11550 54 20 2a 20 46 52 4f 4d 20 63 31 3b 0a 20 20 20  T * FROM c1;.   
11560 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 63   SELECT * FROM c
11570 32 3b 0a 20 20 20 20 53 45 4c 45 43 54 20 2a 20  2;.    SELECT * 
11580 46 52 4f 4d 20 63 33 3b 0a 20 20 7d 0a 7d 20 7b  FROM c3;.  }.} {
11590 78 78 78 20 78 78 78 20 31 20 78 78 78 20 31 20  xxx xxx 1 xxx 1 
115a0 78 78 78 20 31 20 78 78 78 7d 0a 64 6f 5f 74 65  xxx 1 xxx}.do_te
115b0 73 74 20 65 5f 66 6b 65 79 2d 37 2e 34 20 7b 0a  st e_fkey-7.4 {.
115c0 20 20 65 78 65 63 73 71 6c 20 7b 20 53 45 4c 45    execsql { SELE
115d0 43 54 20 73 71 6c 20 46 52 4f 4d 20 73 71 6c 69  CT sql FROM sqli
115e0 74 65 5f 6d 61 73 74 65 72 20 57 48 45 52 45 20  te_master WHERE 
115f0 74 79 70 65 20 3d 20 27 74 61 62 6c 65 27 7d 0a  type = 'table'}.
11600 7d 20 5b 6c 69 73 74 20 20 20 20 20 20 20 20 20  } [list         
11610 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11640 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
11650 7b 43 52 45 41 54 45 20 54 41 42 4c 45 20 22 70  {CREATE TABLE "p
11660 22 28 61 20 52 45 46 45 52 45 4e 43 45 53 20 22  "(a REFERENCES "
11670 70 22 2c 20 62 2c 20 50 52 49 4d 41 52 59 20 4b  p", b, PRIMARY K
11680 45 59 28 62 29 29 7d 20 20 20 20 20 20 20 20 20  EY(b))}         
11690 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 7b 43            \.  {C
116a0 52 45 41 54 45 20 54 41 42 4c 45 20 63 31 28 63  REATE TABLE c1(c
116b0 2c 20 64 20 52 45 46 45 52 45 4e 43 45 53 20 22  , d REFERENCES "
116c0 70 22 20 4f 4e 20 55 50 44 41 54 45 20 43 41 53  p" ON UPDATE CAS
116d0 43 41 44 45 29 7d 20 20 20 20 20 20 20 20 20 20  CADE)}          
116e0 20 20 20 20 20 20 20 20 5c 0a 20 20 7b 43 52 45          \.  {CRE
116f0 41 54 45 20 54 41 42 4c 45 20 63 32 28 65 2c 20  ATE TABLE c2(e, 
11700 66 2c 20 46 4f 52 45 49 47 4e 20 4b 45 59 28 66  f, FOREIGN KEY(f
11710 29 20 52 45 46 45 52 45 4e 43 45 53 20 22 70 22  ) REFERENCES "p"
11720 20 4f 4e 20 55 50 44 41 54 45 20 43 41 53 43 41   ON UPDATE CASCA
11730 44 45 29 7d 20 20 5c 0a 20 20 7b 43 52 45 41 54  DE)}  \.  {CREAT
11740 45 20 54 41 42 4c 45 20 63 33 28 65 2c 20 27 66  E TABLE c3(e, 'f
11750 20 63 6f 6c 20 32 27 2c 20 46 4f 52 45 49 47 4e   col 2', FOREIGN
11760 20 4b 45 59 28 27 66 20 63 6f 6c 20 32 27 29 20   KEY('f col 2') 
11770 52 45 46 45 52 45 4e 43 45 53 20 22 70 22 20 4f  REFERENCES "p" O
11780 4e 20 55 50 44 41 54 45 20 43 41 53 43 41 44 45  N UPDATE CASCADE
11790 29 7d 20 5c 0a 5d 0a 0a 23 2d 2d 2d 2d 2d 2d 2d  )} \.]..#-------
117a0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
117b0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
117c0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
117d0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
117e0 2d 2d 0a 23 20 2f 2a 20 45 56 3a 20 52 2d 31 34  --.# /* EV: R-14
117f0 32 30 38 2d 32 33 39 38 36 20 2a 2f 0a 23 20 2f  208-23986 */.# /
11800 2a 20 45 56 3a 20 52 2d 31 31 30 37 38 2d 30 33  * EV: R-11078-03
11810 39 34 35 20 2a 2f 0a 23 0a 23 20 43 68 65 63 6b  945 */.#.# Check
11820 20 74 68 61 74 20 61 20 44 52 4f 50 20 54 41 42   that a DROP TAB
11830 4c 45 20 64 6f 65 73 20 61 6e 20 69 6d 70 6c 69  LE does an impli
11840 63 69 74 20 44 45 4c 45 54 45 20 46 52 4f 4d 2e  cit DELETE FROM.
11850 20 57 68 69 63 68 20 64 6f 65 73 20 6e 6f 74 0a   Which does not.
11860 23 20 63 61 75 73 65 20 61 6e 79 20 74 72 69 67  # cause any trig
11870 67 65 72 73 20 74 6f 20 66 69 72 65 2c 20 62 75  gers to fire, bu
11880 74 20 64 6f 65 73 20 66 69 72 65 20 66 6f 72 65  t does fire fore
11890 69 67 6e 20 6b 65 79 20 61 63 74 69 6f 6e 73 2e  ign key actions.
118a0 0a 23 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65  .#.do_test e_fke
118b0 79 2d 38 2e 31 20 7b 0a 20 20 64 72 6f 70 5f 61  y-8.1 {.  drop_a
118c0 6c 6c 5f 74 61 62 6c 65 73 0a 20 20 65 78 65 63  ll_tables.  exec
118d0 73 71 6c 20 7b 0a 20 20 20 20 43 52 45 41 54 45  sql {.    CREATE
118e0 20 54 41 42 4c 45 20 70 28 61 2c 20 62 2c 20 50   TABLE p(a, b, P
118f0 52 49 4d 41 52 59 20 4b 45 59 28 61 2c 20 62 29  RIMARY KEY(a, b)
11900 29 3b 0a 0a 20 20 20 20 43 52 45 41 54 45 20 54  );..    CREATE T
11910 41 42 4c 45 20 63 31 28 63 2c 20 64 2c 20 46 4f  ABLE c1(c, d, FO
11920 52 45 49 47 4e 20 4b 45 59 28 63 2c 20 64 29 20  REIGN KEY(c, d) 
11930 52 45 46 45 52 45 4e 43 45 53 20 70 20 4f 4e 20  REFERENCES p ON 
11940 44 45 4c 45 54 45 20 53 45 54 20 4e 55 4c 4c 29  DELETE SET NULL)
11950 3b 0a 20 20 20 20 43 52 45 41 54 45 20 54 41 42  ;.    CREATE TAB
11960 4c 45 20 63 32 28 63 2c 20 64 2c 20 46 4f 52 45  LE c2(c, d, FORE
11970 49 47 4e 20 4b 45 59 28 63 2c 20 64 29 20 52 45  IGN KEY(c, d) RE
11980 46 45 52 45 4e 43 45 53 20 70 20 4f 4e 20 44 45  FERENCES p ON DE
11990 4c 45 54 45 20 53 45 54 20 44 45 46 41 55 4c 54  LETE SET DEFAULT
119a0 29 3b 0a 20 20 20 20 43 52 45 41 54 45 20 54 41  );.    CREATE TA
119b0 42 4c 45 20 63 33 28 63 2c 20 64 2c 20 46 4f 52  BLE c3(c, d, FOR
119c0 45 49 47 4e 20 4b 45 59 28 63 2c 20 64 29 20 52  EIGN KEY(c, d) R
119d0 45 46 45 52 45 4e 43 45 53 20 70 20 4f 4e 20 44  EFERENCES p ON D
119e0 45 4c 45 54 45 20 43 41 53 43 41 44 45 29 3b 0a  ELETE CASCADE);.
119f0 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45      CREATE TABLE
11a00 20 63 34 28 63 2c 20 64 2c 20 46 4f 52 45 49 47   c4(c, d, FOREIG
11a10 4e 20 4b 45 59 28 63 2c 20 64 29 20 52 45 46 45  N KEY(c, d) REFE
11a20 52 45 4e 43 45 53 20 70 20 4f 4e 20 44 45 4c 45  RENCES p ON DELE
11a30 54 45 20 52 45 53 54 52 49 43 54 29 3b 0a 20 20  TE RESTRICT);.  
11a40 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 63    CREATE TABLE c
11a50 35 28 63 2c 20 64 2c 20 46 4f 52 45 49 47 4e 20  5(c, d, FOREIGN 
11a60 4b 45 59 28 63 2c 20 64 29 20 52 45 46 45 52 45  KEY(c, d) REFERE
11a70 4e 43 45 53 20 70 20 4f 4e 20 44 45 4c 45 54 45  NCES p ON DELETE
11a80 20 4e 4f 20 41 43 54 49 4f 4e 29 3b 0a 0a 20 20   NO ACTION);..  
11a90 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 63    CREATE TABLE c
11aa0 36 28 63 2c 20 64 2c 20 0a 20 20 20 20 20 20 46  6(c, d, .      F
11ab0 4f 52 45 49 47 4e 20 4b 45 59 28 63 2c 20 64 29  OREIGN KEY(c, d)
11ac0 20 52 45 46 45 52 45 4e 43 45 53 20 70 20 4f 4e   REFERENCES p ON
11ad0 20 44 45 4c 45 54 45 20 52 45 53 54 52 49 43 54   DELETE RESTRICT
11ae0 20 0a 20 20 20 20 20 20 44 45 46 45 52 52 41 42   .      DEFERRAB
11af0 4c 45 20 49 4e 49 54 49 41 4c 4c 59 20 44 45 46  LE INITIALLY DEF
11b00 45 52 52 45 44 0a 20 20 20 20 29 3b 0a 20 20 20  ERRED.    );.   
11b10 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 63 37   CREATE TABLE c7
11b20 28 63 2c 20 64 2c 20 0a 20 20 20 20 20 20 46 4f  (c, d, .      FO
11b30 52 45 49 47 4e 20 4b 45 59 28 63 2c 20 64 29 20  REIGN KEY(c, d) 
11b40 52 45 46 45 52 45 4e 43 45 53 20 70 20 4f 4e 20  REFERENCES p ON 
11b50 44 45 4c 45 54 45 20 4e 4f 20 41 43 54 49 4f 4e  DELETE NO ACTION
11b60 0a 20 20 20 20 20 20 44 45 46 45 52 52 41 42 4c  .      DEFERRABL
11b70 45 20 49 4e 49 54 49 41 4c 4c 59 20 44 45 46 45  E INITIALLY DEFE
11b80 52 52 45 44 0a 20 20 20 20 29 3b 0a 0a 20 20 20  RRED.    );..   
11b90 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 6c 6f   CREATE TABLE lo
11ba0 67 28 6d 73 67 29 3b 0a 20 20 20 20 43 52 45 41  g(msg);.    CREA
11bb0 54 45 20 54 52 49 47 47 45 52 20 74 74 20 41 46  TE TRIGGER tt AF
11bc0 54 45 52 20 44 45 4c 45 54 45 20 4f 4e 20 70 20  TER DELETE ON p 
11bd0 42 45 47 49 4e 0a 20 20 20 20 20 20 49 4e 53 45  BEGIN.      INSE
11be0 52 54 20 49 4e 54 4f 20 6c 6f 67 20 56 41 4c 55  RT INTO log VALU
11bf0 45 53 28 27 64 65 6c 65 74 65 20 27 20 7c 7c 20  ES('delete ' || 
11c00 6f 6c 64 2e 72 6f 77 69 64 29 3b 0a 20 20 20 20  old.rowid);.    
11c10 45 4e 44 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a 0a 64  END;.  }.} {}..d
11c20 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 38 2e  o_test e_fkey-8.
11c30 32 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a  2 {.  execsql {.
11c40 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20      INSERT INTO 
11c50 70 20 56 41 4c 55 45 53 28 27 61 27 2c 20 27 62  p VALUES('a', 'b
11c60 27 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49  ');.    INSERT I
11c70 4e 54 4f 20 63 31 20 56 41 4c 55 45 53 28 27 61  NTO c1 VALUES('a
11c80 27 2c 20 27 62 27 29 3b 0a 20 20 20 20 49 4e 53  ', 'b');.    INS
11c90 45 52 54 20 49 4e 54 4f 20 63 32 20 56 41 4c 55  ERT INTO c2 VALU
11ca0 45 53 28 27 61 27 2c 20 27 62 27 29 3b 0a 20 20  ES('a', 'b');.  
11cb0 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 63 33    INSERT INTO c3
11cc0 20 56 41 4c 55 45 53 28 27 61 27 2c 20 27 62 27   VALUES('a', 'b'
11cd0 29 3b 0a 20 20 20 20 42 45 47 49 4e 3b 0a 20 20  );.    BEGIN;.  
11ce0 20 20 20 20 44 52 4f 50 20 54 41 42 4c 45 20 70      DROP TABLE p
11cf0 3b 0a 20 20 20 20 20 20 53 45 4c 45 43 54 20 2a  ;.      SELECT *
11d00 20 46 52 4f 4d 20 63 31 3b 0a 20 20 7d 0a 7d 20   FROM c1;.  }.} 
11d10 7b 7b 7d 20 7b 7d 7d 0a 64 6f 5f 74 65 73 74 20  {{} {}}.do_test 
11d20 65 5f 66 6b 65 79 2d 38 2e 33 20 7b 0a 20 20 65  e_fkey-8.3 {.  e
11d30 78 65 63 73 71 6c 20 7b 20 53 45 4c 45 43 54 20  xecsql { SELECT 
11d40 2a 20 46 52 4f 4d 20 63 32 20 7d 0a 7d 20 7b 7b  * FROM c2 }.} {{
11d50 7d 20 7b 7d 7d 0a 64 6f 5f 74 65 73 74 20 65 5f  } {}}.do_test e_
11d60 66 6b 65 79 2d 38 2e 34 20 7b 0a 20 20 65 78 65  fkey-8.4 {.  exe
11d70 63 73 71 6c 20 7b 20 53 45 4c 45 43 54 20 2a 20  csql { SELECT * 
11d80 46 52 4f 4d 20 63 33 20 7d 0a 7d 20 7b 7d 0a 64  FROM c3 }.} {}.d
11d90 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 38 2e  o_test e_fkey-8.
11da0 35 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 20  5 {.  execsql { 
11db0 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 6c 6f  SELECT * FROM lo
11dc0 67 20 7d 0a 7d 20 7b 7d 0a 64 6f 5f 74 65 73 74  g }.} {}.do_test
11dd0 20 65 5f 66 6b 65 79 2d 38 2e 36 20 7b 0a 20 20   e_fkey-8.6 {.  
11de0 65 78 65 63 73 71 6c 20 52 4f 4c 4c 42 41 43 4b  execsql ROLLBACK
11df0 0a 7d 20 7b 7d 0a 64 6f 5f 74 65 73 74 20 65 5f  .} {}.do_test e_
11e00 66 6b 65 79 2d 38 2e 37 20 7b 0a 20 20 65 78 65  fkey-8.7 {.  exe
11e10 63 73 71 6c 20 7b 0a 20 20 20 20 42 45 47 49 4e  csql {.    BEGIN
11e20 3b 0a 20 20 20 20 20 20 44 45 4c 45 54 45 20 46  ;.      DELETE F
11e30 52 4f 4d 20 70 3b 0a 20 20 20 20 20 20 53 45 4c  ROM p;.      SEL
11e40 45 43 54 20 2a 20 46 52 4f 4d 20 6c 6f 67 3b 0a  ECT * FROM log;.
11e50 20 20 20 20 52 4f 4c 4c 42 41 43 4b 3b 0a 20 20      ROLLBACK;.  
11e60 7d 0a 7d 20 7b 7b 64 65 6c 65 74 65 20 31 7d 7d  }.} {{delete 1}}
11e70 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ..#-------------
11e80 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
11e90 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
11ea0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
11eb0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20 2f  ------------.# /
11ec0 2a 20 45 56 3a 20 52 2d 33 32 37 36 38 2d 34 37  * EV: R-32768-47
11ed0 39 32 35 20 2a 2f 0a 23 0a 23 20 49 66 20 61 6e  925 */.#.# If an
11ee0 20 49 4d 4d 45 44 49 41 54 45 20 66 6f 72 65 69   IMMEDIATE forei
11ef0 67 6e 20 6b 65 79 20 66 61 69 6c 73 20 61 73 20  gn key fails as 
11f00 61 20 72 65 73 75 6c 74 20 6f 66 20 61 20 44 52  a result of a DR
11f10 4f 50 20 54 41 42 4c 45 2c 20 74 68 65 0a 23 20  OP TABLE, the.# 
11f20 44 52 4f 50 20 54 41 42 4c 45 20 63 6f 6d 6d 61  DROP TABLE comma
11f30 6e 64 20 66 61 69 6c 73 2e 0a 23 0a 64 6f 5f 74  nd fails..#.do_t
11f40 65 73 74 20 65 5f 66 6b 65 79 2d 39 2e 31 20 7b  est e_fkey-9.1 {
11f50 0a 20 20 65 78 65 63 73 71 6c 20 7b 20 0a 20 20  .  execsql { .  
11f60 20 20 44 45 4c 45 54 45 20 46 52 4f 4d 20 63 31    DELETE FROM c1
11f70 3b 0a 20 20 20 20 44 45 4c 45 54 45 20 46 52 4f  ;.    DELETE FRO
11f80 4d 20 63 32 3b 0a 20 20 20 20 44 45 4c 45 54 45  M c2;.    DELETE
11f90 20 46 52 4f 4d 20 63 33 3b 0a 20 20 7d 0a 20 20   FROM c3;.  }.  
11fa0 65 78 65 63 73 71 6c 20 7b 20 49 4e 53 45 52 54  execsql { INSERT
11fb0 20 49 4e 54 4f 20 63 35 20 56 41 4c 55 45 53 28   INTO c5 VALUES(
11fc0 27 61 27 2c 20 27 62 27 29 20 7d 0a 20 20 63 61  'a', 'b') }.  ca
11fd0 74 63 68 73 71 6c 20 7b 20 44 52 4f 50 20 54 41  tchsql { DROP TA
11fe0 42 4c 45 20 70 20 7d 0a 7d 20 7b 31 20 7b 66 6f  BLE p }.} {1 {fo
11ff0 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72  reign key constr
12000 61 69 6e 74 20 66 61 69 6c 65 64 7d 7d 0a 64 6f  aint failed}}.do
12010 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 39 2e 32  _test e_fkey-9.2
12020 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 20 53   {.  execsql { S
12030 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 70 20 7d  ELECT * FROM p }
12040 0a 7d 20 7b 61 20 62 7d 0a 64 6f 5f 74 65 73 74  .} {a b}.do_test
12050 20 65 5f 66 6b 65 79 2d 39 2e 33 20 7b 0a 20 20   e_fkey-9.3 {.  
12060 63 61 74 63 68 73 71 6c 20 7b 0a 20 20 20 20 42  catchsql {.    B
12070 45 47 49 4e 3b 0a 20 20 20 20 20 20 44 52 4f 50  EGIN;.      DROP
12080 20 54 41 42 4c 45 20 70 3b 0a 20 20 7d 0a 7d 20   TABLE p;.  }.} 
12090 7b 31 20 7b 66 6f 72 65 69 67 6e 20 6b 65 79 20  {1 {foreign key 
120a0 63 6f 6e 73 74 72 61 69 6e 74 20 66 61 69 6c 65  constraint faile
120b0 64 7d 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b  d}}.do_test e_fk
120c0 65 79 2d 39 2e 34 20 7b 0a 20 20 65 78 65 63 73  ey-9.4 {.  execs
120d0 71 6c 20 7b 0a 20 20 20 20 53 45 4c 45 43 54 20  ql {.    SELECT 
120e0 2a 20 46 52 4f 4d 20 70 3b 0a 20 20 20 20 53 45  * FROM p;.    SE
120f0 4c 45 43 54 20 2a 20 46 52 4f 4d 20 63 35 3b 0a  LECT * FROM c5;.
12100 20 20 20 20 52 4f 4c 4c 42 41 43 4b 3b 0a 20 20      ROLLBACK;.  
12110 7d 0a 7d 20 7b 61 20 62 20 61 20 62 7d 0a 0a 23  }.} {a b a b}..#
12120 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
12130 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
12140 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
12150 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
12160 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20 2f 2a 20 45  ---------.# /* E
12170 56 3a 20 52 2d 30 35 39 30 33 2d 30 38 34 36 30  V: R-05903-08460
12180 20 2a 2f 0a 23 0a 23 20 49 66 20 61 20 44 45 46   */.#.# If a DEF
12190 45 52 52 45 44 20 66 6f 72 65 69 67 6e 20 6b 65  ERRED foreign ke
121a0 79 20 66 61 69 6c 73 20 61 73 20 61 20 72 65 73  y fails as a res
121b0 75 6c 74 20 6f 66 20 61 20 44 52 4f 50 20 54 41  ult of a DROP TA
121c0 42 4c 45 2c 20 61 74 74 65 6d 70 74 69 6e 67 0a  BLE, attempting.
121d0 23 20 74 6f 20 63 6f 6d 6d 69 74 20 74 68 65 20  # to commit the 
121e0 74 72 61 6e 73 61 63 74 69 6f 6e 20 66 61 69 6c  transaction fail
121f0 73 20 75 6e 6c 65 73 73 20 74 68 65 20 76 69 6f  s unless the vio
12200 6c 61 74 69 6f 6e 20 69 73 20 66 69 78 65 64 2e  lation is fixed.
12210 0a 23 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65  .#.do_test e_fke
12220 79 2d 31 30 2e 31 20 7b 0a 20 20 65 78 65 63 73  y-10.1 {.  execs
12230 71 6c 20 7b 20 0a 20 20 20 20 44 45 4c 45 54 45  ql { .    DELETE
12240 20 46 52 4f 4d 20 63 31 20 3b 20 44 45 4c 45 54   FROM c1 ; DELET
12250 45 20 46 52 4f 4d 20 63 32 20 3b 20 44 45 4c 45  E FROM c2 ; DELE
12260 54 45 20 46 52 4f 4d 20 63 33 20 3b 0a 20 20 20  TE FROM c3 ;.   
12270 20 44 45 4c 45 54 45 20 46 52 4f 4d 20 63 34 20   DELETE FROM c4 
12280 3b 20 44 45 4c 45 54 45 20 46 52 4f 4d 20 63 35  ; DELETE FROM c5
12290 20 3b 20 44 45 4c 45 54 45 20 46 52 4f 4d 20 63   ; DELETE FROM c
122a0 36 20 3b 0a 20 20 20 20 44 45 4c 45 54 45 20 46  6 ;.    DELETE F
122b0 52 4f 4d 20 63 37 20 0a 20 20 7d 0a 7d 20 7b 7d  ROM c7 .  }.} {}
122c0 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d  .do_test e_fkey-
122d0 31 30 2e 32 20 7b 0a 20 20 65 78 65 63 73 71 6c  10.2 {.  execsql
122e0 20 7b 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 63   { INSERT INTO c
122f0 37 20 56 41 4c 55 45 53 28 27 61 27 2c 20 27 62  7 VALUES('a', 'b
12300 27 29 20 7d 0a 20 20 65 78 65 63 73 71 6c 20 7b  ') }.  execsql {
12310 0a 20 20 20 20 42 45 47 49 4e 3b 0a 20 20 20 20  .    BEGIN;.    
12320 20 20 44 52 4f 50 20 54 41 42 4c 45 20 70 3b 0a    DROP TABLE p;.
12330 20 20 7d 0a 7d 20 7b 7d 0a 64 6f 5f 74 65 73 74    }.} {}.do_test
12340 20 65 5f 66 6b 65 79 2d 31 30 2e 33 20 7b 0a 20   e_fkey-10.3 {. 
12350 20 63 61 74 63 68 73 71 6c 20 43 4f 4d 4d 49 54   catchsql COMMIT
12360 0a 7d 20 7b 31 20 7b 66 6f 72 65 69 67 6e 20 6b  .} {1 {foreign k
12370 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 20 66 61  ey constraint fa
12380 69 6c 65 64 7d 7d 0a 64 6f 5f 74 65 73 74 20 65  iled}}.do_test e
12390 5f 66 6b 65 79 2d 31 30 2e 34 20 7b 0a 20 20 65  _fkey-10.4 {.  e
123a0 78 65 63 73 71 6c 20 7b 20 43 52 45 41 54 45 20  xecsql { CREATE 
123b0 54 41 42 4c 45 20 70 28 61 2c 20 62 2c 20 50 52  TABLE p(a, b, PR
123c0 49 4d 41 52 59 20 4b 45 59 28 61 2c 20 62 29 29  IMARY KEY(a, b))
123d0 20 7d 0a 20 20 63 61 74 63 68 73 71 6c 20 43 4f   }.  catchsql CO
123e0 4d 4d 49 54 0a 7d 20 7b 31 20 7b 66 6f 72 65 69  MMIT.} {1 {forei
123f0 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e  gn key constrain
12400 74 20 66 61 69 6c 65 64 7d 7d 0a 64 6f 5f 74 65  t failed}}.do_te
12410 73 74 20 65 5f 66 6b 65 79 2d 31 30 2e 35 20 7b  st e_fkey-10.5 {
12420 0a 20 20 65 78 65 63 73 71 6c 20 7b 20 49 4e 53  .  execsql { INS
12430 45 52 54 20 49 4e 54 4f 20 70 20 56 41 4c 55 45  ERT INTO p VALUE
12440 53 28 27 61 27 2c 20 27 62 27 29 20 7d 0a 20 20  S('a', 'b') }.  
12450 65 78 65 63 73 71 6c 20 43 4f 4d 4d 49 54 0a 7d  execsql COMMIT.}
12460 20 7b 7d 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d   {}..#----------
12470 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
12480 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
12490 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
124a0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a  ---------------.
124b0 23 20 2f 2a 20 45 56 3a 20 52 2d 35 37 32 34 32  # /* EV: R-57242
124c0 2d 33 37 30 30 35 20 2a 2f 0a 23 0a 23 20 41 6e  -37005 */.#.# An
124d0 79 20 22 66 6f 72 65 69 67 6e 20 6b 65 79 20 6d  y "foreign key m
124e0 69 73 6d 61 74 63 68 22 20 65 72 72 6f 72 73 20  ismatch" errors 
124f0 65 6e 63 6f 75 6e 74 65 72 65 64 20 77 68 69 6c  encountered whil
12500 65 20 72 75 6e 6e 69 6e 67 20 61 6e 20 69 6d 70  e running an imp
12510 6c 69 63 69 74 0a 23 20 22 44 45 4c 45 54 45 20  licit.# "DELETE 
12520 46 52 4f 4d 20 74 62 6c 22 20 61 72 65 20 69 67  FROM tbl" are ig
12530 6e 6f 72 65 64 2e 0a 23 0a 64 72 6f 70 5f 61 6c  nored..#.drop_al
12540 6c 5f 74 61 62 6c 65 73 0a 64 6f 5f 74 65 73 74  l_tables.do_test
12550 20 65 5f 66 6b 65 79 2d 31 31 2e 31 20 7b 0a 20   e_fkey-11.1 {. 
12560 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 50   execsql {.    P
12570 52 41 47 4d 41 20 66 6f 72 65 69 67 6e 5f 6b 65  RAGMA foreign_ke
12580 79 73 20 3d 20 4f 46 46 3b 0a 0a 20 20 20 20 43  ys = OFF;..    C
12590 52 45 41 54 45 20 54 41 42 4c 45 20 70 28 61 20  REATE TABLE p(a 
125a0 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 62 20 52  PRIMARY KEY, b R
125b0 45 46 45 52 45 4e 43 45 53 20 6e 6f 73 75 63 68  EFERENCES nosuch
125c0 74 61 62 6c 65 29 3b 0a 20 20 20 20 43 52 45 41  table);.    CREA
125d0 54 45 20 54 41 42 4c 45 20 63 31 28 63 2c 20 64  TE TABLE c1(c, d
125e0 2c 20 46 4f 52 45 49 47 4e 20 4b 45 59 28 63 2c  , FOREIGN KEY(c,
125f0 20 64 29 20 52 45 46 45 52 45 4e 43 45 53 20 61   d) REFERENCES a
12600 29 3b 0a 20 20 20 20 43 52 45 41 54 45 20 54 41  );.    CREATE TA
12610 42 4c 45 20 63 32 28 63 20 52 45 46 45 52 45 4e  BLE c2(c REFEREN
12620 43 45 53 20 70 28 62 29 2c 20 64 29 3b 0a 20 20  CES p(b), d);.  
12630 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 63    CREATE TABLE c
12640 33 28 63 20 52 45 46 45 52 45 4e 43 45 53 20 70  3(c REFERENCES p
12650 20 4f 4e 20 44 45 4c 45 54 45 20 53 45 54 20 4e   ON DELETE SET N
12660 55 4c 4c 2c 20 64 29 3b 0a 0a 20 20 20 20 49 4e  ULL, d);..    IN
12670 53 45 52 54 20 49 4e 54 4f 20 70 20 56 41 4c 55  SERT INTO p VALU
12680 45 53 28 31 2c 20 32 29 3b 0a 20 20 20 20 49 4e  ES(1, 2);.    IN
12690 53 45 52 54 20 49 4e 54 4f 20 63 31 20 56 41 4c  SERT INTO c1 VAL
126a0 55 45 53 28 31 2c 20 32 29 3b 0a 20 20 20 20 49  UES(1, 2);.    I
126b0 4e 53 45 52 54 20 49 4e 54 4f 20 63 32 20 56 41  NSERT INTO c2 VA
126c0 4c 55 45 53 28 31 2c 20 32 29 3b 0a 20 20 20 20  LUES(1, 2);.    
126d0 49 4e 53 45 52 54 20 49 4e 54 4f 20 63 33 20 56  INSERT INTO c3 V
126e0 41 4c 55 45 53 28 31 2c 20 32 29 3b 0a 20 20 7d  ALUES(1, 2);.  }
126f0 0a 7d 20 7b 7d 0a 64 6f 5f 74 65 73 74 20 65 5f  .} {}.do_test e_
12700 66 6b 65 79 2d 31 31 2e 32 20 7b 0a 20 20 65 78  fkey-11.2 {.  ex
12710 65 63 73 71 6c 20 7b 20 50 52 41 47 4d 41 20 66  ecsql { PRAGMA f
12720 6f 72 65 69 67 6e 5f 6b 65 79 73 20 3d 20 4f 4e  oreign_keys = ON
12730 20 7d 0a 20 20 63 61 74 63 68 73 71 6c 20 7b 20   }.  catchsql { 
12740 44 45 4c 45 54 45 20 46 52 4f 4d 20 70 20 7d 0a  DELETE FROM p }.
12750 7d 20 7b 31 20 7b 6e 6f 20 73 75 63 68 20 74 61  } {1 {no such ta
12760 62 6c 65 3a 20 6d 61 69 6e 2e 6e 6f 73 75 63 68  ble: main.nosuch
12770 74 61 62 6c 65 7d 7d 0a 64 6f 5f 74 65 73 74 20  table}}.do_test 
12780 65 5f 66 6b 65 79 2d 31 31 2e 33 20 7b 0a 20 20  e_fkey-11.3 {.  
12790 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 42 45  execsql {.    BE
127a0 47 49 4e 3b 0a 20 20 20 20 20 20 44 52 4f 50 20  GIN;.      DROP 
127b0 54 41 42 4c 45 20 70 3b 0a 20 20 20 20 20 20 53  TABLE p;.      S
127c0 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 63 33 3b  ELECT * FROM c3;
127d0 0a 20 20 20 20 52 4f 4c 4c 42 41 43 4b 3b 0a 20  .    ROLLBACK;. 
127e0 20 7d 0a 7d 20 7b 7b 7d 20 32 7d 0a 64 6f 5f 74   }.} {{} 2}.do_t
127f0 65 73 74 20 65 5f 66 6b 65 79 2d 31 31 2e 34 20  est e_fkey-11.4 
12800 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 20 43 52  {.  execsql { CR
12810 45 41 54 45 20 54 41 42 4c 45 20 6e 6f 73 75 63  EATE TABLE nosuc
12820 68 74 61 62 6c 65 28 78 20 50 52 49 4d 41 52 59  htable(x PRIMARY
12830 20 4b 45 59 29 20 7d 0a 20 20 63 61 74 63 68 73   KEY) }.  catchs
12840 71 6c 20 7b 20 44 45 4c 45 54 45 20 46 52 4f 4d  ql { DELETE FROM
12850 20 70 20 7d 0a 7d 20 7b 31 20 7b 66 6f 72 65 69   p }.} {1 {forei
12860 67 6e 20 6b 65 79 20 6d 69 73 6d 61 74 63 68 7d  gn key mismatch}
12870 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79  }.do_test e_fkey
12880 2d 31 31 2e 35 20 7b 0a 20 20 65 78 65 63 73 71  -11.5 {.  execsq
12890 6c 20 7b 20 44 52 4f 50 20 54 41 42 4c 45 20 63  l { DROP TABLE c
128a0 31 20 7d 0a 20 20 63 61 74 63 68 73 71 6c 20 7b  1 }.  catchsql {
128b0 20 44 45 4c 45 54 45 20 46 52 4f 4d 20 70 20 7d   DELETE FROM p }
128c0 0a 7d 20 7b 31 20 7b 66 6f 72 65 69 67 6e 20 6b  .} {1 {foreign k
128d0 65 79 20 6d 69 73 6d 61 74 63 68 7d 7d 0a 64 6f  ey mismatch}}.do
128e0 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 31 31 2e  _test e_fkey-11.
128f0 36 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 20  6 {.  execsql { 
12900 44 52 4f 50 20 54 41 42 4c 45 20 63 32 20 7d 0a  DROP TABLE c2 }.
12910 20 20 65 78 65 63 73 71 6c 20 7b 20 44 45 4c 45    execsql { DELE
12920 54 45 20 46 52 4f 4d 20 70 20 7d 0a 7d 20 7b 7d  TE FROM p }.} {}
12930 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ..#-------------
12940 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
12950 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
12960 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
12970 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20 2f  ------------.# /
12980 2a 20 45 56 3a 20 52 2d 35 34 31 34 32 2d 34 31  * EV: R-54142-41
12990 33 34 36 20 2a 2f 0a 23 0a 23 20 54 65 73 74 20  346 */.#.# Test 
129a0 74 68 61 74 20 74 68 65 20 73 70 65 63 69 61 6c  that the special
129b0 20 62 65 68 61 76 69 6f 75 72 73 20 6f 66 20 41   behaviours of A
129c0 4c 54 45 52 20 61 6e 64 20 44 52 4f 50 20 54 41  LTER and DROP TA
129d0 42 4c 45 20 61 72 65 20 6f 6e 6c 79 0a 23 20 61  BLE are only.# a
129e0 63 74 69 76 61 74 65 64 20 77 68 65 6e 20 66 6f  ctivated when fo
129f0 72 65 69 67 6e 20 6b 65 79 73 20 61 72 65 20 65  reign keys are e
12a00 6e 61 62 6c 65 64 2e 20 53 70 65 63 69 61 6c 20  nabled. Special 
12a10 62 65 68 61 76 69 6f 75 72 73 20 61 72 65 3a 0a  behaviours are:.
12a20 23 0a 23 20 20 20 31 2e 20 41 44 44 20 43 4f 4c  #.#   1. ADD COL
12a30 55 4d 4e 20 6e 6f 74 20 61 6c 6c 6f 77 69 6e 67  UMN not allowing
12a40 20 61 20 52 45 46 45 52 45 4e 43 45 53 20 63 6c   a REFERENCES cl
12a50 61 75 73 65 20 77 69 74 68 20 61 20 6e 6f 6e 2d  ause with a non-
12a60 4e 55 4c 4c 20 0a 23 20 20 20 20 20 20 64 65 66  NULL .#      def
12a70 61 75 6c 74 20 76 61 6c 75 65 2e 0a 23 20 20 20  ault value..#   
12a80 32 2e 20 4d 6f 64 69 66 79 69 6e 67 20 66 6f 72  2. Modifying for
12a90 65 69 67 6e 20 6b 65 79 20 64 65 66 69 6e 69 74  eign key definit
12aa0 69 6f 6e 73 20 77 68 65 6e 20 61 20 70 61 72 65  ions when a pare
12ab0 6e 74 20 74 61 62 6c 65 20 69 73 20 52 45 4e 41  nt table is RENA
12ac0 4d 45 64 2e 0a 23 20 20 20 33 2e 20 52 75 6e 6e  MEd..#   3. Runn
12ad0 69 6e 67 20 61 6e 20 69 6d 70 6c 69 63 69 74 20  ing an implicit 
12ae0 44 45 4c 45 54 45 20 46 52 4f 4d 20 63 6f 6d 6d  DELETE FROM comm
12af0 61 6e 64 20 61 73 20 70 61 72 74 20 6f 66 20 44  and as part of D
12b00 52 4f 50 20 54 41 42 4c 45 2e 0a 23 0a 64 6f 5f  ROP TABLE..#.do_
12b10 74 65 73 74 20 65 5f 66 6b 65 79 2d 31 32 2e 31  test e_fkey-12.1
12b20 2e 31 20 7b 0a 20 20 64 72 6f 70 5f 61 6c 6c 5f  .1 {.  drop_all_
12b30 74 61 62 6c 65 73 0a 20 20 65 78 65 63 73 71 6c  tables.  execsql
12b40 20 7b 20 43 52 45 41 54 45 20 54 41 42 4c 45 20   { CREATE TABLE 
12b50 74 31 28 61 2c 20 62 29 20 7d 0a 20 20 63 61 74  t1(a, b) }.  cat
12b60 63 68 73 71 6c 20 7b 20 41 4c 54 45 52 20 54 41  chsql { ALTER TA
12b70 42 4c 45 20 74 31 20 41 44 44 20 43 4f 4c 55 4d  BLE t1 ADD COLUM
12b80 4e 20 63 20 44 45 46 41 55 4c 54 20 27 78 78 78  N c DEFAULT 'xxx
12b90 27 20 52 45 46 45 52 45 4e 43 45 53 20 74 32 20  ' REFERENCES t2 
12ba0 7d 0a 7d 20 7b 31 20 7b 43 61 6e 6e 6f 74 20 61  }.} {1 {Cannot a
12bb0 64 64 20 61 20 52 45 46 45 52 45 4e 43 45 53 20  dd a REFERENCES 
12bc0 63 6f 6c 75 6d 6e 20 77 69 74 68 20 6e 6f 6e 2d  column with non-
12bd0 4e 55 4c 4c 20 64 65 66 61 75 6c 74 20 76 61 6c  NULL default val
12be0 75 65 7d 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66  ue}}.do_test e_f
12bf0 6b 65 79 2d 31 32 2e 31 2e 32 20 7b 0a 20 20 65  key-12.1.2 {.  e
12c00 78 65 63 73 71 6c 20 7b 20 50 52 41 47 4d 41 20  xecsql { PRAGMA 
12c10 66 6f 72 65 69 67 6e 5f 6b 65 79 73 20 3d 20 4f  foreign_keys = O
12c20 46 46 20 7d 0a 20 20 65 78 65 63 73 71 6c 20 7b  FF }.  execsql {
12c30 20 41 4c 54 45 52 20 54 41 42 4c 45 20 74 31 20   ALTER TABLE t1 
12c40 41 44 44 20 43 4f 4c 55 4d 4e 20 63 20 44 45 46  ADD COLUMN c DEF
12c50 41 55 4c 54 20 27 78 78 78 27 20 52 45 46 45 52  AULT 'xxx' REFER
12c60 45 4e 43 45 53 20 74 32 20 7d 0a 20 20 65 78 65  ENCES t2 }.  exe
12c70 63 73 71 6c 20 7b 20 53 45 4c 45 43 54 20 73 71  csql { SELECT sq
12c80 6c 20 46 52 4f 4d 20 73 71 6c 69 74 65 5f 6d 61  l FROM sqlite_ma
12c90 73 74 65 72 20 57 48 45 52 45 20 6e 61 6d 65 20  ster WHERE name 
12ca0 3d 20 27 74 31 27 20 7d 0a 7d 20 7b 7b 43 52 45  = 't1' }.} {{CRE
12cb0 41 54 45 20 54 41 42 4c 45 20 74 31 28 61 2c 20  ATE TABLE t1(a, 
12cc0 62 2c 20 63 20 44 45 46 41 55 4c 54 20 27 78 78  b, c DEFAULT 'xx
12cd0 78 27 20 52 45 46 45 52 45 4e 43 45 53 20 74 32  x' REFERENCES t2
12ce0 29 7d 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b  )}}.do_test e_fk
12cf0 65 79 2d 31 32 2e 31 2e 33 20 7b 0a 20 20 65 78  ey-12.1.3 {.  ex
12d00 65 63 73 71 6c 20 7b 20 50 52 41 47 4d 41 20 66  ecsql { PRAGMA f
12d10 6f 72 65 69 67 6e 5f 6b 65 79 73 20 3d 20 4f 4e  oreign_keys = ON
12d20 20 7d 0a 7d 20 7b 7d 0a 0a 64 6f 5f 74 65 73 74   }.} {}..do_test
12d30 20 65 5f 66 6b 65 79 2d 31 32 2e 32 2e 31 20 7b   e_fkey-12.2.1 {
12d40 0a 20 20 64 72 6f 70 5f 61 6c 6c 5f 74 61 62 6c  .  drop_all_tabl
12d50 65 73 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20  es.  execsql {. 
12d60 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20     CREATE TABLE 
12d70 70 28 61 20 55 4e 49 51 55 45 29 3b 0a 20 20 20  p(a UNIQUE);.   
12d80 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 63 28   CREATE TABLE c(
12d90 62 20 52 45 46 45 52 45 4e 43 45 53 20 70 28 61  b REFERENCES p(a
12da0 29 29 3b 0a 20 20 20 20 42 45 47 49 4e 3b 0a 20  ));.    BEGIN;. 
12db0 20 20 20 20 20 41 4c 54 45 52 20 54 41 42 4c 45       ALTER TABLE
12dc0 20 70 20 52 45 4e 41 4d 45 20 54 4f 20 70 61 72   p RENAME TO par
12dd0 65 6e 74 3b 0a 20 20 20 20 20 20 53 45 4c 45 43  ent;.      SELEC
12de0 54 20 73 71 6c 20 46 52 4f 4d 20 73 71 6c 69 74  T sql FROM sqlit
12df0 65 5f 6d 61 73 74 65 72 20 57 48 45 52 45 20 6e  e_master WHERE n
12e00 61 6d 65 20 3d 20 27 63 27 3b 0a 20 20 20 20 52  ame = 'c';.    R
12e10 4f 4c 4c 42 41 43 4b 3b 0a 20 20 7d 0a 7d 20 7b  OLLBACK;.  }.} {
12e20 7b 43 52 45 41 54 45 20 54 41 42 4c 45 20 63 28  {CREATE TABLE c(
12e30 62 20 52 45 46 45 52 45 4e 43 45 53 20 22 70 61  b REFERENCES "pa
12e40 72 65 6e 74 22 28 61 29 29 7d 7d 0a 64 6f 5f 74  rent"(a))}}.do_t
12e50 65 73 74 20 65 5f 66 6b 65 79 2d 31 32 2e 32 2e  est e_fkey-12.2.
12e60 32 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a  2 {.  execsql {.
12e70 20 20 20 20 50 52 41 47 4d 41 20 66 6f 72 65 69      PRAGMA forei
12e80 67 6e 5f 6b 65 79 73 20 3d 20 4f 46 46 3b 0a 20  gn_keys = OFF;. 
12e90 20 20 20 41 4c 54 45 52 20 54 41 42 4c 45 20 70     ALTER TABLE p
12ea0 20 52 45 4e 41 4d 45 20 54 4f 20 70 61 72 65 6e   RENAME TO paren
12eb0 74 3b 0a 20 20 20 20 53 45 4c 45 43 54 20 73 71  t;.    SELECT sq
12ec0 6c 20 46 52 4f 4d 20 73 71 6c 69 74 65 5f 6d 61  l FROM sqlite_ma
12ed0 73 74 65 72 20 57 48 45 52 45 20 6e 61 6d 65 20  ster WHERE name 
12ee0 3d 20 27 63 27 3b 0a 20 20 7d 0a 7d 20 7b 7b 43  = 'c';.  }.} {{C
12ef0 52 45 41 54 45 20 54 41 42 4c 45 20 63 28 62 20  REATE TABLE c(b 
12f00 52 45 46 45 52 45 4e 43 45 53 20 70 28 61 29 29  REFERENCES p(a))
12f10 7d 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65  }}.do_test e_fke
12f20 79 2d 31 32 2e 32 2e 33 20 7b 0a 20 20 65 78 65  y-12.2.3 {.  exe
12f30 63 73 71 6c 20 7b 20 50 52 41 47 4d 41 20 66 6f  csql { PRAGMA fo
12f40 72 65 69 67 6e 5f 6b 65 79 73 20 3d 20 4f 4e 20  reign_keys = ON 
12f50 7d 0a 7d 20 7b 7d 0a 0a 64 6f 5f 74 65 73 74 20  }.} {}..do_test 
12f60 65 5f 66 6b 65 79 2d 31 32 2e 33 2e 31 20 7b 0a  e_fkey-12.3.1 {.
12f70 20 20 64 72 6f 70 5f 61 6c 6c 5f 74 61 62 6c 65    drop_all_table
12f80 73 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20  s.  execsql {.  
12f90 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 70    CREATE TABLE p
12fa0 28 61 20 55 4e 49 51 55 45 29 3b 0a 20 20 20 20  (a UNIQUE);.    
12fb0 43 52 45 41 54 45 20 54 41 42 4c 45 20 63 28 62  CREATE TABLE c(b
12fc0 20 52 45 46 45 52 45 4e 43 45 53 20 70 28 61 29   REFERENCES p(a)
12fd0 20 4f 4e 20 44 45 4c 45 54 45 20 53 45 54 20 4e   ON DELETE SET N
12fe0 55 4c 4c 29 3b 0a 20 20 20 20 49 4e 53 45 52 54  ULL);.    INSERT
12ff0 20 49 4e 54 4f 20 70 20 56 41 4c 55 45 53 28 27   INTO p VALUES('
13000 78 27 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20  x');.    INSERT 
13010 49 4e 54 4f 20 63 20 56 41 4c 55 45 53 28 27 78  INTO c VALUES('x
13020 27 29 3b 0a 20 20 20 20 42 45 47 49 4e 3b 0a 20  ');.    BEGIN;. 
13030 20 20 20 20 20 44 52 4f 50 20 54 41 42 4c 45 20       DROP TABLE 
13040 70 3b 0a 20 20 20 20 20 20 53 45 4c 45 43 54 20  p;.      SELECT 
13050 2a 20 46 52 4f 4d 20 63 3b 0a 20 20 20 20 52 4f  * FROM c;.    RO
13060 4c 4c 42 41 43 4b 3b 0a 20 20 7d 0a 7d 20 7b 7b  LLBACK;.  }.} {{
13070 7d 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65  }}.do_test e_fke
13080 79 2d 31 32 2e 33 2e 32 20 7b 0a 20 20 65 78 65  y-12.3.2 {.  exe
13090 63 73 71 6c 20 7b 0a 20 20 20 20 50 52 41 47 4d  csql {.    PRAGM
130a0 41 20 66 6f 72 65 69 67 6e 5f 6b 65 79 73 20 3d  A foreign_keys =
130b0 20 4f 46 46 3b 0a 20 20 20 20 44 52 4f 50 20 54   OFF;.    DROP T
130c0 41 42 4c 45 20 70 3b 0a 20 20 20 20 53 45 4c 45  ABLE p;.    SELE
130d0 43 54 20 2a 20 46 52 4f 4d 20 63 3b 0a 20 20 7d  CT * FROM c;.  }
130e0 0a 7d 20 7b 78 7d 0a 64 6f 5f 74 65 73 74 20 65  .} {x}.do_test e
130f0 5f 66 6b 65 79 2d 31 32 2e 33 2e 33 20 7b 0a 20  _fkey-12.3.3 {. 
13100 20 65 78 65 63 73 71 6c 20 7b 20 50 52 41 47 4d   execsql { PRAGM
13110 41 20 66 6f 72 65 69 67 6e 5f 6b 65 79 73 20 3d  A foreign_keys =
13120 20 4f 4e 20 7d 0a 7d 20 7b 7d 0a 0a 23 23 23 23   ON }.} {}..####
13130 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
13140 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
13150 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
13160 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
13170 23 23 23 23 23 23 23 0a 23 23 23 20 53 45 43 54  #######.### SECT
13180 49 4f 4e 20 36 3a 20 4c 69 6d 69 74 73 20 61 6e  ION 6: Limits an
13190 64 20 55 6e 73 75 70 70 6f 72 74 65 64 20 46 65  d Unsupported Fe
131a0 61 74 75 72 65 73 0a 23 23 23 23 23 23 23 23 23  atures.#########
131b0 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
131c0 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
131d0 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
131e0 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23  ################
131f0 23 23 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ##..#-----------
13200 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
13210 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
13220 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
13230 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23  --------------.#
13240 20 2f 2a 20 45 56 3a 20 52 2d 32 34 37 32 38 2d   /* EV: R-24728-
13250 31 33 32 33 30 20 2a 2f 0a 23 20 2f 2a 20 45 56  13230 */.# /* EV
13260 3a 20 52 2d 32 34 34 35 30 2d 34 36 31 37 34 20  : R-24450-46174 
13270 2a 2f 0a 23 0a 23 20 54 65 73 74 20 74 68 61 74  */.#.# Test that
13280 20 4d 41 54 43 48 20 63 6c 61 75 73 65 73 20 61   MATCH clauses a
13290 72 65 20 70 61 72 73 65 64 2c 20 62 75 74 20 53  re parsed, but S
132a0 51 4c 69 74 65 20 74 72 65 61 74 73 20 65 76 65  QLite treats eve
132b0 72 79 20 66 6f 72 65 69 67 6e 20 6b 65 79 0a 23  ry foreign key.#
132c0 20 63 6f 6e 73 74 72 61 69 6e 74 20 61 73 20 69   constraint as i
132d0 66 20 69 74 20 77 65 72 65 20 22 4d 41 54 43 48  f it were "MATCH
132e0 20 53 49 4d 50 4c 45 22 2e 0a 23 0a 66 6f 72 65   SIMPLE"..#.fore
132f0 61 63 68 20 7a 4d 61 74 63 68 20 5b 6c 69 73 74  ach zMatch [list
13300 20 53 49 4d 50 4c 45 20 50 41 52 54 49 41 4c 20   SIMPLE PARTIAL 
13310 46 55 4c 4c 20 53 69 6d 70 6c 65 20 70 61 72 54  FULL Simple parT
13320 49 41 4c 20 46 75 4c 4c 20 5d 20 7b 0a 20 20 64  IAL FuLL ] {.  d
13330 72 6f 70 5f 61 6c 6c 5f 74 61 62 6c 65 73 0a 20  rop_all_tables. 
13340 20 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d   do_test e_fkey-
13350 31 2e 24 7a 4d 61 74 63 68 2e 31 20 7b 0a 20 20  1.$zMatch.1 {.  
13360 20 20 65 78 65 63 73 71 6c 20 22 0a 20 20 20 20    execsql ".    
13370 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 70    CREATE TABLE p
13380 28 61 2c 20 62 2c 20 63 2c 20 50 52 49 4d 41 52  (a, b, c, PRIMAR
13390 59 20 4b 45 59 28 62 2c 20 63 29 29 3b 0a 20 20  Y KEY(b, c));.  
133a0 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45      CREATE TABLE
133b0 20 63 28 64 2c 20 65 2c 20 66 2c 20 46 4f 52 45   c(d, e, f, FORE
133c0 49 47 4e 20 4b 45 59 28 65 2c 20 66 29 20 52 45  IGN KEY(e, f) RE
133d0 46 45 52 45 4e 43 45 53 20 70 20 4d 41 54 43 48  FERENCES p MATCH
133e0 20 24 7a 4d 61 74 63 68 29 3b 0a 20 20 20 20 22   $zMatch);.    "
133f0 0a 20 20 7d 20 7b 7d 0a 20 20 64 6f 5f 74 65 73  .  } {}.  do_tes
13400 74 20 65 5f 66 6b 65 79 2d 31 2e 24 7a 4d 61 74  t e_fkey-1.$zMat
13410 63 68 2e 32 20 7b 0a 20 20 20 20 65 78 65 63 73  ch.2 {.    execs
13420 71 6c 20 7b 20 49 4e 53 45 52 54 20 49 4e 54 4f  ql { INSERT INTO
13430 20 70 20 56 41 4c 55 45 53 28 31 2c 20 32 2c 20   p VALUES(1, 2, 
13440 33 29 20 20 20 20 20 20 20 20 20 7d 0a 0a 20 20  3)         }..  
13450 20 20 23 20 4d 41 54 43 48 20 53 49 4d 50 4c 45    # MATCH SIMPLE
13460 20 62 65 68 61 76 69 6f 75 72 3a 20 41 6c 6c 6f   behaviour: Allo
13470 77 20 61 6e 79 20 63 68 69 6c 64 20 6b 65 79 20  w any child key 
13480 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 6f 6e  that contains on
13490 65 20 6f 72 20 6d 6f 72 65 0a 20 20 20 20 23 20  e or more.    # 
134a0 4e 55 4c 4c 20 76 61 6c 75 65 20 74 6f 20 62 65  NULL value to be
134b0 20 69 6e 73 65 72 74 65 64 2e 20 4e 6f 6e 2d 4e   inserted. Non-N
134c0 55 4c 4c 20 76 61 6c 75 65 73 20 64 6f 20 6e 6f  ULL values do no
134d0 74 20 68 61 76 65 20 74 6f 20 6d 61 70 20 74 6f  t have to map to
134e0 20 61 6e 79 0a 20 20 20 20 23 20 70 61 72 65 6e   any.    # paren
134f0 74 20 6b 65 79 20 76 61 6c 75 65 73 2c 20 73 6f  t key values, so
13500 20 6c 6f 6e 67 20 61 73 20 61 74 20 6c 65 61 73   long as at leas
13510 74 20 6f 6e 65 20 66 69 65 6c 64 20 6f 66 20 74  t one field of t
13520 68 65 20 63 68 69 6c 64 20 6b 65 79 20 69 73 0a  he child key is.
13530 20 20 20 20 23 20 4e 55 4c 4c 2e 0a 20 20 20 20      # NULL..    
13540 65 78 65 63 73 71 6c 20 7b 20 49 4e 53 45 52 54  execsql { INSERT
13550 20 49 4e 54 4f 20 63 20 56 41 4c 55 45 53 28 27   INTO c VALUES('
13560 77 27 2c 20 32 2c 20 33 29 20 20 20 20 20 20 20  w', 2, 3)       
13570 7d 0a 20 20 20 20 65 78 65 63 73 71 6c 20 7b 20  }.    execsql { 
13580 49 4e 53 45 52 54 20 49 4e 54 4f 20 63 20 56 41  INSERT INTO c VA
13590 4c 55 45 53 28 27 78 27 2c 20 27 78 27 2c 20 4e  LUES('x', 'x', N
135a0 55 4c 4c 29 20 20 7d 0a 20 20 20 20 65 78 65 63  ULL)  }.    exec
135b0 73 71 6c 20 7b 20 49 4e 53 45 52 54 20 49 4e 54  sql { INSERT INT
135c0 4f 20 63 20 56 41 4c 55 45 53 28 27 79 27 2c 20  O c VALUES('y', 
135d0 4e 55 4c 4c 2c 20 27 78 27 29 20 20 7d 0a 20 20  NULL, 'x')  }.  
135e0 20 20 65 78 65 63 73 71 6c 20 7b 20 49 4e 53 45    execsql { INSE
135f0 52 54 20 49 4e 54 4f 20 63 20 56 41 4c 55 45 53  RT INTO c VALUES
13600 28 27 7a 27 2c 20 4e 55 4c 4c 2c 20 4e 55 4c 4c  ('z', NULL, NULL
13610 29 20 7d 0a 0a 20 20 20 20 23 20 43 68 65 63 6b  ) }..    # Check
13620 20 74 68 61 74 20 74 68 65 20 46 4b 20 69 73 20   that the FK is 
13630 65 6e 66 6f 72 63 65 64 20 70 72 6f 70 65 72 6c  enforced properl
13640 79 20 69 66 20 74 68 65 72 65 20 61 72 65 20 6e  y if there are n
13650 6f 20 4e 55 4c 4c 20 76 61 6c 75 65 73 20 0a 20  o NULL values . 
13660 20 20 20 23 20 69 6e 20 74 68 65 20 63 68 69 6c     # in the chil
13670 64 20 6b 65 79 20 63 6f 6c 75 6d 6e 73 2e 0a 20  d key columns.. 
13680 20 20 20 63 61 74 63 68 73 71 6c 20 7b 20 49 4e     catchsql { IN
13690 53 45 52 54 20 49 4e 54 4f 20 63 20 56 41 4c 55  SERT INTO c VALU
136a0 45 53 28 27 61 27 2c 20 32 2c 20 34 29 20 7d 0a  ES('a', 2, 4) }.
136b0 20 20 7d 20 7b 31 20 7b 66 6f 72 65 69 67 6e 20    } {1 {foreign 
136c0 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 20 66  key constraint f
136d0 61 69 6c 65 64 7d 7d 0a 7d 0a 0a 23 2d 2d 2d 2d  ailed}}.}..#----
136e0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
136f0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
13700 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
13710 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
13720 2d 2d 2d 2d 2d 0a 23 20 2f 2a 20 45 56 3a 20 52  -----.# /* EV: R
13730 2d 32 31 35 39 39 2d 31 36 30 33 38 20 2a 2f 0a  -21599-16038 */.
13740 23 0a 23 20 54 65 73 74 20 74 68 61 74 20 53 51  #.# Test that SQ
13750 4c 69 74 65 20 64 6f 65 73 20 6e 6f 74 20 73 75  Lite does not su
13760 70 70 6f 72 74 20 74 68 65 20 53 45 54 20 43 4f  pport the SET CO
13770 4e 53 54 52 41 49 4e 54 20 73 74 61 74 65 6d 65  NSTRAINT stateme
13780 6e 74 2e 20 41 6e 64 0a 23 20 74 68 61 74 20 69  nt. And.# that i
13790 74 20 69 73 20 70 6f 73 73 69 62 6c 65 20 74 6f  t is possible to
137a0 20 63 72 65 61 74 65 20 62 6f 74 68 20 69 6d 6d   create both imm
137b0 65 64 69 61 74 65 20 61 6e 64 20 64 65 66 65 72  ediate and defer
137c0 72 65 64 20 63 6f 6e 73 74 72 61 69 6e 74 73 2e  red constraints.
137d0 0a 23 0a 64 72 6f 70 5f 61 6c 6c 5f 74 61 62 6c  .#.drop_all_tabl
137e0 65 73 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65  es.do_test e_fke
137f0 79 2d 32 2e 31 20 7b 0a 20 20 63 61 74 63 68 73  y-2.1 {.  catchs
13800 71 6c 20 7b 20 53 45 54 20 43 4f 4e 53 54 52 41  ql { SET CONSTRA
13810 49 4e 54 53 20 41 4c 4c 20 49 4d 4d 45 44 49 41  INTS ALL IMMEDIA
13820 54 45 20 7d 0a 7d 20 7b 31 20 7b 6e 65 61 72 20  TE }.} {1 {near 
13830 22 53 45 54 22 3a 20 73 79 6e 74 61 78 20 65 72  "SET": syntax er
13840 72 6f 72 7d 7d 0a 64 6f 5f 74 65 73 74 20 65 5f  ror}}.do_test e_
13850 66 6b 65 79 2d 32 2e 32 20 7b 0a 20 20 63 61 74  fkey-2.2 {.  cat
13860 63 68 73 71 6c 20 7b 20 53 45 54 20 43 4f 4e 53  chsql { SET CONS
13870 54 52 41 49 4e 54 53 20 41 4c 4c 20 44 45 46 45  TRAINTS ALL DEFE
13880 52 52 45 44 20 7d 0a 7d 20 7b 31 20 7b 6e 65 61  RRED }.} {1 {nea
13890 72 20 22 53 45 54 22 3a 20 73 79 6e 74 61 78 20  r "SET": syntax 
138a0 65 72 72 6f 72 7d 7d 0a 0a 64 6f 5f 74 65 73 74  error}}..do_test
138b0 20 65 5f 66 6b 65 79 2d 32 2e 33 20 7b 0a 20 20   e_fkey-2.3 {.  
138c0 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 43 52  execsql {.    CR
138d0 45 41 54 45 20 54 41 42 4c 45 20 70 28 61 2c 20  EATE TABLE p(a, 
138e0 62 2c 20 50 52 49 4d 41 52 59 20 4b 45 59 28 61  b, PRIMARY KEY(a
138f0 2c 20 62 29 29 3b 0a 20 20 20 20 43 52 45 41 54  , b));.    CREAT
13900 45 20 54 41 42 4c 45 20 63 64 28 63 2c 20 64 2c  E TABLE cd(c, d,
13910 20 0a 20 20 20 20 20 20 46 4f 52 45 49 47 4e 20   .      FOREIGN 
13920 4b 45 59 28 63 2c 20 64 29 20 52 45 46 45 52 45  KEY(c, d) REFERE
13930 4e 43 45 53 20 70 20 44 45 46 45 52 52 41 42 4c  NCES p DEFERRABL
13940 45 20 49 4e 49 54 49 41 4c 4c 59 20 44 45 46 45  E INITIALLY DEFE
13950 52 52 45 44 29 3b 0a 20 20 20 20 43 52 45 41 54  RRED);.    CREAT
13960 45 20 54 41 42 4c 45 20 63 69 28 63 2c 20 64 2c  E TABLE ci(c, d,
13970 20 0a 20 20 20 20 20 20 46 4f 52 45 49 47 4e 20   .      FOREIGN 
13980 4b 45 59 28 63 2c 20 64 29 20 52 45 46 45 52 45  KEY(c, d) REFERE
13990 4e 43 45 53 20 70 20 44 45 46 45 52 52 41 42 4c  NCES p DEFERRABL
139a0 45 20 49 4e 49 54 49 41 4c 4c 59 20 49 4d 4d 45  E INITIALLY IMME
139b0 44 49 41 54 45 29 3b 0a 20 20 20 20 42 45 47 49  DIATE);.    BEGI
139c0 4e 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a 64 6f 5f 74  N;.  }.} {}.do_t
139d0 65 73 74 20 65 5f 66 6b 65 79 2d 32 2e 34 20 7b  est e_fkey-2.4 {
139e0 0a 20 20 63 61 74 63 68 73 71 6c 20 7b 20 49 4e  .  catchsql { IN
139f0 53 45 52 54 20 49 4e 54 4f 20 63 69 20 56 41 4c  SERT INTO ci VAL
13a00 55 45 53 28 27 78 27 2c 20 27 79 27 29 20 7d 0a  UES('x', 'y') }.
13a10 7d 20 7b 31 20 7b 66 6f 72 65 69 67 6e 20 6b 65  } {1 {foreign ke
13a20 79 20 63 6f 6e 73 74 72 61 69 6e 74 20 66 61 69  y constraint fai
13a30 6c 65 64 7d 7d 0a 64 6f 5f 74 65 73 74 20 65 5f  led}}.do_test e_
13a40 66 6b 65 79 2d 32 2e 35 20 7b 0a 20 20 63 61 74  fkey-2.5 {.  cat
13a50 63 68 73 71 6c 20 7b 20 49 4e 53 45 52 54 20 49  chsql { INSERT I
13a60 4e 54 4f 20 63 64 20 56 41 4c 55 45 53 28 27 78  NTO cd VALUES('x
13a70 27 2c 20 27 79 27 29 20 7d 0a 7d 20 7b 30 20 7b  ', 'y') }.} {0 {
13a80 7d 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65  }}.do_test e_fke
13a90 79 2d 32 2e 36 20 7b 0a 20 20 63 61 74 63 68 73  y-2.6 {.  catchs
13aa0 71 6c 20 7b 20 43 4f 4d 4d 49 54 20 7d 0a 7d 20  ql { COMMIT }.} 
13ab0 7b 31 20 7b 66 6f 72 65 69 67 6e 20 6b 65 79 20  {1 {foreign key 
13ac0 63 6f 6e 73 74 72 61 69 6e 74 20 66 61 69 6c 65  constraint faile
13ad0 64 7d 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b  d}}.do_test e_fk
13ae0 65 79 2d 32 2e 37 20 7b 0a 20 20 65 78 65 63 73  ey-2.7 {.  execs
13af0 71 6c 20 7b 20 0a 20 20 20 20 44 45 4c 45 54 45  ql { .    DELETE
13b00 20 46 52 4f 4d 20 63 64 3b 0a 20 20 20 20 43 4f   FROM cd;.    CO
13b10 4d 4d 49 54 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a 0a  MMIT;.  }.} {}..
13b20 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  #---------------
13b30 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
13b40 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
13b50 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
13b60 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20 2f 2a 20  ----------.# /* 
13b70 45 56 3a 20 52 2d 34 32 32 36 34 2d 33 30 35 30  EV: R-42264-3050
13b80 33 20 2a 2f 0a 23 0a 23 20 54 65 73 74 20 74 68  3 */.#.# Test th
13b90 61 74 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 72  at the maximum r
13ba0 65 63 75 72 73 69 6f 6e 20 64 65 70 74 68 20 6f  ecursion depth o
13bb0 66 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 61 63  f foreign key ac
13bc0 74 69 6f 6e 20 70 72 6f 67 72 61 6d 73 20 69 73  tion programs is
13bd0 0a 23 20 67 6f 76 65 72 6e 65 64 20 62 79 20 74  .# governed by t
13be0 68 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 54 52  he SQLITE_MAX_TR
13bf0 49 47 47 45 52 5f 44 45 50 54 48 20 61 6e 64 20  IGGER_DEPTH and 
13c00 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 54 52 49  SQLITE_LIMIT_TRI
13c10 47 47 45 52 5f 44 45 50 54 48 0a 23 20 73 65 74  GGER_DEPTH.# set
13c20 74 69 6e 67 73 2e 0a 23 0a 70 72 6f 63 20 74 65  tings..#.proc te
13c30 73 74 5f 6f 6e 5f 64 65 6c 65 74 65 5f 72 65 63  st_on_delete_rec
13c40 75 72 73 69 6f 6e 20 7b 6c 69 6d 69 74 7d 20 7b  ursion {limit} {
13c50 0a 20 20 64 72 6f 70 5f 61 6c 6c 5f 74 61 62 6c  .  drop_all_tabl
13c60 65 73 0a 20 20 65 78 65 63 73 71 6c 20 7b 20 0a  es.  execsql { .
13c70 20 20 20 20 42 45 47 49 4e 3b 0a 20 20 20 20 43      BEGIN;.    C
13c80 52 45 41 54 45 20 54 41 42 4c 45 20 74 30 28 61  REATE TABLE t0(a
13c90 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 62 29   PRIMARY KEY, b)
13ca0 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54  ;.    INSERT INT
13cb0 4f 20 74 30 20 56 41 4c 55 45 53 28 27 78 30 27  O t0 VALUES('x0'
13cc0 2c 20 4e 55 4c 4c 29 3b 0a 20 20 7d 0a 20 20 66  , NULL);.  }.  f
13cd0 6f 72 20 7b 73 65 74 20 69 20 31 7d 20 7b 24 69  or {set i 1} {$i
13ce0 20 3c 3d 20 24 6c 69 6d 69 74 7d 20 7b 69 6e 63   <= $limit} {inc
13cf0 72 20 69 7d 20 7b 0a 20 20 20 20 65 78 65 63 73  r i} {.    execs
13d00 71 6c 20 22 0a 20 20 20 20 20 20 43 52 45 41 54  ql ".      CREAT
13d10 45 20 54 41 42 4c 45 20 74 24 69 20 28 0a 20 20  E TABLE t$i (.  
13d20 20 20 20 20 20 20 61 20 50 52 49 4d 41 52 59 20        a PRIMARY 
13d30 4b 45 59 2c 20 62 20 52 45 46 45 52 45 4e 43 45  KEY, b REFERENCE
13d40 53 20 74 5b 65 78 70 72 20 24 69 2d 31 5d 20 4f  S t[expr $i-1] O
13d50 4e 20 44 45 4c 45 54 45 20 43 41 53 43 41 44 45  N DELETE CASCADE
13d60 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20  .      );.      
13d70 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 24 69 20  INSERT INTO t$i 
13d80 56 41 4c 55 45 53 28 27 78 24 69 27 2c 20 27 78  VALUES('x$i', 'x
13d90 5b 65 78 70 72 20 24 69 2d 31 5d 27 29 3b 0a 20  [expr $i-1]');. 
13da0 20 20 20 22 0a 20 20 7d 0a 20 20 65 78 65 63 73     ".  }.  execs
13db0 71 6c 20 43 4f 4d 4d 49 54 0a 20 20 63 61 74 63  ql COMMIT.  catc
13dc0 68 73 71 6c 20 22 0a 20 20 20 20 44 45 4c 45 54  hsql ".    DELET
13dd0 45 20 46 52 4f 4d 20 74 30 3b 0a 20 20 20 20 53  E FROM t0;.    S
13de0 45 4c 45 43 54 20 63 6f 75 6e 74 28 2a 29 20 46  ELECT count(*) F
13df0 52 4f 4d 20 74 24 6c 69 6d 69 74 3b 0a 20 20 22  ROM t$limit;.  "
13e00 0a 7d 0a 70 72 6f 63 20 74 65 73 74 5f 6f 6e 5f  .}.proc test_on_
13e10 75 70 64 61 74 65 5f 72 65 63 75 72 73 69 6f 6e  update_recursion
13e20 20 7b 6c 69 6d 69 74 7d 20 7b 0a 20 20 64 72 6f   {limit} {.  dro
13e30 70 5f 61 6c 6c 5f 74 61 62 6c 65 73 0a 20 20 65  p_all_tables.  e
13e40 78 65 63 73 71 6c 20 7b 20 0a 20 20 20 20 42 45  xecsql { .    BE
13e50 47 49 4e 3b 0a 20 20 20 20 43 52 45 41 54 45 20  GIN;.    CREATE 
13e60 54 41 42 4c 45 20 74 30 28 61 20 50 52 49 4d 41  TABLE t0(a PRIMA
13e70 52 59 20 4b 45 59 29 3b 0a 20 20 20 20 49 4e 53  RY KEY);.    INS
13e80 45 52 54 20 49 4e 54 4f 20 74 30 20 56 41 4c 55  ERT INTO t0 VALU
13e90 45 53 28 27 78 78 78 27 29 3b 0a 20 20 7d 0a 20  ES('xxx');.  }. 
13ea0 20 66 6f 72 20 7b 73 65 74 20 69 20 31 7d 20 7b   for {set i 1} {
13eb0 24 69 20 3c 3d 20 24 6c 69 6d 69 74 7d 20 7b 69  $i <= $limit} {i
13ec0 6e 63 72 20 69 7d 20 7b 0a 20 20 20 20 73 65 74  ncr i} {.    set
13ed0 20 6a 20 5b 65 78 70 72 20 24 69 2d 31 5d 0a 0a   j [expr $i-1]..
13ee0 20 20 20 20 65 78 65 63 73 71 6c 20 22 0a 20 20      execsql ".  
13ef0 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45      CREATE TABLE
13f00 20 74 24 69 20 28 61 20 50 52 49 4d 41 52 59 20   t$i (a PRIMARY 
13f10 4b 45 59 20 52 45 46 45 52 45 4e 43 45 53 20 74  KEY REFERENCES t
13f20 24 6a 20 4f 4e 20 55 50 44 41 54 45 20 43 41 53  $j ON UPDATE CAS
13f30 43 41 44 45 29 3b 0a 20 20 20 20 20 20 49 4e 53  CADE);.      INS
13f40 45 52 54 20 49 4e 54 4f 20 74 24 69 20 56 41 4c  ERT INTO t$i VAL
13f50 55 45 53 28 27 78 78 78 27 29 3b 0a 20 20 20 20  UES('xxx');.    
13f60 22 0a 20 20 7d 0a 20 20 65 78 65 63 73 71 6c 20  ".  }.  execsql 
13f70 43 4f 4d 4d 49 54 0a 20 20 63 61 74 63 68 73 71  COMMIT.  catchsq
13f80 6c 20 22 0a 20 20 20 20 55 50 44 41 54 45 20 74  l ".    UPDATE t
13f90 30 20 53 45 54 20 61 20 3d 20 27 79 79 79 27 3b  0 SET a = 'yyy';
13fa0 0a 20 20 20 20 53 45 4c 45 43 54 20 4e 4f 54 20  .    SELECT NOT 
13fb0 28 61 3d 27 79 79 79 27 29 20 46 52 4f 4d 20 74  (a='yyy') FROM t
13fc0 24 6c 69 6d 69 74 3b 0a 20 20 22 0a 7d 0a 0a 64  $limit;.  ".}..d
13fd0 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d 33 2e  o_test e_fkey-3.
13fe0 31 2e 31 20 7b 0a 20 20 74 65 73 74 5f 6f 6e 5f  1.1 {.  test_on_
13ff0 64 65 6c 65 74 65 5f 72 65 63 75 72 73 69 6f 6e  delete_recursion
14000 20 24 53 51 4c 49 54 45 5f 4d 41 58 5f 54 52 49   $SQLITE_MAX_TRI
14010 47 47 45 52 5f 44 45 50 54 48 0a 7d 20 7b 30 20  GGER_DEPTH.} {0 
14020 30 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65  0}.do_test e_fke
14030 79 2d 33 2e 31 2e 32 20 7b 0a 20 20 74 65 73 74  y-3.1.2 {.  test
14040 5f 6f 6e 5f 64 65 6c 65 74 65 5f 72 65 63 75 72  _on_delete_recur
14050 73 69 6f 6e 20 5b 65 78 70 72 20 24 53 51 4c 49  sion [expr $SQLI
14060 54 45 5f 4d 41 58 5f 54 52 49 47 47 45 52 5f 44  TE_MAX_TRIGGER_D
14070 45 50 54 48 2b 31 5d 0a 7d 20 7b 31 20 7b 74 6f  EPTH+1].} {1 {to
14080 6f 20 6d 61 6e 79 20 6c 65 76 65 6c 73 20 6f 66  o many levels of
14090 20 74 72 69 67 67 65 72 20 72 65 63 75 72 73 69   trigger recursi
140a0 6f 6e 7d 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66  on}}.do_test e_f
140b0 6b 65 79 2d 33 2e 31 2e 33 20 7b 0a 20 20 73 71  key-3.1.3 {.  sq
140c0 6c 69 74 65 33 5f 6c 69 6d 69 74 20 64 62 20 53  lite3_limit db S
140d0 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 54 52 49 47  QLITE_LIMIT_TRIG
140e0 47 45 52 5f 44 45 50 54 48 20 35 0a 20 20 74 65  GER_DEPTH 5.  te
140f0 73 74 5f 6f 6e 5f 64 65 6c 65 74 65 5f 72 65 63  st_on_delete_rec
14100 75 72 73 69 6f 6e 20 35 0a 7d 20 7b 30 20 30 7d  ursion 5.} {0 0}
14110 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79 2d  .do_test e_fkey-
14120 33 2e 31 2e 34 20 7b 0a 20 20 74 65 73 74 5f 6f  3.1.4 {.  test_o
14130 6e 5f 64 65 6c 65 74 65 5f 72 65 63 75 72 73 69  n_delete_recursi
14140 6f 6e 20 36 0a 7d 20 7b 31 20 7b 74 6f 6f 20 6d  on 6.} {1 {too m
14150 61 6e 79 20 6c 65 76 65 6c 73 20 6f 66 20 74 72  any levels of tr
14160 69 67 67 65 72 20 72 65 63 75 72 73 69 6f 6e 7d  igger recursion}
14170 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65 79  }.do_test e_fkey
14180 2d 33 2e 31 2e 35 20 7b 0a 20 20 73 71 6c 69 74  -3.1.5 {.  sqlit
14190 65 33 5f 6c 69 6d 69 74 20 64 62 20 53 51 4c 49  e3_limit db SQLI
141a0 54 45 5f 4c 49 4d 49 54 5f 54 52 49 47 47 45 52  TE_LIMIT_TRIGGER
141b0 5f 44 45 50 54 48 20 31 30 30 30 30 30 30 0a 7d  _DEPTH 1000000.}
141c0 20 7b 35 7d 0a 64 6f 5f 74 65 73 74 20 65 5f 66   {5}.do_test e_f
141d0 6b 65 79 2d 33 2e 32 2e 31 20 7b 0a 20 20 74 65  key-3.2.1 {.  te
141e0 73 74 5f 6f 6e 5f 75 70 64 61 74 65 5f 72 65 63  st_on_update_rec
141f0 75 72 73 69 6f 6e 20 24 53 51 4c 49 54 45 5f 4d  ursion $SQLITE_M
14200 41 58 5f 54 52 49 47 47 45 52 5f 44 45 50 54 48  AX_TRIGGER_DEPTH
14210 0a 7d 20 7b 30 20 30 7d 0a 64 6f 5f 74 65 73 74  .} {0 0}.do_test
14220 20 65 5f 66 6b 65 79 2d 33 2e 32 2e 32 20 7b 0a   e_fkey-3.2.2 {.
14230 20 20 74 65 73 74 5f 6f 6e 5f 75 70 64 61 74 65    test_on_update
14240 5f 72 65 63 75 72 73 69 6f 6e 20 5b 65 78 70 72  _recursion [expr
14250 20 24 53 51 4c 49 54 45 5f 4d 41 58 5f 54 52 49   $SQLITE_MAX_TRI
14260 47 47 45 52 5f 44 45 50 54 48 2b 31 5d 0a 7d 20  GGER_DEPTH+1].} 
14270 7b 31 20 7b 74 6f 6f 20 6d 61 6e 79 20 6c 65 76  {1 {too many lev
14280 65 6c 73 20 6f 66 20 74 72 69 67 67 65 72 20 72  els of trigger r
14290 65 63 75 72 73 69 6f 6e 7d 7d 0a 64 6f 5f 74 65  ecursion}}.do_te
142a0 73 74 20 65 5f 66 6b 65 79 2d 33 2e 32 2e 33 20  st e_fkey-3.2.3 
142b0 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6c 69 6d 69  {.  sqlite3_limi
142c0 74 20 64 62 20 53 51 4c 49 54 45 5f 4c 49 4d 49  t db SQLITE_LIMI
142d0 54 5f 54 52 49 47 47 45 52 5f 44 45 50 54 48 20  T_TRIGGER_DEPTH 
142e0 35 0a 20 20 74 65 73 74 5f 6f 6e 5f 75 70 64 61  5.  test_on_upda
142f0 74 65 5f 72 65 63 75 72 73 69 6f 6e 20 35 0a 7d  te_recursion 5.}
14300 20 7b 30 20 30 7d 0a 64 6f 5f 74 65 73 74 20 65   {0 0}.do_test e
14310 5f 66 6b 65 79 2d 33 2e 32 2e 34 20 7b 0a 20 20  _fkey-3.2.4 {.  
14320 74 65 73 74 5f 6f 6e 5f 75 70 64 61 74 65 5f 72  test_on_update_r
14330 65 63 75 72 73 69 6f 6e 20 36 0a 7d 20 7b 31 20  ecursion 6.} {1 
14340 7b 74 6f 6f 20 6d 61 6e 79 20 6c 65 76 65 6c 73  {too many levels
14350 20 6f 66 20 74 72 69 67 67 65 72 20 72 65 63 75   of trigger recu
14360 72 73 69 6f 6e 7d 7d 0a 64 6f 5f 74 65 73 74 20  rsion}}.do_test 
14370 65 5f 66 6b 65 79 2d 33 2e 32 2e 35 20 7b 0a 20  e_fkey-3.2.5 {. 
14380 20 73 71 6c 69 74 65 33 5f 6c 69 6d 69 74 20 64   sqlite3_limit d
14390 62 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 54  b SQLITE_LIMIT_T
143a0 52 49 47 47 45 52 5f 44 45 50 54 48 20 31 30 30  RIGGER_DEPTH 100
143b0 30 30 30 30 0a 7d 20 7b 35 7d 0a 0a 23 2d 2d 2d  0000.} {5}..#---
143c0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
143d0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
143e0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
143f0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
14400 2d 2d 2d 2d 2d 2d 0a 23 20 2f 2a 20 45 56 3a 20  ------.# /* EV: 
14410 52 2d 35 31 37 36 39 2d 33 32 37 33 30 20 2a 2f  R-51769-32730 */
14420 0a 23 0a 23 20 54 68 65 20 73 65 74 74 69 6e 67  .#.# The setting
14430 20 6f 66 20 74 68 65 20 72 65 63 75 72 73 69 76   of the recursiv
14440 65 5f 74 72 69 67 67 65 72 73 20 70 72 61 67 6d  e_triggers pragm
14450 61 20 64 6f 65 73 20 6e 6f 74 20 61 66 66 65 63  a does not affec
14460 74 20 66 6f 72 65 69 67 6e 0a 23 20 6b 65 79 20  t foreign.# key 
14470 61 63 74 69 6f 6e 73 2e 0a 23 0a 66 6f 72 65 61  actions..#.forea
14480 63 68 20 72 65 63 75 72 73 69 76 65 5f 74 72 69  ch recursive_tri
14490 67 67 65 72 73 5f 73 65 74 74 69 6e 67 20 5b 6c  ggers_setting [l
144a0 69 73 74 20 30 20 31 20 4f 4e 20 4f 46 46 5d 20  ist 0 1 ON OFF] 
144b0 7b 0a 20 20 64 72 6f 70 5f 61 6c 6c 5f 74 61 62  {.  drop_all_tab
144c0 6c 65 73 0a 20 20 65 78 65 63 73 71 6c 20 22 50  les.  execsql "P
144d0 52 41 47 4d 41 20 72 65 63 75 72 73 69 76 65 5f  RAGMA recursive_
144e0 74 72 69 67 67 65 72 73 20 3d 20 24 72 65 63 75  triggers = $recu
144f0 72 73 69 76 65 5f 74 72 69 67 67 65 72 73 5f 73  rsive_triggers_s
14500 65 74 74 69 6e 67 22 0a 0a 20 20 64 6f 5f 74 65  etting"..  do_te
14510 73 74 20 65 5f 66 6b 65 79 2d 34 2e 24 72 65 63  st e_fkey-4.$rec
14520 75 72 73 69 76 65 5f 74 72 69 67 67 65 72 73 5f  ursive_triggers_
14530 73 65 74 74 69 6e 67 2e 31 20 7b 0a 20 20 20 20  setting.1 {.    
14540 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 20 20  execsql {.      
14550 43 52 45 41 54 45 20 54 41 42 4c 45 20 74 31 28  CREATE TABLE t1(
14560 61 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 62  a PRIMARY KEY, b
14570 20 52 45 46 45 52 45 4e 43 45 53 20 74 31 20 4f   REFERENCES t1 O
14580 4e 20 44 45 4c 45 54 45 20 43 41 53 43 41 44 45  N DELETE CASCADE
14590 29 3b 0a 20 20 20 20 20 20 49 4e 53 45 52 54 20  );.      INSERT 
145a0 49 4e 54 4f 20 74 31 20 56 41 4c 55 45 53 28 31  INTO t1 VALUES(1
145b0 2c 20 4e 55 4c 4c 29 3b 0a 20 20 20 20 20 20 49  , NULL);.      I
145c0 4e 53 45 52 54 20 49 4e 54 4f 20 74 31 20 56 41  NSERT INTO t1 VA
145d0 4c 55 45 53 28 32 2c 20 31 29 3b 0a 20 20 20 20  LUES(2, 1);.    
145e0 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 31    INSERT INTO t1
145f0 20 56 41 4c 55 45 53 28 33 2c 20 32 29 3b 0a 20   VALUES(3, 2);. 
14600 20 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f       INSERT INTO
14610 20 74 31 20 56 41 4c 55 45 53 28 34 2c 20 33 29   t1 VALUES(4, 3)
14620 3b 0a 20 20 20 20 20 20 49 4e 53 45 52 54 20 49  ;.      INSERT I
14630 4e 54 4f 20 74 31 20 56 41 4c 55 45 53 28 35 2c  NTO t1 VALUES(5,
14640 20 34 29 3b 0a 20 20 20 20 20 20 53 45 4c 45 43   4);.      SELEC
14650 54 20 63 6f 75 6e 74 28 2a 29 20 46 52 4f 4d 20  T count(*) FROM 
14660 74 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 20 7b 35  t1;.    }.  } {5
14670 7d 0a 20 20 64 6f 5f 74 65 73 74 20 65 5f 66 6b  }.  do_test e_fk
14680 65 79 2d 34 2e 24 72 65 63 75 72 73 69 76 65 5f  ey-4.$recursive_
14690 74 72 69 67 67 65 72 73 5f 73 65 74 74 69 6e 67  triggers_setting
146a0 2e 32 20 7b 0a 20 20 20 20 65 78 65 63 73 71 6c  .2 {.    execsql
146b0 20 7b 20 53 45 4c 45 43 54 20 63 6f 75 6e 74 28   { SELECT count(
146c0 2a 29 20 46 52 4f 4d 20 74 31 20 57 48 45 52 45  *) FROM t1 WHERE
146d0 20 61 20 3d 20 31 20 7d 0a 20 20 7d 20 7b 31 7d   a = 1 }.  } {1}
146e0 0a 20 20 64 6f 5f 74 65 73 74 20 65 5f 66 6b 65  .  do_test e_fke
146f0 79 2d 34 2e 24 72 65 63 75 72 73 69 76 65 5f 74  y-4.$recursive_t
14700 72 69 67 67 65 72 73 5f 73 65 74 74 69 6e 67 2e  riggers_setting.
14710 33 20 7b 0a 20 20 20 20 65 78 65 63 73 71 6c 20  3 {.    execsql 
14720 7b 20 0a 20 20 20 20 20 20 44 45 4c 45 54 45 20  { .      DELETE 
14730 46 52 4f 4d 20 74 31 20 57 48 45 52 45 20 61 20  FROM t1 WHERE a 
14740 3d 20 31 3b 0a 20 20 20 20 20 20 53 45 4c 45 43  = 1;.      SELEC
14750 54 20 63 6f 75 6e 74 28 2a 29 20 46 52 4f 4d 20  T count(*) FROM 
14760 74 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 20 7b 30  t1;.    }.  } {0
14770 7d 0a 7d 0a 0a 66 69 6e 69 73 68 5f 74 65 73 74  }.}..finish_test
14780 0a                                               .