2 #pragma ident "%Z%%M% %I% %E% SMI"
6 # The author disclaims copyright to this source code. In place of
7 # a legal notice, here is a blessing:
9 # May you do good and not evil.
10 # May you find forgiveness for yourself and forgive others.
11 # May you share freely, never taking more than you give.
13 #***********************************************************************
14 # This file implements regression tests for SQLite library. The
15 # focus of this file is testing the DELETE FROM statement.
17 # $Id: delete.test,v 1.13 2003/06/15 23:42:25 drh Exp $
19 set testdir [file dirname $argv0]
20 source $testdir/tester.tcl
22 # Try to delete from a non-existant table.
25 set v [catch {execsql {DELETE FROM test1}} msg]
27 } {1 {no such table: test1}}
29 # Try to delete from sqlite_master
32 set v [catch {execsql {DELETE FROM sqlite_master}} msg]
34 } {1 {table sqlite_master may not be modified}}
36 # Delete selected entries from a table with and without an index.
38 do_test delete-3.1.1 {
39 execsql {CREATE TABLE table1(f1 int, f2 int)}
40 execsql {INSERT INTO table1 VALUES(1,2)}
41 execsql {INSERT INTO table1 VALUES(2,4)}
42 execsql {INSERT INTO table1 VALUES(3,8)}
43 execsql {INSERT INTO table1 VALUES(4,16)}
44 execsql {SELECT * FROM table1 ORDER BY f1}
46 do_test delete-3.1.2 {
47 execsql {DELETE FROM table1 WHERE f1=3}
49 do_test delete-3.1.3 {
50 execsql {SELECT * FROM table1 ORDER BY f1}
52 do_test delete-3.1.4 {
53 execsql {CREATE INDEX index1 ON table1(f1)}
54 execsql {PRAGMA count_changes=on}
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 {
61 execsql {DELETE FROM table1 WHERE f1=2}
63 do_test delete-3.1.7 {
64 execsql {SELECT * FROM table1 ORDER BY f1}
66 integrity_check delete-3.2
69 # Semantic errors in the WHERE clause
72 execsql {CREATE TABLE table2(f1 int, f2 int)}
73 set v [catch {execsql {DELETE FROM table2 WHERE f3=5}} msg]
75 } {1 {no such column: f3}}
78 set v [catch {execsql {DELETE FROM table2 WHERE xyzzy(f1+4)}} msg]
80 } {1 {no such function: xyzzy}}
81 integrity_check delete-4.3
85 do_test delete-5.1.1 {
86 execsql {DELETE FROM table1}
88 do_test delete-5.1.2 {
89 execsql {SELECT count(*) FROM table1}
91 do_test delete-5.2.1 {
92 execsql {BEGIN TRANSACTION}
93 for {set i 1} {$i<=200} {incr i} {
94 execsql "INSERT INTO table1 VALUES($i,[expr {$i*$i}])"
97 execsql {SELECT count(*) FROM table1}
99 do_test delete-5.2.2 {
100 execsql {DELETE FROM table1}
102 do_test delete-5.2.3 {
103 execsql {BEGIN TRANSACTION}
104 for {set i 1} {$i<=200} {incr i} {
105 execsql "INSERT INTO table1 VALUES($i,[expr {$i*$i}])"
108 execsql {SELECT count(*) FROM table1}
110 do_test delete-5.2.4 {
111 execsql {PRAGMA count_changes=off}
112 execsql {DELETE FROM table1}
114 do_test delete-5.2.5 {
115 execsql {SELECT count(*) FROM table1}
117 do_test delete-5.2.6 {
118 execsql {BEGIN TRANSACTION}
119 for {set i 1} {$i<=200} {incr i} {
120 execsql "INSERT INTO table1 VALUES($i,[expr {$i*$i}])"
123 execsql {SELECT count(*) FROM table1}
126 for {set i 1} {$i<=200} {incr i 4} {
127 execsql "DELETE FROM table1 WHERE f1==$i"
129 execsql {SELECT count(*) FROM table1}
132 execsql "DELETE FROM table1 WHERE f1>50"
133 execsql {SELECT count(*) FROM table1}
136 for {set i 1} {$i<=70} {incr i 3} {
137 execsql "DELETE FROM table1 WHERE f1==$i"
139 execsql {SELECT f1 FROM table1 ORDER BY f1}
140 } {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}
142 for {set i 1} {$i<40} {incr i} {
143 execsql "DELETE FROM table1 WHERE f1==$i"
145 execsql {SELECT f1 FROM table1 ORDER BY f1}
148 execsql "DELETE FROM table1 WHERE f1!=48"
149 execsql {SELECT f1 FROM table1 ORDER BY f1}
151 integrity_check delete-5.8
154 # Delete large quantities of data. We want to test the List overflow
155 # mechanism in the vdbe.
158 set fd [open data1.txt w]
159 for {set i 1} {$i<=3000} {incr i} {
160 puts $fd "[expr {$i}]\t[expr {$i*$i}]"
163 execsql {DELETE FROM table1}
164 execsql {COPY table1 FROM 'data1.txt'}
165 execsql {DELETE FROM table2}
166 execsql {COPY table2 FROM 'data1.txt'}
167 file delete data1.txt
168 execsql {SELECT count(*) FROM table1}
171 execsql {SELECT count(*) FROM table2}
174 execsql {SELECT f1 FROM table1 WHERE f1<10 ORDER BY f1}
175 } {1 2 3 4 5 6 7 8 9}
177 execsql {SELECT f1 FROM table2 WHERE f1<10 ORDER BY f1}
178 } {1 2 3 4 5 6 7 8 9}
180 execsql {DELETE FROM table1 WHERE f1>7}
181 execsql {SELECT f1 FROM table1 ORDER BY f1}
184 execsql {DELETE FROM table2 WHERE f1>7}
185 execsql {SELECT f1 FROM table2 ORDER BY f1}
188 execsql {DELETE FROM table1}
189 execsql {SELECT f1 FROM table1}
192 execsql {INSERT INTO table1 VALUES(2,3)}
193 execsql {SELECT f1 FROM table1}
196 execsql {DELETE FROM table2}
197 execsql {SELECT f1 FROM table2}
199 do_test delete-6.10 {
200 execsql {INSERT INTO table2 VALUES(2,3)}
201 execsql {SELECT f1 FROM table2}
203 integrity_check delete-6.11
208 INSERT INTO t3 VALUES(1);
209 INSERT INTO t3 SELECT a+1 FROM t3;
210 INSERT INTO t3 SELECT a+2 FROM t3;
216 CREATE TABLE cnt(del);
217 INSERT INTO cnt VALUES(0);
218 CREATE TRIGGER r1 AFTER DELETE ON t3 FOR EACH ROW BEGIN
219 UPDATE cnt SET del=del+1;
221 DELETE FROM t3 WHERE a<2;
243 INSERT INTO t3 VALUES(1);
244 INSERT INTO t3 SELECT a+1 FROM t3;
245 INSERT INTO t3 SELECT a+2 FROM t3;
246 CREATE TABLE t4 AS SELECT * FROM t3;
247 PRAGMA count_changes=ON;
252 integrity_check delete-7.7
254 # Make sure error messages are consistent when attempting to delete
255 # from a read-only database. Ticket #304.
259 PRAGMA count_changes=OFF;
260 INSERT INTO t3 VALUES(123);
265 catch {file attributes test.db -permissions 0444}
266 catch {file attributes test.db -readonly 1}
272 } {1 {attempt to write a readonly database}}
274 execsql {SELECT * FROM t3}
278 DELETE FROM t3 WHERE 1;
280 } {1 {attempt to write a readonly database}}
282 execsql {SELECT * FROM t3}
286 DELETE FROM t3 WHERE a<100;
290 execsql {SELECT * FROM t3}
292 integrity_check delete-8.7