drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 1 | # 2001 September 15 |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 2 | # |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 3 | # The author disclaims copyright to this source code. In place of |
| 4 | # a legal notice, here is a blessing: |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 5 | # |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 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. |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 9 | # |
| 10 | #*********************************************************************** |
| 11 | # This file implements regression tests for SQLite library. The |
| 12 | # focus of this file is testing the CREATE INDEX statement. |
| 13 | # |
drh | e497f00 | 2004-11-07 13:01:49 +0000 | [diff] [blame^] | 14 | # $Id: index.test,v 1.35 2004/11/07 13:01:50 drh Exp $ |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 15 | |
| 16 | set testdir [file dirname $argv0] |
| 17 | source $testdir/tester.tcl |
| 18 | |
| 19 | # Create a basic index and verify it is added to sqlite_master |
| 20 | # |
drh | 1b6a71f | 2000-05-29 23:58:11 +0000 | [diff] [blame] | 21 | do_test index-1.1 { |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 22 | execsql {CREATE TABLE test1(f1 int, f2 int, f3 int)} |
| 23 | execsql {CREATE INDEX index1 ON test1(f1)} |
drh | 2803757 | 2000-08-02 13:47:41 +0000 | [diff] [blame] | 24 | execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name} |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 25 | } {index1 test1} |
drh | 1b6a71f | 2000-05-29 23:58:11 +0000 | [diff] [blame] | 26 | do_test index-1.1b { |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 27 | execsql {SELECT name, sql, tbl_name, type FROM sqlite_master |
| 28 | WHERE name='index1'} |
| 29 | } {index1 {CREATE INDEX index1 ON test1(f1)} test1 index} |
drh | 1b6a71f | 2000-05-29 23:58:11 +0000 | [diff] [blame] | 30 | do_test index-1.1c { |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 31 | db close |
drh | ef4ac8f | 2004-06-19 00:16:31 +0000 | [diff] [blame] | 32 | sqlite3 db test.db |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 33 | execsql {SELECT name, sql, tbl_name, type FROM sqlite_master |
| 34 | WHERE name='index1'} |
| 35 | } {index1 {CREATE INDEX index1 ON test1(f1)} test1 index} |
drh | 1b6a71f | 2000-05-29 23:58:11 +0000 | [diff] [blame] | 36 | do_test index-1.1d { |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 37 | db close |
drh | ef4ac8f | 2004-06-19 00:16:31 +0000 | [diff] [blame] | 38 | sqlite3 db test.db |
drh | 2803757 | 2000-08-02 13:47:41 +0000 | [diff] [blame] | 39 | execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name} |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 40 | } {index1 test1} |
| 41 | |
| 42 | # Verify that the index dies with the table |
| 43 | # |
drh | 1b6a71f | 2000-05-29 23:58:11 +0000 | [diff] [blame] | 44 | do_test index-1.2 { |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 45 | execsql {DROP TABLE test1} |
drh | 2803757 | 2000-08-02 13:47:41 +0000 | [diff] [blame] | 46 | execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name} |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 47 | } {} |
| 48 | |
| 49 | # Try adding an index to a table that does not exist |
| 50 | # |
drh | 1b6a71f | 2000-05-29 23:58:11 +0000 | [diff] [blame] | 51 | do_test index-2.1 { |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 52 | set v [catch {execsql {CREATE INDEX index1 ON test1(f1)}} msg] |
| 53 | lappend v $msg |
danielk1977 | 48dec7e | 2004-05-28 12:33:30 +0000 | [diff] [blame] | 54 | } {1 {no such table: main.test1}} |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 55 | |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 56 | # Try adding an index on a column of a table where the table |
| 57 | # exists but the column does not. |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 58 | # |
drh | 1b6a71f | 2000-05-29 23:58:11 +0000 | [diff] [blame] | 59 | do_test index-2.1 { |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 60 | execsql {CREATE TABLE test1(f1 int, f2 int, f3 int)} |
| 61 | set v [catch {execsql {CREATE INDEX index1 ON test1(f4)}} msg] |
| 62 | lappend v $msg |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 63 | } {1 {table test1 has no column named f4}} |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 64 | |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 65 | # Try an index with some columns that match and others that do now. |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 66 | # |
drh | 1b6a71f | 2000-05-29 23:58:11 +0000 | [diff] [blame] | 67 | do_test index-2.2 { |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 68 | set v [catch {execsql {CREATE INDEX index1 ON test1(f1, f2, f4, f3)}} msg] |
| 69 | execsql {DROP TABLE test1} |
| 70 | lappend v $msg |
drh | 1ccde15 | 2000-06-17 13:12:39 +0000 | [diff] [blame] | 71 | } {1 {table test1 has no column named f4}} |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 72 | |
| 73 | # Try creating a bunch of indices on the same table |
| 74 | # |
| 75 | set r {} |
| 76 | for {set i 1} {$i<100} {incr i} { |
drh | a9e99ae | 2002-08-13 23:02:57 +0000 | [diff] [blame] | 77 | lappend r [format index%02d $i] |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 78 | } |
drh | 1b6a71f | 2000-05-29 23:58:11 +0000 | [diff] [blame] | 79 | do_test index-3.1 { |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 80 | execsql {CREATE TABLE test1(f1 int, f2 int, f3 int, f4 int, f5 int)} |
| 81 | for {set i 1} {$i<100} {incr i} { |
drh | a9e99ae | 2002-08-13 23:02:57 +0000 | [diff] [blame] | 82 | set sql "CREATE INDEX [format index%02d $i] ON test1(f[expr {($i%5)+1}])" |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 83 | execsql $sql |
| 84 | } |
| 85 | execsql {SELECT name FROM sqlite_master |
| 86 | WHERE type='index' AND tbl_name='test1' |
| 87 | ORDER BY name} |
| 88 | } $r |
drh | e497f00 | 2004-11-07 13:01:49 +0000 | [diff] [blame^] | 89 | integrity_check index-3.2.1 |
| 90 | ifcapable {reindex} { |
| 91 | do_test index-3.2.2 { |
| 92 | execsql REINDEX |
| 93 | } {} |
| 94 | } |
| 95 | integrity_check index-3.2.3 |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 96 | |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 97 | |
| 98 | # Verify that all the indices go away when we drop the table. |
| 99 | # |
drh | 1b6a71f | 2000-05-29 23:58:11 +0000 | [diff] [blame] | 100 | do_test index-3.3 { |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 101 | execsql {DROP TABLE test1} |
| 102 | execsql {SELECT name FROM sqlite_master |
| 103 | WHERE type='index' AND tbl_name='test1' |
| 104 | ORDER BY name} |
| 105 | } {} |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 106 | |
| 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. |
| 110 | # |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 111 | # Note that the index names "index9" and "indext" are chosen because |
| 112 | # they both have the same hash. |
| 113 | # |
drh | 1b6a71f | 2000-05-29 23:58:11 +0000 | [diff] [blame] | 114 | do_test index-4.1 { |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 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 {int(pow(2,$i))}])" |
| 118 | } |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 119 | execsql {CREATE INDEX index9 ON test1(cnt)} |
| 120 | execsql {CREATE INDEX indext ON test1(power)} |
drh | 2803757 | 2000-08-02 13:47:41 +0000 | [diff] [blame] | 121 | execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name} |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 122 | } {index9 indext test1} |
drh | 1b6a71f | 2000-05-29 23:58:11 +0000 | [diff] [blame] | 123 | do_test index-4.2 { |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 124 | execsql {SELECT cnt FROM test1 WHERE power=4} |
| 125 | } {2} |
drh | 1b6a71f | 2000-05-29 23:58:11 +0000 | [diff] [blame] | 126 | do_test index-4.3 { |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 127 | execsql {SELECT cnt FROM test1 WHERE power=1024} |
| 128 | } {10} |
drh | 1b6a71f | 2000-05-29 23:58:11 +0000 | [diff] [blame] | 129 | do_test index-4.4 { |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 130 | execsql {SELECT power FROM test1 WHERE cnt=6} |
| 131 | } {64} |
drh | 1b6a71f | 2000-05-29 23:58:11 +0000 | [diff] [blame] | 132 | do_test index-4.5 { |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 133 | execsql {DROP INDEX indext} |
| 134 | execsql {SELECT power FROM test1 WHERE cnt=6} |
| 135 | } {64} |
| 136 | do_test index-4.6 { |
| 137 | execsql {SELECT cnt FROM test1 WHERE power=1024} |
| 138 | } {10} |
| 139 | do_test index-4.7 { |
| 140 | execsql {CREATE INDEX indext ON test1(cnt)} |
| 141 | execsql {SELECT power FROM test1 WHERE cnt=6} |
| 142 | } {64} |
| 143 | do_test index-4.8 { |
| 144 | execsql {SELECT cnt FROM test1 WHERE power=1024} |
| 145 | } {10} |
| 146 | do_test index-4.9 { |
| 147 | execsql {DROP INDEX index9} |
| 148 | execsql {SELECT power FROM test1 WHERE cnt=6} |
| 149 | } {64} |
| 150 | do_test index-4.10 { |
| 151 | execsql {SELECT cnt FROM test1 WHERE power=1024} |
| 152 | } {10} |
| 153 | do_test index-4.11 { |
| 154 | execsql {DROP INDEX indext} |
| 155 | execsql {SELECT power FROM test1 WHERE cnt=6} |
| 156 | } {64} |
| 157 | do_test index-4.12 { |
| 158 | execsql {SELECT cnt FROM test1 WHERE power=1024} |
| 159 | } {10} |
| 160 | do_test index-4.13 { |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 161 | execsql {DROP TABLE test1} |
drh | 2803757 | 2000-08-02 13:47:41 +0000 | [diff] [blame] | 162 | execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name} |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 163 | } {} |
drh | ed717fe | 2003-06-15 23:42:24 +0000 | [diff] [blame] | 164 | integrity_check index-4.14 |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 165 | |
| 166 | # Do not allow indices to be added to sqlite_master |
| 167 | # |
drh | 1b6a71f | 2000-05-29 23:58:11 +0000 | [diff] [blame] | 168 | do_test index-5.1 { |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 169 | set v [catch {execsql {CREATE INDEX index1 ON sqlite_master(name)}} msg] |
| 170 | lappend v $msg |
drh | 0be9df0 | 2003-03-30 00:19:49 +0000 | [diff] [blame] | 171 | } {1 {table sqlite_master may not be indexed}} |
drh | 1b6a71f | 2000-05-29 23:58:11 +0000 | [diff] [blame] | 172 | do_test index-5.2 { |
drh | 2803757 | 2000-08-02 13:47:41 +0000 | [diff] [blame] | 173 | execsql {SELECT name FROM sqlite_master WHERE type!='meta'} |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 174 | } {} |
| 175 | |
| 176 | # Do not allow indices with duplicate names to be added |
| 177 | # |
drh | 1b6a71f | 2000-05-29 23:58:11 +0000 | [diff] [blame] | 178 | do_test index-6.1 { |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 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] |
| 183 | lappend v $msg |
drh | 1d37e28 | 2000-05-30 03:12:21 +0000 | [diff] [blame] | 184 | } {1 {index index1 already exists}} |
drh | 1b6a71f | 2000-05-29 23:58:11 +0000 | [diff] [blame] | 185 | do_test index-6.1b { |
drh | 2803757 | 2000-08-02 13:47:41 +0000 | [diff] [blame] | 186 | execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name} |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 187 | } {index1 test1 test2} |
drh | 1b6a71f | 2000-05-29 23:58:11 +0000 | [diff] [blame] | 188 | do_test index-6.2 { |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 189 | set v [catch {execsql {CREATE INDEX test1 ON test2(g1)}} msg] |
| 190 | lappend v $msg |
drh | 1d37e28 | 2000-05-30 03:12:21 +0000 | [diff] [blame] | 191 | } {1 {there is already a table named test1}} |
drh | 1b6a71f | 2000-05-29 23:58:11 +0000 | [diff] [blame] | 192 | do_test index-6.2b { |
drh | 2803757 | 2000-08-02 13:47:41 +0000 | [diff] [blame] | 193 | execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name} |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 194 | } {index1 test1 test2} |
drh | 1b6a71f | 2000-05-29 23:58:11 +0000 | [diff] [blame] | 195 | do_test index-6.3 { |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 196 | execsql {DROP TABLE test1} |
| 197 | execsql {DROP TABLE test2} |
drh | 2803757 | 2000-08-02 13:47:41 +0000 | [diff] [blame] | 198 | execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name} |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 199 | } {} |
drh | f5bf0a7 | 2001-11-23 00:24:12 +0000 | [diff] [blame] | 200 | do_test index-6.4 { |
| 201 | execsql { |
| 202 | CREATE TABLE test1(a,b); |
| 203 | CREATE INDEX index1 ON test1(a); |
| 204 | CREATE INDEX index2 ON test1(b); |
| 205 | CREATE INDEX index3 ON test1(a,b); |
| 206 | DROP TABLE test1; |
| 207 | SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name; |
| 208 | } |
| 209 | } {} |
drh | ed717fe | 2003-06-15 23:42:24 +0000 | [diff] [blame] | 210 | integrity_check index-6.5 |
| 211 | |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 212 | |
| 213 | # Create a primary key |
| 214 | # |
drh | 1b6a71f | 2000-05-29 23:58:11 +0000 | [diff] [blame] | 215 | do_test index-7.1 { |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 216 | execsql {CREATE TABLE test1(f1 int, f2 int primary key)} |
| 217 | for {set i 1} {$i<20} {incr i} { |
| 218 | execsql "INSERT INTO test1 VALUES($i,[expr {int(pow(2,$i))}])" |
| 219 | } |
drh | 767c200 | 2000-10-19 14:10:08 +0000 | [diff] [blame] | 220 | execsql {SELECT count(*) FROM test1} |
| 221 | } {19} |
drh | 1b6a71f | 2000-05-29 23:58:11 +0000 | [diff] [blame] | 222 | do_test index-7.2 { |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 223 | execsql {SELECT f1 FROM test1 WHERE f2=65536} |
| 224 | } {16} |
drh | 1b6a71f | 2000-05-29 23:58:11 +0000 | [diff] [blame] | 225 | do_test index-7.3 { |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 226 | execsql { |
| 227 | SELECT name FROM sqlite_master |
| 228 | WHERE type='index' AND tbl_name='test1' |
| 229 | } |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 230 | } {sqlite_autoindex_test1_1} |
drh | 1b6a71f | 2000-05-29 23:58:11 +0000 | [diff] [blame] | 231 | do_test index-7.4 { |
| 232 | execsql {DROP table test1} |
drh | 2803757 | 2000-08-02 13:47:41 +0000 | [diff] [blame] | 233 | execsql {SELECT name FROM sqlite_master WHERE type!='meta'} |
drh | 1b6a71f | 2000-05-29 23:58:11 +0000 | [diff] [blame] | 234 | } {} |
drh | ed717fe | 2003-06-15 23:42:24 +0000 | [diff] [blame] | 235 | integrity_check index-7.5 |
drh | 1b6a71f | 2000-05-29 23:58:11 +0000 | [diff] [blame] | 236 | |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 237 | # Make sure we cannot drop a non-existant index. |
drh | 1b6a71f | 2000-05-29 23:58:11 +0000 | [diff] [blame] | 238 | # |
| 239 | do_test index-8.1 { |
| 240 | set v [catch {execsql {DROP INDEX index1}} msg] |
| 241 | lappend v $msg |
drh | 1d37e28 | 2000-05-30 03:12:21 +0000 | [diff] [blame] | 242 | } {1 {no such index: index1}} |
drh | 1b6a71f | 2000-05-29 23:58:11 +0000 | [diff] [blame] | 243 | |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 244 | # Make sure we don't actually create an index when the EXPLAIN keyword |
| 245 | # is used. |
| 246 | # |
| 247 | do_test index-9.1 { |
| 248 | execsql {CREATE TABLE tab1(a int)} |
drh | 6bf8957 | 2004-11-03 16:27:01 +0000 | [diff] [blame] | 249 | ifcapable {explain} { |
| 250 | execsql {EXPLAIN CREATE INDEX idx1 ON tab1(a)} |
| 251 | } |
drh | 7020f65 | 2000-06-03 18:06:52 +0000 | [diff] [blame] | 252 | execsql {SELECT name FROM sqlite_master WHERE tbl_name='tab1'} |
| 253 | } {tab1} |
| 254 | do_test index-9.2 { |
| 255 | execsql {CREATE INDEX idx1 ON tab1(a)} |
| 256 | execsql {SELECT name FROM sqlite_master WHERE tbl_name='tab1' ORDER BY name} |
| 257 | } {idx1 tab1} |
drh | ed717fe | 2003-06-15 23:42:24 +0000 | [diff] [blame] | 258 | integrity_check index-9.3 |
drh | b24fcbe | 2000-05-29 23:30:50 +0000 | [diff] [blame] | 259 | |
drh | e840972 | 2000-06-08 16:54:40 +0000 | [diff] [blame] | 260 | # Allow more than one entry with the same key. |
| 261 | # |
| 262 | do_test index-10.0 { |
| 263 | execsql { |
| 264 | CREATE TABLE t1(a int, b int); |
| 265 | CREATE INDEX i1 ON t1(a); |
| 266 | INSERT INTO t1 VALUES(1,2); |
| 267 | INSERT INTO t1 VALUES(2,4); |
| 268 | INSERT INTO t1 VALUES(3,8); |
| 269 | INSERT INTO t1 VALUES(1,12); |
| 270 | SELECT b FROM t1 WHERE a=1 ORDER BY b; |
| 271 | } |
| 272 | } {2 12} |
| 273 | do_test index-10.1 { |
| 274 | execsql { |
| 275 | SELECT b FROM t1 WHERE a=2 ORDER BY b; |
| 276 | } |
| 277 | } {4} |
| 278 | do_test index-10.2 { |
| 279 | execsql { |
| 280 | DELETE FROM t1 WHERE b=12; |
| 281 | SELECT b FROM t1 WHERE a=1 ORDER BY b; |
| 282 | } |
| 283 | } {2} |
drh | 353f57e | 2000-08-02 12:26:28 +0000 | [diff] [blame] | 284 | do_test index-10.3 { |
drh | e840972 | 2000-06-08 16:54:40 +0000 | [diff] [blame] | 285 | execsql { |
| 286 | DELETE FROM t1 WHERE b=2; |
| 287 | SELECT b FROM t1 WHERE a=1 ORDER BY b; |
| 288 | } |
| 289 | } {} |
drh | 353f57e | 2000-08-02 12:26:28 +0000 | [diff] [blame] | 290 | do_test index-10.4 { |
| 291 | execsql { |
| 292 | DELETE FROM t1; |
| 293 | INSERT INTO t1 VALUES (1,1); |
| 294 | INSERT INTO t1 VALUES (1,2); |
| 295 | INSERT INTO t1 VALUES (1,3); |
| 296 | INSERT INTO t1 VALUES (1,4); |
| 297 | INSERT INTO t1 VALUES (1,5); |
| 298 | INSERT INTO t1 VALUES (1,6); |
| 299 | INSERT INTO t1 VALUES (1,7); |
| 300 | INSERT INTO t1 VALUES (1,8); |
| 301 | INSERT INTO t1 VALUES (1,9); |
| 302 | INSERT INTO t1 VALUES (2,0); |
| 303 | SELECT b FROM t1 WHERE a=1 ORDER BY b; |
| 304 | } |
| 305 | } {1 2 3 4 5 6 7 8 9} |
| 306 | do_test index-10.5 { |
| 307 | execsql { |
| 308 | DELETE FROM t1 WHERE b IN (2, 4, 6, 8); |
| 309 | SELECT b FROM t1 WHERE a=1 ORDER BY b; |
| 310 | } |
| 311 | } {1 3 5 7 9} |
| 312 | do_test index-10.6 { |
| 313 | execsql { |
| 314 | DELETE FROM t1 WHERE b>2; |
| 315 | SELECT b FROM t1 WHERE a=1 ORDER BY b; |
| 316 | } |
| 317 | } {1} |
| 318 | do_test index-10.7 { |
| 319 | execsql { |
| 320 | DELETE FROM t1 WHERE b=1; |
| 321 | SELECT b FROM t1 WHERE a=1 ORDER BY b; |
| 322 | } |
| 323 | } {} |
| 324 | do_test index-10.8 { |
| 325 | execsql { |
| 326 | SELECT b FROM t1 ORDER BY b; |
| 327 | } |
| 328 | } {0} |
drh | ed717fe | 2003-06-15 23:42:24 +0000 | [diff] [blame] | 329 | integrity_check index-10.9 |
drh | 353f57e | 2000-08-02 12:26:28 +0000 | [diff] [blame] | 330 | |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 331 | # Automatically create an index when we specify a primary key. |
| 332 | # |
| 333 | do_test index-11.1 { |
| 334 | execsql { |
| 335 | CREATE TABLE t3( |
| 336 | a text, |
| 337 | b int, |
| 338 | c float, |
| 339 | PRIMARY KEY(b) |
| 340 | ); |
| 341 | } |
| 342 | for {set i 1} {$i<=50} {incr i} { |
| 343 | execsql "INSERT INTO t3 VALUES('x${i}x',$i,0.$i)" |
| 344 | } |
drh | 487ab3c | 2001-11-08 00:45:21 +0000 | [diff] [blame] | 345 | set sqlite_search_count 0 |
| 346 | concat [execsql {SELECT c FROM t3 WHERE b==10}] $sqlite_search_count |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 347 | } {0.1 3} |
drh | ed717fe | 2003-06-15 23:42:24 +0000 | [diff] [blame] | 348 | integrity_check index-11.2 |
| 349 | |
drh | e840972 | 2000-06-08 16:54:40 +0000 | [diff] [blame] | 350 | |
drh | 7a7c739 | 2001-11-24 00:31:46 +0000 | [diff] [blame] | 351 | # Numeric strings should compare as if they were numbers. So even if the |
| 352 | # strings are not character-by-character the same, if they represent the |
| 353 | # same number they should compare equal to one another. Verify that this |
| 354 | # is true in indices. |
| 355 | # |
drh | ef4ac8f | 2004-06-19 00:16:31 +0000 | [diff] [blame] | 356 | # Updated for sqlite3 v3: SQLite will now store these values as numbers |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 357 | # (because the affinity of column a is NUMERIC) so the quirky |
| 358 | # representations are not retained. i.e. '+1.0' becomes '1'. |
drh | 7a7c739 | 2001-11-24 00:31:46 +0000 | [diff] [blame] | 359 | do_test index-12.1 { |
| 360 | execsql { |
danielk1977 | 93edea9 | 2004-05-16 22:55:28 +0000 | [diff] [blame] | 361 | CREATE TABLE t4(a NUM,b); |
drh | 7a7c739 | 2001-11-24 00:31:46 +0000 | [diff] [blame] | 362 | INSERT INTO t4 VALUES('0.0',1); |
| 363 | INSERT INTO t4 VALUES('0.00',2); |
| 364 | INSERT INTO t4 VALUES('abc',3); |
| 365 | INSERT INTO t4 VALUES('-1.0',4); |
| 366 | INSERT INTO t4 VALUES('+1.0',5); |
| 367 | INSERT INTO t4 VALUES('0',6); |
| 368 | INSERT INTO t4 VALUES('00000',7); |
| 369 | SELECT a FROM t4 ORDER BY b; |
| 370 | } |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 371 | } {0.0 0.0 abc -1.0 1.0 0 0} |
drh | 7a7c739 | 2001-11-24 00:31:46 +0000 | [diff] [blame] | 372 | do_test index-12.2 { |
| 373 | execsql { |
| 374 | SELECT a FROM t4 WHERE a==0 ORDER BY b |
| 375 | } |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 376 | } {0.0 0.0 0 0} |
drh | 7a7c739 | 2001-11-24 00:31:46 +0000 | [diff] [blame] | 377 | do_test index-12.3 { |
| 378 | execsql { |
| 379 | SELECT a FROM t4 WHERE a<0.5 ORDER BY b |
| 380 | } |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 381 | } {0.0 0.0 -1.0 0 0} |
drh | 7a7c739 | 2001-11-24 00:31:46 +0000 | [diff] [blame] | 382 | do_test index-12.4 { |
| 383 | execsql { |
| 384 | SELECT a FROM t4 WHERE a>-0.5 ORDER BY b |
| 385 | } |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 386 | } {0.0 0.0 abc 1.0 0 0} |
drh | 7a7c739 | 2001-11-24 00:31:46 +0000 | [diff] [blame] | 387 | do_test index-12.5 { |
| 388 | execsql { |
| 389 | CREATE INDEX t4i1 ON t4(a); |
| 390 | SELECT a FROM t4 WHERE a==0 ORDER BY b |
| 391 | } |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 392 | } {0.0 0.0 0 0} |
drh | 7a7c739 | 2001-11-24 00:31:46 +0000 | [diff] [blame] | 393 | do_test index-12.6 { |
| 394 | execsql { |
| 395 | SELECT a FROM t4 WHERE a<0.5 ORDER BY b |
| 396 | } |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 397 | } {0.0 0.0 -1.0 0 0} |
drh | 7a7c739 | 2001-11-24 00:31:46 +0000 | [diff] [blame] | 398 | do_test index-12.7 { |
| 399 | execsql { |
| 400 | SELECT a FROM t4 WHERE a>-0.5 ORDER BY b |
| 401 | } |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 402 | } {0.0 0.0 abc 1.0 0 0} |
drh | ed717fe | 2003-06-15 23:42:24 +0000 | [diff] [blame] | 403 | integrity_check index-12.8 |
drh | 7a7c739 | 2001-11-24 00:31:46 +0000 | [diff] [blame] | 404 | |
drh | 485b39b | 2002-07-13 03:11:52 +0000 | [diff] [blame] | 405 | # Make sure we cannot drop an automatically created index. |
| 406 | # |
| 407 | do_test index-13.1 { |
| 408 | execsql { |
| 409 | CREATE TABLE t5( |
| 410 | a int UNIQUE, |
| 411 | b float PRIMARY KEY, |
| 412 | c varchar(10), |
| 413 | UNIQUE(a,c) |
| 414 | ); |
| 415 | INSERT INTO t5 VALUES(1,2,3); |
| 416 | SELECT * FROM t5; |
| 417 | } |
| 418 | } {1 2 3} |
| 419 | do_test index-13.2 { |
| 420 | set ::idxlist [execsql { |
| 421 | SELECT name FROM sqlite_master WHERE type="index" AND tbl_name="t5"; |
| 422 | }] |
| 423 | llength $::idxlist |
| 424 | } {3} |
| 425 | for {set i 0} {$i<[llength $::idxlist]} {incr i} { |
| 426 | do_test index-13.3.$i { |
| 427 | catchsql " |
| 428 | DROP INDEX '[lindex $::idxlist $i]'; |
| 429 | " |
| 430 | } {1 {index associated with UNIQUE or PRIMARY KEY constraint cannot be dropped}} |
| 431 | } |
| 432 | do_test index-13.4 { |
| 433 | execsql { |
| 434 | INSERT INTO t5 VALUES('a','b','c'); |
| 435 | SELECT * FROM t5; |
| 436 | } |
| 437 | } {1 2 3 a b c} |
drh | ed717fe | 2003-06-15 23:42:24 +0000 | [diff] [blame] | 438 | integrity_check index-13.5 |
drh | 485b39b | 2002-07-13 03:11:52 +0000 | [diff] [blame] | 439 | |
drh | 491791a | 2002-07-18 00:34:09 +0000 | [diff] [blame] | 440 | # Check the sort order of data in an index. |
| 441 | # |
| 442 | do_test index-14.1 { |
| 443 | execsql { |
| 444 | CREATE TABLE t6(a,b,c); |
| 445 | CREATE INDEX t6i1 ON t6(a,b); |
| 446 | INSERT INTO t6 VALUES('','',1); |
| 447 | INSERT INTO t6 VALUES('',NULL,2); |
| 448 | INSERT INTO t6 VALUES(NULL,'',3); |
| 449 | INSERT INTO t6 VALUES('abc',123,4); |
| 450 | INSERT INTO t6 VALUES(123,'abc',5); |
| 451 | SELECT c FROM t6 ORDER BY a,b; |
| 452 | } |
| 453 | } {3 5 2 1 4} |
| 454 | do_test index-14.2 { |
| 455 | execsql { |
| 456 | SELECT c FROM t6 WHERE a=''; |
| 457 | } |
| 458 | } {2 1} |
| 459 | do_test index-14.3 { |
| 460 | execsql { |
| 461 | SELECT c FROM t6 WHERE b=''; |
| 462 | } |
| 463 | } {1 3} |
| 464 | do_test index-14.4 { |
| 465 | execsql { |
| 466 | SELECT c FROM t6 WHERE a>''; |
| 467 | } |
| 468 | } {4} |
| 469 | do_test index-14.5 { |
| 470 | execsql { |
| 471 | SELECT c FROM t6 WHERE a>=''; |
| 472 | } |
| 473 | } {2 1 4} |
| 474 | do_test index-14.6 { |
| 475 | execsql { |
| 476 | SELECT c FROM t6 WHERE a>123; |
| 477 | } |
| 478 | } {2 1 4} |
| 479 | do_test index-14.7 { |
| 480 | execsql { |
| 481 | SELECT c FROM t6 WHERE a>=123; |
| 482 | } |
| 483 | } {5 2 1 4} |
| 484 | do_test index-14.8 { |
| 485 | execsql { |
| 486 | SELECT c FROM t6 WHERE a<'abc'; |
| 487 | } |
drh | 562528c | 2003-09-27 00:41:27 +0000 | [diff] [blame] | 488 | } {5 2 1} |
drh | 491791a | 2002-07-18 00:34:09 +0000 | [diff] [blame] | 489 | do_test index-14.9 { |
| 490 | execsql { |
| 491 | SELECT c FROM t6 WHERE a<='abc'; |
| 492 | } |
drh | 562528c | 2003-09-27 00:41:27 +0000 | [diff] [blame] | 493 | } {5 2 1 4} |
drh | 491791a | 2002-07-18 00:34:09 +0000 | [diff] [blame] | 494 | do_test index-14.10 { |
| 495 | execsql { |
| 496 | SELECT c FROM t6 WHERE a<=''; |
| 497 | } |
drh | 562528c | 2003-09-27 00:41:27 +0000 | [diff] [blame] | 498 | } {5 2 1} |
drh | 491791a | 2002-07-18 00:34:09 +0000 | [diff] [blame] | 499 | do_test index-14.11 { |
| 500 | execsql { |
| 501 | SELECT c FROM t6 WHERE a<''; |
| 502 | } |
drh | 562528c | 2003-09-27 00:41:27 +0000 | [diff] [blame] | 503 | } {5} |
drh | ed717fe | 2003-06-15 23:42:24 +0000 | [diff] [blame] | 504 | integrity_check index-14.12 |
drh | 491791a | 2002-07-18 00:34:09 +0000 | [diff] [blame] | 505 | |
drh | bb07e9a | 2003-04-16 02:17:35 +0000 | [diff] [blame] | 506 | do_test index-15.1 { |
| 507 | execsql { |
| 508 | DELETE FROM t1; |
| 509 | SELECT * FROM t1; |
| 510 | } |
| 511 | } {} |
| 512 | do_test index-15.2 { |
| 513 | execsql { |
| 514 | INSERT INTO t1 VALUES('1.234e5',1); |
| 515 | INSERT INTO t1 VALUES('12.33e04',2); |
| 516 | INSERT INTO t1 VALUES('12.35E4',3); |
| 517 | INSERT INTO t1 VALUES('12.34e',4); |
| 518 | INSERT INTO t1 VALUES('12.32e+4',5); |
| 519 | INSERT INTO t1 VALUES('12.36E+04',6); |
| 520 | INSERT INTO t1 VALUES('12.36E+',7); |
| 521 | INSERT INTO t1 VALUES('+123.10000E+0003',8); |
| 522 | INSERT INTO t1 VALUES('+',9); |
| 523 | INSERT INTO t1 VALUES('+12347.E+02',10); |
| 524 | INSERT INTO t1 VALUES('+12347E+02',11); |
| 525 | SELECT b FROM t1 ORDER BY a; |
| 526 | } |
| 527 | } {8 5 2 1 3 6 11 9 10 4 7} |
drh | ed717fe | 2003-06-15 23:42:24 +0000 | [diff] [blame] | 528 | integrity_check index-15.1 |
drh | 491791a | 2002-07-18 00:34:09 +0000 | [diff] [blame] | 529 | |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 530 | # The following tests - index-16.* - test that when a table definition |
| 531 | # includes qualifications that specify the same constraint twice only a |
| 532 | # single index is generated to enforce the constraint. |
| 533 | # |
| 534 | # For example: "CREATE TABLE abc( x PRIMARY KEY, UNIQUE(x) );" |
| 535 | # |
| 536 | do_test index-16.1 { |
| 537 | execsql { |
| 538 | CREATE TABLE t7(c UNIQUE PRIMARY KEY); |
| 539 | SELECT count(*) FROM sqlite_master WHERE tbl_name = 't7' AND type = 'index'; |
| 540 | } |
| 541 | } {1} |
| 542 | do_test index-16.2 { |
| 543 | execsql { |
| 544 | DROP TABLE t7; |
| 545 | CREATE TABLE t7(c UNIQUE PRIMARY KEY); |
| 546 | SELECT count(*) FROM sqlite_master WHERE tbl_name = 't7' AND type = 'index'; |
| 547 | } |
| 548 | } {1} |
| 549 | do_test index-16.3 { |
| 550 | execsql { |
| 551 | DROP TABLE t7; |
| 552 | CREATE TABLE t7(c PRIMARY KEY, UNIQUE(c) ); |
| 553 | SELECT count(*) FROM sqlite_master WHERE tbl_name = 't7' AND type = 'index'; |
| 554 | } |
| 555 | } {1} |
| 556 | do_test index-16.4 { |
| 557 | execsql { |
| 558 | DROP TABLE t7; |
| 559 | CREATE TABLE t7(c, d , UNIQUE(c, d), PRIMARY KEY(c, d) ); |
| 560 | SELECT count(*) FROM sqlite_master WHERE tbl_name = 't7' AND type = 'index'; |
| 561 | } |
| 562 | } {1} |
| 563 | do_test index-16.5 { |
| 564 | execsql { |
| 565 | DROP TABLE t7; |
| 566 | CREATE TABLE t7(c, d , UNIQUE(c), PRIMARY KEY(c, d) ); |
| 567 | SELECT count(*) FROM sqlite_master WHERE tbl_name = 't7' AND type = 'index'; |
| 568 | } |
| 569 | } {2} |
| 570 | |
| 571 | # Test that automatically create indices are named correctly. The current |
| 572 | # convention is: "sqlite_autoindex_<table name>_<integer>" |
| 573 | # |
| 574 | # Then check that it is an error to try to drop any automtically created |
| 575 | # indices. |
| 576 | do_test index-17.1 { |
| 577 | execsql { |
| 578 | DROP TABLE t7; |
| 579 | CREATE TABLE t7(c, d UNIQUE, UNIQUE(c), PRIMARY KEY(c, d) ); |
| 580 | SELECT name FROM sqlite_master WHERE tbl_name = 't7' AND type = 'index'; |
| 581 | } |
| 582 | } {sqlite_autoindex_t7_1 sqlite_autoindex_t7_2 sqlite_autoindex_t7_3} |
| 583 | do_test index-17.2 { |
| 584 | catchsql { |
| 585 | DROP INDEX sqlite_autoindex_t7_1; |
| 586 | } |
| 587 | } {1 {index associated with UNIQUE or PRIMARY KEY constraint cannot be dropped}} |
| 588 | |
| 589 | # The following tests ensure that it is not possible to explicitly name |
| 590 | # a schema object with a name beginning with "sqlite_". Granted that is a |
| 591 | # little outside the focus of this test scripts, but this has got to be |
| 592 | # tested somewhere. |
| 593 | do_test index-18.1 { |
| 594 | catchsql { |
| 595 | CREATE TABLE sqlite_t1(a, b, c); |
| 596 | } |
| 597 | } {1 {object name reserved for internal use: sqlite_t1}} |
| 598 | do_test index-18.2 { |
| 599 | catchsql { |
| 600 | CREATE INDEX sqlite_i1 ON t7(c); |
| 601 | } |
| 602 | } {1 {object name reserved for internal use: sqlite_i1}} |
| 603 | do_test index-18.3 { |
| 604 | catchsql { |
| 605 | CREATE VIEW sqlite_v1 AS SELECT * FROM t7; |
| 606 | } |
| 607 | } {1 {object name reserved for internal use: sqlite_v1}} |
drh | 798da52 | 2004-11-04 04:42:28 +0000 | [diff] [blame] | 608 | ifcapable {trigger} { |
| 609 | do_test index-18.4 { |
| 610 | catchsql { |
| 611 | CREATE TRIGGER sqlite_tr1 BEFORE INSERT ON t7 BEGIN SELECT 1; END; |
| 612 | } |
| 613 | } {1 {object name reserved for internal use: sqlite_tr1}} |
| 614 | } |
danielk1977 | f736b77 | 2004-06-17 06:13:34 +0000 | [diff] [blame] | 615 | do_test index-18.5 { |
| 616 | execsql { |
| 617 | DROP TABLE t7; |
| 618 | } |
| 619 | } {} |
| 620 | |
| 621 | # These tests ensure that if multiple table definition constraints are |
| 622 | # implemented by a single indice, the correct ON CONFLICT policy applies. |
| 623 | do_test index-19.1 { |
| 624 | execsql { |
| 625 | CREATE TABLE t7(a UNIQUE PRIMARY KEY); |
| 626 | CREATE TABLE t8(a UNIQUE PRIMARY KEY ON CONFLICT ROLLBACK); |
| 627 | INSERT INTO t7 VALUES(1); |
| 628 | INSERT INTO t8 VALUES(1); |
| 629 | } |
| 630 | } {} |
| 631 | do_test index-19.2 { |
| 632 | catchsql { |
| 633 | BEGIN; |
| 634 | INSERT INTO t7 VALUES(1); |
| 635 | } |
| 636 | } {1 {column a is not unique}} |
| 637 | do_test index-19.3 { |
| 638 | catchsql { |
| 639 | BEGIN; |
| 640 | } |
| 641 | } {1 {cannot start a transaction within a transaction}} |
| 642 | do_test index-19.4 { |
| 643 | catchsql { |
| 644 | INSERT INTO t8 VALUES(1); |
| 645 | } |
| 646 | } {1 {column a is not unique}} |
| 647 | do_test index-19.5 { |
| 648 | catchsql { |
| 649 | BEGIN; |
| 650 | COMMIT; |
| 651 | } |
| 652 | } {0 {}} |
| 653 | do_test index-19.6 { |
| 654 | catchsql { |
| 655 | DROP TABLE t7; |
| 656 | DROP TABLE t8; |
| 657 | CREATE TABLE t7( |
| 658 | a PRIMARY KEY ON CONFLICT FAIL, |
| 659 | UNIQUE(a) ON CONFLICT IGNORE |
| 660 | ); |
| 661 | } |
| 662 | } {1 {conflicting ON CONFLICT clauses specified}} |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 663 | |
drh | e497f00 | 2004-11-07 13:01:49 +0000 | [diff] [blame^] | 664 | ifcapable {reindex} { |
| 665 | do_test index-19.7 { |
| 666 | execsql REINDEX |
| 667 | } {} |
| 668 | } |
| 669 | integrity_check index-19.8 |
| 670 | |
drh | 78d153e | 2004-07-20 00:52:44 +0000 | [diff] [blame] | 671 | # Drop index with a quoted name. Ticket #695. |
| 672 | # |
| 673 | do_test index-20.1 { |
| 674 | execsql { |
| 675 | CREATE INDEX "t6i2" ON t6(c); |
| 676 | DROP INDEX "t6i2"; |
| 677 | } |
| 678 | } {} |
| 679 | do_test index-20.2 { |
| 680 | execsql { |
| 681 | DROP INDEX "t6i1"; |
| 682 | } |
| 683 | } {} |
| 684 | |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 685 | |
drh | 78d153e | 2004-07-20 00:52:44 +0000 | [diff] [blame] | 686 | finish_test |