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