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.
13 # This file implements tests for the special processing associated
14 # with INTEGER PRIMARY KEY columns.
16 # $Id: intpkey.test,v 1.24 2007/11/29 17:43:28 danielk1977 Exp $
18 set testdir [file dirname $argv0]
19 source $testdir/tester.tcl
21 # Create a table with a primary key and a datatype other than
26 CREATE TABLE t1(a TEXT PRIMARY KEY, b, c);
30 # There should be an index associated with the primary key
34 SELECT name FROM sqlite_master
35 WHERE type='index' AND tbl_name='t1';
37 } {sqlite_autoindex_t1_1}
39 # Now create a table with an integer primary key and verify that
40 # there is no associated index.
45 CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c);
46 SELECT name FROM sqlite_master
47 WHERE type='index' AND tbl_name='t1';
51 # Insert some records into the new table. Specify the primary key
52 # and verify that the key is used as the record number.
56 INSERT INTO t1 VALUES(5,'hello','world');
67 SELECT rowid, * FROM t1;
71 # Attempting to insert a duplicate primary key should give a constraint
75 set r [catch {execsql {
76 INSERT INTO t1 VALUES(5,'second','entry');
79 } {1 {UNIQUE constraint failed: t1.a}}
82 SELECT rowid, * FROM t1;
86 set r [catch {execsql {
87 INSERT INTO t1 VALUES(6,'second','entry');
91 do_test intpkey-1.8.1 {
96 SELECT rowid, * FROM t1;
98 } {5 5 hello world 6 6 second entry}
100 # A ROWID is automatically generated for new records that do not specify
101 # the integer primary key.
103 do_test intpkey-1.10 {
105 INSERT INTO t1(b,c) VALUES('one','two');
106 SELECT b FROM t1 ORDER BY b;
110 # Try to change the ROWID for the new entry.
112 do_test intpkey-1.11 {
114 UPDATE t1 SET a=4 WHERE b='one';
117 } {4 one two 5 hello world 6 second entry}
119 # Make sure SELECT statements are able to use the primary key column
122 do_test intpkey-1.12.1 {
124 SELECT * FROM t1 WHERE a==4;
127 do_test intpkey-1.12.2 {
130 SELECT * FROM t1 WHERE a==4;
134 # Try to insert a non-integer value into the primary key field. This
135 # should result in a data type mismatch.
137 do_test intpkey-1.13.1 {
138 set r [catch {execsql {
139 INSERT INTO t1 VALUES('x','y','z');
142 } {1 {datatype mismatch}}
143 do_test intpkey-1.13.2 {
144 set r [catch {execsql {
145 INSERT INTO t1 VALUES('','y','z');
148 } {1 {datatype mismatch}}
149 do_test intpkey-1.14 {
150 set r [catch {execsql {
151 INSERT INTO t1 VALUES(3.4,'y','z');
154 } {1 {datatype mismatch}}
155 do_test intpkey-1.15 {
156 set r [catch {execsql {
157 INSERT INTO t1 VALUES(-3,'y','z');
161 do_test intpkey-1.16 {
162 execsql {SELECT * FROM t1}
163 } {-3 y z 4 one two 5 hello world 6 second entry}
166 # Check to make sure indices work correctly with integer primary keys
168 do_test intpkey-2.1 {
170 CREATE INDEX i1 ON t1(b);
171 SELECT * FROM t1 WHERE b=='y'
174 do_test intpkey-2.1.1 {
176 SELECT * FROM t1 WHERE b=='y' AND rowid<0
179 do_test intpkey-2.1.2 {
181 SELECT * FROM t1 WHERE b=='y' AND rowid<0 AND rowid>=-20
184 do_test intpkey-2.1.3 {
186 SELECT * FROM t1 WHERE b>='y'
189 do_test intpkey-2.1.4 {
191 SELECT * FROM t1 WHERE b>='y' AND rowid<10
195 do_test intpkey-2.2 {
197 UPDATE t1 SET a=8 WHERE b=='y';
198 SELECT * FROM t1 WHERE b=='y';
201 do_test intpkey-2.3 {
203 SELECT rowid, * FROM t1;
205 } {4 4 one two 5 5 hello world 6 6 second entry 8 8 y z}
206 do_test intpkey-2.4 {
208 SELECT rowid, * FROM t1 WHERE b<'second'
210 } {5 5 hello world 4 4 one two}
211 do_test intpkey-2.4.1 {
213 SELECT rowid, * FROM t1 WHERE 'second'>b
215 } {5 5 hello world 4 4 one two}
216 do_test intpkey-2.4.2 {
218 SELECT rowid, * FROM t1 WHERE 8>rowid AND 'second'>b
220 } {4 4 one two 5 5 hello world}
221 do_test intpkey-2.4.3 {
223 SELECT rowid, * FROM t1 WHERE 8>rowid AND 'second'>b AND 0<rowid
225 } {4 4 one two 5 5 hello world}
226 do_test intpkey-2.5 {
228 SELECT rowid, * FROM t1 WHERE b>'a'
230 } {5 5 hello world 4 4 one two 6 6 second entry 8 8 y z}
231 do_test intpkey-2.6 {
233 DELETE FROM t1 WHERE rowid=4;
234 SELECT * FROM t1 WHERE b>'a';
236 } {5 hello world 6 second entry 8 y z}
237 do_test intpkey-2.7 {
239 UPDATE t1 SET a=-4 WHERE rowid=8;
240 SELECT * FROM t1 WHERE b>'a';
242 } {5 hello world 6 second entry -4 y z}
243 do_test intpkey-2.7 {
247 } {-4 y z 5 hello world 6 second entry}
249 # Do an SQL statement. Append the search count to the end of the result.
252 set ::sqlite_search_count 0
253 return [concat [execsql $sql] $::sqlite_search_count]
256 # Create indices that include the integer primary key as one of their
259 do_test intpkey-3.1 {
261 CREATE INDEX i2 ON t1(a);
264 do_test intpkey-3.2 {
266 SELECT * FROM t1 WHERE a=5;
269 do_test intpkey-3.3 {
271 SELECT * FROM t1 WHERE a>4 AND a<6;
274 do_test intpkey-3.4 {
276 SELECT * FROM t1 WHERE b>='hello' AND b<'hello2';
279 do_test intpkey-3.5 {
281 CREATE INDEX i3 ON t1(c,a);
284 do_test intpkey-3.6 {
286 SELECT * FROM t1 WHERE c=='world';
289 do_test intpkey-3.7 {
290 execsql {INSERT INTO t1 VALUES(11,'hello','world')}
292 SELECT * FROM t1 WHERE c=='world';
294 } {5 hello world 11 hello world 5}
295 do_test intpkey-3.8 {
297 SELECT * FROM t1 WHERE c=='world' AND a>7;
300 do_test intpkey-3.9 {
302 SELECT * FROM t1 WHERE 7<a;
306 # Test inequality constraints on integer primary keys and rowids
308 do_test intpkey-4.1 {
310 SELECT * FROM t1 WHERE 11=rowid
313 do_test intpkey-4.2 {
315 SELECT * FROM t1 WHERE 11=rowid AND b=='hello'
318 do_test intpkey-4.3 {
320 SELECT * FROM t1 WHERE 11=rowid AND b=='hello' AND c IS NOT NULL;
323 do_test intpkey-4.4 {
325 SELECT * FROM t1 WHERE rowid==11
328 do_test intpkey-4.5 {
330 SELECT * FROM t1 WHERE oid==11 AND b=='hello'
333 do_test intpkey-4.6 {
335 SELECT * FROM t1 WHERE a==11 AND b=='hello' AND c IS NOT NULL;
339 do_test intpkey-4.7 {
341 SELECT * FROM t1 WHERE 8<rowid;
344 do_test intpkey-4.8 {
346 SELECT * FROM t1 WHERE 8<rowid AND 11>=oid;
349 do_test intpkey-4.9 {
351 SELECT * FROM t1 WHERE 11<=_rowid_ AND 12>=a;
354 do_test intpkey-4.10 {
356 SELECT * FROM t1 WHERE 0>=_rowid_;
359 do_test intpkey-4.11 {
361 SELECT * FROM t1 WHERE a<0;
364 do_test intpkey-4.12 {
366 SELECT * FROM t1 WHERE a<0 AND a>10;
370 # Make sure it is OK to insert a rowid of 0
372 do_test intpkey-5.1 {
374 INSERT INTO t1 VALUES(0,'zero','entry');
377 SELECT * FROM t1 WHERE a=0;
380 do_test intpkey-5.2 {
382 SELECT rowid, a FROM t1 ORDER BY rowid
384 } {-4 -4 0 0 5 5 6 6 11 11}
386 # Test the ability of the COPY command to put data into a
387 # table that contains an integer primary key.
389 # COPY command has been removed. But we retain these tests so
390 # that the tables will contain the right data for tests that follow.
392 do_test intpkey-6.1 {
395 INSERT INTO t1 VALUES(20,'b-20','c-20');
396 INSERT INTO t1 VALUES(21,'b-21','c-21');
397 INSERT INTO t1 VALUES(22,'b-22','c-22');
399 SELECT * FROM t1 WHERE a>=20;
401 } {20 b-20 c-20 21 b-21 c-21 22 b-22 c-22}
402 do_test intpkey-6.2 {
404 SELECT * FROM t1 WHERE b=='hello'
406 } {5 hello world 11 hello world}
407 do_test intpkey-6.3 {
409 DELETE FROM t1 WHERE b='b-21';
410 SELECT * FROM t1 WHERE b=='b-21';
413 do_test intpkey-6.4 {
415 SELECT * FROM t1 WHERE a>=20
417 } {20 b-20 c-20 22 b-22 c-22}
419 # Do an insert of values with the columns specified out of order.
421 do_test intpkey-7.1 {
423 INSERT INTO t1(c,b,a) VALUES('row','new',30);
424 SELECT * FROM t1 WHERE rowid>=30;
427 do_test intpkey-7.2 {
429 SELECT * FROM t1 WHERE rowid>20;
431 } {22 b-22 c-22 30 new row}
433 # Do an insert from a select statement.
435 do_test intpkey-8.1 {
437 CREATE TABLE t2(x INTEGER PRIMARY KEY, y, z);
438 INSERT INTO t2 SELECT * FROM t1;
439 SELECT rowid FROM t2;
441 } {-4 0 5 6 11 20 22 30}
442 do_test intpkey-8.2 {
446 } {-4 0 5 6 11 20 22 30}
448 do_test intpkey-9.1 {
450 UPDATE t1 SET c='www' WHERE c='world';
451 SELECT rowid, a, c FROM t1 WHERE c=='www';
453 } {5 5 www 11 11 www}
456 # Check insert of NULL for primary key
458 do_test intpkey-10.1 {
461 CREATE TABLE t2(x INTEGER PRIMARY KEY, y, z);
462 INSERT INTO t2 VALUES(NULL, 1, 2);
466 do_test intpkey-10.2 {
468 INSERT INTO t2 VALUES(NULL, 2, 3);
469 SELECT * from t2 WHERE x=2;
472 do_test intpkey-10.3 {
474 INSERT INTO t2 SELECT NULL, z, y FROM t2;
477 } {1 1 2 2 2 3 3 2 1 4 3 2}
479 # This tests checks to see if a floating point number can be used
480 # to reference an integer primary key.
482 do_test intpkey-11.1 {
484 SELECT b FROM t1 WHERE a=2.0+3.0;
487 do_test intpkey-11.1 {
489 SELECT b FROM t1 WHERE a=2.0+3.5;
493 integrity_check intpkey-12.1
495 # Try to use a string that looks like a floating point number as
496 # an integer primary key. This should actually work when the floating
497 # point value can be rounded to an integer without loss of data.
499 do_test intpkey-13.1 {
501 SELECT * FROM t1 WHERE a=1;
504 do_test intpkey-13.2 {
506 INSERT INTO t1 VALUES('1.0',2,3);
507 SELECT * FROM t1 WHERE a=1;
510 do_test intpkey-13.3 {
512 INSERT INTO t1 VALUES('1.5',3,4);
514 } {1 {datatype mismatch}}
515 ifcapable {bloblit} {
516 do_test intpkey-13.4 {
518 INSERT INTO t1 VALUES(x'123456',3,4);
520 } {1 {datatype mismatch}}
522 do_test intpkey-13.5 {
524 INSERT INTO t1 VALUES('+1234567890',3,4);
528 # Compare an INTEGER PRIMARY KEY against a TEXT expression. The INTEGER
529 # affinity should be applied to the text value before the comparison
532 do_test intpkey-14.1 {
534 CREATE TABLE t3(a INTEGER PRIMARY KEY, b INTEGER, c TEXT);
535 INSERT INTO t3 VALUES(1, 1, 'one');
536 INSERT INTO t3 VALUES(2, 2, '2');
537 INSERT INTO t3 VALUES(3, 3, 3);
540 do_test intpkey-14.2 {
542 SELECT * FROM t3 WHERE a>2;
545 do_test intpkey-14.3 {
547 SELECT * FROM t3 WHERE a>'2';
550 do_test intpkey-14.4 {
552 SELECT * FROM t3 WHERE a<'2';
555 do_test intpkey-14.5 {
557 SELECT * FROM t3 WHERE a<c;
560 do_test intpkey-14.6 {
562 SELECT * FROM t3 WHERE a=c;
566 # Check for proper handling of primary keys greater than 2^31.
569 do_test intpkey-15.1 {
571 INSERT INTO t1 VALUES(2147483647, 'big-1', 123);
572 SELECT * FROM t1 WHERE a>2147483648;
575 do_test intpkey-15.2 {
577 INSERT INTO t1 VALUES(NULL, 'big-2', 234);
578 SELECT b FROM t1 WHERE a>=2147483648;
581 do_test intpkey-15.3 {
583 SELECT b FROM t1 WHERE a>2147483648;
586 do_test intpkey-15.4 {
588 SELECT b FROM t1 WHERE a>=2147483647;
591 do_test intpkey-15.5 {
593 SELECT b FROM t1 WHERE a<2147483648;
595 } {y zero 2 hello second hello b-20 b-22 new 3 big-1}
596 do_test intpkey-15.6 {
598 SELECT b FROM t1 WHERE a<12345678901;
600 } {y zero 2 hello second hello b-20 b-22 new 3 big-1 big-2}
601 do_test intpkey-15.7 {
603 SELECT b FROM t1 WHERE a>12345678901;
607 # 2016-04-18 ticket https://www.sqlite.org/src/tktview/7d7525cb01b68712495d3a
608 # Be sure to escape quoted typenames.
610 do_execsql_test intpkey-16.0 {
611 CREATE TABLE t16a(id "INTEGER" PRIMARY KEY AUTOINCREMENT, b [TEXT], c `INT`);
613 do_execsql_test intpkey-16.1 {
614 PRAGMA table_info=t16a;
615 } {0 id INTEGER 0 {} 1 1 b TEXT 0 {} 0 2 c INT 0 {} 0}
617 # 2016-05-06 ticket https://www.sqlite.org/src/tktview/16c9801ceba4923939085
618 # When the schema contains an index on the IPK and no other index
619 # and a WHERE clause on a delete uses an OR where both sides referencing
620 # the IPK, then it is possible that the OP_Delete will fail because there
621 # deferred seek of the OP_Seek is not resolved prior to reaching the OP_Delete.
623 do_execsql_test intpkey-17.0 {
624 CREATE TABLE t17(x INTEGER PRIMARY KEY, y TEXT);
625 INSERT INTO t17(x,y) VALUES(123,'elephant'),(248,'giraffe');
626 CREATE INDEX t17x ON t17(x);
627 DELETE FROM t17 WHERE x=99 OR x<130;
630 do_execsql_test intpkey-17.1 {
633 INSERT INTO t17(x,y) VALUES(123,'elephant'),(248,'giraffe');
634 CREATE UNIQUE INDEX t17x ON t17(abs(x));
635 DELETE FROM t17 WHERE abs(x) IS NULL OR abs(x)<130;
638 do_execsql_test intpkey-17.2 {
640 INSERT INTO t17(x,y) VALUES(123,'elephant'),(248,'giraffe');
641 UPDATE t17 SET y='ostrich' WHERE abs(x)=248;
642 SELECT * FROM t17 ORDER BY +x;
643 } {123 elephant 248 ostrich}