PageRenderTime 59ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 1ms

/trunk/src/sqlite/test/update.test

#
Unknown | 608 lines | 585 code | 23 blank | 0 comment | 0 complexity | d2c21796ce95f467c807098622cc0553 MD5 | raw file
Possible License(s): BSD-3-Clause
  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 UPDATE statement.
  13. #
  14. # $Id: update.test,v 1.19 2008/04/10 18:44:36 drh Exp $
  15. set testdir [file dirname $argv0]
  16. source $testdir/tester.tcl
  17. # Try to update an non-existent table
  18. #
  19. do_test update-1.1 {
  20. set v [catch {execsql {UPDATE test1 SET f2=5 WHERE f1<1}} msg]
  21. lappend v $msg
  22. } {1 {no such table: test1}}
  23. # Try to update a read-only table
  24. #
  25. do_test update-2.1 {
  26. set v [catch \
  27. {execsql {UPDATE sqlite_master SET name='xyz' WHERE name='123'}} msg]
  28. lappend v $msg
  29. } {1 {table sqlite_master may not be modified}}
  30. # Create a table to work with
  31. #
  32. do_test update-3.1 {
  33. execsql {CREATE TABLE test1(f1 int,f2 int)}
  34. for {set i 1} {$i<=10} {incr i} {
  35. set sql "INSERT INTO test1 VALUES($i,[expr {1<<$i}])"
  36. execsql $sql
  37. }
  38. execsql {SELECT * FROM test1 ORDER BY f1}
  39. } {1 2 2 4 3 8 4 16 5 32 6 64 7 128 8 256 9 512 10 1024}
  40. # Unknown column name in an expression
  41. #
  42. do_test update-3.2 {
  43. set v [catch {execsql {UPDATE test1 SET f1=f3*2 WHERE f2==32}} msg]
  44. lappend v $msg
  45. } {1 {no such column: f3}}
  46. do_test update-3.3 {
  47. set v [catch {execsql {UPDATE test1 SET f1=test2.f1*2 WHERE f2==32}} msg]
  48. lappend v $msg
  49. } {1 {no such column: test2.f1}}
  50. do_test update-3.4 {
  51. set v [catch {execsql {UPDATE test1 SET f3=f1*2 WHERE f2==32}} msg]
  52. lappend v $msg
  53. } {1 {no such column: f3}}
  54. # Actually do some updates
  55. #
  56. do_test update-3.5 {
  57. execsql {UPDATE test1 SET f2=f2*3}
  58. } {}
  59. do_test update-3.5.1 {
  60. db changes
  61. } {10}
  62. # verify that SELECT does not reset the change counter
  63. do_test update-3.5.2 {
  64. db eval {SELECT count(*) FROM test1}
  65. } {10}
  66. do_test update-3.5.3 {
  67. db changes
  68. } {10}
  69. do_test update-3.6 {
  70. execsql {SELECT * FROM test1 ORDER BY f1}
  71. } {1 6 2 12 3 24 4 48 5 96 6 192 7 384 8 768 9 1536 10 3072}
  72. do_test update-3.7 {
  73. execsql {PRAGMA count_changes=on}
  74. execsql {UPDATE test1 SET f2=f2/3 WHERE f1<=5}
  75. } {5}
  76. do_test update-3.8 {
  77. execsql {SELECT * FROM test1 ORDER BY f1}
  78. } {1 2 2 4 3 8 4 16 5 32 6 192 7 384 8 768 9 1536 10 3072}
  79. do_test update-3.9 {
  80. execsql {UPDATE test1 SET f2=f2/3 WHERE f1>5}
  81. } {5}
  82. do_test update-3.10 {
  83. execsql {SELECT * FROM test1 ORDER BY f1}
  84. } {1 2 2 4 3 8 4 16 5 32 6 64 7 128 8 256 9 512 10 1024}
  85. # Swap the values of f1 and f2 for all elements
  86. #
  87. do_test update-3.11 {
  88. execsql {UPDATE test1 SET F2=f1, F1=f2}
  89. } {10}
  90. do_test update-3.12 {
  91. execsql {SELECT * FROM test1 ORDER BY F1}
  92. } {2 1 4 2 8 3 16 4 32 5 64 6 128 7 256 8 512 9 1024 10}
  93. do_test update-3.13 {
  94. execsql {PRAGMA count_changes=off}
  95. execsql {UPDATE test1 SET F2=f1, F1=f2}
  96. } {}
  97. do_test update-3.14 {
  98. execsql {SELECT * FROM test1 ORDER BY F1}
  99. } {1 2 2 4 3 8 4 16 5 32 6 64 7 128 8 256 9 512 10 1024}
  100. # Create duplicate entries and make sure updating still
  101. # works.
  102. #
  103. do_test update-4.0 {
  104. execsql {
  105. DELETE FROM test1 WHERE f1<=5;
  106. INSERT INTO test1(f1,f2) VALUES(8,88);
  107. INSERT INTO test1(f1,f2) VALUES(8,888);
  108. INSERT INTO test1(f1,f2) VALUES(77,128);
  109. INSERT INTO test1(f1,f2) VALUES(777,128);
  110. }
  111. execsql {SELECT * FROM test1 ORDER BY f1,f2}
  112. } {6 64 7 128 8 88 8 256 8 888 9 512 10 1024 77 128 777 128}
  113. do_test update-4.1 {
  114. execsql {UPDATE test1 SET f2=f2+1 WHERE f1==8}
  115. execsql {SELECT * FROM test1 ORDER BY f1,f2}
  116. } {6 64 7 128 8 89 8 257 8 889 9 512 10 1024 77 128 777 128}
  117. do_test update-4.2 {
  118. execsql {UPDATE test1 SET f2=f2-1 WHERE f1==8 and f2>800}
  119. execsql {SELECT * FROM test1 ORDER BY f1,f2}
  120. } {6 64 7 128 8 89 8 257 8 888 9 512 10 1024 77 128 777 128}
  121. do_test update-4.3 {
  122. execsql {UPDATE test1 SET f2=f2-1 WHERE f1==8 and f2<800}
  123. execsql {SELECT * FROM test1 ORDER BY f1,f2}
  124. } {6 64 7 128 8 88 8 256 8 888 9 512 10 1024 77 128 777 128}
  125. do_test update-4.4 {
  126. execsql {UPDATE test1 SET f1=f1+1 WHERE f2==128}
  127. execsql {SELECT * FROM test1 ORDER BY f1,f2}
  128. } {6 64 8 88 8 128 8 256 8 888 9 512 10 1024 78 128 778 128}
  129. do_test update-4.5 {
  130. execsql {UPDATE test1 SET f1=f1-1 WHERE f1>100 and f2==128}
  131. execsql {SELECT * FROM test1 ORDER BY f1,f2}
  132. } {6 64 8 88 8 128 8 256 8 888 9 512 10 1024 78 128 777 128}
  133. do_test update-4.6 {
  134. execsql {
  135. PRAGMA count_changes=on;
  136. UPDATE test1 SET f1=f1-1 WHERE f1<=100 and f2==128;
  137. }
  138. } {2}
  139. do_test update-4.7 {
  140. execsql {
  141. PRAGMA count_changes=off;
  142. SELECT * FROM test1 ORDER BY f1,f2
  143. }
  144. } {6 64 7 128 8 88 8 256 8 888 9 512 10 1024 77 128 777 128}
  145. # Repeat the previous sequence of tests with an index.
  146. #
  147. do_test update-5.0 {
  148. execsql {CREATE INDEX idx1 ON test1(f1)}
  149. execsql {SELECT * FROM test1 ORDER BY f1,f2}
  150. } {6 64 7 128 8 88 8 256 8 888 9 512 10 1024 77 128 777 128}
  151. do_test update-5.1 {
  152. execsql {UPDATE test1 SET f2=f2+1 WHERE f1==8}
  153. execsql {SELECT * FROM test1 ORDER BY f1,f2}
  154. } {6 64 7 128 8 89 8 257 8 889 9 512 10 1024 77 128 777 128}
  155. do_test update-5.2 {
  156. execsql {UPDATE test1 SET f2=f2-1 WHERE f1==8 and f2>800}
  157. execsql {SELECT * FROM test1 ORDER BY f1,f2}
  158. } {6 64 7 128 8 89 8 257 8 888 9 512 10 1024 77 128 777 128}
  159. do_test update-5.3 {
  160. execsql {UPDATE test1 SET f2=f2-1 WHERE f1==8 and f2<800}
  161. execsql {SELECT * FROM test1 ORDER BY f1,f2}
  162. } {6 64 7 128 8 88 8 256 8 888 9 512 10 1024 77 128 777 128}
  163. do_test update-5.4 {
  164. execsql {UPDATE test1 SET f1=f1+1 WHERE f2==128}
  165. execsql {SELECT * FROM test1 ORDER BY f1,f2}
  166. } {6 64 8 88 8 128 8 256 8 888 9 512 10 1024 78 128 778 128}
  167. do_test update-5.4.1 {
  168. execsql {SELECT * FROM test1 WHERE f1==78 ORDER BY f1,f2}
  169. } {78 128}
  170. do_test update-5.4.2 {
  171. execsql {SELECT * FROM test1 WHERE f1==778 ORDER BY f1,f2}
  172. } {778 128}
  173. do_test update-5.4.3 {
  174. execsql {SELECT * FROM test1 WHERE f1==8 ORDER BY f1,f2}
  175. } {8 88 8 128 8 256 8 888}
  176. do_test update-5.5 {
  177. execsql {UPDATE test1 SET f1=f1-1 WHERE f1>100 and f2==128}
  178. } {}
  179. do_test update-5.5.1 {
  180. execsql {SELECT * FROM test1 ORDER BY f1,f2}
  181. } {6 64 8 88 8 128 8 256 8 888 9 512 10 1024 78 128 777 128}
  182. do_test update-5.5.2 {
  183. execsql {SELECT * FROM test1 WHERE f1==78 ORDER BY f1,f2}
  184. } {78 128}
  185. do_test update-5.5.3 {
  186. execsql {SELECT * FROM test1 WHERE f1==778 ORDER BY f1,f2}
  187. } {}
  188. do_test update-5.5.4 {
  189. execsql {SELECT * FROM test1 WHERE f1==777 ORDER BY f1,f2}
  190. } {777 128}
  191. do_test update-5.5.5 {
  192. execsql {SELECT * FROM test1 WHERE f1==8 ORDER BY f1,f2}
  193. } {8 88 8 128 8 256 8 888}
  194. do_test update-5.6 {
  195. execsql {
  196. PRAGMA count_changes=on;
  197. UPDATE test1 SET f1=f1-1 WHERE f1<=100 and f2==128;
  198. }
  199. } {2}
  200. do_test update-5.6.1 {
  201. execsql {
  202. PRAGMA count_changes=off;
  203. SELECT * FROM test1 ORDER BY f1,f2
  204. }
  205. } {6 64 7 128 8 88 8 256 8 888 9 512 10 1024 77 128 777 128}
  206. do_test update-5.6.2 {
  207. execsql {SELECT * FROM test1 WHERE f1==77 ORDER BY f1,f2}
  208. } {77 128}
  209. do_test update-5.6.3 {
  210. execsql {SELECT * FROM test1 WHERE f1==778 ORDER BY f1,f2}
  211. } {}
  212. do_test update-5.6.4 {
  213. execsql {SELECT * FROM test1 WHERE f1==777 ORDER BY f1,f2}
  214. } {777 128}
  215. do_test update-5.6.5 {
  216. execsql {SELECT * FROM test1 WHERE f1==8 ORDER BY f1,f2}
  217. } {8 88 8 256 8 888}
  218. # Repeat the previous sequence of tests with a different index.
  219. #
  220. execsql {PRAGMA synchronous=FULL}
  221. do_test update-6.0 {
  222. execsql {DROP INDEX idx1}
  223. execsql {CREATE INDEX idx1 ON test1(f2)}
  224. execsql {SELECT * FROM test1 ORDER BY f1,f2}
  225. } {6 64 7 128 8 88 8 256 8 888 9 512 10 1024 77 128 777 128}
  226. do_test update-6.1 {
  227. execsql {UPDATE test1 SET f2=f2+1 WHERE f1==8}
  228. execsql {SELECT * FROM test1 ORDER BY f1,f2}
  229. } {6 64 7 128 8 89 8 257 8 889 9 512 10 1024 77 128 777 128}
  230. do_test update-6.1.1 {
  231. execsql {SELECT * FROM test1 WHERE f1==8 ORDER BY f1,f2}
  232. } {8 89 8 257 8 889}
  233. do_test update-6.1.2 {
  234. execsql {SELECT * FROM test1 WHERE f2==89 ORDER BY f1,f2}
  235. } {8 89}
  236. do_test update-6.1.3 {
  237. execsql {SELECT * FROM test1 WHERE f1==88 ORDER BY f1,f2}
  238. } {}
  239. do_test update-6.2 {
  240. execsql {UPDATE test1 SET f2=f2-1 WHERE f1==8 and f2>800}
  241. execsql {SELECT * FROM test1 ORDER BY f1,f2}
  242. } {6 64 7 128 8 89 8 257 8 888 9 512 10 1024 77 128 777 128}
  243. do_test update-6.3 {
  244. execsql {UPDATE test1 SET f2=f2-1 WHERE f1==8 and f2<800}
  245. execsql {SELECT * FROM test1 ORDER BY f1,f2}
  246. } {6 64 7 128 8 88 8 256 8 888 9 512 10 1024 77 128 777 128}
  247. do_test update-6.3.1 {
  248. execsql {SELECT * FROM test1 WHERE f1==8 ORDER BY f1,f2}
  249. } {8 88 8 256 8 888}
  250. do_test update-6.3.2 {
  251. execsql {SELECT * FROM test1 WHERE f2==89 ORDER BY f1,f2}
  252. } {}
  253. do_test update-6.3.3 {
  254. execsql {SELECT * FROM test1 WHERE f2==88 ORDER BY f1,f2}
  255. } {8 88}
  256. do_test update-6.4 {
  257. execsql {UPDATE test1 SET f1=f1+1 WHERE f2==128}
  258. execsql {SELECT * FROM test1 ORDER BY f1,f2}
  259. } {6 64 8 88 8 128 8 256 8 888 9 512 10 1024 78 128 778 128}
  260. do_test update-6.4.1 {
  261. execsql {SELECT * FROM test1 WHERE f1==78 ORDER BY f1,f2}
  262. } {78 128}
  263. do_test update-6.4.2 {
  264. execsql {SELECT * FROM test1 WHERE f1==778 ORDER BY f1,f2}
  265. } {778 128}
  266. do_test update-6.4.3 {
  267. execsql {SELECT * FROM test1 WHERE f1==8 ORDER BY f1,f2}
  268. } {8 88 8 128 8 256 8 888}
  269. do_test update-6.5 {
  270. execsql {UPDATE test1 SET f1=f1-1 WHERE f1>100 and f2==128}
  271. execsql {SELECT * FROM test1 ORDER BY f1,f2}
  272. } {6 64 8 88 8 128 8 256 8 888 9 512 10 1024 78 128 777 128}
  273. do_test update-6.5.1 {
  274. execsql {SELECT * FROM test1 WHERE f1==78 ORDER BY f1,f2}
  275. } {78 128}
  276. do_test update-6.5.2 {
  277. execsql {SELECT * FROM test1 WHERE f1==778 ORDER BY f1,f2}
  278. } {}
  279. do_test update-6.5.3 {
  280. execsql {SELECT * FROM test1 WHERE f1==777 ORDER BY f1,f2}
  281. } {777 128}
  282. do_test update-6.5.4 {
  283. execsql {SELECT * FROM test1 WHERE f1==8 ORDER BY f1,f2}
  284. } {8 88 8 128 8 256 8 888}
  285. do_test update-6.6 {
  286. execsql {UPDATE test1 SET f1=f1-1 WHERE f1<=100 and f2==128}
  287. execsql {SELECT * FROM test1 ORDER BY f1,f2}
  288. } {6 64 7 128 8 88 8 256 8 888 9 512 10 1024 77 128 777 128}
  289. do_test update-6.6.1 {
  290. execsql {SELECT * FROM test1 WHERE f1==77 ORDER BY f1,f2}
  291. } {77 128}
  292. do_test update-6.6.2 {
  293. execsql {SELECT * FROM test1 WHERE f1==778 ORDER BY f1,f2}
  294. } {}
  295. do_test update-6.6.3 {
  296. execsql {SELECT * FROM test1 WHERE f1==777 ORDER BY f1,f2}
  297. } {777 128}
  298. do_test update-6.6.4 {
  299. execsql {SELECT * FROM test1 WHERE f1==8 ORDER BY f1,f2}
  300. } {8 88 8 256 8 888}
  301. # Repeat the previous sequence of tests with multiple
  302. # indices
  303. #
  304. do_test update-7.0 {
  305. execsql {CREATE INDEX idx2 ON test1(f2)}
  306. execsql {CREATE INDEX idx3 ON test1(f1,f2)}
  307. execsql {SELECT * FROM test1 ORDER BY f1,f2}
  308. } {6 64 7 128 8 88 8 256 8 888 9 512 10 1024 77 128 777 128}
  309. do_test update-7.1 {
  310. execsql {UPDATE test1 SET f2=f2+1 WHERE f1==8}
  311. execsql {SELECT * FROM test1 ORDER BY f1,f2}
  312. } {6 64 7 128 8 89 8 257 8 889 9 512 10 1024 77 128 777 128}
  313. do_test update-7.1.1 {
  314. execsql {SELECT * FROM test1 WHERE f1==8 ORDER BY f1,f2}
  315. } {8 89 8 257 8 889}
  316. do_test update-7.1.2 {
  317. execsql {SELECT * FROM test1 WHERE f2==89 ORDER BY f1,f2}
  318. } {8 89}
  319. do_test update-7.1.3 {
  320. execsql {SELECT * FROM test1 WHERE f1==88 ORDER BY f1,f2}
  321. } {}
  322. do_test update-7.2 {
  323. execsql {UPDATE test1 SET f2=f2-1 WHERE f1==8 and f2>800}
  324. execsql {SELECT * FROM test1 ORDER BY f1,f2}
  325. } {6 64 7 128 8 89 8 257 8 888 9 512 10 1024 77 128 777 128}
  326. do_test update-7.3 {
  327. # explain {UPDATE test1 SET f2=f2-1 WHERE f1==8 and F2<300}
  328. execsql {UPDATE test1 SET f2=f2-1 WHERE f1==8 and f2<800}
  329. execsql {SELECT * FROM test1 ORDER BY f1,f2}
  330. } {6 64 7 128 8 88 8 256 8 888 9 512 10 1024 77 128 777 128}
  331. do_test update-7.3.1 {
  332. execsql {SELECT * FROM test1 WHERE f1==8 ORDER BY f1,f2}
  333. } {8 88 8 256 8 888}
  334. do_test update-7.3.2 {
  335. execsql {SELECT * FROM test1 WHERE f2==89 ORDER BY f1,f2}
  336. } {}
  337. do_test update-7.3.3 {
  338. execsql {SELECT * FROM test1 WHERE f2==88 ORDER BY f1,f2}
  339. } {8 88}
  340. do_test update-7.4 {
  341. execsql {UPDATE test1 SET f1=f1+1 WHERE f2==128}
  342. execsql {SELECT * FROM test1 ORDER BY f1,f2}
  343. } {6 64 8 88 8 128 8 256 8 888 9 512 10 1024 78 128 778 128}
  344. do_test update-7.4.1 {
  345. execsql {SELECT * FROM test1 WHERE f1==78 ORDER BY f1,f2}
  346. } {78 128}
  347. do_test update-7.4.2 {
  348. execsql {SELECT * FROM test1 WHERE f1==778 ORDER BY f1,f2}
  349. } {778 128}
  350. do_test update-7.4.3 {
  351. execsql {SELECT * FROM test1 WHERE f1==8 ORDER BY f1,f2}
  352. } {8 88 8 128 8 256 8 888}
  353. do_test update-7.5 {
  354. execsql {UPDATE test1 SET f1=f1-1 WHERE f1>100 and f2==128}
  355. execsql {SELECT * FROM test1 ORDER BY f1,f2}
  356. } {6 64 8 88 8 128 8 256 8 888 9 512 10 1024 78 128 777 128}
  357. do_test update-7.5.1 {
  358. execsql {SELECT * FROM test1 WHERE f1==78 ORDER BY f1,f2}
  359. } {78 128}
  360. do_test update-7.5.2 {
  361. execsql {SELECT * FROM test1 WHERE f1==778 ORDER BY f1,f2}
  362. } {}
  363. do_test update-7.5.3 {
  364. execsql {SELECT * FROM test1 WHERE f1==777 ORDER BY f1,f2}
  365. } {777 128}
  366. do_test update-7.5.4 {
  367. execsql {SELECT * FROM test1 WHERE f1==8 ORDER BY f1,f2}
  368. } {8 88 8 128 8 256 8 888}
  369. do_test update-7.6 {
  370. execsql {UPDATE test1 SET f1=f1-1 WHERE f1<=100 and f2==128}
  371. execsql {SELECT * FROM test1 ORDER BY f1,f2}
  372. } {6 64 7 128 8 88 8 256 8 888 9 512 10 1024 77 128 777 128}
  373. do_test update-7.6.1 {
  374. execsql {SELECT * FROM test1 WHERE f1==77 ORDER BY f1,f2}
  375. } {77 128}
  376. do_test update-7.6.2 {
  377. execsql {SELECT * FROM test1 WHERE f1==778 ORDER BY f1,f2}
  378. } {}
  379. do_test update-7.6.3 {
  380. execsql {SELECT * FROM test1 WHERE f1==777 ORDER BY f1,f2}
  381. } {777 128}
  382. do_test update-7.6.4 {
  383. execsql {SELECT * FROM test1 WHERE f1==8 ORDER BY f1,f2}
  384. } {8 88 8 256 8 888}
  385. # Error messages
  386. #
  387. do_test update-9.1 {
  388. set v [catch {execsql {
  389. UPDATE test1 SET x=11 WHERE f1=1025
  390. }} msg]
  391. lappend v $msg
  392. } {1 {no such column: x}}
  393. do_test update-9.2 {
  394. set v [catch {execsql {
  395. UPDATE test1 SET f1=x(11) WHERE f1=1025
  396. }} msg]
  397. lappend v $msg
  398. } {1 {no such function: x}}
  399. do_test update-9.3 {
  400. set v [catch {execsql {
  401. UPDATE test1 SET f1=11 WHERE x=1025
  402. }} msg]
  403. lappend v $msg
  404. } {1 {no such column: x}}
  405. do_test update-9.4 {
  406. set v [catch {execsql {
  407. UPDATE test1 SET f1=11 WHERE x(f1)=1025
  408. }} msg]
  409. lappend v $msg
  410. } {1 {no such function: x}}
  411. # Try doing updates on a unique column where the value does not
  412. # really change.
  413. #
  414. do_test update-10.1 {
  415. execsql {
  416. DROP TABLE test1;
  417. CREATE TABLE t1(
  418. a integer primary key,
  419. b UNIQUE,
  420. c, d,
  421. e, f,
  422. UNIQUE(c,d)
  423. );
  424. INSERT INTO t1 VALUES(1,2,3,4,5,6);
  425. INSERT INTO t1 VALUES(2,3,4,4,6,7);
  426. SELECT * FROM t1
  427. }
  428. } {1 2 3 4 5 6 2 3 4 4 6 7}
  429. do_test update-10.2 {
  430. catchsql {
  431. UPDATE t1 SET a=1, e=9 WHERE f=6;
  432. SELECT * FROM t1;
  433. }
  434. } {0 {1 2 3 4 9 6 2 3 4 4 6 7}}
  435. do_test update-10.3 {
  436. catchsql {
  437. UPDATE t1 SET a=1, e=10 WHERE f=7;
  438. SELECT * FROM t1;
  439. }
  440. } {1 {PRIMARY KEY must be unique}}
  441. do_test update-10.4 {
  442. catchsql {
  443. SELECT * FROM t1;
  444. }
  445. } {0 {1 2 3 4 9 6 2 3 4 4 6 7}}
  446. do_test update-10.5 {
  447. catchsql {
  448. UPDATE t1 SET b=2, e=11 WHERE f=6;
  449. SELECT * FROM t1;
  450. }
  451. } {0 {1 2 3 4 11 6 2 3 4 4 6 7}}
  452. do_test update-10.6 {
  453. catchsql {
  454. UPDATE t1 SET b=2, e=12 WHERE f=7;
  455. SELECT * FROM t1;
  456. }
  457. } {1 {column b is not unique}}
  458. do_test update-10.7 {
  459. catchsql {
  460. SELECT * FROM t1;
  461. }
  462. } {0 {1 2 3 4 11 6 2 3 4 4 6 7}}
  463. do_test update-10.8 {
  464. catchsql {
  465. UPDATE t1 SET c=3, d=4, e=13 WHERE f=6;
  466. SELECT * FROM t1;
  467. }
  468. } {0 {1 2 3 4 13 6 2 3 4 4 6 7}}
  469. do_test update-10.9 {
  470. catchsql {
  471. UPDATE t1 SET c=3, d=4, e=14 WHERE f=7;
  472. SELECT * FROM t1;
  473. }
  474. } {1 {columns c, d are not unique}}
  475. do_test update-10.10 {
  476. catchsql {
  477. SELECT * FROM t1;
  478. }
  479. } {0 {1 2 3 4 13 6 2 3 4 4 6 7}}
  480. # Make sure we can handle a subquery in the where clause.
  481. #
  482. ifcapable subquery {
  483. do_test update-11.1 {
  484. execsql {
  485. UPDATE t1 SET e=e+1 WHERE b IN (SELECT b FROM t1);
  486. SELECT b,e FROM t1;
  487. }
  488. } {2 14 3 7}
  489. do_test update-11.2 {
  490. execsql {
  491. UPDATE t1 SET e=e+1 WHERE a IN (SELECT a FROM t1);
  492. SELECT a,e FROM t1;
  493. }
  494. } {1 15 2 8}
  495. }
  496. integrity_check update-12.1
  497. # Ticket 602. Updates should occur in the same order as the records
  498. # were discovered in the WHERE clause.
  499. #
  500. do_test update-13.1 {
  501. execsql {
  502. BEGIN;
  503. CREATE TABLE t2(a);
  504. INSERT INTO t2 VALUES(1);
  505. INSERT INTO t2 VALUES(2);
  506. INSERT INTO t2 SELECT a+2 FROM t2;
  507. INSERT INTO t2 SELECT a+4 FROM t2;
  508. INSERT INTO t2 SELECT a+8 FROM t2;
  509. INSERT INTO t2 SELECT a+16 FROM t2;
  510. INSERT INTO t2 SELECT a+32 FROM t2;
  511. INSERT INTO t2 SELECT a+64 FROM t2;
  512. INSERT INTO t2 SELECT a+128 FROM t2;
  513. INSERT INTO t2 SELECT a+256 FROM t2;
  514. INSERT INTO t2 SELECT a+512 FROM t2;
  515. INSERT INTO t2 SELECT a+1024 FROM t2;
  516. COMMIT;
  517. SELECT count(*) FROM t2;
  518. }
  519. } {2048}
  520. do_test update-13.2 {
  521. execsql {
  522. SELECT count(*) FROM t2 WHERE a=rowid;
  523. }
  524. } {2048}
  525. do_test update-13.3 {
  526. execsql {
  527. UPDATE t2 SET rowid=rowid-1;
  528. SELECT count(*) FROM t2 WHERE a=rowid+1;
  529. }
  530. } {2048}
  531. do_test update-13.3 {
  532. execsql {
  533. UPDATE t2 SET rowid=rowid+10000;
  534. UPDATE t2 SET rowid=rowid-9999;
  535. SELECT count(*) FROM t2 WHERE a=rowid;
  536. }
  537. } {2048}
  538. do_test update-13.4 {
  539. execsql {
  540. BEGIN;
  541. INSERT INTO t2 SELECT a+2048 FROM t2;
  542. INSERT INTO t2 SELECT a+4096 FROM t2;
  543. INSERT INTO t2 SELECT a+8192 FROM t2;
  544. SELECT count(*) FROM t2 WHERE a=rowid;
  545. COMMIT;
  546. }
  547. } 16384
  548. do_test update-13.5 {
  549. execsql {
  550. UPDATE t2 SET rowid=rowid-1;
  551. SELECT count(*) FROM t2 WHERE a=rowid+1;
  552. }
  553. } 16384
  554. integrity_check update-13.6
  555. ifcapable {trigger} {
  556. # Test for proper detection of malformed WHEN clauses on UPDATE triggers.
  557. #
  558. do_test update-14.1 {
  559. execsql {
  560. CREATE TABLE t3(a,b,c);
  561. CREATE TRIGGER t3r1 BEFORE UPDATE on t3 WHEN nosuchcol BEGIN
  562. SELECT 'illegal WHEN clause';
  563. END;
  564. }
  565. } {}
  566. do_test update-14.2 {
  567. catchsql {
  568. UPDATE t3 SET a=1;
  569. }
  570. } {1 {no such column: nosuchcol}}
  571. do_test update-14.3 {
  572. execsql {
  573. CREATE TABLE t4(a,b,c);
  574. CREATE TRIGGER t4r1 AFTER UPDATE on t4 WHEN nosuchcol BEGIN
  575. SELECT 'illegal WHEN clause';
  576. END;
  577. }
  578. } {}
  579. do_test update-14.4 {
  580. catchsql {
  581. UPDATE t4 SET a=1;
  582. }
  583. } {1 {no such column: nosuchcol}}
  584. } ;# ifcapable {trigger}
  585. finish_test