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 file is testing the CREATE INDEX statement.
14 # $Id: index.test,v 1.43 2008/01/16 18:20:42 danielk1977 Exp $
16 set testdir [file dirname $argv0]
17 source $testdir/tester.tcl
19 # Create a basic index and verify it is added to sqlite_master
22 execsql {CREATE TABLE test1(f1 int, f2 int, f3 int)}
23 execsql {CREATE INDEX index1 ON test1(f1)}
24 execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
27 execsql {SELECT name, sql, tbl_name, type FROM sqlite_master
29 } {index1 {CREATE INDEX index1 ON test1(f1)} test1 index}
33 execsql {SELECT name, sql, tbl_name, type FROM sqlite_master
35 } {index1 {CREATE INDEX index1 ON test1(f1)} test1 index}
39 execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
42 # Verify that the index dies with the table
45 execsql {DROP TABLE test1}
46 execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
49 # Try adding an index to a table that does not exist
52 set v [catch {execsql {CREATE INDEX index1 ON test1(f1)}} msg]
54 } {1 {no such table: main.test1}}
56 # Try adding an index on a column of a table where the table
57 # exists but the column does not.
60 execsql {CREATE TABLE test1(f1 int, f2 int, f3 int)}
61 set v [catch {execsql {CREATE INDEX index1 ON test1(f4)}} msg]
63 } {1 {table test1 has no column named f4}}
65 # Try an index with some columns that match and others that do now.
68 set v [catch {execsql {CREATE INDEX index1 ON test1(f1, f2, f4, f3)}} msg]
69 execsql {DROP TABLE test1}
71 } {1 {table test1 has no column named f4}}
73 # Try creating a bunch of indices on the same table
76 for {set i 1} {$i<100} {incr i} {
77 lappend r [format index%02d $i]
80 execsql {CREATE TABLE test1(f1 int, f2 int, f3 int, f4 int, f5 int)}
81 for {set i 1} {$i<100} {incr i} {
82 set sql "CREATE INDEX [format index%02d $i] ON test1(f[expr {($i%5)+1}])"
85 execsql {SELECT name FROM sqlite_master
86 WHERE type='index' AND tbl_name='test1'
89 integrity_check index-3.2.1
95 integrity_check index-3.2.3
98 # Verify that all the indices go away when we drop the table.
101 execsql {DROP TABLE test1}
102 execsql {SELECT name FROM sqlite_master
103 WHERE type='index' AND tbl_name='test1'
107 # Create a table and insert values into that table. Then create
108 # an index on that table. Verify that we can select values
109 # from the table correctly using the index.
111 # Note that the index names "index9" and "indext" are chosen because
112 # they both have the same hash.
115 execsql {CREATE TABLE test1(cnt int, power int)}
116 for {set i 1} {$i<20} {incr i} {
117 execsql "INSERT INTO test1 VALUES($i,[expr {1<<$i}])"
119 execsql {CREATE INDEX index9 ON test1(cnt)}
120 execsql {CREATE INDEX indext ON test1(power)}
121 execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
122 } {index9 indext test1}
124 execsql {SELECT cnt FROM test1 WHERE power=4}
127 execsql {SELECT cnt FROM test1 WHERE power=1024}
130 execsql {SELECT power FROM test1 WHERE cnt=6}
133 execsql {DROP INDEX indext}
134 execsql {SELECT power FROM test1 WHERE cnt=6}
137 execsql {SELECT cnt FROM test1 WHERE power=1024}
140 execsql {CREATE INDEX indext ON test1(cnt)}
141 execsql {SELECT power FROM test1 WHERE cnt=6}
144 execsql {SELECT cnt FROM test1 WHERE power=1024}
147 execsql {DROP INDEX index9}
148 execsql {SELECT power FROM test1 WHERE cnt=6}
151 execsql {SELECT cnt FROM test1 WHERE power=1024}
154 execsql {DROP INDEX indext}
155 execsql {SELECT power FROM test1 WHERE cnt=6}
158 execsql {SELECT cnt FROM test1 WHERE power=1024}
161 execsql {DROP TABLE test1}
162 execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
164 integrity_check index-4.14
166 # Do not allow indices to be added to sqlite_master
169 set v [catch {execsql {CREATE INDEX index1 ON sqlite_master(name)}} msg]
171 } {1 {table sqlite_master may not be indexed}}
173 execsql {SELECT name FROM sqlite_master WHERE type!='meta'}
176 # Do not allow indices with duplicate names to be added
179 execsql {CREATE TABLE test1(f1 int, f2 int)}
180 execsql {CREATE TABLE test2(g1 real, g2 real)}
181 execsql {CREATE INDEX index1 ON test1(f1)}
182 set v [catch {execsql {CREATE INDEX index1 ON test2(g1)}} msg]
184 } {1 {index index1 already exists}}
185 do_test index-6.1.1 {
186 catchsql {CREATE INDEX [index1] ON test2(g1)}
187 } {1 {index index1 already exists}}
189 execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
190 } {index1 test1 test2}
192 catchsql {CREATE INDEX IF NOT EXISTS index1 ON test1(f1)}
195 set v [catch {execsql {CREATE INDEX test1 ON test2(g1)}} msg]
197 } {1 {there is already a table named test1}}
199 execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
200 } {index1 test1 test2}
202 execsql {DROP TABLE test1}
203 execsql {DROP TABLE test2}
204 execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
208 CREATE TABLE test1(a,b);
209 CREATE INDEX index1 ON test1(a);
210 CREATE INDEX index2 ON test1(b);
211 CREATE INDEX index3 ON test1(a,b);
213 SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name;
216 integrity_check index-6.5
219 # Create a primary key
222 execsql {CREATE TABLE test1(f1 int, f2 int primary key)}
223 for {set i 1} {$i<20} {incr i} {
224 execsql "INSERT INTO test1 VALUES($i,[expr {1<<$i}])"
226 execsql {SELECT count(*) FROM test1}
229 execsql {SELECT f1 FROM test1 WHERE f2=65536}
233 SELECT name FROM sqlite_master
234 WHERE type='index' AND tbl_name='test1'
236 } {sqlite_autoindex_test1_1}
238 execsql {DROP table test1}
239 execsql {SELECT name FROM sqlite_master WHERE type!='meta'}
241 integrity_check index-7.5
243 # Make sure we cannot drop a non-existant index.
246 set v [catch {execsql {DROP INDEX index1}} msg]
248 } {1 {no such index: index1}}
250 # Make sure we don't actually create an index when the EXPLAIN keyword
254 execsql {CREATE TABLE tab1(a int)}
255 ifcapable {explain} {
256 execsql {EXPLAIN CREATE INDEX idx1 ON tab1(a)}
258 execsql {SELECT name FROM sqlite_master WHERE tbl_name='tab1'}
261 execsql {CREATE INDEX idx1 ON tab1(a)}
262 execsql {SELECT name FROM sqlite_master WHERE tbl_name='tab1' ORDER BY name}
264 integrity_check index-9.3
266 # Allow more than one entry with the same key.
270 CREATE TABLE t1(a int, b int);
271 CREATE INDEX i1 ON t1(a);
272 INSERT INTO t1 VALUES(1,2);
273 INSERT INTO t1 VALUES(2,4);
274 INSERT INTO t1 VALUES(3,8);
275 INSERT INTO t1 VALUES(1,12);
276 SELECT b FROM t1 WHERE a=1 ORDER BY b;
281 SELECT b FROM t1 WHERE a=2 ORDER BY b;
286 DELETE FROM t1 WHERE b=12;
287 SELECT b FROM t1 WHERE a=1 ORDER BY b;
292 DELETE FROM t1 WHERE b=2;
293 SELECT b FROM t1 WHERE a=1 ORDER BY b;
299 INSERT INTO t1 VALUES (1,1);
300 INSERT INTO t1 VALUES (1,2);
301 INSERT INTO t1 VALUES (1,3);
302 INSERT INTO t1 VALUES (1,4);
303 INSERT INTO t1 VALUES (1,5);
304 INSERT INTO t1 VALUES (1,6);
305 INSERT INTO t1 VALUES (1,7);
306 INSERT INTO t1 VALUES (1,8);
307 INSERT INTO t1 VALUES (1,9);
308 INSERT INTO t1 VALUES (2,0);
309 SELECT b FROM t1 WHERE a=1 ORDER BY b;
311 } {1 2 3 4 5 6 7 8 9}
314 execsql { DELETE FROM t1 WHERE b IN (2, 4, 6, 8); }
316 execsql { DELETE FROM t1 WHERE b = 2 OR b = 4 OR b = 6 OR b = 8; }
319 SELECT b FROM t1 WHERE a=1 ORDER BY b;
324 DELETE FROM t1 WHERE b>2;
325 SELECT b FROM t1 WHERE a=1 ORDER BY b;
330 DELETE FROM t1 WHERE b=1;
331 SELECT b FROM t1 WHERE a=1 ORDER BY b;
336 SELECT b FROM t1 ORDER BY b;
339 integrity_check index-10.9
341 # Automatically create an index when we specify a primary key.
352 for {set i 1} {$i<=50} {incr i} {
353 execsql "INSERT INTO t3 VALUES('x${i}x',$i,0.$i)"
355 set sqlite_search_count 0
356 concat [execsql {SELECT c FROM t3 WHERE b==10}] $sqlite_search_count
358 integrity_check index-11.2
361 # Numeric strings should compare as if they were numbers. So even if the
362 # strings are not character-by-character the same, if they represent the
363 # same number they should compare equal to one another. Verify that this
364 # is true in indices.
366 # Updated for sqlite3 v3: SQLite will now store these values as numbers
367 # (because the affinity of column a is NUMERIC) so the quirky
368 # representations are not retained. i.e. '+1.0' becomes '1'.
371 CREATE TABLE t4(a NUM,b);
372 INSERT INTO t4 VALUES('0.0',1);
373 INSERT INTO t4 VALUES('0.00',2);
374 INSERT INTO t4 VALUES('abc',3);
375 INSERT INTO t4 VALUES('-1.0',4);
376 INSERT INTO t4 VALUES('+1.0',5);
377 INSERT INTO t4 VALUES('0',6);
378 INSERT INTO t4 VALUES('00000',7);
379 SELECT a FROM t4 ORDER BY b;
384 SELECT a FROM t4 WHERE a==0 ORDER BY b
389 SELECT a FROM t4 WHERE a<0.5 ORDER BY b
394 SELECT a FROM t4 WHERE a>-0.5 ORDER BY b
399 CREATE INDEX t4i1 ON t4(a);
400 SELECT a FROM t4 WHERE a==0 ORDER BY b
405 SELECT a FROM t4 WHERE a<0.5 ORDER BY b
410 SELECT a FROM t4 WHERE a>-0.5 ORDER BY b
413 integrity_check index-12.8
415 # Make sure we cannot drop an automatically created index.
425 INSERT INTO t5 VALUES(1,2,3);
430 set ::idxlist [execsql {
431 SELECT name FROM sqlite_master WHERE type="index" AND tbl_name="t5";
435 for {set i 0} {$i<[llength $::idxlist]} {incr i} {
436 do_test index-13.3.$i {
438 DROP INDEX '[lindex $::idxlist $i]';
440 } {1 {index associated with UNIQUE or PRIMARY KEY constraint cannot be dropped}}
444 INSERT INTO t5 VALUES('a','b','c');
448 integrity_check index-13.5
450 # Check the sort order of data in an index.
454 CREATE TABLE t6(a,b,c);
455 CREATE INDEX t6i1 ON t6(a,b);
456 INSERT INTO t6 VALUES('','',1);
457 INSERT INTO t6 VALUES('',NULL,2);
458 INSERT INTO t6 VALUES(NULL,'',3);
459 INSERT INTO t6 VALUES('abc',123,4);
460 INSERT INTO t6 VALUES(123,'abc',5);
461 SELECT c FROM t6 ORDER BY a,b;
466 SELECT c FROM t6 WHERE a='';
471 SELECT c FROM t6 WHERE b='';
476 SELECT c FROM t6 WHERE a>'';
481 SELECT c FROM t6 WHERE a>='';
486 SELECT c FROM t6 WHERE a>123;
491 SELECT c FROM t6 WHERE a>=123;
496 SELECT c FROM t6 WHERE a<'abc';
501 SELECT c FROM t6 WHERE a<='abc';
504 do_test index-14.10 {
506 SELECT c FROM t6 WHERE a<='';
509 do_test index-14.11 {
511 SELECT c FROM t6 WHERE a<'';
514 integrity_check index-14.12
524 INSERT INTO t1 VALUES('1.234e5',1);
525 INSERT INTO t1 VALUES('12.33e04',2);
526 INSERT INTO t1 VALUES('12.35E4',3);
527 INSERT INTO t1 VALUES('12.34e',4);
528 INSERT INTO t1 VALUES('12.32e+4',5);
529 INSERT INTO t1 VALUES('12.36E+04',6);
530 INSERT INTO t1 VALUES('12.36E+',7);
531 INSERT INTO t1 VALUES('+123.10000E+0003',8);
532 INSERT INTO t1 VALUES('+',9);
533 INSERT INTO t1 VALUES('+12347.E+02',10);
534 INSERT INTO t1 VALUES('+12347E+02',11);
535 INSERT INTO t1 VALUES('+.125E+04',12);
536 INSERT INTO t1 VALUES('-.125E+04',13);
537 INSERT INTO t1 VALUES('.125E+0',14);
538 INSERT INTO t1 VALUES('.125',15);
539 SELECT b FROM t1 ORDER BY a, b;
541 } {13 14 15 12 8 5 2 1 3 6 10 11 9 4 7}
544 SELECT b FROM t1 WHERE typeof(a) IN ('integer','real') ORDER BY b;
546 } {1 2 3 5 6 8 10 11 12 13 14 15}
547 integrity_check index-15.4
549 # The following tests - index-16.* - test that when a table definition
550 # includes qualifications that specify the same constraint twice only a
551 # single index is generated to enforce the constraint.
553 # For example: "CREATE TABLE abc( x PRIMARY KEY, UNIQUE(x) );"
557 CREATE TABLE t7(c UNIQUE PRIMARY KEY);
558 SELECT count(*) FROM sqlite_master WHERE tbl_name = 't7' AND type = 'index';
564 CREATE TABLE t7(c UNIQUE PRIMARY KEY);
565 SELECT count(*) FROM sqlite_master WHERE tbl_name = 't7' AND type = 'index';
571 CREATE TABLE t7(c PRIMARY KEY, UNIQUE(c) );
572 SELECT count(*) FROM sqlite_master WHERE tbl_name = 't7' AND type = 'index';
578 CREATE TABLE t7(c, d , UNIQUE(c, d), PRIMARY KEY(c, d) );
579 SELECT count(*) FROM sqlite_master WHERE tbl_name = 't7' AND type = 'index';
585 CREATE TABLE t7(c, d , UNIQUE(c), PRIMARY KEY(c, d) );
586 SELECT count(*) FROM sqlite_master WHERE tbl_name = 't7' AND type = 'index';
590 # Test that automatically create indices are named correctly. The current
591 # convention is: "sqlite_autoindex_<table name>_<integer>"
593 # Then check that it is an error to try to drop any automtically created
598 CREATE TABLE t7(c, d UNIQUE, UNIQUE(c), PRIMARY KEY(c, d) );
599 SELECT name FROM sqlite_master WHERE tbl_name = 't7' AND type = 'index';
601 } {sqlite_autoindex_t7_1 sqlite_autoindex_t7_2 sqlite_autoindex_t7_3}
604 DROP INDEX sqlite_autoindex_t7_1;
606 } {1 {index associated with UNIQUE or PRIMARY KEY constraint cannot be dropped}}
609 DROP INDEX IF EXISTS sqlite_autoindex_t7_1;
611 } {1 {index associated with UNIQUE or PRIMARY KEY constraint cannot be dropped}}
614 DROP INDEX IF EXISTS no_such_index;
619 # The following tests ensure that it is not possible to explicitly name
620 # a schema object with a name beginning with "sqlite_". Granted that is a
621 # little outside the focus of this test scripts, but this has got to be
625 CREATE TABLE sqlite_t1(a, b, c);
627 } {1 {object name reserved for internal use: sqlite_t1}}
630 CREATE INDEX sqlite_i1 ON t7(c);
632 } {1 {object name reserved for internal use: sqlite_i1}}
636 CREATE VIEW sqlite_v1 AS SELECT * FROM t7;
638 } {1 {object name reserved for internal use: sqlite_v1}}
640 ifcapable {trigger} {
643 CREATE TRIGGER sqlite_tr1 BEFORE INSERT ON t7 BEGIN SELECT 1; END;
645 } {1 {object name reserved for internal use: sqlite_tr1}}
653 # These tests ensure that if multiple table definition constraints are
654 # implemented by a single indice, the correct ON CONFLICT policy applies.
658 CREATE TABLE t7(a UNIQUE PRIMARY KEY);
659 CREATE TABLE t8(a UNIQUE PRIMARY KEY ON CONFLICT ROLLBACK);
660 INSERT INTO t7 VALUES(1);
661 INSERT INTO t8 VALUES(1);
667 INSERT INTO t7 VALUES(1);
669 } {1 {UNIQUE constraint failed: t7.a}}
674 } {1 {cannot start a transaction within a transaction}}
677 INSERT INTO t8 VALUES(1);
679 } {1 {UNIQUE constraint failed: t8.a}}
691 a PRIMARY KEY ON CONFLICT FAIL,
692 UNIQUE(a) ON CONFLICT IGNORE
695 } {1 {conflicting ON CONFLICT clauses specified}}
696 } ; # end of "ifcapable conflict" block
698 ifcapable {reindex} {
703 integrity_check index-19.8
705 # Drop index with a quoted name. Ticket #695.
709 CREATE INDEX "t6i2" ON t6(c);
719 # Try to create a TEMP index on a non-TEMP table. */
723 CREATE INDEX temp.i21 ON t6(c);
725 } {1 {cannot create a TEMP index on non-TEMP table "t6"}}
728 CREATE TEMP TABLE t6(x);
729 INSERT INTO temp.t6 values(1),(5),(9);
730 CREATE INDEX temp.i21 ON t6(x);
731 SELECT x FROM t6 ORDER BY x DESC;