3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
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.
10 #*************************************************************************
11 # This file implements regression tests for SQLite library. The
12 # focus of this script is testing the AUTOINCREMENT features.
14 # $Id: autoinc.test,v 1.14 2009/06/23 20:28:54 drh Exp $
17 set testdir [file dirname $argv0]
18 source $testdir/tester.tcl
20 # If the library is not compiled with autoincrement support then
21 # skip all tests in this file.
23 ifcapable {!autoinc} {
28 sqlite3_db_config_lookaside db 0 0 0
30 # The database is initially empty.
34 SELECT name FROM sqlite_master WHERE type='table';
38 # Add a table with the AUTOINCREMENT feature. Verify that the
39 # SQLITE_SEQUENCE table gets created.
43 CREATE TABLE t1(x INTEGER PRIMARY KEY AUTOINCREMENT, y);
44 SELECT name FROM sqlite_master WHERE type='table';
46 } {t1 sqlite_sequence}
48 # The SQLITE_SEQUENCE table is initially empty
52 SELECT * FROM sqlite_sequence;
55 do_test autoinc-1.3.1 {
57 CREATE INDEX seqidx ON sqlite_sequence(name)
59 } {1 {table sqlite_sequence may not be indexed}}
61 # Close and reopen the database. Verify that everything is still there.
67 SELECT * FROM sqlite_sequence;
71 # We are not allowed to drop the sqlite_sequence table.
74 catchsql {DROP TABLE sqlite_sequence}
75 } {1 {table sqlite_sequence may not be dropped}}
77 execsql {SELECT name FROM sqlite_master WHERE type='table'}
78 } {t1 sqlite_sequence}
80 # Insert an entries into the t1 table and make sure the largest key
81 # is always recorded in the sqlite_sequence table.
85 SELECT * FROM sqlite_sequence
90 INSERT INTO t1 VALUES(12,34);
91 SELECT * FROM sqlite_sequence;
96 INSERT INTO t1 VALUES(1,23);
97 SELECT * FROM sqlite_sequence;
100 do_test autoinc-2.4 {
102 INSERT INTO t1 VALUES(123,456);
103 SELECT * FROM sqlite_sequence;
106 do_test autoinc-2.5 {
108 INSERT INTO t1 VALUES(NULL,567);
109 SELECT * FROM sqlite_sequence;
112 do_test autoinc-2.6 {
114 DELETE FROM t1 WHERE y=567;
115 SELECT * FROM sqlite_sequence;
118 do_test autoinc-2.7 {
120 INSERT INTO t1 VALUES(NULL,567);
121 SELECT * FROM sqlite_sequence;
124 do_test autoinc-2.8 {
127 SELECT * FROM sqlite_sequence;
130 do_test autoinc-2.9 {
132 INSERT INTO t1 VALUES(12,34);
133 SELECT * FROM sqlite_sequence;
136 do_test autoinc-2.10 {
138 INSERT INTO t1 VALUES(125,456);
139 SELECT * FROM sqlite_sequence;
142 do_test autoinc-2.11 {
144 INSERT INTO t1 VALUES(-1234567,-1);
145 SELECT * FROM sqlite_sequence;
148 do_test autoinc-2.12 {
150 INSERT INTO t1 VALUES(234,5678);
151 SELECT * FROM sqlite_sequence;
154 do_test autoinc-2.13 {
157 INSERT INTO t1 VALUES(NULL,1);
158 SELECT * FROM sqlite_sequence;
161 do_test autoinc-2.14 {
167 # Manually change the autoincrement values in sqlite_sequence.
169 do_test autoinc-2.20 {
171 UPDATE sqlite_sequence SET seq=1234 WHERE name='t1';
172 INSERT INTO t1 VALUES(NULL,2);
176 do_test autoinc-2.21 {
178 SELECT * FROM sqlite_sequence;
181 do_test autoinc-2.22 {
183 UPDATE sqlite_sequence SET seq=NULL WHERE name='t1';
184 INSERT INTO t1 VALUES(NULL,3);
187 } {235 1 1235 2 1236 3}
188 do_test autoinc-2.23 {
190 SELECT * FROM sqlite_sequence;
193 do_test autoinc-2.24 {
195 UPDATE sqlite_sequence SET seq='a-string' WHERE name='t1';
196 INSERT INTO t1 VALUES(NULL,4);
199 } {235 1 1235 2 1236 3 1237 4}
200 do_test autoinc-2.25 {
202 SELECT * FROM sqlite_sequence;
205 do_test autoinc-2.26 {
207 DELETE FROM sqlite_sequence WHERE name='t1';
208 INSERT INTO t1 VALUES(NULL,5);
211 } {235 1 1235 2 1236 3 1237 4 1238 5}
212 do_test autoinc-2.27 {
214 SELECT * FROM sqlite_sequence;
217 do_test autoinc-2.28 {
219 UPDATE sqlite_sequence SET seq='-12345678901234567890'
221 INSERT INTO t1 VALUES(NULL,6);
224 } {235 1 1235 2 1236 3 1237 4 1238 5 1239 6}
225 do_test autoinc-2.29 {
227 SELECT * FROM sqlite_sequence;
231 # Test multi-row inserts
233 do_test autoinc-2.50 {
235 DELETE FROM t1 WHERE y>=3;
236 INSERT INTO t1 SELECT NULL, y+2 FROM t1;
239 } {235 1 1235 2 1240 3 1241 4}
240 do_test autoinc-2.51 {
242 SELECT * FROM sqlite_sequence
247 do_test autoinc-2.52 {
249 CREATE TEMP TABLE t2 AS SELECT y FROM t1;
252 INSERT INTO t1 SELECT NULL, y+4 FROM t2;
255 } {235 1 1235 2 1240 3 1241 4 1242 5 1243 6 1244 7 1245 8}
256 do_test autoinc-2.53 {
258 SELECT * FROM sqlite_sequence
261 do_test autoinc-2.54 {
264 INSERT INTO t1 SELECT NULL, y FROM t2;
267 } {1246 1 1247 2 1248 3 1249 4}
268 do_test autoinc-2.55 {
270 SELECT * FROM sqlite_sequence
275 # Create multiple AUTOINCREMENT tables. Make sure all sequences are
276 # tracked separately and do not interfere with one another.
278 do_test autoinc-2.70 {
283 CREATE TABLE t2(d, e INTEGER PRIMARY KEY AUTOINCREMENT, f);
284 INSERT INTO t2(d) VALUES(1);
285 SELECT * FROM sqlite_sequence;
287 } [ifcapable tempdb {list t1 1249 t2 1} else {list t1 1241 t2 1}]
288 do_test autoinc-2.71 {
290 INSERT INTO t2(d) VALUES(2);
291 SELECT * FROM sqlite_sequence;
293 } [ifcapable tempdb {list t1 1249 t2 2} else {list t1 1241 t2 2}]
294 do_test autoinc-2.72 {
296 INSERT INTO t1(x) VALUES(10000);
297 SELECT * FROM sqlite_sequence;
300 do_test autoinc-2.73 {
302 CREATE TABLE t3(g INTEGER PRIMARY KEY AUTOINCREMENT, h);
303 INSERT INTO t3(h) VALUES(1);
304 SELECT * FROM sqlite_sequence;
306 } {t1 10000 t2 2 t3 1}
307 do_test autoinc-2.74 {
309 INSERT INTO t2(d,e) VALUES(3,100);
310 SELECT * FROM sqlite_sequence;
312 } {t1 10000 t2 100 t3 1}
315 # When a table with an AUTOINCREMENT is deleted, the corresponding entry
316 # in the SQLITE_SEQUENCE table should also be deleted. But the SQLITE_SEQUENCE
317 # table itself should remain behind.
319 do_test autoinc-3.1 {
320 execsql {SELECT name FROM sqlite_sequence}
322 do_test autoinc-3.2 {
325 SELECT name FROM sqlite_sequence;
328 do_test autoinc-3.3 {
331 SELECT name FROM sqlite_sequence;
334 do_test autoinc-3.4 {
337 SELECT name FROM sqlite_sequence;
341 # AUTOINCREMENT on TEMP tables.
344 do_test autoinc-4.1 {
346 SELECT 1, name FROM sqlite_master WHERE type='table';
347 SELECT 2, name FROM sqlite_temp_master WHERE type='table';
349 } {1 sqlite_sequence}
350 do_test autoinc-4.2 {
352 CREATE TABLE t1(x INTEGER PRIMARY KEY AUTOINCREMENT, y);
353 CREATE TEMP TABLE t3(a INTEGER PRIMARY KEY AUTOINCREMENT, b);
354 SELECT 1, name FROM sqlite_master WHERE type='table';
355 SELECT 2, name FROM sqlite_temp_master WHERE type='table';
357 } {1 sqlite_sequence 1 t1 2 t3 2 sqlite_sequence}
358 do_test autoinc-4.3 {
360 SELECT 1, * FROM main.sqlite_sequence;
361 SELECT 2, * FROM temp.sqlite_sequence;
364 do_test autoinc-4.4 {
366 INSERT INTO t1 VALUES(10,1);
367 INSERT INTO t3 VALUES(20,2);
368 INSERT INTO t1 VALUES(NULL,3);
369 INSERT INTO t3 VALUES(NULL,4);
374 do_test autoinc-4.4.1 {
376 SELECT * FROM t1 UNION ALL SELECT * FROM t3;
378 } {10 1 11 3 20 2 21 4}
379 } ;# ifcapable compound
381 do_test autoinc-4.5 {
383 SELECT 1, * FROM main.sqlite_sequence;
384 SELECT 2, * FROM temp.sqlite_sequence;
387 do_test autoinc-4.6 {
389 INSERT INTO t1 SELECT * FROM t3;
390 SELECT 1, * FROM main.sqlite_sequence;
391 SELECT 2, * FROM temp.sqlite_sequence;
394 do_test autoinc-4.7 {
396 INSERT INTO t3 SELECT x+100, y FROM t1;
397 SELECT 1, * FROM main.sqlite_sequence;
398 SELECT 2, * FROM temp.sqlite_sequence;
401 do_test autoinc-4.8 {
404 SELECT 1, * FROM main.sqlite_sequence;
405 SELECT 2, * FROM temp.sqlite_sequence;
408 do_test autoinc-4.9 {
410 CREATE TEMP TABLE t2(p INTEGER PRIMARY KEY AUTOINCREMENT, q);
411 INSERT INTO t2 SELECT * FROM t1;
413 SELECT 1, * FROM main.sqlite_sequence;
414 SELECT 2, * FROM temp.sqlite_sequence;
417 do_test autoinc-4.10 {
420 SELECT 1, * FROM main.sqlite_sequence;
421 SELECT 2, * FROM temp.sqlite_sequence;
426 # Make sure AUTOINCREMENT works on ATTACH-ed tables.
428 ifcapable tempdb&&attach {
429 do_test autoinc-5.1 {
431 forcedelete test2.db-journal
434 CREATE TABLE t4(m INTEGER PRIMARY KEY AUTOINCREMENT, n);
435 CREATE TABLE t5(o, p INTEGER PRIMARY KEY AUTOINCREMENT);
438 ATTACH 'test2.db' as aux;
439 SELECT 1, * FROM main.sqlite_sequence;
440 SELECT 2, * FROM temp.sqlite_sequence;
441 SELECT 3, * FROM aux.sqlite_sequence;
444 do_test autoinc-5.2 {
446 INSERT INTO t4 VALUES(NULL,1);
447 SELECT 1, * FROM main.sqlite_sequence;
448 SELECT 2, * FROM temp.sqlite_sequence;
449 SELECT 3, * FROM aux.sqlite_sequence;
452 do_test autoinc-5.3 {
454 INSERT INTO t5 VALUES(100,200);
455 SELECT * FROM sqlite_sequence
458 do_test autoinc-5.4 {
460 SELECT 1, * FROM main.sqlite_sequence;
461 SELECT 2, * FROM temp.sqlite_sequence;
462 SELECT 3, * FROM aux.sqlite_sequence;
467 # Requirement REQ00310: Make sure an insert fails if the sequence is
468 # already at its maximum value.
470 ifcapable {rowid32} {
471 do_test autoinc-6.1 {
473 CREATE TABLE t6(v INTEGER PRIMARY KEY AUTOINCREMENT, w);
474 INSERT INTO t6 VALUES(2147483647,1);
475 SELECT seq FROM main.sqlite_sequence WHERE name='t6';
479 ifcapable {!rowid32} {
480 do_test autoinc-6.1 {
482 CREATE TABLE t6(v INTEGER PRIMARY KEY AUTOINCREMENT, w);
483 INSERT INTO t6 VALUES(9223372036854775807,1);
484 SELECT seq FROM main.sqlite_sequence WHERE name='t6';
486 } 9223372036854775807
488 do_test autoinc-6.2 {
490 INSERT INTO t6 VALUES(NULL,1);
492 } {1 {database or disk is full}}
494 # Allow the AUTOINCREMENT keyword inside the parentheses
495 # on a separate PRIMARY KEY designation.
497 do_test autoinc-7.1 {
499 CREATE TABLE t7(x INTEGER, y REAL, PRIMARY KEY(x AUTOINCREMENT));
500 INSERT INTO t7(y) VALUES(123);
501 INSERT INTO t7(y) VALUES(234);
503 INSERT INTO t7(y) VALUES(345);
508 # Test that if the AUTOINCREMENT is applied to a non integer primary key
509 # the error message is sensible.
510 do_test autoinc-7.2 {
512 CREATE TABLE t8(x TEXT PRIMARY KEY AUTOINCREMENT);
514 } {1 {AUTOINCREMENT is only allowed on an INTEGER PRIMARY KEY}}
517 # Ticket #1283. Make sure that preparing but never running a statement
518 # that creates the sqlite_sequence table does not mess up the database.
520 do_test autoinc-8.1 {
525 set DB [sqlite3_connection_pointer db]
526 set STMT [sqlite3_prepare $DB {
528 x INTEGER PRIMARY KEY AUTOINCREMENT
531 sqlite3_finalize $STMT
532 set STMT [sqlite3_prepare $DB {
534 x INTEGER PRIMARY KEY AUTOINCREMENT
538 sqlite3_finalize $STMT
540 INSERT INTO t1 VALUES(NULL);
546 # Make sure the sqlite_sequence table is not damaged when doing
547 # an empty insert - an INSERT INTO ... SELECT ... where the SELECT
548 # clause returns an empty set.
550 do_test autoinc-9.1 {
552 CREATE TABLE t2(x INTEGER PRIMARY KEY AUTOINCREMENT, y);
553 INSERT INTO t2 VALUES(NULL, 1);
554 CREATE TABLE t3(a INTEGER PRIMARY KEY AUTOINCREMENT, b);
555 INSERT INTO t3 SELECT * FROM t2 WHERE y>1;
557 SELECT * FROM sqlite_sequence WHERE name='t3';
562 catchsql { pragma recursive_triggers = off }
564 # Ticket #3928. Make sure that triggers to not make extra slots in
565 # the SQLITE_SEQUENCE table.
567 do_test autoinc-3928.1 {
569 CREATE TABLE t3928(a INTEGER PRIMARY KEY AUTOINCREMENT, b);
570 CREATE TRIGGER t3928r1 BEFORE INSERT ON t3928 BEGIN
571 INSERT INTO t3928(b) VALUES('before1');
572 INSERT INTO t3928(b) VALUES('before2');
574 CREATE TRIGGER t3928r2 AFTER INSERT ON t3928 BEGIN
575 INSERT INTO t3928(b) VALUES('after1');
576 INSERT INTO t3928(b) VALUES('after2');
578 INSERT INTO t3928(b) VALUES('test');
579 SELECT * FROM t3928 ORDER BY a;
581 } {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}
582 do_test autoinc-3928.2 {
584 SELECT * FROM sqlite_sequence WHERE name='t3928'
588 do_test autoinc-3928.3 {
590 DROP TRIGGER t3928r1;
591 DROP TRIGGER t3928r2;
592 CREATE TRIGGER t3928r3 BEFORE UPDATE ON t3928
593 WHEN typeof(new.b)=='integer' BEGIN
594 INSERT INTO t3928(b) VALUES('before-int-' || new.b);
596 CREATE TRIGGER t3928r4 AFTER UPDATE ON t3928
597 WHEN typeof(new.b)=='integer' BEGIN
598 INSERT INTO t3928(b) VALUES('after-int-' || new.b);
600 DELETE FROM t3928 WHERE a!=1;
601 UPDATE t3928 SET b=456 WHERE a=1;
602 SELECT * FROM t3928 ORDER BY a;
604 } {1 456 14 before-int-456 15 after-int-456}
605 do_test autoinc-3928.4 {
607 SELECT * FROM sqlite_sequence WHERE name='t3928'
611 do_test autoinc-3928.5 {
613 CREATE TABLE t3928b(x);
614 INSERT INTO t3928b VALUES(100);
615 INSERT INTO t3928b VALUES(200);
616 INSERT INTO t3928b VALUES(300);
618 CREATE TABLE t3928c(y INTEGER PRIMARY KEY AUTOINCREMENT, z);
619 CREATE TRIGGER t3928br1 BEFORE DELETE ON t3928b BEGIN
620 INSERT INTO t3928(b) VALUES('before-del-'||old.x);
621 INSERT INTO t3928c(z) VALUES('before-del-'||old.x);
623 CREATE TRIGGER t3928br2 AFTER DELETE ON t3928b BEGIN
624 INSERT INTO t3928(b) VALUES('after-del-'||old.x);
625 INSERT INTO t3928c(z) VALUES('after-del-'||old.x);
628 SELECT * FROM t3928 ORDER BY a;
630 } {16 before-del-100 17 after-del-100 18 before-del-200 19 after-del-200 20 before-del-300 21 after-del-300}
631 do_test autoinc-3928.6 {
633 SELECT * FROM t3928c ORDER BY y;
635 } {1 before-del-100 2 after-del-100 3 before-del-200 4 after-del-200 5 before-del-300 6 after-del-300}
636 do_test autoinc-3928.7 {
638 SELECT * FROM sqlite_sequence WHERE name LIKE 't3928%' ORDER BY name;
640 } {t3928 21 t3928c 6}
642 # Ticket [a696379c1f0886615541a48b35bd8181a80e88f8]
643 do_test autoinc-a69637.1 {
645 CREATE TABLE ta69637_1(x INTEGER PRIMARY KEY AUTOINCREMENT, y);
646 CREATE TABLE ta69637_2(z);
647 CREATE TRIGGER ra69637_1 AFTER INSERT ON ta69637_2 BEGIN
648 INSERT INTO ta69637_1(y) VALUES(new.z+1);
650 INSERT INTO ta69637_2 VALUES(123);
651 SELECT * FROM ta69637_1;
654 do_test autoinc-a69637.2 {
656 CREATE VIEW va69637_2 AS SELECT * FROM ta69637_2;
657 CREATE TRIGGER ra69637_2 INSTEAD OF INSERT ON va69637_2 BEGIN
658 INSERT INTO ta69637_1(y) VALUES(new.z+10000);
660 INSERT INTO va69637_2 VALUES(123);
661 SELECT * FROM ta69637_1;