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 UPDATE statement.
17 # $Id: update.test,v 1.15 2004/02/10 13:41:53 drh Exp $
19 set testdir [file dirname $argv0]
20 source $testdir/tester.tcl
22 # Try to update an non-existent table
25 set v [catch {execsql {UPDATE test1 SET f2=5 WHERE f1<1}} msg]
27 } {1 {no such table: test1}}
29 # Try to update a read-only table
33 {execsql {UPDATE sqlite_master SET name='xyz' WHERE name='123'}} msg]
35 } {1 {table sqlite_master may not be modified}}
37 # Create a table to work with
40 execsql {CREATE TABLE test1(f1 int,f2 int)}
41 for {set i 1} {$i<=10} {incr i} {
42 set sql "INSERT INTO test1 VALUES($i,[expr {int(pow(2,$i))}])"
45 execsql {SELECT * FROM test1 ORDER BY f1}
46 } {1 2 2 4 3 8 4 16 5 32 6 64 7 128 8 256 9 512 10 1024}
48 # Unknown column name in an expression
51 set v [catch {execsql {UPDATE test1 SET f1=f3*2 WHERE f2==32}} msg]
53 } {1 {no such column: f3}}
55 set v [catch {execsql {UPDATE test1 SET f1=test2.f1*2 WHERE f2==32}} msg]
57 } {1 {no such column: test2.f1}}
59 set v [catch {execsql {UPDATE test1 SET f3=f1*2 WHERE f2==32}} msg]
61 } {1 {no such column: f3}}
63 # Actually do some updates
66 execsql {UPDATE test1 SET f2=f2*3}
69 execsql {SELECT * FROM test1 ORDER BY f1}
70 } {1 6 2 12 3 24 4 48 5 96 6 192 7 384 8 768 9 1536 10 3072}
72 execsql {PRAGMA count_changes=on}
73 execsql {UPDATE test1 SET f2=f2/3 WHERE f1<=5}
76 execsql {SELECT * FROM test1 ORDER BY f1}
77 } {1 2 2 4 3 8 4 16 5 32 6 192 7 384 8 768 9 1536 10 3072}
79 execsql {UPDATE test1 SET f2=f2/3 WHERE f1>5}
82 execsql {SELECT * FROM test1 ORDER BY f1}
83 } {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
88 execsql {UPDATE test1 SET F2=f1, F1=f2}
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}
94 execsql {PRAGMA count_changes=off}
95 execsql {UPDATE test1 SET F2=f1, F1=f2}
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}
101 # Create duplicate entries and make sure updating still
106 DELETE FROM test1 WHERE f1<=5;
107 INSERT INTO test1(f1,f2) VALUES(8,88);
108 INSERT INTO test1(f1,f2) VALUES(8,888);
109 INSERT INTO test1(f1,f2) VALUES(77,128);
110 INSERT INTO test1(f1,f2) VALUES(777,128);
112 execsql {SELECT * FROM test1 ORDER BY f1,f2}
113 } {6 64 7 128 8 88 8 256 8 888 9 512 10 1024 77 128 777 128}
115 execsql {UPDATE test1 SET f2=f2+1 WHERE f1==8}
116 execsql {SELECT * FROM test1 ORDER BY f1,f2}
117 } {6 64 7 128 8 89 8 257 8 889 9 512 10 1024 77 128 777 128}
119 execsql {UPDATE test1 SET f2=f2-1 WHERE f1==8 and f2>800}
120 execsql {SELECT * FROM test1 ORDER BY f1,f2}
121 } {6 64 7 128 8 89 8 257 8 888 9 512 10 1024 77 128 777 128}
123 execsql {UPDATE test1 SET f2=f2-1 WHERE f1==8 and f2<800}
124 execsql {SELECT * FROM test1 ORDER BY f1,f2}
125 } {6 64 7 128 8 88 8 256 8 888 9 512 10 1024 77 128 777 128}
127 execsql {UPDATE test1 SET f1=f1+1 WHERE f2==128}
128 execsql {SELECT * FROM test1 ORDER BY f1,f2}
129 } {6 64 8 88 8 128 8 256 8 888 9 512 10 1024 78 128 778 128}
131 execsql {UPDATE test1 SET f1=f1-1 WHERE f1>100 and f2==128}
132 execsql {SELECT * FROM test1 ORDER BY f1,f2}
133 } {6 64 8 88 8 128 8 256 8 888 9 512 10 1024 78 128 777 128}
136 PRAGMA count_changes=on;
137 UPDATE test1 SET f1=f1-1 WHERE f1<=100 and f2==128;
142 PRAGMA count_changes=off;
143 SELECT * FROM test1 ORDER BY f1,f2
145 } {6 64 7 128 8 88 8 256 8 888 9 512 10 1024 77 128 777 128}
147 # Repeat the previous sequence of tests with an index.
150 execsql {CREATE INDEX idx1 ON test1(f1)}
151 execsql {SELECT * FROM test1 ORDER BY f1,f2}
152 } {6 64 7 128 8 88 8 256 8 888 9 512 10 1024 77 128 777 128}
154 execsql {UPDATE test1 SET f2=f2+1 WHERE f1==8}
155 execsql {SELECT * FROM test1 ORDER BY f1,f2}
156 } {6 64 7 128 8 89 8 257 8 889 9 512 10 1024 77 128 777 128}
158 execsql {UPDATE test1 SET f2=f2-1 WHERE f1==8 and f2>800}
159 execsql {SELECT * FROM test1 ORDER BY f1,f2}
160 } {6 64 7 128 8 89 8 257 8 888 9 512 10 1024 77 128 777 128}
162 execsql {UPDATE test1 SET f2=f2-1 WHERE f1==8 and f2<800}
163 execsql {SELECT * FROM test1 ORDER BY f1,f2}
164 } {6 64 7 128 8 88 8 256 8 888 9 512 10 1024 77 128 777 128}
166 execsql {UPDATE test1 SET f1=f1+1 WHERE f2==128}
167 execsql {SELECT * FROM test1 ORDER BY f1,f2}
168 } {6 64 8 88 8 128 8 256 8 888 9 512 10 1024 78 128 778 128}
169 do_test update-5.4.1 {
170 execsql {SELECT * FROM test1 WHERE f1==78 ORDER BY f1,f2}
172 do_test update-5.4.2 {
173 execsql {SELECT * FROM test1 WHERE f1==778 ORDER BY f1,f2}
175 do_test update-5.4.3 {
176 execsql {SELECT * FROM test1 WHERE f1==8 ORDER BY f1,f2}
177 } {8 88 8 128 8 256 8 888}
179 execsql {UPDATE test1 SET f1=f1-1 WHERE f1>100 and f2==128}
181 do_test update-5.5.1 {
182 execsql {SELECT * FROM test1 ORDER BY f1,f2}
183 } {6 64 8 88 8 128 8 256 8 888 9 512 10 1024 78 128 777 128}
184 do_test update-5.5.2 {
185 execsql {SELECT * FROM test1 WHERE f1==78 ORDER BY f1,f2}
187 do_test update-5.5.3 {
188 execsql {SELECT * FROM test1 WHERE f1==778 ORDER BY f1,f2}
190 do_test update-5.5.4 {
191 execsql {SELECT * FROM test1 WHERE f1==777 ORDER BY f1,f2}
193 do_test update-5.5.5 {
194 execsql {SELECT * FROM test1 WHERE f1==8 ORDER BY f1,f2}
195 } {8 88 8 128 8 256 8 888}
198 PRAGMA count_changes=on;
199 UPDATE test1 SET f1=f1-1 WHERE f1<=100 and f2==128;
202 do_test update-5.6.1 {
204 PRAGMA count_changes=off;
205 SELECT * FROM test1 ORDER BY f1,f2
207 } {6 64 7 128 8 88 8 256 8 888 9 512 10 1024 77 128 777 128}
208 do_test update-5.6.2 {
209 execsql {SELECT * FROM test1 WHERE f1==77 ORDER BY f1,f2}
211 do_test update-5.6.3 {
212 execsql {SELECT * FROM test1 WHERE f1==778 ORDER BY f1,f2}
214 do_test update-5.6.4 {
215 execsql {SELECT * FROM test1 WHERE f1==777 ORDER BY f1,f2}
217 do_test update-5.6.5 {
218 execsql {SELECT * FROM test1 WHERE f1==8 ORDER BY f1,f2}
221 # Repeat the previous sequence of tests with a different index.
223 execsql {PRAGMA synchronous=FULL}
225 execsql {DROP INDEX idx1}
226 execsql {CREATE INDEX idx1 ON test1(f2)}
227 execsql {SELECT * FROM test1 ORDER BY f1,f2}
228 } {6 64 7 128 8 88 8 256 8 888 9 512 10 1024 77 128 777 128}
230 execsql {UPDATE test1 SET f2=f2+1 WHERE f1==8}
231 execsql {SELECT * FROM test1 ORDER BY f1,f2}
232 } {6 64 7 128 8 89 8 257 8 889 9 512 10 1024 77 128 777 128}
233 do_test update-6.1.1 {
234 execsql {SELECT * FROM test1 WHERE f1==8 ORDER BY f1,f2}
236 do_test update-6.1.2 {
237 execsql {SELECT * FROM test1 WHERE f2==89 ORDER BY f1,f2}
239 do_test update-6.1.3 {
240 execsql {SELECT * FROM test1 WHERE f1==88 ORDER BY f1,f2}
243 execsql {UPDATE test1 SET f2=f2-1 WHERE f1==8 and f2>800}
244 execsql {SELECT * FROM test1 ORDER BY f1,f2}
245 } {6 64 7 128 8 89 8 257 8 888 9 512 10 1024 77 128 777 128}
247 execsql {UPDATE test1 SET f2=f2-1 WHERE f1==8 and f2<800}
248 execsql {SELECT * FROM test1 ORDER BY f1,f2}
249 } {6 64 7 128 8 88 8 256 8 888 9 512 10 1024 77 128 777 128}
250 do_test update-6.3.1 {
251 execsql {SELECT * FROM test1 WHERE f1==8 ORDER BY f1,f2}
253 do_test update-6.3.2 {
254 execsql {SELECT * FROM test1 WHERE f2==89 ORDER BY f1,f2}
256 do_test update-6.3.3 {
257 execsql {SELECT * FROM test1 WHERE f2==88 ORDER BY f1,f2}
260 execsql {UPDATE test1 SET f1=f1+1 WHERE f2==128}
261 execsql {SELECT * FROM test1 ORDER BY f1,f2}
262 } {6 64 8 88 8 128 8 256 8 888 9 512 10 1024 78 128 778 128}
263 do_test update-6.4.1 {
264 execsql {SELECT * FROM test1 WHERE f1==78 ORDER BY f1,f2}
266 do_test update-6.4.2 {
267 execsql {SELECT * FROM test1 WHERE f1==778 ORDER BY f1,f2}
269 do_test update-6.4.3 {
270 execsql {SELECT * FROM test1 WHERE f1==8 ORDER BY f1,f2}
271 } {8 88 8 128 8 256 8 888}
273 execsql {UPDATE test1 SET f1=f1-1 WHERE f1>100 and f2==128}
274 execsql {SELECT * FROM test1 ORDER BY f1,f2}
275 } {6 64 8 88 8 128 8 256 8 888 9 512 10 1024 78 128 777 128}
276 do_test update-6.5.1 {
277 execsql {SELECT * FROM test1 WHERE f1==78 ORDER BY f1,f2}
279 do_test update-6.5.2 {
280 execsql {SELECT * FROM test1 WHERE f1==778 ORDER BY f1,f2}
282 do_test update-6.5.3 {
283 execsql {SELECT * FROM test1 WHERE f1==777 ORDER BY f1,f2}
285 do_test update-6.5.4 {
286 execsql {SELECT * FROM test1 WHERE f1==8 ORDER BY f1,f2}
287 } {8 88 8 128 8 256 8 888}
289 execsql {UPDATE test1 SET f1=f1-1 WHERE f1<=100 and f2==128}
290 execsql {SELECT * FROM test1 ORDER BY f1,f2}
291 } {6 64 7 128 8 88 8 256 8 888 9 512 10 1024 77 128 777 128}
292 do_test update-6.6.1 {
293 execsql {SELECT * FROM test1 WHERE f1==77 ORDER BY f1,f2}
295 do_test update-6.6.2 {
296 execsql {SELECT * FROM test1 WHERE f1==778 ORDER BY f1,f2}
298 do_test update-6.6.3 {
299 execsql {SELECT * FROM test1 WHERE f1==777 ORDER BY f1,f2}
301 do_test update-6.6.4 {
302 execsql {SELECT * FROM test1 WHERE f1==8 ORDER BY f1,f2}
305 # Repeat the previous sequence of tests with multiple
309 execsql {CREATE INDEX idx2 ON test1(f2)}
310 execsql {CREATE INDEX idx3 ON test1(f1,f2)}
311 execsql {SELECT * FROM test1 ORDER BY f1,f2}
312 } {6 64 7 128 8 88 8 256 8 888 9 512 10 1024 77 128 777 128}
314 execsql {UPDATE test1 SET f2=f2+1 WHERE f1==8}
315 execsql {SELECT * FROM test1 ORDER BY f1,f2}
316 } {6 64 7 128 8 89 8 257 8 889 9 512 10 1024 77 128 777 128}
317 do_test update-7.1.1 {
318 execsql {SELECT * FROM test1 WHERE f1==8 ORDER BY f1,f2}
320 do_test update-7.1.2 {
321 execsql {SELECT * FROM test1 WHERE f2==89 ORDER BY f1,f2}
323 do_test update-7.1.3 {
324 execsql {SELECT * FROM test1 WHERE f1==88 ORDER BY f1,f2}
327 execsql {UPDATE test1 SET f2=f2-1 WHERE f1==8 and f2>800}
328 execsql {SELECT * FROM test1 ORDER BY f1,f2}
329 } {6 64 7 128 8 89 8 257 8 888 9 512 10 1024 77 128 777 128}
331 # explain {UPDATE test1 SET f2=f2-1 WHERE f1==8 and F2<300}
332 execsql {UPDATE test1 SET f2=f2-1 WHERE f1==8 and f2<800}
333 execsql {SELECT * FROM test1 ORDER BY f1,f2}
334 } {6 64 7 128 8 88 8 256 8 888 9 512 10 1024 77 128 777 128}
335 do_test update-7.3.1 {
336 execsql {SELECT * FROM test1 WHERE f1==8 ORDER BY f1,f2}
338 do_test update-7.3.2 {
339 execsql {SELECT * FROM test1 WHERE f2==89 ORDER BY f1,f2}
341 do_test update-7.3.3 {
342 execsql {SELECT * FROM test1 WHERE f2==88 ORDER BY f1,f2}
345 execsql {UPDATE test1 SET f1=f1+1 WHERE f2==128}
346 execsql {SELECT * FROM test1 ORDER BY f1,f2}
347 } {6 64 8 88 8 128 8 256 8 888 9 512 10 1024 78 128 778 128}
348 do_test update-7.4.1 {
349 execsql {SELECT * FROM test1 WHERE f1==78 ORDER BY f1,f2}
351 do_test update-7.4.2 {
352 execsql {SELECT * FROM test1 WHERE f1==778 ORDER BY f1,f2}
354 do_test update-7.4.3 {
355 execsql {SELECT * FROM test1 WHERE f1==8 ORDER BY f1,f2}
356 } {8 88 8 128 8 256 8 888}
358 execsql {UPDATE test1 SET f1=f1-1 WHERE f1>100 and f2==128}
359 execsql {SELECT * FROM test1 ORDER BY f1,f2}
360 } {6 64 8 88 8 128 8 256 8 888 9 512 10 1024 78 128 777 128}
361 do_test update-7.5.1 {
362 execsql {SELECT * FROM test1 WHERE f1==78 ORDER BY f1,f2}
364 do_test update-7.5.2 {
365 execsql {SELECT * FROM test1 WHERE f1==778 ORDER BY f1,f2}
367 do_test update-7.5.3 {
368 execsql {SELECT * FROM test1 WHERE f1==777 ORDER BY f1,f2}
370 do_test update-7.5.4 {
371 execsql {SELECT * FROM test1 WHERE f1==8 ORDER BY f1,f2}
372 } {8 88 8 128 8 256 8 888}
374 execsql {UPDATE test1 SET f1=f1-1 WHERE f1<=100 and f2==128}
375 execsql {SELECT * FROM test1 ORDER BY f1,f2}
376 } {6 64 7 128 8 88 8 256 8 888 9 512 10 1024 77 128 777 128}
377 do_test update-7.6.1 {
378 execsql {SELECT * FROM test1 WHERE f1==77 ORDER BY f1,f2}
380 do_test update-7.6.2 {
381 execsql {SELECT * FROM test1 WHERE f1==778 ORDER BY f1,f2}
383 do_test update-7.6.3 {
384 execsql {SELECT * FROM test1 WHERE f1==777 ORDER BY f1,f2}
386 do_test update-7.6.4 {
387 execsql {SELECT * FROM test1 WHERE f1==8 ORDER BY f1,f2}
393 set v [catch {execsql {
394 UPDATE test1 SET x=11 WHERE f1=1025
397 } {1 {no such column: x}}
399 set v [catch {execsql {
400 UPDATE test1 SET f1=x(11) WHERE f1=1025
403 } {1 {no such function: x}}
405 set v [catch {execsql {
406 UPDATE test1 SET f1=11 WHERE x=1025
409 } {1 {no such column: x}}
411 set v [catch {execsql {
412 UPDATE test1 SET f1=11 WHERE x(f1)=1025
415 } {1 {no such function: x}}
417 # Try doing updates on a unique column where the value does not
420 do_test update-10.1 {
424 a integer primary key,
430 INSERT INTO t1 VALUES(1,2,3,4,5,6);
431 INSERT INTO t1 VALUES(2,3,4,4,6,7);
434 } {1 2 3 4 5 6 2 3 4 4 6 7}
435 do_test update-10.2 {
437 UPDATE t1 SET a=1, e=9 WHERE f=6;
440 } {0 {1 2 3 4 9 6 2 3 4 4 6 7}}
441 do_test update-10.3 {
443 UPDATE t1 SET a=1, e=10 WHERE f=7;
446 } {1 {PRIMARY KEY must be unique}}
447 do_test update-10.4 {
451 } {0 {1 2 3 4 9 6 2 3 4 4 6 7}}
452 do_test update-10.5 {
454 UPDATE t1 SET b=2, e=11 WHERE f=6;
457 } {0 {1 2 3 4 11 6 2 3 4 4 6 7}}
458 do_test update-10.6 {
460 UPDATE t1 SET b=2, e=12 WHERE f=7;
463 } {1 {column b is not unique}}
464 do_test update-10.7 {
468 } {0 {1 2 3 4 11 6 2 3 4 4 6 7}}
469 do_test update-10.8 {
471 UPDATE t1 SET c=3, d=4, e=13 WHERE f=6;
474 } {0 {1 2 3 4 13 6 2 3 4 4 6 7}}
475 do_test update-10.9 {
477 UPDATE t1 SET c=3, d=4, e=14 WHERE f=7;
480 } {1 {columns c, d are not unique}}
481 do_test update-10.10 {
485 } {0 {1 2 3 4 13 6 2 3 4 4 6 7}}
487 # Make sure we can handle a subquery in the where clause.
489 do_test update-11.1 {
491 UPDATE t1 SET e=e+1 WHERE b IN (SELECT b FROM t1);
495 do_test update-11.2 {
497 UPDATE t1 SET e=e+1 WHERE a IN (SELECT a FROM t1);
502 integrity_check update-12.1
504 # Ticket 602. Updates should occur in the same order as the records
505 # were discovered in the WHERE clause.
507 do_test update-13.1 {
511 INSERT INTO t2 VALUES(1);
512 INSERT INTO t2 VALUES(2);
513 INSERT INTO t2 SELECT a+2 FROM t2;
514 INSERT INTO t2 SELECT a+4 FROM t2;
515 INSERT INTO t2 SELECT a+8 FROM t2;
516 INSERT INTO t2 SELECT a+16 FROM t2;
517 INSERT INTO t2 SELECT a+32 FROM t2;
518 INSERT INTO t2 SELECT a+64 FROM t2;
519 INSERT INTO t2 SELECT a+128 FROM t2;
520 INSERT INTO t2 SELECT a+256 FROM t2;
521 INSERT INTO t2 SELECT a+512 FROM t2;
522 INSERT INTO t2 SELECT a+1024 FROM t2;
524 SELECT count(*) FROM t2;
527 do_test update-13.2 {
529 SELECT count(*) FROM t2 WHERE a=rowid;
532 do_test update-13.3 {
534 UPDATE t2 SET rowid=rowid-1;
535 SELECT count(*) FROM t2 WHERE a=rowid+1;
538 do_test update-13.3 {
540 UPDATE t2 SET rowid=rowid+10000;
541 UPDATE t2 SET rowid=rowid-9999;
542 SELECT count(*) FROM t2 WHERE a=rowid;
545 do_test update-13.4 {
548 INSERT INTO t2 SELECT a+2048 FROM t2;
549 INSERT INTO t2 SELECT a+4096 FROM t2;
550 INSERT INTO t2 SELECT a+8192 FROM t2;
551 SELECT count(*) FROM t2 WHERE a=rowid;
555 do_test update-13.5 {
557 UPDATE t2 SET rowid=rowid-1;
558 SELECT count(*) FROM t2 WHERE a=rowid+1;
562 integrity_check update-13.6