sl@0: # 2001 September 15 sl@0: # sl@0: # The author disclaims copyright to this source code. In place of sl@0: # a legal notice, here is a blessing: sl@0: # sl@0: # May you do good and not evil. sl@0: # May you find forgiveness for yourself and forgive others. sl@0: # May you share freely, never taking more than you give. sl@0: # sl@0: #*********************************************************************** sl@0: # This file implements regression tests for SQLite library. The sl@0: # focus of this file is testing the CREATE INDEX statement. sl@0: # sl@0: # $Id: index.test,v 1.43 2008/01/16 18:20:42 danielk1977 Exp $ sl@0: sl@0: set testdir [file dirname $argv0] sl@0: source $testdir/tester.tcl sl@0: sl@0: # Create a basic index and verify it is added to sqlite_master sl@0: # sl@0: do_test index-1.1 { sl@0: execsql {CREATE TABLE test1(f1 int, f2 int, f3 int)} sl@0: execsql {CREATE INDEX index1 ON test1(f1)} sl@0: execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name} sl@0: } {index1 test1} sl@0: do_test index-1.1b { sl@0: execsql {SELECT name, sql, tbl_name, type FROM sqlite_master sl@0: WHERE name='index1'} sl@0: } {index1 {CREATE INDEX index1 ON test1(f1)} test1 index} sl@0: do_test index-1.1c { sl@0: db close sl@0: sqlite3 db test.db sl@0: execsql {SELECT name, sql, tbl_name, type FROM sqlite_master sl@0: WHERE name='index1'} sl@0: } {index1 {CREATE INDEX index1 ON test1(f1)} test1 index} sl@0: do_test index-1.1d { sl@0: db close sl@0: sqlite3 db test.db sl@0: execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name} sl@0: } {index1 test1} sl@0: sl@0: # Verify that the index dies with the table sl@0: # sl@0: do_test index-1.2 { sl@0: execsql {DROP TABLE test1} sl@0: execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name} sl@0: } {} sl@0: sl@0: # Try adding an index to a table that does not exist sl@0: # sl@0: do_test index-2.1 { sl@0: set v [catch {execsql {CREATE INDEX index1 ON test1(f1)}} msg] sl@0: lappend v $msg sl@0: } {1 {no such table: main.test1}} sl@0: sl@0: # Try adding an index on a column of a table where the table sl@0: # exists but the column does not. sl@0: # sl@0: do_test index-2.1 { sl@0: execsql {CREATE TABLE test1(f1 int, f2 int, f3 int)} sl@0: set v [catch {execsql {CREATE INDEX index1 ON test1(f4)}} msg] sl@0: lappend v $msg sl@0: } {1 {table test1 has no column named f4}} sl@0: sl@0: # Try an index with some columns that match and others that do now. sl@0: # sl@0: do_test index-2.2 { sl@0: set v [catch {execsql {CREATE INDEX index1 ON test1(f1, f2, f4, f3)}} msg] sl@0: execsql {DROP TABLE test1} sl@0: lappend v $msg sl@0: } {1 {table test1 has no column named f4}} sl@0: sl@0: # Try creating a bunch of indices on the same table sl@0: # sl@0: set r {} sl@0: for {set i 1} {$i<100} {incr i} { sl@0: lappend r [format index%02d $i] sl@0: } sl@0: do_test index-3.1 { sl@0: execsql {CREATE TABLE test1(f1 int, f2 int, f3 int, f4 int, f5 int)} sl@0: for {set i 1} {$i<100} {incr i} { sl@0: set sql "CREATE INDEX [format index%02d $i] ON test1(f[expr {($i%5)+1}])" sl@0: execsql $sql sl@0: } sl@0: execsql {SELECT name FROM sqlite_master sl@0: WHERE type='index' AND tbl_name='test1' sl@0: ORDER BY name} sl@0: } $r sl@0: integrity_check index-3.2.1 sl@0: ifcapable {reindex} { sl@0: do_test index-3.2.2 { sl@0: execsql REINDEX sl@0: } {} sl@0: } sl@0: integrity_check index-3.2.3 sl@0: sl@0: sl@0: # Verify that all the indices go away when we drop the table. sl@0: # sl@0: do_test index-3.3 { sl@0: execsql {DROP TABLE test1} sl@0: execsql {SELECT name FROM sqlite_master sl@0: WHERE type='index' AND tbl_name='test1' sl@0: ORDER BY name} sl@0: } {} sl@0: sl@0: # Create a table and insert values into that table. Then create sl@0: # an index on that table. Verify that we can select values sl@0: # from the table correctly using the index. sl@0: # sl@0: # Note that the index names "index9" and "indext" are chosen because sl@0: # they both have the same hash. sl@0: # sl@0: do_test index-4.1 { sl@0: execsql {CREATE TABLE test1(cnt int, power int)} sl@0: for {set i 1} {$i<20} {incr i} { sl@0: execsql "INSERT INTO test1 VALUES($i,[expr {1<<$i}])" sl@0: } sl@0: execsql {CREATE INDEX index9 ON test1(cnt)} sl@0: execsql {CREATE INDEX indext ON test1(power)} sl@0: execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name} sl@0: } {index9 indext test1} sl@0: do_test index-4.2 { sl@0: execsql {SELECT cnt FROM test1 WHERE power=4} sl@0: } {2} sl@0: do_test index-4.3 { sl@0: execsql {SELECT cnt FROM test1 WHERE power=1024} sl@0: } {10} sl@0: do_test index-4.4 { sl@0: execsql {SELECT power FROM test1 WHERE cnt=6} sl@0: } {64} sl@0: do_test index-4.5 { sl@0: execsql {DROP INDEX indext} sl@0: execsql {SELECT power FROM test1 WHERE cnt=6} sl@0: } {64} sl@0: do_test index-4.6 { sl@0: execsql {SELECT cnt FROM test1 WHERE power=1024} sl@0: } {10} sl@0: do_test index-4.7 { sl@0: execsql {CREATE INDEX indext ON test1(cnt)} sl@0: execsql {SELECT power FROM test1 WHERE cnt=6} sl@0: } {64} sl@0: do_test index-4.8 { sl@0: execsql {SELECT cnt FROM test1 WHERE power=1024} sl@0: } {10} sl@0: do_test index-4.9 { sl@0: execsql {DROP INDEX index9} sl@0: execsql {SELECT power FROM test1 WHERE cnt=6} sl@0: } {64} sl@0: do_test index-4.10 { sl@0: execsql {SELECT cnt FROM test1 WHERE power=1024} sl@0: } {10} sl@0: do_test index-4.11 { sl@0: execsql {DROP INDEX indext} sl@0: execsql {SELECT power FROM test1 WHERE cnt=6} sl@0: } {64} sl@0: do_test index-4.12 { sl@0: execsql {SELECT cnt FROM test1 WHERE power=1024} sl@0: } {10} sl@0: do_test index-4.13 { sl@0: execsql {DROP TABLE test1} sl@0: execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name} sl@0: } {} sl@0: integrity_check index-4.14 sl@0: sl@0: # Do not allow indices to be added to sqlite_master sl@0: # sl@0: do_test index-5.1 { sl@0: set v [catch {execsql {CREATE INDEX index1 ON sqlite_master(name)}} msg] sl@0: lappend v $msg sl@0: } {1 {table sqlite_master may not be indexed}} sl@0: do_test index-5.2 { sl@0: execsql {SELECT name FROM sqlite_master WHERE type!='meta'} sl@0: } {} sl@0: sl@0: # Do not allow indices with duplicate names to be added sl@0: # sl@0: do_test index-6.1 { sl@0: execsql {CREATE TABLE test1(f1 int, f2 int)} sl@0: execsql {CREATE TABLE test2(g1 real, g2 real)} sl@0: execsql {CREATE INDEX index1 ON test1(f1)} sl@0: set v [catch {execsql {CREATE INDEX index1 ON test2(g1)}} msg] sl@0: lappend v $msg sl@0: } {1 {index index1 already exists}} sl@0: do_test index-6.1.1 { sl@0: catchsql {CREATE INDEX [index1] ON test2(g1)} sl@0: } {1 {index index1 already exists}} sl@0: do_test index-6.1b { sl@0: execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name} sl@0: } {index1 test1 test2} sl@0: do_test index-6.1c { sl@0: catchsql {CREATE INDEX IF NOT EXISTS index1 ON test1(f1)} sl@0: } {0 {}} sl@0: do_test index-6.2 { sl@0: set v [catch {execsql {CREATE INDEX test1 ON test2(g1)}} msg] sl@0: lappend v $msg sl@0: } {1 {there is already a table named test1}} sl@0: do_test index-6.2b { sl@0: execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name} sl@0: } {index1 test1 test2} sl@0: do_test index-6.3 { sl@0: execsql {DROP TABLE test1} sl@0: execsql {DROP TABLE test2} sl@0: execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name} sl@0: } {} sl@0: do_test index-6.4 { sl@0: execsql { sl@0: CREATE TABLE test1(a,b); sl@0: CREATE INDEX index1 ON test1(a); sl@0: CREATE INDEX index2 ON test1(b); sl@0: CREATE INDEX index3 ON test1(a,b); sl@0: DROP TABLE test1; sl@0: SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name; sl@0: } sl@0: } {} sl@0: integrity_check index-6.5 sl@0: sl@0: sl@0: # Create a primary key sl@0: # sl@0: do_test index-7.1 { sl@0: execsql {CREATE TABLE test1(f1 int, f2 int primary key)} sl@0: for {set i 1} {$i<20} {incr i} { sl@0: execsql "INSERT INTO test1 VALUES($i,[expr {1<<$i}])" sl@0: } sl@0: execsql {SELECT count(*) FROM test1} sl@0: } {19} sl@0: do_test index-7.2 { sl@0: execsql {SELECT f1 FROM test1 WHERE f2=65536} sl@0: } {16} sl@0: do_test index-7.3 { sl@0: execsql { sl@0: SELECT name FROM sqlite_master sl@0: WHERE type='index' AND tbl_name='test1' sl@0: } sl@0: } {sqlite_autoindex_test1_1} sl@0: do_test index-7.4 { sl@0: execsql {DROP table test1} sl@0: execsql {SELECT name FROM sqlite_master WHERE type!='meta'} sl@0: } {} sl@0: integrity_check index-7.5 sl@0: sl@0: # Make sure we cannot drop a non-existant index. sl@0: # sl@0: do_test index-8.1 { sl@0: set v [catch {execsql {DROP INDEX index1}} msg] sl@0: lappend v $msg sl@0: } {1 {no such index: index1}} sl@0: sl@0: # Make sure we don't actually create an index when the EXPLAIN keyword sl@0: # is used. sl@0: # sl@0: do_test index-9.1 { sl@0: execsql {CREATE TABLE tab1(a int)} sl@0: ifcapable {explain} { sl@0: execsql {EXPLAIN CREATE INDEX idx1 ON tab1(a)} sl@0: } sl@0: execsql {SELECT name FROM sqlite_master WHERE tbl_name='tab1'} sl@0: } {tab1} sl@0: do_test index-9.2 { sl@0: execsql {CREATE INDEX idx1 ON tab1(a)} sl@0: execsql {SELECT name FROM sqlite_master WHERE tbl_name='tab1' ORDER BY name} sl@0: } {idx1 tab1} sl@0: integrity_check index-9.3 sl@0: sl@0: # Allow more than one entry with the same key. sl@0: # sl@0: do_test index-10.0 { sl@0: execsql { sl@0: CREATE TABLE t1(a int, b int); sl@0: CREATE INDEX i1 ON t1(a); sl@0: INSERT INTO t1 VALUES(1,2); sl@0: INSERT INTO t1 VALUES(2,4); sl@0: INSERT INTO t1 VALUES(3,8); sl@0: INSERT INTO t1 VALUES(1,12); sl@0: SELECT b FROM t1 WHERE a=1 ORDER BY b; sl@0: } sl@0: } {2 12} sl@0: do_test index-10.1 { sl@0: execsql { sl@0: SELECT b FROM t1 WHERE a=2 ORDER BY b; sl@0: } sl@0: } {4} sl@0: do_test index-10.2 { sl@0: execsql { sl@0: DELETE FROM t1 WHERE b=12; sl@0: SELECT b FROM t1 WHERE a=1 ORDER BY b; sl@0: } sl@0: } {2} sl@0: do_test index-10.3 { sl@0: execsql { sl@0: DELETE FROM t1 WHERE b=2; sl@0: SELECT b FROM t1 WHERE a=1 ORDER BY b; sl@0: } sl@0: } {} sl@0: do_test index-10.4 { sl@0: execsql { sl@0: DELETE FROM t1; sl@0: INSERT INTO t1 VALUES (1,1); sl@0: INSERT INTO t1 VALUES (1,2); sl@0: INSERT INTO t1 VALUES (1,3); sl@0: INSERT INTO t1 VALUES (1,4); sl@0: INSERT INTO t1 VALUES (1,5); sl@0: INSERT INTO t1 VALUES (1,6); sl@0: INSERT INTO t1 VALUES (1,7); sl@0: INSERT INTO t1 VALUES (1,8); sl@0: INSERT INTO t1 VALUES (1,9); sl@0: INSERT INTO t1 VALUES (2,0); sl@0: SELECT b FROM t1 WHERE a=1 ORDER BY b; sl@0: } sl@0: } {1 2 3 4 5 6 7 8 9} sl@0: do_test index-10.5 { sl@0: ifcapable subquery { sl@0: execsql { DELETE FROM t1 WHERE b IN (2, 4, 6, 8); } sl@0: } else { sl@0: execsql { DELETE FROM t1 WHERE b = 2 OR b = 4 OR b = 6 OR b = 8; } sl@0: } sl@0: execsql { sl@0: SELECT b FROM t1 WHERE a=1 ORDER BY b; sl@0: } sl@0: } {1 3 5 7 9} sl@0: do_test index-10.6 { sl@0: execsql { sl@0: DELETE FROM t1 WHERE b>2; sl@0: SELECT b FROM t1 WHERE a=1 ORDER BY b; sl@0: } sl@0: } {1} sl@0: do_test index-10.7 { sl@0: execsql { sl@0: DELETE FROM t1 WHERE b=1; sl@0: SELECT b FROM t1 WHERE a=1 ORDER BY b; sl@0: } sl@0: } {} sl@0: do_test index-10.8 { sl@0: execsql { sl@0: SELECT b FROM t1 ORDER BY b; sl@0: } sl@0: } {0} sl@0: integrity_check index-10.9 sl@0: sl@0: # Automatically create an index when we specify a primary key. sl@0: # sl@0: do_test index-11.1 { sl@0: execsql { sl@0: CREATE TABLE t3( sl@0: a text, sl@0: b int, sl@0: c float, sl@0: PRIMARY KEY(b) sl@0: ); sl@0: } sl@0: for {set i 1} {$i<=50} {incr i} { sl@0: execsql "INSERT INTO t3 VALUES('x${i}x',$i,0.$i)" sl@0: } sl@0: set sqlite_search_count 0 sl@0: concat [execsql {SELECT c FROM t3 WHERE b==10}] $sqlite_search_count sl@0: } {0.1 3} sl@0: integrity_check index-11.2 sl@0: sl@0: sl@0: # Numeric strings should compare as if they were numbers. So even if the sl@0: # strings are not character-by-character the same, if they represent the sl@0: # same number they should compare equal to one another. Verify that this sl@0: # is true in indices. sl@0: # sl@0: # Updated for sqlite3 v3: SQLite will now store these values as numbers sl@0: # (because the affinity of column a is NUMERIC) so the quirky sl@0: # representations are not retained. i.e. '+1.0' becomes '1'. sl@0: do_test index-12.1 { sl@0: execsql { sl@0: CREATE TABLE t4(a NUM,b); sl@0: INSERT INTO t4 VALUES('0.0',1); sl@0: INSERT INTO t4 VALUES('0.00',2); sl@0: INSERT INTO t4 VALUES('abc',3); sl@0: INSERT INTO t4 VALUES('-1.0',4); sl@0: INSERT INTO t4 VALUES('+1.0',5); sl@0: INSERT INTO t4 VALUES('0',6); sl@0: INSERT INTO t4 VALUES('00000',7); sl@0: SELECT a FROM t4 ORDER BY b; sl@0: } sl@0: } {0 0 abc -1 1 0 0} sl@0: do_test index-12.2 { sl@0: execsql { sl@0: SELECT a FROM t4 WHERE a==0 ORDER BY b sl@0: } sl@0: } {0 0 0 0} sl@0: do_test index-12.3 { sl@0: execsql { sl@0: SELECT a FROM t4 WHERE a<0.5 ORDER BY b sl@0: } sl@0: } {0 0 -1 0 0} sl@0: do_test index-12.4 { sl@0: execsql { sl@0: SELECT a FROM t4 WHERE a>-0.5 ORDER BY b sl@0: } sl@0: } {0 0 abc 1 0 0} sl@0: do_test index-12.5 { sl@0: execsql { sl@0: CREATE INDEX t4i1 ON t4(a); sl@0: SELECT a FROM t4 WHERE a==0 ORDER BY b sl@0: } sl@0: } {0 0 0 0} sl@0: do_test index-12.6 { sl@0: execsql { sl@0: SELECT a FROM t4 WHERE a<0.5 ORDER BY b sl@0: } sl@0: } {0 0 -1 0 0} sl@0: do_test index-12.7 { sl@0: execsql { sl@0: SELECT a FROM t4 WHERE a>-0.5 ORDER BY b sl@0: } sl@0: } {0 0 abc 1 0 0} sl@0: integrity_check index-12.8 sl@0: sl@0: # Make sure we cannot drop an automatically created index. sl@0: # sl@0: do_test index-13.1 { sl@0: execsql { sl@0: CREATE TABLE t5( sl@0: a int UNIQUE, sl@0: b float PRIMARY KEY, sl@0: c varchar(10), sl@0: UNIQUE(a,c) sl@0: ); sl@0: INSERT INTO t5 VALUES(1,2,3); sl@0: SELECT * FROM t5; sl@0: } sl@0: } {1 2.0 3} sl@0: do_test index-13.2 { sl@0: set ::idxlist [execsql { sl@0: SELECT name FROM sqlite_master WHERE type="index" AND tbl_name="t5"; sl@0: }] sl@0: llength $::idxlist sl@0: } {3} sl@0: for {set i 0} {$i<[llength $::idxlist]} {incr i} { sl@0: do_test index-13.3.$i { sl@0: catchsql " sl@0: DROP INDEX '[lindex $::idxlist $i]'; sl@0: " sl@0: } {1 {index associated with UNIQUE or PRIMARY KEY constraint cannot be dropped}} sl@0: } sl@0: do_test index-13.4 { sl@0: execsql { sl@0: INSERT INTO t5 VALUES('a','b','c'); sl@0: SELECT * FROM t5; sl@0: } sl@0: } {1 2.0 3 a b c} sl@0: integrity_check index-13.5 sl@0: sl@0: # Check the sort order of data in an index. sl@0: # sl@0: do_test index-14.1 { sl@0: execsql { sl@0: CREATE TABLE t6(a,b,c); sl@0: CREATE INDEX t6i1 ON t6(a,b); sl@0: INSERT INTO t6 VALUES('','',1); sl@0: INSERT INTO t6 VALUES('',NULL,2); sl@0: INSERT INTO t6 VALUES(NULL,'',3); sl@0: INSERT INTO t6 VALUES('abc',123,4); sl@0: INSERT INTO t6 VALUES(123,'abc',5); sl@0: SELECT c FROM t6 ORDER BY a,b; sl@0: } sl@0: } {3 5 2 1 4} sl@0: do_test index-14.2 { sl@0: execsql { sl@0: SELECT c FROM t6 WHERE a=''; sl@0: } sl@0: } {2 1} sl@0: do_test index-14.3 { sl@0: execsql { sl@0: SELECT c FROM t6 WHERE b=''; sl@0: } sl@0: } {1 3} sl@0: do_test index-14.4 { sl@0: execsql { sl@0: SELECT c FROM t6 WHERE a>''; sl@0: } sl@0: } {4} sl@0: do_test index-14.5 { sl@0: execsql { sl@0: SELECT c FROM t6 WHERE a>=''; sl@0: } sl@0: } {2 1 4} sl@0: do_test index-14.6 { sl@0: execsql { sl@0: SELECT c FROM t6 WHERE a>123; sl@0: } sl@0: } {2 1 4} sl@0: do_test index-14.7 { sl@0: execsql { sl@0: SELECT c FROM t6 WHERE a>=123; sl@0: } sl@0: } {5 2 1 4} sl@0: do_test index-14.8 { sl@0: execsql { sl@0: SELECT c FROM t6 WHERE a<'abc'; sl@0: } sl@0: } {5 2 1} sl@0: do_test index-14.9 { sl@0: execsql { sl@0: SELECT c FROM t6 WHERE a<='abc'; sl@0: } sl@0: } {5 2 1 4} sl@0: do_test index-14.10 { sl@0: execsql { sl@0: SELECT c FROM t6 WHERE a<=''; sl@0: } sl@0: } {5 2 1} sl@0: do_test index-14.11 { sl@0: execsql { sl@0: SELECT c FROM t6 WHERE a<''; sl@0: } sl@0: } {5} sl@0: integrity_check index-14.12 sl@0: sl@0: do_test index-15.1 { sl@0: execsql { sl@0: DELETE FROM t1; sl@0: SELECT * FROM t1; sl@0: } sl@0: } {} sl@0: do_test index-15.2 { sl@0: execsql { sl@0: INSERT INTO t1 VALUES('1.234e5',1); sl@0: INSERT INTO t1 VALUES('12.33e04',2); sl@0: INSERT INTO t1 VALUES('12.35E4',3); sl@0: INSERT INTO t1 VALUES('12.34e',4); sl@0: INSERT INTO t1 VALUES('12.32e+4',5); sl@0: INSERT INTO t1 VALUES('12.36E+04',6); sl@0: INSERT INTO t1 VALUES('12.36E+',7); sl@0: INSERT INTO t1 VALUES('+123.10000E+0003',8); sl@0: INSERT INTO t1 VALUES('+',9); sl@0: INSERT INTO t1 VALUES('+12347.E+02',10); sl@0: INSERT INTO t1 VALUES('+12347E+02',11); sl@0: SELECT b FROM t1 ORDER BY a; sl@0: } sl@0: } {8 5 2 1 3 6 11 9 10 4 7} sl@0: integrity_check index-15.1 sl@0: sl@0: # The following tests - index-16.* - test that when a table definition sl@0: # includes qualifications that specify the same constraint twice only a sl@0: # single index is generated to enforce the constraint. sl@0: # sl@0: # For example: "CREATE TABLE abc( x PRIMARY KEY, UNIQUE(x) );" sl@0: # sl@0: do_test index-16.1 { sl@0: execsql { sl@0: CREATE TABLE t7(c UNIQUE PRIMARY KEY); sl@0: SELECT count(*) FROM sqlite_master WHERE tbl_name = 't7' AND type = 'index'; sl@0: } sl@0: } {1} sl@0: do_test index-16.2 { sl@0: execsql { sl@0: DROP TABLE t7; sl@0: CREATE TABLE t7(c UNIQUE PRIMARY KEY); sl@0: SELECT count(*) FROM sqlite_master WHERE tbl_name = 't7' AND type = 'index'; sl@0: } sl@0: } {1} sl@0: do_test index-16.3 { sl@0: execsql { sl@0: DROP TABLE t7; sl@0: CREATE TABLE t7(c PRIMARY KEY, UNIQUE(c) ); sl@0: SELECT count(*) FROM sqlite_master WHERE tbl_name = 't7' AND type = 'index'; sl@0: } sl@0: } {1} sl@0: do_test index-16.4 { sl@0: execsql { sl@0: DROP TABLE t7; sl@0: CREATE TABLE t7(c, d , UNIQUE(c, d), PRIMARY KEY(c, d) ); sl@0: SELECT count(*) FROM sqlite_master WHERE tbl_name = 't7' AND type = 'index'; sl@0: } sl@0: } {1} sl@0: do_test index-16.5 { sl@0: execsql { sl@0: DROP TABLE t7; sl@0: CREATE TABLE t7(c, d , UNIQUE(c), PRIMARY KEY(c, d) ); sl@0: SELECT count(*) FROM sqlite_master WHERE tbl_name = 't7' AND type = 'index'; sl@0: } sl@0: } {2} sl@0: sl@0: # Test that automatically create indices are named correctly. The current sl@0: # convention is: "sqlite_autoindex__" sl@0: # sl@0: # Then check that it is an error to try to drop any automtically created sl@0: # indices. sl@0: do_test index-17.1 { sl@0: execsql { sl@0: DROP TABLE t7; sl@0: CREATE TABLE t7(c, d UNIQUE, UNIQUE(c), PRIMARY KEY(c, d) ); sl@0: SELECT name FROM sqlite_master WHERE tbl_name = 't7' AND type = 'index'; sl@0: } sl@0: } {sqlite_autoindex_t7_1 sqlite_autoindex_t7_2 sqlite_autoindex_t7_3} sl@0: do_test index-17.2 { sl@0: catchsql { sl@0: DROP INDEX sqlite_autoindex_t7_1; sl@0: } sl@0: } {1 {index associated with UNIQUE or PRIMARY KEY constraint cannot be dropped}} sl@0: do_test index-17.3 { sl@0: catchsql { sl@0: DROP INDEX IF EXISTS sqlite_autoindex_t7_1; sl@0: } sl@0: } {1 {index associated with UNIQUE or PRIMARY KEY constraint cannot be dropped}} sl@0: do_test index-17.4 { sl@0: catchsql { sl@0: DROP INDEX IF EXISTS no_such_index; sl@0: } sl@0: } {0 {}} sl@0: sl@0: sl@0: # The following tests ensure that it is not possible to explicitly name sl@0: # a schema object with a name beginning with "sqlite_". Granted that is a sl@0: # little outside the focus of this test scripts, but this has got to be sl@0: # tested somewhere. sl@0: do_test index-18.1 { sl@0: catchsql { sl@0: CREATE TABLE sqlite_t1(a, b, c); sl@0: } sl@0: } {1 {object name reserved for internal use: sqlite_t1}} sl@0: do_test index-18.2 { sl@0: catchsql { sl@0: CREATE INDEX sqlite_i1 ON t7(c); sl@0: } sl@0: } {1 {object name reserved for internal use: sqlite_i1}} sl@0: ifcapable view { sl@0: do_test index-18.3 { sl@0: catchsql { sl@0: CREATE VIEW sqlite_v1 AS SELECT * FROM t7; sl@0: } sl@0: } {1 {object name reserved for internal use: sqlite_v1}} sl@0: } ;# ifcapable view sl@0: ifcapable {trigger} { sl@0: do_test index-18.4 { sl@0: catchsql { sl@0: CREATE TRIGGER sqlite_tr1 BEFORE INSERT ON t7 BEGIN SELECT 1; END; sl@0: } sl@0: } {1 {object name reserved for internal use: sqlite_tr1}} sl@0: } sl@0: do_test index-18.5 { sl@0: execsql { sl@0: DROP TABLE t7; sl@0: } sl@0: } {} sl@0: sl@0: # These tests ensure that if multiple table definition constraints are sl@0: # implemented by a single indice, the correct ON CONFLICT policy applies. sl@0: ifcapable conflict { sl@0: do_test index-19.1 { sl@0: execsql { sl@0: CREATE TABLE t7(a UNIQUE PRIMARY KEY); sl@0: CREATE TABLE t8(a UNIQUE PRIMARY KEY ON CONFLICT ROLLBACK); sl@0: INSERT INTO t7 VALUES(1); sl@0: INSERT INTO t8 VALUES(1); sl@0: } sl@0: } {} sl@0: do_test index-19.2 { sl@0: catchsql { sl@0: BEGIN; sl@0: INSERT INTO t7 VALUES(1); sl@0: } sl@0: } {1 {column a is not unique}} sl@0: do_test index-19.3 { sl@0: catchsql { sl@0: BEGIN; sl@0: } sl@0: } {1 {cannot start a transaction within a transaction}} sl@0: do_test index-19.4 { sl@0: catchsql { sl@0: INSERT INTO t8 VALUES(1); sl@0: } sl@0: } {1 {column a is not unique}} sl@0: do_test index-19.5 { sl@0: catchsql { sl@0: BEGIN; sl@0: COMMIT; sl@0: } sl@0: } {0 {}} sl@0: do_test index-19.6 { sl@0: catchsql { sl@0: DROP TABLE t7; sl@0: DROP TABLE t8; sl@0: CREATE TABLE t7( sl@0: a PRIMARY KEY ON CONFLICT FAIL, sl@0: UNIQUE(a) ON CONFLICT IGNORE sl@0: ); sl@0: } sl@0: } {1 {conflicting ON CONFLICT clauses specified}} sl@0: } ; # end of "ifcapable conflict" block sl@0: sl@0: ifcapable {reindex} { sl@0: do_test index-19.7 { sl@0: execsql REINDEX sl@0: } {} sl@0: } sl@0: integrity_check index-19.8 sl@0: sl@0: # Drop index with a quoted name. Ticket #695. sl@0: # sl@0: do_test index-20.1 { sl@0: execsql { sl@0: CREATE INDEX "t6i2" ON t6(c); sl@0: DROP INDEX "t6i2"; sl@0: } sl@0: } {} sl@0: do_test index-20.2 { sl@0: execsql { sl@0: DROP INDEX "t6i1"; sl@0: } sl@0: } {} sl@0: sl@0: sl@0: finish_test