0000: 0a 4f 56 45 52 56 49 45 57 0a 0a 20 20 54 68 65 .OVERVIEW.. The
0010: 20 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79 20 SQLite library
0020: 69 73 20 63 61 70 61 62 6c 65 20 6f 66 20 70 61 is capable of pa
0030: 72 73 69 6e 67 20 53 51 4c 20 66 6f 72 65 69 67 rsing SQL foreig
0040: 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 n key constraint
0050: 73 20 0a 20 20 73 75 70 70 6c 69 65 64 20 61 73 s . supplied as
0060: 20 70 61 72 74 20 6f 66 20 43 52 45 41 54 45 20 part of CREATE
0070: 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 73 TABLE statements
0080: 2c 20 62 75 74 20 69 74 20 64 6f 65 73 20 6e 6f , but it does no
0090: 74 20 61 63 74 75 61 6c 6c 79 20 0a 20 20 69 6d t actually . im
00a0: 70 6c 65 6d 65 6e 74 20 74 68 65 6d 2e 20 48 6f plement them. Ho
00b0: 77 65 76 65 72 2c 20 6d 6f 73 74 20 6f 66 20 74 wever, most of t
00c0: 68 65 20 66 65 61 74 75 72 65 73 20 6f 66 20 66 he features of f
00d0: 6f 72 65 69 67 6e 20 6b 65 79 73 20 6d 61 79 20 oreign keys may
00e0: 62 65 0a 20 20 69 6d 70 6c 65 6d 65 6e 74 65 64 be. implemented
00f0: 20 75 73 69 6e 67 20 53 51 4c 20 74 72 69 67 67 using SQL trigg
0100: 65 72 73 2c 20 77 68 69 63 68 20 53 51 4c 69 74 ers, which SQLit
0110: 65 20 64 6f 65 73 20 73 75 70 70 6f 72 74 2e 20 e does support.
0120: 54 68 69 73 20 74 65 78 74 0a 20 20 66 69 6c 65 This text. file
0130: 20 64 65 73 63 72 69 62 65 73 20 61 20 66 65 61 describes a fea
0140: 74 75 72 65 20 6f 66 20 74 68 65 20 53 51 4c 69 ture of the SQLi
0150: 74 65 20 73 68 65 6c 6c 20 74 6f 6f 6c 20 28 73 te shell tool (s
0160: 71 6c 69 74 65 33 29 20 74 68 61 74 0a 20 20 65 qlite3) that. e
0170: 78 74 72 61 63 74 73 20 66 6f 72 65 69 67 6e 20 xtracts foreign
0180: 6b 65 79 20 64 65 66 69 6e 69 74 69 6f 6e 73 20 key definitions
0190: 66 72 6f 6d 20 61 6e 20 65 78 69 73 74 69 6e 67 from an existing
01a0: 20 53 51 4c 69 74 65 20 64 61 74 61 62 61 73 65 SQLite database
01b0: 20 61 6e 64 0a 20 20 63 72 65 61 74 65 73 20 74 and. creates t
01c0: 68 65 20 73 65 74 20 6f 66 20 43 52 45 41 54 45 he set of CREATE
01d0: 20 54 52 49 47 47 45 52 20 73 74 61 74 65 6d 65 TRIGGER stateme
01e0: 6e 74 73 20 72 65 71 75 69 72 65 64 20 74 6f 20 nts required to
01f0: 69 6d 70 6c 65 6d 65 6e 74 0a 20 20 74 68 65 20 implement. the
0200: 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 foreign key cons
0210: 74 72 61 69 6e 74 73 2e 0a 0a 43 41 50 41 42 49 traints...CAPABI
0220: 4c 49 54 49 45 53 0a 0a 20 20 41 6e 20 53 51 4c LITIES.. An SQL
0230: 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 69 73 20 foreign key is
0240: 61 20 63 6f 6e 73 74 72 61 69 6e 74 20 74 68 61 a constraint tha
0250: 74 20 72 65 71 75 69 72 65 73 20 74 68 61 74 20 t requires that
0260: 65 61 63 68 20 72 6f 77 20 69 6e 0a 20 20 74 68 each row in. th
0270: 65 20 22 63 68 69 6c 64 22 20 74 61 62 6c 65 20 e "child" table
0280: 63 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f 20 61 corresponds to a
0290: 20 72 6f 77 20 69 6e 20 74 68 65 20 22 70 61 72 row in the "par
02a0: 65 6e 74 22 20 74 61 62 6c 65 2e 20 46 6f 72 0a ent" table. For.
02b0: 20 20 65 78 61 6d 70 6c 65 2c 20 74 68 65 20 66 example, the f
02c0: 6f 6c 6c 6f 77 69 6e 67 20 73 63 68 65 6d 61 3a ollowing schema:
02d0: 0a 0a 20 20 20 20 43 52 45 41 54 45 20 54 41 42 .. CREATE TAB
02e0: 4c 45 20 70 61 72 65 6e 74 28 61 2c 20 62 2c 20 LE parent(a, b,
02f0: 63 2c 20 50 52 49 4d 41 52 59 20 4b 45 59 28 61 c, PRIMARY KEY(a
0300: 2c 20 62 29 29 3b 0a 20 20 20 20 43 52 45 41 54 , b));. CREAT
0310: 45 20 54 41 42 4c 45 20 63 68 69 6c 64 28 64 2c E TABLE child(d,
0320: 20 65 2c 20 66 2c 20 46 4f 52 45 49 47 4e 20 4b e, f, FOREIGN K
0330: 45 59 28 64 2c 20 65 29 20 52 45 46 45 52 45 4e EY(d, e) REFEREN
0340: 43 45 53 20 70 61 72 65 6e 74 28 61 2c 20 62 29 CES parent(a, b)
0350: 29 3b 0a 0a 20 20 69 6d 70 6c 69 65 73 20 74 68 );.. implies th
0360: 61 74 20 66 6f 72 20 65 61 63 68 20 72 6f 77 20 at for each row
0370: 69 6e 20 74 61 62 6c 65 20 22 63 68 69 6c 64 22 in table "child"
0380: 2c 20 74 68 65 72 65 20 6d 75 73 74 20 62 65 20 , there must be
0390: 61 20 72 6f 77 20 69 6e 0a 20 20 22 70 61 72 65 a row in. "pare
03a0: 6e 74 22 20 66 6f 72 20 77 68 69 63 68 20 74 68 nt" for which th
03b0: 65 20 65 78 70 72 65 73 73 69 6f 6e 20 28 63 68 e expression (ch
03c0: 69 6c 64 2e 64 3d 3d 70 61 72 65 6e 74 2e 61 20 ild.d==parent.a
03d0: 41 4e 44 20 63 68 69 6c 64 2e 65 3d 3d 70 61 72 AND child.e==par
03e0: 65 6e 74 2e 62 29 20 0a 20 20 69 73 20 74 72 75 ent.b) . is tru
03f0: 65 2e 20 54 68 65 20 63 6f 6c 75 6d 6e 73 20 69 e. The columns i
0400: 6e 20 74 68 65 20 70 61 72 65 6e 74 20 74 61 62 n the parent tab
0410: 6c 65 20 61 72 65 20 72 65 71 75 69 72 65 64 20 le are required
0420: 74 6f 20 62 65 20 65 69 74 68 65 72 20 74 68 65 to be either the
0430: 0a 20 20 70 72 69 6d 61 72 79 20 6b 65 79 20 63 . primary key c
0440: 6f 6c 75 6d 6e 73 20 6f 72 20 73 75 62 6a 65 63 olumns or subjec
0450: 74 20 74 6f 20 61 20 55 4e 49 51 55 45 20 63 6f t to a UNIQUE co
0460: 6e 73 74 72 61 69 6e 74 2e 20 54 68 65 72 65 20 nstraint. There
0470: 69 73 20 6e 6f 20 73 75 63 68 0a 20 20 72 65 71 is no such. req
0480: 75 69 72 65 6d 65 6e 74 20 66 6f 72 20 74 68 65 uirement for the
0490: 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 74 68 65 20 columns of the
04a0: 63 68 69 6c 64 20 74 61 62 6c 65 2e 0a 0a 20 20 child table...
04b0: 41 74 20 74 68 69 73 20 74 69 6d 65 2c 20 61 6c At this time, al
04c0: 6c 20 66 6f 72 65 69 67 6e 20 6b 65 79 73 20 61 l foreign keys a
04d0: 72 65 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 61 re implemented a
04e0: 73 20 69 66 20 74 68 65 79 20 77 65 72 65 20 0a s if they were .
04f0: 20 20 22 4d 41 54 43 48 20 4e 4f 4e 45 22 2c 20 "MATCH NONE",
0500: 65 76 65 6e 20 69 66 20 74 68 65 20 64 65 63 6c even if the decl
0510: 61 72 61 74 69 6f 6e 20 73 70 65 63 69 66 69 65 aration specifie
0520: 64 20 22 4d 41 54 43 48 20 50 41 52 54 49 41 4c d "MATCH PARTIAL
0530: 22 20 6f 72 20 0a 20 20 22 4d 41 54 43 48 20 46 " or . "MATCH F
0540: 55 4c 4c 22 2e 20 22 4d 41 54 43 48 20 4e 4f 4e ULL". "MATCH NON
0550: 45 22 20 6d 65 61 6e 73 20 74 68 61 74 20 69 66 E" means that if
0560: 20 61 6e 79 20 6f 66 20 74 68 65 20 6b 65 79 20 any of the key
0570: 63 6f 6c 75 6d 6e 73 20 69 6e 0a 20 20 74 68 65 columns in. the
0580: 20 63 68 69 6c 64 20 74 61 62 6c 65 20 61 72 65 child table are
0590: 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 68 65 72 NULL, then ther
05a0: 65 20 69 73 20 6e 6f 20 72 65 71 75 69 72 65 6d e is no requirem
05b0: 65 6e 74 20 66 6f 72 20 61 20 63 6f 72 72 65 73 ent for a corres
05c0: 70 6f 6e 64 69 6e 67 0a 20 20 72 6f 77 20 69 6e ponding. row in
05d0: 20 74 68 65 20 70 61 72 65 6e 74 20 74 61 62 6c the parent tabl
05e0: 65 2e 20 53 6f 2c 20 74 61 6b 69 6e 67 20 74 68 e. So, taking th
05f0: 69 73 20 69 6e 74 6f 20 61 63 63 6f 75 6e 74 2c is into account,
0600: 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 the expression
0610: 74 68 61 74 0a 20 20 6d 75 73 74 20 62 65 20 74 that. must be t
0620: 72 75 65 20 66 6f 72 20 65 76 65 72 79 20 72 6f rue for every ro
0630: 77 20 6f 66 20 74 68 65 20 63 68 69 6c 64 20 74 w of the child t
0640: 61 62 6c 65 20 69 6e 20 74 68 65 20 61 62 6f 76 able in the abov
0650: 65 20 65 78 61 6d 70 6c 65 20 69 73 0a 20 20 61 e example is. a
0660: 63 74 75 61 6c 6c 79 3a 0a 0a 20 20 20 20 20 20 ctually:..
0670: 28 63 68 69 6c 64 2e 64 20 49 53 20 4e 55 4c 4c (child.d IS NULL
0680: 29 20 4f 52 20 0a 20 20 20 20 20 20 28 63 68 69 ) OR . (chi
0690: 6c 64 2e 65 20 49 53 20 4e 55 4c 4c 29 20 4f 52 ld.e IS NULL) OR
06a0: 20 0a 20 20 20 20 20 20 28 63 68 69 6c 64 2e 64 . (child.d
06b0: 3d 3d 70 61 72 65 6e 74 2e 61 20 41 4e 44 20 63 ==parent.a AND c
06c0: 68 69 6c 64 2e 65 3d 3d 70 61 72 65 6e 74 2e 62 hild.e==parent.b
06d0: 29 0a 0a 20 20 41 74 74 65 6d 70 74 69 6e 67 20 ).. Attempting
06e0: 74 6f 20 69 6e 73 65 72 74 20 6f 72 20 75 70 64 to insert or upd
06f0: 61 74 65 20 61 20 72 6f 77 20 69 6e 20 74 68 65 ate a row in the
0700: 20 63 68 69 6c 64 20 74 61 62 6c 65 20 73 6f 20 child table so
0710: 74 68 61 74 20 74 68 65 20 0a 20 20 61 66 66 65 that the . affe
0720: 63 74 65 64 20 72 6f 77 20 76 69 6f 6c 61 74 65 cted row violate
0730: 73 20 74 68 69 73 20 63 6f 6e 73 74 72 61 69 6e s this constrain
0740: 74 20 72 65 73 75 6c 74 73 20 69 6e 20 61 6e 20 t results in an
0750: 65 78 63 65 70 74 69 6f 6e 20 62 65 69 6e 67 20 exception being
0760: 0a 20 20 74 68 72 6f 77 6e 2e 0a 0a 20 20 54 68 . thrown... Th
0770: 65 20 65 66 66 65 63 74 20 6f 66 20 61 74 74 65 e effect of atte
0780: 6d 70 74 69 6e 67 20 74 6f 20 64 65 6c 65 74 65 mpting to delete
0790: 20 6f 72 20 75 70 64 61 74 65 20 61 20 72 6f 77 or update a row
07a0: 20 69 6e 20 74 68 65 20 70 61 72 65 6e 74 20 74 in the parent t
07b0: 61 62 6c 65 20 0a 20 20 73 6f 20 74 68 61 74 20 able . so that
07c0: 74 68 65 20 63 6f 6e 73 74 72 61 69 6e 74 20 62 the constraint b
07d0: 65 63 6f 6d 65 73 20 75 6e 74 72 75 65 20 66 6f ecomes untrue fo
07e0: 72 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 72 6f r one or more ro
07f0: 77 73 20 69 6e 20 74 68 65 20 63 68 69 6c 64 0a ws in the child.
0800: 20 20 74 61 62 6c 65 20 64 65 70 65 6e 64 73 20 table depends
0810: 6f 6e 20 74 68 65 20 22 4f 4e 20 44 45 4c 45 54 on the "ON DELET
0820: 45 22 20 6f 72 20 22 4f 4e 20 55 50 44 41 54 45 E" or "ON UPDATE
0830: 22 20 61 63 74 69 6f 6e 73 20 73 70 65 63 69 66 " actions specif
0840: 69 65 64 20 61 73 0a 20 20 70 61 72 74 20 6f 66 ied as. part of
0850: 20 74 68 65 20 66 6f 72 65 69 67 6e 20 6b 65 79 the foreign key
0860: 20 64 65 66 69 6e 69 74 69 6f 6e 2c 20 72 65 73 definition, res
0870: 70 65 63 74 69 76 65 6c 79 2e 20 54 68 72 65 65 pectively. Three
0880: 20 64 69 66 66 65 72 65 6e 74 20 61 63 74 69 6f different actio
0890: 6e 73 0a 20 20 61 72 65 20 73 75 70 70 6f 72 74 ns. are support
08a0: 65 64 3a 20 22 52 45 53 54 52 49 43 54 22 20 28 ed: "RESTRICT" (
08b0: 74 68 65 20 64 65 66 61 75 6c 74 29 2c 20 22 43 the default), "C
08c0: 41 53 43 41 44 45 22 20 61 6e 64 20 22 53 45 54 ASCADE" and "SET
08d0: 20 4e 55 4c 4c 22 2e 20 53 51 4c 69 74 65 0a 20 NULL". SQLite.
08e0: 20 77 69 6c 6c 20 61 6c 73 6f 20 70 61 72 73 65 will also parse
08f0: 20 74 68 65 20 22 53 45 54 20 44 45 46 41 55 4c the "SET DEFAUL
0900: 54 22 20 61 63 74 69 6f 6e 2c 20 62 75 74 20 74 T" action, but t
0910: 68 69 73 20 69 73 20 6e 6f 74 20 69 6d 70 6c 65 his is not imple
0920: 6d 65 6e 74 65 64 0a 20 20 61 6e 64 20 22 52 45 mented. and "RE
0930: 53 54 52 49 43 54 22 20 69 73 20 75 73 65 64 20 STRICT" is used
0940: 69 6e 73 74 65 61 64 2e 0a 0a 20 20 20 20 52 45 instead... RE
0950: 53 54 52 49 43 54 3a 20 41 74 74 65 6d 70 74 69 STRICT: Attempti
0960: 6e 67 20 74 6f 20 75 70 64 61 74 65 20 6f 72 20 ng to update or
0970: 64 65 6c 65 74 65 20 61 20 72 6f 77 20 69 6e 20 delete a row in
0980: 74 68 65 20 70 61 72 65 6e 74 20 74 61 62 6c 65 the parent table
0990: 20 73 6f 0a 20 20 20 20 20 20 20 20 20 20 20 20 so.
09a0: 20 20 74 68 61 74 20 74 68 65 20 63 6f 6e 73 74 that the const
09b0: 72 61 69 6e 74 20 62 65 63 6f 6d 65 73 20 75 6e raint becomes un
09c0: 74 72 75 65 20 66 6f 72 20 6f 6e 65 20 6f 72 20 true for one or
09d0: 6d 6f 72 65 20 72 6f 77 73 20 69 6e 0a 20 20 20 more rows in.
09e0: 20 20 20 20 20 20 20 20 20 20 20 74 68 65 20 63 the c
09f0: 68 69 6c 64 20 74 61 62 6c 65 20 69 73 20 6e 6f hild table is no
0a00: 74 20 61 6c 6c 6f 77 65 64 2e 20 41 6e 20 65 78 t allowed. An ex
0a10: 63 65 70 74 69 6f 6e 20 69 73 20 74 68 72 6f 77 ception is throw
0a20: 6e 2e 0a 0a 20 20 20 20 43 41 53 43 41 44 45 3a n... CASCADE:
0a30: 20 20 49 6e 73 74 65 61 64 20 6f 66 20 74 68 72 Instead of thr
0a40: 6f 77 69 6e 67 20 61 6e 20 65 78 63 65 70 74 69 owing an excepti
0a50: 6f 6e 2c 20 61 6c 6c 20 63 6f 72 72 65 73 70 6f on, all correspo
0a60: 6e 64 69 6e 67 20 63 68 69 6c 64 20 74 61 62 6c nding child tabl
0a70: 65 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 e.
0a80: 72 6f 77 73 20 61 72 65 20 65 69 74 68 65 72 20 rows are either
0a90: 64 65 6c 65 74 65 64 20 28 69 66 20 74 68 65 20 deleted (if the
0aa0: 70 61 72 65 6e 74 20 72 6f 77 20 69 73 20 62 65 parent row is be
0ab0: 69 6e 67 20 64 65 6c 65 74 65 64 29 0a 20 20 20 ing deleted).
0ac0: 20 20 20 20 20 20 20 20 20 20 20 6f 72 20 75 70 or up
0ad0: 64 61 74 65 64 20 74 6f 20 6d 61 74 63 68 20 74 dated to match t
0ae0: 68 65 20 6e 65 77 20 70 61 72 65 6e 74 20 6b 65 he new parent ke
0af0: 79 20 76 61 6c 75 65 73 20 28 69 66 20 74 68 65 y values (if the
0b00: 20 70 61 72 65 6e 74 20 0a 20 20 20 20 20 20 20 parent .
0b10: 20 20 20 20 20 20 20 72 6f 77 20 69 73 20 62 65 row is be
0b20: 69 6e 67 20 75 70 64 61 74 65 64 29 2e 0a 0a 20 ing updated)...
0b30: 20 20 20 53 45 54 20 4e 55 4c 4c 3a 20 49 6e 73 SET NULL: Ins
0b40: 74 65 61 64 20 6f 66 20 74 68 72 6f 77 69 6e 67 tead of throwing
0b50: 20 61 6e 20 65 78 63 65 70 74 69 6f 6e 2c 20 74 an exception, t
0b60: 68 65 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 66 he foreign key f
0b70: 69 65 6c 64 73 20 6f 66 0a 20 20 20 20 20 20 20 ields of.
0b80: 20 20 20 20 20 20 20 61 6c 6c 20 63 6f 72 72 65 all corre
0b90: 73 70 6f 6e 64 69 6e 67 20 63 68 69 6c 64 20 74 sponding child t
0ba0: 61 62 6c 65 20 72 6f 77 73 20 61 72 65 20 73 65 able rows are se
0bb0: 74 20 74 6f 20 4e 55 4c 4c 2e 0a 0a 4c 49 4d 49 t to NULL...LIMI
0bc0: 54 41 54 49 4f 4e 53 0a 0a 20 20 41 70 61 72 74 TATIONS.. Apart
0bd0: 20 66 72 6f 6d 20 74 68 6f 73 65 20 6c 69 6d 69 from those limi
0be0: 74 69 61 74 69 6f 6e 73 20 64 65 73 63 72 69 62 tiations describ
0bf0: 65 64 20 61 62 6f 76 65 3a 0a 0a 20 20 20 20 2a ed above:.. *
0c00: 20 49 6d 70 6c 69 63 69 74 20 6d 61 70 70 69 6e Implicit mappin
0c10: 67 20 74 6f 20 63 6f 6d 70 6f 73 69 74 65 20 70 g to composite p
0c20: 72 69 6d 61 72 79 20 6b 65 79 73 20 69 73 20 6e rimary keys is n
0c30: 6f 74 20 73 75 70 70 6f 72 74 65 64 2e 20 49 66 ot supported. If
0c40: 0a 20 20 20 20 20 20 61 20 70 61 72 65 6e 74 20 . a parent
0c50: 74 61 62 6c 65 20 68 61 73 20 61 20 63 6f 6d 70 table has a comp
0c60: 6f 73 69 74 65 20 70 72 69 6d 61 72 79 20 6b 65 osite primary ke
0c70: 79 2c 20 74 68 65 6e 20 61 6e 79 20 63 68 69 6c y, then any chil
0c80: 64 20 74 61 62 6c 65 0a 20 20 20 20 20 20 74 68 d table. th
0c90: 61 74 20 72 65 66 65 72 73 20 74 6f 20 69 74 20 at refers to it
0ca0: 6d 75 73 74 20 65 78 70 6c 69 63 69 74 6c 79 20 must explicitly
0cb0: 6d 61 70 20 65 61 63 68 20 63 6f 6c 75 6d 6e 2e map each column.
0cc0: 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 67 69 For example, gi
0cd0: 76 65 6e 0a 20 20 20 20 20 20 74 68 65 20 66 6f ven. the fo
0ce0: 6c 6c 6f 77 69 6e 67 20 64 65 66 69 6e 69 74 69 llowing definiti
0cf0: 6f 6e 20 6f 66 20 74 61 62 6c 65 20 22 70 61 72 on of table "par
0d00: 65 6e 74 22 3a 0a 0a 20 20 20 20 20 20 20 20 43 ent":.. C
0d10: 52 45 41 54 45 20 54 41 42 4c 45 20 70 61 72 65 REATE TABLE pare
0d20: 6e 74 28 61 2c 20 62 2c 20 63 2c 20 50 52 49 4d nt(a, b, c, PRIM
0d30: 41 52 59 20 4b 45 59 28 61 2c 20 62 29 29 3b 0a ARY KEY(a, b));.
0d40: 0a 20 20 20 20 20 20 6f 6e 6c 79 20 74 68 65 20 . only the
0d50: 66 69 72 73 74 20 6f 66 20 74 68 65 20 66 6f 6c first of the fol
0d60: 6c 6f 77 69 6e 67 20 74 77 6f 20 64 65 66 69 6e lowing two defin
0d70: 69 74 69 6f 6e 73 20 6f 66 20 74 61 62 6c 65 20 itions of table
0d80: 22 63 68 69 6c 64 22 0a 20 20 20 20 20 20 69 73 "child". is
0d90: 20 73 75 70 70 6f 72 74 65 64 3a 0a 0a 20 20 20 supported:..
0da0: 20 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c CREATE TABL
0db0: 45 20 63 68 69 6c 64 28 64 2c 20 65 2c 20 66 2c E child(d, e, f,
0dc0: 20 46 4f 52 45 49 47 4e 20 4b 45 59 28 64 2c 20 FOREIGN KEY(d,
0dd0: 65 29 20 52 45 46 45 52 45 4e 43 45 53 20 70 61 e) REFERENCES pa
0de0: 72 65 6e 74 28 61 2c 20 62 29 29 3b 0a 20 20 20 rent(a, b));.
0df0: 20 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c CREATE TABL
0e00: 45 20 63 68 69 6c 64 28 64 2c 20 65 2c 20 66 2c E child(d, e, f,
0e10: 20 46 4f 52 45 49 47 4e 20 4b 45 59 28 64 2c 20 FOREIGN KEY(d,
0e20: 65 29 20 52 45 46 45 52 45 4e 43 45 53 20 70 61 e) REFERENCES pa
0e30: 72 65 6e 74 29 3b 0a 0a 20 20 20 20 20 20 41 6e rent);.. An
0e40: 20 69 6d 70 6c 69 63 69 74 20 72 65 66 65 72 65 implicit refere
0e50: 6e 63 65 20 74 6f 20 61 20 63 6f 6d 70 6f 73 69 nce to a composi
0e60: 74 65 20 70 72 69 6d 61 72 79 20 6b 65 79 20 69 te primary key i
0e70: 73 20 64 65 74 65 63 74 65 64 20 61 73 20 61 6e s detected as an
0e80: 0a 20 20 20 20 20 20 65 72 72 6f 72 20 77 68 65 . error whe
0e90: 6e 20 74 68 65 20 70 72 6f 67 72 61 6d 20 69 73 n the program is
0ea0: 20 72 75 6e 20 28 73 65 65 20 62 65 6c 6f 77 29 run (see below)
0eb0: 2e 0a 0a 20 20 20 20 2a 20 53 51 4c 69 74 65 20 ... * SQLite
0ec0: 64 6f 65 73 20 6e 6f 74 20 73 75 70 70 6f 72 74 does not support
0ed0: 20 72 65 63 75 72 73 69 76 65 20 74 72 69 67 67 recursive trigg
0ee0: 65 72 73 2c 20 61 6e 64 20 74 68 65 72 65 66 6f ers, and therefo
0ef0: 72 65 20 74 68 69 73 20 70 72 6f 67 72 61 6d 0a re this program.
0f00: 20 20 20 20 20 20 64 6f 65 73 20 6e 6f 74 20 73 does not s
0f10: 75 70 70 6f 72 74 20 72 65 63 75 72 73 69 76 65 upport recursive
0f20: 20 43 41 53 43 41 44 45 20 6f 72 20 53 45 54 20 CASCADE or SET
0f30: 4e 55 4c 4c 20 66 6f 72 65 69 67 6e 20 6b 65 79 NULL foreign key
0f40: 20 0a 20 20 20 20 20 20 72 65 6c 61 74 69 6f 6e . relation
0f50: 73 68 69 70 73 2e 20 49 66 20 74 68 65 20 70 61 ships. If the pa
0f60: 72 65 6e 74 20 61 6e 64 20 74 68 65 20 63 68 69 rent and the chi
0f70: 6c 64 20 74 61 62 6c 65 73 20 6f 66 20 61 20 43 ld tables of a C
0f80: 41 53 43 41 44 45 20 6f 72 0a 20 20 20 20 20 20 ASCADE or.
0f90: 53 45 54 20 4e 55 4c 4c 20 66 6f 72 65 69 67 6e SET NULL foreign
0fa0: 20 6b 65 79 20 61 72 65 20 74 68 65 20 73 61 6d key are the sam
0fb0: 65 20 74 61 62 6c 65 2c 20 74 68 65 20 67 65 6e e table, the gen
0fc0: 65 72 61 74 65 64 20 74 72 69 67 67 65 72 73 20 erated triggers
0fd0: 77 69 6c 6c 0a 20 20 20 20 20 20 6d 61 6c 66 75 will. malfu
0fe0: 6e 63 74 69 6f 6e 2e 20 54 68 69 73 20 69 73 20 nction. This is
0ff0: 61 6c 73 6f 20 74 72 75 65 20 69 66 20 74 68 65 also true if the
1000: 20 72 65 63 75 72 73 69 76 65 20 66 6f 72 65 69 recursive forei
1010: 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e gn key constrain
1020: 74 0a 20 20 20 20 20 20 69 73 20 69 6e 64 69 72 t. is indir
1030: 65 63 74 20 28 66 6f 72 20 65 78 61 6d 70 6c 65 ect (for example
1040: 20 69 66 20 74 61 62 6c 65 20 41 20 72 65 66 65 if table A refe
1050: 72 65 6e 63 65 73 20 74 61 62 6c 65 20 42 20 77 rences table B w
1060: 68 69 63 68 20 72 65 66 65 72 65 6e 63 65 73 0a hich references.
1070: 20 20 20 20 20 20 74 61 62 6c 65 20 41 20 77 69 table A wi
1080: 74 68 20 61 20 43 41 53 43 41 44 45 20 6f 72 20 th a CASCADE or
1090: 53 45 54 20 4e 55 4c 4c 20 66 6f 72 65 69 67 6e SET NULL foreign
10a0: 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 29 key constraint)
10b0: 2e 0a 0a 20 20 20 20 20 20 52 65 63 75 72 73 69 ... Recursi
10c0: 76 65 20 43 41 53 43 41 44 45 20 6f 72 20 53 45 ve CASCADE or SE
10d0: 54 20 4e 55 4c 4c 20 66 6f 72 65 69 67 6e 20 6b T NULL foreign k
10e0: 65 79 20 72 65 6c 61 74 69 6f 6e 73 68 69 70 73 ey relationships
10f0: 20 61 72 65 20 2a 6e 6f 74 2a 0a 20 20 20 20 20 are *not*.
1100: 20 64 65 74 65 63 74 65 64 20 61 73 20 65 72 72 detected as err
1110: 6f 72 73 20 77 68 65 6e 20 74 68 65 20 70 72 6f ors when the pro
1120: 67 72 61 6d 20 69 73 20 72 75 6e 2e 20 42 75 79 gram is run. Buy
1130: 65 72 20 62 65 77 61 72 65 2e 0a 0a 55 53 41 47 er beware...USAG
1140: 45 0a 0a 20 20 54 68 65 20 66 75 6e 63 74 69 6f E.. The functio
1150: 6e 61 6c 69 74 79 20 69 73 20 61 63 63 65 73 73 nality is access
1160: 65 64 20 74 68 72 6f 75 67 68 20 61 6e 20 73 71 ed through an sq
1170: 6c 69 74 65 33 20 73 68 65 6c 6c 20 74 6f 6f 6c lite3 shell tool
1180: 20 22 64 6f 74 2d 63 6f 6d 6d 61 6e 64 22 3a 0a "dot-command":.
1190: 0a 20 20 20 20 2e 67 65 6e 66 6b 65 79 20 3f 2d . .genfkey ?-
11a0: 2d 6e 6f 2d 64 72 6f 70 3f 20 3f 2d 2d 69 67 6e -no-drop? ?--ign
11b0: 6f 72 65 2d 65 72 72 6f 72 73 3f 20 3f 2d 2d 65 ore-errors? ?--e
11c0: 78 65 63 3f 0a 0a 20 20 57 68 65 6e 20 74 68 69 xec?.. When thi
11d0: 73 20 63 6f 6d 6d 61 6e 64 20 69 73 20 72 75 6e s command is run
11e0: 2c 20 69 74 20 66 69 72 73 74 20 63 68 65 63 6b , it first check
11f0: 73 20 74 68 65 20 73 63 68 65 6d 61 20 6f 66 20 s the schema of
1200: 74 68 65 20 6f 70 65 6e 20 53 51 4c 69 74 65 0a the open SQLite.
1210: 20 20 64 61 74 61 62 61 73 65 20 66 6f 72 20 66 database for f
1220: 6f 72 65 69 67 6e 20 6b 65 79 20 72 65 6c 61 74 oreign key relat
1230: 65 64 20 65 72 72 6f 72 73 20 6f 72 20 69 6e 63 ed errors or inc
1240: 6f 6e 73 69 73 74 65 6e 63 69 65 73 2e 20 46 6f onsistencies. Fo
1250: 72 20 65 78 61 6d 70 6c 65 2c 0a 20 20 61 20 66 r example,. a f
1260: 6f 72 65 69 67 6e 20 6b 65 79 20 74 68 61 74 20 oreign key that
1270: 72 65 66 65 72 73 20 74 6f 20 61 20 70 61 72 65 refers to a pare
1280: 6e 74 20 74 61 62 6c 65 20 74 68 61 74 20 64 6f nt table that do
1290: 65 73 20 6e 6f 74 20 65 78 69 73 74 2c 20 6f 72 es not exist, or
12a0: 0a 20 20 61 20 66 6f 72 65 69 67 6e 20 6b 65 79 . a foreign key
12b0: 20 74 68 61 74 20 72 65 66 65 72 73 20 74 6f 20 that refers to
12c0: 63 6f 6c 75 6d 6e 73 20 69 6e 20 61 20 70 61 72 columns in a par
12d0: 65 6e 74 20 74 61 62 6c 65 20 74 68 61 74 20 61 ent table that a
12e0: 72 65 20 6e 6f 74 0a 20 20 67 75 61 72 61 6e 74 re not. guarant
12f0: 65 65 64 20 74 6f 20 62 65 20 75 6e 69 71 75 65 eed to be unique
1300: 2e 20 49 66 20 73 75 63 68 20 65 72 72 6f 72 73 . If such errors
1310: 20 61 72 65 20 66 6f 75 6e 64 20 61 6e 64 20 74 are found and t
1320: 68 65 20 2d 2d 69 67 6e 6f 72 65 2d 65 72 72 6f he --ignore-erro
1330: 72 73 0a 20 20 6f 70 74 69 6f 6e 20 77 61 73 20 rs. option was
1340: 6e 6f 74 20 70 72 65 73 65 6e 74 2c 20 61 20 6d not present, a m
1350: 65 73 73 61 67 65 20 66 6f 72 20 65 61 63 68 20 essage for each
1360: 6f 6e 65 20 69 73 20 70 72 69 6e 74 65 64 20 74 one is printed t
1370: 6f 20 73 74 64 65 72 72 20 61 6e 64 0a 20 20 6e o stderr and. n
1380: 6f 20 66 75 72 74 68 65 72 20 70 72 6f 63 65 73 o further proces
1390: 73 69 6e 67 20 74 61 6b 65 73 20 70 6c 61 63 65 sing takes place
13a0: 2e 0a 0a 20 20 49 66 20 65 72 72 6f 72 73 20 61 ... If errors a
13b0: 72 65 20 66 6f 75 6e 64 20 61 6e 64 20 74 68 65 re found and the
13c0: 20 2d 2d 69 67 6e 6f 72 65 2d 65 72 72 6f 72 73 --ignore-errors
13d0: 20 6f 70 74 69 6f 6e 20 69 73 20 70 61 73 73 65 option is passe
13e0: 64 2c 20 74 68 65 6e 0a 20 20 6e 6f 20 65 72 72 d, then. no err
13f0: 6f 72 20 6d 65 73 73 61 67 65 73 20 61 72 65 20 or messages are
1400: 70 72 69 6e 74 65 64 2e 20 4e 6f 20 22 43 52 45 printed. No "CRE
1410: 41 54 45 20 54 52 49 47 47 45 52 22 20 73 74 61 ATE TRIGGER" sta
1420: 74 65 6d 65 6e 74 73 20 61 72 65 20 67 65 6e 65 tements are gene
1430: 72 61 74 65 64 0a 20 20 66 6f 72 20 66 6f 72 69 rated. for fori
1440: 65 67 6e 2d 6b 65 79 20 64 65 66 69 6e 69 74 69 egn-key definiti
1450: 6f 6e 73 20 74 68 61 74 20 63 6f 6e 74 61 69 6e ons that contain
1460: 65 64 20 65 72 72 6f 72 73 2c 20 74 68 65 79 20 ed errors, they
1470: 61 72 65 20 73 69 6c 65 6e 74 6c 79 0a 20 20 69 are silently. i
1480: 67 6e 6f 72 65 64 20 62 79 20 73 75 62 73 65 71 gnored by subseq
1490: 75 65 6e 74 20 70 72 6f 63 65 73 73 69 6e 67 2e uent processing.
14a0: 0a 0a 20 20 41 6c 6c 20 74 72 69 67 67 65 72 73 .. All triggers
14b0: 20 67 65 6e 65 72 61 74 65 64 20 62 79 20 74 68 generated by th
14c0: 69 73 20 63 6f 6d 6d 61 6e 64 20 68 61 76 65 20 is command have
14d0: 6e 61 6d 65 73 20 74 68 61 74 20 6d 61 74 63 68 names that match
14e0: 20 74 68 65 20 70 61 74 74 65 72 6e 0a 20 20 22 the pattern. "
14f0: 67 65 6e 66 6b 65 79 2a 22 2e 20 55 6e 6c 65 73 genfkey*". Unles
1500: 73 20 74 68 65 20 2d 2d 6e 6f 2d 64 72 6f 70 20 s the --no-drop
1510: 6f 70 74 69 6f 6e 20 69 73 20 73 70 65 63 69 66 option is specif
1520: 69 65 64 2c 20 74 68 65 6e 20 74 68 65 20 70 72 ied, then the pr
1530: 6f 67 72 61 6d 0a 20 20 61 6c 73 6f 20 67 65 6e ogram. also gen
1540: 65 72 61 74 65 73 20 61 20 22 44 52 4f 50 20 54 erates a "DROP T
1550: 52 49 47 47 45 52 22 20 73 74 61 74 65 6d 65 6e RIGGER" statemen
1560: 74 20 66 6f 72 20 65 61 63 68 20 74 72 69 67 67 t for each trigg
1570: 65 72 20 74 68 61 74 20 65 78 69 73 74 73 0a 20 er that exists.
1580: 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 in the database
1590: 20 77 69 74 68 20 61 20 6e 61 6d 65 20 74 68 61 with a name tha
15a0: 74 20 6d 61 74 63 68 65 73 20 74 68 69 73 20 70 t matches this p
15b0: 61 74 74 65 72 6e 2e 20 54 68 69 73 20 61 6c 6c attern. This all
15c0: 6f 77 73 20 74 68 65 0a 20 20 70 72 6f 67 72 61 ows the. progra
15d0: 6d 20 74 6f 20 62 65 20 75 73 65 64 20 74 6f 20 m to be used to
15e0: 75 70 67 72 61 64 65 20 61 20 64 61 74 61 62 61 upgrade a databa
15f0: 73 65 20 73 63 68 65 6d 61 20 66 6f 72 20 77 68 se schema for wh
1600: 69 63 68 20 66 6f 72 65 69 67 6e 20 6b 65 79 0a ich foreign key.
1610: 20 20 74 72 69 67 67 65 72 73 20 68 61 76 65 20 triggers have
1620: 61 6c 72 65 61 64 79 20 62 65 65 6e 20 69 6e 73 already been ins
1630: 74 61 6c 6c 65 64 20 28 69 2e 65 2e 20 61 66 74 talled (i.e. aft
1640: 65 72 20 6e 65 77 20 74 61 62 6c 65 73 20 61 72 er new tables ar
1650: 65 20 63 72 65 61 74 65 64 0a 20 20 6f 72 20 65 e created. or e
1660: 78 69 73 74 69 6e 67 20 74 61 62 6c 65 73 20 64 xisting tables d
1670: 72 6f 70 70 65 64 29 2e 0a 0a 20 20 46 69 6e 61 ropped)... Fina
1680: 6c 6c 79 2c 20 61 20 73 65 72 69 65 73 20 6f 66 lly, a series of
1690: 20 53 51 4c 20 74 72 69 67 67 65 72 20 64 65 66 SQL trigger def
16a0: 69 6e 69 74 69 6f 6e 73 20 28 43 52 45 41 54 45 initions (CREATE
16b0: 20 54 52 49 47 47 45 52 20 73 74 61 74 65 6d 65 TRIGGER stateme
16c0: 6e 74 73 29 20 0a 20 20 74 68 61 74 20 69 6d 70 nts) . that imp
16d0: 6c 65 6d 65 6e 74 20 74 68 65 20 66 6f 72 65 69 lement the forei
16e0: 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e gn key constrain
16f0: 74 73 20 66 6f 75 6e 64 20 69 6e 20 74 68 65 20 ts found in the
1700: 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 20 database schema
1710: 61 72 65 0a 20 20 67 65 6e 65 72 61 74 65 64 2e are. generated.
1720: 0a 0a 20 20 49 66 20 74 68 65 20 2d 2d 65 78 65 .. If the --exe
1730: 63 20 6f 70 74 69 6f 6e 20 77 61 73 20 70 61 73 c option was pas
1740: 73 65 64 2c 20 74 68 65 6e 20 61 6c 6c 20 67 65 sed, then all ge
1750: 6e 65 72 61 74 65 64 20 53 51 4c 20 69 73 20 69 nerated SQL is i
1760: 6d 6d 65 64 69 61 74 65 6c 79 0a 20 20 65 78 65 mmediately. exe
1770: 63 75 74 65 64 20 6f 6e 20 74 68 65 20 64 61 74 cuted on the dat
1780: 61 62 61 73 65 2e 20 4f 74 68 65 72 77 69 73 65 abase. Otherwise
1790: 2c 20 74 68 65 20 67 65 6e 65 72 61 74 65 64 20 , the generated
17a0: 53 51 4c 20 73 74 72 69 6e 67 73 20 61 72 65 20 SQL strings are
17b0: 6f 75 74 70 75 74 0a 20 20 69 6e 20 74 68 65 20 output. in the
17c0: 73 61 6d 65 20 77 61 79 20 61 73 20 74 68 65 20 same way as the
17d0: 72 65 73 75 6c 74 73 20 6f 66 20 53 45 4c 45 43 results of SELEC
17e0: 54 20 71 75 65 72 69 65 73 20 61 72 65 2e 20 4e T queries are. N
17f0: 6f 72 6d 61 6c 6c 79 2c 20 74 68 69 73 20 6d 65 ormally, this me
1800: 61 6e 73 0a 20 20 74 68 65 79 20 77 69 6c 6c 20 ans. they will
1810: 62 65 20 70 72 69 6e 74 65 64 20 74 6f 20 73 74 be printed to st
1820: 64 6f 75 74 2c 20 62 75 74 20 74 68 69 73 20 63 dout, but this c
1830: 61 6e 20 62 65 20 63 6f 6e 66 69 67 75 72 65 64 an be configured
1840: 20 75 73 69 6e 67 20 6f 74 68 65 72 0a 20 20 64 using other. d
1850: 6f 74 2d 63 6f 6d 6d 61 6e 64 73 20 28 69 2e 65 ot-commands (i.e
1860: 2e 20 22 2e 6f 75 74 70 75 74 22 29 2e 0a 0a 20 . ".output")...
1870: 20 54 68 65 20 73 69 6d 70 6c 65 73 74 20 77 61 The simplest wa
1880: 79 20 74 6f 20 61 63 74 69 76 61 74 65 20 74 68 y to activate th
1890: 65 20 66 6f 72 69 65 67 6e 20 6b 65 79 20 64 65 e foriegn key de
18a0: 66 69 6e 69 74 69 6f 6e 73 20 69 6e 20 61 20 64 finitions in a d
18b0: 61 74 61 62 61 73 65 0a 20 20 69 73 20 73 69 6d atabase. is sim
18c0: 70 6c 79 20 74 6f 20 6f 70 65 6e 20 69 74 20 75 ply to open it u
18d0: 73 69 6e 67 20 74 68 65 20 73 68 65 6c 6c 20 74 sing the shell t
18e0: 6f 6f 6c 20 61 6e 64 20 65 6e 74 65 72 20 74 68 ool and enter th
18f0: 65 20 63 6f 6d 6d 61 6e 64 20 0a 20 20 22 2e 67 e command . ".g
1900: 65 6e 66 6b 65 79 20 2d 2d 65 78 65 63 22 3a 0a enfkey --exec":.
1910: 0a 20 20 20 20 73 71 6c 69 74 65 3e 20 2e 67 65 . sqlite> .ge
1920: 6e 66 6b 65 79 20 2d 2d 65 78 65 63 0a 0a nfkey --exec..