blob: 680affcbb091040a5b1929f2664893837f961a47 [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#
drh6bf89572004-11-03 16:27:01 +000014# $Id: index.test,v 1.33 2004/11/03 16:27:02 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
89
drhb24fcbe2000-05-29 23:30:50 +000090
91# Verify that all the indices go away when we drop the table.
92#
drh1b6a71f2000-05-29 23:58:11 +000093do_test index-3.3 {
drhb24fcbe2000-05-29 23:30:50 +000094 execsql {DROP TABLE test1}
95 execsql {SELECT name FROM sqlite_master
96 WHERE type='index' AND tbl_name='test1'
97 ORDER BY name}
98} {}
drhb24fcbe2000-05-29 23:30:50 +000099
100# Create a table and insert values into that table. Then create
101# an index on that table. Verify that we can select values
102# from the table correctly using the index.
103#
drh7020f652000-06-03 18:06:52 +0000104# Note that the index names "index9" and "indext" are chosen because
105# they both have the same hash.
106#
drh1b6a71f2000-05-29 23:58:11 +0000107do_test index-4.1 {
drhb24fcbe2000-05-29 23:30:50 +0000108 execsql {CREATE TABLE test1(cnt int, power int)}
109 for {set i 1} {$i<20} {incr i} {
110 execsql "INSERT INTO test1 VALUES($i,[expr {int(pow(2,$i))}])"
111 }
drh7020f652000-06-03 18:06:52 +0000112 execsql {CREATE INDEX index9 ON test1(cnt)}
113 execsql {CREATE INDEX indext ON test1(power)}
drh28037572000-08-02 13:47:41 +0000114 execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
drh7020f652000-06-03 18:06:52 +0000115} {index9 indext test1}
drh1b6a71f2000-05-29 23:58:11 +0000116do_test index-4.2 {
drhb24fcbe2000-05-29 23:30:50 +0000117 execsql {SELECT cnt FROM test1 WHERE power=4}
118} {2}
drh1b6a71f2000-05-29 23:58:11 +0000119do_test index-4.3 {
drhb24fcbe2000-05-29 23:30:50 +0000120 execsql {SELECT cnt FROM test1 WHERE power=1024}
121} {10}
drh1b6a71f2000-05-29 23:58:11 +0000122do_test index-4.4 {
drhb24fcbe2000-05-29 23:30:50 +0000123 execsql {SELECT power FROM test1 WHERE cnt=6}
124} {64}
drh1b6a71f2000-05-29 23:58:11 +0000125do_test index-4.5 {
drh7020f652000-06-03 18:06:52 +0000126 execsql {DROP INDEX indext}
127 execsql {SELECT power FROM test1 WHERE cnt=6}
128} {64}
129do_test index-4.6 {
130 execsql {SELECT cnt FROM test1 WHERE power=1024}
131} {10}
132do_test index-4.7 {
133 execsql {CREATE INDEX indext ON test1(cnt)}
134 execsql {SELECT power FROM test1 WHERE cnt=6}
135} {64}
136do_test index-4.8 {
137 execsql {SELECT cnt FROM test1 WHERE power=1024}
138} {10}
139do_test index-4.9 {
140 execsql {DROP INDEX index9}
141 execsql {SELECT power FROM test1 WHERE cnt=6}
142} {64}
143do_test index-4.10 {
144 execsql {SELECT cnt FROM test1 WHERE power=1024}
145} {10}
146do_test index-4.11 {
147 execsql {DROP INDEX indext}
148 execsql {SELECT power FROM test1 WHERE cnt=6}
149} {64}
150do_test index-4.12 {
151 execsql {SELECT cnt FROM test1 WHERE power=1024}
152} {10}
153do_test index-4.13 {
drhb24fcbe2000-05-29 23:30:50 +0000154 execsql {DROP TABLE test1}
drh28037572000-08-02 13:47:41 +0000155 execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
drhb24fcbe2000-05-29 23:30:50 +0000156} {}
drhed717fe2003-06-15 23:42:24 +0000157integrity_check index-4.14
drhb24fcbe2000-05-29 23:30:50 +0000158
159# Do not allow indices to be added to sqlite_master
160#
drh1b6a71f2000-05-29 23:58:11 +0000161do_test index-5.1 {
drhb24fcbe2000-05-29 23:30:50 +0000162 set v [catch {execsql {CREATE INDEX index1 ON sqlite_master(name)}} msg]
163 lappend v $msg
drh0be9df02003-03-30 00:19:49 +0000164} {1 {table sqlite_master may not be indexed}}
drh1b6a71f2000-05-29 23:58:11 +0000165do_test index-5.2 {
drh28037572000-08-02 13:47:41 +0000166 execsql {SELECT name FROM sqlite_master WHERE type!='meta'}
drhb24fcbe2000-05-29 23:30:50 +0000167} {}
168
169# Do not allow indices with duplicate names to be added
170#
drh1b6a71f2000-05-29 23:58:11 +0000171do_test index-6.1 {
drhb24fcbe2000-05-29 23:30:50 +0000172 execsql {CREATE TABLE test1(f1 int, f2 int)}
173 execsql {CREATE TABLE test2(g1 real, g2 real)}
174 execsql {CREATE INDEX index1 ON test1(f1)}
175 set v [catch {execsql {CREATE INDEX index1 ON test2(g1)}} msg]
176 lappend v $msg
drh1d37e282000-05-30 03:12:21 +0000177} {1 {index index1 already exists}}
drh1b6a71f2000-05-29 23:58:11 +0000178do_test index-6.1b {
drh28037572000-08-02 13:47:41 +0000179 execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
drhb24fcbe2000-05-29 23:30:50 +0000180} {index1 test1 test2}
drh1b6a71f2000-05-29 23:58:11 +0000181do_test index-6.2 {
drhb24fcbe2000-05-29 23:30:50 +0000182 set v [catch {execsql {CREATE INDEX test1 ON test2(g1)}} msg]
183 lappend v $msg
drh1d37e282000-05-30 03:12:21 +0000184} {1 {there is already a table named test1}}
drh1b6a71f2000-05-29 23:58:11 +0000185do_test index-6.2b {
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}
drh1b6a71f2000-05-29 23:58:11 +0000188do_test index-6.3 {
drhb24fcbe2000-05-29 23:30:50 +0000189 execsql {DROP TABLE test1}
190 execsql {DROP TABLE test2}
drh28037572000-08-02 13:47:41 +0000191 execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
drhb24fcbe2000-05-29 23:30:50 +0000192} {}
drhf5bf0a72001-11-23 00:24:12 +0000193do_test index-6.4 {
194 execsql {
195 CREATE TABLE test1(a,b);
196 CREATE INDEX index1 ON test1(a);
197 CREATE INDEX index2 ON test1(b);
198 CREATE INDEX index3 ON test1(a,b);
199 DROP TABLE test1;
200 SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name;
201 }
202} {}
drhed717fe2003-06-15 23:42:24 +0000203integrity_check index-6.5
204
drhb24fcbe2000-05-29 23:30:50 +0000205
206# Create a primary key
207#
drh1b6a71f2000-05-29 23:58:11 +0000208do_test index-7.1 {
drhb24fcbe2000-05-29 23:30:50 +0000209 execsql {CREATE TABLE test1(f1 int, f2 int primary key)}
210 for {set i 1} {$i<20} {incr i} {
211 execsql "INSERT INTO test1 VALUES($i,[expr {int(pow(2,$i))}])"
212 }
drh767c2002000-10-19 14:10:08 +0000213 execsql {SELECT count(*) FROM test1}
214} {19}
drh1b6a71f2000-05-29 23:58:11 +0000215do_test index-7.2 {
drhb24fcbe2000-05-29 23:30:50 +0000216 execsql {SELECT f1 FROM test1 WHERE f2=65536}
217} {16}
drh1b6a71f2000-05-29 23:58:11 +0000218do_test index-7.3 {
drhadbca9c2001-09-27 15:11:53 +0000219 execsql {
220 SELECT name FROM sqlite_master
221 WHERE type='index' AND tbl_name='test1'
222 }
danielk1977d8123362004-06-12 09:25:12 +0000223} {sqlite_autoindex_test1_1}
drh1b6a71f2000-05-29 23:58:11 +0000224do_test index-7.4 {
225 execsql {DROP table test1}
drh28037572000-08-02 13:47:41 +0000226 execsql {SELECT name FROM sqlite_master WHERE type!='meta'}
drh1b6a71f2000-05-29 23:58:11 +0000227} {}
drhed717fe2003-06-15 23:42:24 +0000228integrity_check index-7.5
drh1b6a71f2000-05-29 23:58:11 +0000229
drh7020f652000-06-03 18:06:52 +0000230# Make sure we cannot drop a non-existant index.
drh1b6a71f2000-05-29 23:58:11 +0000231#
232do_test index-8.1 {
233 set v [catch {execsql {DROP INDEX index1}} msg]
234 lappend v $msg
drh1d37e282000-05-30 03:12:21 +0000235} {1 {no such index: index1}}
drh1b6a71f2000-05-29 23:58:11 +0000236
drh7020f652000-06-03 18:06:52 +0000237# Make sure we don't actually create an index when the EXPLAIN keyword
238# is used.
239#
240do_test index-9.1 {
241 execsql {CREATE TABLE tab1(a int)}
drh6bf89572004-11-03 16:27:01 +0000242 ifcapable {explain} {
243 execsql {EXPLAIN CREATE INDEX idx1 ON tab1(a)}
244 }
drh7020f652000-06-03 18:06:52 +0000245 execsql {SELECT name FROM sqlite_master WHERE tbl_name='tab1'}
246} {tab1}
247do_test index-9.2 {
248 execsql {CREATE INDEX idx1 ON tab1(a)}
249 execsql {SELECT name FROM sqlite_master WHERE tbl_name='tab1' ORDER BY name}
250} {idx1 tab1}
drhed717fe2003-06-15 23:42:24 +0000251integrity_check index-9.3
drhb24fcbe2000-05-29 23:30:50 +0000252
drhe8409722000-06-08 16:54:40 +0000253# Allow more than one entry with the same key.
254#
255do_test index-10.0 {
256 execsql {
257 CREATE TABLE t1(a int, b int);
258 CREATE INDEX i1 ON t1(a);
259 INSERT INTO t1 VALUES(1,2);
260 INSERT INTO t1 VALUES(2,4);
261 INSERT INTO t1 VALUES(3,8);
262 INSERT INTO t1 VALUES(1,12);
263 SELECT b FROM t1 WHERE a=1 ORDER BY b;
264 }
265} {2 12}
266do_test index-10.1 {
267 execsql {
268 SELECT b FROM t1 WHERE a=2 ORDER BY b;
269 }
270} {4}
271do_test index-10.2 {
272 execsql {
273 DELETE FROM t1 WHERE b=12;
274 SELECT b FROM t1 WHERE a=1 ORDER BY b;
275 }
276} {2}
drh353f57e2000-08-02 12:26:28 +0000277do_test index-10.3 {
drhe8409722000-06-08 16:54:40 +0000278 execsql {
279 DELETE FROM t1 WHERE b=2;
280 SELECT b FROM t1 WHERE a=1 ORDER BY b;
281 }
282} {}
drh353f57e2000-08-02 12:26:28 +0000283do_test index-10.4 {
284 execsql {
285 DELETE FROM t1;
286 INSERT INTO t1 VALUES (1,1);
287 INSERT INTO t1 VALUES (1,2);
288 INSERT INTO t1 VALUES (1,3);
289 INSERT INTO t1 VALUES (1,4);
290 INSERT INTO t1 VALUES (1,5);
291 INSERT INTO t1 VALUES (1,6);
292 INSERT INTO t1 VALUES (1,7);
293 INSERT INTO t1 VALUES (1,8);
294 INSERT INTO t1 VALUES (1,9);
295 INSERT INTO t1 VALUES (2,0);
296 SELECT b FROM t1 WHERE a=1 ORDER BY b;
297 }
298} {1 2 3 4 5 6 7 8 9}
299do_test index-10.5 {
300 execsql {
301 DELETE FROM t1 WHERE b IN (2, 4, 6, 8);
302 SELECT b FROM t1 WHERE a=1 ORDER BY b;
303 }
304} {1 3 5 7 9}
305do_test index-10.6 {
306 execsql {
307 DELETE FROM t1 WHERE b>2;
308 SELECT b FROM t1 WHERE a=1 ORDER BY b;
309 }
310} {1}
311do_test index-10.7 {
312 execsql {
313 DELETE FROM t1 WHERE b=1;
314 SELECT b FROM t1 WHERE a=1 ORDER BY b;
315 }
316} {}
317do_test index-10.8 {
318 execsql {
319 SELECT b FROM t1 ORDER BY b;
320 }
321} {0}
drhed717fe2003-06-15 23:42:24 +0000322integrity_check index-10.9
drh353f57e2000-08-02 12:26:28 +0000323
drhc4a3c772001-04-04 11:48:57 +0000324# Automatically create an index when we specify a primary key.
325#
326do_test index-11.1 {
327 execsql {
328 CREATE TABLE t3(
329 a text,
330 b int,
331 c float,
332 PRIMARY KEY(b)
333 );
334 }
335 for {set i 1} {$i<=50} {incr i} {
336 execsql "INSERT INTO t3 VALUES('x${i}x',$i,0.$i)"
337 }
drh487ab3c2001-11-08 00:45:21 +0000338 set sqlite_search_count 0
339 concat [execsql {SELECT c FROM t3 WHERE b==10}] $sqlite_search_count
danielk19773d1bfea2004-05-14 11:00:53 +0000340} {0.1 3}
drhed717fe2003-06-15 23:42:24 +0000341integrity_check index-11.2
342
drhe8409722000-06-08 16:54:40 +0000343
drh7a7c7392001-11-24 00:31:46 +0000344# Numeric strings should compare as if they were numbers. So even if the
345# strings are not character-by-character the same, if they represent the
346# same number they should compare equal to one another. Verify that this
347# is true in indices.
348#
drhef4ac8f2004-06-19 00:16:31 +0000349# Updated for sqlite3 v3: SQLite will now store these values as numbers
danielk19773d1bfea2004-05-14 11:00:53 +0000350# (because the affinity of column a is NUMERIC) so the quirky
351# representations are not retained. i.e. '+1.0' becomes '1'.
drh7a7c7392001-11-24 00:31:46 +0000352do_test index-12.1 {
353 execsql {
danielk197793edea92004-05-16 22:55:28 +0000354 CREATE TABLE t4(a NUM,b);
drh7a7c7392001-11-24 00:31:46 +0000355 INSERT INTO t4 VALUES('0.0',1);
356 INSERT INTO t4 VALUES('0.00',2);
357 INSERT INTO t4 VALUES('abc',3);
358 INSERT INTO t4 VALUES('-1.0',4);
359 INSERT INTO t4 VALUES('+1.0',5);
360 INSERT INTO t4 VALUES('0',6);
361 INSERT INTO t4 VALUES('00000',7);
362 SELECT a FROM t4 ORDER BY b;
363 }
drh92febd92004-08-20 18:34:20 +0000364} {0.0 0.0 abc -1.0 1.0 0 0}
drh7a7c7392001-11-24 00:31:46 +0000365do_test index-12.2 {
366 execsql {
367 SELECT a FROM t4 WHERE a==0 ORDER BY b
368 }
drh92febd92004-08-20 18:34:20 +0000369} {0.0 0.0 0 0}
drh7a7c7392001-11-24 00:31:46 +0000370do_test index-12.3 {
371 execsql {
372 SELECT a FROM t4 WHERE a<0.5 ORDER BY b
373 }
drh92febd92004-08-20 18:34:20 +0000374} {0.0 0.0 -1.0 0 0}
drh7a7c7392001-11-24 00:31:46 +0000375do_test index-12.4 {
376 execsql {
377 SELECT a FROM t4 WHERE a>-0.5 ORDER BY b
378 }
drh92febd92004-08-20 18:34:20 +0000379} {0.0 0.0 abc 1.0 0 0}
drh7a7c7392001-11-24 00:31:46 +0000380do_test index-12.5 {
381 execsql {
382 CREATE INDEX t4i1 ON t4(a);
383 SELECT a FROM t4 WHERE a==0 ORDER BY b
384 }
drh92febd92004-08-20 18:34:20 +0000385} {0.0 0.0 0 0}
drh7a7c7392001-11-24 00:31:46 +0000386do_test index-12.6 {
387 execsql {
388 SELECT a FROM t4 WHERE a<0.5 ORDER BY b
389 }
drh92febd92004-08-20 18:34:20 +0000390} {0.0 0.0 -1.0 0 0}
drh7a7c7392001-11-24 00:31:46 +0000391do_test index-12.7 {
392 execsql {
393 SELECT a FROM t4 WHERE a>-0.5 ORDER BY b
394 }
drh92febd92004-08-20 18:34:20 +0000395} {0.0 0.0 abc 1.0 0 0}
drhed717fe2003-06-15 23:42:24 +0000396integrity_check index-12.8
drh7a7c7392001-11-24 00:31:46 +0000397
drh485b39b2002-07-13 03:11:52 +0000398# Make sure we cannot drop an automatically created index.
399#
400do_test index-13.1 {
401 execsql {
402 CREATE TABLE t5(
403 a int UNIQUE,
404 b float PRIMARY KEY,
405 c varchar(10),
406 UNIQUE(a,c)
407 );
408 INSERT INTO t5 VALUES(1,2,3);
409 SELECT * FROM t5;
410 }
411} {1 2 3}
412do_test index-13.2 {
413 set ::idxlist [execsql {
414 SELECT name FROM sqlite_master WHERE type="index" AND tbl_name="t5";
415 }]
416 llength $::idxlist
417} {3}
418for {set i 0} {$i<[llength $::idxlist]} {incr i} {
419 do_test index-13.3.$i {
420 catchsql "
421 DROP INDEX '[lindex $::idxlist $i]';
422 "
423 } {1 {index associated with UNIQUE or PRIMARY KEY constraint cannot be dropped}}
424}
425do_test index-13.4 {
426 execsql {
427 INSERT INTO t5 VALUES('a','b','c');
428 SELECT * FROM t5;
429 }
430} {1 2 3 a b c}
drhed717fe2003-06-15 23:42:24 +0000431integrity_check index-13.5
drh485b39b2002-07-13 03:11:52 +0000432
drh491791a2002-07-18 00:34:09 +0000433# Check the sort order of data in an index.
434#
435do_test index-14.1 {
436 execsql {
437 CREATE TABLE t6(a,b,c);
438 CREATE INDEX t6i1 ON t6(a,b);
439 INSERT INTO t6 VALUES('','',1);
440 INSERT INTO t6 VALUES('',NULL,2);
441 INSERT INTO t6 VALUES(NULL,'',3);
442 INSERT INTO t6 VALUES('abc',123,4);
443 INSERT INTO t6 VALUES(123,'abc',5);
444 SELECT c FROM t6 ORDER BY a,b;
445 }
446} {3 5 2 1 4}
447do_test index-14.2 {
448 execsql {
449 SELECT c FROM t6 WHERE a='';
450 }
451} {2 1}
452do_test index-14.3 {
453 execsql {
454 SELECT c FROM t6 WHERE b='';
455 }
456} {1 3}
457do_test index-14.4 {
458 execsql {
459 SELECT c FROM t6 WHERE a>'';
460 }
461} {4}
462do_test index-14.5 {
463 execsql {
464 SELECT c FROM t6 WHERE a>='';
465 }
466} {2 1 4}
467do_test index-14.6 {
468 execsql {
469 SELECT c FROM t6 WHERE a>123;
470 }
471} {2 1 4}
472do_test index-14.7 {
473 execsql {
474 SELECT c FROM t6 WHERE a>=123;
475 }
476} {5 2 1 4}
477do_test index-14.8 {
478 execsql {
479 SELECT c FROM t6 WHERE a<'abc';
480 }
drh562528c2003-09-27 00:41:27 +0000481} {5 2 1}
drh491791a2002-07-18 00:34:09 +0000482do_test index-14.9 {
483 execsql {
484 SELECT c FROM t6 WHERE a<='abc';
485 }
drh562528c2003-09-27 00:41:27 +0000486} {5 2 1 4}
drh491791a2002-07-18 00:34:09 +0000487do_test index-14.10 {
488 execsql {
489 SELECT c FROM t6 WHERE a<='';
490 }
drh562528c2003-09-27 00:41:27 +0000491} {5 2 1}
drh491791a2002-07-18 00:34:09 +0000492do_test index-14.11 {
493 execsql {
494 SELECT c FROM t6 WHERE a<'';
495 }
drh562528c2003-09-27 00:41:27 +0000496} {5}
drhed717fe2003-06-15 23:42:24 +0000497integrity_check index-14.12
drh491791a2002-07-18 00:34:09 +0000498
drhbb07e9a2003-04-16 02:17:35 +0000499do_test index-15.1 {
500 execsql {
501 DELETE FROM t1;
502 SELECT * FROM t1;
503 }
504} {}
505do_test index-15.2 {
506 execsql {
507 INSERT INTO t1 VALUES('1.234e5',1);
508 INSERT INTO t1 VALUES('12.33e04',2);
509 INSERT INTO t1 VALUES('12.35E4',3);
510 INSERT INTO t1 VALUES('12.34e',4);
511 INSERT INTO t1 VALUES('12.32e+4',5);
512 INSERT INTO t1 VALUES('12.36E+04',6);
513 INSERT INTO t1 VALUES('12.36E+',7);
514 INSERT INTO t1 VALUES('+123.10000E+0003',8);
515 INSERT INTO t1 VALUES('+',9);
516 INSERT INTO t1 VALUES('+12347.E+02',10);
517 INSERT INTO t1 VALUES('+12347E+02',11);
518 SELECT b FROM t1 ORDER BY a;
519 }
520} {8 5 2 1 3 6 11 9 10 4 7}
drhed717fe2003-06-15 23:42:24 +0000521integrity_check index-15.1
drh491791a2002-07-18 00:34:09 +0000522
danielk1977d8123362004-06-12 09:25:12 +0000523# The following tests - index-16.* - test that when a table definition
524# includes qualifications that specify the same constraint twice only a
525# single index is generated to enforce the constraint.
526#
527# For example: "CREATE TABLE abc( x PRIMARY KEY, UNIQUE(x) );"
528#
529do_test index-16.1 {
530 execsql {
531 CREATE TABLE t7(c UNIQUE PRIMARY KEY);
532 SELECT count(*) FROM sqlite_master WHERE tbl_name = 't7' AND type = 'index';
533 }
534} {1}
535do_test index-16.2 {
536 execsql {
537 DROP TABLE t7;
538 CREATE TABLE t7(c UNIQUE PRIMARY KEY);
539 SELECT count(*) FROM sqlite_master WHERE tbl_name = 't7' AND type = 'index';
540 }
541} {1}
542do_test index-16.3 {
543 execsql {
544 DROP TABLE t7;
545 CREATE TABLE t7(c PRIMARY KEY, UNIQUE(c) );
546 SELECT count(*) FROM sqlite_master WHERE tbl_name = 't7' AND type = 'index';
547 }
548} {1}
549do_test index-16.4 {
550 execsql {
551 DROP TABLE t7;
552 CREATE TABLE t7(c, d , UNIQUE(c, d), PRIMARY KEY(c, d) );
553 SELECT count(*) FROM sqlite_master WHERE tbl_name = 't7' AND type = 'index';
554 }
555} {1}
556do_test index-16.5 {
557 execsql {
558 DROP TABLE t7;
559 CREATE TABLE t7(c, d , UNIQUE(c), PRIMARY KEY(c, d) );
560 SELECT count(*) FROM sqlite_master WHERE tbl_name = 't7' AND type = 'index';
561 }
562} {2}
563
564# Test that automatically create indices are named correctly. The current
565# convention is: "sqlite_autoindex_<table name>_<integer>"
566#
567# Then check that it is an error to try to drop any automtically created
568# indices.
569do_test index-17.1 {
570 execsql {
571 DROP TABLE t7;
572 CREATE TABLE t7(c, d UNIQUE, UNIQUE(c), PRIMARY KEY(c, d) );
573 SELECT name FROM sqlite_master WHERE tbl_name = 't7' AND type = 'index';
574 }
575} {sqlite_autoindex_t7_1 sqlite_autoindex_t7_2 sqlite_autoindex_t7_3}
576do_test index-17.2 {
577 catchsql {
578 DROP INDEX sqlite_autoindex_t7_1;
579 }
580} {1 {index associated with UNIQUE or PRIMARY KEY constraint cannot be dropped}}
581
582# The following tests ensure that it is not possible to explicitly name
583# a schema object with a name beginning with "sqlite_". Granted that is a
584# little outside the focus of this test scripts, but this has got to be
585# tested somewhere.
586do_test index-18.1 {
587 catchsql {
588 CREATE TABLE sqlite_t1(a, b, c);
589 }
590} {1 {object name reserved for internal use: sqlite_t1}}
591do_test index-18.2 {
592 catchsql {
593 CREATE INDEX sqlite_i1 ON t7(c);
594 }
595} {1 {object name reserved for internal use: sqlite_i1}}
596do_test index-18.3 {
597 catchsql {
598 CREATE VIEW sqlite_v1 AS SELECT * FROM t7;
599 }
600} {1 {object name reserved for internal use: sqlite_v1}}
601do_test index-18.4 {
602 catchsql {
603 CREATE TRIGGER sqlite_tr1 BEFORE INSERT ON t7 BEGIN SELECT 1; END;
604 }
605} {1 {object name reserved for internal use: sqlite_tr1}}
danielk1977f736b772004-06-17 06:13:34 +0000606do_test index-18.5 {
607 execsql {
608 DROP TABLE t7;
609 }
610} {}
611
612# These tests ensure that if multiple table definition constraints are
613# implemented by a single indice, the correct ON CONFLICT policy applies.
614do_test index-19.1 {
615 execsql {
616 CREATE TABLE t7(a UNIQUE PRIMARY KEY);
617 CREATE TABLE t8(a UNIQUE PRIMARY KEY ON CONFLICT ROLLBACK);
618 INSERT INTO t7 VALUES(1);
619 INSERT INTO t8 VALUES(1);
620 }
621} {}
622do_test index-19.2 {
623 catchsql {
624 BEGIN;
625 INSERT INTO t7 VALUES(1);
626 }
627} {1 {column a is not unique}}
628do_test index-19.3 {
629 catchsql {
630 BEGIN;
631 }
632} {1 {cannot start a transaction within a transaction}}
633do_test index-19.4 {
634 catchsql {
635 INSERT INTO t8 VALUES(1);
636 }
637} {1 {column a is not unique}}
638do_test index-19.5 {
639 catchsql {
640 BEGIN;
641 COMMIT;
642 }
643} {0 {}}
644do_test index-19.6 {
645 catchsql {
646 DROP TABLE t7;
647 DROP TABLE t8;
648 CREATE TABLE t7(
649 a PRIMARY KEY ON CONFLICT FAIL,
650 UNIQUE(a) ON CONFLICT IGNORE
651 );
652 }
653} {1 {conflicting ON CONFLICT clauses specified}}
danielk1977d8123362004-06-12 09:25:12 +0000654
drh78d153e2004-07-20 00:52:44 +0000655# Drop index with a quoted name. Ticket #695.
656#
657do_test index-20.1 {
658 execsql {
659 CREATE INDEX "t6i2" ON t6(c);
660 DROP INDEX "t6i2";
661 }
662} {}
663do_test index-20.2 {
664 execsql {
665 DROP INDEX "t6i1";
666 }
667} {}
668
danielk1977d8123362004-06-12 09:25:12 +0000669
drh78d153e2004-07-20 00:52:44 +0000670finish_test