blob: 86a28afd9510a86292e6f42aec64ada086e84a8c [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#
drh8df447f2005-11-01 15:48:24 +000014# $Id: index.test,v 1.38 2005/11/01 15:48:25 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}
drh1b6a71f2000-05-29 23:58:11 +0000188do_test index-6.2 {
drhb24fcbe2000-05-29 23:30:50 +0000189 set v [catch {execsql {CREATE INDEX test1 ON test2(g1)}} msg]
190 lappend v $msg
drh1d37e282000-05-30 03:12:21 +0000191} {1 {there is already a table named test1}}
drh1b6a71f2000-05-29 23:58:11 +0000192do_test index-6.2b {
drh28037572000-08-02 13:47:41 +0000193 execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
drhb24fcbe2000-05-29 23:30:50 +0000194} {index1 test1 test2}
drh1b6a71f2000-05-29 23:58:11 +0000195do_test index-6.3 {
drhb24fcbe2000-05-29 23:30:50 +0000196 execsql {DROP TABLE test1}
197 execsql {DROP TABLE test2}
drh28037572000-08-02 13:47:41 +0000198 execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
drhb24fcbe2000-05-29 23:30:50 +0000199} {}
drhf5bf0a72001-11-23 00:24:12 +0000200do_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} {}
drhed717fe2003-06-15 23:42:24 +0000210integrity_check index-6.5
211
drhb24fcbe2000-05-29 23:30:50 +0000212
213# Create a primary key
214#
drh1b6a71f2000-05-29 23:58:11 +0000215do_test index-7.1 {
drhb24fcbe2000-05-29 23:30:50 +0000216 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 }
drh767c2002000-10-19 14:10:08 +0000220 execsql {SELECT count(*) FROM test1}
221} {19}
drh1b6a71f2000-05-29 23:58:11 +0000222do_test index-7.2 {
drhb24fcbe2000-05-29 23:30:50 +0000223 execsql {SELECT f1 FROM test1 WHERE f2=65536}
224} {16}
drh1b6a71f2000-05-29 23:58:11 +0000225do_test index-7.3 {
drhadbca9c2001-09-27 15:11:53 +0000226 execsql {
227 SELECT name FROM sqlite_master
228 WHERE type='index' AND tbl_name='test1'
229 }
danielk1977d8123362004-06-12 09:25:12 +0000230} {sqlite_autoindex_test1_1}
drh1b6a71f2000-05-29 23:58:11 +0000231do_test index-7.4 {
232 execsql {DROP table test1}
drh28037572000-08-02 13:47:41 +0000233 execsql {SELECT name FROM sqlite_master WHERE type!='meta'}
drh1b6a71f2000-05-29 23:58:11 +0000234} {}
drhed717fe2003-06-15 23:42:24 +0000235integrity_check index-7.5
drh1b6a71f2000-05-29 23:58:11 +0000236
drh7020f652000-06-03 18:06:52 +0000237# Make sure we cannot drop a non-existant index.
drh1b6a71f2000-05-29 23:58:11 +0000238#
239do_test index-8.1 {
240 set v [catch {execsql {DROP INDEX index1}} msg]
241 lappend v $msg
drh1d37e282000-05-30 03:12:21 +0000242} {1 {no such index: index1}}
drh1b6a71f2000-05-29 23:58:11 +0000243
drh7020f652000-06-03 18:06:52 +0000244# Make sure we don't actually create an index when the EXPLAIN keyword
245# is used.
246#
247do_test index-9.1 {
248 execsql {CREATE TABLE tab1(a int)}
drh6bf89572004-11-03 16:27:01 +0000249 ifcapable {explain} {
250 execsql {EXPLAIN CREATE INDEX idx1 ON tab1(a)}
251 }
drh7020f652000-06-03 18:06:52 +0000252 execsql {SELECT name FROM sqlite_master WHERE tbl_name='tab1'}
253} {tab1}
254do_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}
drhed717fe2003-06-15 23:42:24 +0000258integrity_check index-9.3
drhb24fcbe2000-05-29 23:30:50 +0000259
drhe8409722000-06-08 16:54:40 +0000260# Allow more than one entry with the same key.
261#
262do_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}
273do_test index-10.1 {
274 execsql {
275 SELECT b FROM t1 WHERE a=2 ORDER BY b;
276 }
277} {4}
278do_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}
drh353f57e2000-08-02 12:26:28 +0000284do_test index-10.3 {
drhe8409722000-06-08 16:54:40 +0000285 execsql {
286 DELETE FROM t1 WHERE b=2;
287 SELECT b FROM t1 WHERE a=1 ORDER BY b;
288 }
289} {}
drh353f57e2000-08-02 12:26:28 +0000290do_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}
306do_test index-10.5 {
danielk19773e8c37e2005-01-21 03:12:14 +0000307 ifcapable subquery {
308 execsql { DELETE FROM t1 WHERE b IN (2, 4, 6, 8); }
309 } else {
310 execsql { DELETE FROM t1 WHERE b = 2 OR b = 4 OR b = 6 OR b = 8; }
311 }
drh353f57e2000-08-02 12:26:28 +0000312 execsql {
drh353f57e2000-08-02 12:26:28 +0000313 SELECT b FROM t1 WHERE a=1 ORDER BY b;
314 }
315} {1 3 5 7 9}
316do_test index-10.6 {
317 execsql {
318 DELETE FROM t1 WHERE b>2;
319 SELECT b FROM t1 WHERE a=1 ORDER BY b;
320 }
321} {1}
322do_test index-10.7 {
323 execsql {
324 DELETE FROM t1 WHERE b=1;
325 SELECT b FROM t1 WHERE a=1 ORDER BY b;
326 }
327} {}
328do_test index-10.8 {
329 execsql {
330 SELECT b FROM t1 ORDER BY b;
331 }
332} {0}
drhed717fe2003-06-15 23:42:24 +0000333integrity_check index-10.9
drh353f57e2000-08-02 12:26:28 +0000334
drhc4a3c772001-04-04 11:48:57 +0000335# Automatically create an index when we specify a primary key.
336#
337do_test index-11.1 {
338 execsql {
339 CREATE TABLE t3(
340 a text,
341 b int,
342 c float,
343 PRIMARY KEY(b)
344 );
345 }
346 for {set i 1} {$i<=50} {incr i} {
347 execsql "INSERT INTO t3 VALUES('x${i}x',$i,0.$i)"
348 }
drh487ab3c2001-11-08 00:45:21 +0000349 set sqlite_search_count 0
350 concat [execsql {SELECT c FROM t3 WHERE b==10}] $sqlite_search_count
danielk19773d1bfea2004-05-14 11:00:53 +0000351} {0.1 3}
drhed717fe2003-06-15 23:42:24 +0000352integrity_check index-11.2
353
drhe8409722000-06-08 16:54:40 +0000354
drh7a7c7392001-11-24 00:31:46 +0000355# Numeric strings should compare as if they were numbers. So even if the
356# strings are not character-by-character the same, if they represent the
357# same number they should compare equal to one another. Verify that this
358# is true in indices.
359#
drhef4ac8f2004-06-19 00:16:31 +0000360# Updated for sqlite3 v3: SQLite will now store these values as numbers
danielk19773d1bfea2004-05-14 11:00:53 +0000361# (because the affinity of column a is NUMERIC) so the quirky
362# representations are not retained. i.e. '+1.0' becomes '1'.
drh7a7c7392001-11-24 00:31:46 +0000363do_test index-12.1 {
364 execsql {
danielk197793edea92004-05-16 22:55:28 +0000365 CREATE TABLE t4(a NUM,b);
drh7a7c7392001-11-24 00:31:46 +0000366 INSERT INTO t4 VALUES('0.0',1);
367 INSERT INTO t4 VALUES('0.00',2);
368 INSERT INTO t4 VALUES('abc',3);
369 INSERT INTO t4 VALUES('-1.0',4);
370 INSERT INTO t4 VALUES('+1.0',5);
371 INSERT INTO t4 VALUES('0',6);
372 INSERT INTO t4 VALUES('00000',7);
373 SELECT a FROM t4 ORDER BY b;
374 }
drh8df447f2005-11-01 15:48:24 +0000375} {0 0 abc -1 1 0 0}
drh7a7c7392001-11-24 00:31:46 +0000376do_test index-12.2 {
377 execsql {
378 SELECT a FROM t4 WHERE a==0 ORDER BY b
379 }
drh8df447f2005-11-01 15:48:24 +0000380} {0 0 0 0}
drh7a7c7392001-11-24 00:31:46 +0000381do_test index-12.3 {
382 execsql {
383 SELECT a FROM t4 WHERE a<0.5 ORDER BY b
384 }
drh8df447f2005-11-01 15:48:24 +0000385} {0 0 -1 0 0}
drh7a7c7392001-11-24 00:31:46 +0000386do_test index-12.4 {
387 execsql {
388 SELECT a FROM t4 WHERE a>-0.5 ORDER BY b
389 }
drh8df447f2005-11-01 15:48:24 +0000390} {0 0 abc 1 0 0}
drh7a7c7392001-11-24 00:31:46 +0000391do_test index-12.5 {
392 execsql {
393 CREATE INDEX t4i1 ON t4(a);
394 SELECT a FROM t4 WHERE a==0 ORDER BY b
395 }
drh8df447f2005-11-01 15:48:24 +0000396} {0 0 0 0}
drh7a7c7392001-11-24 00:31:46 +0000397do_test index-12.6 {
398 execsql {
399 SELECT a FROM t4 WHERE a<0.5 ORDER BY b
400 }
drh8df447f2005-11-01 15:48:24 +0000401} {0 0 -1 0 0}
drh7a7c7392001-11-24 00:31:46 +0000402do_test index-12.7 {
403 execsql {
404 SELECT a FROM t4 WHERE a>-0.5 ORDER BY b
405 }
drh8df447f2005-11-01 15:48:24 +0000406} {0 0 abc 1 0 0}
drhed717fe2003-06-15 23:42:24 +0000407integrity_check index-12.8
drh7a7c7392001-11-24 00:31:46 +0000408
drh485b39b2002-07-13 03:11:52 +0000409# Make sure we cannot drop an automatically created index.
410#
411do_test index-13.1 {
412 execsql {
413 CREATE TABLE t5(
414 a int UNIQUE,
415 b float PRIMARY KEY,
416 c varchar(10),
417 UNIQUE(a,c)
418 );
419 INSERT INTO t5 VALUES(1,2,3);
420 SELECT * FROM t5;
421 }
422} {1 2 3}
423do_test index-13.2 {
424 set ::idxlist [execsql {
425 SELECT name FROM sqlite_master WHERE type="index" AND tbl_name="t5";
426 }]
427 llength $::idxlist
428} {3}
429for {set i 0} {$i<[llength $::idxlist]} {incr i} {
430 do_test index-13.3.$i {
431 catchsql "
432 DROP INDEX '[lindex $::idxlist $i]';
433 "
434 } {1 {index associated with UNIQUE or PRIMARY KEY constraint cannot be dropped}}
435}
436do_test index-13.4 {
437 execsql {
438 INSERT INTO t5 VALUES('a','b','c');
439 SELECT * FROM t5;
440 }
441} {1 2 3 a b c}
drhed717fe2003-06-15 23:42:24 +0000442integrity_check index-13.5
drh485b39b2002-07-13 03:11:52 +0000443
drh491791a2002-07-18 00:34:09 +0000444# Check the sort order of data in an index.
445#
446do_test index-14.1 {
447 execsql {
448 CREATE TABLE t6(a,b,c);
449 CREATE INDEX t6i1 ON t6(a,b);
450 INSERT INTO t6 VALUES('','',1);
451 INSERT INTO t6 VALUES('',NULL,2);
452 INSERT INTO t6 VALUES(NULL,'',3);
453 INSERT INTO t6 VALUES('abc',123,4);
454 INSERT INTO t6 VALUES(123,'abc',5);
455 SELECT c FROM t6 ORDER BY a,b;
456 }
457} {3 5 2 1 4}
458do_test index-14.2 {
459 execsql {
460 SELECT c FROM t6 WHERE a='';
461 }
462} {2 1}
463do_test index-14.3 {
464 execsql {
465 SELECT c FROM t6 WHERE b='';
466 }
467} {1 3}
468do_test index-14.4 {
469 execsql {
470 SELECT c FROM t6 WHERE a>'';
471 }
472} {4}
473do_test index-14.5 {
474 execsql {
475 SELECT c FROM t6 WHERE a>='';
476 }
477} {2 1 4}
478do_test index-14.6 {
479 execsql {
480 SELECT c FROM t6 WHERE a>123;
481 }
482} {2 1 4}
483do_test index-14.7 {
484 execsql {
485 SELECT c FROM t6 WHERE a>=123;
486 }
487} {5 2 1 4}
488do_test index-14.8 {
489 execsql {
490 SELECT c FROM t6 WHERE a<'abc';
491 }
drh562528c2003-09-27 00:41:27 +0000492} {5 2 1}
drh491791a2002-07-18 00:34:09 +0000493do_test index-14.9 {
494 execsql {
495 SELECT c FROM t6 WHERE a<='abc';
496 }
drh562528c2003-09-27 00:41:27 +0000497} {5 2 1 4}
drh491791a2002-07-18 00:34:09 +0000498do_test index-14.10 {
499 execsql {
500 SELECT c FROM t6 WHERE a<='';
501 }
drh562528c2003-09-27 00:41:27 +0000502} {5 2 1}
drh491791a2002-07-18 00:34:09 +0000503do_test index-14.11 {
504 execsql {
505 SELECT c FROM t6 WHERE a<'';
506 }
drh562528c2003-09-27 00:41:27 +0000507} {5}
drhed717fe2003-06-15 23:42:24 +0000508integrity_check index-14.12
drh491791a2002-07-18 00:34:09 +0000509
drhbb07e9a2003-04-16 02:17:35 +0000510do_test index-15.1 {
511 execsql {
512 DELETE FROM t1;
513 SELECT * FROM t1;
514 }
515} {}
516do_test index-15.2 {
517 execsql {
518 INSERT INTO t1 VALUES('1.234e5',1);
519 INSERT INTO t1 VALUES('12.33e04',2);
520 INSERT INTO t1 VALUES('12.35E4',3);
521 INSERT INTO t1 VALUES('12.34e',4);
522 INSERT INTO t1 VALUES('12.32e+4',5);
523 INSERT INTO t1 VALUES('12.36E+04',6);
524 INSERT INTO t1 VALUES('12.36E+',7);
525 INSERT INTO t1 VALUES('+123.10000E+0003',8);
526 INSERT INTO t1 VALUES('+',9);
527 INSERT INTO t1 VALUES('+12347.E+02',10);
528 INSERT INTO t1 VALUES('+12347E+02',11);
529 SELECT b FROM t1 ORDER BY a;
530 }
531} {8 5 2 1 3 6 11 9 10 4 7}
drhed717fe2003-06-15 23:42:24 +0000532integrity_check index-15.1
drh491791a2002-07-18 00:34:09 +0000533
danielk1977d8123362004-06-12 09:25:12 +0000534# The following tests - index-16.* - test that when a table definition
535# includes qualifications that specify the same constraint twice only a
536# single index is generated to enforce the constraint.
537#
538# For example: "CREATE TABLE abc( x PRIMARY KEY, UNIQUE(x) );"
539#
540do_test index-16.1 {
541 execsql {
542 CREATE TABLE t7(c UNIQUE PRIMARY KEY);
543 SELECT count(*) FROM sqlite_master WHERE tbl_name = 't7' AND type = 'index';
544 }
545} {1}
546do_test index-16.2 {
547 execsql {
548 DROP TABLE t7;
549 CREATE TABLE t7(c UNIQUE PRIMARY KEY);
550 SELECT count(*) FROM sqlite_master WHERE tbl_name = 't7' AND type = 'index';
551 }
552} {1}
553do_test index-16.3 {
554 execsql {
555 DROP TABLE t7;
556 CREATE TABLE t7(c PRIMARY KEY, UNIQUE(c) );
557 SELECT count(*) FROM sqlite_master WHERE tbl_name = 't7' AND type = 'index';
558 }
559} {1}
560do_test index-16.4 {
561 execsql {
562 DROP TABLE t7;
563 CREATE TABLE t7(c, d , UNIQUE(c, d), PRIMARY KEY(c, d) );
564 SELECT count(*) FROM sqlite_master WHERE tbl_name = 't7' AND type = 'index';
565 }
566} {1}
567do_test index-16.5 {
568 execsql {
569 DROP TABLE t7;
570 CREATE TABLE t7(c, d , UNIQUE(c), PRIMARY KEY(c, d) );
571 SELECT count(*) FROM sqlite_master WHERE tbl_name = 't7' AND type = 'index';
572 }
573} {2}
574
575# Test that automatically create indices are named correctly. The current
576# convention is: "sqlite_autoindex_<table name>_<integer>"
577#
578# Then check that it is an error to try to drop any automtically created
579# indices.
580do_test index-17.1 {
581 execsql {
582 DROP TABLE t7;
583 CREATE TABLE t7(c, d UNIQUE, UNIQUE(c), PRIMARY KEY(c, d) );
584 SELECT name FROM sqlite_master WHERE tbl_name = 't7' AND type = 'index';
585 }
586} {sqlite_autoindex_t7_1 sqlite_autoindex_t7_2 sqlite_autoindex_t7_3}
587do_test index-17.2 {
588 catchsql {
589 DROP INDEX sqlite_autoindex_t7_1;
590 }
591} {1 {index associated with UNIQUE or PRIMARY KEY constraint cannot be dropped}}
592
593# The following tests ensure that it is not possible to explicitly name
594# a schema object with a name beginning with "sqlite_". Granted that is a
595# little outside the focus of this test scripts, but this has got to be
596# tested somewhere.
597do_test index-18.1 {
598 catchsql {
599 CREATE TABLE sqlite_t1(a, b, c);
600 }
601} {1 {object name reserved for internal use: sqlite_t1}}
602do_test index-18.2 {
603 catchsql {
604 CREATE INDEX sqlite_i1 ON t7(c);
605 }
606} {1 {object name reserved for internal use: sqlite_i1}}
danielk19770fa8ddb2004-11-22 08:43:32 +0000607ifcapable view {
danielk1977d8123362004-06-12 09:25:12 +0000608do_test index-18.3 {
609 catchsql {
610 CREATE VIEW sqlite_v1 AS SELECT * FROM t7;
611 }
612} {1 {object name reserved for internal use: sqlite_v1}}
danielk19770fa8ddb2004-11-22 08:43:32 +0000613} ;# ifcapable view
drh798da522004-11-04 04:42:28 +0000614ifcapable {trigger} {
615 do_test index-18.4 {
616 catchsql {
617 CREATE TRIGGER sqlite_tr1 BEFORE INSERT ON t7 BEGIN SELECT 1; END;
618 }
619 } {1 {object name reserved for internal use: sqlite_tr1}}
620}
danielk1977f736b772004-06-17 06:13:34 +0000621do_test index-18.5 {
622 execsql {
623 DROP TABLE t7;
624 }
625} {}
626
627# These tests ensure that if multiple table definition constraints are
628# implemented by a single indice, the correct ON CONFLICT policy applies.
629do_test index-19.1 {
630 execsql {
631 CREATE TABLE t7(a UNIQUE PRIMARY KEY);
632 CREATE TABLE t8(a UNIQUE PRIMARY KEY ON CONFLICT ROLLBACK);
633 INSERT INTO t7 VALUES(1);
634 INSERT INTO t8 VALUES(1);
635 }
636} {}
637do_test index-19.2 {
638 catchsql {
639 BEGIN;
640 INSERT INTO t7 VALUES(1);
641 }
642} {1 {column a is not unique}}
643do_test index-19.3 {
644 catchsql {
645 BEGIN;
646 }
647} {1 {cannot start a transaction within a transaction}}
648do_test index-19.4 {
649 catchsql {
650 INSERT INTO t8 VALUES(1);
651 }
652} {1 {column a is not unique}}
653do_test index-19.5 {
654 catchsql {
655 BEGIN;
656 COMMIT;
657 }
658} {0 {}}
659do_test index-19.6 {
660 catchsql {
661 DROP TABLE t7;
662 DROP TABLE t8;
663 CREATE TABLE t7(
664 a PRIMARY KEY ON CONFLICT FAIL,
665 UNIQUE(a) ON CONFLICT IGNORE
666 );
667 }
668} {1 {conflicting ON CONFLICT clauses specified}}
danielk1977d8123362004-06-12 09:25:12 +0000669
drhe497f002004-11-07 13:01:49 +0000670ifcapable {reindex} {
671 do_test index-19.7 {
672 execsql REINDEX
673 } {}
674}
675integrity_check index-19.8
676
drh78d153e2004-07-20 00:52:44 +0000677# Drop index with a quoted name. Ticket #695.
678#
679do_test index-20.1 {
680 execsql {
681 CREATE INDEX "t6i2" ON t6(c);
682 DROP INDEX "t6i2";
683 }
684} {}
685do_test index-20.2 {
686 execsql {
687 DROP INDEX "t6i1";
688 }
689} {}
690
danielk1977d8123362004-06-12 09:25:12 +0000691
drh78d153e2004-07-20 00:52:44 +0000692finish_test