blob: 27c419cf613fd453f9771e4dd09d9515008c46aa [file] [log] [blame]
drhb19a2bc2001-09-16 00:13:26 +00001# 2001 September 15
drhb24fcbe2000-05-29 23:30:50 +00002#
drhb19a2bc2001-09-16 00:13:26 +00003# The author disclaims copyright to this source code. In place of
4# a legal notice, here is a blessing:
drhb24fcbe2000-05-29 23:30:50 +00005#
drhb19a2bc2001-09-16 00:13:26 +00006# 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.
drhb24fcbe2000-05-29 23:30:50 +00009#
10#***********************************************************************
11# This file implements regression tests for SQLite library. The
12# focus of this file is testing the CREATE INDEX statement.
13#
drh4d91a702006-01-04 15:54:36 +000014# $Id: index.test,v 1.40 2006/01/04 15:54:37 drh Exp $
drhb24fcbe2000-05-29 23:30:50 +000015
16set testdir [file dirname $argv0]
17source $testdir/tester.tcl
18
19# Create a basic index and verify it is added to sqlite_master
20#
drh1b6a71f2000-05-29 23:58:11 +000021do_test index-1.1 {
drhb24fcbe2000-05-29 23:30:50 +000022 execsql {CREATE TABLE test1(f1 int, f2 int, f3 int)}
23 execsql {CREATE INDEX index1 ON test1(f1)}
drh28037572000-08-02 13:47:41 +000024 execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
drhb24fcbe2000-05-29 23:30:50 +000025} {index1 test1}
drh1b6a71f2000-05-29 23:58:11 +000026do_test index-1.1b {
drhb24fcbe2000-05-29 23:30:50 +000027 execsql {SELECT name, sql, tbl_name, type FROM sqlite_master
28 WHERE name='index1'}
29} {index1 {CREATE INDEX index1 ON test1(f1)} test1 index}
drh1b6a71f2000-05-29 23:58:11 +000030do_test index-1.1c {
drhb24fcbe2000-05-29 23:30:50 +000031 db close
drhef4ac8f2004-06-19 00:16:31 +000032 sqlite3 db test.db
drhb24fcbe2000-05-29 23:30:50 +000033 execsql {SELECT name, sql, tbl_name, type FROM sqlite_master
34 WHERE name='index1'}
35} {index1 {CREATE INDEX index1 ON test1(f1)} test1 index}
drh1b6a71f2000-05-29 23:58:11 +000036do_test index-1.1d {
drhb24fcbe2000-05-29 23:30:50 +000037 db close
drhef4ac8f2004-06-19 00:16:31 +000038 sqlite3 db test.db
drh28037572000-08-02 13:47:41 +000039 execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
drhb24fcbe2000-05-29 23:30:50 +000040} {index1 test1}
41
42# Verify that the index dies with the table
43#
drh1b6a71f2000-05-29 23:58:11 +000044do_test index-1.2 {
drhb24fcbe2000-05-29 23:30:50 +000045 execsql {DROP TABLE test1}
drh28037572000-08-02 13:47:41 +000046 execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
drhb24fcbe2000-05-29 23:30:50 +000047} {}
48
49# Try adding an index to a table that does not exist
50#
drh1b6a71f2000-05-29 23:58:11 +000051do_test index-2.1 {
drhb24fcbe2000-05-29 23:30:50 +000052 set v [catch {execsql {CREATE INDEX index1 ON test1(f1)}} msg]
53 lappend v $msg
danielk197748dec7e2004-05-28 12:33:30 +000054} {1 {no such table: main.test1}}
drhb24fcbe2000-05-29 23:30:50 +000055
drh1ccde152000-06-17 13:12:39 +000056# Try adding an index on a column of a table where the table
57# exists but the column does not.
drhb24fcbe2000-05-29 23:30:50 +000058#
drh1b6a71f2000-05-29 23:58:11 +000059do_test index-2.1 {
drhb24fcbe2000-05-29 23:30:50 +000060 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
drh1ccde152000-06-17 13:12:39 +000063} {1 {table test1 has no column named f4}}
drhb24fcbe2000-05-29 23:30:50 +000064
drh1ccde152000-06-17 13:12:39 +000065# Try an index with some columns that match and others that do now.
drhb24fcbe2000-05-29 23:30:50 +000066#
drh1b6a71f2000-05-29 23:58:11 +000067do_test index-2.2 {
drhb24fcbe2000-05-29 23:30:50 +000068 set v [catch {execsql {CREATE INDEX index1 ON test1(f1, f2, f4, f3)}} msg]
69 execsql {DROP TABLE test1}
70 lappend v $msg
drh1ccde152000-06-17 13:12:39 +000071} {1 {table test1 has no column named f4}}
drhb24fcbe2000-05-29 23:30:50 +000072
73# Try creating a bunch of indices on the same table
74#
75set r {}
76for {set i 1} {$i<100} {incr i} {
drha9e99ae2002-08-13 23:02:57 +000077 lappend r [format index%02d $i]
drhb24fcbe2000-05-29 23:30:50 +000078}
drh1b6a71f2000-05-29 23:58:11 +000079do_test index-3.1 {
drhb24fcbe2000-05-29 23:30:50 +000080 execsql {CREATE TABLE test1(f1 int, f2 int, f3 int, f4 int, f5 int)}
81 for {set i 1} {$i<100} {incr i} {
drha9e99ae2002-08-13 23:02:57 +000082 set sql "CREATE INDEX [format index%02d $i] ON test1(f[expr {($i%5)+1}])"
drhb24fcbe2000-05-29 23:30:50 +000083 execsql $sql
84 }
85 execsql {SELECT name FROM sqlite_master
86 WHERE type='index' AND tbl_name='test1'
87 ORDER BY name}
88} $r
drhe497f002004-11-07 13:01:49 +000089integrity_check index-3.2.1
90ifcapable {reindex} {
91 do_test index-3.2.2 {
92 execsql REINDEX
93 } {}
94}
95integrity_check index-3.2.3
drhb24fcbe2000-05-29 23:30:50 +000096
drhb24fcbe2000-05-29 23:30:50 +000097
98# Verify that all the indices go away when we drop the table.
99#
drh1b6a71f2000-05-29 23:58:11 +0000100do_test index-3.3 {
drhb24fcbe2000-05-29 23:30:50 +0000101 execsql {DROP TABLE test1}
102 execsql {SELECT name FROM sqlite_master
103 WHERE type='index' AND tbl_name='test1'
104 ORDER BY name}
105} {}
drhb24fcbe2000-05-29 23:30:50 +0000106
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#
drh7020f652000-06-03 18:06:52 +0000111# Note that the index names "index9" and "indext" are chosen because
112# they both have the same hash.
113#
drh1b6a71f2000-05-29 23:58:11 +0000114do_test index-4.1 {
drhb24fcbe2000-05-29 23:30:50 +0000115 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 }
drh7020f652000-06-03 18:06:52 +0000119 execsql {CREATE INDEX index9 ON test1(cnt)}
120 execsql {CREATE INDEX indext ON test1(power)}
drh28037572000-08-02 13:47:41 +0000121 execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
drh7020f652000-06-03 18:06:52 +0000122} {index9 indext test1}
drh1b6a71f2000-05-29 23:58:11 +0000123do_test index-4.2 {
drhb24fcbe2000-05-29 23:30:50 +0000124 execsql {SELECT cnt FROM test1 WHERE power=4}
125} {2}
drh1b6a71f2000-05-29 23:58:11 +0000126do_test index-4.3 {
drhb24fcbe2000-05-29 23:30:50 +0000127 execsql {SELECT cnt FROM test1 WHERE power=1024}
128} {10}
drh1b6a71f2000-05-29 23:58:11 +0000129do_test index-4.4 {
drhb24fcbe2000-05-29 23:30:50 +0000130 execsql {SELECT power FROM test1 WHERE cnt=6}
131} {64}
drh1b6a71f2000-05-29 23:58:11 +0000132do_test index-4.5 {
drh7020f652000-06-03 18:06:52 +0000133 execsql {DROP INDEX indext}
134 execsql {SELECT power FROM test1 WHERE cnt=6}
135} {64}
136do_test index-4.6 {
137 execsql {SELECT cnt FROM test1 WHERE power=1024}
138} {10}
139do_test index-4.7 {
140 execsql {CREATE INDEX indext ON test1(cnt)}
141 execsql {SELECT power FROM test1 WHERE cnt=6}
142} {64}
143do_test index-4.8 {
144 execsql {SELECT cnt FROM test1 WHERE power=1024}
145} {10}
146do_test index-4.9 {
147 execsql {DROP INDEX index9}
148 execsql {SELECT power FROM test1 WHERE cnt=6}
149} {64}
150do_test index-4.10 {
151 execsql {SELECT cnt FROM test1 WHERE power=1024}
152} {10}
153do_test index-4.11 {
154 execsql {DROP INDEX indext}
155 execsql {SELECT power FROM test1 WHERE cnt=6}
156} {64}
157do_test index-4.12 {
158 execsql {SELECT cnt FROM test1 WHERE power=1024}
159} {10}
160do_test index-4.13 {
drhb24fcbe2000-05-29 23:30:50 +0000161 execsql {DROP TABLE test1}
drh28037572000-08-02 13:47:41 +0000162 execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
drhb24fcbe2000-05-29 23:30:50 +0000163} {}
drhed717fe2003-06-15 23:42:24 +0000164integrity_check index-4.14
drhb24fcbe2000-05-29 23:30:50 +0000165
166# Do not allow indices to be added to sqlite_master
167#
drh1b6a71f2000-05-29 23:58:11 +0000168do_test index-5.1 {
drhb24fcbe2000-05-29 23:30:50 +0000169 set v [catch {execsql {CREATE INDEX index1 ON sqlite_master(name)}} msg]
170 lappend v $msg
drh0be9df02003-03-30 00:19:49 +0000171} {1 {table sqlite_master may not be indexed}}
drh1b6a71f2000-05-29 23:58:11 +0000172do_test index-5.2 {
drh28037572000-08-02 13:47:41 +0000173 execsql {SELECT name FROM sqlite_master WHERE type!='meta'}
drhb24fcbe2000-05-29 23:30:50 +0000174} {}
175
176# Do not allow indices with duplicate names to be added
177#
drh1b6a71f2000-05-29 23:58:11 +0000178do_test index-6.1 {
drhb24fcbe2000-05-29 23:30:50 +0000179 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
drh1d37e282000-05-30 03:12:21 +0000184} {1 {index index1 already exists}}
drh1b6a71f2000-05-29 23:58:11 +0000185do_test index-6.1b {
drh28037572000-08-02 13:47:41 +0000186 execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
drhb24fcbe2000-05-29 23:30:50 +0000187} {index1 test1 test2}
drh4d91a702006-01-04 15:54:36 +0000188do_test index-6.1c {
189 catchsql {CREATE INDEX IF NOT EXISTS index1 ON test1(f1)}
190} {0 {}}
drh1b6a71f2000-05-29 23:58:11 +0000191do_test index-6.2 {
drhb24fcbe2000-05-29 23:30:50 +0000192 set v [catch {execsql {CREATE INDEX test1 ON test2(g1)}} msg]
193 lappend v $msg
drh1d37e282000-05-30 03:12:21 +0000194} {1 {there is already a table named test1}}
drh1b6a71f2000-05-29 23:58:11 +0000195do_test index-6.2b {
drh28037572000-08-02 13:47:41 +0000196 execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
drhb24fcbe2000-05-29 23:30:50 +0000197} {index1 test1 test2}
drh1b6a71f2000-05-29 23:58:11 +0000198do_test index-6.3 {
drhb24fcbe2000-05-29 23:30:50 +0000199 execsql {DROP TABLE test1}
200 execsql {DROP TABLE test2}
drh28037572000-08-02 13:47:41 +0000201 execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
drhb24fcbe2000-05-29 23:30:50 +0000202} {}
drhf5bf0a72001-11-23 00:24:12 +0000203do_test index-6.4 {
204 execsql {
205 CREATE TABLE test1(a,b);
206 CREATE INDEX index1 ON test1(a);
207 CREATE INDEX index2 ON test1(b);
208 CREATE INDEX index3 ON test1(a,b);
209 DROP TABLE test1;
210 SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name;
211 }
212} {}
drhed717fe2003-06-15 23:42:24 +0000213integrity_check index-6.5
214
drhb24fcbe2000-05-29 23:30:50 +0000215
216# Create a primary key
217#
drh1b6a71f2000-05-29 23:58:11 +0000218do_test index-7.1 {
drhb24fcbe2000-05-29 23:30:50 +0000219 execsql {CREATE TABLE test1(f1 int, f2 int primary key)}
220 for {set i 1} {$i<20} {incr i} {
221 execsql "INSERT INTO test1 VALUES($i,[expr {int(pow(2,$i))}])"
222 }
drh767c2002000-10-19 14:10:08 +0000223 execsql {SELECT count(*) FROM test1}
224} {19}
drh1b6a71f2000-05-29 23:58:11 +0000225do_test index-7.2 {
drhb24fcbe2000-05-29 23:30:50 +0000226 execsql {SELECT f1 FROM test1 WHERE f2=65536}
227} {16}
drh1b6a71f2000-05-29 23:58:11 +0000228do_test index-7.3 {
drhadbca9c2001-09-27 15:11:53 +0000229 execsql {
230 SELECT name FROM sqlite_master
231 WHERE type='index' AND tbl_name='test1'
232 }
danielk1977d8123362004-06-12 09:25:12 +0000233} {sqlite_autoindex_test1_1}
drh1b6a71f2000-05-29 23:58:11 +0000234do_test index-7.4 {
235 execsql {DROP table test1}
drh28037572000-08-02 13:47:41 +0000236 execsql {SELECT name FROM sqlite_master WHERE type!='meta'}
drh1b6a71f2000-05-29 23:58:11 +0000237} {}
drhed717fe2003-06-15 23:42:24 +0000238integrity_check index-7.5
drh1b6a71f2000-05-29 23:58:11 +0000239
drh7020f652000-06-03 18:06:52 +0000240# Make sure we cannot drop a non-existant index.
drh1b6a71f2000-05-29 23:58:11 +0000241#
242do_test index-8.1 {
243 set v [catch {execsql {DROP INDEX index1}} msg]
244 lappend v $msg
drh1d37e282000-05-30 03:12:21 +0000245} {1 {no such index: index1}}
drh1b6a71f2000-05-29 23:58:11 +0000246
drh7020f652000-06-03 18:06:52 +0000247# Make sure we don't actually create an index when the EXPLAIN keyword
248# is used.
249#
250do_test index-9.1 {
251 execsql {CREATE TABLE tab1(a int)}
drh6bf89572004-11-03 16:27:01 +0000252 ifcapable {explain} {
253 execsql {EXPLAIN CREATE INDEX idx1 ON tab1(a)}
254 }
drh7020f652000-06-03 18:06:52 +0000255 execsql {SELECT name FROM sqlite_master WHERE tbl_name='tab1'}
256} {tab1}
257do_test index-9.2 {
258 execsql {CREATE INDEX idx1 ON tab1(a)}
259 execsql {SELECT name FROM sqlite_master WHERE tbl_name='tab1' ORDER BY name}
260} {idx1 tab1}
drhed717fe2003-06-15 23:42:24 +0000261integrity_check index-9.3
drhb24fcbe2000-05-29 23:30:50 +0000262
drhe8409722000-06-08 16:54:40 +0000263# Allow more than one entry with the same key.
264#
265do_test index-10.0 {
266 execsql {
267 CREATE TABLE t1(a int, b int);
268 CREATE INDEX i1 ON t1(a);
269 INSERT INTO t1 VALUES(1,2);
270 INSERT INTO t1 VALUES(2,4);
271 INSERT INTO t1 VALUES(3,8);
272 INSERT INTO t1 VALUES(1,12);
273 SELECT b FROM t1 WHERE a=1 ORDER BY b;
274 }
275} {2 12}
276do_test index-10.1 {
277 execsql {
278 SELECT b FROM t1 WHERE a=2 ORDER BY b;
279 }
280} {4}
281do_test index-10.2 {
282 execsql {
283 DELETE FROM t1 WHERE b=12;
284 SELECT b FROM t1 WHERE a=1 ORDER BY b;
285 }
286} {2}
drh353f57e2000-08-02 12:26:28 +0000287do_test index-10.3 {
drhe8409722000-06-08 16:54:40 +0000288 execsql {
289 DELETE FROM t1 WHERE b=2;
290 SELECT b FROM t1 WHERE a=1 ORDER BY b;
291 }
292} {}
drh353f57e2000-08-02 12:26:28 +0000293do_test index-10.4 {
294 execsql {
295 DELETE FROM t1;
296 INSERT INTO t1 VALUES (1,1);
297 INSERT INTO t1 VALUES (1,2);
298 INSERT INTO t1 VALUES (1,3);
299 INSERT INTO t1 VALUES (1,4);
300 INSERT INTO t1 VALUES (1,5);
301 INSERT INTO t1 VALUES (1,6);
302 INSERT INTO t1 VALUES (1,7);
303 INSERT INTO t1 VALUES (1,8);
304 INSERT INTO t1 VALUES (1,9);
305 INSERT INTO t1 VALUES (2,0);
306 SELECT b FROM t1 WHERE a=1 ORDER BY b;
307 }
308} {1 2 3 4 5 6 7 8 9}
309do_test index-10.5 {
danielk19773e8c37e2005-01-21 03:12:14 +0000310 ifcapable subquery {
311 execsql { DELETE FROM t1 WHERE b IN (2, 4, 6, 8); }
312 } else {
313 execsql { DELETE FROM t1 WHERE b = 2 OR b = 4 OR b = 6 OR b = 8; }
314 }
drh353f57e2000-08-02 12:26:28 +0000315 execsql {
drh353f57e2000-08-02 12:26:28 +0000316 SELECT b FROM t1 WHERE a=1 ORDER BY b;
317 }
318} {1 3 5 7 9}
319do_test index-10.6 {
320 execsql {
321 DELETE FROM t1 WHERE b>2;
322 SELECT b FROM t1 WHERE a=1 ORDER BY b;
323 }
324} {1}
325do_test index-10.7 {
326 execsql {
327 DELETE FROM t1 WHERE b=1;
328 SELECT b FROM t1 WHERE a=1 ORDER BY b;
329 }
330} {}
331do_test index-10.8 {
332 execsql {
333 SELECT b FROM t1 ORDER BY b;
334 }
335} {0}
drhed717fe2003-06-15 23:42:24 +0000336integrity_check index-10.9
drh353f57e2000-08-02 12:26:28 +0000337
drhc4a3c772001-04-04 11:48:57 +0000338# Automatically create an index when we specify a primary key.
339#
340do_test index-11.1 {
341 execsql {
342 CREATE TABLE t3(
343 a text,
344 b int,
345 c float,
346 PRIMARY KEY(b)
347 );
348 }
349 for {set i 1} {$i<=50} {incr i} {
350 execsql "INSERT INTO t3 VALUES('x${i}x',$i,0.$i)"
351 }
drh487ab3c2001-11-08 00:45:21 +0000352 set sqlite_search_count 0
353 concat [execsql {SELECT c FROM t3 WHERE b==10}] $sqlite_search_count
danielk19773d1bfea2004-05-14 11:00:53 +0000354} {0.1 3}
drhed717fe2003-06-15 23:42:24 +0000355integrity_check index-11.2
356
drhe8409722000-06-08 16:54:40 +0000357
drh7a7c7392001-11-24 00:31:46 +0000358# Numeric strings should compare as if they were numbers. So even if the
359# strings are not character-by-character the same, if they represent the
360# same number they should compare equal to one another. Verify that this
361# is true in indices.
362#
drhef4ac8f2004-06-19 00:16:31 +0000363# Updated for sqlite3 v3: SQLite will now store these values as numbers
danielk19773d1bfea2004-05-14 11:00:53 +0000364# (because the affinity of column a is NUMERIC) so the quirky
365# representations are not retained. i.e. '+1.0' becomes '1'.
drh7a7c7392001-11-24 00:31:46 +0000366do_test index-12.1 {
367 execsql {
danielk197793edea92004-05-16 22:55:28 +0000368 CREATE TABLE t4(a NUM,b);
drh7a7c7392001-11-24 00:31:46 +0000369 INSERT INTO t4 VALUES('0.0',1);
370 INSERT INTO t4 VALUES('0.00',2);
371 INSERT INTO t4 VALUES('abc',3);
372 INSERT INTO t4 VALUES('-1.0',4);
373 INSERT INTO t4 VALUES('+1.0',5);
374 INSERT INTO t4 VALUES('0',6);
375 INSERT INTO t4 VALUES('00000',7);
376 SELECT a FROM t4 ORDER BY b;
377 }
drh8df447f2005-11-01 15:48:24 +0000378} {0 0 abc -1 1 0 0}
drh7a7c7392001-11-24 00:31:46 +0000379do_test index-12.2 {
380 execsql {
381 SELECT a FROM t4 WHERE a==0 ORDER BY b
382 }
drh8df447f2005-11-01 15:48:24 +0000383} {0 0 0 0}
drh7a7c7392001-11-24 00:31:46 +0000384do_test index-12.3 {
385 execsql {
386 SELECT a FROM t4 WHERE a<0.5 ORDER BY b
387 }
drh8df447f2005-11-01 15:48:24 +0000388} {0 0 -1 0 0}
drh7a7c7392001-11-24 00:31:46 +0000389do_test index-12.4 {
390 execsql {
391 SELECT a FROM t4 WHERE a>-0.5 ORDER BY b
392 }
drh8df447f2005-11-01 15:48:24 +0000393} {0 0 abc 1 0 0}
drh7a7c7392001-11-24 00:31:46 +0000394do_test index-12.5 {
395 execsql {
396 CREATE INDEX t4i1 ON t4(a);
397 SELECT a FROM t4 WHERE a==0 ORDER BY b
398 }
drh8df447f2005-11-01 15:48:24 +0000399} {0 0 0 0}
drh7a7c7392001-11-24 00:31:46 +0000400do_test index-12.6 {
401 execsql {
402 SELECT a FROM t4 WHERE a<0.5 ORDER BY b
403 }
drh8df447f2005-11-01 15:48:24 +0000404} {0 0 -1 0 0}
drh7a7c7392001-11-24 00:31:46 +0000405do_test index-12.7 {
406 execsql {
407 SELECT a FROM t4 WHERE a>-0.5 ORDER BY b
408 }
drh8df447f2005-11-01 15:48:24 +0000409} {0 0 abc 1 0 0}
drhed717fe2003-06-15 23:42:24 +0000410integrity_check index-12.8
drh7a7c7392001-11-24 00:31:46 +0000411
drh485b39b2002-07-13 03:11:52 +0000412# Make sure we cannot drop an automatically created index.
413#
414do_test index-13.1 {
415 execsql {
416 CREATE TABLE t5(
417 a int UNIQUE,
418 b float PRIMARY KEY,
419 c varchar(10),
420 UNIQUE(a,c)
421 );
422 INSERT INTO t5 VALUES(1,2,3);
423 SELECT * FROM t5;
424 }
drh8a512562005-11-14 22:29:05 +0000425} {1 2.0 3}
drh485b39b2002-07-13 03:11:52 +0000426do_test index-13.2 {
427 set ::idxlist [execsql {
428 SELECT name FROM sqlite_master WHERE type="index" AND tbl_name="t5";
429 }]
430 llength $::idxlist
431} {3}
432for {set i 0} {$i<[llength $::idxlist]} {incr i} {
433 do_test index-13.3.$i {
434 catchsql "
435 DROP INDEX '[lindex $::idxlist $i]';
436 "
437 } {1 {index associated with UNIQUE or PRIMARY KEY constraint cannot be dropped}}
438}
439do_test index-13.4 {
440 execsql {
441 INSERT INTO t5 VALUES('a','b','c');
442 SELECT * FROM t5;
443 }
drh8a512562005-11-14 22:29:05 +0000444} {1 2.0 3 a b c}
drhed717fe2003-06-15 23:42:24 +0000445integrity_check index-13.5
drh485b39b2002-07-13 03:11:52 +0000446
drh491791a2002-07-18 00:34:09 +0000447# Check the sort order of data in an index.
448#
449do_test index-14.1 {
450 execsql {
451 CREATE TABLE t6(a,b,c);
452 CREATE INDEX t6i1 ON t6(a,b);
453 INSERT INTO t6 VALUES('','',1);
454 INSERT INTO t6 VALUES('',NULL,2);
455 INSERT INTO t6 VALUES(NULL,'',3);
456 INSERT INTO t6 VALUES('abc',123,4);
457 INSERT INTO t6 VALUES(123,'abc',5);
458 SELECT c FROM t6 ORDER BY a,b;
459 }
460} {3 5 2 1 4}
461do_test index-14.2 {
462 execsql {
463 SELECT c FROM t6 WHERE a='';
464 }
465} {2 1}
466do_test index-14.3 {
467 execsql {
468 SELECT c FROM t6 WHERE b='';
469 }
470} {1 3}
471do_test index-14.4 {
472 execsql {
473 SELECT c FROM t6 WHERE a>'';
474 }
475} {4}
476do_test index-14.5 {
477 execsql {
478 SELECT c FROM t6 WHERE a>='';
479 }
480} {2 1 4}
481do_test index-14.6 {
482 execsql {
483 SELECT c FROM t6 WHERE a>123;
484 }
485} {2 1 4}
486do_test index-14.7 {
487 execsql {
488 SELECT c FROM t6 WHERE a>=123;
489 }
490} {5 2 1 4}
491do_test index-14.8 {
492 execsql {
493 SELECT c FROM t6 WHERE a<'abc';
494 }
drh562528c2003-09-27 00:41:27 +0000495} {5 2 1}
drh491791a2002-07-18 00:34:09 +0000496do_test index-14.9 {
497 execsql {
498 SELECT c FROM t6 WHERE a<='abc';
499 }
drh562528c2003-09-27 00:41:27 +0000500} {5 2 1 4}
drh491791a2002-07-18 00:34:09 +0000501do_test index-14.10 {
502 execsql {
503 SELECT c FROM t6 WHERE a<='';
504 }
drh562528c2003-09-27 00:41:27 +0000505} {5 2 1}
drh491791a2002-07-18 00:34:09 +0000506do_test index-14.11 {
507 execsql {
508 SELECT c FROM t6 WHERE a<'';
509 }
drh562528c2003-09-27 00:41:27 +0000510} {5}
drhed717fe2003-06-15 23:42:24 +0000511integrity_check index-14.12
drh491791a2002-07-18 00:34:09 +0000512
drhbb07e9a2003-04-16 02:17:35 +0000513do_test index-15.1 {
514 execsql {
515 DELETE FROM t1;
516 SELECT * FROM t1;
517 }
518} {}
519do_test index-15.2 {
520 execsql {
521 INSERT INTO t1 VALUES('1.234e5',1);
522 INSERT INTO t1 VALUES('12.33e04',2);
523 INSERT INTO t1 VALUES('12.35E4',3);
524 INSERT INTO t1 VALUES('12.34e',4);
525 INSERT INTO t1 VALUES('12.32e+4',5);
526 INSERT INTO t1 VALUES('12.36E+04',6);
527 INSERT INTO t1 VALUES('12.36E+',7);
528 INSERT INTO t1 VALUES('+123.10000E+0003',8);
529 INSERT INTO t1 VALUES('+',9);
530 INSERT INTO t1 VALUES('+12347.E+02',10);
531 INSERT INTO t1 VALUES('+12347E+02',11);
532 SELECT b FROM t1 ORDER BY a;
533 }
534} {8 5 2 1 3 6 11 9 10 4 7}
drhed717fe2003-06-15 23:42:24 +0000535integrity_check index-15.1
drh491791a2002-07-18 00:34:09 +0000536
danielk1977d8123362004-06-12 09:25:12 +0000537# The following tests - index-16.* - test that when a table definition
538# includes qualifications that specify the same constraint twice only a
539# single index is generated to enforce the constraint.
540#
541# For example: "CREATE TABLE abc( x PRIMARY KEY, UNIQUE(x) );"
542#
543do_test index-16.1 {
544 execsql {
545 CREATE TABLE t7(c UNIQUE PRIMARY KEY);
546 SELECT count(*) FROM sqlite_master WHERE tbl_name = 't7' AND type = 'index';
547 }
548} {1}
549do_test index-16.2 {
550 execsql {
551 DROP TABLE t7;
552 CREATE TABLE t7(c UNIQUE PRIMARY KEY);
553 SELECT count(*) FROM sqlite_master WHERE tbl_name = 't7' AND type = 'index';
554 }
555} {1}
556do_test index-16.3 {
557 execsql {
558 DROP TABLE t7;
559 CREATE TABLE t7(c PRIMARY KEY, UNIQUE(c) );
560 SELECT count(*) FROM sqlite_master WHERE tbl_name = 't7' AND type = 'index';
561 }
562} {1}
563do_test index-16.4 {
564 execsql {
565 DROP TABLE t7;
566 CREATE TABLE t7(c, d , UNIQUE(c, d), PRIMARY KEY(c, d) );
567 SELECT count(*) FROM sqlite_master WHERE tbl_name = 't7' AND type = 'index';
568 }
569} {1}
570do_test index-16.5 {
571 execsql {
572 DROP TABLE t7;
573 CREATE TABLE t7(c, d , UNIQUE(c), PRIMARY KEY(c, d) );
574 SELECT count(*) FROM sqlite_master WHERE tbl_name = 't7' AND type = 'index';
575 }
576} {2}
577
578# Test that automatically create indices are named correctly. The current
579# convention is: "sqlite_autoindex_<table name>_<integer>"
580#
581# Then check that it is an error to try to drop any automtically created
582# indices.
583do_test index-17.1 {
584 execsql {
585 DROP TABLE t7;
586 CREATE TABLE t7(c, d UNIQUE, UNIQUE(c), PRIMARY KEY(c, d) );
587 SELECT name FROM sqlite_master WHERE tbl_name = 't7' AND type = 'index';
588 }
589} {sqlite_autoindex_t7_1 sqlite_autoindex_t7_2 sqlite_autoindex_t7_3}
590do_test index-17.2 {
591 catchsql {
592 DROP INDEX sqlite_autoindex_t7_1;
593 }
594} {1 {index associated with UNIQUE or PRIMARY KEY constraint cannot be dropped}}
drh4d91a702006-01-04 15:54:36 +0000595do_test index-17.3 {
596 catchsql {
597 DROP INDEX IF EXISTS sqlite_autoindex_t7_1;
598 }
599} {1 {index associated with UNIQUE or PRIMARY KEY constraint cannot be dropped}}
600do_test index-17.4 {
601 catchsql {
602 DROP INDEX IF EXISTS no_such_index;
603 }
604} {0 {}}
605
danielk1977d8123362004-06-12 09:25:12 +0000606
607# The following tests ensure that it is not possible to explicitly name
608# a schema object with a name beginning with "sqlite_". Granted that is a
609# little outside the focus of this test scripts, but this has got to be
610# tested somewhere.
611do_test index-18.1 {
612 catchsql {
613 CREATE TABLE sqlite_t1(a, b, c);
614 }
615} {1 {object name reserved for internal use: sqlite_t1}}
616do_test index-18.2 {
617 catchsql {
618 CREATE INDEX sqlite_i1 ON t7(c);
619 }
620} {1 {object name reserved for internal use: sqlite_i1}}
danielk19770fa8ddb2004-11-22 08:43:32 +0000621ifcapable view {
danielk1977d8123362004-06-12 09:25:12 +0000622do_test index-18.3 {
623 catchsql {
624 CREATE VIEW sqlite_v1 AS SELECT * FROM t7;
625 }
626} {1 {object name reserved for internal use: sqlite_v1}}
danielk19770fa8ddb2004-11-22 08:43:32 +0000627} ;# ifcapable view
drh798da522004-11-04 04:42:28 +0000628ifcapable {trigger} {
629 do_test index-18.4 {
630 catchsql {
631 CREATE TRIGGER sqlite_tr1 BEFORE INSERT ON t7 BEGIN SELECT 1; END;
632 }
633 } {1 {object name reserved for internal use: sqlite_tr1}}
634}
danielk1977f736b772004-06-17 06:13:34 +0000635do_test index-18.5 {
636 execsql {
637 DROP TABLE t7;
638 }
639} {}
640
641# These tests ensure that if multiple table definition constraints are
642# implemented by a single indice, the correct ON CONFLICT policy applies.
643do_test index-19.1 {
644 execsql {
645 CREATE TABLE t7(a UNIQUE PRIMARY KEY);
646 CREATE TABLE t8(a UNIQUE PRIMARY KEY ON CONFLICT ROLLBACK);
647 INSERT INTO t7 VALUES(1);
648 INSERT INTO t8 VALUES(1);
649 }
650} {}
651do_test index-19.2 {
652 catchsql {
653 BEGIN;
654 INSERT INTO t7 VALUES(1);
655 }
656} {1 {column a is not unique}}
657do_test index-19.3 {
658 catchsql {
659 BEGIN;
660 }
661} {1 {cannot start a transaction within a transaction}}
662do_test index-19.4 {
663 catchsql {
664 INSERT INTO t8 VALUES(1);
665 }
666} {1 {column a is not unique}}
667do_test index-19.5 {
668 catchsql {
669 BEGIN;
670 COMMIT;
671 }
672} {0 {}}
673do_test index-19.6 {
674 catchsql {
675 DROP TABLE t7;
676 DROP TABLE t8;
677 CREATE TABLE t7(
678 a PRIMARY KEY ON CONFLICT FAIL,
679 UNIQUE(a) ON CONFLICT IGNORE
680 );
681 }
682} {1 {conflicting ON CONFLICT clauses specified}}
danielk1977d8123362004-06-12 09:25:12 +0000683
drhe497f002004-11-07 13:01:49 +0000684ifcapable {reindex} {
685 do_test index-19.7 {
686 execsql REINDEX
687 } {}
688}
689integrity_check index-19.8
690
drh78d153e2004-07-20 00:52:44 +0000691# Drop index with a quoted name. Ticket #695.
692#
693do_test index-20.1 {
694 execsql {
695 CREATE INDEX "t6i2" ON t6(c);
696 DROP INDEX "t6i2";
697 }
698} {}
699do_test index-20.2 {
700 execsql {
701 DROP INDEX "t6i1";
702 }
703} {}
704
danielk1977d8123362004-06-12 09:25:12 +0000705
drh78d153e2004-07-20 00:52:44 +0000706finish_test