3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
6 # 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.
10 #***********************************************************************
11 # This file implements regression tests for SQLite library. The
12 # focus of this file is testing the DELETE FROM statement.
14 # $Id: delete.test,v 1.26 2009/06/05 17:09:12 drh Exp $
16 set testdir [file dirname $argv0]
17 source $testdir/tester.tcl
19 # Try to delete from a non-existant table.
22 set v [catch {execsql {DELETE FROM test1}} msg]
24 } {1 {no such table: test1}}
26 # Try to delete from sqlite_master
29 set v [catch {execsql {DELETE FROM sqlite_master}} msg]
31 } {1 {table sqlite_master may not be modified}}
33 # Delete selected entries from a table with and without an index.
35 do_test delete-3.1.1 {
36 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}
43 do_test delete-3.1.2 {
44 execsql {DELETE FROM table1 WHERE f1=3}
46 do_test delete-3.1.3 {
47 execsql {SELECT * FROM table1 ORDER BY f1}
49 do_test delete-3.1.4 {
50 execsql {CREATE INDEX index1 ON table1(f1)}
51 execsql {PRAGMA count_changes=on}
53 execsql {EXPLAIN DELETE FROM table1 WHERE f1=3}
55 execsql {DELETE FROM 'table1' WHERE f1=3}
57 do_test delete-3.1.5 {
58 execsql {SELECT * FROM table1 ORDER BY f1}
60 do_test delete-3.1.6.1 {
61 execsql {DELETE FROM table1 WHERE f1=2}
63 do_test delete-3.1.6.2 {
66 do_test delete-3.1.7 {
67 execsql {SELECT * FROM table1 ORDER BY f1}
69 integrity_check delete-3.2
72 # Semantic errors in the WHERE clause
75 execsql {CREATE TABLE table2(f1 int, f2 int)}
76 set v [catch {execsql {DELETE FROM table2 WHERE f3=5}} msg]
78 } {1 {no such column: f3}}
81 set v [catch {execsql {DELETE FROM table2 WHERE xyzzy(f1+4)}} msg]
83 } {1 {no such function: xyzzy}}
84 integrity_check delete-4.3
88 do_test delete-5.1.1 {
89 execsql {DELETE FROM table1}
91 do_test delete-5.1.2 {
92 execsql {SELECT count(*) FROM table1}
94 do_test delete-5.2.1 {
95 execsql {BEGIN TRANSACTION}
96 for {set i 1} {$i<=200} {incr i} {
97 execsql "INSERT INTO table1 VALUES($i,[expr {$i*$i}])"
100 execsql {SELECT count(*) FROM table1}
102 do_test delete-5.2.2 {
103 execsql {DELETE FROM table1}
105 do_test delete-5.2.3 {
106 execsql {BEGIN TRANSACTION}
107 for {set i 1} {$i<=200} {incr i} {
108 execsql "INSERT INTO table1 VALUES($i,[expr {$i*$i}])"
111 execsql {SELECT count(*) FROM table1}
113 do_test delete-5.2.4 {
114 execsql {PRAGMA count_changes=off}
115 execsql {DELETE FROM table1}
117 do_test delete-5.2.5 {
118 execsql {SELECT count(*) FROM table1}
120 do_test delete-5.2.6 {
121 execsql {BEGIN TRANSACTION}
122 for {set i 1} {$i<=200} {incr i} {
123 execsql "INSERT INTO table1 VALUES($i,[expr {$i*$i}])"
126 execsql {SELECT count(*) FROM table1}
129 for {set i 1} {$i<=200} {incr i 4} {
130 execsql "DELETE FROM table1 WHERE f1==$i"
132 execsql {SELECT count(*) FROM table1}
134 do_test delete-5.4.1 {
135 execsql "DELETE FROM table1 WHERE f1>50"
137 } [db one {SELECT count(*) FROM table1 WHERE f1>50}]
138 do_test delete-5.4.2 {
139 execsql {SELECT count(*) FROM table1}
142 for {set i 1} {$i<=70} {incr i 3} {
143 execsql "DELETE FROM table1 WHERE f1==$i"
145 execsql {SELECT f1 FROM table1 ORDER BY f1}
146 } {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}
148 for {set i 1} {$i<40} {incr i} {
149 execsql "DELETE FROM table1 WHERE f1==$i"
151 execsql {SELECT f1 FROM table1 ORDER BY f1}
154 execsql "DELETE FROM table1 WHERE f1!=48"
155 execsql {SELECT f1 FROM table1 ORDER BY f1}
157 integrity_check delete-5.8
160 # Delete large quantities of data. We want to test the List overflow
161 # mechanism in the vdbe.
164 execsql {BEGIN; DELETE FROM table1}
165 for {set i 1} {$i<=3000} {incr i} {
166 execsql "INSERT INTO table1 VALUES($i,[expr {$i*$i}])"
168 execsql {DELETE FROM table2}
169 for {set i 1} {$i<=3000} {incr i} {
170 execsql "INSERT INTO table2 VALUES($i,[expr {$i*$i}])"
173 execsql {SELECT count(*) FROM table1}
176 execsql {SELECT count(*) FROM table2}
179 execsql {SELECT f1 FROM table1 WHERE f1<10 ORDER BY f1}
180 } {1 2 3 4 5 6 7 8 9}
182 execsql {SELECT f1 FROM table2 WHERE f1<10 ORDER BY f1}
183 } {1 2 3 4 5 6 7 8 9}
184 do_test delete-6.5.1 {
185 execsql {DELETE FROM table1 WHERE f1>7}
188 do_test delete-6.5.2 {
189 execsql {SELECT f1 FROM table1 ORDER BY f1}
192 execsql {DELETE FROM table2 WHERE f1>7}
193 execsql {SELECT f1 FROM table2 ORDER BY f1}
196 execsql {DELETE FROM table1}
197 execsql {SELECT f1 FROM table1}
200 execsql {INSERT INTO table1 VALUES(2,3)}
201 execsql {SELECT f1 FROM table1}
204 execsql {DELETE FROM table2}
205 execsql {SELECT f1 FROM table2}
207 do_test delete-6.10 {
208 execsql {INSERT INTO table2 VALUES(2,3)}
209 execsql {SELECT f1 FROM table2}
211 integrity_check delete-6.11
216 INSERT INTO t3 VALUES(1);
217 INSERT INTO t3 SELECT a+1 FROM t3;
218 INSERT INTO t3 SELECT a+2 FROM t3;
222 ifcapable {trigger} {
225 CREATE TABLE cnt(del);
226 INSERT INTO cnt VALUES(0);
227 CREATE TRIGGER r1 AFTER DELETE ON t3 FOR EACH ROW BEGIN
228 UPDATE cnt SET del=del+1;
230 DELETE FROM t3 WHERE a<2;
252 INSERT INTO t3 VALUES(1);
253 INSERT INTO t3 SELECT a+1 FROM t3;
254 INSERT INTO t3 SELECT a+2 FROM t3;
255 CREATE TABLE t4 AS SELECT * FROM t3;
256 PRAGMA count_changes=ON;
262 ifcapable {!trigger} {
263 execsql {DELETE FROM t3}
265 integrity_check delete-7.7
267 # Make sure error messages are consistent when attempting to delete
268 # from a read-only database. Ticket #304.
272 PRAGMA count_changes=OFF;
273 INSERT INTO t3 VALUES(123);
278 catch {file delete -force test.db-journal}
279 catch {file attributes test.db -permissions 0444}
280 catch {file attributes test.db -readonly 1}
282 set ::DB [sqlite3_connection_pointer db]
287 } {1 {attempt to write a readonly database}}
289 execsql {SELECT * FROM t3}
293 DELETE FROM t3 WHERE 1;
295 } {1 {attempt to write a readonly database}}
297 execsql {SELECT * FROM t3}
300 # Update for v3: In v2 the DELETE statement would succeed because no
301 # database writes actually occur. Version 3 refuses to open a transaction
302 # on a read-only file, so the statement fails.
305 DELETE FROM t3 WHERE a<100;
308 } {1 {attempt to write a readonly database}}
310 execsql {SELECT * FROM t3}
312 integrity_check delete-8.7
314 # Need to do the following for tcl 8.5 on mac. On that configuration, the
315 # -readonly flag is taken so seriously that a subsequent [file delete -force]
316 # (required before the next test file can be executed) will fail.
318 catch {file attributes test.db -readonly 0}
320 file delete -force test.db test.db-journal
322 # The following tests verify that SQLite correctly handles the case
323 # where an index B-Tree is being scanned, the rowid column being read
324 # from each index entry and another statement deletes some rows from
325 # the index B-Tree. At one point this (obscure) scenario was causing
326 # SQLite to return spurious SQLITE_CORRUPT errors and arguably incorrect
332 CREATE TABLE t5(a, b);
333 CREATE TABLE t6(c, d);
334 INSERT INTO t5 VALUES(1, 2);
335 INSERT INTO t5 VALUES(3, 4);
336 INSERT INTO t5 VALUES(5, 6);
337 INSERT INTO t6 VALUES('a', 'b');
338 INSERT INTO t6 VALUES('c', 'd');
339 CREATE INDEX i5 ON t5(a);
340 CREATE INDEX i6 ON t6(c);
345 db eval { SELECT t5.rowid AS r, c, d FROM t5, t6 ORDER BY a } {
346 if {$r==2} { db eval { DELETE FROM t5 } }
350 } {1 a b 1 c d 2 a b {} c d}
353 INSERT INTO t5 VALUES(1, 2);
354 INSERT INTO t5 VALUES(3, 4);
355 INSERT INTO t5 VALUES(5, 6);
358 db eval { SELECT t5.rowid AS r, c, d FROM t5, t6 ORDER BY a } {
359 if {$r==2} { db eval { DELETE FROM t5 WHERE rowid = 2 } }
363 } {1 a b 1 c d 2 a b {} c d 3 a b 3 c d}
367 INSERT INTO t5 VALUES(1, 2);
368 INSERT INTO t5 VALUES(3, 4);
369 INSERT INTO t5 VALUES(5, 6);
372 db eval { SELECT t5.rowid AS r, c, d FROM t5, t6 ORDER BY a } {
373 if {$r==2} { db eval { DELETE FROM t5 WHERE rowid = 1 } }
377 } {1 a b 1 c d 2 a b 2 c d 3 a b 3 c d}
381 INSERT INTO t5 VALUES(1, 2);
382 INSERT INTO t5 VALUES(3, 4);
383 INSERT INTO t5 VALUES(5, 6);
386 db eval { SELECT t5.rowid AS r, c, d FROM t5, t6 ORDER BY a } {
387 if {$r==2} { db eval { DELETE FROM t5 WHERE rowid = 3 } }
391 } {1 a b 1 c d 2 a b 2 c d}