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. The
15 # focus of this file is testing the CREATE INDEX statement.
17 # $Id: index.test,v 1.24.2.1 2004/07/20 00:50:30 drh Exp $
19 set testdir [file dirname $argv0]
20 source $testdir/tester.tcl
22 # Create a basic index and verify it is added to sqlite_master
25 execsql {CREATE TABLE test1(f1 int, f2 int, f3 int)}
26 execsql {CREATE INDEX index1 ON test1(f1)}
27 execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
30 execsql {SELECT name, sql, tbl_name, type FROM sqlite_master
32 } {index1 {CREATE INDEX index1 ON test1(f1)} test1 index}
36 execsql {SELECT name, sql, tbl_name, type FROM sqlite_master
38 } {index1 {CREATE INDEX index1 ON test1(f1)} test1 index}
42 execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
45 # Verify that the index dies with the table
48 execsql {DROP TABLE test1}
49 execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
52 # Try adding an index to a table that does not exist
55 set v [catch {execsql {CREATE INDEX index1 ON test1(f1)}} msg]
57 } {1 {no such table: test1}}
59 # Try adding an index on a column of a table where the table
60 # exists but the column does not.
63 execsql {CREATE TABLE test1(f1 int, f2 int, f3 int)}
64 set v [catch {execsql {CREATE INDEX index1 ON test1(f4)}} msg]
66 } {1 {table test1 has no column named f4}}
68 # Try an index with some columns that match and others that do now.
71 set v [catch {execsql {CREATE INDEX index1 ON test1(f1, f2, f4, f3)}} msg]
72 execsql {DROP TABLE test1}
74 } {1 {table test1 has no column named f4}}
76 # Try creating a bunch of indices on the same table
79 for {set i 1} {$i<100} {incr i} {
80 lappend r [format index%02d $i]
83 execsql {CREATE TABLE test1(f1 int, f2 int, f3 int, f4 int, f5 int)}
84 for {set i 1} {$i<100} {incr i} {
85 set sql "CREATE INDEX [format index%02d $i] ON test1(f[expr {($i%5)+1}])"
88 execsql {SELECT name FROM sqlite_master
89 WHERE type='index' AND tbl_name='test1'
94 # Verify that all the indices go away when we drop the table.
97 execsql {DROP TABLE test1}
98 execsql {SELECT name FROM sqlite_master
99 WHERE type='index' AND tbl_name='test1'
103 # Create a table and insert values into that table. Then create
104 # an index on that table. Verify that we can select values
105 # from the table correctly using the index.
107 # Note that the index names "index9" and "indext" are chosen because
108 # they both have the same hash.
111 execsql {CREATE TABLE test1(cnt int, power int)}
112 for {set i 1} {$i<20} {incr i} {
113 execsql "INSERT INTO test1 VALUES($i,[expr {int(pow(2,$i))}])"
115 execsql {CREATE INDEX index9 ON test1(cnt)}
116 execsql {CREATE INDEX indext ON test1(power)}
117 execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
118 } {index9 indext test1}
120 execsql {SELECT cnt FROM test1 WHERE power=4}
123 execsql {SELECT cnt FROM test1 WHERE power=1024}
126 execsql {SELECT power FROM test1 WHERE cnt=6}
129 execsql {DROP INDEX indext}
130 execsql {SELECT power FROM test1 WHERE cnt=6}
133 execsql {SELECT cnt FROM test1 WHERE power=1024}
136 execsql {CREATE INDEX indext ON test1(cnt)}
137 execsql {SELECT power FROM test1 WHERE cnt=6}
140 execsql {SELECT cnt FROM test1 WHERE power=1024}
143 execsql {DROP INDEX index9}
144 execsql {SELECT power FROM test1 WHERE cnt=6}
147 execsql {SELECT cnt FROM test1 WHERE power=1024}
150 execsql {DROP INDEX indext}
151 execsql {SELECT power FROM test1 WHERE cnt=6}
154 execsql {SELECT cnt FROM test1 WHERE power=1024}
157 execsql {DROP TABLE test1}
158 execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
160 integrity_check index-4.14
162 # Do not allow indices to be added to sqlite_master
165 set v [catch {execsql {CREATE INDEX index1 ON sqlite_master(name)}} msg]
167 } {1 {table sqlite_master may not be indexed}}
169 execsql {SELECT name FROM sqlite_master WHERE type!='meta'}
172 # Do not allow indices with duplicate names to be added
175 execsql {CREATE TABLE test1(f1 int, f2 int)}
176 execsql {CREATE TABLE test2(g1 real, g2 real)}
177 execsql {CREATE INDEX index1 ON test1(f1)}
178 set v [catch {execsql {CREATE INDEX index1 ON test2(g1)}} msg]
180 } {1 {index index1 already exists}}
182 execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
183 } {index1 test1 test2}
185 set v [catch {execsql {CREATE INDEX test1 ON test2(g1)}} msg]
187 } {1 {there is already a table named test1}}
189 execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
190 } {index1 test1 test2}
192 execsql {DROP TABLE test1}
193 execsql {DROP TABLE test2}
194 execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
198 CREATE TABLE test1(a,b);
199 CREATE INDEX index1 ON test1(a);
200 CREATE INDEX index2 ON test1(b);
201 CREATE INDEX index3 ON test1(a,b);
203 SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name;
206 integrity_check index-6.5
209 # Create a primary key
212 execsql {CREATE TABLE test1(f1 int, f2 int primary key)}
213 for {set i 1} {$i<20} {incr i} {
214 execsql "INSERT INTO test1 VALUES($i,[expr {int(pow(2,$i))}])"
216 execsql {SELECT count(*) FROM test1}
219 execsql {SELECT f1 FROM test1 WHERE f2=65536}
223 SELECT name FROM sqlite_master
224 WHERE type='index' AND tbl_name='test1'
226 } {{(test1 autoindex 1)}}
228 execsql {DROP table test1}
229 execsql {SELECT name FROM sqlite_master WHERE type!='meta'}
231 integrity_check index-7.5
233 # Make sure we cannot drop a non-existant index.
236 set v [catch {execsql {DROP INDEX index1}} msg]
238 } {1 {no such index: index1}}
240 # Make sure we don't actually create an index when the EXPLAIN keyword
244 execsql {CREATE TABLE tab1(a int)}
245 execsql {EXPLAIN CREATE INDEX idx1 ON tab1(a)}
246 execsql {SELECT name FROM sqlite_master WHERE tbl_name='tab1'}
249 execsql {CREATE INDEX idx1 ON tab1(a)}
250 execsql {SELECT name FROM sqlite_master WHERE tbl_name='tab1' ORDER BY name}
252 integrity_check index-9.3
254 # Allow more than one entry with the same key.
258 CREATE TABLE t1(a int, b int);
259 CREATE INDEX i1 ON t1(a);
260 INSERT INTO t1 VALUES(1,2);
261 INSERT INTO t1 VALUES(2,4);
262 INSERT INTO t1 VALUES(3,8);
263 INSERT INTO t1 VALUES(1,12);
264 SELECT b FROM t1 WHERE a=1 ORDER BY b;
269 SELECT b FROM t1 WHERE a=2 ORDER BY b;
274 DELETE FROM t1 WHERE b=12;
275 SELECT b FROM t1 WHERE a=1 ORDER BY b;
280 DELETE FROM t1 WHERE b=2;
281 SELECT b FROM t1 WHERE a=1 ORDER BY b;
287 INSERT INTO t1 VALUES (1,1);
288 INSERT INTO t1 VALUES (1,2);
289 INSERT INTO t1 VALUES (1,3);
290 INSERT INTO t1 VALUES (1,4);
291 INSERT INTO t1 VALUES (1,5);
292 INSERT INTO t1 VALUES (1,6);
293 INSERT INTO t1 VALUES (1,7);
294 INSERT INTO t1 VALUES (1,8);
295 INSERT INTO t1 VALUES (1,9);
296 INSERT INTO t1 VALUES (2,0);
297 SELECT b FROM t1 WHERE a=1 ORDER BY b;
299 } {1 2 3 4 5 6 7 8 9}
302 DELETE FROM t1 WHERE b IN (2, 4, 6, 8);
303 SELECT b FROM t1 WHERE a=1 ORDER BY b;
308 DELETE FROM t1 WHERE b>2;
309 SELECT b FROM t1 WHERE a=1 ORDER BY b;
314 DELETE FROM t1 WHERE b=1;
315 SELECT b FROM t1 WHERE a=1 ORDER BY b;
320 SELECT b FROM t1 ORDER BY b;
323 integrity_check index-10.9
325 # Automatically create an index when we specify a primary key.
336 for {set i 1} {$i<=50} {incr i} {
337 execsql "INSERT INTO t3 VALUES('x${i}x',$i,0.$i)"
339 set sqlite_search_count 0
340 concat [execsql {SELECT c FROM t3 WHERE b==10}] $sqlite_search_count
342 integrity_check index-11.2
345 # Numeric strings should compare as if they were numbers. So even if the
346 # strings are not character-by-character the same, if they represent the
347 # same number they should compare equal to one another. Verify that this
348 # is true in indices.
352 CREATE TABLE t4(a,b);
353 INSERT INTO t4 VALUES('0.0',1);
354 INSERT INTO t4 VALUES('0.00',2);
355 INSERT INTO t4 VALUES('abc',3);
356 INSERT INTO t4 VALUES('-1.0',4);
357 INSERT INTO t4 VALUES('+1.0',5);
358 INSERT INTO t4 VALUES('0',6);
359 INSERT INTO t4 VALUES('00000',7);
360 SELECT a FROM t4 ORDER BY b;
362 } {0.0 0.00 abc -1.0 +1.0 0 00000}
365 SELECT a FROM t4 WHERE a==0 ORDER BY b
370 SELECT a FROM t4 WHERE a<0.5 ORDER BY b
372 } {0.0 0.00 -1.0 0 00000}
375 SELECT a FROM t4 WHERE a>-0.5 ORDER BY b
377 } {0.0 0.00 abc +1.0 0 00000}
380 CREATE INDEX t4i1 ON t4(a);
381 SELECT a FROM t4 WHERE a==0 ORDER BY b
386 SELECT a FROM t4 WHERE a<0.5 ORDER BY b
388 } {0.0 0.00 -1.0 0 00000}
391 SELECT a FROM t4 WHERE a>-0.5 ORDER BY b
393 } {0.0 0.00 abc +1.0 0 00000}
394 integrity_check index-12.8
396 # Make sure we cannot drop an automatically created index.
406 INSERT INTO t5 VALUES(1,2,3);
411 set ::idxlist [execsql {
412 SELECT name FROM sqlite_master WHERE type="index" AND tbl_name="t5";
416 for {set i 0} {$i<[llength $::idxlist]} {incr i} {
417 do_test index-13.3.$i {
419 DROP INDEX '[lindex $::idxlist $i]';
421 } {1 {index associated with UNIQUE or PRIMARY KEY constraint cannot be dropped}}
425 INSERT INTO t5 VALUES('a','b','c');
429 integrity_check index-13.5
431 # Check the sort order of data in an index.
435 CREATE TABLE t6(a,b,c);
436 CREATE INDEX t6i1 ON t6(a,b);
437 INSERT INTO t6 VALUES('','',1);
438 INSERT INTO t6 VALUES('',NULL,2);
439 INSERT INTO t6 VALUES(NULL,'',3);
440 INSERT INTO t6 VALUES('abc',123,4);
441 INSERT INTO t6 VALUES(123,'abc',5);
442 SELECT c FROM t6 ORDER BY a,b;
447 SELECT c FROM t6 WHERE a='';
452 SELECT c FROM t6 WHERE b='';
457 SELECT c FROM t6 WHERE a>'';
462 SELECT c FROM t6 WHERE a>='';
467 SELECT c FROM t6 WHERE a>123;
472 SELECT c FROM t6 WHERE a>=123;
477 SELECT c FROM t6 WHERE a<'abc';
482 SELECT c FROM t6 WHERE a<='abc';
485 do_test index-14.10 {
487 SELECT c FROM t6 WHERE a<='';
490 do_test index-14.11 {
492 SELECT c FROM t6 WHERE a<'';
495 integrity_check index-14.12
505 INSERT INTO t1 VALUES('1.234e5',1);
506 INSERT INTO t1 VALUES('12.33e04',2);
507 INSERT INTO t1 VALUES('12.35E4',3);
508 INSERT INTO t1 VALUES('12.34e',4);
509 INSERT INTO t1 VALUES('12.32e+4',5);
510 INSERT INTO t1 VALUES('12.36E+04',6);
511 INSERT INTO t1 VALUES('12.36E+',7);
512 INSERT INTO t1 VALUES('+123.10000E+0003',8);
513 INSERT INTO t1 VALUES('+',9);
514 INSERT INTO t1 VALUES('+12347.E+02',10);
515 INSERT INTO t1 VALUES('+12347E+02',11);
516 SELECT b FROM t1 ORDER BY a;
518 } {8 5 2 1 3 6 11 9 10 4 7}
519 integrity_check index-15.1
521 # Drop index with a quoted name. Ticket #695.
525 CREATE INDEX "t6i2" ON t6(c);