2 #pragma ident "%Z%%M% %I% %E% SMI"
6 # The author disclaims copyright to this source code. In place of
7 # a legal notice, here is a blessing:
9 # May you do good and not evil.
10 # May you find forgiveness for yourself and forgive others.
11 # May you share freely, never taking more than you give.
13 #***********************************************************************
14 # This file implements regression tests for SQLite library.
16 # This file implements tests for the special processing associated
17 # with INTEGER PRIMARY KEY columns.
19 # $Id: intpkey.test,v 1.14 2003/06/15 23:42:25 drh Exp $
21 set testdir [file dirname $argv0]
22 source $testdir/tester.tcl
24 # Create a table with a primary key and a datatype other than
29 CREATE TABLE t1(a TEXT PRIMARY KEY, b, c);
33 # There should be an index associated with the primary key
37 SELECT name FROM sqlite_master
38 WHERE type='index' AND tbl_name='t1';
40 } {{(t1 autoindex 1)}}
42 # Now create a table with an integer primary key and verify that
43 # there is no associated index.
48 CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c);
49 SELECT name FROM sqlite_master
50 WHERE type='index' AND tbl_name='t1';
54 # Insert some records into the new table. Specify the primary key
55 # and verify that the key is used as the record number.
59 INSERT INTO t1 VALUES(5,'hello','world');
70 SELECT rowid, * FROM t1;
74 # Attempting to insert a duplicate primary key should give a constraint
78 set r [catch {execsql {
79 INSERT INTO t1 VALUES(5,'second','entry');
82 } {1 {PRIMARY KEY must be unique}}
85 SELECT rowid, * FROM t1;
89 set r [catch {execsql {
90 INSERT INTO t1 VALUES(6,'second','entry');
94 do_test intpkey-1.8.1 {
99 SELECT rowid, * FROM t1;
101 } {5 5 hello world 6 6 second entry}
103 # A ROWID is automatically generated for new records that do not specify
104 # the integer primary key.
106 do_test intpkey-1.10 {
108 INSERT INTO t1(b,c) VALUES('one','two');
109 SELECT b FROM t1 ORDER BY b;
113 # Try to change the ROWID for the new entry.
115 do_test intpkey-1.11 {
117 UPDATE t1 SET a=4 WHERE b='one';
120 } {4 one two 5 hello world 6 second entry}
122 # Make sure SELECT statements are able to use the primary key column
125 do_test intpkey-1.12 {
127 SELECT * FROM t1 WHERE a==4;
131 # Try to insert a non-integer value into the primary key field. This
132 # should result in a data type mismatch.
134 do_test intpkey-1.13.1 {
135 set r [catch {execsql {
136 INSERT INTO t1 VALUES('x','y','z');
139 } {1 {datatype mismatch}}
140 do_test intpkey-1.13.2 {
141 set r [catch {execsql {
142 INSERT INTO t1 VALUES('','y','z');
145 } {1 {datatype mismatch}}
146 do_test intpkey-1.14 {
147 set r [catch {execsql {
148 INSERT INTO t1 VALUES(3.4,'y','z');
151 } {1 {datatype mismatch}}
152 do_test intpkey-1.15 {
153 set r [catch {execsql {
154 INSERT INTO t1 VALUES(-3,'y','z');
158 do_test intpkey-1.16 {
159 execsql {SELECT * FROM t1}
160 } {-3 y z 4 one two 5 hello world 6 second entry}
163 # Check to make sure indices work correctly with integer primary keys
165 do_test intpkey-2.1 {
167 CREATE INDEX i1 ON t1(b);
168 SELECT * FROM t1 WHERE b=='y'
171 do_test intpkey-2.1.1 {
173 SELECT * FROM t1 WHERE b=='y' AND rowid<0
176 do_test intpkey-2.1.2 {
178 SELECT * FROM t1 WHERE b=='y' AND rowid<0 AND rowid>=-20
181 do_test intpkey-2.1.3 {
183 SELECT * FROM t1 WHERE b>='y'
186 do_test intpkey-2.1.4 {
188 SELECT * FROM t1 WHERE b>='y' AND rowid<10
192 do_test intpkey-2.2 {
194 UPDATE t1 SET a=8 WHERE b=='y';
195 SELECT * FROM t1 WHERE b=='y';
198 do_test intpkey-2.3 {
200 SELECT rowid, * FROM t1;
202 } {4 4 one two 5 5 hello world 6 6 second entry 8 8 y z}
203 do_test intpkey-2.4 {
205 SELECT rowid, * FROM t1 WHERE b<'second'
207 } {5 5 hello world 4 4 one two}
208 do_test intpkey-2.4.1 {
210 SELECT rowid, * FROM t1 WHERE 'second'>b
212 } {5 5 hello world 4 4 one two}
213 do_test intpkey-2.4.2 {
215 SELECT rowid, * FROM t1 WHERE 8>rowid AND 'second'>b
217 } {4 4 one two 5 5 hello world}
218 do_test intpkey-2.4.3 {
220 SELECT rowid, * FROM t1 WHERE 8>rowid AND 'second'>b AND 0<rowid
222 } {4 4 one two 5 5 hello world}
223 do_test intpkey-2.5 {
225 SELECT rowid, * FROM t1 WHERE b>'a'
227 } {5 5 hello world 4 4 one two 6 6 second entry 8 8 y z}
228 do_test intpkey-2.6 {
230 DELETE FROM t1 WHERE rowid=4;
231 SELECT * FROM t1 WHERE b>'a';
233 } {5 hello world 6 second entry 8 y z}
234 do_test intpkey-2.7 {
236 UPDATE t1 SET a=-4 WHERE rowid=8;
237 SELECT * FROM t1 WHERE b>'a';
239 } {5 hello world 6 second entry -4 y z}
240 do_test intpkey-2.7 {
244 } {-4 y z 5 hello world 6 second entry}
246 # Do an SQL statement. Append the search count to the end of the result.
249 set ::sqlite_search_count 0
250 return [concat [execsql $sql] $::sqlite_search_count]
253 # Create indices that include the integer primary key as one of their
256 do_test intpkey-3.1 {
258 CREATE INDEX i2 ON t1(a);
261 do_test intpkey-3.2 {
263 SELECT * FROM t1 WHERE a=5;
266 do_test intpkey-3.3 {
268 SELECT * FROM t1 WHERE a>4 AND a<6;
271 do_test intpkey-3.4 {
273 SELECT * FROM t1 WHERE b>='hello' AND b<'hello2';
276 do_test intpkey-3.5 {
278 CREATE INDEX i3 ON t1(c,a);
281 do_test intpkey-3.6 {
283 SELECT * FROM t1 WHERE c=='world';
286 do_test intpkey-3.7 {
287 execsql {INSERT INTO t1 VALUES(11,'hello','world')}
289 SELECT * FROM t1 WHERE c=='world';
291 } {5 hello world 11 hello world 5}
292 do_test intpkey-3.8 {
294 SELECT * FROM t1 WHERE c=='world' AND a>7;
297 do_test intpkey-3.9 {
299 SELECT * FROM t1 WHERE 7<a;
303 # Test inequality constraints on integer primary keys and rowids
305 do_test intpkey-4.1 {
307 SELECT * FROM t1 WHERE 11=rowid
310 do_test intpkey-4.2 {
312 SELECT * FROM t1 WHERE 11=rowid AND b=='hello'
315 do_test intpkey-4.3 {
317 SELECT * FROM t1 WHERE 11=rowid AND b=='hello' AND c IS NOT NULL;
320 do_test intpkey-4.4 {
322 SELECT * FROM t1 WHERE rowid==11
325 do_test intpkey-4.5 {
327 SELECT * FROM t1 WHERE oid==11 AND b=='hello'
330 do_test intpkey-4.6 {
332 SELECT * FROM t1 WHERE a==11 AND b=='hello' AND c IS NOT NULL;
336 do_test intpkey-4.7 {
338 SELECT * FROM t1 WHERE 8<rowid;
341 do_test intpkey-4.8 {
343 SELECT * FROM t1 WHERE 8<rowid AND 11>=oid;
346 do_test intpkey-4.9 {
348 SELECT * FROM t1 WHERE 11<=_rowid_ AND 12>=a;
351 do_test intpkey-4.10 {
353 SELECT * FROM t1 WHERE 0>=_rowid_;
356 do_test intpkey-4.11 {
358 SELECT * FROM t1 WHERE a<0;
361 do_test intpkey-4.12 {
363 SELECT * FROM t1 WHERE a<0 AND a>10;
367 # Make sure it is OK to insert a rowid of 0
369 do_test intpkey-5.1 {
371 INSERT INTO t1 VALUES(0,'zero','entry');
374 SELECT * FROM t1 WHERE a=0;
377 do_test intpkey=5.2 {
379 SELECT rowid, a FROM t1
381 } {-4 -4 0 0 5 5 6 6 11 11}
383 # Test the ability of the COPY command to put data into a
384 # table that contains an integer primary key.
386 do_test intpkey-6.1 {
387 set f [open ./data1.txt w]
388 puts $f "20\tb-20\tc-20"
389 puts $f "21\tb-21\tc-21"
390 puts $f "22\tb-22\tc-22"
393 COPY t1 FROM 'data1.txt';
394 SELECT * FROM t1 WHERE a>=20;
396 } {20 b-20 c-20 21 b-21 c-21 22 b-22 c-22}
397 do_test intpkey-6.2 {
399 SELECT * FROM t1 WHERE b=='hello'
401 } {5 hello world 11 hello world}
402 do_test intpkey-6.3 {
404 DELETE FROM t1 WHERE b='b-21';
405 SELECT * FROM t1 WHERE b=='b-21';
408 do_test intpkey-6.4 {
410 SELECT * FROM t1 WHERE a>=20
412 } {20 b-20 c-20 22 b-22 c-22}
414 # Do an insert of values with the columns specified out of order.
416 do_test intpkey-7.1 {
418 INSERT INTO t1(c,b,a) VALUES('row','new',30);
419 SELECT * FROM t1 WHERE rowid>=30;
422 do_test intpkey-7.2 {
424 SELECT * FROM t1 WHERE rowid>20;
426 } {22 b-22 c-22 30 new row}
428 # Do an insert from a select statement.
430 do_test intpkey-8.1 {
432 CREATE TABLE t2(x INTEGER PRIMARY KEY, y, z);
433 INSERT INTO t2 SELECT * FROM t1;
434 SELECT rowid FROM t2;
436 } {-4 0 5 6 11 20 22 30}
437 do_test intpkey-8.2 {
441 } {-4 0 5 6 11 20 22 30}
443 do_test intpkey-9.1 {
445 UPDATE t1 SET c='www' WHERE c='world';
446 SELECT rowid, a, c FROM t1 WHERE c=='www';
448 } {5 5 www 11 11 www}
451 # Check insert of NULL for primary key
453 do_test intpkey-10.1 {
456 CREATE TABLE t2(x INTEGER PRIMARY KEY, y, z);
457 INSERT INTO t2 VALUES(NULL, 1, 2);
461 do_test intpkey-10.2 {
463 INSERT INTO t2 VALUES(NULL, 2, 3);
464 SELECT * from t2 WHERE x=2;
467 do_test intpkey-10.3 {
469 INSERT INTO t2 SELECT NULL, z, y FROM t2;
472 } {1 1 2 2 2 3 3 2 1 4 3 2}
474 # This tests checks to see if a floating point number can be used
475 # to reference an integer primary key.
477 do_test intpkey-11.1 {
479 SELECT b FROM t1 WHERE a=2.0+3.0;
482 do_test intpkey-11.1 {
484 SELECT b FROM t1 WHERE a=2.0+3.5;
488 integrity_check intpkey-12.1