1*c5c4113dSnw141292 2*c5c4113dSnw141292#pragma ident "%Z%%M% %I% %E% SMI" 3*c5c4113dSnw141292 4*c5c4113dSnw141292# 2001 September 15 5*c5c4113dSnw141292# 6*c5c4113dSnw141292# The author disclaims copyright to this source code. In place of 7*c5c4113dSnw141292# a legal notice, here is a blessing: 8*c5c4113dSnw141292# 9*c5c4113dSnw141292# May you do good and not evil. 10*c5c4113dSnw141292# May you find forgiveness for yourself and forgive others. 11*c5c4113dSnw141292# May you share freely, never taking more than you give. 12*c5c4113dSnw141292# 13*c5c4113dSnw141292#*********************************************************************** 14*c5c4113dSnw141292# This file implements regression tests for SQLite library. The 15*c5c4113dSnw141292# focus of this file is testing the DELETE FROM statement. 16*c5c4113dSnw141292# 17*c5c4113dSnw141292# $Id: delete.test,v 1.13 2003/06/15 23:42:25 drh Exp $ 18*c5c4113dSnw141292 19*c5c4113dSnw141292set testdir [file dirname $argv0] 20*c5c4113dSnw141292source $testdir/tester.tcl 21*c5c4113dSnw141292 22*c5c4113dSnw141292# Try to delete from a non-existant table. 23*c5c4113dSnw141292# 24*c5c4113dSnw141292do_test delete-1.1 { 25*c5c4113dSnw141292 set v [catch {execsql {DELETE FROM test1}} msg] 26*c5c4113dSnw141292 lappend v $msg 27*c5c4113dSnw141292} {1 {no such table: test1}} 28*c5c4113dSnw141292 29*c5c4113dSnw141292# Try to delete from sqlite_master 30*c5c4113dSnw141292# 31*c5c4113dSnw141292do_test delete-2.1 { 32*c5c4113dSnw141292 set v [catch {execsql {DELETE FROM sqlite_master}} msg] 33*c5c4113dSnw141292 lappend v $msg 34*c5c4113dSnw141292} {1 {table sqlite_master may not be modified}} 35*c5c4113dSnw141292 36*c5c4113dSnw141292# Delete selected entries from a table with and without an index. 37*c5c4113dSnw141292# 38*c5c4113dSnw141292do_test delete-3.1.1 { 39*c5c4113dSnw141292 execsql {CREATE TABLE table1(f1 int, f2 int)} 40*c5c4113dSnw141292 execsql {INSERT INTO table1 VALUES(1,2)} 41*c5c4113dSnw141292 execsql {INSERT INTO table1 VALUES(2,4)} 42*c5c4113dSnw141292 execsql {INSERT INTO table1 VALUES(3,8)} 43*c5c4113dSnw141292 execsql {INSERT INTO table1 VALUES(4,16)} 44*c5c4113dSnw141292 execsql {SELECT * FROM table1 ORDER BY f1} 45*c5c4113dSnw141292} {1 2 2 4 3 8 4 16} 46*c5c4113dSnw141292do_test delete-3.1.2 { 47*c5c4113dSnw141292 execsql {DELETE FROM table1 WHERE f1=3} 48*c5c4113dSnw141292} {} 49*c5c4113dSnw141292do_test delete-3.1.3 { 50*c5c4113dSnw141292 execsql {SELECT * FROM table1 ORDER BY f1} 51*c5c4113dSnw141292} {1 2 2 4 4 16} 52*c5c4113dSnw141292do_test delete-3.1.4 { 53*c5c4113dSnw141292 execsql {CREATE INDEX index1 ON table1(f1)} 54*c5c4113dSnw141292 execsql {PRAGMA count_changes=on} 55*c5c4113dSnw141292 execsql {DELETE FROM 'table1' WHERE f1=3} 56*c5c4113dSnw141292} {0} 57*c5c4113dSnw141292do_test delete-3.1.5 { 58*c5c4113dSnw141292 execsql {SELECT * FROM table1 ORDER BY f1} 59*c5c4113dSnw141292} {1 2 2 4 4 16} 60*c5c4113dSnw141292do_test delete-3.1.6 { 61*c5c4113dSnw141292 execsql {DELETE FROM table1 WHERE f1=2} 62*c5c4113dSnw141292} {1} 63*c5c4113dSnw141292do_test delete-3.1.7 { 64*c5c4113dSnw141292 execsql {SELECT * FROM table1 ORDER BY f1} 65*c5c4113dSnw141292} {1 2 4 16} 66*c5c4113dSnw141292integrity_check delete-3.2 67*c5c4113dSnw141292 68*c5c4113dSnw141292 69*c5c4113dSnw141292# Semantic errors in the WHERE clause 70*c5c4113dSnw141292# 71*c5c4113dSnw141292do_test delete-4.1 { 72*c5c4113dSnw141292 execsql {CREATE TABLE table2(f1 int, f2 int)} 73*c5c4113dSnw141292 set v [catch {execsql {DELETE FROM table2 WHERE f3=5}} msg] 74*c5c4113dSnw141292 lappend v $msg 75*c5c4113dSnw141292} {1 {no such column: f3}} 76*c5c4113dSnw141292 77*c5c4113dSnw141292do_test delete-4.2 { 78*c5c4113dSnw141292 set v [catch {execsql {DELETE FROM table2 WHERE xyzzy(f1+4)}} msg] 79*c5c4113dSnw141292 lappend v $msg 80*c5c4113dSnw141292} {1 {no such function: xyzzy}} 81*c5c4113dSnw141292integrity_check delete-4.3 82*c5c4113dSnw141292 83*c5c4113dSnw141292# Lots of deletes 84*c5c4113dSnw141292# 85*c5c4113dSnw141292do_test delete-5.1.1 { 86*c5c4113dSnw141292 execsql {DELETE FROM table1} 87*c5c4113dSnw141292} {2} 88*c5c4113dSnw141292do_test delete-5.1.2 { 89*c5c4113dSnw141292 execsql {SELECT count(*) FROM table1} 90*c5c4113dSnw141292} {0} 91*c5c4113dSnw141292do_test delete-5.2.1 { 92*c5c4113dSnw141292 execsql {BEGIN TRANSACTION} 93*c5c4113dSnw141292 for {set i 1} {$i<=200} {incr i} { 94*c5c4113dSnw141292 execsql "INSERT INTO table1 VALUES($i,[expr {$i*$i}])" 95*c5c4113dSnw141292 } 96*c5c4113dSnw141292 execsql {COMMIT} 97*c5c4113dSnw141292 execsql {SELECT count(*) FROM table1} 98*c5c4113dSnw141292} {200} 99*c5c4113dSnw141292do_test delete-5.2.2 { 100*c5c4113dSnw141292 execsql {DELETE FROM table1} 101*c5c4113dSnw141292} {200} 102*c5c4113dSnw141292do_test delete-5.2.3 { 103*c5c4113dSnw141292 execsql {BEGIN TRANSACTION} 104*c5c4113dSnw141292 for {set i 1} {$i<=200} {incr i} { 105*c5c4113dSnw141292 execsql "INSERT INTO table1 VALUES($i,[expr {$i*$i}])" 106*c5c4113dSnw141292 } 107*c5c4113dSnw141292 execsql {COMMIT} 108*c5c4113dSnw141292 execsql {SELECT count(*) FROM table1} 109*c5c4113dSnw141292} {200} 110*c5c4113dSnw141292do_test delete-5.2.4 { 111*c5c4113dSnw141292 execsql {PRAGMA count_changes=off} 112*c5c4113dSnw141292 execsql {DELETE FROM table1} 113*c5c4113dSnw141292} {} 114*c5c4113dSnw141292do_test delete-5.2.5 { 115*c5c4113dSnw141292 execsql {SELECT count(*) FROM table1} 116*c5c4113dSnw141292} {0} 117*c5c4113dSnw141292do_test delete-5.2.6 { 118*c5c4113dSnw141292 execsql {BEGIN TRANSACTION} 119*c5c4113dSnw141292 for {set i 1} {$i<=200} {incr i} { 120*c5c4113dSnw141292 execsql "INSERT INTO table1 VALUES($i,[expr {$i*$i}])" 121*c5c4113dSnw141292 } 122*c5c4113dSnw141292 execsql {COMMIT} 123*c5c4113dSnw141292 execsql {SELECT count(*) FROM table1} 124*c5c4113dSnw141292} {200} 125*c5c4113dSnw141292do_test delete-5.3 { 126*c5c4113dSnw141292 for {set i 1} {$i<=200} {incr i 4} { 127*c5c4113dSnw141292 execsql "DELETE FROM table1 WHERE f1==$i" 128*c5c4113dSnw141292 } 129*c5c4113dSnw141292 execsql {SELECT count(*) FROM table1} 130*c5c4113dSnw141292} {150} 131*c5c4113dSnw141292do_test delete-5.4 { 132*c5c4113dSnw141292 execsql "DELETE FROM table1 WHERE f1>50" 133*c5c4113dSnw141292 execsql {SELECT count(*) FROM table1} 134*c5c4113dSnw141292} {37} 135*c5c4113dSnw141292do_test delete-5.5 { 136*c5c4113dSnw141292 for {set i 1} {$i<=70} {incr i 3} { 137*c5c4113dSnw141292 execsql "DELETE FROM table1 WHERE f1==$i" 138*c5c4113dSnw141292 } 139*c5c4113dSnw141292 execsql {SELECT f1 FROM table1 ORDER BY f1} 140*c5c4113dSnw141292} {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} 141*c5c4113dSnw141292do_test delete-5.6 { 142*c5c4113dSnw141292 for {set i 1} {$i<40} {incr i} { 143*c5c4113dSnw141292 execsql "DELETE FROM table1 WHERE f1==$i" 144*c5c4113dSnw141292 } 145*c5c4113dSnw141292 execsql {SELECT f1 FROM table1 ORDER BY f1} 146*c5c4113dSnw141292} {42 44 47 48 50} 147*c5c4113dSnw141292do_test delete-5.7 { 148*c5c4113dSnw141292 execsql "DELETE FROM table1 WHERE f1!=48" 149*c5c4113dSnw141292 execsql {SELECT f1 FROM table1 ORDER BY f1} 150*c5c4113dSnw141292} {48} 151*c5c4113dSnw141292integrity_check delete-5.8 152*c5c4113dSnw141292 153*c5c4113dSnw141292 154*c5c4113dSnw141292# Delete large quantities of data. We want to test the List overflow 155*c5c4113dSnw141292# mechanism in the vdbe. 156*c5c4113dSnw141292# 157*c5c4113dSnw141292do_test delete-6.1 { 158*c5c4113dSnw141292 set fd [open data1.txt w] 159*c5c4113dSnw141292 for {set i 1} {$i<=3000} {incr i} { 160*c5c4113dSnw141292 puts $fd "[expr {$i}]\t[expr {$i*$i}]" 161*c5c4113dSnw141292 } 162*c5c4113dSnw141292 close $fd 163*c5c4113dSnw141292 execsql {DELETE FROM table1} 164*c5c4113dSnw141292 execsql {COPY table1 FROM 'data1.txt'} 165*c5c4113dSnw141292 execsql {DELETE FROM table2} 166*c5c4113dSnw141292 execsql {COPY table2 FROM 'data1.txt'} 167*c5c4113dSnw141292 file delete data1.txt 168*c5c4113dSnw141292 execsql {SELECT count(*) FROM table1} 169*c5c4113dSnw141292} {3000} 170*c5c4113dSnw141292do_test delete-6.2 { 171*c5c4113dSnw141292 execsql {SELECT count(*) FROM table2} 172*c5c4113dSnw141292} {3000} 173*c5c4113dSnw141292do_test delete-6.3 { 174*c5c4113dSnw141292 execsql {SELECT f1 FROM table1 WHERE f1<10 ORDER BY f1} 175*c5c4113dSnw141292} {1 2 3 4 5 6 7 8 9} 176*c5c4113dSnw141292do_test delete-6.4 { 177*c5c4113dSnw141292 execsql {SELECT f1 FROM table2 WHERE f1<10 ORDER BY f1} 178*c5c4113dSnw141292} {1 2 3 4 5 6 7 8 9} 179*c5c4113dSnw141292do_test delete-6.5 { 180*c5c4113dSnw141292 execsql {DELETE FROM table1 WHERE f1>7} 181*c5c4113dSnw141292 execsql {SELECT f1 FROM table1 ORDER BY f1} 182*c5c4113dSnw141292} {1 2 3 4 5 6 7} 183*c5c4113dSnw141292do_test delete-6.6 { 184*c5c4113dSnw141292 execsql {DELETE FROM table2 WHERE f1>7} 185*c5c4113dSnw141292 execsql {SELECT f1 FROM table2 ORDER BY f1} 186*c5c4113dSnw141292} {1 2 3 4 5 6 7} 187*c5c4113dSnw141292do_test delete-6.7 { 188*c5c4113dSnw141292 execsql {DELETE FROM table1} 189*c5c4113dSnw141292 execsql {SELECT f1 FROM table1} 190*c5c4113dSnw141292} {} 191*c5c4113dSnw141292do_test delete-6.8 { 192*c5c4113dSnw141292 execsql {INSERT INTO table1 VALUES(2,3)} 193*c5c4113dSnw141292 execsql {SELECT f1 FROM table1} 194*c5c4113dSnw141292} {2} 195*c5c4113dSnw141292do_test delete-6.9 { 196*c5c4113dSnw141292 execsql {DELETE FROM table2} 197*c5c4113dSnw141292 execsql {SELECT f1 FROM table2} 198*c5c4113dSnw141292} {} 199*c5c4113dSnw141292do_test delete-6.10 { 200*c5c4113dSnw141292 execsql {INSERT INTO table2 VALUES(2,3)} 201*c5c4113dSnw141292 execsql {SELECT f1 FROM table2} 202*c5c4113dSnw141292} {2} 203*c5c4113dSnw141292integrity_check delete-6.11 204*c5c4113dSnw141292 205*c5c4113dSnw141292do_test delete-7.1 { 206*c5c4113dSnw141292 execsql { 207*c5c4113dSnw141292 CREATE TABLE t3(a); 208*c5c4113dSnw141292 INSERT INTO t3 VALUES(1); 209*c5c4113dSnw141292 INSERT INTO t3 SELECT a+1 FROM t3; 210*c5c4113dSnw141292 INSERT INTO t3 SELECT a+2 FROM t3; 211*c5c4113dSnw141292 SELECT * FROM t3; 212*c5c4113dSnw141292 } 213*c5c4113dSnw141292} {1 2 3 4} 214*c5c4113dSnw141292do_test delete-7.2 { 215*c5c4113dSnw141292 execsql { 216*c5c4113dSnw141292 CREATE TABLE cnt(del); 217*c5c4113dSnw141292 INSERT INTO cnt VALUES(0); 218*c5c4113dSnw141292 CREATE TRIGGER r1 AFTER DELETE ON t3 FOR EACH ROW BEGIN 219*c5c4113dSnw141292 UPDATE cnt SET del=del+1; 220*c5c4113dSnw141292 END; 221*c5c4113dSnw141292 DELETE FROM t3 WHERE a<2; 222*c5c4113dSnw141292 SELECT * FROM t3; 223*c5c4113dSnw141292 } 224*c5c4113dSnw141292} {2 3 4} 225*c5c4113dSnw141292do_test delete-7.3 { 226*c5c4113dSnw141292 execsql { 227*c5c4113dSnw141292 SELECT * FROM cnt; 228*c5c4113dSnw141292 } 229*c5c4113dSnw141292} {1} 230*c5c4113dSnw141292do_test delete-7.4 { 231*c5c4113dSnw141292 execsql { 232*c5c4113dSnw141292 DELETE FROM t3; 233*c5c4113dSnw141292 SELECT * FROM t3; 234*c5c4113dSnw141292 } 235*c5c4113dSnw141292} {} 236*c5c4113dSnw141292do_test delete-7.5 { 237*c5c4113dSnw141292 execsql { 238*c5c4113dSnw141292 SELECT * FROM cnt; 239*c5c4113dSnw141292 } 240*c5c4113dSnw141292} {4} 241*c5c4113dSnw141292do_test delete-7.6 { 242*c5c4113dSnw141292 execsql { 243*c5c4113dSnw141292 INSERT INTO t3 VALUES(1); 244*c5c4113dSnw141292 INSERT INTO t3 SELECT a+1 FROM t3; 245*c5c4113dSnw141292 INSERT INTO t3 SELECT a+2 FROM t3; 246*c5c4113dSnw141292 CREATE TABLE t4 AS SELECT * FROM t3; 247*c5c4113dSnw141292 PRAGMA count_changes=ON; 248*c5c4113dSnw141292 DELETE FROM t3; 249*c5c4113dSnw141292 DELETE FROM t4; 250*c5c4113dSnw141292 } 251*c5c4113dSnw141292} {4 4} 252*c5c4113dSnw141292integrity_check delete-7.7 253*c5c4113dSnw141292 254*c5c4113dSnw141292# Make sure error messages are consistent when attempting to delete 255*c5c4113dSnw141292# from a read-only database. Ticket #304. 256*c5c4113dSnw141292# 257*c5c4113dSnw141292do_test delete-8.0 { 258*c5c4113dSnw141292 execsql { 259*c5c4113dSnw141292 PRAGMA count_changes=OFF; 260*c5c4113dSnw141292 INSERT INTO t3 VALUES(123); 261*c5c4113dSnw141292 SELECT * FROM t3; 262*c5c4113dSnw141292 } 263*c5c4113dSnw141292} {123} 264*c5c4113dSnw141292db close 265*c5c4113dSnw141292catch {file attributes test.db -permissions 0444} 266*c5c4113dSnw141292catch {file attributes test.db -readonly 1} 267*c5c4113dSnw141292sqlite db test.db 268*c5c4113dSnw141292do_test delete-8.1 { 269*c5c4113dSnw141292 catchsql { 270*c5c4113dSnw141292 DELETE FROM t3; 271*c5c4113dSnw141292 } 272*c5c4113dSnw141292} {1 {attempt to write a readonly database}} 273*c5c4113dSnw141292do_test delete-8.2 { 274*c5c4113dSnw141292 execsql {SELECT * FROM t3} 275*c5c4113dSnw141292} {123} 276*c5c4113dSnw141292do_test delete-8.3 { 277*c5c4113dSnw141292 catchsql { 278*c5c4113dSnw141292 DELETE FROM t3 WHERE 1; 279*c5c4113dSnw141292 } 280*c5c4113dSnw141292} {1 {attempt to write a readonly database}} 281*c5c4113dSnw141292do_test delete-8.4 { 282*c5c4113dSnw141292 execsql {SELECT * FROM t3} 283*c5c4113dSnw141292} {123} 284*c5c4113dSnw141292do_test delete-8.5 { 285*c5c4113dSnw141292 catchsql { 286*c5c4113dSnw141292 DELETE FROM t3 WHERE a<100; 287*c5c4113dSnw141292 } 288*c5c4113dSnw141292} {0 {}} 289*c5c4113dSnw141292do_test delete-8.6 { 290*c5c4113dSnw141292 execsql {SELECT * FROM t3} 291*c5c4113dSnw141292} {123} 292*c5c4113dSnw141292integrity_check delete-8.7 293*c5c4113dSnw141292 294*c5c4113dSnw141292finish_test 295