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