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
71 # Semantic errors in the WHERE clause
74 execsql {CREATE TABLE table2(f1 int, f2 int)}
75 set v [catch {execsql {DELETE FROM table2 WHERE f3=5}} msg]
77 } {1 {no such column: f3}}
80 set v [catch {execsql {DELETE FROM table2 WHERE xyzzy(f1+4)}} msg]
82 } {1 {no such function: xyzzy}}
83 integrity_check delete-4.3
87 do_test delete-5.1.1 {
88 execsql {DELETE FROM table1}
90 do_test delete-5.1.2 {
91 execsql {SELECT count(*) FROM table1}
93 do_test delete-5.2.1 {
94 execsql {BEGIN TRANSACTION}
95 for {set i 1} {$i<=200} {incr i} {
96 execsql "INSERT INTO table1 VALUES($i,[expr {$i*$i}])"
99 execsql {SELECT count(*) FROM table1}
101 do_test delete-5.2.2 {
102 execsql {DELETE FROM table1}
104 do_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}])"
110 execsql {SELECT count(*) FROM table1}
112 do_test delete-5.2.4 {
113 execsql {PRAGMA count_changes=off}
114 execsql {DELETE FROM table1}
116 do_test delete-5.2.5 {
117 execsql {SELECT count(*) FROM table1}
119 do_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}])"
125 execsql {SELECT count(*) FROM table1}
128 for {set i 1} {$i<=200} {incr i 4} {
129 execsql "DELETE FROM table1 WHERE f1==$i"
131 execsql {SELECT count(*) FROM table1}
133 do_test delete-5.4.1 {
134 execsql "DELETE FROM table1 WHERE f1>50"
136 } [db one {SELECT count(*) FROM table1 WHERE f1>50}]
137 do_test delete-5.4.2 {
138 execsql {SELECT count(*) FROM table1}
141 for {set i 1} {$i<=70} {incr i 3} {
142 execsql "DELETE FROM table1 WHERE f1==$i"
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}
147 for {set i 1} {$i<40} {incr i} {
148 execsql "DELETE FROM table1 WHERE f1==$i"
150 execsql {SELECT f1 FROM table1 ORDER BY f1}
153 execsql "DELETE FROM table1 WHERE f1!=48"
154 execsql {SELECT f1 FROM table1 ORDER BY f1}
156 integrity_check delete-5.8
159 # Delete large quantities of data. We want to test the List overflow
160 # mechanism in the vdbe.
163 execsql {BEGIN; DELETE FROM table1}
164 for {set i 1} {$i<=3000} {incr i} {
165 execsql "INSERT INTO table1 VALUES($i,[expr {$i*$i}])"
167 execsql {DELETE FROM table2}
168 for {set i 1} {$i<=3000} {incr i} {
169 execsql "INSERT INTO table2 VALUES($i,[expr {$i*$i}])"
172 execsql {SELECT count(*) FROM table1}
175 execsql {SELECT count(*) FROM table2}
178 execsql {SELECT f1 FROM table1 WHERE f1<10 ORDER BY f1}
179 } {1 2 3 4 5 6 7 8 9}
181 execsql {SELECT f1 FROM table2 WHERE f1<10 ORDER BY f1}
182 } {1 2 3 4 5 6 7 8 9}
183 do_test delete-6.5.1 {
184 execsql {DELETE FROM table1 WHERE f1>7}
187 do_test delete-6.5.2 {
188 execsql {SELECT f1 FROM table1 ORDER BY f1}
191 execsql {DELETE FROM table2 WHERE f1>7}
192 execsql {SELECT f1 FROM table2 ORDER BY f1}
195 execsql {DELETE FROM table1}
196 execsql {SELECT f1 FROM table1}
199 execsql {INSERT INTO table1 VALUES(2,3)}
200 execsql {SELECT f1 FROM table1}
203 execsql {DELETE FROM table2}
204 execsql {SELECT f1 FROM table2}
206 do_test delete-6.10 {
207 execsql {INSERT INTO table2 VALUES(2,3)}
208 execsql {SELECT f1 FROM table2}
210 integrity_check delete-6.11
215 INSERT INTO t3 VALUES(1);
216 INSERT INTO t3 SELECT a+1 FROM t3;
217 INSERT INTO t3 SELECT a+2 FROM t3;
221 ifcapable {trigger} {
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;
229 DELETE FROM t3 WHERE a<2;
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;
261 ifcapable {!trigger} {
262 execsql {DELETE FROM t3}
264 integrity_check delete-7.7
266 # Make sure error messages are consistent when attempting to delete
267 # from a read-only database. Ticket #304.
271 PRAGMA count_changes=OFF;
272 INSERT INTO t3 VALUES(123);
277 catch {forcedelete test.db-journal}
278 catch {file attributes test.db -permissions 0444}
279 catch {file attributes test.db -readonly 1}
281 set ::DB [sqlite3_connection_pointer db]
286 } {1 {attempt to write a readonly database}}
288 execsql {SELECT * FROM t3}
292 DELETE FROM t3 WHERE 1;
294 } {1 {attempt to write a readonly database}}
296 execsql {SELECT * FROM t3}
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.
304 DELETE FROM t3 WHERE a<100;
307 } {1 {attempt to write a readonly database}}
309 execsql {SELECT * FROM t3}
311 integrity_check delete-8.7
313 # Need to do the following for tcl 8.5 on mac. On that configuration, the
314 # -readonly flag is taken so seriously that a subsequent [forcedelete]
315 # (required before the next test file can be executed) will fail.
317 catch {file attributes test.db -readonly 0}
319 forcedelete test.db test.db-journal
321 # 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
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');
338 CREATE INDEX i5 ON t5(a);
339 CREATE INDEX i6 ON t6(c);
344 db eval { SELECT t5.rowid AS r, c, d FROM t5, t6 ORDER BY a } {
345 if {$r==2} { db eval { DELETE FROM t5 } }
349 } {1 a b 1 c d 2 a b {} c d}
352 INSERT INTO t5 VALUES(1, 2);
353 INSERT INTO t5 VALUES(3, 4);
354 INSERT INTO t5 VALUES(5, 6);
357 db eval { SELECT t5.rowid AS r, c, d FROM t5, t6 ORDER BY a } {
358 if {$r==2} { db eval { DELETE FROM t5 WHERE rowid = 2 } }
362 } {1 a b 1 c d 2 a b {} c d 3 a b 3 c d}
366 INSERT INTO t5 VALUES(1, 2);
367 INSERT INTO t5 VALUES(3, 4);
368 INSERT INTO t5 VALUES(5, 6);
371 db eval { SELECT t5.rowid AS r, c, d FROM t5, t6 ORDER BY a } {
372 if {$r==2} { db eval { DELETE FROM t5 WHERE rowid = 1 } }
376 } {1 a b 1 c d 2 a b 2 c d 3 a b 3 c d}
380 INSERT INTO t5 VALUES(1, 2);
381 INSERT INTO t5 VALUES(3, 4);
382 INSERT INTO t5 VALUES(5, 6);
385 db eval { SELECT t5.rowid AS r, c, d FROM t5, t6 ORDER BY a } {
386 if {$r==2} { db eval { DELETE FROM t5 WHERE rowid = 3 } }
390 } {1 a b 1 c d 2 a b 2 c d}
392 do_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';
398 do_execsql_test delete-10.1 {
399 DELETE FROM t1 WHERE a='1' AND b='2';
402 do_execsql_test delete-10.2 {
403 SELECT * FROM t1 WHERE a='1' AND b='2';