1
0

e_fkey.test 95 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015
  1. # 2009 October 7
  2. #
  3. # The author disclaims copyright to this source code. In place of
  4. # a legal notice, here is a blessing:
  5. #
  6. # May you do good and not evil.
  7. # May you find forgiveness for yourself and forgive others.
  8. # May you share freely, never taking more than you give.
  9. #
  10. #***********************************************************************
  11. #
  12. # This file implements tests to verify the "testable statements" in the
  13. # foreignkeys.in document.
  14. #
  15. # The tests in this file are arranged to mirror the structure of
  16. # foreignkey.in, with one exception: The statements in section 2, which
  17. # deals with enabling/disabling foreign key support, is tested first,
  18. # before section 1. This is because some statements in section 2 deal
  19. # with builds that do not include complete foreign key support (because
  20. # either SQLITE_OMIT_TRIGGER or SQLITE_OMIT_FOREIGN_KEY was defined
  21. # at build time).
  22. #
  23. set testdir [file dirname $argv0]
  24. source $testdir/tester.tcl
  25. proc eqp {sql {db db}} { uplevel execsql [list "EXPLAIN QUERY PLAN $sql"] $db }
  26. ###########################################################################
  27. ### SECTION 2: Enabling Foreign Key Support
  28. ###########################################################################
  29. #-------------------------------------------------------------------------
  30. # EVIDENCE-OF: R-33710-56344 In order to use foreign key constraints in
  31. # SQLite, the library must be compiled with neither
  32. # SQLITE_OMIT_FOREIGN_KEY or SQLITE_OMIT_TRIGGER defined.
  33. #
  34. ifcapable trigger&&foreignkey {
  35. do_test e_fkey-1 {
  36. execsql {
  37. PRAGMA foreign_keys = ON;
  38. CREATE TABLE p(i PRIMARY KEY);
  39. CREATE TABLE c(j REFERENCES p ON UPDATE CASCADE);
  40. INSERT INTO p VALUES('hello');
  41. INSERT INTO c VALUES('hello');
  42. UPDATE p SET i = 'world';
  43. SELECT * FROM c;
  44. }
  45. } {world}
  46. }
  47. #-------------------------------------------------------------------------
  48. # Test the effects of defining OMIT_TRIGGER but not OMIT_FOREIGN_KEY.
  49. #
  50. # EVIDENCE-OF: R-44697-61543 If SQLITE_OMIT_TRIGGER is defined but
  51. # SQLITE_OMIT_FOREIGN_KEY is not, then SQLite behaves as it did prior to
  52. # version 3.6.19 - foreign key definitions are parsed and may be queried
  53. # using PRAGMA foreign_key_list, but foreign key constraints are not
  54. # enforced.
  55. #
  56. # Specifically, test that "PRAGMA foreign_keys" is a no-op in this case.
  57. # When using the pragma to query the current setting, 0 rows are returned.
  58. #
  59. # EVIDENCE-OF: R-22567-44039 The PRAGMA foreign_keys command is a no-op
  60. # in this configuration.
  61. #
  62. # EVIDENCE-OF: R-41784-13339 Tip: If the command "PRAGMA foreign_keys"
  63. # returns no data instead of a single row containing "0" or "1", then
  64. # the version of SQLite you are using does not support foreign keys
  65. # (either because it is older than 3.6.19 or because it was compiled
  66. # with SQLITE_OMIT_FOREIGN_KEY or SQLITE_OMIT_TRIGGER defined).
  67. #
  68. reset_db
  69. ifcapable !trigger&&foreignkey {
  70. do_test e_fkey-2.1 {
  71. execsql {
  72. PRAGMA foreign_keys = ON;
  73. CREATE TABLE p(i PRIMARY KEY);
  74. CREATE TABLE c(j REFERENCES p ON UPDATE CASCADE);
  75. INSERT INTO p VALUES('hello');
  76. INSERT INTO c VALUES('hello');
  77. UPDATE p SET i = 'world';
  78. SELECT * FROM c;
  79. }
  80. } {hello}
  81. do_test e_fkey-2.2 {
  82. execsql { PRAGMA foreign_key_list(c) }
  83. } {0 0 p j {} CASCADE {NO ACTION} NONE}
  84. do_test e_fkey-2.3 {
  85. execsql { PRAGMA foreign_keys }
  86. } {}
  87. }
  88. #-------------------------------------------------------------------------
  89. # Test the effects of defining OMIT_FOREIGN_KEY.
  90. #
  91. # EVIDENCE-OF: R-58428-36660 If OMIT_FOREIGN_KEY is defined, then
  92. # foreign key definitions cannot even be parsed (attempting to specify a
  93. # foreign key definition is a syntax error).
  94. #
  95. # Specifically, test that foreign key constraints cannot even be parsed
  96. # in such a build.
  97. #
  98. reset_db
  99. ifcapable !foreignkey {
  100. do_test e_fkey-3.1 {
  101. execsql { CREATE TABLE p(i PRIMARY KEY) }
  102. catchsql { CREATE TABLE c(j REFERENCES p ON UPDATE CASCADE) }
  103. } {1 {near "ON": syntax error}}
  104. do_test e_fkey-3.2 {
  105. # This is allowed, as in this build, "REFERENCES" is not a keyword.
  106. # The declared datatype of column j is "REFERENCES p".
  107. execsql { CREATE TABLE c(j REFERENCES p) }
  108. } {}
  109. do_test e_fkey-3.3 {
  110. execsql { PRAGMA table_info(c) }
  111. } {0 j {REFERENCES p} 0 {} 0}
  112. do_test e_fkey-3.4 {
  113. execsql { PRAGMA foreign_key_list(c) }
  114. } {}
  115. do_test e_fkey-3.5 {
  116. execsql { PRAGMA foreign_keys }
  117. } {}
  118. }
  119. ifcapable !foreignkey||!trigger { finish_test ; return }
  120. reset_db
  121. #-------------------------------------------------------------------------
  122. # EVIDENCE-OF: R-07280-60510 Assuming the library is compiled with
  123. # foreign key constraints enabled, it must still be enabled by the
  124. # application at runtime, using the PRAGMA foreign_keys command.
  125. #
  126. # This also tests that foreign key constraints are disabled by default.
  127. #
  128. # EVIDENCE-OF: R-59578-04990 Foreign key constraints are disabled by
  129. # default (for backwards compatibility), so must be enabled separately
  130. # for each database connection separately.
  131. #
  132. drop_all_tables
  133. do_test e_fkey-4.1 {
  134. execsql {
  135. CREATE TABLE p(i PRIMARY KEY);
  136. CREATE TABLE c(j REFERENCES p ON UPDATE CASCADE);
  137. INSERT INTO p VALUES('hello');
  138. INSERT INTO c VALUES('hello');
  139. UPDATE p SET i = 'world';
  140. SELECT * FROM c;
  141. }
  142. } {hello}
  143. do_test e_fkey-4.2 {
  144. execsql {
  145. DELETE FROM c;
  146. DELETE FROM p;
  147. PRAGMA foreign_keys = ON;
  148. INSERT INTO p VALUES('hello');
  149. INSERT INTO c VALUES('hello');
  150. UPDATE p SET i = 'world';
  151. SELECT * FROM c;
  152. }
  153. } {world}
  154. #-------------------------------------------------------------------------
  155. # EVIDENCE-OF: R-15278-54456 The application can can also use a PRAGMA
  156. # foreign_keys statement to determine if foreign keys are currently
  157. # enabled.
  158. #
  159. # This also tests the example code in section 2 of foreignkeys.in.
  160. #
  161. # EVIDENCE-OF: R-11255-19907
  162. #
  163. reset_db
  164. do_test e_fkey-5.1 {
  165. execsql { PRAGMA foreign_keys }
  166. } {0}
  167. do_test e_fkey-5.2 {
  168. execsql {
  169. PRAGMA foreign_keys = ON;
  170. PRAGMA foreign_keys;
  171. }
  172. } {1}
  173. do_test e_fkey-5.3 {
  174. execsql {
  175. PRAGMA foreign_keys = OFF;
  176. PRAGMA foreign_keys;
  177. }
  178. } {0}
  179. #-------------------------------------------------------------------------
  180. # Test that it is not possible to enable or disable foreign key support
  181. # while not in auto-commit mode.
  182. #
  183. # EVIDENCE-OF: R-46649-58537 It is not possible to enable or disable
  184. # foreign key constraints in the middle of a multi-statement transaction
  185. # (when SQLite is not in autocommit mode). Attempting to do so does not
  186. # return an error; it simply has no effect.
  187. #
  188. reset_db
  189. do_test e_fkey-6.1 {
  190. execsql {
  191. PRAGMA foreign_keys = ON;
  192. CREATE TABLE t1(a UNIQUE, b);
  193. CREATE TABLE t2(c, d REFERENCES t1(a));
  194. INSERT INTO t1 VALUES(1, 2);
  195. INSERT INTO t2 VALUES(2, 1);
  196. BEGIN;
  197. PRAGMA foreign_keys = OFF;
  198. }
  199. catchsql {
  200. DELETE FROM t1
  201. }
  202. } {1 {foreign key constraint failed}}
  203. do_test e_fkey-6.2 {
  204. execsql { PRAGMA foreign_keys }
  205. } {1}
  206. do_test e_fkey-6.3 {
  207. execsql {
  208. COMMIT;
  209. PRAGMA foreign_keys = OFF;
  210. BEGIN;
  211. PRAGMA foreign_keys = ON;
  212. DELETE FROM t1;
  213. PRAGMA foreign_keys;
  214. }
  215. } {0}
  216. do_test e_fkey-6.4 {
  217. execsql COMMIT
  218. } {}
  219. ###########################################################################
  220. ### SECTION 1: Introduction to Foreign Key Constraints
  221. ###########################################################################
  222. execsql "PRAGMA foreign_keys = ON"
  223. #-------------------------------------------------------------------------
  224. # Verify that the syntax in the first example in section 1 is valid.
  225. #
  226. # EVIDENCE-OF: R-04042-24825 To do so, a foreign key definition may be
  227. # added by modifying the declaration of the track table to the
  228. # following: CREATE TABLE track( trackid INTEGER, trackname TEXT,
  229. # trackartist INTEGER, FOREIGN KEY(trackartist) REFERENCES
  230. # artist(artistid) );
  231. #
  232. do_test e_fkey-7.1 {
  233. execsql {
  234. CREATE TABLE artist(
  235. artistid INTEGER PRIMARY KEY,
  236. artistname TEXT
  237. );
  238. CREATE TABLE track(
  239. trackid INTEGER,
  240. trackname TEXT,
  241. trackartist INTEGER,
  242. FOREIGN KEY(trackartist) REFERENCES artist(artistid)
  243. );
  244. }
  245. } {}
  246. #-------------------------------------------------------------------------
  247. # EVIDENCE-OF: R-61362-32087 Attempting to insert a row into the track
  248. # table that does not correspond to any row in the artist table will
  249. # fail,
  250. #
  251. do_test e_fkey-8.1 {
  252. catchsql { INSERT INTO track VALUES(1, 'track 1', 1) }
  253. } {1 {foreign key constraint failed}}
  254. do_test e_fkey-8.2 {
  255. execsql { INSERT INTO artist VALUES(2, 'artist 1') }
  256. catchsql { INSERT INTO track VALUES(1, 'track 1', 1) }
  257. } {1 {foreign key constraint failed}}
  258. do_test e_fkey-8.2 {
  259. execsql { INSERT INTO track VALUES(1, 'track 1', 2) }
  260. } {}
  261. #-------------------------------------------------------------------------
  262. # Attempting to delete a row from the 'artist' table while there are
  263. # dependent rows in the track table also fails.
  264. #
  265. # EVIDENCE-OF: R-24401-52400 as will attempting to delete a row from the
  266. # artist table when there exist dependent rows in the track table
  267. #
  268. do_test e_fkey-9.1 {
  269. catchsql { DELETE FROM artist WHERE artistid = 2 }
  270. } {1 {foreign key constraint failed}}
  271. do_test e_fkey-9.2 {
  272. execsql {
  273. DELETE FROM track WHERE trackartist = 2;
  274. DELETE FROM artist WHERE artistid = 2;
  275. }
  276. } {}
  277. #-------------------------------------------------------------------------
  278. # If the foreign key column (trackartist) in table 'track' is set to NULL,
  279. # there is no requirement for a matching row in the 'artist' table.
  280. #
  281. # EVIDENCE-OF: R-23980-48859 There is one exception: if the foreign key
  282. # column in the track table is NULL, then no corresponding entry in the
  283. # artist table is required.
  284. #
  285. do_test e_fkey-10.1 {
  286. execsql {
  287. INSERT INTO track VALUES(1, 'track 1', NULL);
  288. INSERT INTO track VALUES(2, 'track 2', NULL);
  289. }
  290. } {}
  291. do_test e_fkey-10.2 {
  292. execsql { SELECT * FROM artist }
  293. } {}
  294. do_test e_fkey-10.3 {
  295. # Setting the trackid to a non-NULL value fails, of course.
  296. catchsql { UPDATE track SET trackartist = 5 WHERE trackid = 1 }
  297. } {1 {foreign key constraint failed}}
  298. do_test e_fkey-10.4 {
  299. execsql {
  300. INSERT INTO artist VALUES(5, 'artist 5');
  301. UPDATE track SET trackartist = 5 WHERE trackid = 1;
  302. }
  303. catchsql { DELETE FROM artist WHERE artistid = 5}
  304. } {1 {foreign key constraint failed}}
  305. do_test e_fkey-10.5 {
  306. execsql {
  307. UPDATE track SET trackartist = NULL WHERE trackid = 1;
  308. DELETE FROM artist WHERE artistid = 5;
  309. }
  310. } {}
  311. #-------------------------------------------------------------------------
  312. # Test that the following is true fo all rows in the track table:
  313. #
  314. # trackartist IS NULL OR
  315. # EXISTS(SELECT 1 FROM artist WHERE artistid=trackartist)
  316. #
  317. # EVIDENCE-OF: R-52486-21352 Expressed in SQL, this means that for every
  318. # row in the track table, the following expression evaluates to true:
  319. # trackartist IS NULL OR EXISTS(SELECT 1 FROM artist WHERE
  320. # artistid=trackartist)
  321. # This procedure executes a test case to check that statement
  322. # R-52486-21352 is true after executing the SQL statement passed.
  323. # as the second argument.
  324. proc test_r52486_21352 {tn sql} {
  325. set res [catchsql $sql]
  326. set results {
  327. {0 {}}
  328. {1 {PRIMARY KEY must be unique}}
  329. {1 {foreign key constraint failed}}
  330. }
  331. if {[lsearch $results $res]<0} {
  332. error $res
  333. }
  334. do_test e_fkey-11.$tn {
  335. execsql {
  336. SELECT count(*) FROM track WHERE NOT (
  337. trackartist IS NULL OR
  338. EXISTS(SELECT 1 FROM artist WHERE artistid=trackartist)
  339. )
  340. }
  341. } {0}
  342. }
  343. # Execute a series of random INSERT, UPDATE and DELETE operations
  344. # (some of which may fail due to FK or PK constraint violations) on
  345. # the two tables in the example schema. Test that R-52486-21352
  346. # is true after executing each operation.
  347. #
  348. set Template {
  349. {INSERT INTO track VALUES($t, 'track $t', $a)}
  350. {DELETE FROM track WHERE trackid = $t}
  351. {UPDATE track SET trackartist = $a WHERE trackid = $t}
  352. {INSERT INTO artist VALUES($a, 'artist $a')}
  353. {DELETE FROM artist WHERE artistid = $a}
  354. {UPDATE artist SET artistid = $a2 WHERE artistid = $a}
  355. }
  356. for {set i 0} {$i < 500} {incr i} {
  357. set a [expr int(rand()*10)]
  358. set a2 [expr int(rand()*10)]
  359. set t [expr int(rand()*50)]
  360. set sql [subst [lindex $Template [expr int(rand()*6)]]]
  361. test_r52486_21352 $i $sql
  362. }
  363. #-------------------------------------------------------------------------
  364. # Check that a NOT NULL constraint can be added to the example schema
  365. # to prohibit NULL child keys from being inserted.
  366. #
  367. # EVIDENCE-OF: R-42412-59321 Tip: If the application requires a stricter
  368. # relationship between artist and track, where NULL values are not
  369. # permitted in the trackartist column, simply add the appropriate "NOT
  370. # NULL" constraint to the schema.
  371. #
  372. drop_all_tables
  373. do_test e_fkey-12.1 {
  374. execsql {
  375. CREATE TABLE artist(
  376. artistid INTEGER PRIMARY KEY,
  377. artistname TEXT
  378. );
  379. CREATE TABLE track(
  380. trackid INTEGER,
  381. trackname TEXT,
  382. trackartist INTEGER NOT NULL,
  383. FOREIGN KEY(trackartist) REFERENCES artist(artistid)
  384. );
  385. }
  386. } {}
  387. do_test e_fkey-12.2 {
  388. catchsql { INSERT INTO track VALUES(14, 'Mr. Bojangles', NULL) }
  389. } {1 {track.trackartist may not be NULL}}
  390. #-------------------------------------------------------------------------
  391. # EVIDENCE-OF: R-16127-35442
  392. #
  393. # Test an example from foreignkeys.html.
  394. #
  395. drop_all_tables
  396. do_test e_fkey-13.1 {
  397. execsql {
  398. CREATE TABLE artist(
  399. artistid INTEGER PRIMARY KEY,
  400. artistname TEXT
  401. );
  402. CREATE TABLE track(
  403. trackid INTEGER,
  404. trackname TEXT,
  405. trackartist INTEGER,
  406. FOREIGN KEY(trackartist) REFERENCES artist(artistid)
  407. );
  408. INSERT INTO artist VALUES(1, 'Dean Martin');
  409. INSERT INTO artist VALUES(2, 'Frank Sinatra');
  410. INSERT INTO track VALUES(11, 'That''s Amore', 1);
  411. INSERT INTO track VALUES(12, 'Christmas Blues', 1);
  412. INSERT INTO track VALUES(13, 'My Way', 2);
  413. }
  414. } {}
  415. do_test e_fkey-13.2 {
  416. catchsql { INSERT INTO track VALUES(14, 'Mr. Bojangles', 3) }
  417. } {1 {foreign key constraint failed}}
  418. do_test e_fkey-13.3 {
  419. execsql { INSERT INTO track VALUES(14, 'Mr. Bojangles', NULL) }
  420. } {}
  421. do_test e_fkey-13.4 {
  422. catchsql {
  423. UPDATE track SET trackartist = 3 WHERE trackname = 'Mr. Bojangles';
  424. }
  425. } {1 {foreign key constraint failed}}
  426. do_test e_fkey-13.5 {
  427. execsql {
  428. INSERT INTO artist VALUES(3, 'Sammy Davis Jr.');
  429. UPDATE track SET trackartist = 3 WHERE trackname = 'Mr. Bojangles';
  430. INSERT INTO track VALUES(15, 'Boogie Woogie', 3);
  431. }
  432. } {}
  433. #-------------------------------------------------------------------------
  434. # EVIDENCE-OF: R-15958-50233
  435. #
  436. # Test the second example from the first section of foreignkeys.html.
  437. #
  438. do_test e_fkey-14.1 {
  439. catchsql {
  440. DELETE FROM artist WHERE artistname = 'Frank Sinatra';
  441. }
  442. } {1 {foreign key constraint failed}}
  443. do_test e_fkey-14.2 {
  444. execsql {
  445. DELETE FROM track WHERE trackname = 'My Way';
  446. DELETE FROM artist WHERE artistname = 'Frank Sinatra';
  447. }
  448. } {}
  449. do_test e_fkey-14.3 {
  450. catchsql {
  451. UPDATE artist SET artistid=4 WHERE artistname = 'Dean Martin';
  452. }
  453. } {1 {foreign key constraint failed}}
  454. do_test e_fkey-14.4 {
  455. execsql {
  456. DELETE FROM track WHERE trackname IN('That''s Amore', 'Christmas Blues');
  457. UPDATE artist SET artistid=4 WHERE artistname = 'Dean Martin';
  458. }
  459. } {}
  460. #-------------------------------------------------------------------------
  461. # EVIDENCE-OF: R-56032-24923 The foreign key constraint is satisfied if
  462. # for each row in the child table either one or more of the child key
  463. # columns are NULL, or there exists a row in the parent table for which
  464. # each parent key column contains a value equal to the value in its
  465. # associated child key column.
  466. #
  467. # Test also that the usual comparison rules are used when testing if there
  468. # is a matching row in the parent table of a foreign key constraint.
  469. #
  470. # EVIDENCE-OF: R-57765-12380 In the above paragraph, the term "equal"
  471. # means equal when values are compared using the rules specified here.
  472. #
  473. drop_all_tables
  474. do_test e_fkey-15.1 {
  475. execsql {
  476. CREATE TABLE par(p PRIMARY KEY);
  477. CREATE TABLE chi(c REFERENCES par);
  478. INSERT INTO par VALUES(1);
  479. INSERT INTO par VALUES('1');
  480. INSERT INTO par VALUES(X'31');
  481. SELECT typeof(p) FROM par;
  482. }
  483. } {integer text blob}
  484. proc test_efkey_45 {tn isError sql} {
  485. do_test e_fkey-15.$tn.1 "
  486. catchsql {$sql}
  487. " [lindex {{0 {}} {1 {foreign key constraint failed}}} $isError]
  488. do_test e_fkey-15.$tn.2 {
  489. execsql {
  490. SELECT * FROM chi WHERE c IS NOT NULL AND c NOT IN (SELECT p FROM par)
  491. }
  492. } {}
  493. }
  494. test_efkey_45 1 0 "INSERT INTO chi VALUES(1)"
  495. test_efkey_45 2 1 "INSERT INTO chi VALUES('1.0')"
  496. test_efkey_45 3 0 "INSERT INTO chi VALUES('1')"
  497. test_efkey_45 4 1 "DELETE FROM par WHERE p = '1'"
  498. test_efkey_45 5 0 "DELETE FROM chi WHERE c = '1'"
  499. test_efkey_45 6 0 "DELETE FROM par WHERE p = '1'"
  500. test_efkey_45 7 1 "INSERT INTO chi VALUES('1')"
  501. test_efkey_45 8 0 "INSERT INTO chi VALUES(X'31')"
  502. test_efkey_45 9 1 "INSERT INTO chi VALUES(X'32')"
  503. #-------------------------------------------------------------------------
  504. # Specifically, test that when comparing child and parent key values the
  505. # default collation sequence of the parent key column is used.
  506. #
  507. # EVIDENCE-OF: R-15796-47513 When comparing text values, the collating
  508. # sequence associated with the parent key column is always used.
  509. #
  510. drop_all_tables
  511. do_test e_fkey-16.1 {
  512. execsql {
  513. CREATE TABLE t1(a COLLATE nocase PRIMARY KEY);
  514. CREATE TABLE t2(b REFERENCES t1);
  515. }
  516. } {}
  517. do_test e_fkey-16.2 {
  518. execsql {
  519. INSERT INTO t1 VALUES('oNe');
  520. INSERT INTO t2 VALUES('one');
  521. INSERT INTO t2 VALUES('ONE');
  522. UPDATE t2 SET b = 'OnE';
  523. UPDATE t1 SET a = 'ONE';
  524. }
  525. } {}
  526. do_test e_fkey-16.3 {
  527. catchsql { UPDATE t2 SET b = 'two' WHERE rowid = 1 }
  528. } {1 {foreign key constraint failed}}
  529. do_test e_fkey-16.4 {
  530. catchsql { DELETE FROM t1 WHERE rowid = 1 }
  531. } {1 {foreign key constraint failed}}
  532. #-------------------------------------------------------------------------
  533. # Specifically, test that when comparing child and parent key values the
  534. # affinity of the parent key column is applied to the child key value
  535. # before the comparison takes place.
  536. #
  537. # EVIDENCE-OF: R-04240-13860 When comparing values, if the parent key
  538. # column has an affinity, then that affinity is applied to the child key
  539. # value before the comparison is performed.
  540. #
  541. drop_all_tables
  542. do_test e_fkey-17.1 {
  543. execsql {
  544. CREATE TABLE t1(a NUMERIC PRIMARY KEY);
  545. CREATE TABLE t2(b TEXT REFERENCES t1);
  546. }
  547. } {}
  548. do_test e_fkey-17.2 {
  549. execsql {
  550. INSERT INTO t1 VALUES(1);
  551. INSERT INTO t1 VALUES(2);
  552. INSERT INTO t1 VALUES('three');
  553. INSERT INTO t2 VALUES('2.0');
  554. SELECT b, typeof(b) FROM t2;
  555. }
  556. } {2.0 text}
  557. do_test e_fkey-17.3 {
  558. execsql { SELECT typeof(a) FROM t1 }
  559. } {integer integer text}
  560. do_test e_fkey-17.4 {
  561. catchsql { DELETE FROM t1 WHERE rowid = 2 }
  562. } {1 {foreign key constraint failed}}
  563. ###########################################################################
  564. ### SECTION 3: Required and Suggested Database Indexes
  565. ###########################################################################
  566. #-------------------------------------------------------------------------
  567. # A parent key must be either a PRIMARY KEY, subject to a UNIQUE
  568. # constraint, or have a UNIQUE index created on it.
  569. #
  570. # EVIDENCE-OF: R-13435-26311 Usually, the parent key of a foreign key
  571. # constraint is the primary key of the parent table. If they are not the
  572. # primary key, then the parent key columns must be collectively subject
  573. # to a UNIQUE constraint or have a UNIQUE index.
  574. #
  575. # Also test that if a parent key is not subject to a PRIMARY KEY or UNIQUE
  576. # constraint, but does have a UNIQUE index created on it, then the UNIQUE index
  577. # must use the default collation sequences associated with the parent key
  578. # columns.
  579. #
  580. # EVIDENCE-OF: R-00376-39212 If the parent key columns have a UNIQUE
  581. # index, then that index must use the collation sequences that are
  582. # specified in the CREATE TABLE statement for the parent table.
  583. #
  584. drop_all_tables
  585. do_test e_fkey-18.1 {
  586. execsql {
  587. CREATE TABLE t2(a REFERENCES t1(x));
  588. }
  589. } {}
  590. proc test_efkey_57 {tn isError sql} {
  591. catchsql { DROP TABLE t1 }
  592. execsql $sql
  593. do_test e_fkey-18.$tn {
  594. catchsql { INSERT INTO t2 VALUES(NULL) }
  595. } [lindex {{0 {}} {/1 {foreign key mismatch - ".*" referencing ".*"}/}} \
  596. $isError]
  597. }
  598. test_efkey_57 2 0 { CREATE TABLE t1(x PRIMARY KEY) }
  599. test_efkey_57 3 0 { CREATE TABLE t1(x UNIQUE) }
  600. test_efkey_57 4 0 { CREATE TABLE t1(x); CREATE UNIQUE INDEX t1i ON t1(x) }
  601. test_efkey_57 5 1 {
  602. CREATE TABLE t1(x);
  603. CREATE UNIQUE INDEX t1i ON t1(x COLLATE nocase);
  604. }
  605. test_efkey_57 6 1 { CREATE TABLE t1(x) }
  606. test_efkey_57 7 1 { CREATE TABLE t1(x, y, PRIMARY KEY(x, y)) }
  607. test_efkey_57 8 1 { CREATE TABLE t1(x, y, UNIQUE(x, y)) }
  608. test_efkey_57 9 1 {
  609. CREATE TABLE t1(x, y);
  610. CREATE UNIQUE INDEX t1i ON t1(x, y);
  611. }
  612. #-------------------------------------------------------------------------
  613. # This block tests an example in foreignkeys.html. Several testable
  614. # statements refer to this example, as follows
  615. #
  616. # EVIDENCE-OF: R-27484-01467
  617. #
  618. # FK Constraints on child1, child2 and child3 are Ok.
  619. #
  620. # Problem with FK on child4:
  621. #
  622. # EVIDENCE-OF: R-51039-44840 The foreign key declared as part of table
  623. # child4 is an error because even though the parent key column is
  624. # indexed, the index is not UNIQUE.
  625. #
  626. # Problem with FK on child5:
  627. #
  628. # EVIDENCE-OF: R-01060-48788 The foreign key for table child5 is an
  629. # error because even though the parent key column has a unique index,
  630. # the index uses a different collating sequence.
  631. #
  632. # Problem with FK on child6 and child7:
  633. #
  634. # EVIDENCE-OF: R-63088-37469 Tables child6 and child7 are incorrect
  635. # because while both have UNIQUE indices on their parent keys, the keys
  636. # are not an exact match to the columns of a single UNIQUE index.
  637. #
  638. drop_all_tables
  639. do_test e_fkey-19.1 {
  640. execsql {
  641. CREATE TABLE parent(a PRIMARY KEY, b UNIQUE, c, d, e, f);
  642. CREATE UNIQUE INDEX i1 ON parent(c, d);
  643. CREATE INDEX i2 ON parent(e);
  644. CREATE UNIQUE INDEX i3 ON parent(f COLLATE nocase);
  645. CREATE TABLE child1(f, g REFERENCES parent(a)); -- Ok
  646. CREATE TABLE child2(h, i REFERENCES parent(b)); -- Ok
  647. CREATE TABLE child3(j, k, FOREIGN KEY(j, k) REFERENCES parent(c, d)); -- Ok
  648. CREATE TABLE child4(l, m REFERENCES parent(e)); -- Err
  649. CREATE TABLE child5(n, o REFERENCES parent(f)); -- Err
  650. CREATE TABLE child6(p, q, FOREIGN KEY(p,q) REFERENCES parent(b, c)); -- Err
  651. CREATE TABLE child7(r REFERENCES parent(c)); -- Err
  652. }
  653. } {}
  654. do_test e_fkey-19.2 {
  655. execsql {
  656. INSERT INTO parent VALUES(1, 2, 3, 4, 5, 6);
  657. INSERT INTO child1 VALUES('xxx', 1);
  658. INSERT INTO child2 VALUES('xxx', 2);
  659. INSERT INTO child3 VALUES(3, 4);
  660. }
  661. } {}
  662. do_test e_fkey-19.2 {
  663. catchsql { INSERT INTO child4 VALUES('xxx', 5) }
  664. } {1 {foreign key mismatch - "child4" referencing "parent"}}
  665. do_test e_fkey-19.3 {
  666. catchsql { INSERT INTO child5 VALUES('xxx', 6) }
  667. } {1 {foreign key mismatch - "child5" referencing "parent"}}
  668. do_test e_fkey-19.4 {
  669. catchsql { INSERT INTO child6 VALUES(2, 3) }
  670. } {1 {foreign key mismatch - "child6" referencing "parent"}}
  671. do_test e_fkey-19.5 {
  672. catchsql { INSERT INTO child7 VALUES(3) }
  673. } {1 {foreign key mismatch - "child7" referencing "parent"}}
  674. #-------------------------------------------------------------------------
  675. # Test errors in the database schema that are detected while preparing
  676. # DML statements. The error text for these messages always matches
  677. # either "foreign key mismatch" or "no such table*" (using [string match]).
  678. #
  679. # EVIDENCE-OF: R-45488-08504 If the database schema contains foreign key
  680. # errors that require looking at more than one table definition to
  681. # identify, then those errors are not detected when the tables are
  682. # created.
  683. #
  684. # EVIDENCE-OF: R-48391-38472 Instead, such errors prevent the
  685. # application from preparing SQL statements that modify the content of
  686. # the child or parent tables in ways that use the foreign keys.
  687. #
  688. # EVIDENCE-OF: R-03108-63659 The English language error message for
  689. # foreign key DML errors is usually "foreign key mismatch" but can also
  690. # be "no such table" if the parent table does not exist.
  691. #
  692. # EVIDENCE-OF: R-60781-26576 Foreign key DML errors are may be reported
  693. # if: The parent table does not exist, or The parent key columns named
  694. # in the foreign key constraint do not exist, or The parent key columns
  695. # named in the foreign key constraint are not the primary key of the
  696. # parent table and are not subject to a unique constraint using
  697. # collating sequence specified in the CREATE TABLE, or The child table
  698. # references the primary key of the parent without specifying the
  699. # primary key columns and the number of primary key columns in the
  700. # parent do not match the number of child key columns.
  701. #
  702. do_test e_fkey-20.1 {
  703. execsql {
  704. CREATE TABLE c1(c REFERENCES nosuchtable, d);
  705. CREATE TABLE p2(a, b, UNIQUE(a, b));
  706. CREATE TABLE c2(c, d, FOREIGN KEY(c, d) REFERENCES p2(a, x));
  707. CREATE TABLE p3(a PRIMARY KEY, b);
  708. CREATE TABLE c3(c REFERENCES p3(b), d);
  709. CREATE TABLE p4(a PRIMARY KEY, b);
  710. CREATE UNIQUE INDEX p4i ON p4(b COLLATE nocase);
  711. CREATE TABLE c4(c REFERENCES p4(b), d);
  712. CREATE TABLE p5(a PRIMARY KEY, b COLLATE nocase);
  713. CREATE UNIQUE INDEX p5i ON p5(b COLLATE binary);
  714. CREATE TABLE c5(c REFERENCES p5(b), d);
  715. CREATE TABLE p6(a PRIMARY KEY, b);
  716. CREATE TABLE c6(c, d, FOREIGN KEY(c, d) REFERENCES p6);
  717. CREATE TABLE p7(a, b, PRIMARY KEY(a, b));
  718. CREATE TABLE c7(c, d REFERENCES p7);
  719. }
  720. } {}
  721. foreach {tn tbl ptbl err} {
  722. 2 c1 {} "no such table: main.nosuchtable"
  723. 3 c2 p2 "foreign key mismatch - \"c2\" referencing \"p2\""
  724. 4 c3 p3 "foreign key mismatch - \"c3\" referencing \"p3\""
  725. 5 c4 p4 "foreign key mismatch - \"c4\" referencing \"p4\""
  726. 6 c5 p5 "foreign key mismatch - \"c5\" referencing \"p5\""
  727. 7 c6 p6 "foreign key mismatch - \"c6\" referencing \"p6\""
  728. 8 c7 p7 "foreign key mismatch - \"c7\" referencing \"p7\""
  729. } {
  730. do_test e_fkey-20.$tn.1 {
  731. catchsql "INSERT INTO $tbl VALUES('a', 'b')"
  732. } [list 1 $err]
  733. do_test e_fkey-20.$tn.2 {
  734. catchsql "UPDATE $tbl SET c = ?, d = ?"
  735. } [list 1 $err]
  736. do_test e_fkey-20.$tn.3 {
  737. catchsql "INSERT INTO $tbl SELECT ?, ?"
  738. } [list 1 $err]
  739. if {$ptbl ne ""} {
  740. do_test e_fkey-20.$tn.4 {
  741. catchsql "DELETE FROM $ptbl"
  742. } [list 1 $err]
  743. do_test e_fkey-20.$tn.5 {
  744. catchsql "UPDATE $ptbl SET a = ?, b = ?"
  745. } [list 1 $err]
  746. do_test e_fkey-20.$tn.6 {
  747. catchsql "INSERT INTO $ptbl SELECT ?, ?"
  748. } [list 1 $err]
  749. }
  750. }
  751. #-------------------------------------------------------------------------
  752. # EVIDENCE-OF: R-19353-43643
  753. #
  754. # Test the example of foreign key mismatch errors caused by implicitly
  755. # mapping a child key to the primary key of the parent table when the
  756. # child key consists of a different number of columns to that primary key.
  757. #
  758. drop_all_tables
  759. do_test e_fkey-21.1 {
  760. execsql {
  761. CREATE TABLE parent2(a, b, PRIMARY KEY(a,b));
  762. CREATE TABLE child8(x, y, FOREIGN KEY(x,y) REFERENCES parent2); -- Ok
  763. CREATE TABLE child9(x REFERENCES parent2); -- Err
  764. CREATE TABLE child10(x,y,z, FOREIGN KEY(x,y,z) REFERENCES parent2); -- Err
  765. }
  766. } {}
  767. do_test e_fkey-21.2 {
  768. execsql {
  769. INSERT INTO parent2 VALUES('I', 'II');
  770. INSERT INTO child8 VALUES('I', 'II');
  771. }
  772. } {}
  773. do_test e_fkey-21.3 {
  774. catchsql { INSERT INTO child9 VALUES('I') }
  775. } {1 {foreign key mismatch - "child9" referencing "parent2"}}
  776. do_test e_fkey-21.4 {
  777. catchsql { INSERT INTO child9 VALUES('II') }
  778. } {1 {foreign key mismatch - "child9" referencing "parent2"}}
  779. do_test e_fkey-21.5 {
  780. catchsql { INSERT INTO child9 VALUES(NULL) }
  781. } {1 {foreign key mismatch - "child9" referencing "parent2"}}
  782. do_test e_fkey-21.6 {
  783. catchsql { INSERT INTO child10 VALUES('I', 'II', 'III') }
  784. } {1 {foreign key mismatch - "child10" referencing "parent2"}}
  785. do_test e_fkey-21.7 {
  786. catchsql { INSERT INTO child10 VALUES(1, 2, 3) }
  787. } {1 {foreign key mismatch - "child10" referencing "parent2"}}
  788. do_test e_fkey-21.8 {
  789. catchsql { INSERT INTO child10 VALUES(NULL, NULL, NULL) }
  790. } {1 {foreign key mismatch - "child10" referencing "parent2"}}
  791. #-------------------------------------------------------------------------
  792. # Test errors that are reported when creating the child table.
  793. # Specifically:
  794. #
  795. # * different number of child and parent key columns, and
  796. # * child columns that do not exist.
  797. #
  798. # EVIDENCE-OF: R-23682-59820 By contrast, if foreign key errors can be
  799. # recognized simply by looking at the definition of the child table and
  800. # without having to consult the parent table definition, then the CREATE
  801. # TABLE statement for the child table fails.
  802. #
  803. # These errors are reported whether or not FK support is enabled.
  804. #
  805. # EVIDENCE-OF: R-33883-28833 Foreign key DDL errors are reported
  806. # regardless of whether or not foreign key constraints are enabled when
  807. # the table is created.
  808. #
  809. drop_all_tables
  810. foreach fk [list OFF ON] {
  811. execsql "PRAGMA foreign_keys = $fk"
  812. set i 0
  813. foreach {sql error} {
  814. "CREATE TABLE child1(a, b, FOREIGN KEY(a, b) REFERENCES p(c))"
  815. {number of columns in foreign key does not match the number of columns in the referenced table}
  816. "CREATE TABLE child2(a, b, FOREIGN KEY(a, b) REFERENCES p(c, d, e))"
  817. {number of columns in foreign key does not match the number of columns in the referenced table}
  818. "CREATE TABLE child2(a, b, FOREIGN KEY(a, c) REFERENCES p(c, d))"
  819. {unknown column "c" in foreign key definition}
  820. "CREATE TABLE child2(a, b, FOREIGN KEY(c, b) REFERENCES p(c, d))"
  821. {unknown column "c" in foreign key definition}
  822. } {
  823. do_test e_fkey-22.$fk.[incr i] {
  824. catchsql $sql
  825. } [list 1 $error]
  826. }
  827. }
  828. #-------------------------------------------------------------------------
  829. # Test that a REFERENCING clause that does not specify parent key columns
  830. # implicitly maps to the primary key of the parent table.
  831. #
  832. # EVIDENCE-OF: R-43879-08025 Attaching a "REFERENCES <parent-table>"
  833. # clause to a column definition creates a foreign
  834. # key constraint that maps the column to the primary key of
  835. # <parent-table>.
  836. #
  837. do_test e_fkey-23.1 {
  838. execsql {
  839. CREATE TABLE p1(a, b, PRIMARY KEY(a, b));
  840. CREATE TABLE p2(a, b PRIMARY KEY);
  841. CREATE TABLE c1(c, d, FOREIGN KEY(c, d) REFERENCES p1);
  842. CREATE TABLE c2(a, b REFERENCES p2);
  843. }
  844. } {}
  845. proc test_efkey_60 {tn isError sql} {
  846. do_test e_fkey-23.$tn "
  847. catchsql {$sql}
  848. " [lindex {{0 {}} {1 {foreign key constraint failed}}} $isError]
  849. }
  850. test_efkey_60 2 1 "INSERT INTO c1 VALUES(239, 231)"
  851. test_efkey_60 3 0 "INSERT INTO p1 VALUES(239, 231)"
  852. test_efkey_60 4 0 "INSERT INTO c1 VALUES(239, 231)"
  853. test_efkey_60 5 1 "INSERT INTO c2 VALUES(239, 231)"
  854. test_efkey_60 6 0 "INSERT INTO p2 VALUES(239, 231)"
  855. test_efkey_60 7 0 "INSERT INTO c2 VALUES(239, 231)"
  856. #-------------------------------------------------------------------------
  857. # Test that an index on on the child key columns of an FK constraint
  858. # is optional.
  859. #
  860. # EVIDENCE-OF: R-15417-28014 Indices are not required for child key
  861. # columns
  862. #
  863. # Also test that if an index is created on the child key columns, it does
  864. # not make a difference whether or not it is a UNIQUE index.
  865. #
  866. # EVIDENCE-OF: R-15741-50893 The child key index does not have to be
  867. # (and usually will not be) a UNIQUE index.
  868. #
  869. drop_all_tables
  870. do_test e_fkey-24.1 {
  871. execsql {
  872. CREATE TABLE parent(x, y, UNIQUE(y, x));
  873. CREATE TABLE c1(a, b, FOREIGN KEY(a, b) REFERENCES parent(x, y));
  874. CREATE TABLE c2(a, b, FOREIGN KEY(a, b) REFERENCES parent(x, y));
  875. CREATE TABLE c3(a, b, FOREIGN KEY(a, b) REFERENCES parent(x, y));
  876. CREATE INDEX c2i ON c2(a, b);
  877. CREATE UNIQUE INDEX c3i ON c2(b, a);
  878. }
  879. } {}
  880. proc test_efkey_61 {tn isError sql} {
  881. do_test e_fkey-24.$tn "
  882. catchsql {$sql}
  883. " [lindex {{0 {}} {1 {foreign key constraint failed}}} $isError]
  884. }
  885. foreach {tn c} [list 2 c1 3 c2 4 c3] {
  886. test_efkey_61 $tn.1 1 "INSERT INTO $c VALUES(1, 2)"
  887. test_efkey_61 $tn.2 0 "INSERT INTO parent VALUES(1, 2)"
  888. test_efkey_61 $tn.3 0 "INSERT INTO $c VALUES(1, 2)"
  889. execsql "DELETE FROM $c ; DELETE FROM parent"
  890. }
  891. #-------------------------------------------------------------------------
  892. # EVIDENCE-OF: R-00279-52283
  893. #
  894. # Test an example showing that when a row is deleted from the parent
  895. # table, the child table is queried for orphaned rows as follows:
  896. #
  897. # SELECT rowid FROM track WHERE trackartist = ?
  898. #
  899. # EVIDENCE-OF: R-23302-30956 If this SELECT returns any rows at all,
  900. # then SQLite concludes that deleting the row from the parent table
  901. # would violate the foreign key constraint and returns an error.
  902. #
  903. do_test e_fkey-25.1 {
  904. execsql {
  905. CREATE TABLE artist(
  906. artistid INTEGER PRIMARY KEY,
  907. artistname TEXT
  908. );
  909. CREATE TABLE track(
  910. trackid INTEGER,
  911. trackname TEXT,
  912. trackartist INTEGER,
  913. FOREIGN KEY(trackartist) REFERENCES artist(artistid)
  914. );
  915. }
  916. } {}
  917. do_execsql_test e_fkey-25.2 {
  918. PRAGMA foreign_keys = OFF;
  919. EXPLAIN QUERY PLAN DELETE FROM artist WHERE 1;
  920. EXPLAIN QUERY PLAN SELECT rowid FROM track WHERE trackartist = ?;
  921. } {
  922. 0 0 0 {SCAN TABLE artist}
  923. 0 0 0 {SCAN TABLE track}
  924. }
  925. do_execsql_test e_fkey-25.3 {
  926. PRAGMA foreign_keys = ON;
  927. EXPLAIN QUERY PLAN DELETE FROM artist WHERE 1;
  928. } {
  929. 0 0 0 {SCAN TABLE artist}
  930. 0 0 0 {SCAN TABLE track}
  931. }
  932. do_test e_fkey-25.4 {
  933. execsql {
  934. INSERT INTO artist VALUES(5, 'artist 5');
  935. INSERT INTO artist VALUES(6, 'artist 6');
  936. INSERT INTO artist VALUES(7, 'artist 7');
  937. INSERT INTO track VALUES(1, 'track 1', 5);
  938. INSERT INTO track VALUES(2, 'track 2', 6);
  939. }
  940. } {}
  941. do_test e_fkey-25.5 {
  942. concat \
  943. [execsql { SELECT rowid FROM track WHERE trackartist = 5 }] \
  944. [catchsql { DELETE FROM artist WHERE artistid = 5 }]
  945. } {1 1 {foreign key constraint failed}}
  946. do_test e_fkey-25.6 {
  947. concat \
  948. [execsql { SELECT rowid FROM track WHERE trackartist = 7 }] \
  949. [catchsql { DELETE FROM artist WHERE artistid = 7 }]
  950. } {0 {}}
  951. do_test e_fkey-25.7 {
  952. concat \
  953. [execsql { SELECT rowid FROM track WHERE trackartist = 6 }] \
  954. [catchsql { DELETE FROM artist WHERE artistid = 6 }]
  955. } {2 1 {foreign key constraint failed}}
  956. #-------------------------------------------------------------------------
  957. # EVIDENCE-OF: R-47936-10044 Or, more generally:
  958. # SELECT rowid FROM <child-table> WHERE <child-key> = :parent_key_value
  959. #
  960. # Test that when a row is deleted from the parent table of an FK
  961. # constraint, the child table is queried for orphaned rows. The
  962. # query is equivalent to:
  963. #
  964. # SELECT rowid FROM <child-table> WHERE <child-key> = :parent_key_value
  965. #
  966. # Also test that when a row is inserted into the parent table, or when the
  967. # parent key values of an existing row are modified, a query equivalent
  968. # to the following is planned. In some cases it is not executed, but it
  969. # is always planned.
  970. #
  971. # SELECT rowid FROM <child-table> WHERE <child-key> = :parent_key_value
  972. #
  973. # EVIDENCE-OF: R-61616-46700 Similar queries may be run if the content
  974. # of the parent key is modified or a new row is inserted into the parent
  975. # table.
  976. #
  977. #
  978. drop_all_tables
  979. do_test e_fkey-26.1 {
  980. execsql { CREATE TABLE parent(x, y, UNIQUE(y, x)) }
  981. } {}
  982. foreach {tn sql} {
  983. 2 {
  984. CREATE TABLE child(a, b, FOREIGN KEY(a, b) REFERENCES parent(x, y))
  985. }
  986. 3 {
  987. CREATE TABLE child(a, b, FOREIGN KEY(a, b) REFERENCES parent(x, y));
  988. CREATE INDEX childi ON child(a, b);
  989. }
  990. 4 {
  991. CREATE TABLE child(a, b, FOREIGN KEY(a, b) REFERENCES parent(x, y));
  992. CREATE UNIQUE INDEX childi ON child(b, a);
  993. }
  994. } {
  995. execsql $sql
  996. execsql {PRAGMA foreign_keys = OFF}
  997. set delete [concat \
  998. [eqp "DELETE FROM parent WHERE 1"] \
  999. [eqp "SELECT rowid FROM child WHERE a = ? AND b = ?"]
  1000. ]
  1001. set update [concat \
  1002. [eqp "UPDATE parent SET x=?, y=?"] \
  1003. [eqp "SELECT rowid FROM child WHERE a = ? AND b = ?"] \
  1004. [eqp "SELECT rowid FROM child WHERE a = ? AND b = ?"]
  1005. ]
  1006. execsql {PRAGMA foreign_keys = ON}
  1007. do_test e_fkey-26.$tn.1 { eqp "DELETE FROM parent WHERE 1" } $delete
  1008. do_test e_fkey-26.$tn.2 { eqp "UPDATE parent set x=?, y=?" } $update
  1009. execsql {DROP TABLE child}
  1010. }
  1011. #-------------------------------------------------------------------------
  1012. # EVIDENCE-OF: R-14553-34013
  1013. #
  1014. # Test the example schema at the end of section 3. Also test that is
  1015. # is "efficient". In this case "efficient" means that foreign key
  1016. # related operations on the parent table do not provoke linear scans.
  1017. #
  1018. drop_all_tables
  1019. do_test e_fkey-27.1 {
  1020. execsql {
  1021. CREATE TABLE artist(
  1022. artistid INTEGER PRIMARY KEY,
  1023. artistname TEXT
  1024. );
  1025. CREATE TABLE track(
  1026. trackid INTEGER,
  1027. trackname TEXT,
  1028. trackartist INTEGER REFERENCES artist
  1029. );
  1030. CREATE INDEX trackindex ON track(trackartist);
  1031. }
  1032. } {}
  1033. do_test e_fkey-27.2 {
  1034. eqp { INSERT INTO artist VALUES(?, ?) }
  1035. } {}
  1036. do_execsql_test e_fkey-27.3 {
  1037. EXPLAIN QUERY PLAN UPDATE artist SET artistid = ?, artistname = ?
  1038. } {
  1039. 0 0 0 {SCAN TABLE artist}
  1040. 0 0 0 {SEARCH TABLE track USING COVERING INDEX trackindex (trackartist=?)}
  1041. 0 0 0 {SEARCH TABLE track USING COVERING INDEX trackindex (trackartist=?)}
  1042. }
  1043. do_execsql_test e_fkey-27.4 {
  1044. EXPLAIN QUERY PLAN DELETE FROM artist
  1045. } {
  1046. 0 0 0 {SCAN TABLE artist}
  1047. 0 0 0 {SEARCH TABLE track USING COVERING INDEX trackindex (trackartist=?)}
  1048. }
  1049. ###########################################################################
  1050. ### SECTION 4.1: Composite Foreign Key Constraints
  1051. ###########################################################################
  1052. #-------------------------------------------------------------------------
  1053. # Check that parent and child keys must have the same number of columns.
  1054. #
  1055. # EVIDENCE-OF: R-41062-34431 Parent and child keys must have the same
  1056. # cardinality.
  1057. #
  1058. foreach {tn sql err} {
  1059. 1 "CREATE TABLE c(jj REFERENCES p(x, y))"
  1060. {foreign key on jj should reference only one column of table p}
  1061. 2 "CREATE TABLE c(jj REFERENCES p())" {near ")": syntax error}
  1062. 3 "CREATE TABLE c(jj, FOREIGN KEY(jj) REFERENCES p(x, y))"
  1063. {number of columns in foreign key does not match the number of columns in the referenced table}
  1064. 4 "CREATE TABLE c(jj, FOREIGN KEY(jj) REFERENCES p())"
  1065. {near ")": syntax error}
  1066. 5 "CREATE TABLE c(ii, jj, FOREIGN KEY(jj, ii) REFERENCES p())"
  1067. {near ")": syntax error}
  1068. 6 "CREATE TABLE c(ii, jj, FOREIGN KEY(jj, ii) REFERENCES p(x))"
  1069. {number of columns in foreign key does not match the number of columns in the referenced table}
  1070. 7 "CREATE TABLE c(ii, jj, FOREIGN KEY(jj, ii) REFERENCES p(x,y,z))"
  1071. {number of columns in foreign key does not match the number of columns in the referenced table}
  1072. } {
  1073. drop_all_tables
  1074. do_test e_fkey-28.$tn [list catchsql $sql] [list 1 $err]
  1075. }
  1076. do_test e_fkey-28.8 {
  1077. drop_all_tables
  1078. execsql {
  1079. CREATE TABLE p(x PRIMARY KEY);
  1080. CREATE TABLE c(a, b, FOREIGN KEY(a,b) REFERENCES p);
  1081. }
  1082. catchsql {DELETE FROM p}
  1083. } {1 {foreign key mismatch - "c" referencing "p"}}
  1084. do_test e_fkey-28.9 {
  1085. drop_all_tables
  1086. execsql {
  1087. CREATE TABLE p(x, y, PRIMARY KEY(x,y));
  1088. CREATE TABLE c(a REFERENCES p);
  1089. }
  1090. catchsql {DELETE FROM p}
  1091. } {1 {foreign key mismatch - "c" referencing "p"}}
  1092. #-------------------------------------------------------------------------
  1093. # EVIDENCE-OF: R-24676-09859
  1094. #
  1095. # Test the example schema in the "Composite Foreign Key Constraints"
  1096. # section.
  1097. #
  1098. do_test e_fkey-29.1 {
  1099. execsql {
  1100. CREATE TABLE album(
  1101. albumartist TEXT,
  1102. albumname TEXT,
  1103. albumcover BINARY,
  1104. PRIMARY KEY(albumartist, albumname)
  1105. );
  1106. CREATE TABLE song(
  1107. songid INTEGER,
  1108. songartist TEXT,
  1109. songalbum TEXT,
  1110. songname TEXT,
  1111. FOREIGN KEY(songartist, songalbum) REFERENCES album(albumartist,albumname)
  1112. );
  1113. }
  1114. } {}
  1115. do_test e_fkey-29.2 {
  1116. execsql {
  1117. INSERT INTO album VALUES('Elvis Presley', 'Elvis'' Christmas Album', NULL);
  1118. INSERT INTO song VALUES(
  1119. 1, 'Elvis Presley', 'Elvis'' Christmas Album', 'Here Comes Santa Clause'
  1120. );
  1121. }
  1122. } {}
  1123. do_test e_fkey-29.3 {
  1124. catchsql {
  1125. INSERT INTO song VALUES(2, 'Elvis Presley', 'Elvis Is Back!', 'Fever');
  1126. }
  1127. } {1 {foreign key constraint failed}}
  1128. #-------------------------------------------------------------------------
  1129. # EVIDENCE-OF: R-33626-48418 In SQLite, if any of the child key columns
  1130. # (in this case songartist and songalbum) are NULL, then there is no
  1131. # requirement for a corresponding row in the parent table.
  1132. #
  1133. do_test e_fkey-30.1 {
  1134. execsql {
  1135. INSERT INTO song VALUES(2, 'Elvis Presley', NULL, 'Fever');
  1136. INSERT INTO song VALUES(3, NULL, 'Elvis Is Back', 'Soldier Boy');
  1137. }
  1138. } {}
  1139. ###########################################################################
  1140. ### SECTION 4.2: Deferred Foreign Key Constraints
  1141. ###########################################################################
  1142. #-------------------------------------------------------------------------
  1143. # Test that if a statement violates an immediate FK constraint, and the
  1144. # database does not satisfy the FK constraint once all effects of the
  1145. # statement have been applied, an error is reported and the effects of
  1146. # the statement rolled back.
  1147. #
  1148. # EVIDENCE-OF: R-09323-30470 If a statement modifies the contents of the
  1149. # database so that an immediate foreign key constraint is in violation
  1150. # at the conclusion the statement, an exception is thrown and the
  1151. # effects of the statement are reverted.
  1152. #
  1153. drop_all_tables
  1154. do_test e_fkey-31.1 {
  1155. execsql {
  1156. CREATE TABLE king(a, b, PRIMARY KEY(a));
  1157. CREATE TABLE prince(c REFERENCES king, d);
  1158. }
  1159. } {}
  1160. do_test e_fkey-31.2 {
  1161. # Execute a statement that violates the immediate FK constraint.
  1162. catchsql { INSERT INTO prince VALUES(1, 2) }
  1163. } {1 {foreign key constraint failed}}
  1164. do_test e_fkey-31.3 {
  1165. # This time, use a trigger to fix the constraint violation before the
  1166. # statement has finished executing. Then execute the same statement as
  1167. # in the previous test case. This time, no error.
  1168. execsql {
  1169. CREATE TRIGGER kt AFTER INSERT ON prince WHEN
  1170. NOT EXISTS (SELECT a FROM king WHERE a = new.c)
  1171. BEGIN
  1172. INSERT INTO king VALUES(new.c, NULL);
  1173. END
  1174. }
  1175. execsql { INSERT INTO prince VALUES(1, 2) }
  1176. } {}
  1177. # Test that operating inside a transaction makes no difference to
  1178. # immediate constraint violation handling.
  1179. do_test e_fkey-31.4 {
  1180. execsql {
  1181. BEGIN;
  1182. INSERT INTO prince VALUES(2, 3);
  1183. DROP TRIGGER kt;
  1184. }
  1185. catchsql { INSERT INTO prince VALUES(3, 4) }
  1186. } {1 {foreign key constraint failed}}
  1187. do_test e_fkey-31.5 {
  1188. execsql {
  1189. COMMIT;
  1190. SELECT * FROM king;
  1191. }
  1192. } {1 {} 2 {}}
  1193. #-------------------------------------------------------------------------
  1194. # Test that if a deferred constraint is violated within a transaction,
  1195. # nothing happens immediately and the database is allowed to persist
  1196. # in a state that does not satisfy the FK constraint. However attempts
  1197. # to COMMIT the transaction fail until the FK constraint is satisfied.
  1198. #
  1199. # EVIDENCE-OF: R-49178-21358 By contrast, if a statement modifies the
  1200. # contents of the database such that a deferred foreign key constraint
  1201. # is violated, the violation is not reported immediately.
  1202. #
  1203. # EVIDENCE-OF: R-39692-12488 Deferred foreign key constraints are not
  1204. # checked until the transaction tries to COMMIT.
  1205. #
  1206. # EVIDENCE-OF: R-55147-47664 For as long as the user has an open
  1207. # transaction, the database is allowed to exist in a state that violates
  1208. # any number of deferred foreign key constraints.
  1209. #
  1210. # EVIDENCE-OF: R-29604-30395 However, COMMIT will fail as long as
  1211. # foreign key constraints remain in violation.
  1212. #
  1213. proc test_efkey_34 {tn isError sql} {
  1214. do_test e_fkey-32.$tn "
  1215. catchsql {$sql}
  1216. " [lindex {{0 {}} {1 {foreign key constraint failed}}} $isError]
  1217. }
  1218. drop_all_tables
  1219. test_efkey_34 1 0 {
  1220. CREATE TABLE ll(k PRIMARY KEY);
  1221. CREATE TABLE kk(c REFERENCES ll DEFERRABLE INITIALLY DEFERRED);
  1222. }
  1223. test_efkey_34 2 0 "BEGIN"
  1224. test_efkey_34 3 0 "INSERT INTO kk VALUES(5)"
  1225. test_efkey_34 4 0 "INSERT INTO kk VALUES(10)"
  1226. test_efkey_34 5 1 "COMMIT"
  1227. test_efkey_34 6 0 "INSERT INTO ll VALUES(10)"
  1228. test_efkey_34 7 1 "COMMIT"
  1229. test_efkey_34 8 0 "INSERT INTO ll VALUES(5)"
  1230. test_efkey_34 9 0 "COMMIT"
  1231. #-------------------------------------------------------------------------
  1232. # When not running inside a transaction, a deferred constraint is similar
  1233. # to an immediate constraint (violations are reported immediately).
  1234. #
  1235. # EVIDENCE-OF: R-56844-61705 If the current statement is not inside an
  1236. # explicit transaction (a BEGIN/COMMIT/ROLLBACK block), then an implicit
  1237. # transaction is committed as soon as the statement has finished
  1238. # executing. In this case deferred constraints behave the same as
  1239. # immediate constraints.
  1240. #
  1241. drop_all_tables
  1242. proc test_efkey_35 {tn isError sql} {
  1243. do_test e_fkey-33.$tn "
  1244. catchsql {$sql}
  1245. " [lindex {{0 {}} {1 {foreign key constraint failed}}} $isError]
  1246. }
  1247. do_test e_fkey-33.1 {
  1248. execsql {
  1249. CREATE TABLE parent(x, y);
  1250. CREATE UNIQUE INDEX pi ON parent(x, y);
  1251. CREATE TABLE child(a, b,
  1252. FOREIGN KEY(a, b) REFERENCES parent(x, y) DEFERRABLE INITIALLY DEFERRED
  1253. );
  1254. }
  1255. } {}
  1256. test_efkey_35 2 1 "INSERT INTO child VALUES('x', 'y')"
  1257. test_efkey_35 3 0 "INSERT INTO parent VALUES('x', 'y')"
  1258. test_efkey_35 4 0 "INSERT INTO child VALUES('x', 'y')"
  1259. #-------------------------------------------------------------------------
  1260. # EVIDENCE-OF: R-12782-61841
  1261. #
  1262. # Test that an FK constraint is made deferred by adding the following
  1263. # to the definition:
  1264. #
  1265. # DEFERRABLE INITIALLY DEFERRED
  1266. #
  1267. # EVIDENCE-OF: R-09005-28791
  1268. #
  1269. # Also test that adding any of the following to a foreign key definition
  1270. # makes the constraint IMMEDIATE:
  1271. #
  1272. # NOT DEFERRABLE INITIALLY DEFERRED
  1273. # NOT DEFERRABLE INITIALLY IMMEDIATE
  1274. # NOT DEFERRABLE
  1275. # DEFERRABLE INITIALLY IMMEDIATE
  1276. # DEFERRABLE
  1277. #
  1278. # Foreign keys are IMMEDIATE by default (if there is no DEFERRABLE or NOT
  1279. # DEFERRABLE clause).
  1280. #
  1281. # EVIDENCE-OF: R-35290-16460 Foreign key constraints are immediate by
  1282. # default.
  1283. #
  1284. # EVIDENCE-OF: R-30323-21917 Each foreign key constraint in SQLite is
  1285. # classified as either immediate or deferred.
  1286. #
  1287. drop_all_tables
  1288. do_test e_fkey-34.1 {
  1289. execsql {
  1290. CREATE TABLE parent(x, y, z, PRIMARY KEY(x,y,z));
  1291. CREATE TABLE c1(a, b, c,
  1292. FOREIGN KEY(a, b, c) REFERENCES parent NOT DEFERRABLE INITIALLY DEFERRED
  1293. );
  1294. CREATE TABLE c2(a, b, c,
  1295. FOREIGN KEY(a, b, c) REFERENCES parent NOT DEFERRABLE INITIALLY IMMEDIATE
  1296. );
  1297. CREATE TABLE c3(a, b, c,
  1298. FOREIGN KEY(a, b, c) REFERENCES parent NOT DEFERRABLE
  1299. );
  1300. CREATE TABLE c4(a, b, c,
  1301. FOREIGN KEY(a, b, c) REFERENCES parent DEFERRABLE INITIALLY IMMEDIATE
  1302. );
  1303. CREATE TABLE c5(a, b, c,
  1304. FOREIGN KEY(a, b, c) REFERENCES parent DEFERRABLE
  1305. );
  1306. CREATE TABLE c6(a, b, c, FOREIGN KEY(a, b, c) REFERENCES parent);
  1307. -- This FK constraint is the only deferrable one.
  1308. CREATE TABLE c7(a, b, c,
  1309. FOREIGN KEY(a, b, c) REFERENCES parent DEFERRABLE INITIALLY DEFERRED
  1310. );
  1311. INSERT INTO parent VALUES('a', 'b', 'c');
  1312. INSERT INTO parent VALUES('d', 'e', 'f');
  1313. INSERT INTO parent VALUES('g', 'h', 'i');
  1314. INSERT INTO parent VALUES('j', 'k', 'l');
  1315. INSERT INTO parent VALUES('m', 'n', 'o');
  1316. INSERT INTO parent VALUES('p', 'q', 'r');
  1317. INSERT INTO parent VALUES('s', 't', 'u');
  1318. INSERT INTO c1 VALUES('a', 'b', 'c');
  1319. INSERT INTO c2 VALUES('d', 'e', 'f');
  1320. INSERT INTO c3 VALUES('g', 'h', 'i');
  1321. INSERT INTO c4 VALUES('j', 'k', 'l');
  1322. INSERT INTO c5 VALUES('m', 'n', 'o');
  1323. INSERT INTO c6 VALUES('p', 'q', 'r');
  1324. INSERT INTO c7 VALUES('s', 't', 'u');
  1325. }
  1326. } {}
  1327. proc test_efkey_29 {tn sql isError} {
  1328. do_test e_fkey-34.$tn "catchsql {$sql}" [
  1329. lindex {{0 {}} {1 {foreign key constraint failed}}} $isError
  1330. ]
  1331. }
  1332. test_efkey_29 2 "BEGIN" 0
  1333. test_efkey_29 3 "DELETE FROM parent WHERE x = 'a'" 1
  1334. test_efkey_29 4 "DELETE FROM parent WHERE x = 'd'" 1
  1335. test_efkey_29 5 "DELETE FROM parent WHERE x = 'g'" 1
  1336. test_efkey_29 6 "DELETE FROM parent WHERE x = 'j'" 1
  1337. test_efkey_29 7 "DELETE FROM parent WHERE x = 'm'" 1
  1338. test_efkey_29 8 "DELETE FROM parent WHERE x = 'p'" 1
  1339. test_efkey_29 9 "DELETE FROM parent WHERE x = 's'" 0
  1340. test_efkey_29 10 "COMMIT" 1
  1341. test_efkey_29 11 "ROLLBACK" 0
  1342. test_efkey_29 9 "BEGIN" 0
  1343. test_efkey_29 10 "UPDATE parent SET z = 'z' WHERE z = 'c'" 1
  1344. test_efkey_29 11 "UPDATE parent SET z = 'z' WHERE z = 'f'" 1
  1345. test_efkey_29 12 "UPDATE parent SET z = 'z' WHERE z = 'i'" 1
  1346. test_efkey_29 13 "UPDATE parent SET z = 'z' WHERE z = 'l'" 1
  1347. test_efkey_29 14 "UPDATE parent SET z = 'z' WHERE z = 'o'" 1
  1348. test_efkey_29 15 "UPDATE parent SET z = 'z' WHERE z = 'r'" 1
  1349. test_efkey_29 16 "UPDATE parent SET z = 'z' WHERE z = 'u'" 0
  1350. test_efkey_29 17 "COMMIT" 1
  1351. test_efkey_29 18 "ROLLBACK" 0
  1352. test_efkey_29 17 "BEGIN" 0
  1353. test_efkey_29 18 "INSERT INTO c1 VALUES(1, 2, 3)" 1
  1354. test_efkey_29 19 "INSERT INTO c2 VALUES(1, 2, 3)" 1
  1355. test_efkey_29 20 "INSERT INTO c3 VALUES(1, 2, 3)" 1
  1356. test_efkey_29 21 "INSERT INTO c4 VALUES(1, 2, 3)" 1
  1357. test_efkey_29 22 "INSERT INTO c5 VALUES(1, 2, 3)" 1
  1358. test_efkey_29 22 "INSERT INTO c6 VALUES(1, 2, 3)" 1
  1359. test_efkey_29 22 "INSERT INTO c7 VALUES(1, 2, 3)" 0
  1360. test_efkey_29 23 "COMMIT" 1
  1361. test_efkey_29 24 "INSERT INTO parent VALUES(1, 2, 3)" 0
  1362. test_efkey_29 25 "COMMIT" 0
  1363. test_efkey_29 26 "BEGIN" 0
  1364. test_efkey_29 27 "UPDATE c1 SET a = 10" 1
  1365. test_efkey_29 28 "UPDATE c2 SET a = 10" 1
  1366. test_efkey_29 29 "UPDATE c3 SET a = 10" 1
  1367. test_efkey_29 30 "UPDATE c4 SET a = 10" 1
  1368. test_efkey_29 31 "UPDATE c5 SET a = 10" 1
  1369. test_efkey_29 31 "UPDATE c6 SET a = 10" 1
  1370. test_efkey_29 31 "UPDATE c7 SET a = 10" 0
  1371. test_efkey_29 32 "COMMIT" 1
  1372. test_efkey_29 33 "ROLLBACK" 0
  1373. #-------------------------------------------------------------------------
  1374. # EVIDENCE-OF: R-24499-57071
  1375. #
  1376. # Test an example from foreignkeys.html dealing with a deferred foreign
  1377. # key constraint.
  1378. #
  1379. do_test e_fkey-35.1 {
  1380. drop_all_tables
  1381. execsql {
  1382. CREATE TABLE artist(
  1383. artistid INTEGER PRIMARY KEY,
  1384. artistname TEXT
  1385. );
  1386. CREATE TABLE track(
  1387. trackid INTEGER,
  1388. trackname TEXT,
  1389. trackartist INTEGER REFERENCES artist(artistid) DEFERRABLE INITIALLY DEFERRED
  1390. );
  1391. }
  1392. } {}
  1393. do_test e_fkey-35.2 {
  1394. execsql {
  1395. BEGIN;
  1396. INSERT INTO track VALUES(1, 'White Christmas', 5);
  1397. }
  1398. catchsql COMMIT
  1399. } {1 {foreign key constraint failed}}
  1400. do_test e_fkey-35.3 {
  1401. execsql {
  1402. INSERT INTO artist VALUES(5, 'Bing Crosby');
  1403. COMMIT;
  1404. }
  1405. } {}
  1406. #-------------------------------------------------------------------------
  1407. # Verify that a nested savepoint may be released without satisfying
  1408. # deferred foreign key constraints.
  1409. #
  1410. # EVIDENCE-OF: R-07223-48323 A nested savepoint transaction may be
  1411. # RELEASEd while the database is in a state that does not satisfy a
  1412. # deferred foreign key constraint.
  1413. #
  1414. drop_all_tables
  1415. do_test e_fkey-36.1 {
  1416. execsql {
  1417. CREATE TABLE t1(a PRIMARY KEY,
  1418. b REFERENCES t1 DEFERRABLE INITIALLY DEFERRED
  1419. );
  1420. INSERT INTO t1 VALUES(1, 1);
  1421. INSERT INTO t1 VALUES(2, 2);
  1422. INSERT INTO t1 VALUES(3, 3);
  1423. }
  1424. } {}
  1425. do_test e_fkey-36.2 {
  1426. execsql {
  1427. BEGIN;
  1428. SAVEPOINT one;
  1429. INSERT INTO t1 VALUES(4, 5);
  1430. RELEASE one;
  1431. }
  1432. } {}
  1433. do_test e_fkey-36.3 {
  1434. catchsql COMMIT
  1435. } {1 {foreign key constraint failed}}
  1436. do_test e_fkey-36.4 {
  1437. execsql {
  1438. UPDATE t1 SET a = 5 WHERE a = 4;
  1439. COMMIT;
  1440. }
  1441. } {}
  1442. #-------------------------------------------------------------------------
  1443. # Check that a transaction savepoint (an outermost savepoint opened when
  1444. # the database was in auto-commit mode) cannot be released without
  1445. # satisfying deferred foreign key constraints. It may be rolled back.
  1446. #
  1447. # EVIDENCE-OF: R-44295-13823 A transaction savepoint (a non-nested
  1448. # savepoint that was opened while there was not currently an open
  1449. # transaction), on the other hand, is subject to the same restrictions
  1450. # as a COMMIT - attempting to RELEASE it while the database is in such a
  1451. # state will fail.
  1452. #
  1453. do_test e_fkey-37.1 {
  1454. execsql {
  1455. SAVEPOINT one;
  1456. SAVEPOINT two;
  1457. INSERT INTO t1 VALUES(6, 7);
  1458. RELEASE two;
  1459. }
  1460. } {}
  1461. do_test e_fkey-37.2 {
  1462. catchsql {RELEASE one}
  1463. } {1 {foreign key constraint failed}}
  1464. do_test e_fkey-37.3 {
  1465. execsql {
  1466. UPDATE t1 SET a = 7 WHERE a = 6;
  1467. RELEASE one;
  1468. }
  1469. } {}
  1470. do_test e_fkey-37.4 {
  1471. execsql {
  1472. SAVEPOINT one;
  1473. SAVEPOINT two;
  1474. INSERT INTO t1 VALUES(9, 10);
  1475. RELEASE two;
  1476. }
  1477. } {}
  1478. do_test e_fkey-37.5 {
  1479. catchsql {RELEASE one}
  1480. } {1 {foreign key constraint failed}}
  1481. do_test e_fkey-37.6 {
  1482. execsql {ROLLBACK TO one ; RELEASE one}
  1483. } {}
  1484. #-------------------------------------------------------------------------
  1485. # Test that if a COMMIT operation fails due to deferred foreign key
  1486. # constraints, any nested savepoints remain open.
  1487. #
  1488. # EVIDENCE-OF: R-37736-42616 If a COMMIT statement (or the RELEASE of a
  1489. # transaction SAVEPOINT) fails because the database is currently in a
  1490. # state that violates a deferred foreign key constraint and there are
  1491. # currently nested savepoints, the nested savepoints remain open.
  1492. #
  1493. do_test e_fkey-38.1 {
  1494. execsql {
  1495. DELETE FROM t1 WHERE a>3;
  1496. SELECT * FROM t1;
  1497. }
  1498. } {1 1 2 2 3 3}
  1499. do_test e_fkey-38.2 {
  1500. execsql {
  1501. BEGIN;
  1502. INSERT INTO t1 VALUES(4, 4);
  1503. SAVEPOINT one;
  1504. INSERT INTO t1 VALUES(5, 6);
  1505. SELECT * FROM t1;
  1506. }
  1507. } {1 1 2 2 3 3 4 4 5 6}
  1508. do_test e_fkey-38.3 {
  1509. catchsql COMMIT
  1510. } {1 {foreign key constraint failed}}
  1511. do_test e_fkey-38.4 {
  1512. execsql {
  1513. ROLLBACK TO one;
  1514. COMMIT;
  1515. SELECT * FROM t1;
  1516. }
  1517. } {1 1 2 2 3 3 4 4}
  1518. do_test e_fkey-38.5 {
  1519. execsql {
  1520. SAVEPOINT a;
  1521. INSERT INTO t1 VALUES(5, 5);
  1522. SAVEPOINT b;
  1523. INSERT INTO t1 VALUES(6, 7);
  1524. SAVEPOINT c;
  1525. INSERT INTO t1 VALUES(7, 8);
  1526. }
  1527. } {}
  1528. do_test e_fkey-38.6 {
  1529. catchsql {RELEASE a}
  1530. } {1 {foreign key constraint failed}}
  1531. do_test e_fkey-38.7 {
  1532. execsql {ROLLBACK TO c}
  1533. catchsql {RELEASE a}
  1534. } {1 {foreign key constraint failed}}
  1535. do_test e_fkey-38.8 {
  1536. execsql {
  1537. ROLLBACK TO b;
  1538. RELEASE a;
  1539. SELECT * FROM t1;
  1540. }
  1541. } {1 1 2 2 3 3 4 4 5 5}
  1542. ###########################################################################
  1543. ### SECTION 4.3: ON DELETE and ON UPDATE Actions
  1544. ###########################################################################
  1545. #-------------------------------------------------------------------------
  1546. # Test that configured ON DELETE and ON UPDATE actions take place when
  1547. # deleting or modifying rows of the parent table, respectively.
  1548. #
  1549. # EVIDENCE-OF: R-48270-44282 Foreign key ON DELETE and ON UPDATE clauses
  1550. # are used to configure actions that take place when deleting rows from
  1551. # the parent table (ON DELETE), or modifying the parent key values of
  1552. # existing rows (ON UPDATE).
  1553. #
  1554. # Test that a single FK constraint may have different actions configured
  1555. # for ON DELETE and ON UPDATE.
  1556. #
  1557. # EVIDENCE-OF: R-48124-63225 A single foreign key constraint may have
  1558. # different actions configured for ON DELETE and ON UPDATE.
  1559. #
  1560. do_test e_fkey-39.1 {
  1561. execsql {
  1562. CREATE TABLE p(a, b PRIMARY KEY, c);
  1563. CREATE TABLE c1(d, e, f DEFAULT 'k0' REFERENCES p
  1564. ON UPDATE SET DEFAULT
  1565. ON DELETE SET NULL
  1566. );
  1567. INSERT INTO p VALUES(0, 'k0', '');
  1568. INSERT INTO p VALUES(1, 'k1', 'I');
  1569. INSERT INTO p VALUES(2, 'k2', 'II');
  1570. INSERT INTO p VALUES(3, 'k3', 'III');
  1571. INSERT INTO c1 VALUES(1, 'xx', 'k1');
  1572. INSERT INTO c1 VALUES(2, 'xx', 'k2');
  1573. INSERT INTO c1 VALUES(3, 'xx', 'k3');
  1574. }
  1575. } {}
  1576. do_test e_fkey-39.2 {
  1577. execsql {
  1578. UPDATE p SET b = 'k4' WHERE a = 1;
  1579. SELECT * FROM c1;
  1580. }
  1581. } {1 xx k0 2 xx k2 3 xx k3}
  1582. do_test e_fkey-39.3 {
  1583. execsql {
  1584. DELETE FROM p WHERE a = 2;
  1585. SELECT * FROM c1;
  1586. }
  1587. } {1 xx k0 2 xx {} 3 xx k3}
  1588. do_test e_fkey-39.4 {
  1589. execsql {
  1590. CREATE UNIQUE INDEX pi ON p(c);
  1591. REPLACE INTO p VALUES(5, 'k5', 'III');
  1592. SELECT * FROM c1;
  1593. }
  1594. } {1 xx k0 2 xx {} 3 xx {}}
  1595. #-------------------------------------------------------------------------
  1596. # Each foreign key in the system has an ON UPDATE and ON DELETE action,
  1597. # either "NO ACTION", "RESTRICT", "SET NULL", "SET DEFAULT" or "CASCADE".
  1598. #
  1599. # EVIDENCE-OF: R-33326-45252 The ON DELETE and ON UPDATE action
  1600. # associated with each foreign key in an SQLite database is one of "NO
  1601. # ACTION", "RESTRICT", "SET NULL", "SET DEFAULT" or "CASCADE".
  1602. #
  1603. # If none is specified explicitly, "NO ACTION" is the default.
  1604. #
  1605. # EVIDENCE-OF: R-19803-45884 If an action is not explicitly specified,
  1606. # it defaults to "NO ACTION".
  1607. #
  1608. drop_all_tables
  1609. do_test e_fkey-40.1 {
  1610. execsql {
  1611. CREATE TABLE parent(x PRIMARY KEY, y);
  1612. CREATE TABLE child1(a,
  1613. b REFERENCES parent ON UPDATE NO ACTION ON DELETE RESTRICT
  1614. );
  1615. CREATE TABLE child2(a,
  1616. b REFERENCES parent ON UPDATE RESTRICT ON DELETE SET NULL
  1617. );
  1618. CREATE TABLE child3(a,
  1619. b REFERENCES parent ON UPDATE SET NULL ON DELETE SET DEFAULT
  1620. );
  1621. CREATE TABLE child4(a,
  1622. b REFERENCES parent ON UPDATE SET DEFAULT ON DELETE CASCADE
  1623. );
  1624. -- Create some foreign keys that use the default action - "NO ACTION"
  1625. CREATE TABLE child5(a, b REFERENCES parent ON UPDATE CASCADE);
  1626. CREATE TABLE child6(a, b REFERENCES parent ON DELETE RESTRICT);
  1627. CREATE TABLE child7(a, b REFERENCES parent ON DELETE NO ACTION);
  1628. CREATE TABLE child8(a, b REFERENCES parent ON UPDATE NO ACTION);
  1629. }
  1630. } {}
  1631. foreach {tn zTab lRes} {
  1632. 2 child1 {0 0 parent b {} {NO ACTION} RESTRICT NONE}
  1633. 3 child2 {0 0 parent b {} RESTRICT {SET NULL} NONE}
  1634. 4 child3 {0 0 parent b {} {SET NULL} {SET DEFAULT} NONE}
  1635. 5 child4 {0 0 parent b {} {SET DEFAULT} CASCADE NONE}
  1636. 6 child5 {0 0 parent b {} CASCADE {NO ACTION} NONE}
  1637. 7 child6 {0 0 parent b {} {NO ACTION} RESTRICT NONE}
  1638. 8 child7 {0 0 parent b {} {NO ACTION} {NO ACTION} NONE}
  1639. 9 child8 {0 0 parent b {} {NO ACTION} {NO ACTION} NONE}
  1640. } {
  1641. do_test e_fkey-40.$tn { execsql "PRAGMA foreign_key_list($zTab)" } $lRes
  1642. }
  1643. #-------------------------------------------------------------------------
  1644. # Test that "NO ACTION" means that nothing happens to a child row when
  1645. # it's parent row is updated or deleted.
  1646. #
  1647. # EVIDENCE-OF: R-19971-54976 Configuring "NO ACTION" means just that:
  1648. # when a parent key is modified or deleted from the database, no special
  1649. # action is taken.
  1650. #
  1651. drop_all_tables
  1652. do_test e_fkey-41.1 {
  1653. execsql {
  1654. CREATE TABLE parent(p1, p2, PRIMARY KEY(p1, p2));
  1655. CREATE TABLE child(c1, c2,
  1656. FOREIGN KEY(c1, c2) REFERENCES parent
  1657. ON UPDATE NO ACTION
  1658. ON DELETE NO ACTION
  1659. DEFERRABLE INITIALLY DEFERRED
  1660. );
  1661. INSERT INTO parent VALUES('j', 'k');
  1662. INSERT INTO parent VALUES('l', 'm');
  1663. INSERT INTO child VALUES('j', 'k');
  1664. INSERT INTO child VALUES('l', 'm');
  1665. }
  1666. } {}
  1667. do_test e_fkey-41.2 {
  1668. execsql {
  1669. BEGIN;
  1670. UPDATE parent SET p1='k' WHERE p1='j';
  1671. DELETE FROM parent WHERE p1='l';
  1672. SELECT * FROM child;
  1673. }
  1674. } {j k l m}
  1675. do_test e_fkey-41.3 {
  1676. catchsql COMMIT
  1677. } {1 {foreign key constraint failed}}
  1678. do_test e_fkey-41.4 {
  1679. execsql ROLLBACK
  1680. } {}
  1681. #-------------------------------------------------------------------------
  1682. # Test that "RESTRICT" means the application is prohibited from deleting
  1683. # or updating a parent table row when there exists one or more child keys
  1684. # mapped to it.
  1685. #
  1686. # EVIDENCE-OF: R-04272-38653 The "RESTRICT" action means that the
  1687. # application is prohibited from deleting (for ON DELETE RESTRICT) or
  1688. # modifying (for ON UPDATE RESTRICT) a parent key when there exists one
  1689. # or more child keys mapped to it.
  1690. #
  1691. drop_all_tables
  1692. do_test e_fkey-41.1 {
  1693. execsql {
  1694. CREATE TABLE parent(p1, p2);
  1695. CREATE UNIQUE INDEX parent_i ON parent(p1, p2);
  1696. CREATE TABLE child1(c1, c2,
  1697. FOREIGN KEY(c2, c1) REFERENCES parent(p1, p2) ON DELETE RESTRICT
  1698. );
  1699. CREATE TABLE child2(c1, c2,
  1700. FOREIGN KEY(c2, c1) REFERENCES parent(p1, p2) ON UPDATE RESTRICT
  1701. );
  1702. }
  1703. } {}
  1704. do_test e_fkey-41.2 {
  1705. execsql {
  1706. INSERT INTO parent VALUES('a', 'b');
  1707. INSERT INTO parent VALUES('c', 'd');
  1708. INSERT INTO child1 VALUES('b', 'a');
  1709. INSERT INTO child2 VALUES('d', 'c');
  1710. }
  1711. } {}
  1712. do_test e_fkey-41.3 {
  1713. catchsql { DELETE FROM parent WHERE p1 = 'a' }
  1714. } {1 {foreign key constraint failed}}
  1715. do_test e_fkey-41.4 {
  1716. catchsql { UPDATE parent SET p2 = 'e' WHERE p1 = 'c' }
  1717. } {1 {foreign key constraint failed}}
  1718. #-------------------------------------------------------------------------
  1719. # Test that RESTRICT is slightly different from NO ACTION for IMMEDIATE
  1720. # constraints, in that it is enforced immediately, not at the end of the
  1721. # statement.
  1722. #
  1723. # EVIDENCE-OF: R-37997-42187 The difference between the effect of a
  1724. # RESTRICT action and normal foreign key constraint enforcement is that
  1725. # the RESTRICT action processing happens as soon as the field is updated
  1726. # - not at the end of the current statement as it would with an
  1727. # immediate constraint, or at the end of the current transaction as it
  1728. # would with a deferred constraint.
  1729. #
  1730. drop_all_tables
  1731. do_test e_fkey-42.1 {
  1732. execsql {
  1733. CREATE TABLE parent(x PRIMARY KEY);
  1734. CREATE TABLE child1(c REFERENCES parent ON UPDATE RESTRICT);
  1735. CREATE TABLE child2(c REFERENCES parent ON UPDATE NO ACTION);
  1736. INSERT INTO parent VALUES('key1');
  1737. INSERT INTO parent VALUES('key2');
  1738. INSERT INTO child1 VALUES('key1');
  1739. INSERT INTO child2 VALUES('key2');
  1740. CREATE TRIGGER parent_t AFTER UPDATE ON parent BEGIN
  1741. UPDATE child1 set c = new.x WHERE c = old.x;
  1742. UPDATE child2 set c = new.x WHERE c = old.x;
  1743. END;
  1744. }
  1745. } {}
  1746. do_test e_fkey-42.2 {
  1747. catchsql { UPDATE parent SET x = 'key one' WHERE x = 'key1' }
  1748. } {1 {foreign key constraint failed}}
  1749. do_test e_fkey-42.3 {
  1750. execsql {
  1751. UPDATE parent SET x = 'key two' WHERE x = 'key2';
  1752. SELECT * FROM child2;
  1753. }
  1754. } {{key two}}
  1755. drop_all_tables
  1756. do_test e_fkey-42.4 {
  1757. execsql {
  1758. CREATE TABLE parent(x PRIMARY KEY);
  1759. CREATE TABLE child1(c REFERENCES parent ON DELETE RESTRICT);
  1760. CREATE TABLE child2(c REFERENCES parent ON DELETE NO ACTION);
  1761. INSERT INTO parent VALUES('key1');
  1762. INSERT INTO parent VALUES('key2');
  1763. INSERT INTO child1 VALUES('key1');
  1764. INSERT INTO child2 VALUES('key2');
  1765. CREATE TRIGGER parent_t AFTER DELETE ON parent BEGIN
  1766. UPDATE child1 SET c = NULL WHERE c = old.x;
  1767. UPDATE child2 SET c = NULL WHERE c = old.x;
  1768. END;
  1769. }
  1770. } {}
  1771. do_test e_fkey-42.5 {
  1772. catchsql { DELETE FROM parent WHERE x = 'key1' }
  1773. } {1 {foreign key constraint failed}}
  1774. do_test e_fkey-42.6 {
  1775. execsql {
  1776. DELETE FROM parent WHERE x = 'key2';
  1777. SELECT * FROM child2;
  1778. }
  1779. } {{}}
  1780. drop_all_tables
  1781. do_test e_fkey-42.7 {
  1782. execsql {
  1783. CREATE TABLE parent(x PRIMARY KEY);
  1784. CREATE TABLE child1(c REFERENCES parent ON DELETE RESTRICT);
  1785. CREATE TABLE child2(c REFERENCES parent ON DELETE NO ACTION);
  1786. INSERT INTO parent VALUES('key1');
  1787. INSERT INTO parent VALUES('key2');
  1788. INSERT INTO child1 VALUES('key1');
  1789. INSERT INTO child2 VALUES('key2');
  1790. }
  1791. } {}
  1792. do_test e_fkey-42.8 {
  1793. catchsql { REPLACE INTO parent VALUES('key1') }
  1794. } {1 {foreign key constraint failed}}
  1795. do_test e_fkey-42.9 {
  1796. execsql {
  1797. REPLACE INTO parent VALUES('key2');
  1798. SELECT * FROM child2;
  1799. }
  1800. } {key2}
  1801. #-------------------------------------------------------------------------
  1802. # Test that RESTRICT is enforced immediately, even for a DEFERRED constraint.
  1803. #
  1804. # EVIDENCE-OF: R-24179-60523 Even if the foreign key constraint it is
  1805. # attached to is deferred, configuring a RESTRICT action causes SQLite
  1806. # to return an error immediately if a parent key with dependent child
  1807. # keys is deleted or modified.
  1808. #
  1809. drop_all_tables
  1810. do_test e_fkey-43.1 {
  1811. execsql {
  1812. CREATE TABLE parent(x PRIMARY KEY);
  1813. CREATE TABLE child1(c REFERENCES parent ON UPDATE RESTRICT
  1814. DEFERRABLE INITIALLY DEFERRED
  1815. );
  1816. CREATE TABLE child2(c REFERENCES parent ON UPDATE NO ACTION
  1817. DEFERRABLE INITIALLY DEFERRED
  1818. );
  1819. INSERT INTO parent VALUES('key1');
  1820. INSERT INTO parent VALUES('key2');
  1821. INSERT INTO child1 VALUES('key1');
  1822. INSERT INTO child2 VALUES('key2');
  1823. BEGIN;
  1824. }
  1825. } {}
  1826. do_test e_fkey-43.2 {
  1827. catchsql { UPDATE parent SET x = 'key one' WHERE x = 'key1' }
  1828. } {1 {foreign key constraint failed}}
  1829. do_test e_fkey-43.3 {
  1830. execsql { UPDATE parent SET x = 'key two' WHERE x = 'key2' }
  1831. } {}
  1832. do_test e_fkey-43.4 {
  1833. catchsql COMMIT
  1834. } {1 {foreign key constraint failed}}
  1835. do_test e_fkey-43.5 {
  1836. execsql {
  1837. UPDATE child2 SET c = 'key two';
  1838. COMMIT;
  1839. }
  1840. } {}
  1841. drop_all_tables
  1842. do_test e_fkey-43.6 {
  1843. execsql {
  1844. CREATE TABLE parent(x PRIMARY KEY);
  1845. CREATE TABLE child1(c REFERENCES parent ON DELETE RESTRICT
  1846. DEFERRABLE INITIALLY DEFERRED
  1847. );
  1848. CREATE TABLE child2(c REFERENCES parent ON DELETE NO ACTION
  1849. DEFERRABLE INITIALLY DEFERRED
  1850. );
  1851. INSERT INTO parent VALUES('key1');
  1852. INSERT INTO parent VALUES('key2');
  1853. INSERT INTO child1 VALUES('key1');
  1854. INSERT INTO child2 VALUES('key2');
  1855. BEGIN;
  1856. }
  1857. } {}
  1858. do_test e_fkey-43.7 {
  1859. catchsql { DELETE FROM parent WHERE x = 'key1' }
  1860. } {1 {foreign key constraint failed}}
  1861. do_test e_fkey-43.8 {
  1862. execsql { DELETE FROM parent WHERE x = 'key2' }
  1863. } {}
  1864. do_test e_fkey-43.9 {
  1865. catchsql COMMIT
  1866. } {1 {foreign key constraint failed}}
  1867. do_test e_fkey-43.10 {
  1868. execsql {
  1869. UPDATE child2 SET c = NULL;
  1870. COMMIT;
  1871. }
  1872. } {}
  1873. #-------------------------------------------------------------------------
  1874. # Test SET NULL actions.
  1875. #
  1876. # EVIDENCE-OF: R-03353-05327 If the configured action is "SET NULL",
  1877. # then when a parent key is deleted (for ON DELETE SET NULL) or modified
  1878. # (for ON UPDATE SET NULL), the child key columns of all rows in the
  1879. # child table that mapped to the parent key are set to contain SQL NULL
  1880. # values.
  1881. #
  1882. drop_all_tables
  1883. do_test e_fkey-44.1 {
  1884. execsql {
  1885. CREATE TABLE pA(x PRIMARY KEY);
  1886. CREATE TABLE cA(c REFERENCES pA ON DELETE SET NULL);
  1887. CREATE TABLE cB(c REFERENCES pA ON UPDATE SET NULL);
  1888. INSERT INTO pA VALUES(X'ABCD');
  1889. INSERT INTO pA VALUES(X'1234');
  1890. INSERT INTO cA VALUES(X'ABCD');
  1891. INSERT INTO cB VALUES(X'1234');
  1892. }
  1893. } {}
  1894. do_test e_fkey-44.2 {
  1895. execsql {
  1896. DELETE FROM pA WHERE rowid = 1;
  1897. SELECT quote(x) FROM pA;
  1898. }
  1899. } {X'1234'}
  1900. do_test e_fkey-44.3 {
  1901. execsql {
  1902. SELECT quote(c) FROM cA;
  1903. }
  1904. } {NULL}
  1905. do_test e_fkey-44.4 {
  1906. execsql {
  1907. UPDATE pA SET x = X'8765' WHERE rowid = 2;
  1908. SELECT quote(x) FROM pA;
  1909. }
  1910. } {X'8765'}
  1911. do_test e_fkey-44.5 {
  1912. execsql { SELECT quote(c) FROM cB }
  1913. } {NULL}
  1914. #-------------------------------------------------------------------------
  1915. # Test SET DEFAULT actions.
  1916. #
  1917. # EVIDENCE-OF: R-43054-54832 The "SET DEFAULT" actions are similar to
  1918. # "SET NULL", except that each of the child key columns is set to
  1919. # contain the columns default value instead of NULL.
  1920. #
  1921. drop_all_tables
  1922. do_test e_fkey-45.1 {
  1923. execsql {
  1924. CREATE TABLE pA(x PRIMARY KEY);
  1925. CREATE TABLE cA(c DEFAULT X'0000' REFERENCES pA ON DELETE SET DEFAULT);
  1926. CREATE TABLE cB(c DEFAULT X'9999' REFERENCES pA ON UPDATE SET DEFAULT);
  1927. INSERT INTO pA(rowid, x) VALUES(1, X'0000');
  1928. INSERT INTO pA(rowid, x) VALUES(2, X'9999');
  1929. INSERT INTO pA(rowid, x) VALUES(3, X'ABCD');
  1930. INSERT INTO pA(rowid, x) VALUES(4, X'1234');
  1931. INSERT INTO cA VALUES(X'ABCD');
  1932. INSERT INTO cB VALUES(X'1234');
  1933. }
  1934. } {}
  1935. do_test e_fkey-45.2 {
  1936. execsql {
  1937. DELETE FROM pA WHERE rowid = 3;
  1938. SELECT quote(x) FROM pA ORDER BY rowid;
  1939. }
  1940. } {X'0000' X'9999' X'1234'}
  1941. do_test e_fkey-45.3 {
  1942. execsql { SELECT quote(c) FROM cA }
  1943. } {X'0000'}
  1944. do_test e_fkey-45.4 {
  1945. execsql {
  1946. UPDATE pA SET x = X'8765' WHERE rowid = 4;
  1947. SELECT quote(x) FROM pA ORDER BY rowid;
  1948. }
  1949. } {X'0000' X'9999' X'8765'}
  1950. do_test e_fkey-45.5 {
  1951. execsql { SELECT quote(c) FROM cB }
  1952. } {X'9999'}
  1953. #-------------------------------------------------------------------------
  1954. # Test ON DELETE CASCADE actions.
  1955. #
  1956. # EVIDENCE-OF: R-61376-57267 A "CASCADE" action propagates the delete or
  1957. # update operation on the parent key to each dependent child key.
  1958. #
  1959. # EVIDENCE-OF: R-61809-62207 For an "ON DELETE CASCADE" action, this
  1960. # means that each row in the child table that was associated with the
  1961. # deleted parent row is also deleted.
  1962. #
  1963. drop_all_tables
  1964. do_test e_fkey-46.1 {
  1965. execsql {
  1966. CREATE TABLE p1(a, b UNIQUE);
  1967. CREATE TABLE c1(c REFERENCES p1(b) ON DELETE CASCADE, d);
  1968. INSERT INTO p1 VALUES(NULL, NULL);
  1969. INSERT INTO p1 VALUES(4, 4);
  1970. INSERT INTO p1 VALUES(5, 5);
  1971. INSERT INTO c1 VALUES(NULL, NULL);
  1972. INSERT INTO c1 VALUES(4, 4);
  1973. INSERT INTO c1 VALUES(5, 5);
  1974. SELECT count(*) FROM c1;
  1975. }
  1976. } {3}
  1977. do_test e_fkey-46.2 {
  1978. execsql {
  1979. DELETE FROM p1 WHERE a = 4;
  1980. SELECT d, c FROM c1;
  1981. }
  1982. } {{} {} 5 5}
  1983. do_test e_fkey-46.3 {
  1984. execsql {
  1985. DELETE FROM p1;
  1986. SELECT d, c FROM c1;
  1987. }
  1988. } {{} {}}
  1989. do_test e_fkey-46.4 {
  1990. execsql { SELECT * FROM p1 }
  1991. } {}
  1992. #-------------------------------------------------------------------------
  1993. # Test ON UPDATE CASCADE actions.
  1994. #
  1995. # EVIDENCE-OF: R-13877-64542 For an "ON UPDATE CASCADE" action, it means
  1996. # that the values stored in each dependent child key are modified to
  1997. # match the new parent key values.
  1998. #
  1999. # EVIDENCE-OF: R-61376-57267 A "CASCADE" action propagates the delete or
  2000. # update operation on the parent key to each dependent child key.
  2001. #
  2002. drop_all_tables
  2003. do_test e_fkey-47.1 {
  2004. execsql {
  2005. CREATE TABLE p1(a, b UNIQUE);
  2006. CREATE TABLE c1(c REFERENCES p1(b) ON UPDATE CASCADE, d);
  2007. INSERT INTO p1 VALUES(NULL, NULL);
  2008. INSERT INTO p1 VALUES(4, 4);
  2009. INSERT INTO p1 VALUES(5, 5);
  2010. INSERT INTO c1 VALUES(NULL, NULL);
  2011. INSERT INTO c1 VALUES(4, 4);
  2012. INSERT INTO c1 VALUES(5, 5);
  2013. SELECT count(*) FROM c1;
  2014. }
  2015. } {3}
  2016. do_test e_fkey-47.2 {
  2017. execsql {
  2018. UPDATE p1 SET b = 10 WHERE b = 5;
  2019. SELECT d, c FROM c1;
  2020. }
  2021. } {{} {} 4 4 5 10}
  2022. do_test e_fkey-47.3 {
  2023. execsql {
  2024. UPDATE p1 SET b = 11 WHERE b = 4;
  2025. SELECT d, c FROM c1;
  2026. }
  2027. } {{} {} 4 11 5 10}
  2028. do_test e_fkey-47.4 {
  2029. execsql {
  2030. UPDATE p1 SET b = 6 WHERE b IS NULL;
  2031. SELECT d, c FROM c1;
  2032. }
  2033. } {{} {} 4 11 5 10}
  2034. do_test e_fkey-46.5 {
  2035. execsql { SELECT * FROM p1 }
  2036. } {{} 6 4 11 5 10}
  2037. #-------------------------------------------------------------------------
  2038. # EVIDENCE-OF: R-65058-57158
  2039. #
  2040. # Test an example from the "ON DELETE and ON UPDATE Actions" section
  2041. # of foreignkeys.html.
  2042. #
  2043. drop_all_tables
  2044. do_test e_fkey-48.1 {
  2045. execsql {
  2046. CREATE TABLE artist(
  2047. artistid INTEGER PRIMARY KEY,
  2048. artistname TEXT
  2049. );
  2050. CREATE TABLE track(
  2051. trackid INTEGER,
  2052. trackname TEXT,
  2053. trackartist INTEGER REFERENCES artist(artistid) ON UPDATE CASCADE
  2054. );
  2055. INSERT INTO artist VALUES(1, 'Dean Martin');
  2056. INSERT INTO artist VALUES(2, 'Frank Sinatra');
  2057. INSERT INTO track VALUES(11, 'That''s Amore', 1);
  2058. INSERT INTO track VALUES(12, 'Christmas Blues', 1);
  2059. INSERT INTO track VALUES(13, 'My Way', 2);
  2060. }
  2061. } {}
  2062. do_test e_fkey-48.2 {
  2063. execsql {
  2064. UPDATE artist SET artistid = 100 WHERE artistname = 'Dean Martin';
  2065. }
  2066. } {}
  2067. do_test e_fkey-48.3 {
  2068. execsql { SELECT * FROM artist }
  2069. } {2 {Frank Sinatra} 100 {Dean Martin}}
  2070. do_test e_fkey-48.4 {
  2071. execsql { SELECT * FROM track }
  2072. } {11 {That's Amore} 100 12 {Christmas Blues} 100 13 {My Way} 2}
  2073. #-------------------------------------------------------------------------
  2074. # Verify that adding an FK action does not absolve the user of the
  2075. # requirement not to violate the foreign key constraint.
  2076. #
  2077. # EVIDENCE-OF: R-53968-51642 Configuring an ON UPDATE or ON DELETE
  2078. # action does not mean that the foreign key constraint does not need to
  2079. # be satisfied.
  2080. #
  2081. drop_all_tables
  2082. do_test e_fkey-49.1 {
  2083. execsql {
  2084. CREATE TABLE parent(a COLLATE nocase, b, c, PRIMARY KEY(c, a));
  2085. CREATE TABLE child(d DEFAULT 'a', e, f DEFAULT 'c',
  2086. FOREIGN KEY(f, d) REFERENCES parent ON UPDATE SET DEFAULT
  2087. );
  2088. INSERT INTO parent VALUES('A', 'b', 'c');
  2089. INSERT INTO parent VALUES('ONE', 'two', 'three');
  2090. INSERT INTO child VALUES('one', 'two', 'three');
  2091. }
  2092. } {}
  2093. do_test e_fkey-49.2 {
  2094. execsql {
  2095. BEGIN;
  2096. UPDATE parent SET a = '' WHERE a = 'oNe';
  2097. SELECT * FROM child;
  2098. }
  2099. } {a two c}
  2100. do_test e_fkey-49.3 {
  2101. execsql {
  2102. ROLLBACK;
  2103. DELETE FROM parent WHERE a = 'A';
  2104. SELECT * FROM parent;
  2105. }
  2106. } {ONE two three}
  2107. do_test e_fkey-49.4 {
  2108. catchsql { UPDATE parent SET a = '' WHERE a = 'oNe' }
  2109. } {1 {foreign key constraint failed}}
  2110. #-------------------------------------------------------------------------
  2111. # EVIDENCE-OF: R-11856-19836
  2112. #
  2113. # Test an example from the "ON DELETE and ON UPDATE Actions" section
  2114. # of foreignkeys.html. This example shows that adding an "ON DELETE DEFAULT"
  2115. # clause does not abrogate the need to satisfy the foreign key constraint
  2116. # (R-28220-46694).
  2117. #
  2118. # EVIDENCE-OF: R-28220-46694 For example, if an "ON DELETE SET DEFAULT"
  2119. # action is configured, but there is no row in the parent table that
  2120. # corresponds to the default values of the child key columns, deleting a
  2121. # parent key while dependent child keys exist still causes a foreign key
  2122. # violation.
  2123. #
  2124. drop_all_tables
  2125. do_test e_fkey-50.1 {
  2126. execsql {
  2127. CREATE TABLE artist(
  2128. artistid INTEGER PRIMARY KEY,
  2129. artistname TEXT
  2130. );
  2131. CREATE TABLE track(
  2132. trackid INTEGER,
  2133. trackname TEXT,
  2134. trackartist INTEGER DEFAULT 0 REFERENCES artist(artistid) ON DELETE SET DEFAULT
  2135. );
  2136. INSERT INTO artist VALUES(3, 'Sammy Davis Jr.');
  2137. INSERT INTO track VALUES(14, 'Mr. Bojangles', 3);
  2138. }
  2139. } {}
  2140. do_test e_fkey-50.2 {
  2141. catchsql { DELETE FROM artist WHERE artistname = 'Sammy Davis Jr.' }
  2142. } {1 {foreign key constraint failed}}
  2143. do_test e_fkey-50.3 {
  2144. execsql {
  2145. INSERT INTO artist VALUES(0, 'Unknown Artist');
  2146. DELETE FROM artist WHERE artistname = 'Sammy Davis Jr.';
  2147. }
  2148. } {}
  2149. do_test e_fkey-50.4 {
  2150. execsql { SELECT * FROM artist }
  2151. } {0 {Unknown Artist}}
  2152. do_test e_fkey-50.5 {
  2153. execsql { SELECT * FROM track }
  2154. } {14 {Mr. Bojangles} 0}
  2155. #-------------------------------------------------------------------------
  2156. # EVIDENCE-OF: R-09564-22170
  2157. #
  2158. # Check that the order of steps in an UPDATE or DELETE on a parent
  2159. # table is as follows:
  2160. #
  2161. # 1. Execute applicable BEFORE trigger programs,
  2162. # 2. Check local (non foreign key) constraints,
  2163. # 3. Update or delete the row in the parent table,
  2164. # 4. Perform any required foreign key actions,
  2165. # 5. Execute applicable AFTER trigger programs.
  2166. #
  2167. drop_all_tables
  2168. do_test e_fkey-51.1 {
  2169. proc maxparent {args} { db one {SELECT max(x) FROM parent} }
  2170. db func maxparent maxparent
  2171. execsql {
  2172. CREATE TABLE parent(x PRIMARY KEY);
  2173. CREATE TRIGGER bu BEFORE UPDATE ON parent BEGIN
  2174. INSERT INTO parent VALUES(new.x-old.x);
  2175. END;
  2176. CREATE TABLE child(
  2177. a DEFAULT (maxparent()) REFERENCES parent ON UPDATE SET DEFAULT
  2178. );
  2179. CREATE TRIGGER au AFTER UPDATE ON parent BEGIN
  2180. INSERT INTO parent VALUES(new.x+old.x);
  2181. END;
  2182. INSERT INTO parent VALUES(1);
  2183. INSERT INTO child VALUES(1);
  2184. }
  2185. } {}
  2186. do_test e_fkey-51.2 {
  2187. execsql {
  2188. UPDATE parent SET x = 22;
  2189. SELECT * FROM parent ORDER BY rowid; SELECT 'xxx' ; SELECT a FROM child;
  2190. }
  2191. } {22 21 23 xxx 22}
  2192. do_test e_fkey-51.3 {
  2193. execsql {
  2194. DELETE FROM child;
  2195. DELETE FROM parent;
  2196. INSERT INTO parent VALUES(-1);
  2197. INSERT INTO child VALUES(-1);
  2198. UPDATE parent SET x = 22;
  2199. SELECT * FROM parent ORDER BY rowid; SELECT 'xxx' ; SELECT a FROM child;
  2200. }
  2201. } {22 23 21 xxx 23}
  2202. #-------------------------------------------------------------------------
  2203. # Verify that ON UPDATE actions only actually take place if the parent key
  2204. # is set to a new value that is distinct from the old value. The default
  2205. # collation sequence and affinity are used to determine if the new value
  2206. # is 'distinct' from the old or not.
  2207. #
  2208. # EVIDENCE-OF: R-27383-10246 An ON UPDATE action is only taken if the
  2209. # values of the parent key are modified so that the new parent key
  2210. # values are not equal to the old.
  2211. #
  2212. drop_all_tables
  2213. do_test e_fkey-52.1 {
  2214. execsql {
  2215. CREATE TABLE zeus(a INTEGER COLLATE NOCASE, b, PRIMARY KEY(a, b));
  2216. CREATE TABLE apollo(c, d,
  2217. FOREIGN KEY(c, d) REFERENCES zeus ON UPDATE CASCADE
  2218. );
  2219. INSERT INTO zeus VALUES('abc', 'xyz');
  2220. INSERT INTO apollo VALUES('ABC', 'xyz');
  2221. }
  2222. execsql {
  2223. UPDATE zeus SET a = 'aBc';
  2224. SELECT * FROM apollo;
  2225. }
  2226. } {ABC xyz}
  2227. do_test e_fkey-52.2 {
  2228. execsql {
  2229. UPDATE zeus SET a = 1, b = 1;
  2230. SELECT * FROM apollo;
  2231. }
  2232. } {1 1}
  2233. do_test e_fkey-52.3 {
  2234. execsql {
  2235. UPDATE zeus SET a = 1, b = 1;
  2236. SELECT typeof(c), c, typeof(d), d FROM apollo;
  2237. }
  2238. } {integer 1 integer 1}
  2239. do_test e_fkey-52.4 {
  2240. execsql {
  2241. UPDATE zeus SET a = '1';
  2242. SELECT typeof(c), c, typeof(d), d FROM apollo;
  2243. }
  2244. } {integer 1 integer 1}
  2245. do_test e_fkey-52.5 {
  2246. execsql {
  2247. UPDATE zeus SET b = '1';
  2248. SELECT typeof(c), c, typeof(d), d FROM apollo;
  2249. }
  2250. } {integer 1 text 1}
  2251. do_test e_fkey-52.6 {
  2252. execsql {
  2253. UPDATE zeus SET b = NULL;
  2254. SELECT typeof(c), c, typeof(d), d FROM apollo;
  2255. }
  2256. } {integer 1 null {}}
  2257. #-------------------------------------------------------------------------
  2258. # EVIDENCE-OF: R-35129-58141
  2259. #
  2260. # Test an example from the "ON DELETE and ON UPDATE Actions" section
  2261. # of foreignkeys.html. This example demonstrates that ON UPDATE actions
  2262. # only take place if at least one parent key column is set to a value
  2263. # that is distinct from its previous value.
  2264. #
  2265. drop_all_tables
  2266. do_test e_fkey-53.1 {
  2267. execsql {
  2268. CREATE TABLE parent(x PRIMARY KEY);
  2269. CREATE TABLE child(y REFERENCES parent ON UPDATE SET NULL);
  2270. INSERT INTO parent VALUES('key');
  2271. INSERT INTO child VALUES('key');
  2272. }
  2273. } {}
  2274. do_test e_fkey-53.2 {
  2275. execsql {
  2276. UPDATE parent SET x = 'key';
  2277. SELECT IFNULL(y, 'null') FROM child;
  2278. }
  2279. } {key}
  2280. do_test e_fkey-53.3 {
  2281. execsql {
  2282. UPDATE parent SET x = 'key2';
  2283. SELECT IFNULL(y, 'null') FROM child;
  2284. }
  2285. } {null}
  2286. ###########################################################################
  2287. ### SECTION 5: CREATE, ALTER and DROP TABLE commands
  2288. ###########################################################################
  2289. #-------------------------------------------------------------------------
  2290. # Test that parent keys are not checked when tables are created.
  2291. #
  2292. # EVIDENCE-OF: R-36018-21755 The parent key definitions of foreign key
  2293. # constraints are not checked when a table is created.
  2294. #
  2295. # EVIDENCE-OF: R-25384-39337 There is nothing stopping the user from
  2296. # creating a foreign key definition that refers to a parent table that
  2297. # does not exist, or to parent key columns that do not exist or are not
  2298. # collectively bound by a PRIMARY KEY or UNIQUE constraint.
  2299. #
  2300. # Child keys are checked to ensure all component columns exist. If parent
  2301. # key columns are explicitly specified, SQLite checks to make sure there
  2302. # are the same number of columns in the child and parent keys. (TODO: This
  2303. # is tested but does not correspond to any testable statement.)
  2304. #
  2305. # Also test that the above statements are true regardless of whether or not
  2306. # foreign keys are enabled: "A CREATE TABLE command operates the same whether
  2307. # or not foreign key constraints are enabled."
  2308. #
  2309. # EVIDENCE-OF: R-08908-23439 A CREATE TABLE command operates the same
  2310. # whether or not foreign key constraints are enabled.
  2311. #
  2312. foreach {tn zCreateTbl lRes} {
  2313. 1 "CREATE TABLE t1(a, b REFERENCES t1)" {0 {}}
  2314. 2 "CREATE TABLE t1(a, b REFERENCES t2)" {0 {}}
  2315. 3 "CREATE TABLE t1(a, b, FOREIGN KEY(a,b) REFERENCES t1)" {0 {}}
  2316. 4 "CREATE TABLE t1(a, b, FOREIGN KEY(a,b) REFERENCES t2)" {0 {}}
  2317. 5 "CREATE TABLE t1(a, b, FOREIGN KEY(a,b) REFERENCES t2)" {0 {}}
  2318. 6 "CREATE TABLE t1(a, b, FOREIGN KEY(a,b) REFERENCES t2(n,d))" {0 {}}
  2319. 7 "CREATE TABLE t1(a, b, FOREIGN KEY(a,b) REFERENCES t1(a,b))" {0 {}}
  2320. A "CREATE TABLE t1(a, b, FOREIGN KEY(c,b) REFERENCES t2)"
  2321. {1 {unknown column "c" in foreign key definition}}
  2322. B "CREATE TABLE t1(a, b, FOREIGN KEY(c,b) REFERENCES t2(d))"
  2323. {1 {number of columns in foreign key does not match the number of columns in the referenced table}}
  2324. } {
  2325. do_test e_fkey-54.$tn.off {
  2326. drop_all_tables
  2327. execsql {PRAGMA foreign_keys = OFF}
  2328. catchsql $zCreateTbl
  2329. } $lRes
  2330. do_test e_fkey-54.$tn.on {
  2331. drop_all_tables
  2332. execsql {PRAGMA foreign_keys = ON}
  2333. catchsql $zCreateTbl
  2334. } $lRes
  2335. }
  2336. #-------------------------------------------------------------------------
  2337. # EVIDENCE-OF: R-47952-62498 It is not possible to use the "ALTER TABLE
  2338. # ... ADD COLUMN" syntax to add a column that includes a REFERENCES
  2339. # clause, unless the default value of the new column is NULL. Attempting
  2340. # to do so returns an error.
  2341. #
  2342. proc test_efkey_6 {tn zAlter isError} {
  2343. drop_all_tables
  2344. do_test e_fkey-56.$tn.1 "
  2345. execsql { CREATE TABLE tbl(a, b) }
  2346. [list catchsql $zAlter]
  2347. " [lindex {{0 {}} {1 {Cannot add a REFERENCES column with non-NULL default value}}} $isError]
  2348. }
  2349. test_efkey_6 1 "ALTER TABLE tbl ADD COLUMN c REFERENCES xx" 0
  2350. test_efkey_6 2 "ALTER TABLE tbl ADD COLUMN c DEFAULT NULL REFERENCES xx" 0
  2351. test_efkey_6 3 "ALTER TABLE tbl ADD COLUMN c DEFAULT 0 REFERENCES xx" 1
  2352. #-------------------------------------------------------------------------
  2353. # Test that ALTER TABLE adjusts REFERENCES clauses when the parent table
  2354. # is RENAMED.
  2355. #
  2356. # EVIDENCE-OF: R-47080-02069 If an "ALTER TABLE ... RENAME TO" command
  2357. # is used to rename a table that is the parent table of one or more
  2358. # foreign key constraints, the definitions of the foreign key
  2359. # constraints are modified to refer to the parent table by its new name
  2360. #
  2361. # Test that these adjustments are visible in the sqlite_master table.
  2362. #
  2363. # EVIDENCE-OF: R-63827-54774 The text of the child CREATE TABLE
  2364. # statement or statements stored in the sqlite_master table are modified
  2365. # to reflect the new parent table name.
  2366. #
  2367. do_test e_fkey-56.1 {
  2368. drop_all_tables
  2369. execsql {
  2370. CREATE TABLE 'p 1 "parent one"'(a REFERENCES 'p 1 "parent one"', b, PRIMARY KEY(b));
  2371. CREATE TABLE c1(c, d REFERENCES 'p 1 "parent one"' ON UPDATE CASCADE);
  2372. CREATE TABLE c2(e, f, FOREIGN KEY(f) REFERENCES 'p 1 "parent one"' ON UPDATE CASCADE);
  2373. CREATE TABLE c3(e, 'f col 2', FOREIGN KEY('f col 2') REFERENCES 'p 1 "parent one"' ON UPDATE CASCADE);
  2374. INSERT INTO 'p 1 "parent one"' VALUES(1, 1);
  2375. INSERT INTO c1 VALUES(1, 1);
  2376. INSERT INTO c2 VALUES(1, 1);
  2377. INSERT INTO c3 VALUES(1, 1);
  2378. -- CREATE TABLE q(a, b, PRIMARY KEY(b));
  2379. }
  2380. } {}
  2381. do_test e_fkey-56.2 {
  2382. execsql { ALTER TABLE 'p 1 "parent one"' RENAME TO p }
  2383. } {}
  2384. do_test e_fkey-56.3 {
  2385. execsql {
  2386. UPDATE p SET a = 'xxx', b = 'xxx';
  2387. SELECT * FROM p;
  2388. SELECT * FROM c1;
  2389. SELECT * FROM c2;
  2390. SELECT * FROM c3;
  2391. }
  2392. } {xxx xxx 1 xxx 1 xxx 1 xxx}
  2393. do_test e_fkey-56.4 {
  2394. execsql { SELECT sql FROM sqlite_master WHERE type = 'table'}
  2395. } [list \
  2396. {CREATE TABLE "p"(a REFERENCES "p", b, PRIMARY KEY(b))} \
  2397. {CREATE TABLE c1(c, d REFERENCES "p" ON UPDATE CASCADE)} \
  2398. {CREATE TABLE c2(e, f, FOREIGN KEY(f) REFERENCES "p" ON UPDATE CASCADE)} \
  2399. {CREATE TABLE c3(e, 'f col 2', FOREIGN KEY('f col 2') REFERENCES "p" ON UPDATE CASCADE)} \
  2400. ]
  2401. #-------------------------------------------------------------------------
  2402. # Check that a DROP TABLE does an implicit DELETE FROM. Which does not
  2403. # cause any triggers to fire, but does fire foreign key actions.
  2404. #
  2405. # EVIDENCE-OF: R-14208-23986 If foreign key constraints are enabled when
  2406. # it is prepared, the DROP TABLE command performs an implicit DELETE to
  2407. # remove all rows from the table before dropping it.
  2408. #
  2409. # EVIDENCE-OF: R-11078-03945 The implicit DELETE does not cause any SQL
  2410. # triggers to fire, but may invoke foreign key actions or constraint
  2411. # violations.
  2412. #
  2413. do_test e_fkey-57.1 {
  2414. drop_all_tables
  2415. execsql {
  2416. CREATE TABLE p(a, b, PRIMARY KEY(a, b));
  2417. CREATE TABLE c1(c, d, FOREIGN KEY(c, d) REFERENCES p ON DELETE SET NULL);
  2418. CREATE TABLE c2(c, d, FOREIGN KEY(c, d) REFERENCES p ON DELETE SET DEFAULT);
  2419. CREATE TABLE c3(c, d, FOREIGN KEY(c, d) REFERENCES p ON DELETE CASCADE);
  2420. CREATE TABLE c4(c, d, FOREIGN KEY(c, d) REFERENCES p ON DELETE RESTRICT);
  2421. CREATE TABLE c5(c, d, FOREIGN KEY(c, d) REFERENCES p ON DELETE NO ACTION);
  2422. CREATE TABLE c6(c, d,
  2423. FOREIGN KEY(c, d) REFERENCES p ON DELETE RESTRICT
  2424. DEFERRABLE INITIALLY DEFERRED
  2425. );
  2426. CREATE TABLE c7(c, d,
  2427. FOREIGN KEY(c, d) REFERENCES p ON DELETE NO ACTION
  2428. DEFERRABLE INITIALLY DEFERRED
  2429. );
  2430. CREATE TABLE log(msg);
  2431. CREATE TRIGGER tt AFTER DELETE ON p BEGIN
  2432. INSERT INTO log VALUES('delete ' || old.rowid);
  2433. END;
  2434. }
  2435. } {}
  2436. do_test e_fkey-57.2 {
  2437. execsql {
  2438. INSERT INTO p VALUES('a', 'b');
  2439. INSERT INTO c1 VALUES('a', 'b');
  2440. INSERT INTO c2 VALUES('a', 'b');
  2441. INSERT INTO c3 VALUES('a', 'b');
  2442. BEGIN;
  2443. DROP TABLE p;
  2444. SELECT * FROM c1;
  2445. }
  2446. } {{} {}}
  2447. do_test e_fkey-57.3 {
  2448. execsql { SELECT * FROM c2 }
  2449. } {{} {}}
  2450. do_test e_fkey-57.4 {
  2451. execsql { SELECT * FROM c3 }
  2452. } {}
  2453. do_test e_fkey-57.5 {
  2454. execsql { SELECT * FROM log }
  2455. } {}
  2456. do_test e_fkey-57.6 {
  2457. execsql ROLLBACK
  2458. } {}
  2459. do_test e_fkey-57.7 {
  2460. execsql {
  2461. BEGIN;
  2462. DELETE FROM p;
  2463. SELECT * FROM log;
  2464. ROLLBACK;
  2465. }
  2466. } {{delete 1}}
  2467. #-------------------------------------------------------------------------
  2468. # If an IMMEDIATE foreign key fails as a result of a DROP TABLE, the
  2469. # DROP TABLE command fails.
  2470. #
  2471. # EVIDENCE-OF: R-32768-47925 If an immediate foreign key constraint is
  2472. # violated, the DROP TABLE statement fails and the table is not dropped.
  2473. #
  2474. do_test e_fkey-58.1 {
  2475. execsql {
  2476. DELETE FROM c1;
  2477. DELETE FROM c2;
  2478. DELETE FROM c3;
  2479. }
  2480. execsql { INSERT INTO c5 VALUES('a', 'b') }
  2481. catchsql { DROP TABLE p }
  2482. } {1 {foreign key constraint failed}}
  2483. do_test e_fkey-58.2 {
  2484. execsql { SELECT * FROM p }
  2485. } {a b}
  2486. do_test e_fkey-58.3 {
  2487. catchsql {
  2488. BEGIN;
  2489. DROP TABLE p;
  2490. }
  2491. } {1 {foreign key constraint failed}}
  2492. do_test e_fkey-58.4 {
  2493. execsql {
  2494. SELECT * FROM p;
  2495. SELECT * FROM c5;
  2496. ROLLBACK;
  2497. }
  2498. } {a b a b}
  2499. #-------------------------------------------------------------------------
  2500. # If a DEFERRED foreign key fails as a result of a DROP TABLE, attempting
  2501. # to commit the transaction fails unless the violation is fixed.
  2502. #
  2503. # EVIDENCE-OF: R-05903-08460 If a deferred foreign key constraint is
  2504. # violated, then an error is reported when the user attempts to commit
  2505. # the transaction if the foreign key constraint violations still exist
  2506. # at that point.
  2507. #
  2508. do_test e_fkey-59.1 {
  2509. execsql {
  2510. DELETE FROM c1 ; DELETE FROM c2 ; DELETE FROM c3 ;
  2511. DELETE FROM c4 ; DELETE FROM c5 ; DELETE FROM c6 ;
  2512. DELETE FROM c7
  2513. }
  2514. } {}
  2515. do_test e_fkey-59.2 {
  2516. execsql { INSERT INTO c7 VALUES('a', 'b') }
  2517. execsql {
  2518. BEGIN;
  2519. DROP TABLE p;
  2520. }
  2521. } {}
  2522. do_test e_fkey-59.3 {
  2523. catchsql COMMIT
  2524. } {1 {foreign key constraint failed}}
  2525. do_test e_fkey-59.4 {
  2526. execsql { CREATE TABLE p(a, b, PRIMARY KEY(a, b)) }
  2527. catchsql COMMIT
  2528. } {1 {foreign key constraint failed}}
  2529. do_test e_fkey-59.5 {
  2530. execsql { INSERT INTO p VALUES('a', 'b') }
  2531. execsql COMMIT
  2532. } {}
  2533. #-------------------------------------------------------------------------
  2534. # Any "foreign key mismatch" errors encountered while running an implicit
  2535. # "DELETE FROM tbl" are ignored.
  2536. #
  2537. # EVIDENCE-OF: R-57242-37005 Any "foreign key mismatch" errors
  2538. # encountered as part of an implicit DELETE are ignored.
  2539. #
  2540. drop_all_tables
  2541. do_test e_fkey-60.1 {
  2542. execsql {
  2543. PRAGMA foreign_keys = OFF;
  2544. CREATE TABLE p(a PRIMARY KEY, b REFERENCES nosuchtable);
  2545. CREATE TABLE c1(c, d, FOREIGN KEY(c, d) REFERENCES a);
  2546. CREATE TABLE c2(c REFERENCES p(b), d);
  2547. CREATE TABLE c3(c REFERENCES p ON DELETE SET NULL, d);
  2548. INSERT INTO p VALUES(1, 2);
  2549. INSERT INTO c1 VALUES(1, 2);
  2550. INSERT INTO c2 VALUES(1, 2);
  2551. INSERT INTO c3 VALUES(1, 2);
  2552. }
  2553. } {}
  2554. do_test e_fkey-60.2 {
  2555. execsql { PRAGMA foreign_keys = ON }
  2556. catchsql { DELETE FROM p }
  2557. } {1 {no such table: main.nosuchtable}}
  2558. do_test e_fkey-60.3 {
  2559. execsql {
  2560. BEGIN;
  2561. DROP TABLE p;
  2562. SELECT * FROM c3;
  2563. ROLLBACK;
  2564. }
  2565. } {{} 2}
  2566. do_test e_fkey-60.4 {
  2567. execsql { CREATE TABLE nosuchtable(x PRIMARY KEY) }
  2568. catchsql { DELETE FROM p }
  2569. } {1 {foreign key mismatch - "c2" referencing "p"}}
  2570. do_test e_fkey-60.5 {
  2571. execsql { DROP TABLE c1 }
  2572. catchsql { DELETE FROM p }
  2573. } {1 {foreign key mismatch - "c2" referencing "p"}}
  2574. do_test e_fkey-60.6 {
  2575. execsql { DROP TABLE c2 }
  2576. execsql { DELETE FROM p }
  2577. } {}
  2578. #-------------------------------------------------------------------------
  2579. # Test that the special behaviors of ALTER and DROP TABLE are only
  2580. # activated when foreign keys are enabled. Special behaviors are:
  2581. #
  2582. # 1. ADD COLUMN not allowing a REFERENCES clause with a non-NULL
  2583. # default value.
  2584. # 2. Modifying foreign key definitions when a parent table is RENAMEd.
  2585. # 3. Running an implicit DELETE FROM command as part of DROP TABLE.
  2586. #
  2587. # EVIDENCE-OF: R-54142-41346 The properties of the DROP TABLE and ALTER
  2588. # TABLE commands described above only apply if foreign keys are enabled.
  2589. #
  2590. do_test e_fkey-61.1.1 {
  2591. drop_all_tables
  2592. execsql { CREATE TABLE t1(a, b) }
  2593. catchsql { ALTER TABLE t1 ADD COLUMN c DEFAULT 'xxx' REFERENCES t2 }
  2594. } {1 {Cannot add a REFERENCES column with non-NULL default value}}
  2595. do_test e_fkey-61.1.2 {
  2596. execsql { PRAGMA foreign_keys = OFF }
  2597. execsql { ALTER TABLE t1 ADD COLUMN c DEFAULT 'xxx' REFERENCES t2 }
  2598. execsql { SELECT sql FROM sqlite_master WHERE name = 't1' }
  2599. } {{CREATE TABLE t1(a, b, c DEFAULT 'xxx' REFERENCES t2)}}
  2600. do_test e_fkey-61.1.3 {
  2601. execsql { PRAGMA foreign_keys = ON }
  2602. } {}
  2603. do_test e_fkey-61.2.1 {
  2604. drop_all_tables
  2605. execsql {
  2606. CREATE TABLE p(a UNIQUE);
  2607. CREATE TABLE c(b REFERENCES p(a));
  2608. BEGIN;
  2609. ALTER TABLE p RENAME TO parent;
  2610. SELECT sql FROM sqlite_master WHERE name = 'c';
  2611. ROLLBACK;
  2612. }
  2613. } {{CREATE TABLE c(b REFERENCES "parent"(a))}}
  2614. do_test e_fkey-61.2.2 {
  2615. execsql {
  2616. PRAGMA foreign_keys = OFF;
  2617. ALTER TABLE p RENAME TO parent;
  2618. SELECT sql FROM sqlite_master WHERE name = 'c';
  2619. }
  2620. } {{CREATE TABLE c(b REFERENCES p(a))}}
  2621. do_test e_fkey-61.2.3 {
  2622. execsql { PRAGMA foreign_keys = ON }
  2623. } {}
  2624. do_test e_fkey-61.3.1 {
  2625. drop_all_tables
  2626. execsql {
  2627. CREATE TABLE p(a UNIQUE);
  2628. CREATE TABLE c(b REFERENCES p(a) ON DELETE SET NULL);
  2629. INSERT INTO p VALUES('x');
  2630. INSERT INTO c VALUES('x');
  2631. BEGIN;
  2632. DROP TABLE p;
  2633. SELECT * FROM c;
  2634. ROLLBACK;
  2635. }
  2636. } {{}}
  2637. do_test e_fkey-61.3.2 {
  2638. execsql {
  2639. PRAGMA foreign_keys = OFF;
  2640. DROP TABLE p;
  2641. SELECT * FROM c;
  2642. }
  2643. } {x}
  2644. do_test e_fkey-61.3.3 {
  2645. execsql { PRAGMA foreign_keys = ON }
  2646. } {}
  2647. ###########################################################################
  2648. ### SECTION 6: Limits and Unsupported Features
  2649. ###########################################################################
  2650. #-------------------------------------------------------------------------
  2651. # Test that MATCH clauses are parsed, but SQLite treats every foreign key
  2652. # constraint as if it were "MATCH SIMPLE".
  2653. #
  2654. # EVIDENCE-OF: R-24728-13230 SQLite parses MATCH clauses (i.e. does not
  2655. # report a syntax error if you specify one), but does not enforce them.
  2656. #
  2657. # EVIDENCE-OF: R-24450-46174 All foreign key constraints in SQLite are
  2658. # handled as if MATCH SIMPLE were specified.
  2659. #
  2660. foreach zMatch [list SIMPLE PARTIAL FULL Simple parTIAL FuLL ] {
  2661. drop_all_tables
  2662. do_test e_fkey-62.$zMatch.1 {
  2663. execsql "
  2664. CREATE TABLE p(a, b, c, PRIMARY KEY(b, c));
  2665. CREATE TABLE c(d, e, f, FOREIGN KEY(e, f) REFERENCES p MATCH $zMatch);
  2666. "
  2667. } {}
  2668. do_test e_fkey-62.$zMatch.2 {
  2669. execsql { INSERT INTO p VALUES(1, 2, 3) }
  2670. # MATCH SIMPLE behavior: Allow any child key that contains one or more
  2671. # NULL value to be inserted. Non-NULL values do not have to map to any
  2672. # parent key values, so long as at least one field of the child key is
  2673. # NULL.
  2674. execsql { INSERT INTO c VALUES('w', 2, 3) }
  2675. execsql { INSERT INTO c VALUES('x', 'x', NULL) }
  2676. execsql { INSERT INTO c VALUES('y', NULL, 'x') }
  2677. execsql { INSERT INTO c VALUES('z', NULL, NULL) }
  2678. # Check that the FK is enforced properly if there are no NULL values
  2679. # in the child key columns.
  2680. catchsql { INSERT INTO c VALUES('a', 2, 4) }
  2681. } {1 {foreign key constraint failed}}
  2682. }
  2683. #-------------------------------------------------------------------------
  2684. # Test that SQLite does not support the SET CONSTRAINT statement. And
  2685. # that it is possible to create both immediate and deferred constraints.
  2686. #
  2687. # EVIDENCE-OF: R-21599-16038 In SQLite, a foreign key constraint is
  2688. # permanently marked as deferred or immediate when it is created.
  2689. #
  2690. drop_all_tables
  2691. do_test e_fkey-62.1 {
  2692. catchsql { SET CONSTRAINTS ALL IMMEDIATE }
  2693. } {1 {near "SET": syntax error}}
  2694. do_test e_fkey-62.2 {
  2695. catchsql { SET CONSTRAINTS ALL DEFERRED }
  2696. } {1 {near "SET": syntax error}}
  2697. do_test e_fkey-62.3 {
  2698. execsql {
  2699. CREATE TABLE p(a, b, PRIMARY KEY(a, b));
  2700. CREATE TABLE cd(c, d,
  2701. FOREIGN KEY(c, d) REFERENCES p DEFERRABLE INITIALLY DEFERRED);
  2702. CREATE TABLE ci(c, d,
  2703. FOREIGN KEY(c, d) REFERENCES p DEFERRABLE INITIALLY IMMEDIATE);
  2704. BEGIN;
  2705. }
  2706. } {}
  2707. do_test e_fkey-62.4 {
  2708. catchsql { INSERT INTO ci VALUES('x', 'y') }
  2709. } {1 {foreign key constraint failed}}
  2710. do_test e_fkey-62.5 {
  2711. catchsql { INSERT INTO cd VALUES('x', 'y') }
  2712. } {0 {}}
  2713. do_test e_fkey-62.6 {
  2714. catchsql { COMMIT }
  2715. } {1 {foreign key constraint failed}}
  2716. do_test e_fkey-62.7 {
  2717. execsql {
  2718. DELETE FROM cd;
  2719. COMMIT;
  2720. }
  2721. } {}
  2722. #-------------------------------------------------------------------------
  2723. # Test that the maximum recursion depth of foreign key action programs is
  2724. # governed by the SQLITE_MAX_TRIGGER_DEPTH and SQLITE_LIMIT_TRIGGER_DEPTH
  2725. # settings.
  2726. #
  2727. # EVIDENCE-OF: R-42264-30503 The SQLITE_MAX_TRIGGER_DEPTH and
  2728. # SQLITE_LIMIT_TRIGGER_DEPTH settings determine the maximum allowable
  2729. # depth of trigger program recursion. For the purposes of these limits,
  2730. # foreign key actions are considered trigger programs.
  2731. #
  2732. proc test_on_delete_recursion {limit} {
  2733. drop_all_tables
  2734. execsql {
  2735. BEGIN;
  2736. CREATE TABLE t0(a PRIMARY KEY, b);
  2737. INSERT INTO t0 VALUES('x0', NULL);
  2738. }
  2739. for {set i 1} {$i <= $limit} {incr i} {
  2740. execsql "
  2741. CREATE TABLE t$i (
  2742. a PRIMARY KEY, b REFERENCES t[expr $i-1] ON DELETE CASCADE
  2743. );
  2744. INSERT INTO t$i VALUES('x$i', 'x[expr $i-1]');
  2745. "
  2746. }
  2747. execsql COMMIT
  2748. catchsql "
  2749. DELETE FROM t0;
  2750. SELECT count(*) FROM t$limit;
  2751. "
  2752. }
  2753. proc test_on_update_recursion {limit} {
  2754. drop_all_tables
  2755. execsql {
  2756. BEGIN;
  2757. CREATE TABLE t0(a PRIMARY KEY);
  2758. INSERT INTO t0 VALUES('xxx');
  2759. }
  2760. for {set i 1} {$i <= $limit} {incr i} {
  2761. set j [expr $i-1]
  2762. execsql "
  2763. CREATE TABLE t$i (a PRIMARY KEY REFERENCES t$j ON UPDATE CASCADE);
  2764. INSERT INTO t$i VALUES('xxx');
  2765. "
  2766. }
  2767. execsql COMMIT
  2768. catchsql "
  2769. UPDATE t0 SET a = 'yyy';
  2770. SELECT NOT (a='yyy') FROM t$limit;
  2771. "
  2772. }
  2773. do_test e_fkey-63.1.1 {
  2774. test_on_delete_recursion $SQLITE_MAX_TRIGGER_DEPTH
  2775. } {0 0}
  2776. do_test e_fkey-63.1.2 {
  2777. test_on_delete_recursion [expr $SQLITE_MAX_TRIGGER_DEPTH+1]
  2778. } {1 {too many levels of trigger recursion}}
  2779. do_test e_fkey-63.1.3 {
  2780. sqlite3_limit db SQLITE_LIMIT_TRIGGER_DEPTH 5
  2781. test_on_delete_recursion 5
  2782. } {0 0}
  2783. do_test e_fkey-63.1.4 {
  2784. test_on_delete_recursion 6
  2785. } {1 {too many levels of trigger recursion}}
  2786. do_test e_fkey-63.1.5 {
  2787. sqlite3_limit db SQLITE_LIMIT_TRIGGER_DEPTH 1000000
  2788. } {5}
  2789. do_test e_fkey-63.2.1 {
  2790. test_on_update_recursion $SQLITE_MAX_TRIGGER_DEPTH
  2791. } {0 0}
  2792. do_test e_fkey-63.2.2 {
  2793. test_on_update_recursion [expr $SQLITE_MAX_TRIGGER_DEPTH+1]
  2794. } {1 {too many levels of trigger recursion}}
  2795. do_test e_fkey-63.2.3 {
  2796. sqlite3_limit db SQLITE_LIMIT_TRIGGER_DEPTH 5
  2797. test_on_update_recursion 5
  2798. } {0 0}
  2799. do_test e_fkey-63.2.4 {
  2800. test_on_update_recursion 6
  2801. } {1 {too many levels of trigger recursion}}
  2802. do_test e_fkey-63.2.5 {
  2803. sqlite3_limit db SQLITE_LIMIT_TRIGGER_DEPTH 1000000
  2804. } {5}
  2805. #-------------------------------------------------------------------------
  2806. # The setting of the recursive_triggers pragma does not affect foreign
  2807. # key actions.
  2808. #
  2809. # EVIDENCE-OF: R-51769-32730 The PRAGMA recursive_triggers setting does
  2810. # not not affect the operation of foreign key actions.
  2811. #
  2812. foreach recursive_triggers_setting [list 0 1 ON OFF] {
  2813. drop_all_tables
  2814. execsql "PRAGMA recursive_triggers = $recursive_triggers_setting"
  2815. do_test e_fkey-64.$recursive_triggers_setting.1 {
  2816. execsql {
  2817. CREATE TABLE t1(a PRIMARY KEY, b REFERENCES t1 ON DELETE CASCADE);
  2818. INSERT INTO t1 VALUES(1, NULL);
  2819. INSERT INTO t1 VALUES(2, 1);
  2820. INSERT INTO t1 VALUES(3, 2);
  2821. INSERT INTO t1 VALUES(4, 3);
  2822. INSERT INTO t1 VALUES(5, 4);
  2823. SELECT count(*) FROM t1;
  2824. }
  2825. } {5}
  2826. do_test e_fkey-64.$recursive_triggers_setting.2 {
  2827. execsql { SELECT count(*) FROM t1 WHERE a = 1 }
  2828. } {1}
  2829. do_test e_fkey-64.$recursive_triggers_setting.3 {
  2830. execsql {
  2831. DELETE FROM t1 WHERE a = 1;
  2832. SELECT count(*) FROM t1;
  2833. }
  2834. } {0}
  2835. }
  2836. finish_test