Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/sqlite/blob/master/test/update.test # sqlite [Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `sqlite / test / update.test` 800 lines ยท 24374 bytes Raw Blame History 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 16 set testdir [file dirname $argv0] 17 source $testdir/tester.tcl 18 19 # Try to update an non-existent table 20 # 21 do_test update-1.1 { 22 set v [catch {execsql {UPDATE test1 SET f2=5 WHERE f1<1}} msg] 23 lappend v $msg 24 } {1 {no such table: test1}} 25 26 # Try to update a read-only table 27 # 28 do_test update-2.1 { 29 set v [catch \ 30 {execsql {UPDATE sqlite_master SET name='xyz' WHERE name='123'}} msg] 31 lappend v $msg 32 } {1 {table sqlite_master may not be modified}} 33 34 # Create a table to work with 35 # 36 do_test update-3.1 { 37 execsql {CREATE TABLE test1(f1 int,f2 int)} 38 for {set i 1} {$i<=10} {incr i} { 39 set sql "INSERT INTO test1 VALUES($i,[expr {1<<$i}])" 40 execsql $sql 41 } 42 execsql {SELECT * FROM test1 ORDER BY f1} 43 } {1 2 2 4 3 8 4 16 5 32 6 64 7 128 8 256 9 512 10 1024} 44 45 # Unknown column name in an expression 46 # 47 do_test update-3.2 { 48 set v [catch {execsql {UPDATE test1 SET f1=f3*2 WHERE f2==32}} msg] 49 lappend v $msg 50 } {1 {no such column: f3}} 51 do_test update-3.3 { 52 set v [catch {execsql {UPDATE test1 SET f1=test2.f1*2 WHERE f2==32}} msg] 53 lappend v $msg 54 } {1 {no such column: test2.f1}} 55 do_test update-3.4 { 56 set v [catch {execsql {UPDATE test1 SET f3=f1*2 WHERE f2==32}} msg] 57 lappend v $msg 58 } {1 {no such column: f3}} 59 60 # Actually do some updates 61 # 62 do_test update-3.5 { 63 execsql {UPDATE test1 SET f2=f2*3} 64 } {} 65 do_test update-3.5.1 { 66 db changes 67 } {10} 68 69 # verify that SELECT does not reset the change counter 70 do_test update-3.5.2 { 71 db eval {SELECT count(*) FROM test1} 72 } {10} 73 do_test update-3.5.3 { 74 db changes 75 } {10} 76 77 do_test update-3.6 { 78 execsql {SELECT * FROM test1 ORDER BY f1} 79 } {1 6 2 12 3 24 4 48 5 96 6 192 7 384 8 768 9 1536 10 3072} 80 do_test update-3.7 { 81 execsql {PRAGMA count_changes=on} 82 execsql {UPDATE test1 SET f2=f2/3 WHERE f1<=5} 83 } {5} 84 do_test update-3.8 { 85 execsql {SELECT * FROM test1 ORDER BY f1} 86 } {1 2 2 4 3 8 4 16 5 32 6 192 7 384 8 768 9 1536 10 3072} 87 do_test update-3.9 { 88 execsql {UPDATE test1 SET f2=f2/3 WHERE f1>5} 89 } {5} 90 do_test update-3.10 { 91 execsql {SELECT * FROM test1 ORDER BY f1} 92 } {1 2 2 4 3 8 4 16 5 32 6 64 7 128 8 256 9 512 10 1024} 93 94 # Swap the values of f1 and f2 for all elements 95 # 96 do_test update-3.11 { 97 execsql {UPDATE test1 SET F2=f1, F1=f2} 98 } {10} 99 do_test update-3.12 { 100 execsql {SELECT * FROM test1 ORDER BY F1} 101 } {2 1 4 2 8 3 16 4 32 5 64 6 128 7 256 8 512 9 1024 10} 102 do_test update-3.13 { 103 execsql {PRAGMA count_changes=off} 104 execsql {UPDATE test1 SET F2=f1, F1=f2} 105 } {} 106 do_test update-3.14 { 107 execsql {SELECT * FROM test1 ORDER BY F1} 108 } {1 2 2 4 3 8 4 16 5 32 6 64 7 128 8 256 9 512 10 1024} 109 110 # Create duplicate entries and make sure updating still 111 # works. 112 # 113 do_test update-4.0 { 114 execsql { 115 DELETE FROM test1 WHERE f1<=5; 116 INSERT INTO test1(f1,f2) VALUES(8,88); 117 INSERT INTO test1(f1,f2) VALUES(8,888); 118 INSERT INTO test1(f1,f2) VALUES(77,128); 119 INSERT INTO test1(f1,f2) VALUES(777,128); 120 } 121 execsql {SELECT * FROM test1 ORDER BY f1,f2} 122 } {6 64 7 128 8 88 8 256 8 888 9 512 10 1024 77 128 777 128} 123 do_test update-4.1 { 124 execsql {UPDATE test1 SET f2=f2+1 WHERE f1==8} 125 execsql {SELECT * FROM test1 ORDER BY f1,f2} 126 } {6 64 7 128 8 89 8 257 8 889 9 512 10 1024 77 128 777 128} 127 do_test update-4.2 { 128 execsql {UPDATE test1 SET f2=f2-1 WHERE f1==8 and f2>800} 129 execsql {SELECT * FROM test1 ORDER BY f1,f2} 130 } {6 64 7 128 8 89 8 257 8 888 9 512 10 1024 77 128 777 128} 131 do_test update-4.3 { 132 execsql {UPDATE test1 SET f2=f2-1 WHERE f1==8 and f2<800} 133 execsql {SELECT * FROM test1 ORDER BY f1,f2} 134 } {6 64 7 128 8 88 8 256 8 888 9 512 10 1024 77 128 777 128} 135 do_test update-4.4 { 136 execsql {UPDATE test1 SET f1=f1+1 WHERE f2==128} 137 execsql {SELECT * FROM test1 ORDER BY f1,f2} 138 } {6 64 8 88 8 128 8 256 8 888 9 512 10 1024 78 128 778 128} 139 do_test update-4.5 { 140 execsql {UPDATE test1 SET f1=f1-1 WHERE f1>100 and f2==128} 141 execsql {SELECT * FROM test1 ORDER BY f1,f2} 142 } {6 64 8 88 8 128 8 256 8 888 9 512 10 1024 78 128 777 128} 143 do_test update-4.6 { 144 execsql { 145 PRAGMA count_changes=on; 146 UPDATE test1 SET f1=f1-1 WHERE f1<=100 and f2==128; 147 } 148 } {2} 149 do_test update-4.7 { 150 execsql { 151 PRAGMA count_changes=off; 152 SELECT * FROM test1 ORDER BY f1,f2 153 } 154 } {6 64 7 128 8 88 8 256 8 888 9 512 10 1024 77 128 777 128} 155 156 # Repeat the previous sequence of tests with an index. 157 # 158 do_test update-5.0 { 159 execsql {CREATE INDEX idx1 ON test1(f1)} 160 execsql {SELECT * FROM test1 ORDER BY f1,f2} 161 } {6 64 7 128 8 88 8 256 8 888 9 512 10 1024 77 128 777 128} 162 do_test update-5.1 { 163 execsql {UPDATE test1 SET f2=f2+1 WHERE f1==8} 164 execsql {SELECT * FROM test1 ORDER BY f1,f2} 165 } {6 64 7 128 8 89 8 257 8 889 9 512 10 1024 77 128 777 128} 166 do_test update-5.2 { 167 execsql {UPDATE test1 SET f2=f2-1 WHERE f1==8 and f2>800} 168 execsql {SELECT * FROM test1 ORDER BY f1,f2} 169 } {6 64 7 128 8 89 8 257 8 888 9 512 10 1024 77 128 777 128} 170 do_test update-5.3 { 171 execsql {UPDATE test1 SET f2=f2-1 WHERE f1==8 and f2<800} 172 execsql {SELECT * FROM test1 ORDER BY f1,f2} 173 } {6 64 7 128 8 88 8 256 8 888 9 512 10 1024 77 128 777 128} 174 do_test update-5.4 { 175 execsql {UPDATE test1 SET f1=f1+1 WHERE f2==128} 176 execsql {SELECT * FROM test1 ORDER BY f1,f2} 177 } {6 64 8 88 8 128 8 256 8 888 9 512 10 1024 78 128 778 128} 178 do_test update-5.4.1 { 179 execsql {SELECT * FROM test1 WHERE f1==78 ORDER BY f1,f2} 180 } {78 128} 181 do_test update-5.4.2 { 182 execsql {SELECT * FROM test1 WHERE f1==778 ORDER BY f1,f2} 183 } {778 128} 184 do_test update-5.4.3 { 185 execsql {SELECT * FROM test1 WHERE f1==8 ORDER BY f1,f2} 186 } {8 88 8 128 8 256 8 888} 187 do_test update-5.5 { 188 execsql {UPDATE test1 SET f1=f1-1 WHERE f1>100 and f2==128} 189 } {} 190 do_test update-5.5.1 { 191 execsql {SELECT * FROM test1 ORDER BY f1,f2} 192 } {6 64 8 88 8 128 8 256 8 888 9 512 10 1024 78 128 777 128} 193 do_test update-5.5.2 { 194 execsql {SELECT * FROM test1 WHERE f1==78 ORDER BY f1,f2} 195 } {78 128} 196 do_test update-5.5.3 { 197 execsql {SELECT * FROM test1 WHERE f1==778 ORDER BY f1,f2} 198 } {} 199 do_test update-5.5.4 { 200 execsql {SELECT * FROM test1 WHERE f1==777 ORDER BY f1,f2} 201 } {777 128} 202 do_test update-5.5.5 { 203 execsql {SELECT * FROM test1 WHERE f1==8 ORDER BY f1,f2} 204 } {8 88 8 128 8 256 8 888} 205 do_test update-5.6 { 206 execsql { 207 PRAGMA count_changes=on; 208 UPDATE test1 SET f1=f1-1 WHERE f1<=100 and f2==128; 209 } 210 } {2} 211 do_test update-5.6.1 { 212 execsql { 213 PRAGMA count_changes=off; 214 SELECT * FROM test1 ORDER BY f1,f2 215 } 216 } {6 64 7 128 8 88 8 256 8 888 9 512 10 1024 77 128 777 128} 217 do_test update-5.6.2 { 218 execsql {SELECT * FROM test1 WHERE f1==77 ORDER BY f1,f2} 219 } {77 128} 220 do_test update-5.6.3 { 221 execsql {SELECT * FROM test1 WHERE f1==778 ORDER BY f1,f2} 222 } {} 223 do_test update-5.6.4 { 224 execsql {SELECT * FROM test1 WHERE f1==777 ORDER BY f1,f2} 225 } {777 128} 226 do_test update-5.6.5 { 227 execsql {SELECT * FROM test1 WHERE f1==8 ORDER BY f1,f2} 228 } {8 88 8 256 8 888} 229 230 # Repeat the previous sequence of tests with a different index. 231 # 232 execsql {PRAGMA synchronous=FULL} 233 do_test update-6.0 { 234 execsql {DROP INDEX idx1} 235 execsql {CREATE INDEX idx1 ON test1(f2)} 236 execsql {SELECT * FROM test1 ORDER BY f1,f2} 237 } {6 64 7 128 8 88 8 256 8 888 9 512 10 1024 77 128 777 128} 238 do_test update-6.1 { 239 execsql {UPDATE test1 SET f2=f2+1 WHERE f1==8} 240 execsql {SELECT * FROM test1 ORDER BY f1,f2} 241 } {6 64 7 128 8 89 8 257 8 889 9 512 10 1024 77 128 777 128} 242 do_test update-6.1.1 { 243 execsql {SELECT * FROM test1 WHERE f1==8 ORDER BY f1,f2} 244 } {8 89 8 257 8 889} 245 do_test update-6.1.2 { 246 execsql {SELECT * FROM test1 WHERE f2==89 ORDER BY f1,f2} 247 } {8 89} 248 do_test update-6.1.3 { 249 execsql {SELECT * FROM test1 WHERE f1==88 ORDER BY f1,f2} 250 } {} 251 do_test update-6.2 { 252 execsql {UPDATE test1 SET f2=f2-1 WHERE f1==8 and f2>800} 253 execsql {SELECT * FROM test1 ORDER BY f1,f2} 254 } {6 64 7 128 8 89 8 257 8 888 9 512 10 1024 77 128 777 128} 255 do_test update-6.3 { 256 execsql {UPDATE test1 SET f2=f2-1 WHERE f1==8 and f2<800} 257 execsql {SELECT * FROM test1 ORDER BY f1,f2} 258 } {6 64 7 128 8 88 8 256 8 888 9 512 10 1024 77 128 777 128} 259 do_test update-6.3.1 { 260 execsql {SELECT * FROM test1 WHERE f1==8 ORDER BY f1,f2} 261 } {8 88 8 256 8 888} 262 do_test update-6.3.2 { 263 execsql {SELECT * FROM test1 WHERE f2==89 ORDER BY f1,f2} 264 } {} 265 do_test update-6.3.3 { 266 execsql {SELECT * FROM test1 WHERE f2==88 ORDER BY f1,f2} 267 } {8 88} 268 do_test update-6.4 { 269 execsql {UPDATE test1 SET f1=f1+1 WHERE f2==128} 270 execsql {SELECT * FROM test1 ORDER BY f1,f2} 271 } {6 64 8 88 8 128 8 256 8 888 9 512 10 1024 78 128 778 128} 272 do_test update-6.4.1 { 273 execsql {SELECT * FROM test1 WHERE f1==78 ORDER BY f1,f2} 274 } {78 128} 275 do_test update-6.4.2 { 276 execsql {SELECT * FROM test1 WHERE f1==778 ORDER BY f1,f2} 277 } {778 128} 278 do_test update-6.4.3 { 279 execsql {SELECT * FROM test1 WHERE f1==8 ORDER BY f1,f2} 280 } {8 88 8 128 8 256 8 888} 281 do_test update-6.5 { 282 execsql {UPDATE test1 SET f1=f1-1 WHERE f1>100 and f2==128} 283 execsql {SELECT * FROM test1 ORDER BY f1,f2} 284 } {6 64 8 88 8 128 8 256 8 888 9 512 10 1024 78 128 777 128} 285 do_test update-6.5.1 { 286 execsql {SELECT * FROM test1 WHERE f1==78 ORDER BY f1,f2} 287 } {78 128} 288 do_test update-6.5.2 { 289 execsql {SELECT * FROM test1 WHERE f1==778 ORDER BY f1,f2} 290 } {} 291 do_test update-6.5.3 { 292 execsql {SELECT * FROM test1 WHERE f1==777 ORDER BY f1,f2} 293 } {777 128} 294 do_test update-6.5.4 { 295 execsql {SELECT * FROM test1 WHERE f1==8 ORDER BY f1,f2} 296 } {8 88 8 128 8 256 8 888} 297 do_test update-6.6 { 298 execsql {UPDATE test1 SET f1=f1-1 WHERE f1<=100 and f2==128} 299 execsql {SELECT * FROM test1 ORDER BY f1,f2} 300 } {6 64 7 128 8 88 8 256 8 888 9 512 10 1024 77 128 777 128} 301 do_test update-6.6.1 { 302 execsql {SELECT * FROM test1 WHERE f1==77 ORDER BY f1,f2} 303 } {77 128} 304 do_test update-6.6.2 { 305 execsql {SELECT * FROM test1 WHERE f1==778 ORDER BY f1,f2} 306 } {} 307 do_test update-6.6.3 { 308 execsql {SELECT * FROM test1 WHERE f1==777 ORDER BY f1,f2} 309 } {777 128} 310 do_test update-6.6.4 { 311 execsql {SELECT * FROM test1 WHERE f1==8 ORDER BY f1,f2} 312 } {8 88 8 256 8 888} 313 314 # Repeat the previous sequence of tests with multiple 315 # indices 316 # 317 do_test update-7.0 { 318 execsql {CREATE INDEX idx2 ON test1(f2)} 319 execsql {CREATE INDEX idx3 ON test1(f1,f2)} 320 execsql {SELECT * FROM test1 ORDER BY f1,f2} 321 } {6 64 7 128 8 88 8 256 8 888 9 512 10 1024 77 128 777 128} 322 do_test update-7.1 { 323 execsql {UPDATE test1 SET f2=f2+1 WHERE f1==8} 324 execsql {SELECT * FROM test1 ORDER BY f1,f2} 325 } {6 64 7 128 8 89 8 257 8 889 9 512 10 1024 77 128 777 128} 326 do_test update-7.1.1 { 327 execsql {SELECT * FROM test1 WHERE f1==8 ORDER BY f1,f2} 328 } {8 89 8 257 8 889} 329 do_test update-7.1.2 { 330 execsql {SELECT * FROM test1 WHERE f2==89 ORDER BY f1,f2} 331 } {8 89} 332 do_test update-7.1.3 { 333 execsql {SELECT * FROM test1 WHERE f1==88 ORDER BY f1,f2} 334 } {} 335 do_test update-7.2 { 336 execsql {UPDATE test1 SET f2=f2-1 WHERE f1==8 and f2>800} 337 execsql {SELECT * FROM test1 ORDER BY f1,f2} 338 } {6 64 7 128 8 89 8 257 8 888 9 512 10 1024 77 128 777 128} 339 do_test update-7.3 { 340 # explain {UPDATE test1 SET f2=f2-1 WHERE f1==8 and F2<300} 341 execsql {UPDATE test1 SET f2=f2-1 WHERE f1==8 and f2<800} 342 execsql {SELECT * FROM test1 ORDER BY f1,f2} 343 } {6 64 7 128 8 88 8 256 8 888 9 512 10 1024 77 128 777 128} 344 do_test update-7.3.1 { 345 execsql {SELECT * FROM test1 WHERE f1==8 ORDER BY f1,f2} 346 } {8 88 8 256 8 888} 347 do_test update-7.3.2 { 348 execsql {SELECT * FROM test1 WHERE f2==89 ORDER BY f1,f2} 349 } {} 350 do_test update-7.3.3 { 351 execsql {SELECT * FROM test1 WHERE f2==88 ORDER BY f1,f2} 352 } {8 88} 353 do_test update-7.4 { 354 execsql {UPDATE test1 SET f1=f1+1 WHERE 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 778 128} 357 do_test update-7.4.1 { 358 execsql {SELECT * FROM test1 WHERE f1==78 ORDER BY f1,f2} 359 } {78 128} 360 do_test update-7.4.2 { 361 execsql {SELECT * FROM test1 WHERE f1==778 ORDER BY f1,f2} 362 } {778 128} 363 do_test update-7.4.3 { 364 execsql {SELECT * FROM test1 WHERE f1==8 ORDER BY f1,f2} 365 } {8 88 8 128 8 256 8 888} 366 do_test update-7.5 { 367 execsql {UPDATE test1 SET f1=f1-1 WHERE f1>100 and f2==128} 368 execsql {SELECT * FROM test1 ORDER BY f1,f2} 369 } {6 64 8 88 8 128 8 256 8 888 9 512 10 1024 78 128 777 128} 370 do_test update-7.5.1 { 371 execsql {SELECT * FROM test1 WHERE f1==78 ORDER BY f1,f2} 372 } {78 128} 373 do_test update-7.5.2 { 374 execsql {SELECT * FROM test1 WHERE f1==778 ORDER BY f1,f2} 375 } {} 376 do_test update-7.5.3 { 377 execsql {SELECT * FROM test1 WHERE f1==777 ORDER BY f1,f2} 378 } {777 128} 379 do_test update-7.5.4 { 380 execsql {SELECT * FROM test1 WHERE f1==8 ORDER BY f1,f2} 381 } {8 88 8 128 8 256 8 888} 382 do_test update-7.6 { 383 execsql {UPDATE test1 SET f1=f1-1 WHERE f1<=100 and f2==128} 384 execsql {SELECT * FROM test1 ORDER BY f1,f2} 385 } {6 64 7 128 8 88 8 256 8 888 9 512 10 1024 77 128 777 128} 386 do_test update-7.6.1 { 387 execsql {SELECT * FROM test1 WHERE f1==77 ORDER BY f1,f2} 388 } {77 128} 389 do_test update-7.6.2 { 390 execsql {SELECT * FROM test1 WHERE f1==778 ORDER BY f1,f2} 391 } {} 392 do_test update-7.6.3 { 393 execsql {SELECT * FROM test1 WHERE f1==777 ORDER BY f1,f2} 394 } {777 128} 395 do_test update-7.6.4 { 396 execsql {SELECT * FROM test1 WHERE f1==8 ORDER BY f1,f2} 397 } {8 88 8 256 8 888} 398 399 # Error messages 400 # 401 do_test update-9.1 { 402 set v [catch {execsql { 403 UPDATE test1 SET x=11 WHERE f1=1025 404 }} msg] 405 lappend v $msg 406 } {1 {no such column: x}} 407 do_test update-9.2 { 408 set v [catch {execsql { 409 UPDATE test1 SET f1=x(11) WHERE f1=1025 410 }} msg] 411 lappend v $msg 412 } {1 {no such function: x}} 413 do_test update-9.3 { 414 set v [catch {execsql { 415 UPDATE test1 SET f1=11 WHERE x=1025 416 }} msg] 417 lappend v $msg 418 } {1 {no such column: x}} 419 do_test update-9.4 { 420 set v [catch {execsql { 421 UPDATE test1 SET f1=11 WHERE x(f1)=1025 422 }} msg] 423 lappend v $msg 424 } {1 {no such function: x}} 425 426 # Try doing updates on a unique column where the value does not 427 # really change. 428 # 429 do_test update-10.1 { 430 execsql { 431 DROP TABLE test1; 432 CREATE TABLE t1( 433 a integer primary key, 434 b UNIQUE, 435 c, d, 436 e, f, 437 UNIQUE(c,d) 438 ); 439 INSERT INTO t1 VALUES(1,2,3,4,5,6); 440 INSERT INTO t1 VALUES(2,3,4,4,6,7); 441 SELECT * FROM t1 442 } 443 } {1 2 3 4 5 6 2 3 4 4 6 7} 444 do_test update-10.2 { 445 catchsql { 446 UPDATE t1 SET a=1, e=9 WHERE f=6; 447 SELECT * FROM t1; 448 } 449 } {0 {1 2 3 4 9 6 2 3 4 4 6 7}} 450 do_test update-10.3 { 451 catchsql { 452 UPDATE t1 SET a=1, e=10 WHERE f=7; 453 SELECT * FROM t1; 454 } 455 } {1 {UNIQUE constraint failed: t1.a}} 456 do_test update-10.4 { 457 catchsql { 458 SELECT * FROM t1; 459 } 460 } {0 {1 2 3 4 9 6 2 3 4 4 6 7}} 461 do_test update-10.5 { 462 catchsql { 463 UPDATE t1 SET b=2, e=11 WHERE f=6; 464 SELECT * FROM t1; 465 } 466 } {0 {1 2 3 4 11 6 2 3 4 4 6 7}} 467 do_test update-10.6 { 468 catchsql { 469 UPDATE t1 SET b=2, e=12 WHERE f=7; 470 SELECT * FROM t1; 471 } 472 } {1 {UNIQUE constraint failed: t1.b}} 473 do_test update-10.7 { 474 catchsql { 475 SELECT * FROM t1; 476 } 477 } {0 {1 2 3 4 11 6 2 3 4 4 6 7}} 478 do_test update-10.8 { 479 catchsql { 480 UPDATE t1 SET c=3, d=4, e=13 WHERE f=6; 481 SELECT * FROM t1; 482 } 483 } {0 {1 2 3 4 13 6 2 3 4 4 6 7}} 484 do_test update-10.9 { 485 catchsql { 486 UPDATE t1 SET c=3, d=4, e=14 WHERE f=7; 487 SELECT * FROM t1; 488 } 489 } {1 {UNIQUE constraint failed: t1.c, t1.d}} 490 do_test update-10.10 { 491 catchsql { 492 SELECT * FROM t1; 493 } 494 } {0 {1 2 3 4 13 6 2 3 4 4 6 7}} 495 496 # Make sure we can handle a subquery in the where clause. 497 # 498 ifcapable subquery { 499 do_test update-11.1 { 500 execsql { 501 UPDATE t1 SET e=e+1 WHERE b IN (SELECT b FROM t1); 502 SELECT b,e FROM t1; 503 } 504 } {2 14 3 7} 505 do_test update-11.2 { 506 execsql { 507 UPDATE t1 SET e=e+1 WHERE a IN (SELECT a FROM t1); 508 SELECT a,e FROM t1; 509 } 510 } {1 15 2 8} 511 do_test update-11.3 { 512 execsql { 513 UPDATE t1 AS xyz SET e=e+1 WHERE xyz.a IN (SELECT a FROM t1); 514 SELECT a,e FROM t1; 515 } 516 } {1 16 2 9} 517 do_test update-11.4 { 518 execsql { 519 UPDATE t1 AS xyz SET e=e+1 WHERE EXISTS(SELECT 1 FROM t1 WHERE t1.a=2; 703 SELECT * FROM t1; 704 } {2 2} 705 706 # 2019-12-29 ticket https://sqlite.org/src/info/`314cc133e5ada126` 707 # REPLACE conflict resolution during an UPDATE causes a DELETE trigger 708 # to fire. If that DELETE trigger subsequently modifies the row 709 # being updated, bad things can happen. Prevent this by prohibiting 710 # triggers from making changes to the table being updated while doing 711 # REPLACE conflict resolution on the UPDATE. 712 # 713 # See also tickets: 714 # https://sqlite.org/src/info/`c1e19e12046d23fe` 2019-10-25 715 # https://sqlite.org/src/info/`a8a4847a2d96f5de` 2019-10-16 716 # 717 reset_db 718 do_execsql_test update-20.10 { 719 PRAGMA recursive_triggers = true; 720 CREATE TABLE t1(a UNIQUE ON CONFLICT REPLACE, b); 721 INSERT INTO t1(a,b) VALUES(4,12),(9,13); 722 CREATE INDEX i0 ON t1(b); 723 CREATE TRIGGER tr0 DELETE ON t1 BEGIN 724 UPDATE t1 SET b = a; 725 END; 726 PRAGMA integrity_check; 727 } {ok} 728 do_catchsql_test update-20.20 { 729 UPDATE t1 SET a=0; 730 } {1 {constraint failed}} 731 do_execsql_test update-20.30 { 732 PRAGMA integrity_check; 733 } {ok} 734 735 # 2023-03-16 https://sqlite.org/forum/forumpost/`0007d1fdb1` 736 # A subquery in the WHERE clause of an UPDATE and behind a 737 # short-circuit evaluation caused problems because multi-row 738 # single-pass was selected. 739 # 740 # Similar problem for DELETE tested by delete-12.0. 741 # https://sqlite.org/src/info/`73f0036f045bf371` 742 # 743 reset_db 744 do_execsql_test update-21.1 { 745 CREATE TABLE t1 (vkey INTEGER, c5 INTEGER); 746 INSERT INTO t1 VALUES(3,NULL),(6,-54); 747 } 748 db null NULL 749 do_execsql_test update-21.2 { 750 BEGIN; 751 UPDATE t1 SET vkey = 100 WHERE c5 is null; 752 SELECT * FROM t1 ORDER BY vkey, c5; 753 ROLLBACK; 754 } {6 -54 100 NULL} 755 do_execsql_test update-21.3 { 756 BEGIN; 757 UPDATE t1 SET vkey = 100 WHERE NOT (-10*(select min(vkey) from t1) >= c5); 758 SELECT * FROM t1 ORDER BY vkey, c5; 759 ROLLBACK; 760 } {3 NULL 6 -54} 761 do_execsql_test update-21.4 { 762 BEGIN; 763 UPDATE t1 SET vkey = 100 WHERE c5 is null OR NOT (-10*(select min(vkey) from t1) >= c5); 764 SELECT * FROM t1 ORDER BY vkey, c5; 765 ROLLBACK; 766 } {6 -54 100 NULL} 767 # Follow-up on 2023-07-31 (forum post https://sqlite.org/forum/forumpost/`8ab195fd44e75ed0`): 768 # Only disable one-pass if the subquery is in the WHERE clause. The SET expressions 769 # do not count. 770 do_execsql_test update-21.11 { 771 DROP TABLE t1; 772 CREATE TABLE t1(a INTEGER PRIMARY KEY, b INT); 773 CREATE TABLE t2(d INT); 774 } 775 do_eqp_test update-21.12 { 776 WITH t3(x,y) AS (SELECT d, row_number()OVER() FROM t2) 777 UPDATE t1 SET b=(SELECT y FROM t3 WHERE t1.a=t3.x); 778 } { 779 QUERY PLAN 780 |--SCAN t1 781 `--CORRELATED SCALAR SUBQUERY xxxxxx 782 |--CO-ROUTINE t3 783 | |--CO-ROUTINE (subquery-xxxxxx) 784 | | `--SCAN t2 785 | `--SCAN (subquery-xxxxxx) 786 |--BLOOM FILTER ON t3 (x=?) 787 `--SEARCH t3 USING AUTOMATIC COVERING INDEX (x=?) 788 } 789 790 # /forumpost/2026-04-21T19:10:55Z 791 do_execsql_test update-22.0 { 792 DROP TABLE IF EXISTS t1; 793 CREATE TABLE t1(x INT, y INT); 794 INSERT INTO t1(x) VALUES(1),(2),(3),(4),(5); 795 UPDATE t1 SET x=x+100, y=x<=(SELECT min(x) FROM t1) 796 WHERE x<3 OR (1 BETWEEN 0 AND x<=(SELECT min(x)+2 FROM t1)); 797 SELECT x FROM t1 WHERE x<100 ORDER BY x; 798 } {4 5} 799 800 finish_test