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.5 2005/03/29 03:11:00 danielk1977 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 # The database is initially empty.
32 SELECT name FROM sqlite_master WHERE type='table';
36 # Add a table with the AUTOINCREMENT feature. Verify that the
37 # SQLITE_SEQUENCE table gets created.
41 CREATE TABLE t1(x INTEGER PRIMARY KEY AUTOINCREMENT, y);
42 SELECT name FROM sqlite_master WHERE type='table';
44 } {t1 sqlite_sequence}
46 # The SQLITE_SEQUENCE table is initially empty
50 SELECT * FROM sqlite_sequence;
54 # Close and reopen the database. Verify that everything is still there.
60 SELECT * FROM sqlite_sequence;
64 # We are not allowed to drop the sqlite_sequence table.
67 catchsql {DROP TABLE sqlite_sequence}
68 } {1 {table sqlite_sequence may not be dropped}}
70 execsql {SELECT name FROM sqlite_master WHERE type='table'}
71 } {t1 sqlite_sequence}
73 # Insert an entries into the t1 table and make sure the largest key
74 # is always recorded in the sqlite_sequence table.
78 SELECT * FROM sqlite_sequence
83 INSERT INTO t1 VALUES(12,34);
84 SELECT * FROM sqlite_sequence;
89 INSERT INTO t1 VALUES(1,23);
90 SELECT * FROM sqlite_sequence;
95 INSERT INTO t1 VALUES(123,456);
96 SELECT * FROM sqlite_sequence;
101 INSERT INTO t1 VALUES(NULL,567);
102 SELECT * FROM sqlite_sequence;
105 do_test autoinc-2.6 {
107 DELETE FROM t1 WHERE y=567;
108 SELECT * FROM sqlite_sequence;
111 do_test autoinc-2.7 {
113 INSERT INTO t1 VALUES(NULL,567);
114 SELECT * FROM sqlite_sequence;
117 do_test autoinc-2.8 {
120 SELECT * FROM sqlite_sequence;
123 do_test autoinc-2.9 {
125 INSERT INTO t1 VALUES(12,34);
126 SELECT * FROM sqlite_sequence;
129 do_test autoinc-2.10 {
131 INSERT INTO t1 VALUES(125,456);
132 SELECT * FROM sqlite_sequence;
135 do_test autoinc-2.11 {
137 INSERT INTO t1 VALUES(-1234567,-1);
138 SELECT * FROM sqlite_sequence;
141 do_test autoinc-2.12 {
143 INSERT INTO t1 VALUES(234,5678);
144 SELECT * FROM sqlite_sequence;
147 do_test autoinc-2.13 {
150 INSERT INTO t1 VALUES(NULL,1);
151 SELECT * FROM sqlite_sequence;
154 do_test autoinc-2.14 {
160 # Manually change the autoincrement values in sqlite_sequence.
162 do_test autoinc-2.20 {
164 UPDATE sqlite_sequence SET seq=1234 WHERE name='t1';
165 INSERT INTO t1 VALUES(NULL,2);
169 do_test autoinc-2.21 {
171 SELECT * FROM sqlite_sequence;
174 do_test autoinc-2.22 {
176 UPDATE sqlite_sequence SET seq=NULL WHERE name='t1';
177 INSERT INTO t1 VALUES(NULL,3);
180 } {235 1 1235 2 1236 3}
181 do_test autoinc-2.23 {
183 SELECT * FROM sqlite_sequence;
186 do_test autoinc-2.24 {
188 UPDATE sqlite_sequence SET seq='a-string' WHERE name='t1';
189 INSERT INTO t1 VALUES(NULL,4);
192 } {235 1 1235 2 1236 3 1237 4}
193 do_test autoinc-2.25 {
195 SELECT * FROM sqlite_sequence;
198 do_test autoinc-2.26 {
200 DELETE FROM sqlite_sequence WHERE name='t1';
201 INSERT INTO t1 VALUES(NULL,5);
204 } {235 1 1235 2 1236 3 1237 4 1238 5}
205 do_test autoinc-2.27 {
207 SELECT * FROM sqlite_sequence;
210 do_test autoinc-2.28 {
212 UPDATE sqlite_sequence SET seq='12345678901234567890'
214 INSERT INTO t1 VALUES(NULL,6);
217 } {235 1 1235 2 1236 3 1237 4 1238 5 1239 6}
218 do_test autoinc-2.29 {
220 SELECT * FROM sqlite_sequence;
224 # Test multi-row inserts
226 do_test autoinc-2.50 {
228 DELETE FROM t1 WHERE y>=3;
229 INSERT INTO t1 SELECT NULL, y+2 FROM t1;
232 } {235 1 1235 2 1240 3 1241 4}
233 do_test autoinc-2.51 {
235 SELECT * FROM sqlite_sequence
240 do_test autoinc-2.52 {
242 CREATE TEMP TABLE t2 AS SELECT y FROM t1;
243 INSERT INTO t1 SELECT NULL, y+4 FROM t2;
246 } {235 1 1235 2 1240 3 1241 4 1242 5 1243 6 1244 7 1245 8}
247 do_test autoinc-2.53 {
249 SELECT * FROM sqlite_sequence
252 do_test autoinc-2.54 {
255 INSERT INTO t1 SELECT NULL, y FROM t2;
258 } {1246 1 1247 2 1248 3 1249 4}
259 do_test autoinc-2.55 {
261 SELECT * FROM sqlite_sequence
266 # Create multiple AUTOINCREMENT tables. Make sure all sequences are
267 # tracked separately and do not interfere with one another.
269 do_test autoinc-2.70 {
274 CREATE TABLE t2(d, e INTEGER PRIMARY KEY AUTOINCREMENT, f);
275 INSERT INTO t2(d) VALUES(1);
276 SELECT * FROM sqlite_sequence;
278 } [ifcapable tempdb {list t1 1249 t2 1} else {list t1 1241 t2 1}]
279 do_test autoinc-2.71 {
281 INSERT INTO t2(d) VALUES(2);
282 SELECT * FROM sqlite_sequence;
284 } [ifcapable tempdb {list t1 1249 t2 2} else {list t1 1241 t2 2}]
285 do_test autoinc-2.72 {
287 INSERT INTO t1(x) VALUES(10000);
288 SELECT * FROM sqlite_sequence;
291 do_test autoinc-2.73 {
293 CREATE TABLE t3(g INTEGER PRIMARY KEY AUTOINCREMENT, h);
294 INSERT INTO t3(h) VALUES(1);
295 SELECT * FROM sqlite_sequence;
297 } {t1 10000 t2 2 t3 1}
298 do_test autoinc-2.74 {
300 INSERT INTO t2(d,e) VALUES(3,100);
301 SELECT * FROM sqlite_sequence;
303 } {t1 10000 t2 100 t3 1}
306 # When a table with an AUTOINCREMENT is deleted, the corresponding entry
307 # in the SQLITE_SEQUENCE table should also be deleted. But the SQLITE_SEQUENCE
308 # table itself should remain behind.
310 do_test autoinc-3.1 {
311 execsql {SELECT name FROM sqlite_sequence}
313 do_test autoinc-3.2 {
316 SELECT name FROM sqlite_sequence;
319 do_test autoinc-3.3 {
322 SELECT name FROM sqlite_sequence;
325 do_test autoinc-3.4 {
328 SELECT name FROM sqlite_sequence;
332 # AUTOINCREMENT on TEMP tables.
335 do_test autoinc-4.1 {
337 SELECT 1, name FROM sqlite_master WHERE type='table';
338 SELECT 2, name FROM sqlite_temp_master WHERE type='table';
340 } {1 sqlite_sequence}
341 do_test autoinc-4.2 {
343 CREATE TABLE t1(x INTEGER PRIMARY KEY AUTOINCREMENT, y);
344 CREATE TEMP TABLE t3(a INTEGER PRIMARY KEY AUTOINCREMENT, b);
345 SELECT 1, name FROM sqlite_master WHERE type='table';
346 SELECT 2, name FROM sqlite_temp_master WHERE type='table';
348 } {1 sqlite_sequence 1 t1 2 t3 2 sqlite_sequence}
349 do_test autoinc-4.3 {
351 SELECT 1, * FROM main.sqlite_sequence;
352 SELECT 2, * FROM temp.sqlite_sequence;
355 do_test autoinc-4.4 {
357 INSERT INTO t1 VALUES(10,1);
358 INSERT INTO t3 VALUES(20,2);
359 INSERT INTO t1 VALUES(NULL,3);
360 INSERT INTO t3 VALUES(NULL,4);
365 do_test autoinc-4.4.1 {
367 SELECT * FROM t1 UNION ALL SELECT * FROM t3;
369 } {10 1 11 3 20 2 21 4}
370 } ;# ifcapable compound
372 do_test autoinc-4.5 {
374 SELECT 1, * FROM main.sqlite_sequence;
375 SELECT 2, * FROM temp.sqlite_sequence;
378 do_test autoinc-4.6 {
380 INSERT INTO t1 SELECT * FROM t3;
381 SELECT 1, * FROM main.sqlite_sequence;
382 SELECT 2, * FROM temp.sqlite_sequence;
385 do_test autoinc-4.7 {
387 INSERT INTO t3 SELECT x+100, y FROM t1;
388 SELECT 1, * FROM main.sqlite_sequence;
389 SELECT 2, * FROM temp.sqlite_sequence;
392 do_test autoinc-4.8 {
395 SELECT 1, * FROM main.sqlite_sequence;
396 SELECT 2, * FROM temp.sqlite_sequence;
399 do_test autoinc-4.9 {
401 CREATE TEMP TABLE t2(p INTEGER PRIMARY KEY AUTOINCREMENT, q);
402 INSERT INTO t2 SELECT * FROM t1;
404 SELECT 1, * FROM main.sqlite_sequence;
405 SELECT 2, * FROM temp.sqlite_sequence;
408 do_test autoinc-4.10 {
411 SELECT 1, * FROM main.sqlite_sequence;
412 SELECT 2, * FROM temp.sqlite_sequence;
417 # Make sure AUTOINCREMENT works on ATTACH-ed tables.
420 do_test autoinc-5.1 {
421 file delete -force test2.db
422 file delete -force test2.db-journal
425 CREATE TABLE t4(m INTEGER PRIMARY KEY AUTOINCREMENT, n);
426 CREATE TABLE t5(o, p INTEGER PRIMARY KEY AUTOINCREMENT);
429 ATTACH 'test2.db' as aux;
430 SELECT 1, * FROM main.sqlite_sequence;
431 SELECT 2, * FROM temp.sqlite_sequence;
432 SELECT 3, * FROM aux.sqlite_sequence;
435 do_test autoinc-5.2 {
437 INSERT INTO t4 VALUES(NULL,1);
438 SELECT 1, * FROM main.sqlite_sequence;
439 SELECT 2, * FROM temp.sqlite_sequence;
440 SELECT 3, * FROM aux.sqlite_sequence;
443 do_test autoinc-5.3 {
445 INSERT INTO t5 VALUES(100,200);
446 SELECT * FROM sqlite_sequence
449 do_test autoinc-5.4 {
451 SELECT 1, * FROM main.sqlite_sequence;
452 SELECT 2, * FROM temp.sqlite_sequence;
453 SELECT 3, * FROM aux.sqlite_sequence;
458 # Requirement REQ00310: Make sure an insert fails if the sequence is
459 # already at its maximum value.
461 ifcapable {rowid32} {
462 do_test autoinc-6.1 {
464 CREATE TABLE t6(v INTEGER PRIMARY KEY AUTOINCREMENT, w);
465 INSERT INTO t6 VALUES(2147483647,1);
466 SELECT seq FROM main.sqlite_sequence WHERE name='t6';
470 ifcapable {!rowid32} {
471 do_test autoinc-6.1 {
473 CREATE TABLE t6(v INTEGER PRIMARY KEY AUTOINCREMENT, w);
474 INSERT INTO t6 VALUES(9223372036854775807,1);
475 SELECT seq FROM main.sqlite_sequence WHERE name='t6';
477 } 9223372036854775807
479 do_test autoinc-6.2 {
481 INSERT INTO t6 VALUES(NULL,1);
483 } {1 {database is full}}
485 # Allow the AUTOINCREMENT keyword inside the parentheses
486 # on a separate PRIMARY KEY designation.
488 do_test autoinc-7.1 {
490 CREATE TABLE t7(x INTEGER, y REAL, PRIMARY KEY(x AUTOINCREMENT));
491 INSERT INTO t7(y) VALUES(123);
492 INSERT INTO t7(y) VALUES(234);
494 INSERT INTO t7(y) VALUES(345);
499 # Test that if the AUTOINCREMENT is applied to a non integer primary key
500 # the error message is sensible.
501 do_test autoinc-7.2 {
503 CREATE TABLE t8(x TEXT PRIMARY KEY AUTOINCREMENT);
505 } {1 {AUTOINCREMENT is only allowed on an INTEGER PRIMARY KEY}}