xref: /titanic_52/usr/src/lib/libsqlite/test/index.test (revision c5c4113dfcabb1eed3d4bdf7609de5170027a794)
1*c5c4113dSnw141292
2*c5c4113dSnw141292#pragma ident	"%Z%%M%	%I%	%E% SMI"
3*c5c4113dSnw141292
4*c5c4113dSnw141292# 2001 September 15
5*c5c4113dSnw141292#
6*c5c4113dSnw141292# The author disclaims copyright to this source code.  In place of
7*c5c4113dSnw141292# a legal notice, here is a blessing:
8*c5c4113dSnw141292#
9*c5c4113dSnw141292#    May you do good and not evil.
10*c5c4113dSnw141292#    May you find forgiveness for yourself and forgive others.
11*c5c4113dSnw141292#    May you share freely, never taking more than you give.
12*c5c4113dSnw141292#
13*c5c4113dSnw141292#***********************************************************************
14*c5c4113dSnw141292# This file implements regression tests for SQLite library.  The
15*c5c4113dSnw141292# focus of this file is testing the CREATE INDEX statement.
16*c5c4113dSnw141292#
17*c5c4113dSnw141292# $Id: index.test,v 1.24.2.1 2004/07/20 00:50:30 drh Exp $
18*c5c4113dSnw141292
19*c5c4113dSnw141292set testdir [file dirname $argv0]
20*c5c4113dSnw141292source $testdir/tester.tcl
21*c5c4113dSnw141292
22*c5c4113dSnw141292# Create a basic index and verify it is added to sqlite_master
23*c5c4113dSnw141292#
24*c5c4113dSnw141292do_test index-1.1 {
25*c5c4113dSnw141292  execsql {CREATE TABLE test1(f1 int, f2 int, f3 int)}
26*c5c4113dSnw141292  execsql {CREATE INDEX index1 ON test1(f1)}
27*c5c4113dSnw141292  execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
28*c5c4113dSnw141292} {index1 test1}
29*c5c4113dSnw141292do_test index-1.1b {
30*c5c4113dSnw141292  execsql {SELECT name, sql, tbl_name, type FROM sqlite_master
31*c5c4113dSnw141292           WHERE name='index1'}
32*c5c4113dSnw141292} {index1 {CREATE INDEX index1 ON test1(f1)} test1 index}
33*c5c4113dSnw141292do_test index-1.1c {
34*c5c4113dSnw141292  db close
35*c5c4113dSnw141292  sqlite db test.db
36*c5c4113dSnw141292  execsql {SELECT name, sql, tbl_name, type FROM sqlite_master
37*c5c4113dSnw141292           WHERE name='index1'}
38*c5c4113dSnw141292} {index1 {CREATE INDEX index1 ON test1(f1)} test1 index}
39*c5c4113dSnw141292do_test index-1.1d {
40*c5c4113dSnw141292  db close
41*c5c4113dSnw141292  sqlite db test.db
42*c5c4113dSnw141292  execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
43*c5c4113dSnw141292} {index1 test1}
44*c5c4113dSnw141292
45*c5c4113dSnw141292# Verify that the index dies with the table
46*c5c4113dSnw141292#
47*c5c4113dSnw141292do_test index-1.2 {
48*c5c4113dSnw141292  execsql {DROP TABLE test1}
49*c5c4113dSnw141292  execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
50*c5c4113dSnw141292} {}
51*c5c4113dSnw141292
52*c5c4113dSnw141292# Try adding an index to a table that does not exist
53*c5c4113dSnw141292#
54*c5c4113dSnw141292do_test index-2.1 {
55*c5c4113dSnw141292  set v [catch {execsql {CREATE INDEX index1 ON test1(f1)}} msg]
56*c5c4113dSnw141292  lappend v $msg
57*c5c4113dSnw141292} {1 {no such table: test1}}
58*c5c4113dSnw141292
59*c5c4113dSnw141292# Try adding an index on a column of a table where the table
60*c5c4113dSnw141292# exists but the column does not.
61*c5c4113dSnw141292#
62*c5c4113dSnw141292do_test index-2.1 {
63*c5c4113dSnw141292  execsql {CREATE TABLE test1(f1 int, f2 int, f3 int)}
64*c5c4113dSnw141292  set v [catch {execsql {CREATE INDEX index1 ON test1(f4)}} msg]
65*c5c4113dSnw141292  lappend v $msg
66*c5c4113dSnw141292} {1 {table test1 has no column named f4}}
67*c5c4113dSnw141292
68*c5c4113dSnw141292# Try an index with some columns that match and others that do now.
69*c5c4113dSnw141292#
70*c5c4113dSnw141292do_test index-2.2 {
71*c5c4113dSnw141292  set v [catch {execsql {CREATE INDEX index1 ON test1(f1, f2, f4, f3)}} msg]
72*c5c4113dSnw141292  execsql {DROP TABLE test1}
73*c5c4113dSnw141292  lappend v $msg
74*c5c4113dSnw141292} {1 {table test1 has no column named f4}}
75*c5c4113dSnw141292
76*c5c4113dSnw141292# Try creating a bunch of indices on the same table
77*c5c4113dSnw141292#
78*c5c4113dSnw141292set r {}
79*c5c4113dSnw141292for {set i 1} {$i<100} {incr i} {
80*c5c4113dSnw141292  lappend r [format index%02d $i]
81*c5c4113dSnw141292}
82*c5c4113dSnw141292do_test index-3.1 {
83*c5c4113dSnw141292  execsql {CREATE TABLE test1(f1 int, f2 int, f3 int, f4 int, f5 int)}
84*c5c4113dSnw141292  for {set i 1} {$i<100} {incr i} {
85*c5c4113dSnw141292    set sql "CREATE INDEX [format index%02d $i] ON test1(f[expr {($i%5)+1}])"
86*c5c4113dSnw141292    execsql $sql
87*c5c4113dSnw141292  }
88*c5c4113dSnw141292  execsql {SELECT name FROM sqlite_master
89*c5c4113dSnw141292           WHERE type='index' AND tbl_name='test1'
90*c5c4113dSnw141292           ORDER BY name}
91*c5c4113dSnw141292} $r
92*c5c4113dSnw141292
93*c5c4113dSnw141292
94*c5c4113dSnw141292# Verify that all the indices go away when we drop the table.
95*c5c4113dSnw141292#
96*c5c4113dSnw141292do_test index-3.3 {
97*c5c4113dSnw141292  execsql {DROP TABLE test1}
98*c5c4113dSnw141292  execsql {SELECT name FROM sqlite_master
99*c5c4113dSnw141292           WHERE type='index' AND tbl_name='test1'
100*c5c4113dSnw141292           ORDER BY name}
101*c5c4113dSnw141292} {}
102*c5c4113dSnw141292
103*c5c4113dSnw141292# Create a table and insert values into that table.  Then create
104*c5c4113dSnw141292# an index on that table.  Verify that we can select values
105*c5c4113dSnw141292# from the table correctly using the index.
106*c5c4113dSnw141292#
107*c5c4113dSnw141292# Note that the index names "index9" and "indext" are chosen because
108*c5c4113dSnw141292# they both have the same hash.
109*c5c4113dSnw141292#
110*c5c4113dSnw141292do_test index-4.1 {
111*c5c4113dSnw141292  execsql {CREATE TABLE test1(cnt int, power int)}
112*c5c4113dSnw141292  for {set i 1} {$i<20} {incr i} {
113*c5c4113dSnw141292    execsql "INSERT INTO test1 VALUES($i,[expr {int(pow(2,$i))}])"
114*c5c4113dSnw141292  }
115*c5c4113dSnw141292  execsql {CREATE INDEX index9 ON test1(cnt)}
116*c5c4113dSnw141292  execsql {CREATE INDEX indext ON test1(power)}
117*c5c4113dSnw141292  execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
118*c5c4113dSnw141292} {index9 indext test1}
119*c5c4113dSnw141292do_test index-4.2 {
120*c5c4113dSnw141292  execsql {SELECT cnt FROM test1 WHERE power=4}
121*c5c4113dSnw141292} {2}
122*c5c4113dSnw141292do_test index-4.3 {
123*c5c4113dSnw141292  execsql {SELECT cnt FROM test1 WHERE power=1024}
124*c5c4113dSnw141292} {10}
125*c5c4113dSnw141292do_test index-4.4 {
126*c5c4113dSnw141292  execsql {SELECT power FROM test1 WHERE cnt=6}
127*c5c4113dSnw141292} {64}
128*c5c4113dSnw141292do_test index-4.5 {
129*c5c4113dSnw141292  execsql {DROP INDEX indext}
130*c5c4113dSnw141292  execsql {SELECT power FROM test1 WHERE cnt=6}
131*c5c4113dSnw141292} {64}
132*c5c4113dSnw141292do_test index-4.6 {
133*c5c4113dSnw141292  execsql {SELECT cnt FROM test1 WHERE power=1024}
134*c5c4113dSnw141292} {10}
135*c5c4113dSnw141292do_test index-4.7 {
136*c5c4113dSnw141292  execsql {CREATE INDEX indext ON test1(cnt)}
137*c5c4113dSnw141292  execsql {SELECT power FROM test1 WHERE cnt=6}
138*c5c4113dSnw141292} {64}
139*c5c4113dSnw141292do_test index-4.8 {
140*c5c4113dSnw141292  execsql {SELECT cnt FROM test1 WHERE power=1024}
141*c5c4113dSnw141292} {10}
142*c5c4113dSnw141292do_test index-4.9 {
143*c5c4113dSnw141292  execsql {DROP INDEX index9}
144*c5c4113dSnw141292  execsql {SELECT power FROM test1 WHERE cnt=6}
145*c5c4113dSnw141292} {64}
146*c5c4113dSnw141292do_test index-4.10 {
147*c5c4113dSnw141292  execsql {SELECT cnt FROM test1 WHERE power=1024}
148*c5c4113dSnw141292} {10}
149*c5c4113dSnw141292do_test index-4.11 {
150*c5c4113dSnw141292  execsql {DROP INDEX indext}
151*c5c4113dSnw141292  execsql {SELECT power FROM test1 WHERE cnt=6}
152*c5c4113dSnw141292} {64}
153*c5c4113dSnw141292do_test index-4.12 {
154*c5c4113dSnw141292  execsql {SELECT cnt FROM test1 WHERE power=1024}
155*c5c4113dSnw141292} {10}
156*c5c4113dSnw141292do_test index-4.13 {
157*c5c4113dSnw141292  execsql {DROP TABLE test1}
158*c5c4113dSnw141292  execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
159*c5c4113dSnw141292} {}
160*c5c4113dSnw141292integrity_check index-4.14
161*c5c4113dSnw141292
162*c5c4113dSnw141292# Do not allow indices to be added to sqlite_master
163*c5c4113dSnw141292#
164*c5c4113dSnw141292do_test index-5.1 {
165*c5c4113dSnw141292  set v [catch {execsql {CREATE INDEX index1 ON sqlite_master(name)}} msg]
166*c5c4113dSnw141292  lappend v $msg
167*c5c4113dSnw141292} {1 {table sqlite_master may not be indexed}}
168*c5c4113dSnw141292do_test index-5.2 {
169*c5c4113dSnw141292  execsql {SELECT name FROM sqlite_master WHERE type!='meta'}
170*c5c4113dSnw141292} {}
171*c5c4113dSnw141292
172*c5c4113dSnw141292# Do not allow indices with duplicate names to be added
173*c5c4113dSnw141292#
174*c5c4113dSnw141292do_test index-6.1 {
175*c5c4113dSnw141292  execsql {CREATE TABLE test1(f1 int, f2 int)}
176*c5c4113dSnw141292  execsql {CREATE TABLE test2(g1 real, g2 real)}
177*c5c4113dSnw141292  execsql {CREATE INDEX index1 ON test1(f1)}
178*c5c4113dSnw141292  set v [catch {execsql {CREATE INDEX index1 ON test2(g1)}} msg]
179*c5c4113dSnw141292  lappend v $msg
180*c5c4113dSnw141292} {1 {index index1 already exists}}
181*c5c4113dSnw141292do_test index-6.1b {
182*c5c4113dSnw141292  execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
183*c5c4113dSnw141292} {index1 test1 test2}
184*c5c4113dSnw141292do_test index-6.2 {
185*c5c4113dSnw141292  set v [catch {execsql {CREATE INDEX test1 ON test2(g1)}} msg]
186*c5c4113dSnw141292  lappend v $msg
187*c5c4113dSnw141292} {1 {there is already a table named test1}}
188*c5c4113dSnw141292do_test index-6.2b {
189*c5c4113dSnw141292  execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
190*c5c4113dSnw141292} {index1 test1 test2}
191*c5c4113dSnw141292do_test index-6.3 {
192*c5c4113dSnw141292  execsql {DROP TABLE test1}
193*c5c4113dSnw141292  execsql {DROP TABLE test2}
194*c5c4113dSnw141292  execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
195*c5c4113dSnw141292} {}
196*c5c4113dSnw141292do_test index-6.4 {
197*c5c4113dSnw141292  execsql {
198*c5c4113dSnw141292    CREATE TABLE test1(a,b);
199*c5c4113dSnw141292    CREATE INDEX index1 ON test1(a);
200*c5c4113dSnw141292    CREATE INDEX index2 ON test1(b);
201*c5c4113dSnw141292    CREATE INDEX index3 ON test1(a,b);
202*c5c4113dSnw141292    DROP TABLE test1;
203*c5c4113dSnw141292    SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name;
204*c5c4113dSnw141292  }
205*c5c4113dSnw141292} {}
206*c5c4113dSnw141292integrity_check index-6.5
207*c5c4113dSnw141292
208*c5c4113dSnw141292
209*c5c4113dSnw141292# Create a primary key
210*c5c4113dSnw141292#
211*c5c4113dSnw141292do_test index-7.1 {
212*c5c4113dSnw141292  execsql {CREATE TABLE test1(f1 int, f2 int primary key)}
213*c5c4113dSnw141292  for {set i 1} {$i<20} {incr i} {
214*c5c4113dSnw141292    execsql "INSERT INTO test1 VALUES($i,[expr {int(pow(2,$i))}])"
215*c5c4113dSnw141292  }
216*c5c4113dSnw141292  execsql {SELECT count(*) FROM test1}
217*c5c4113dSnw141292} {19}
218*c5c4113dSnw141292do_test index-7.2 {
219*c5c4113dSnw141292  execsql {SELECT f1 FROM test1 WHERE f2=65536}
220*c5c4113dSnw141292} {16}
221*c5c4113dSnw141292do_test index-7.3 {
222*c5c4113dSnw141292  execsql {
223*c5c4113dSnw141292    SELECT name FROM sqlite_master
224*c5c4113dSnw141292    WHERE type='index' AND tbl_name='test1'
225*c5c4113dSnw141292  }
226*c5c4113dSnw141292} {{(test1 autoindex 1)}}
227*c5c4113dSnw141292do_test index-7.4 {
228*c5c4113dSnw141292  execsql {DROP table test1}
229*c5c4113dSnw141292  execsql {SELECT name FROM sqlite_master WHERE type!='meta'}
230*c5c4113dSnw141292} {}
231*c5c4113dSnw141292integrity_check index-7.5
232*c5c4113dSnw141292
233*c5c4113dSnw141292# Make sure we cannot drop a non-existant index.
234*c5c4113dSnw141292#
235*c5c4113dSnw141292do_test index-8.1 {
236*c5c4113dSnw141292  set v [catch {execsql {DROP INDEX index1}} msg]
237*c5c4113dSnw141292  lappend v $msg
238*c5c4113dSnw141292} {1 {no such index: index1}}
239*c5c4113dSnw141292
240*c5c4113dSnw141292# Make sure we don't actually create an index when the EXPLAIN keyword
241*c5c4113dSnw141292# is used.
242*c5c4113dSnw141292#
243*c5c4113dSnw141292do_test index-9.1 {
244*c5c4113dSnw141292  execsql {CREATE TABLE tab1(a int)}
245*c5c4113dSnw141292  execsql {EXPLAIN CREATE INDEX idx1 ON tab1(a)}
246*c5c4113dSnw141292  execsql {SELECT name FROM sqlite_master WHERE tbl_name='tab1'}
247*c5c4113dSnw141292} {tab1}
248*c5c4113dSnw141292do_test index-9.2 {
249*c5c4113dSnw141292  execsql {CREATE INDEX idx1 ON tab1(a)}
250*c5c4113dSnw141292  execsql {SELECT name FROM sqlite_master WHERE tbl_name='tab1' ORDER BY name}
251*c5c4113dSnw141292} {idx1 tab1}
252*c5c4113dSnw141292integrity_check index-9.3
253*c5c4113dSnw141292
254*c5c4113dSnw141292# Allow more than one entry with the same key.
255*c5c4113dSnw141292#
256*c5c4113dSnw141292do_test index-10.0 {
257*c5c4113dSnw141292  execsql {
258*c5c4113dSnw141292    CREATE TABLE t1(a int, b int);
259*c5c4113dSnw141292    CREATE INDEX i1 ON t1(a);
260*c5c4113dSnw141292    INSERT INTO t1 VALUES(1,2);
261*c5c4113dSnw141292    INSERT INTO t1 VALUES(2,4);
262*c5c4113dSnw141292    INSERT INTO t1 VALUES(3,8);
263*c5c4113dSnw141292    INSERT INTO t1 VALUES(1,12);
264*c5c4113dSnw141292    SELECT b FROM t1 WHERE a=1 ORDER BY b;
265*c5c4113dSnw141292  }
266*c5c4113dSnw141292} {2 12}
267*c5c4113dSnw141292do_test index-10.1 {
268*c5c4113dSnw141292  execsql {
269*c5c4113dSnw141292    SELECT b FROM t1 WHERE a=2 ORDER BY b;
270*c5c4113dSnw141292  }
271*c5c4113dSnw141292} {4}
272*c5c4113dSnw141292do_test index-10.2 {
273*c5c4113dSnw141292  execsql {
274*c5c4113dSnw141292    DELETE FROM t1 WHERE b=12;
275*c5c4113dSnw141292    SELECT b FROM t1 WHERE a=1 ORDER BY b;
276*c5c4113dSnw141292  }
277*c5c4113dSnw141292} {2}
278*c5c4113dSnw141292do_test index-10.3 {
279*c5c4113dSnw141292  execsql {
280*c5c4113dSnw141292    DELETE FROM t1 WHERE b=2;
281*c5c4113dSnw141292    SELECT b FROM t1 WHERE a=1 ORDER BY b;
282*c5c4113dSnw141292  }
283*c5c4113dSnw141292} {}
284*c5c4113dSnw141292do_test index-10.4 {
285*c5c4113dSnw141292  execsql {
286*c5c4113dSnw141292    DELETE FROM t1;
287*c5c4113dSnw141292    INSERT INTO t1 VALUES (1,1);
288*c5c4113dSnw141292    INSERT INTO t1 VALUES (1,2);
289*c5c4113dSnw141292    INSERT INTO t1 VALUES (1,3);
290*c5c4113dSnw141292    INSERT INTO t1 VALUES (1,4);
291*c5c4113dSnw141292    INSERT INTO t1 VALUES (1,5);
292*c5c4113dSnw141292    INSERT INTO t1 VALUES (1,6);
293*c5c4113dSnw141292    INSERT INTO t1 VALUES (1,7);
294*c5c4113dSnw141292    INSERT INTO t1 VALUES (1,8);
295*c5c4113dSnw141292    INSERT INTO t1 VALUES (1,9);
296*c5c4113dSnw141292    INSERT INTO t1 VALUES (2,0);
297*c5c4113dSnw141292    SELECT b FROM t1 WHERE a=1 ORDER BY b;
298*c5c4113dSnw141292  }
299*c5c4113dSnw141292} {1 2 3 4 5 6 7 8 9}
300*c5c4113dSnw141292do_test index-10.5 {
301*c5c4113dSnw141292  execsql {
302*c5c4113dSnw141292    DELETE FROM t1 WHERE b IN (2, 4, 6, 8);
303*c5c4113dSnw141292    SELECT b FROM t1 WHERE a=1 ORDER BY b;
304*c5c4113dSnw141292  }
305*c5c4113dSnw141292} {1 3 5 7 9}
306*c5c4113dSnw141292do_test index-10.6 {
307*c5c4113dSnw141292  execsql {
308*c5c4113dSnw141292    DELETE FROM t1 WHERE b>2;
309*c5c4113dSnw141292    SELECT b FROM t1 WHERE a=1 ORDER BY b;
310*c5c4113dSnw141292  }
311*c5c4113dSnw141292} {1}
312*c5c4113dSnw141292do_test index-10.7 {
313*c5c4113dSnw141292  execsql {
314*c5c4113dSnw141292    DELETE FROM t1 WHERE b=1;
315*c5c4113dSnw141292    SELECT b FROM t1 WHERE a=1 ORDER BY b;
316*c5c4113dSnw141292  }
317*c5c4113dSnw141292} {}
318*c5c4113dSnw141292do_test index-10.8 {
319*c5c4113dSnw141292  execsql {
320*c5c4113dSnw141292    SELECT b FROM t1 ORDER BY b;
321*c5c4113dSnw141292  }
322*c5c4113dSnw141292} {0}
323*c5c4113dSnw141292integrity_check index-10.9
324*c5c4113dSnw141292
325*c5c4113dSnw141292# Automatically create an index when we specify a primary key.
326*c5c4113dSnw141292#
327*c5c4113dSnw141292do_test index-11.1 {
328*c5c4113dSnw141292  execsql {
329*c5c4113dSnw141292    CREATE TABLE t3(
330*c5c4113dSnw141292      a text,
331*c5c4113dSnw141292      b int,
332*c5c4113dSnw141292      c float,
333*c5c4113dSnw141292      PRIMARY KEY(b)
334*c5c4113dSnw141292    );
335*c5c4113dSnw141292  }
336*c5c4113dSnw141292  for {set i 1} {$i<=50} {incr i} {
337*c5c4113dSnw141292    execsql "INSERT INTO t3 VALUES('x${i}x',$i,0.$i)"
338*c5c4113dSnw141292  }
339*c5c4113dSnw141292  set sqlite_search_count 0
340*c5c4113dSnw141292  concat [execsql {SELECT c FROM t3 WHERE b==10}] $sqlite_search_count
341*c5c4113dSnw141292} {0.10 3}
342*c5c4113dSnw141292integrity_check index-11.2
343*c5c4113dSnw141292
344*c5c4113dSnw141292
345*c5c4113dSnw141292# Numeric strings should compare as if they were numbers.  So even if the
346*c5c4113dSnw141292# strings are not character-by-character the same, if they represent the
347*c5c4113dSnw141292# same number they should compare equal to one another.  Verify that this
348*c5c4113dSnw141292# is true in indices.
349*c5c4113dSnw141292#
350*c5c4113dSnw141292do_test index-12.1 {
351*c5c4113dSnw141292  execsql {
352*c5c4113dSnw141292    CREATE TABLE t4(a,b);
353*c5c4113dSnw141292    INSERT INTO t4 VALUES('0.0',1);
354*c5c4113dSnw141292    INSERT INTO t4 VALUES('0.00',2);
355*c5c4113dSnw141292    INSERT INTO t4 VALUES('abc',3);
356*c5c4113dSnw141292    INSERT INTO t4 VALUES('-1.0',4);
357*c5c4113dSnw141292    INSERT INTO t4 VALUES('+1.0',5);
358*c5c4113dSnw141292    INSERT INTO t4 VALUES('0',6);
359*c5c4113dSnw141292    INSERT INTO t4 VALUES('00000',7);
360*c5c4113dSnw141292    SELECT a FROM t4 ORDER BY b;
361*c5c4113dSnw141292  }
362*c5c4113dSnw141292} {0.0 0.00 abc -1.0 +1.0 0 00000}
363*c5c4113dSnw141292do_test index-12.2 {
364*c5c4113dSnw141292  execsql {
365*c5c4113dSnw141292    SELECT a FROM t4 WHERE a==0 ORDER BY b
366*c5c4113dSnw141292  }
367*c5c4113dSnw141292} {0.0 0.00 0 00000}
368*c5c4113dSnw141292do_test index-12.3 {
369*c5c4113dSnw141292  execsql {
370*c5c4113dSnw141292    SELECT a FROM t4 WHERE a<0.5 ORDER BY b
371*c5c4113dSnw141292  }
372*c5c4113dSnw141292} {0.0 0.00 -1.0 0 00000}
373*c5c4113dSnw141292do_test index-12.4 {
374*c5c4113dSnw141292  execsql {
375*c5c4113dSnw141292    SELECT a FROM t4 WHERE a>-0.5 ORDER BY b
376*c5c4113dSnw141292  }
377*c5c4113dSnw141292} {0.0 0.00 abc +1.0 0 00000}
378*c5c4113dSnw141292do_test index-12.5 {
379*c5c4113dSnw141292  execsql {
380*c5c4113dSnw141292    CREATE INDEX t4i1 ON t4(a);
381*c5c4113dSnw141292    SELECT a FROM t4 WHERE a==0 ORDER BY b
382*c5c4113dSnw141292  }
383*c5c4113dSnw141292} {0.0 0.00 0 00000}
384*c5c4113dSnw141292do_test index-12.6 {
385*c5c4113dSnw141292  execsql {
386*c5c4113dSnw141292    SELECT a FROM t4 WHERE a<0.5 ORDER BY b
387*c5c4113dSnw141292  }
388*c5c4113dSnw141292} {0.0 0.00 -1.0 0 00000}
389*c5c4113dSnw141292do_test index-12.7 {
390*c5c4113dSnw141292  execsql {
391*c5c4113dSnw141292    SELECT a FROM t4 WHERE a>-0.5 ORDER BY b
392*c5c4113dSnw141292  }
393*c5c4113dSnw141292} {0.0 0.00 abc +1.0 0 00000}
394*c5c4113dSnw141292integrity_check index-12.8
395*c5c4113dSnw141292
396*c5c4113dSnw141292# Make sure we cannot drop an automatically created index.
397*c5c4113dSnw141292#
398*c5c4113dSnw141292do_test index-13.1 {
399*c5c4113dSnw141292  execsql {
400*c5c4113dSnw141292   CREATE TABLE t5(
401*c5c4113dSnw141292      a int UNIQUE,
402*c5c4113dSnw141292      b float PRIMARY KEY,
403*c5c4113dSnw141292      c varchar(10),
404*c5c4113dSnw141292      UNIQUE(a,c)
405*c5c4113dSnw141292   );
406*c5c4113dSnw141292   INSERT INTO t5 VALUES(1,2,3);
407*c5c4113dSnw141292   SELECT * FROM t5;
408*c5c4113dSnw141292  }
409*c5c4113dSnw141292} {1 2 3}
410*c5c4113dSnw141292do_test index-13.2 {
411*c5c4113dSnw141292  set ::idxlist [execsql {
412*c5c4113dSnw141292    SELECT name FROM sqlite_master WHERE type="index" AND tbl_name="t5";
413*c5c4113dSnw141292  }]
414*c5c4113dSnw141292  llength $::idxlist
415*c5c4113dSnw141292} {3}
416*c5c4113dSnw141292for {set i 0} {$i<[llength $::idxlist]} {incr i} {
417*c5c4113dSnw141292  do_test index-13.3.$i {
418*c5c4113dSnw141292    catchsql "
419*c5c4113dSnw141292      DROP INDEX '[lindex $::idxlist $i]';
420*c5c4113dSnw141292    "
421*c5c4113dSnw141292  } {1 {index associated with UNIQUE or PRIMARY KEY constraint cannot be dropped}}
422*c5c4113dSnw141292}
423*c5c4113dSnw141292do_test index-13.4 {
424*c5c4113dSnw141292  execsql {
425*c5c4113dSnw141292    INSERT INTO t5 VALUES('a','b','c');
426*c5c4113dSnw141292    SELECT * FROM t5;
427*c5c4113dSnw141292  }
428*c5c4113dSnw141292} {1 2 3 a b c}
429*c5c4113dSnw141292integrity_check index-13.5
430*c5c4113dSnw141292
431*c5c4113dSnw141292# Check the sort order of data in an index.
432*c5c4113dSnw141292#
433*c5c4113dSnw141292do_test index-14.1 {
434*c5c4113dSnw141292  execsql {
435*c5c4113dSnw141292    CREATE TABLE t6(a,b,c);
436*c5c4113dSnw141292    CREATE INDEX t6i1 ON t6(a,b);
437*c5c4113dSnw141292    INSERT INTO t6 VALUES('','',1);
438*c5c4113dSnw141292    INSERT INTO t6 VALUES('',NULL,2);
439*c5c4113dSnw141292    INSERT INTO t6 VALUES(NULL,'',3);
440*c5c4113dSnw141292    INSERT INTO t6 VALUES('abc',123,4);
441*c5c4113dSnw141292    INSERT INTO t6 VALUES(123,'abc',5);
442*c5c4113dSnw141292    SELECT c FROM t6 ORDER BY a,b;
443*c5c4113dSnw141292  }
444*c5c4113dSnw141292} {3 5 2 1 4}
445*c5c4113dSnw141292do_test index-14.2 {
446*c5c4113dSnw141292  execsql {
447*c5c4113dSnw141292    SELECT c FROM t6 WHERE a='';
448*c5c4113dSnw141292  }
449*c5c4113dSnw141292} {2 1}
450*c5c4113dSnw141292do_test index-14.3 {
451*c5c4113dSnw141292  execsql {
452*c5c4113dSnw141292    SELECT c FROM t6 WHERE b='';
453*c5c4113dSnw141292  }
454*c5c4113dSnw141292} {1 3}
455*c5c4113dSnw141292do_test index-14.4 {
456*c5c4113dSnw141292  execsql {
457*c5c4113dSnw141292    SELECT c FROM t6 WHERE a>'';
458*c5c4113dSnw141292  }
459*c5c4113dSnw141292} {4}
460*c5c4113dSnw141292do_test index-14.5 {
461*c5c4113dSnw141292  execsql {
462*c5c4113dSnw141292    SELECT c FROM t6 WHERE a>='';
463*c5c4113dSnw141292  }
464*c5c4113dSnw141292} {2 1 4}
465*c5c4113dSnw141292do_test index-14.6 {
466*c5c4113dSnw141292  execsql {
467*c5c4113dSnw141292    SELECT c FROM t6 WHERE a>123;
468*c5c4113dSnw141292  }
469*c5c4113dSnw141292} {2 1 4}
470*c5c4113dSnw141292do_test index-14.7 {
471*c5c4113dSnw141292  execsql {
472*c5c4113dSnw141292    SELECT c FROM t6 WHERE a>=123;
473*c5c4113dSnw141292  }
474*c5c4113dSnw141292} {5 2 1 4}
475*c5c4113dSnw141292do_test index-14.8 {
476*c5c4113dSnw141292  execsql {
477*c5c4113dSnw141292    SELECT c FROM t6 WHERE a<'abc';
478*c5c4113dSnw141292  }
479*c5c4113dSnw141292} {5 2 1}
480*c5c4113dSnw141292do_test index-14.9 {
481*c5c4113dSnw141292  execsql {
482*c5c4113dSnw141292    SELECT c FROM t6 WHERE a<='abc';
483*c5c4113dSnw141292  }
484*c5c4113dSnw141292} {5 2 1 4}
485*c5c4113dSnw141292do_test index-14.10 {
486*c5c4113dSnw141292  execsql {
487*c5c4113dSnw141292    SELECT c FROM t6 WHERE a<='';
488*c5c4113dSnw141292  }
489*c5c4113dSnw141292} {5 2 1}
490*c5c4113dSnw141292do_test index-14.11 {
491*c5c4113dSnw141292  execsql {
492*c5c4113dSnw141292    SELECT c FROM t6 WHERE a<'';
493*c5c4113dSnw141292  }
494*c5c4113dSnw141292} {5}
495*c5c4113dSnw141292integrity_check index-14.12
496*c5c4113dSnw141292
497*c5c4113dSnw141292do_test index-15.1 {
498*c5c4113dSnw141292  execsql {
499*c5c4113dSnw141292    DELETE FROM t1;
500*c5c4113dSnw141292    SELECT * FROM t1;
501*c5c4113dSnw141292  }
502*c5c4113dSnw141292} {}
503*c5c4113dSnw141292do_test index-15.2 {
504*c5c4113dSnw141292  execsql {
505*c5c4113dSnw141292    INSERT INTO t1 VALUES('1.234e5',1);
506*c5c4113dSnw141292    INSERT INTO t1 VALUES('12.33e04',2);
507*c5c4113dSnw141292    INSERT INTO t1 VALUES('12.35E4',3);
508*c5c4113dSnw141292    INSERT INTO t1 VALUES('12.34e',4);
509*c5c4113dSnw141292    INSERT INTO t1 VALUES('12.32e+4',5);
510*c5c4113dSnw141292    INSERT INTO t1 VALUES('12.36E+04',6);
511*c5c4113dSnw141292    INSERT INTO t1 VALUES('12.36E+',7);
512*c5c4113dSnw141292    INSERT INTO t1 VALUES('+123.10000E+0003',8);
513*c5c4113dSnw141292    INSERT INTO t1 VALUES('+',9);
514*c5c4113dSnw141292    INSERT INTO t1 VALUES('+12347.E+02',10);
515*c5c4113dSnw141292    INSERT INTO t1 VALUES('+12347E+02',11);
516*c5c4113dSnw141292    SELECT b FROM t1 ORDER BY a;
517*c5c4113dSnw141292  }
518*c5c4113dSnw141292} {8 5 2 1 3 6 11 9 10 4 7}
519*c5c4113dSnw141292integrity_check index-15.1
520*c5c4113dSnw141292
521*c5c4113dSnw141292# Drop index with a quoted name.  Ticket #695.
522*c5c4113dSnw141292#
523*c5c4113dSnw141292do_test index-16.1 {
524*c5c4113dSnw141292  execsql {
525*c5c4113dSnw141292    CREATE INDEX "t6i2" ON t6(c);
526*c5c4113dSnw141292    DROP INDEX "t6i2";
527*c5c4113dSnw141292  }
528*c5c4113dSnw141292} {}
529*c5c4113dSnw141292do_test index-16.2 {
530*c5c4113dSnw141292  execsql {
531*c5c4113dSnw141292    DROP INDEX "t6i1";
532*c5c4113dSnw141292  }
533*c5c4113dSnw141292} {}
534*c5c4113dSnw141292
535*c5c4113dSnw141292
536*c5c4113dSnw141292finish_test
537