4 # The author disclaims copyright to this source code. In place of
5 # a legal notice, here is a blessing:
7 # May you do good and not evil.
8 # May you find forgiveness for yourself and forgive others.
9 # May you share freely, never taking more than you give.
11 #***********************************************************************
12 # This file implements regression tests for SQLite library. The
13 # focus of this file is testing the CREATE INDEX statement.
15 # $Id: index.test,v 1.24.2.1 2004/07/20 00:50:30 drh Exp $
17 set testdir [file dirname $argv0]
18 source $testdir/tester.tcl
20 # Create a basic index and verify it is added to sqlite_master
23 execsql {CREATE TABLE test1(f1 int, f2 int, f3 int)}
24 execsql {CREATE INDEX index1 ON test1(f1)}
25 execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
28 execsql {SELECT name, sql, tbl_name, type FROM sqlite_master
30 } {index1 {CREATE INDEX index1 ON test1(f1)} test1 index}
34 execsql {SELECT name, sql, tbl_name, type FROM sqlite_master
36 } {index1 {CREATE INDEX index1 ON test1(f1)} test1 index}
40 execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
43 # Verify that the index dies with the table
46 execsql {DROP TABLE test1}
47 execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
50 # Try adding an index to a table that does not exist
53 set v [catch {execsql {CREATE INDEX index1 ON test1(f1)}} msg]
55 } {1 {no such table: test1}}
57 # Try adding an index on a column of a table where the table
58 # exists but the column does not.
61 execsql {CREATE TABLE test1(f1 int, f2 int, f3 int)}
62 set v [catch {execsql {CREATE INDEX index1 ON test1(f4)}} msg]
64 } {1 {table test1 has no column named f4}}
66 # Try an index with some columns that match and others that do now.
69 set v [catch {execsql {CREATE INDEX index1 ON test1(f1, f2, f4, f3)}} msg]
70 execsql {DROP TABLE test1}
72 } {1 {table test1 has no column named f4}}
74 # Try creating a bunch of indices on the same table
77 for {set i 1} {$i<100} {incr i} {
78 lappend r [format index%02d $i]
81 execsql {CREATE TABLE test1(f1 int, f2 int, f3 int, f4 int, f5 int)}
82 for {set i 1} {$i<100} {incr i} {
83 set sql "CREATE INDEX [format index%02d $i] ON test1(f[expr {($i%5)+1}])"
86 execsql {SELECT name FROM sqlite_master
87 WHERE type='index' AND tbl_name='test1'
92 # Verify that all the indices go away when we drop the table.
95 execsql {DROP TABLE test1}
96 execsql {SELECT name FROM sqlite_master
97 WHERE type='index' AND tbl_name='test1'
101 # Create a table and insert values into that table. Then create
102 # an index on that table. Verify that we can select values
103 # from the table correctly using the index.
105 # Note that the index names "index9" and "indext" are chosen because
106 # they both have the same hash.
109 execsql {CREATE TABLE test1(cnt int, power int)}
110 for {set i 1} {$i<20} {incr i} {
111 execsql "INSERT INTO test1 VALUES($i,[expr {int(pow(2,$i))}])"
113 execsql {CREATE INDEX index9 ON test1(cnt)}
114 execsql {CREATE INDEX indext ON test1(power)}
115 execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
116 } {index9 indext test1}
118 execsql {SELECT cnt FROM test1 WHERE power=4}
121 execsql {SELECT cnt FROM test1 WHERE power=1024}
124 execsql {SELECT power FROM test1 WHERE cnt=6}
127 execsql {DROP INDEX indext}
128 execsql {SELECT power FROM test1 WHERE cnt=6}
131 execsql {SELECT cnt FROM test1 WHERE power=1024}
134 execsql {CREATE INDEX indext ON test1(cnt)}
135 execsql {SELECT power FROM test1 WHERE cnt=6}
138 execsql {SELECT cnt FROM test1 WHERE power=1024}
141 execsql {DROP INDEX index9}
142 execsql {SELECT power FROM test1 WHERE cnt=6}
145 execsql {SELECT cnt FROM test1 WHERE power=1024}
148 execsql {DROP INDEX indext}
149 execsql {SELECT power FROM test1 WHERE cnt=6}
152 execsql {SELECT cnt FROM test1 WHERE power=1024}
155 execsql {DROP TABLE test1}
156 execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
158 integrity_check index-4.14
160 # Do not allow indices to be added to sqlite_master
163 set v [catch {execsql {CREATE INDEX index1 ON sqlite_master(name)}} msg]
165 } {1 {table sqlite_master may not be indexed}}
167 execsql {SELECT name FROM sqlite_master WHERE type!='meta'}
170 # Do not allow indices with duplicate names to be added
173 execsql {CREATE TABLE test1(f1 int, f2 int)}
174 execsql {CREATE TABLE test2(g1 real, g2 real)}
175 execsql {CREATE INDEX index1 ON test1(f1)}
176 set v [catch {execsql {CREATE INDEX index1 ON test2(g1)}} msg]
178 } {1 {index index1 already exists}}
180 execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
181 } {index1 test1 test2}
183 set v [catch {execsql {CREATE INDEX test1 ON test2(g1)}} msg]
185 } {1 {there is already a table named test1}}
187 execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
188 } {index1 test1 test2}
190 execsql {DROP TABLE test1}
191 execsql {DROP TABLE test2}
192 execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
196 CREATE TABLE test1(a,b);
197 CREATE INDEX index1 ON test1(a);
198 CREATE INDEX index2 ON test1(b);
199 CREATE INDEX index3 ON test1(a,b);
201 SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name;
204 integrity_check index-6.5
207 # Create a primary key
210 execsql {CREATE TABLE test1(f1 int, f2 int primary key)}
211 for {set i 1} {$i<20} {incr i} {
212 execsql "INSERT INTO test1 VALUES($i,[expr {int(pow(2,$i))}])"
214 execsql {SELECT count(*) FROM test1}
217 execsql {SELECT f1 FROM test1 WHERE f2=65536}
221 SELECT name FROM sqlite_master
222 WHERE type='index' AND tbl_name='test1'
224 } {{(test1 autoindex 1)}}
226 execsql {DROP table test1}
227 execsql {SELECT name FROM sqlite_master WHERE type!='meta'}
229 integrity_check index-7.5
231 # Make sure we cannot drop a non-existant index.
234 set v [catch {execsql {DROP INDEX index1}} msg]
236 } {1 {no such index: index1}}
238 # Make sure we don't actually create an index when the EXPLAIN keyword
242 execsql {CREATE TABLE tab1(a int)}
243 execsql {EXPLAIN CREATE INDEX idx1 ON tab1(a)}
244 execsql {SELECT name FROM sqlite_master WHERE tbl_name='tab1'}
247 execsql {CREATE INDEX idx1 ON tab1(a)}
248 execsql {SELECT name FROM sqlite_master WHERE tbl_name='tab1' ORDER BY name}
250 integrity_check index-9.3
252 # Allow more than one entry with the same key.
256 CREATE TABLE t1(a int, b int);
257 CREATE INDEX i1 ON t1(a);
258 INSERT INTO t1 VALUES(1,2);
259 INSERT INTO t1 VALUES(2,4);
260 INSERT INTO t1 VALUES(3,8);
261 INSERT INTO t1 VALUES(1,12);
262 SELECT b FROM t1 WHERE a=1 ORDER BY b;
267 SELECT b FROM t1 WHERE a=2 ORDER BY b;
272 DELETE FROM t1 WHERE b=12;
273 SELECT b FROM t1 WHERE a=1 ORDER BY b;
278 DELETE FROM t1 WHERE b=2;
279 SELECT b FROM t1 WHERE a=1 ORDER BY b;
285 INSERT INTO t1 VALUES (1,1);
286 INSERT INTO t1 VALUES (1,2);
287 INSERT INTO t1 VALUES (1,3);
288 INSERT INTO t1 VALUES (1,4);
289 INSERT INTO t1 VALUES (1,5);
290 INSERT INTO t1 VALUES (1,6);
291 INSERT INTO t1 VALUES (1,7);
292 INSERT INTO t1 VALUES (1,8);
293 INSERT INTO t1 VALUES (1,9);
294 INSERT INTO t1 VALUES (2,0);
295 SELECT b FROM t1 WHERE a=1 ORDER BY b;
297 } {1 2 3 4 5 6 7 8 9}
300 DELETE FROM t1 WHERE b IN (2, 4, 6, 8);
301 SELECT b FROM t1 WHERE a=1 ORDER BY b;
306 DELETE FROM t1 WHERE b>2;
307 SELECT b FROM t1 WHERE a=1 ORDER BY b;
312 DELETE FROM t1 WHERE b=1;
313 SELECT b FROM t1 WHERE a=1 ORDER BY b;
318 SELECT b FROM t1 ORDER BY b;
321 integrity_check index-10.9
323 # Automatically create an index when we specify a primary key.
334 for {set i 1} {$i<=50} {incr i} {
335 execsql "INSERT INTO t3 VALUES('x${i}x',$i,0.$i)"
337 set sqlite_search_count 0
338 concat [execsql {SELECT c FROM t3 WHERE b==10}] $sqlite_search_count
340 integrity_check index-11.2
343 # Numeric strings should compare as if they were numbers. So even if the
344 # strings are not character-by-character the same, if they represent the
345 # same number they should compare equal to one another. Verify that this
346 # is true in indices.
350 CREATE TABLE t4(a,b);
351 INSERT INTO t4 VALUES('0.0',1);
352 INSERT INTO t4 VALUES('0.00',2);
353 INSERT INTO t4 VALUES('abc',3);
354 INSERT INTO t4 VALUES('-1.0',4);
355 INSERT INTO t4 VALUES('+1.0',5);
356 INSERT INTO t4 VALUES('0',6);
357 INSERT INTO t4 VALUES('00000',7);
358 SELECT a FROM t4 ORDER BY b;
360 } {0.0 0.00 abc -1.0 +1.0 0 00000}
363 SELECT a FROM t4 WHERE a==0 ORDER BY b
368 SELECT a FROM t4 WHERE a<0.5 ORDER BY b
370 } {0.0 0.00 -1.0 0 00000}
373 SELECT a FROM t4 WHERE a>-0.5 ORDER BY b
375 } {0.0 0.00 abc +1.0 0 00000}
378 CREATE INDEX t4i1 ON t4(a);
379 SELECT a FROM t4 WHERE a==0 ORDER BY b
384 SELECT a FROM t4 WHERE a<0.5 ORDER BY b
386 } {0.0 0.00 -1.0 0 00000}
389 SELECT a FROM t4 WHERE a>-0.5 ORDER BY b
391 } {0.0 0.00 abc +1.0 0 00000}
392 integrity_check index-12.8
394 # Make sure we cannot drop an automatically created index.
404 INSERT INTO t5 VALUES(1,2,3);
409 set ::idxlist [execsql {
410 SELECT name FROM sqlite_master WHERE type="index" AND tbl_name="t5";
414 for {set i 0} {$i<[llength $::idxlist]} {incr i} {
415 do_test index-13.3.$i {
417 DROP INDEX '[lindex $::idxlist $i]';
419 } {1 {index associated with UNIQUE or PRIMARY KEY constraint cannot be dropped}}
423 INSERT INTO t5 VALUES('a','b','c');
427 integrity_check index-13.5
429 # Check the sort order of data in an index.
433 CREATE TABLE t6(a,b,c);
434 CREATE INDEX t6i1 ON t6(a,b);
435 INSERT INTO t6 VALUES('','',1);
436 INSERT INTO t6 VALUES('',NULL,2);
437 INSERT INTO t6 VALUES(NULL,'',3);
438 INSERT INTO t6 VALUES('abc',123,4);
439 INSERT INTO t6 VALUES(123,'abc',5);
440 SELECT c FROM t6 ORDER BY a,b;
445 SELECT c FROM t6 WHERE a='';
450 SELECT c FROM t6 WHERE b='';
455 SELECT c FROM t6 WHERE a>'';
460 SELECT c FROM t6 WHERE a>='';
465 SELECT c FROM t6 WHERE a>123;
470 SELECT c FROM t6 WHERE a>=123;
475 SELECT c FROM t6 WHERE a<'abc';
480 SELECT c FROM t6 WHERE a<='abc';
483 do_test index-14.10 {
485 SELECT c FROM t6 WHERE a<='';
488 do_test index-14.11 {
490 SELECT c FROM t6 WHERE a<'';
493 integrity_check index-14.12
503 INSERT INTO t1 VALUES('1.234e5',1);
504 INSERT INTO t1 VALUES('12.33e04',2);
505 INSERT INTO t1 VALUES('12.35E4',3);
506 INSERT INTO t1 VALUES('12.34e',4);
507 INSERT INTO t1 VALUES('12.32e+4',5);
508 INSERT INTO t1 VALUES('12.36E+04',6);
509 INSERT INTO t1 VALUES('12.36E+',7);
510 INSERT INTO t1 VALUES('+123.10000E+0003',8);
511 INSERT INTO t1 VALUES('+',9);
512 INSERT INTO t1 VALUES('+12347.E+02',10);
513 INSERT INTO t1 VALUES('+12347E+02',11);
514 SELECT b FROM t1 ORDER BY a;
516 } {8 5 2 1 3 6 11 9 10 4 7}
517 integrity_check index-15.1
519 # Drop index with a quoted name. Ticket #695.
523 CREATE INDEX "t6i2" ON t6(c);