delete.test 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. # 2001 September 15
  2. #
  3. # The author disclaims copyright to this source code. In place of
  4. # a legal notice, here is a blessing:
  5. #
  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.
  9. #
  10. #***********************************************************************
  11. # This file implements regression tests for SQLite library. The
  12. # focus of this file is testing the DELETE FROM statement.
  13. #
  14. # $Id: delete.test,v 1.26 2009/06/05 17:09:12 drh Exp $
  15. set testdir [file dirname $argv0]
  16. source $testdir/tester.tcl
  17. # Try to delete from a non-existant table.
  18. #
  19. do_test delete-1.1 {
  20. set v [catch {execsql {DELETE FROM test1}} msg]
  21. lappend v $msg
  22. } {1 {no such table: test1}}
  23. # Try to delete from sqlite_master
  24. #
  25. do_test delete-2.1 {
  26. set v [catch {execsql {DELETE FROM sqlite_master}} msg]
  27. lappend v $msg
  28. } {1 {table sqlite_master may not be modified}}
  29. # Delete selected entries from a table with and without an index.
  30. #
  31. do_test delete-3.1.1 {
  32. execsql {CREATE TABLE table1(f1 int, f2 int)}
  33. execsql {INSERT INTO table1 VALUES(1,2)}
  34. execsql {INSERT INTO table1 VALUES(2,4)}
  35. execsql {INSERT INTO table1 VALUES(3,8)}
  36. execsql {INSERT INTO table1 VALUES(4,16)}
  37. execsql {SELECT * FROM table1 ORDER BY f1}
  38. } {1 2 2 4 3 8 4 16}
  39. do_test delete-3.1.2 {
  40. execsql {DELETE FROM table1 WHERE f1=3}
  41. } {}
  42. do_test delete-3.1.3 {
  43. execsql {SELECT * FROM table1 ORDER BY f1}
  44. } {1 2 2 4 4 16}
  45. do_test delete-3.1.4 {
  46. execsql {CREATE INDEX index1 ON table1(f1)}
  47. execsql {PRAGMA count_changes=on}
  48. ifcapable explain {
  49. execsql {EXPLAIN DELETE FROM table1 WHERE f1=3}
  50. }
  51. execsql {DELETE FROM 'table1' WHERE f1=3}
  52. } {0}
  53. do_test delete-3.1.5 {
  54. execsql {SELECT * FROM table1 ORDER BY f1}
  55. } {1 2 2 4 4 16}
  56. do_test delete-3.1.6.1 {
  57. execsql {DELETE FROM table1 WHERE f1=2}
  58. } {1}
  59. do_test delete-3.1.6.2 {
  60. db changes
  61. } 1
  62. do_test delete-3.1.7 {
  63. execsql {SELECT * FROM table1 ORDER BY f1}
  64. } {1 2 4 16}
  65. integrity_check delete-3.2
  66. # Semantic errors in the WHERE clause
  67. #
  68. do_test delete-4.1 {
  69. execsql {CREATE TABLE table2(f1 int, f2 int)}
  70. set v [catch {execsql {DELETE FROM table2 WHERE f3=5}} msg]
  71. lappend v $msg
  72. } {1 {no such column: f3}}
  73. do_test delete-4.2 {
  74. set v [catch {execsql {DELETE FROM table2 WHERE xyzzy(f1+4)}} msg]
  75. lappend v $msg
  76. } {1 {no such function: xyzzy}}
  77. integrity_check delete-4.3
  78. # Lots of deletes
  79. #
  80. do_test delete-5.1.1 {
  81. execsql {DELETE FROM table1}
  82. } {2}
  83. do_test delete-5.1.2 {
  84. execsql {SELECT count(*) FROM table1}
  85. } {0}
  86. do_test delete-5.2.1 {
  87. execsql {BEGIN TRANSACTION}
  88. for {set i 1} {$i<=200} {incr i} {
  89. execsql "INSERT INTO table1 VALUES($i,[expr {$i*$i}])"
  90. }
  91. execsql {COMMIT}
  92. execsql {SELECT count(*) FROM table1}
  93. } {200}
  94. do_test delete-5.2.2 {
  95. execsql {DELETE FROM table1}
  96. } {200}
  97. do_test delete-5.2.3 {
  98. execsql {BEGIN TRANSACTION}
  99. for {set i 1} {$i<=200} {incr i} {
  100. execsql "INSERT INTO table1 VALUES($i,[expr {$i*$i}])"
  101. }
  102. execsql {COMMIT}
  103. execsql {SELECT count(*) FROM table1}
  104. } {200}
  105. do_test delete-5.2.4 {
  106. execsql {PRAGMA count_changes=off}
  107. execsql {DELETE FROM table1}
  108. } {}
  109. do_test delete-5.2.5 {
  110. execsql {SELECT count(*) FROM table1}
  111. } {0}
  112. do_test delete-5.2.6 {
  113. execsql {BEGIN TRANSACTION}
  114. for {set i 1} {$i<=200} {incr i} {
  115. execsql "INSERT INTO table1 VALUES($i,[expr {$i*$i}])"
  116. }
  117. execsql {COMMIT}
  118. execsql {SELECT count(*) FROM table1}
  119. } {200}
  120. do_test delete-5.3 {
  121. for {set i 1} {$i<=200} {incr i 4} {
  122. execsql "DELETE FROM table1 WHERE f1==$i"
  123. }
  124. execsql {SELECT count(*) FROM table1}
  125. } {150}
  126. do_test delete-5.4.1 {
  127. execsql "DELETE FROM table1 WHERE f1>50"
  128. db changes
  129. } [db one {SELECT count(*) FROM table1 WHERE f1>50}]
  130. do_test delete-5.4.2 {
  131. execsql {SELECT count(*) FROM table1}
  132. } {37}
  133. do_test delete-5.5 {
  134. for {set i 1} {$i<=70} {incr i 3} {
  135. execsql "DELETE FROM table1 WHERE f1==$i"
  136. }
  137. execsql {SELECT f1 FROM table1 ORDER BY f1}
  138. } {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}
  139. do_test delete-5.6 {
  140. for {set i 1} {$i<40} {incr i} {
  141. execsql "DELETE FROM table1 WHERE f1==$i"
  142. }
  143. execsql {SELECT f1 FROM table1 ORDER BY f1}
  144. } {42 44 47 48 50}
  145. do_test delete-5.7 {
  146. execsql "DELETE FROM table1 WHERE f1!=48"
  147. execsql {SELECT f1 FROM table1 ORDER BY f1}
  148. } {48}
  149. integrity_check delete-5.8
  150. # Delete large quantities of data. We want to test the List overflow
  151. # mechanism in the vdbe.
  152. #
  153. do_test delete-6.1 {
  154. execsql {BEGIN; DELETE FROM table1}
  155. for {set i 1} {$i<=3000} {incr i} {
  156. execsql "INSERT INTO table1 VALUES($i,[expr {$i*$i}])"
  157. }
  158. execsql {DELETE FROM table2}
  159. for {set i 1} {$i<=3000} {incr i} {
  160. execsql "INSERT INTO table2 VALUES($i,[expr {$i*$i}])"
  161. }
  162. execsql {COMMIT}
  163. execsql {SELECT count(*) FROM table1}
  164. } {3000}
  165. do_test delete-6.2 {
  166. execsql {SELECT count(*) FROM table2}
  167. } {3000}
  168. do_test delete-6.3 {
  169. execsql {SELECT f1 FROM table1 WHERE f1<10 ORDER BY f1}
  170. } {1 2 3 4 5 6 7 8 9}
  171. do_test delete-6.4 {
  172. execsql {SELECT f1 FROM table2 WHERE f1<10 ORDER BY f1}
  173. } {1 2 3 4 5 6 7 8 9}
  174. do_test delete-6.5.1 {
  175. execsql {DELETE FROM table1 WHERE f1>7}
  176. db changes
  177. } {2993}
  178. do_test delete-6.5.2 {
  179. execsql {SELECT f1 FROM table1 ORDER BY f1}
  180. } {1 2 3 4 5 6 7}
  181. do_test delete-6.6 {
  182. execsql {DELETE FROM table2 WHERE f1>7}
  183. execsql {SELECT f1 FROM table2 ORDER BY f1}
  184. } {1 2 3 4 5 6 7}
  185. do_test delete-6.7 {
  186. execsql {DELETE FROM table1}
  187. execsql {SELECT f1 FROM table1}
  188. } {}
  189. do_test delete-6.8 {
  190. execsql {INSERT INTO table1 VALUES(2,3)}
  191. execsql {SELECT f1 FROM table1}
  192. } {2}
  193. do_test delete-6.9 {
  194. execsql {DELETE FROM table2}
  195. execsql {SELECT f1 FROM table2}
  196. } {}
  197. do_test delete-6.10 {
  198. execsql {INSERT INTO table2 VALUES(2,3)}
  199. execsql {SELECT f1 FROM table2}
  200. } {2}
  201. integrity_check delete-6.11
  202. do_test delete-7.1 {
  203. execsql {
  204. CREATE TABLE t3(a);
  205. INSERT INTO t3 VALUES(1);
  206. INSERT INTO t3 SELECT a+1 FROM t3;
  207. INSERT INTO t3 SELECT a+2 FROM t3;
  208. SELECT * FROM t3;
  209. }
  210. } {1 2 3 4}
  211. ifcapable {trigger} {
  212. do_test delete-7.2 {
  213. execsql {
  214. CREATE TABLE cnt(del);
  215. INSERT INTO cnt VALUES(0);
  216. CREATE TRIGGER r1 AFTER DELETE ON t3 FOR EACH ROW BEGIN
  217. UPDATE cnt SET del=del+1;
  218. END;
  219. DELETE FROM t3 WHERE a<2;
  220. SELECT * FROM t3;
  221. }
  222. } {2 3 4}
  223. do_test delete-7.3 {
  224. execsql {
  225. SELECT * FROM cnt;
  226. }
  227. } {1}
  228. do_test delete-7.4 {
  229. execsql {
  230. DELETE FROM t3;
  231. SELECT * FROM t3;
  232. }
  233. } {}
  234. do_test delete-7.5 {
  235. execsql {
  236. SELECT * FROM cnt;
  237. }
  238. } {4}
  239. do_test delete-7.6 {
  240. execsql {
  241. INSERT INTO t3 VALUES(1);
  242. INSERT INTO t3 SELECT a+1 FROM t3;
  243. INSERT INTO t3 SELECT a+2 FROM t3;
  244. CREATE TABLE t4 AS SELECT * FROM t3;
  245. PRAGMA count_changes=ON;
  246. DELETE FROM t3;
  247. DELETE FROM t4;
  248. }
  249. } {4 4}
  250. } ;# endif trigger
  251. ifcapable {!trigger} {
  252. execsql {DELETE FROM t3}
  253. }
  254. integrity_check delete-7.7
  255. # Make sure error messages are consistent when attempting to delete
  256. # from a read-only database. Ticket #304.
  257. #
  258. do_test delete-8.0 {
  259. execsql {
  260. PRAGMA count_changes=OFF;
  261. INSERT INTO t3 VALUES(123);
  262. SELECT * FROM t3;
  263. }
  264. } {123}
  265. db close
  266. catch {forcedelete test.db-journal}
  267. catch {file attributes test.db -permissions 0444}
  268. catch {file attributes test.db -readonly 1}
  269. sqlite3 db test.db
  270. set ::DB [sqlite3_connection_pointer db]
  271. do_test delete-8.1 {
  272. catchsql {
  273. DELETE FROM t3;
  274. }
  275. } {1 {attempt to write a readonly database}}
  276. do_test delete-8.2 {
  277. execsql {SELECT * FROM t3}
  278. } {123}
  279. do_test delete-8.3 {
  280. catchsql {
  281. DELETE FROM t3 WHERE 1;
  282. }
  283. } {1 {attempt to write a readonly database}}
  284. do_test delete-8.4 {
  285. execsql {SELECT * FROM t3}
  286. } {123}
  287. # Update for v3: In v2 the DELETE statement would succeed because no
  288. # database writes actually occur. Version 3 refuses to open a transaction
  289. # on a read-only file, so the statement fails.
  290. do_test delete-8.5 {
  291. catchsql {
  292. DELETE FROM t3 WHERE a<100;
  293. }
  294. # v2 result: {0 {}}
  295. } {1 {attempt to write a readonly database}}
  296. do_test delete-8.6 {
  297. execsql {SELECT * FROM t3}
  298. } {123}
  299. integrity_check delete-8.7
  300. # Need to do the following for tcl 8.5 on mac. On that configuration, the
  301. # -readonly flag is taken so seriously that a subsequent [forcedelete]
  302. # (required before the next test file can be executed) will fail.
  303. #
  304. catch {file attributes test.db -readonly 0}
  305. db close
  306. forcedelete test.db test.db-journal
  307. # The following tests verify that SQLite correctly handles the case
  308. # where an index B-Tree is being scanned, the rowid column being read
  309. # from each index entry and another statement deletes some rows from
  310. # the index B-Tree. At one point this (obscure) scenario was causing
  311. # SQLite to return spurious SQLITE_CORRUPT errors and arguably incorrect
  312. # query results.
  313. #
  314. do_test delete-9.1 {
  315. sqlite3 db test.db
  316. execsql {
  317. CREATE TABLE t5(a, b);
  318. CREATE TABLE t6(c, d);
  319. INSERT INTO t5 VALUES(1, 2);
  320. INSERT INTO t5 VALUES(3, 4);
  321. INSERT INTO t5 VALUES(5, 6);
  322. INSERT INTO t6 VALUES('a', 'b');
  323. INSERT INTO t6 VALUES('c', 'd');
  324. CREATE INDEX i5 ON t5(a);
  325. CREATE INDEX i6 ON t6(c);
  326. }
  327. } {}
  328. do_test delete-9.2 {
  329. set res [list]
  330. db eval { SELECT t5.rowid AS r, c, d FROM t5, t6 ORDER BY a } {
  331. if {$r==2} { db eval { DELETE FROM t5 } }
  332. lappend res $r $c $d
  333. }
  334. set res
  335. } {1 a b 1 c d 2 a b {} c d}
  336. do_test delete-9.3 {
  337. execsql {
  338. INSERT INTO t5 VALUES(1, 2);
  339. INSERT INTO t5 VALUES(3, 4);
  340. INSERT INTO t5 VALUES(5, 6);
  341. }
  342. set res [list]
  343. db eval { SELECT t5.rowid AS r, c, d FROM t5, t6 ORDER BY a } {
  344. if {$r==2} { db eval { DELETE FROM t5 WHERE rowid = 2 } }
  345. lappend res $r $c $d
  346. }
  347. set res
  348. } {1 a b 1 c d 2 a b {} c d 3 a b 3 c d}
  349. do_test delete-9.4 {
  350. execsql {
  351. DELETE FROM t5;
  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 } {
  358. if {$r==2} { db eval { DELETE FROM t5 WHERE rowid = 1 } }
  359. lappend res $r $c $d
  360. }
  361. set res
  362. } {1 a b 1 c d 2 a b 2 c d 3 a b 3 c d}
  363. do_test delete-9.5 {
  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 } {
  372. if {$r==2} { db eval { DELETE FROM t5 WHERE rowid = 3 } }
  373. lappend res $r $c $d
  374. }
  375. set res
  376. } {1 a b 1 c d 2 a b 2 c d}
  377. finish_test