autoinc.test 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  1. # 2004 November 12
  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. # This file implements regression tests for SQLite library. The
  12. # focus of this script is testing the AUTOINCREMENT features.
  13. #
  14. # $Id: autoinc.test,v 1.14 2009/06/23 20:28:54 drh Exp $
  15. #
  16. set testdir [file dirname $argv0]
  17. source $testdir/tester.tcl
  18. # If the library is not compiled with autoincrement support then
  19. # skip all tests in this file.
  20. #
  21. ifcapable {!autoinc} {
  22. finish_test
  23. return
  24. }
  25. sqlite3_db_config_lookaside db 0 0 0
  26. # The database is initially empty.
  27. #
  28. do_test autoinc-1.1 {
  29. execsql {
  30. SELECT name FROM sqlite_master WHERE type='table';
  31. }
  32. } {}
  33. # Add a table with the AUTOINCREMENT feature. Verify that the
  34. # SQLITE_SEQUENCE table gets created.
  35. #
  36. do_test autoinc-1.2 {
  37. execsql {
  38. CREATE TABLE t1(x INTEGER PRIMARY KEY AUTOINCREMENT, y);
  39. SELECT name FROM sqlite_master WHERE type='table';
  40. }
  41. } {t1 sqlite_sequence}
  42. # The SQLITE_SEQUENCE table is initially empty
  43. #
  44. do_test autoinc-1.3 {
  45. execsql {
  46. SELECT * FROM sqlite_sequence;
  47. }
  48. } {}
  49. do_test autoinc-1.3.1 {
  50. catchsql {
  51. CREATE INDEX seqidx ON sqlite_sequence(name)
  52. }
  53. } {1 {table sqlite_sequence may not be indexed}}
  54. # Close and reopen the database. Verify that everything is still there.
  55. #
  56. do_test autoinc-1.4 {
  57. db close
  58. sqlite3 db test.db
  59. execsql {
  60. SELECT * FROM sqlite_sequence;
  61. }
  62. } {}
  63. # We are not allowed to drop the sqlite_sequence table.
  64. #
  65. do_test autoinc-1.5 {
  66. catchsql {DROP TABLE sqlite_sequence}
  67. } {1 {table sqlite_sequence may not be dropped}}
  68. do_test autoinc-1.6 {
  69. execsql {SELECT name FROM sqlite_master WHERE type='table'}
  70. } {t1 sqlite_sequence}
  71. # Insert an entries into the t1 table and make sure the largest key
  72. # is always recorded in the sqlite_sequence table.
  73. #
  74. do_test autoinc-2.1 {
  75. execsql {
  76. SELECT * FROM sqlite_sequence
  77. }
  78. } {}
  79. do_test autoinc-2.2 {
  80. execsql {
  81. INSERT INTO t1 VALUES(12,34);
  82. SELECT * FROM sqlite_sequence;
  83. }
  84. } {t1 12}
  85. do_test autoinc-2.3 {
  86. execsql {
  87. INSERT INTO t1 VALUES(1,23);
  88. SELECT * FROM sqlite_sequence;
  89. }
  90. } {t1 12}
  91. do_test autoinc-2.4 {
  92. execsql {
  93. INSERT INTO t1 VALUES(123,456);
  94. SELECT * FROM sqlite_sequence;
  95. }
  96. } {t1 123}
  97. do_test autoinc-2.5 {
  98. execsql {
  99. INSERT INTO t1 VALUES(NULL,567);
  100. SELECT * FROM sqlite_sequence;
  101. }
  102. } {t1 124}
  103. do_test autoinc-2.6 {
  104. execsql {
  105. DELETE FROM t1 WHERE y=567;
  106. SELECT * FROM sqlite_sequence;
  107. }
  108. } {t1 124}
  109. do_test autoinc-2.7 {
  110. execsql {
  111. INSERT INTO t1 VALUES(NULL,567);
  112. SELECT * FROM sqlite_sequence;
  113. }
  114. } {t1 125}
  115. do_test autoinc-2.8 {
  116. execsql {
  117. DELETE FROM t1;
  118. SELECT * FROM sqlite_sequence;
  119. }
  120. } {t1 125}
  121. do_test autoinc-2.9 {
  122. execsql {
  123. INSERT INTO t1 VALUES(12,34);
  124. SELECT * FROM sqlite_sequence;
  125. }
  126. } {t1 125}
  127. do_test autoinc-2.10 {
  128. execsql {
  129. INSERT INTO t1 VALUES(125,456);
  130. SELECT * FROM sqlite_sequence;
  131. }
  132. } {t1 125}
  133. do_test autoinc-2.11 {
  134. execsql {
  135. INSERT INTO t1 VALUES(-1234567,-1);
  136. SELECT * FROM sqlite_sequence;
  137. }
  138. } {t1 125}
  139. do_test autoinc-2.12 {
  140. execsql {
  141. INSERT INTO t1 VALUES(234,5678);
  142. SELECT * FROM sqlite_sequence;
  143. }
  144. } {t1 234}
  145. do_test autoinc-2.13 {
  146. execsql {
  147. DELETE FROM t1;
  148. INSERT INTO t1 VALUES(NULL,1);
  149. SELECT * FROM sqlite_sequence;
  150. }
  151. } {t1 235}
  152. do_test autoinc-2.14 {
  153. execsql {
  154. SELECT * FROM t1;
  155. }
  156. } {235 1}
  157. # Manually change the autoincrement values in sqlite_sequence.
  158. #
  159. do_test autoinc-2.20 {
  160. execsql {
  161. UPDATE sqlite_sequence SET seq=1234 WHERE name='t1';
  162. INSERT INTO t1 VALUES(NULL,2);
  163. SELECT * FROM t1;
  164. }
  165. } {235 1 1235 2}
  166. do_test autoinc-2.21 {
  167. execsql {
  168. SELECT * FROM sqlite_sequence;
  169. }
  170. } {t1 1235}
  171. do_test autoinc-2.22 {
  172. execsql {
  173. UPDATE sqlite_sequence SET seq=NULL WHERE name='t1';
  174. INSERT INTO t1 VALUES(NULL,3);
  175. SELECT * FROM t1;
  176. }
  177. } {235 1 1235 2 1236 3}
  178. do_test autoinc-2.23 {
  179. execsql {
  180. SELECT * FROM sqlite_sequence;
  181. }
  182. } {t1 1236}
  183. do_test autoinc-2.24 {
  184. execsql {
  185. UPDATE sqlite_sequence SET seq='a-string' WHERE name='t1';
  186. INSERT INTO t1 VALUES(NULL,4);
  187. SELECT * FROM t1;
  188. }
  189. } {235 1 1235 2 1236 3 1237 4}
  190. do_test autoinc-2.25 {
  191. execsql {
  192. SELECT * FROM sqlite_sequence;
  193. }
  194. } {t1 1237}
  195. do_test autoinc-2.26 {
  196. execsql {
  197. DELETE FROM sqlite_sequence WHERE name='t1';
  198. INSERT INTO t1 VALUES(NULL,5);
  199. SELECT * FROM t1;
  200. }
  201. } {235 1 1235 2 1236 3 1237 4 1238 5}
  202. do_test autoinc-2.27 {
  203. execsql {
  204. SELECT * FROM sqlite_sequence;
  205. }
  206. } {t1 1238}
  207. do_test autoinc-2.28 {
  208. execsql {
  209. UPDATE sqlite_sequence SET seq='12345678901234567890'
  210. WHERE name='t1';
  211. INSERT INTO t1 VALUES(NULL,6);
  212. SELECT * FROM t1;
  213. }
  214. } {235 1 1235 2 1236 3 1237 4 1238 5 1239 6}
  215. do_test autoinc-2.29 {
  216. execsql {
  217. SELECT * FROM sqlite_sequence;
  218. }
  219. } {t1 1239}
  220. # Test multi-row inserts
  221. #
  222. do_test autoinc-2.50 {
  223. execsql {
  224. DELETE FROM t1 WHERE y>=3;
  225. INSERT INTO t1 SELECT NULL, y+2 FROM t1;
  226. SELECT * FROM t1;
  227. }
  228. } {235 1 1235 2 1240 3 1241 4}
  229. do_test autoinc-2.51 {
  230. execsql {
  231. SELECT * FROM sqlite_sequence
  232. }
  233. } {t1 1241}
  234. ifcapable tempdb {
  235. do_test autoinc-2.52 {
  236. execsql {
  237. CREATE TEMP TABLE t2 AS SELECT y FROM t1;
  238. }
  239. execsql {
  240. INSERT INTO t1 SELECT NULL, y+4 FROM t2;
  241. SELECT * FROM t1;
  242. }
  243. } {235 1 1235 2 1240 3 1241 4 1242 5 1243 6 1244 7 1245 8}
  244. do_test autoinc-2.53 {
  245. execsql {
  246. SELECT * FROM sqlite_sequence
  247. }
  248. } {t1 1245}
  249. do_test autoinc-2.54 {
  250. execsql {
  251. DELETE FROM t1;
  252. INSERT INTO t1 SELECT NULL, y FROM t2;
  253. SELECT * FROM t1;
  254. }
  255. } {1246 1 1247 2 1248 3 1249 4}
  256. do_test autoinc-2.55 {
  257. execsql {
  258. SELECT * FROM sqlite_sequence
  259. }
  260. } {t1 1249}
  261. }
  262. # Create multiple AUTOINCREMENT tables. Make sure all sequences are
  263. # tracked separately and do not interfere with one another.
  264. #
  265. do_test autoinc-2.70 {
  266. catchsql {
  267. DROP TABLE t2;
  268. }
  269. execsql {
  270. CREATE TABLE t2(d, e INTEGER PRIMARY KEY AUTOINCREMENT, f);
  271. INSERT INTO t2(d) VALUES(1);
  272. SELECT * FROM sqlite_sequence;
  273. }
  274. } [ifcapable tempdb {list t1 1249 t2 1} else {list t1 1241 t2 1}]
  275. do_test autoinc-2.71 {
  276. execsql {
  277. INSERT INTO t2(d) VALUES(2);
  278. SELECT * FROM sqlite_sequence;
  279. }
  280. } [ifcapable tempdb {list t1 1249 t2 2} else {list t1 1241 t2 2}]
  281. do_test autoinc-2.72 {
  282. execsql {
  283. INSERT INTO t1(x) VALUES(10000);
  284. SELECT * FROM sqlite_sequence;
  285. }
  286. } {t1 10000 t2 2}
  287. do_test autoinc-2.73 {
  288. execsql {
  289. CREATE TABLE t3(g INTEGER PRIMARY KEY AUTOINCREMENT, h);
  290. INSERT INTO t3(h) VALUES(1);
  291. SELECT * FROM sqlite_sequence;
  292. }
  293. } {t1 10000 t2 2 t3 1}
  294. do_test autoinc-2.74 {
  295. execsql {
  296. INSERT INTO t2(d,e) VALUES(3,100);
  297. SELECT * FROM sqlite_sequence;
  298. }
  299. } {t1 10000 t2 100 t3 1}
  300. # When a table with an AUTOINCREMENT is deleted, the corresponding entry
  301. # in the SQLITE_SEQUENCE table should also be deleted. But the SQLITE_SEQUENCE
  302. # table itself should remain behind.
  303. #
  304. do_test autoinc-3.1 {
  305. execsql {SELECT name FROM sqlite_sequence}
  306. } {t1 t2 t3}
  307. do_test autoinc-3.2 {
  308. execsql {
  309. DROP TABLE t1;
  310. SELECT name FROM sqlite_sequence;
  311. }
  312. } {t2 t3}
  313. do_test autoinc-3.3 {
  314. execsql {
  315. DROP TABLE t3;
  316. SELECT name FROM sqlite_sequence;
  317. }
  318. } {t2}
  319. do_test autoinc-3.4 {
  320. execsql {
  321. DROP TABLE t2;
  322. SELECT name FROM sqlite_sequence;
  323. }
  324. } {}
  325. # AUTOINCREMENT on TEMP tables.
  326. #
  327. ifcapable tempdb {
  328. do_test autoinc-4.1 {
  329. execsql {
  330. SELECT 1, name FROM sqlite_master WHERE type='table';
  331. SELECT 2, name FROM sqlite_temp_master WHERE type='table';
  332. }
  333. } {1 sqlite_sequence}
  334. do_test autoinc-4.2 {
  335. execsql {
  336. CREATE TABLE t1(x INTEGER PRIMARY KEY AUTOINCREMENT, y);
  337. CREATE TEMP TABLE t3(a INTEGER PRIMARY KEY AUTOINCREMENT, b);
  338. SELECT 1, name FROM sqlite_master WHERE type='table';
  339. SELECT 2, name FROM sqlite_temp_master WHERE type='table';
  340. }
  341. } {1 sqlite_sequence 1 t1 2 t3 2 sqlite_sequence}
  342. do_test autoinc-4.3 {
  343. execsql {
  344. SELECT 1, * FROM main.sqlite_sequence;
  345. SELECT 2, * FROM temp.sqlite_sequence;
  346. }
  347. } {}
  348. do_test autoinc-4.4 {
  349. execsql {
  350. INSERT INTO t1 VALUES(10,1);
  351. INSERT INTO t3 VALUES(20,2);
  352. INSERT INTO t1 VALUES(NULL,3);
  353. INSERT INTO t3 VALUES(NULL,4);
  354. }
  355. } {}
  356. ifcapable compound {
  357. do_test autoinc-4.4.1 {
  358. execsql {
  359. SELECT * FROM t1 UNION ALL SELECT * FROM t3;
  360. }
  361. } {10 1 11 3 20 2 21 4}
  362. } ;# ifcapable compound
  363. do_test autoinc-4.5 {
  364. execsql {
  365. SELECT 1, * FROM main.sqlite_sequence;
  366. SELECT 2, * FROM temp.sqlite_sequence;
  367. }
  368. } {1 t1 11 2 t3 21}
  369. do_test autoinc-4.6 {
  370. execsql {
  371. INSERT INTO t1 SELECT * FROM t3;
  372. SELECT 1, * FROM main.sqlite_sequence;
  373. SELECT 2, * FROM temp.sqlite_sequence;
  374. }
  375. } {1 t1 21 2 t3 21}
  376. do_test autoinc-4.7 {
  377. execsql {
  378. INSERT INTO t3 SELECT x+100, y FROM t1;
  379. SELECT 1, * FROM main.sqlite_sequence;
  380. SELECT 2, * FROM temp.sqlite_sequence;
  381. }
  382. } {1 t1 21 2 t3 121}
  383. do_test autoinc-4.8 {
  384. execsql {
  385. DROP TABLE t3;
  386. SELECT 1, * FROM main.sqlite_sequence;
  387. SELECT 2, * FROM temp.sqlite_sequence;
  388. }
  389. } {1 t1 21}
  390. do_test autoinc-4.9 {
  391. execsql {
  392. CREATE TEMP TABLE t2(p INTEGER PRIMARY KEY AUTOINCREMENT, q);
  393. INSERT INTO t2 SELECT * FROM t1;
  394. DROP TABLE t1;
  395. SELECT 1, * FROM main.sqlite_sequence;
  396. SELECT 2, * FROM temp.sqlite_sequence;
  397. }
  398. } {2 t2 21}
  399. do_test autoinc-4.10 {
  400. execsql {
  401. DROP TABLE t2;
  402. SELECT 1, * FROM main.sqlite_sequence;
  403. SELECT 2, * FROM temp.sqlite_sequence;
  404. }
  405. } {}
  406. }
  407. # Make sure AUTOINCREMENT works on ATTACH-ed tables.
  408. #
  409. ifcapable tempdb&&attach {
  410. do_test autoinc-5.1 {
  411. forcedelete test2.db
  412. forcedelete test2.db-journal
  413. sqlite3 db2 test2.db
  414. execsql {
  415. CREATE TABLE t4(m INTEGER PRIMARY KEY AUTOINCREMENT, n);
  416. CREATE TABLE t5(o, p INTEGER PRIMARY KEY AUTOINCREMENT);
  417. } db2;
  418. execsql {
  419. ATTACH 'test2.db' as aux;
  420. SELECT 1, * FROM main.sqlite_sequence;
  421. SELECT 2, * FROM temp.sqlite_sequence;
  422. SELECT 3, * FROM aux.sqlite_sequence;
  423. }
  424. } {}
  425. do_test autoinc-5.2 {
  426. execsql {
  427. INSERT INTO t4 VALUES(NULL,1);
  428. SELECT 1, * FROM main.sqlite_sequence;
  429. SELECT 2, * FROM temp.sqlite_sequence;
  430. SELECT 3, * FROM aux.sqlite_sequence;
  431. }
  432. } {3 t4 1}
  433. do_test autoinc-5.3 {
  434. execsql {
  435. INSERT INTO t5 VALUES(100,200);
  436. SELECT * FROM sqlite_sequence
  437. } db2
  438. } {t4 1 t5 200}
  439. do_test autoinc-5.4 {
  440. execsql {
  441. SELECT 1, * FROM main.sqlite_sequence;
  442. SELECT 2, * FROM temp.sqlite_sequence;
  443. SELECT 3, * FROM aux.sqlite_sequence;
  444. }
  445. } {3 t4 1 3 t5 200}
  446. }
  447. # Requirement REQ00310: Make sure an insert fails if the sequence is
  448. # already at its maximum value.
  449. #
  450. ifcapable {rowid32} {
  451. do_test autoinc-6.1 {
  452. execsql {
  453. CREATE TABLE t6(v INTEGER PRIMARY KEY AUTOINCREMENT, w);
  454. INSERT INTO t6 VALUES(2147483647,1);
  455. SELECT seq FROM main.sqlite_sequence WHERE name='t6';
  456. }
  457. } 2147483647
  458. }
  459. ifcapable {!rowid32} {
  460. do_test autoinc-6.1 {
  461. execsql {
  462. CREATE TABLE t6(v INTEGER PRIMARY KEY AUTOINCREMENT, w);
  463. INSERT INTO t6 VALUES(9223372036854775807,1);
  464. SELECT seq FROM main.sqlite_sequence WHERE name='t6';
  465. }
  466. } 9223372036854775807
  467. }
  468. do_test autoinc-6.2 {
  469. catchsql {
  470. INSERT INTO t6 VALUES(NULL,1);
  471. }
  472. } {1 {database or disk is full}}
  473. # Allow the AUTOINCREMENT keyword inside the parentheses
  474. # on a separate PRIMARY KEY designation.
  475. #
  476. do_test autoinc-7.1 {
  477. execsql {
  478. CREATE TABLE t7(x INTEGER, y REAL, PRIMARY KEY(x AUTOINCREMENT));
  479. INSERT INTO t7(y) VALUES(123);
  480. INSERT INTO t7(y) VALUES(234);
  481. DELETE FROM t7;
  482. INSERT INTO t7(y) VALUES(345);
  483. SELECT * FROM t7;
  484. }
  485. } {3 345.0}
  486. # Test that if the AUTOINCREMENT is applied to a non integer primary key
  487. # the error message is sensible.
  488. do_test autoinc-7.2 {
  489. catchsql {
  490. CREATE TABLE t8(x TEXT PRIMARY KEY AUTOINCREMENT);
  491. }
  492. } {1 {AUTOINCREMENT is only allowed on an INTEGER PRIMARY KEY}}
  493. # Ticket #1283. Make sure that preparing but never running a statement
  494. # that creates the sqlite_sequence table does not mess up the database.
  495. #
  496. do_test autoinc-8.1 {
  497. catch {db2 close}
  498. catch {db close}
  499. forcedelete test.db
  500. sqlite3 db test.db
  501. set DB [sqlite3_connection_pointer db]
  502. set STMT [sqlite3_prepare $DB {
  503. CREATE TABLE t1(
  504. x INTEGER PRIMARY KEY AUTOINCREMENT
  505. )
  506. } -1 TAIL]
  507. sqlite3_finalize $STMT
  508. set STMT [sqlite3_prepare $DB {
  509. CREATE TABLE t1(
  510. x INTEGER PRIMARY KEY AUTOINCREMENT
  511. )
  512. } -1 TAIL]
  513. sqlite3_step $STMT
  514. sqlite3_finalize $STMT
  515. execsql {
  516. INSERT INTO t1 VALUES(NULL);
  517. SELECT * FROM t1;
  518. }
  519. } {1}
  520. # Ticket #3148
  521. # Make sure the sqlite_sequence table is not damaged when doing
  522. # an empty insert - an INSERT INTO ... SELECT ... where the SELECT
  523. # clause returns an empty set.
  524. #
  525. do_test autoinc-9.1 {
  526. db eval {
  527. CREATE TABLE t2(x INTEGER PRIMARY KEY AUTOINCREMENT, y);
  528. INSERT INTO t2 VALUES(NULL, 1);
  529. CREATE TABLE t3(a INTEGER PRIMARY KEY AUTOINCREMENT, b);
  530. INSERT INTO t3 SELECT * FROM t2 WHERE y>1;
  531. SELECT * FROM sqlite_sequence WHERE name='t3';
  532. }
  533. } {t3 0}
  534. ifcapable trigger {
  535. catchsql { pragma recursive_triggers = off }
  536. # Ticket #3928. Make sure that triggers to not make extra slots in
  537. # the SQLITE_SEQUENCE table.
  538. #
  539. do_test autoinc-3928.1 {
  540. db eval {
  541. CREATE TABLE t3928(a INTEGER PRIMARY KEY AUTOINCREMENT, b);
  542. CREATE TRIGGER t3928r1 BEFORE INSERT ON t3928 BEGIN
  543. INSERT INTO t3928(b) VALUES('before1');
  544. INSERT INTO t3928(b) VALUES('before2');
  545. END;
  546. CREATE TRIGGER t3928r2 AFTER INSERT ON t3928 BEGIN
  547. INSERT INTO t3928(b) VALUES('after1');
  548. INSERT INTO t3928(b) VALUES('after2');
  549. END;
  550. INSERT INTO t3928(b) VALUES('test');
  551. SELECT * FROM t3928 ORDER BY a;
  552. }
  553. } {1 before1 2 after1 3 after2 4 before2 5 after1 6 after2 7 test 8 before1 9 before2 10 after1 11 before1 12 before2 13 after2}
  554. do_test autoinc-3928.2 {
  555. db eval {
  556. SELECT * FROM sqlite_sequence WHERE name='t3928'
  557. }
  558. } {t3928 13}
  559. do_test autoinc-3928.3 {
  560. db eval {
  561. DROP TRIGGER t3928r1;
  562. DROP TRIGGER t3928r2;
  563. CREATE TRIGGER t3928r3 BEFORE UPDATE ON t3928
  564. WHEN typeof(new.b)=='integer' BEGIN
  565. INSERT INTO t3928(b) VALUES('before-int-' || new.b);
  566. END;
  567. CREATE TRIGGER t3928r4 AFTER UPDATE ON t3928
  568. WHEN typeof(new.b)=='integer' BEGIN
  569. INSERT INTO t3928(b) VALUES('after-int-' || new.b);
  570. END;
  571. DELETE FROM t3928 WHERE a!=1;
  572. UPDATE t3928 SET b=456 WHERE a=1;
  573. SELECT * FROM t3928 ORDER BY a;
  574. }
  575. } {1 456 14 before-int-456 15 after-int-456}
  576. do_test autoinc-3928.4 {
  577. db eval {
  578. SELECT * FROM sqlite_sequence WHERE name='t3928'
  579. }
  580. } {t3928 15}
  581. do_test autoinc-3928.5 {
  582. db eval {
  583. CREATE TABLE t3928b(x);
  584. INSERT INTO t3928b VALUES(100);
  585. INSERT INTO t3928b VALUES(200);
  586. INSERT INTO t3928b VALUES(300);
  587. DELETE FROM t3928;
  588. CREATE TABLE t3928c(y INTEGER PRIMARY KEY AUTOINCREMENT, z);
  589. CREATE TRIGGER t3928br1 BEFORE DELETE ON t3928b BEGIN
  590. INSERT INTO t3928(b) VALUES('before-del-'||old.x);
  591. INSERT INTO t3928c(z) VALUES('before-del-'||old.x);
  592. END;
  593. CREATE TRIGGER t3928br2 AFTER DELETE ON t3928b BEGIN
  594. INSERT INTO t3928(b) VALUES('after-del-'||old.x);
  595. INSERT INTO t3928c(z) VALUES('after-del-'||old.x);
  596. END;
  597. DELETE FROM t3928b;
  598. SELECT * FROM t3928 ORDER BY a;
  599. }
  600. } {16 before-del-100 17 after-del-100 18 before-del-200 19 after-del-200 20 before-del-300 21 after-del-300}
  601. do_test autoinc-3928.6 {
  602. db eval {
  603. SELECT * FROM t3928c ORDER BY y;
  604. }
  605. } {1 before-del-100 2 after-del-100 3 before-del-200 4 after-del-200 5 before-del-300 6 after-del-300}
  606. do_test autoinc-3928.7 {
  607. db eval {
  608. SELECT * FROM sqlite_sequence WHERE name LIKE 't3928%' ORDER BY name;
  609. }
  610. } {t3928 21 t3928c 6}
  611. # Ticket [a696379c1f0886615541a48b35bd8181a80e88f8]
  612. do_test autoinc-a69637.1 {
  613. db eval {
  614. CREATE TABLE ta69637_1(x INTEGER PRIMARY KEY AUTOINCREMENT, y);
  615. CREATE TABLE ta69637_2(z);
  616. CREATE TRIGGER ra69637_1 AFTER INSERT ON ta69637_2 BEGIN
  617. INSERT INTO ta69637_1(y) VALUES(new.z+1);
  618. END;
  619. INSERT INTO ta69637_2 VALUES(123);
  620. SELECT * FROM ta69637_1;
  621. }
  622. } {1 124}
  623. do_test autoinc-a69637.2 {
  624. db eval {
  625. CREATE VIEW va69637_2 AS SELECT * FROM ta69637_2;
  626. CREATE TRIGGER ra69637_2 INSTEAD OF INSERT ON va69637_2 BEGIN
  627. INSERT INTO ta69637_1(y) VALUES(new.z+10000);
  628. END;
  629. INSERT INTO va69637_2 VALUES(123);
  630. SELECT * FROM ta69637_1;
  631. }
  632. } {1 124 2 10123}
  633. }
  634. finish_test