blob: a448e52dd285847fb7241aba9bded79e920b7bf2 [file] [log] [blame]
drhb19a2bc2001-09-16 00:13:26 +00001# 2001 September 15
drh62c68192000-05-30 00:51:26 +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:
drh62c68192000-05-30 00:51:26 +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.
drh62c68192000-05-30 00:51:26 +00009#
10#***********************************************************************
11# This file implements regression tests for SQLite library. The
12# focus of this file is testing the DELETE FROM statement.
13#
drhdda70fe2009-06-05 17:09:11 +000014# $Id: delete.test,v 1.26 2009/06/05 17:09:12 drh Exp $
drh62c68192000-05-30 00:51:26 +000015
16set testdir [file dirname $argv0]
17source $testdir/tester.tcl
18
19# Try to delete from a non-existant table.
20#
21do_test delete-1.1 {
22 set v [catch {execsql {DELETE FROM test1}} msg]
23 lappend v $msg
24} {1 {no such table: test1}}
25
26# Try to delete from sqlite_master
27#
28do_test delete-2.1 {
29 set v [catch {execsql {DELETE FROM sqlite_master}} msg]
30 lappend v $msg
drh1d37e282000-05-30 03:12:21 +000031} {1 {table sqlite_master may not be modified}}
drh62c68192000-05-30 00:51:26 +000032
drh7020f652000-06-03 18:06:52 +000033# Delete selected entries from a table with and without an index.
34#
drh1bee3d72001-10-15 00:44:35 +000035do_test delete-3.1.1 {
drh7020f652000-06-03 18:06:52 +000036 execsql {CREATE TABLE table1(f1 int, f2 int)}
37 execsql {INSERT INTO table1 VALUES(1,2)}
38 execsql {INSERT INTO table1 VALUES(2,4)}
39 execsql {INSERT INTO table1 VALUES(3,8)}
40 execsql {INSERT INTO table1 VALUES(4,16)}
41 execsql {SELECT * FROM table1 ORDER BY f1}
42} {1 2 2 4 3 8 4 16}
drh1bee3d72001-10-15 00:44:35 +000043do_test delete-3.1.2 {
drh7020f652000-06-03 18:06:52 +000044 execsql {DELETE FROM table1 WHERE f1=3}
drh1bee3d72001-10-15 00:44:35 +000045} {}
46do_test delete-3.1.3 {
drh7020f652000-06-03 18:06:52 +000047 execsql {SELECT * FROM table1 ORDER BY f1}
48} {1 2 2 4 4 16}
drh1bee3d72001-10-15 00:44:35 +000049do_test delete-3.1.4 {
drh7020f652000-06-03 18:06:52 +000050 execsql {CREATE INDEX index1 ON table1(f1)}
drh1bee3d72001-10-15 00:44:35 +000051 execsql {PRAGMA count_changes=on}
drhcc43cab2005-10-05 11:35:09 +000052 ifcapable explain {
53 execsql {EXPLAIN DELETE FROM table1 WHERE f1=3}
54 }
drh4cfa7932000-06-08 15:10:46 +000055 execsql {DELETE FROM 'table1' WHERE f1=3}
drh1bee3d72001-10-15 00:44:35 +000056} {0}
57do_test delete-3.1.5 {
drh7020f652000-06-03 18:06:52 +000058 execsql {SELECT * FROM table1 ORDER BY f1}
59} {1 2 2 4 4 16}
drh4ebfef12004-07-15 20:08:39 +000060do_test delete-3.1.6.1 {
drh7020f652000-06-03 18:06:52 +000061 execsql {DELETE FROM table1 WHERE f1=2}
drh1bee3d72001-10-15 00:44:35 +000062} {1}
drh4ebfef12004-07-15 20:08:39 +000063do_test delete-3.1.6.2 {
64 db changes
65} 1
drh1bee3d72001-10-15 00:44:35 +000066do_test delete-3.1.7 {
drh7020f652000-06-03 18:06:52 +000067 execsql {SELECT * FROM table1 ORDER BY f1}
68} {1 2 4 16}
drhed717fe2003-06-15 23:42:24 +000069integrity_check delete-3.2
70
drh8be51132000-06-03 19:19:41 +000071# Semantic errors in the WHERE clause
72#
73do_test delete-4.1 {
74 execsql {CREATE TABLE table2(f1 int, f2 int)}
75 set v [catch {execsql {DELETE FROM table2 WHERE f3=5}} msg]
76 lappend v $msg
drh967e8b72000-06-21 13:59:10 +000077} {1 {no such column: f3}}
drh8be51132000-06-03 19:19:41 +000078
79do_test delete-4.2 {
80 set v [catch {execsql {DELETE FROM table2 WHERE xyzzy(f1+4)}} msg]
81 lappend v $msg
82} {1 {no such function: xyzzy}}
drhed717fe2003-06-15 23:42:24 +000083integrity_check delete-4.3
drh62c68192000-05-30 00:51:26 +000084
drh3494ffe2001-03-20 12:55:13 +000085# Lots of deletes
86#
drh1bee3d72001-10-15 00:44:35 +000087do_test delete-5.1.1 {
drh3494ffe2001-03-20 12:55:13 +000088 execsql {DELETE FROM table1}
drh1bee3d72001-10-15 00:44:35 +000089} {2}
90do_test delete-5.1.2 {
drh3494ffe2001-03-20 12:55:13 +000091 execsql {SELECT count(*) FROM table1}
drh1bee3d72001-10-15 00:44:35 +000092} {0}
93do_test delete-5.2.1 {
94 execsql {BEGIN TRANSACTION}
drh3494ffe2001-03-20 12:55:13 +000095 for {set i 1} {$i<=200} {incr i} {
96 execsql "INSERT INTO table1 VALUES($i,[expr {$i*$i}])"
97 }
drh1bee3d72001-10-15 00:44:35 +000098 execsql {COMMIT}
99 execsql {SELECT count(*) FROM table1}
100} {200}
101do_test delete-5.2.2 {
102 execsql {DELETE FROM table1}
103} {200}
104do_test delete-5.2.3 {
105 execsql {BEGIN TRANSACTION}
106 for {set i 1} {$i<=200} {incr i} {
107 execsql "INSERT INTO table1 VALUES($i,[expr {$i*$i}])"
108 }
109 execsql {COMMIT}
110 execsql {SELECT count(*) FROM table1}
111} {200}
112do_test delete-5.2.4 {
113 execsql {PRAGMA count_changes=off}
114 execsql {DELETE FROM table1}
115} {}
116do_test delete-5.2.5 {
117 execsql {SELECT count(*) FROM table1}
118} {0}
119do_test delete-5.2.6 {
120 execsql {BEGIN TRANSACTION}
121 for {set i 1} {$i<=200} {incr i} {
122 execsql "INSERT INTO table1 VALUES($i,[expr {$i*$i}])"
123 }
124 execsql {COMMIT}
drh3494ffe2001-03-20 12:55:13 +0000125 execsql {SELECT count(*) FROM table1}
126} {200}
127do_test delete-5.3 {
128 for {set i 1} {$i<=200} {incr i 4} {
129 execsql "DELETE FROM table1 WHERE f1==$i"
130 }
131 execsql {SELECT count(*) FROM table1}
132} {150}
drh4ebfef12004-07-15 20:08:39 +0000133do_test delete-5.4.1 {
drh3494ffe2001-03-20 12:55:13 +0000134 execsql "DELETE FROM table1 WHERE f1>50"
drh4ebfef12004-07-15 20:08:39 +0000135 db changes
136} [db one {SELECT count(*) FROM table1 WHERE f1>50}]
137do_test delete-5.4.2 {
drh3494ffe2001-03-20 12:55:13 +0000138 execsql {SELECT count(*) FROM table1}
139} {37}
140do_test delete-5.5 {
141 for {set i 1} {$i<=70} {incr i 3} {
142 execsql "DELETE FROM table1 WHERE f1==$i"
143 }
144 execsql {SELECT f1 FROM table1 ORDER BY f1}
145} {2 3 6 8 11 12 14 15 18 20 23 24 26 27 30 32 35 36 38 39 42 44 47 48 50}
146do_test delete-5.6 {
147 for {set i 1} {$i<40} {incr i} {
148 execsql "DELETE FROM table1 WHERE f1==$i"
149 }
150 execsql {SELECT f1 FROM table1 ORDER BY f1}
151} {42 44 47 48 50}
152do_test delete-5.7 {
153 execsql "DELETE FROM table1 WHERE f1!=48"
154 execsql {SELECT f1 FROM table1 ORDER BY f1}
155} {48}
drhed717fe2003-06-15 23:42:24 +0000156integrity_check delete-5.8
157
drh3494ffe2001-03-20 12:55:13 +0000158
drh0353ced2001-03-20 22:05:00 +0000159# Delete large quantities of data. We want to test the List overflow
160# mechanism in the vdbe.
161#
162do_test delete-6.1 {
drh5f3b4ab2004-05-27 17:22:54 +0000163 execsql {BEGIN; DELETE FROM table1}
drh0353ced2001-03-20 22:05:00 +0000164 for {set i 1} {$i<=3000} {incr i} {
drh5f3b4ab2004-05-27 17:22:54 +0000165 execsql "INSERT INTO table1 VALUES($i,[expr {$i*$i}])"
drh0353ced2001-03-20 22:05:00 +0000166 }
drh0353ced2001-03-20 22:05:00 +0000167 execsql {DELETE FROM table2}
drh5f3b4ab2004-05-27 17:22:54 +0000168 for {set i 1} {$i<=3000} {incr i} {
169 execsql "INSERT INTO table2 VALUES($i,[expr {$i*$i}])"
170 }
171 execsql {COMMIT}
drh0353ced2001-03-20 22:05:00 +0000172 execsql {SELECT count(*) FROM table1}
173} {3000}
174do_test delete-6.2 {
175 execsql {SELECT count(*) FROM table2}
176} {3000}
177do_test delete-6.3 {
178 execsql {SELECT f1 FROM table1 WHERE f1<10 ORDER BY f1}
179} {1 2 3 4 5 6 7 8 9}
180do_test delete-6.4 {
181 execsql {SELECT f1 FROM table2 WHERE f1<10 ORDER BY f1}
182} {1 2 3 4 5 6 7 8 9}
drh4ebfef12004-07-15 20:08:39 +0000183do_test delete-6.5.1 {
drh0353ced2001-03-20 22:05:00 +0000184 execsql {DELETE FROM table1 WHERE f1>7}
drh4ebfef12004-07-15 20:08:39 +0000185 db changes
186} {2993}
187do_test delete-6.5.2 {
drh0353ced2001-03-20 22:05:00 +0000188 execsql {SELECT f1 FROM table1 ORDER BY f1}
189} {1 2 3 4 5 6 7}
190do_test delete-6.6 {
191 execsql {DELETE FROM table2 WHERE f1>7}
192 execsql {SELECT f1 FROM table2 ORDER BY f1}
193} {1 2 3 4 5 6 7}
194do_test delete-6.7 {
195 execsql {DELETE FROM table1}
196 execsql {SELECT f1 FROM table1}
197} {}
198do_test delete-6.8 {
199 execsql {INSERT INTO table1 VALUES(2,3)}
200 execsql {SELECT f1 FROM table1}
201} {2}
202do_test delete-6.9 {
203 execsql {DELETE FROM table2}
204 execsql {SELECT f1 FROM table2}
205} {}
206do_test delete-6.10 {
207 execsql {INSERT INTO table2 VALUES(2,3)}
208 execsql {SELECT f1 FROM table2}
209} {2}
drhed717fe2003-06-15 23:42:24 +0000210integrity_check delete-6.11
drh0353ced2001-03-20 22:05:00 +0000211
drh26b3e1b2002-07-19 18:52:40 +0000212do_test delete-7.1 {
213 execsql {
214 CREATE TABLE t3(a);
215 INSERT INTO t3 VALUES(1);
216 INSERT INTO t3 SELECT a+1 FROM t3;
217 INSERT INTO t3 SELECT a+2 FROM t3;
218 SELECT * FROM t3;
219 }
220} {1 2 3 4}
drh798da522004-11-04 04:42:28 +0000221ifcapable {trigger} {
222 do_test delete-7.2 {
223 execsql {
224 CREATE TABLE cnt(del);
225 INSERT INTO cnt VALUES(0);
226 CREATE TRIGGER r1 AFTER DELETE ON t3 FOR EACH ROW BEGIN
227 UPDATE cnt SET del=del+1;
228 END;
229 DELETE FROM t3 WHERE a<2;
230 SELECT * FROM t3;
231 }
232 } {2 3 4}
233 do_test delete-7.3 {
234 execsql {
235 SELECT * FROM cnt;
236 }
237 } {1}
238 do_test delete-7.4 {
239 execsql {
240 DELETE FROM t3;
241 SELECT * FROM t3;
242 }
243 } {}
244 do_test delete-7.5 {
245 execsql {
246 SELECT * FROM cnt;
247 }
248 } {4}
249 do_test delete-7.6 {
250 execsql {
251 INSERT INTO t3 VALUES(1);
252 INSERT INTO t3 SELECT a+1 FROM t3;
253 INSERT INTO t3 SELECT a+2 FROM t3;
254 CREATE TABLE t4 AS SELECT * FROM t3;
255 PRAGMA count_changes=ON;
256 DELETE FROM t3;
257 DELETE FROM t4;
258 }
259 } {4 4}
260} ;# endif trigger
261ifcapable {!trigger} {
262 execsql {DELETE FROM t3}
263}
drhed717fe2003-06-15 23:42:24 +0000264integrity_check delete-7.7
drh0353ced2001-03-20 22:05:00 +0000265
drh66b4eba2003-05-04 20:42:56 +0000266# Make sure error messages are consistent when attempting to delete
267# from a read-only database. Ticket #304.
268#
269do_test delete-8.0 {
270 execsql {
271 PRAGMA count_changes=OFF;
272 INSERT INTO t3 VALUES(123);
273 SELECT * FROM t3;
274 }
275} {123}
276db close
mistachkinfda06be2011-08-02 00:57:34 +0000277catch {forcedelete test.db-journal}
drh66b4eba2003-05-04 20:42:56 +0000278catch {file attributes test.db -permissions 0444}
279catch {file attributes test.db -readonly 1}
drhdddca282006-01-03 00:33:50 +0000280sqlite3 db test.db
281set ::DB [sqlite3_connection_pointer db]
drh66b4eba2003-05-04 20:42:56 +0000282do_test delete-8.1 {
283 catchsql {
284 DELETE FROM t3;
285 }
286} {1 {attempt to write a readonly database}}
287do_test delete-8.2 {
288 execsql {SELECT * FROM t3}
289} {123}
290do_test delete-8.3 {
291 catchsql {
292 DELETE FROM t3 WHERE 1;
293 }
294} {1 {attempt to write a readonly database}}
295do_test delete-8.4 {
296 execsql {SELECT * FROM t3}
297} {123}
danielk19770de0bb32004-06-10 05:59:24 +0000298
299# Update for v3: In v2 the DELETE statement would succeed because no
300# database writes actually occur. Version 3 refuses to open a transaction
301# on a read-only file, so the statement fails.
drh66b4eba2003-05-04 20:42:56 +0000302do_test delete-8.5 {
303 catchsql {
304 DELETE FROM t3 WHERE a<100;
305 }
danielk19770de0bb32004-06-10 05:59:24 +0000306# v2 result: {0 {}}
307} {1 {attempt to write a readonly database}}
drh66b4eba2003-05-04 20:42:56 +0000308do_test delete-8.6 {
309 execsql {SELECT * FROM t3}
310} {123}
drhed717fe2003-06-15 23:42:24 +0000311integrity_check delete-8.7
drh0353ced2001-03-20 22:05:00 +0000312
danielk1977382e28f2007-10-05 15:53:29 +0000313# Need to do the following for tcl 8.5 on mac. On that configuration, the
mistachkinfda06be2011-08-02 00:57:34 +0000314# -readonly flag is taken so seriously that a subsequent [forcedelete]
danielk1977382e28f2007-10-05 15:53:29 +0000315# (required before the next test file can be executed) will fail.
316#
317catch {file attributes test.db -readonly 0}
danielk1977ef165ce2009-04-06 17:50:03 +0000318db close
mistachkinfda06be2011-08-02 00:57:34 +0000319forcedelete test.db test.db-journal
danielk1977ef165ce2009-04-06 17:50:03 +0000320
danielk1977c4d201c2009-04-07 09:16:56 +0000321# The following tests verify that SQLite correctly handles the case
322# where an index B-Tree is being scanned, the rowid column being read
323# from each index entry and another statement deletes some rows from
324# the index B-Tree. At one point this (obscure) scenario was causing
325# SQLite to return spurious SQLITE_CORRUPT errors and arguably incorrect
326# query results.
327#
danielk1977ef165ce2009-04-06 17:50:03 +0000328do_test delete-9.1 {
329 sqlite3 db test.db
330 execsql {
331 CREATE TABLE t5(a, b);
332 CREATE TABLE t6(c, d);
333 INSERT INTO t5 VALUES(1, 2);
334 INSERT INTO t5 VALUES(3, 4);
335 INSERT INTO t5 VALUES(5, 6);
336 INSERT INTO t6 VALUES('a', 'b');
337 INSERT INTO t6 VALUES('c', 'd');
danielk1977ef165ce2009-04-06 17:50:03 +0000338 CREATE INDEX i5 ON t5(a);
danielk1977c4d201c2009-04-07 09:16:56 +0000339 CREATE INDEX i6 ON t6(c);
danielk1977ef165ce2009-04-06 17:50:03 +0000340 }
341} {}
342do_test delete-9.2 {
danielk1977c4d201c2009-04-07 09:16:56 +0000343 set res [list]
344 db eval { SELECT t5.rowid AS r, c, d FROM t5, t6 ORDER BY a } {
drhdda70fe2009-06-05 17:09:11 +0000345 if {$r==2} { db eval { DELETE FROM t5 } }
danielk1977c4d201c2009-04-07 09:16:56 +0000346 lappend res $r $c $d
347 }
348 set res
349} {1 a b 1 c d 2 a b {} c d}
350do_test delete-9.3 {
351 execsql {
352 INSERT INTO t5 VALUES(1, 2);
353 INSERT INTO t5 VALUES(3, 4);
354 INSERT INTO t5 VALUES(5, 6);
355 }
356 set res [list]
357 db eval { SELECT t5.rowid AS r, c, d FROM t5, t6 ORDER BY a } {
drhdda70fe2009-06-05 17:09:11 +0000358 if {$r==2} { db eval { DELETE FROM t5 WHERE rowid = 2 } }
danielk1977c4d201c2009-04-07 09:16:56 +0000359 lappend res $r $c $d
360 }
361 set res
362} {1 a b 1 c d 2 a b {} c d 3 a b 3 c d}
363do_test delete-9.4 {
364 execsql {
365 DELETE FROM t5;
366 INSERT INTO t5 VALUES(1, 2);
367 INSERT INTO t5 VALUES(3, 4);
368 INSERT INTO t5 VALUES(5, 6);
369 }
370 set res [list]
371 db eval { SELECT t5.rowid AS r, c, d FROM t5, t6 ORDER BY a } {
drhdda70fe2009-06-05 17:09:11 +0000372 if {$r==2} { db eval { DELETE FROM t5 WHERE rowid = 1 } }
danielk1977c4d201c2009-04-07 09:16:56 +0000373 lappend res $r $c $d
374 }
375 set res
376} {1 a b 1 c d 2 a b 2 c d 3 a b 3 c d}
377do_test delete-9.5 {
378 execsql {
379 DELETE FROM t5;
380 INSERT INTO t5 VALUES(1, 2);
381 INSERT INTO t5 VALUES(3, 4);
382 INSERT INTO t5 VALUES(5, 6);
383 }
384 set res [list]
385 db eval { SELECT t5.rowid AS r, c, d FROM t5, t6 ORDER BY a } {
drhdda70fe2009-06-05 17:09:11 +0000386 if {$r==2} { db eval { DELETE FROM t5 WHERE rowid = 3 } }
danielk1977c4d201c2009-04-07 09:16:56 +0000387 lappend res $r $c $d
388 }
389 set res
390} {1 a b 1 c d 2 a b 2 c d}
danielk1977ef165ce2009-04-06 17:50:03 +0000391
dan61441c32016-08-26 12:00:50 +0000392do_execsql_test delete-10.0 {
393 CREATE TABLE t1(a INT UNIQUE, b INT);
394 INSERT INTO t1(a,b) VALUES('1','2');
395 SELECT * FROM t1 WHERE a='1' AND b='2';
396} {1 2}
397
398do_execsql_test delete-10.1 {
399 DELETE FROM t1 WHERE a='1' AND b='2';
400}
401
402do_execsql_test delete-10.2 {
403 SELECT * FROM t1 WHERE a='1' AND b='2';
404}
405
drh5e3a6eb2018-04-19 11:45:16 +0000406do_execsql_test delete-11.0 {
407 CREATE TABLE t11(a INTEGER PRIMARY KEY, b INT);
408 WITH RECURSIVE cnt(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM cnt WHERE x<20)
409 INSERT INTO t11(a,b) SELECT x, (x*17)%100 FROM cnt;
410 SELECT * FROM t11;
411} {1 17 2 34 3 51 4 68 5 85 6 2 7 19 8 36 9 53 10 70 11 87 12 4 13 21 14 38 15 55 16 72 17 89 18 6 19 23 20 40}
412do_execsql_test delete-11.1 {
413 DELETE FROM t11 AS xyz
414 WHERE EXISTS(SELECT 1 FROM t11 WHERE t11.a>xyz.a AND t11.b<=xyz.b);
415 SELECT * FROM t11;
416} {6 2 12 4 18 6 19 23 20 40}
417
danielk1977382e28f2007-10-05 15:53:29 +0000418
drh62c68192000-05-30 00:51:26 +0000419finish_test