drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 1 | # 2001 September 15 |
drh | 1d37e28 | 2000-05-30 03:12:21 +0000 | [diff] [blame] | 2 | # |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 3 | # The author disclaims copyright to this source code. In place of |
| 4 | # a legal notice, here is a blessing: |
drh | 1d37e28 | 2000-05-30 03:12:21 +0000 | [diff] [blame] | 5 | # |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 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. |
drh | 1d37e28 | 2000-05-30 03:12:21 +0000 | [diff] [blame] | 9 | # |
| 10 | #*********************************************************************** |
| 11 | # This file implements regression tests for SQLite library. The |
| 12 | # focus of this file is testing the UPDATE statement. |
| 13 | # |
drh | 37ed48e | 2003-08-05 13:13:38 +0000 | [diff] [blame^] | 14 | # $Id: update.test,v 1.14 2003/08/05 13:13:39 drh Exp $ |
drh | 1d37e28 | 2000-05-30 03:12:21 +0000 | [diff] [blame] | 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 {int(pow(2,$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 | |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 45 | # Unknown column name in an expression |
drh | 1d37e28 | 2000-05-30 03:12:21 +0000 | [diff] [blame] | 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 |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 50 | } {1 {no such column: f3}} |
drh | 1d37e28 | 2000-05-30 03:12:21 +0000 | [diff] [blame] | 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 |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 54 | } {1 {no such column: test2.f1}} |
drh | 1d37e28 | 2000-05-30 03:12:21 +0000 | [diff] [blame] | 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 |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 58 | } {1 {no such column: f3}} |
drh | 1d37e28 | 2000-05-30 03:12:21 +0000 | [diff] [blame] | 59 | |
| 60 | # Actually do some updates |
| 61 | # |
| 62 | do_test update-3.5 { |
| 63 | execsql {UPDATE test1 SET f2=f2*3} |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 64 | } {} |
| 65 | do_test update-3.6 { |
drh | 1d37e28 | 2000-05-30 03:12:21 +0000 | [diff] [blame] | 66 | execsql {SELECT * FROM test1 ORDER BY f1} |
| 67 | } {1 6 2 12 3 24 4 48 5 96 6 192 7 384 8 768 9 1536 10 3072} |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 68 | do_test update-3.7 { |
| 69 | execsql {PRAGMA count_changes=on} |
drh | 1d37e28 | 2000-05-30 03:12:21 +0000 | [diff] [blame] | 70 | execsql {UPDATE test1 SET f2=f2/3 WHERE f1<=5} |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 71 | } {5} |
| 72 | do_test update-3.8 { |
drh | 1d37e28 | 2000-05-30 03:12:21 +0000 | [diff] [blame] | 73 | execsql {SELECT * FROM test1 ORDER BY f1} |
| 74 | } {1 2 2 4 3 8 4 16 5 32 6 192 7 384 8 768 9 1536 10 3072} |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 75 | do_test update-3.9 { |
drh | 1d37e28 | 2000-05-30 03:12:21 +0000 | [diff] [blame] | 76 | execsql {UPDATE test1 SET f2=f2/3 WHERE f1>5} |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 77 | } {5} |
| 78 | do_test update-3.10 { |
drh | 1d37e28 | 2000-05-30 03:12:21 +0000 | [diff] [blame] | 79 | execsql {SELECT * FROM test1 ORDER BY f1} |
| 80 | } {1 2 2 4 3 8 4 16 5 32 6 64 7 128 8 256 9 512 10 1024} |
| 81 | |
| 82 | # Swap the values of f1 and f2 for all elements |
| 83 | # |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 84 | do_test update-3.11 { |
drh | 6206d50 | 2000-06-19 19:09:08 +0000 | [diff] [blame] | 85 | execsql {UPDATE test1 SET F2=f1, F1=f2} |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 86 | } {10} |
| 87 | do_test update-3.12 { |
drh | 6206d50 | 2000-06-19 19:09:08 +0000 | [diff] [blame] | 88 | execsql {SELECT * FROM test1 ORDER BY F1} |
drh | 1d37e28 | 2000-05-30 03:12:21 +0000 | [diff] [blame] | 89 | } {2 1 4 2 8 3 16 4 32 5 64 6 128 7 256 8 512 9 1024 10} |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 90 | do_test update-3.13 { |
| 91 | execsql {PRAGMA count_changes=off} |
drh | 4f3c63e | 2001-02-19 18:24:21 +0000 | [diff] [blame] | 92 | execsql {UPDATE test1 SET F2=f1, F1=f2} |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 93 | } {} |
| 94 | do_test update-3.14 { |
drh | 4f3c63e | 2001-02-19 18:24:21 +0000 | [diff] [blame] | 95 | execsql {SELECT * FROM test1 ORDER BY F1} |
| 96 | } {1 2 2 4 3 8 4 16 5 32 6 64 7 128 8 256 9 512 10 1024} |
| 97 | |
| 98 | # Create duplicate entries and make sure updating still |
| 99 | # works. |
| 100 | # |
| 101 | do_test update-4.0 { |
| 102 | execsql { |
| 103 | DELETE FROM test1 WHERE f1<=5; |
| 104 | INSERT INTO test1(f1,f2) VALUES(8,88); |
| 105 | INSERT INTO test1(f1,f2) VALUES(8,888); |
| 106 | INSERT INTO test1(f1,f2) VALUES(77,128); |
| 107 | INSERT INTO test1(f1,f2) VALUES(777,128); |
| 108 | } |
| 109 | execsql {SELECT * FROM test1 ORDER BY f1,f2} |
| 110 | } {6 64 7 128 8 88 8 256 8 888 9 512 10 1024 77 128 777 128} |
| 111 | do_test update-4.1 { |
| 112 | execsql {UPDATE test1 SET f2=f2+1 WHERE f1==8} |
| 113 | execsql {SELECT * FROM test1 ORDER BY f1,f2} |
| 114 | } {6 64 7 128 8 89 8 257 8 889 9 512 10 1024 77 128 777 128} |
| 115 | do_test update-4.2 { |
| 116 | execsql {UPDATE test1 SET f2=f2-1 WHERE f1==8 and f2>800} |
| 117 | execsql {SELECT * FROM test1 ORDER BY f1,f2} |
| 118 | } {6 64 7 128 8 89 8 257 8 888 9 512 10 1024 77 128 777 128} |
| 119 | do_test update-4.3 { |
| 120 | execsql {UPDATE test1 SET f2=f2-1 WHERE f1==8 and f2<800} |
| 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.4 { |
| 124 | execsql {UPDATE test1 SET f1=f1+1 WHERE f2==128} |
| 125 | execsql {SELECT * FROM test1 ORDER BY f1,f2} |
| 126 | } {6 64 8 88 8 128 8 256 8 888 9 512 10 1024 78 128 778 128} |
| 127 | do_test update-4.5 { |
| 128 | execsql {UPDATE test1 SET f1=f1-1 WHERE f1>100 and f2==128} |
| 129 | execsql {SELECT * FROM test1 ORDER BY f1,f2} |
| 130 | } {6 64 8 88 8 128 8 256 8 888 9 512 10 1024 78 128 777 128} |
| 131 | do_test update-4.6 { |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 132 | execsql { |
| 133 | PRAGMA count_changes=on; |
| 134 | UPDATE test1 SET f1=f1-1 WHERE f1<=100 and f2==128; |
| 135 | } |
| 136 | } {2} |
| 137 | do_test update-4.7 { |
| 138 | execsql { |
| 139 | PRAGMA count_changes=off; |
| 140 | SELECT * FROM test1 ORDER BY f1,f2 |
| 141 | } |
drh | 4f3c63e | 2001-02-19 18:24:21 +0000 | [diff] [blame] | 142 | } {6 64 7 128 8 88 8 256 8 888 9 512 10 1024 77 128 777 128} |
| 143 | |
| 144 | # Repeat the previous sequence of tests with an index. |
| 145 | # |
| 146 | do_test update-5.0 { |
| 147 | execsql {CREATE INDEX idx1 ON test1(f1)} |
| 148 | execsql {SELECT * FROM test1 ORDER BY f1,f2} |
| 149 | } {6 64 7 128 8 88 8 256 8 888 9 512 10 1024 77 128 777 128} |
| 150 | do_test update-5.1 { |
| 151 | execsql {UPDATE test1 SET f2=f2+1 WHERE f1==8} |
| 152 | execsql {SELECT * FROM test1 ORDER BY f1,f2} |
| 153 | } {6 64 7 128 8 89 8 257 8 889 9 512 10 1024 77 128 777 128} |
| 154 | do_test update-5.2 { |
| 155 | execsql {UPDATE test1 SET f2=f2-1 WHERE f1==8 and f2>800} |
| 156 | execsql {SELECT * FROM test1 ORDER BY f1,f2} |
| 157 | } {6 64 7 128 8 89 8 257 8 888 9 512 10 1024 77 128 777 128} |
| 158 | do_test update-5.3 { |
| 159 | execsql {UPDATE test1 SET f2=f2-1 WHERE f1==8 and f2<800} |
| 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.4 { |
| 163 | execsql {UPDATE test1 SET f1=f1+1 WHERE f2==128} |
| 164 | execsql {SELECT * FROM test1 ORDER BY f1,f2} |
| 165 | } {6 64 8 88 8 128 8 256 8 888 9 512 10 1024 78 128 778 128} |
| 166 | do_test update-5.4.1 { |
| 167 | execsql {SELECT * FROM test1 WHERE f1==78 ORDER BY f1,f2} |
| 168 | } {78 128} |
| 169 | do_test update-5.4.2 { |
| 170 | execsql {SELECT * FROM test1 WHERE f1==778 ORDER BY f1,f2} |
| 171 | } {778 128} |
| 172 | do_test update-5.4.3 { |
| 173 | execsql {SELECT * FROM test1 WHERE f1==8 ORDER BY f1,f2} |
| 174 | } {8 88 8 128 8 256 8 888} |
| 175 | do_test update-5.5 { |
| 176 | execsql {UPDATE test1 SET f1=f1-1 WHERE f1>100 and f2==128} |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 177 | } {} |
| 178 | do_test update-5.5.1 { |
drh | 4f3c63e | 2001-02-19 18:24:21 +0000 | [diff] [blame] | 179 | execsql {SELECT * FROM test1 ORDER BY f1,f2} |
| 180 | } {6 64 8 88 8 128 8 256 8 888 9 512 10 1024 78 128 777 128} |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 181 | do_test update-5.5.2 { |
drh | 4f3c63e | 2001-02-19 18:24:21 +0000 | [diff] [blame] | 182 | execsql {SELECT * FROM test1 WHERE f1==78 ORDER BY f1,f2} |
| 183 | } {78 128} |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 184 | do_test update-5.5.3 { |
drh | 4f3c63e | 2001-02-19 18:24:21 +0000 | [diff] [blame] | 185 | execsql {SELECT * FROM test1 WHERE f1==778 ORDER BY f1,f2} |
| 186 | } {} |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 187 | do_test update-5.5.4 { |
drh | 4f3c63e | 2001-02-19 18:24:21 +0000 | [diff] [blame] | 188 | execsql {SELECT * FROM test1 WHERE f1==777 ORDER BY f1,f2} |
| 189 | } {777 128} |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 190 | do_test update-5.5.5 { |
drh | 4f3c63e | 2001-02-19 18:24:21 +0000 | [diff] [blame] | 191 | execsql {SELECT * FROM test1 WHERE f1==8 ORDER BY f1,f2} |
| 192 | } {8 88 8 128 8 256 8 888} |
| 193 | do_test update-5.6 { |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 194 | execsql { |
| 195 | PRAGMA count_changes=on; |
| 196 | UPDATE test1 SET f1=f1-1 WHERE f1<=100 and f2==128; |
| 197 | } |
| 198 | } {2} |
drh | 4f3c63e | 2001-02-19 18:24:21 +0000 | [diff] [blame] | 199 | do_test update-5.6.1 { |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 200 | execsql { |
| 201 | PRAGMA count_changes=off; |
| 202 | SELECT * FROM test1 ORDER BY f1,f2 |
| 203 | } |
| 204 | } {6 64 7 128 8 88 8 256 8 888 9 512 10 1024 77 128 777 128} |
| 205 | do_test update-5.6.2 { |
drh | 4f3c63e | 2001-02-19 18:24:21 +0000 | [diff] [blame] | 206 | execsql {SELECT * FROM test1 WHERE f1==77 ORDER BY f1,f2} |
| 207 | } {77 128} |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 208 | do_test update-5.6.3 { |
drh | 4f3c63e | 2001-02-19 18:24:21 +0000 | [diff] [blame] | 209 | execsql {SELECT * FROM test1 WHERE f1==778 ORDER BY f1,f2} |
| 210 | } {} |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 211 | do_test update-5.6.4 { |
drh | 4f3c63e | 2001-02-19 18:24:21 +0000 | [diff] [blame] | 212 | execsql {SELECT * FROM test1 WHERE f1==777 ORDER BY f1,f2} |
| 213 | } {777 128} |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 214 | do_test update-5.6.5 { |
drh | 4f3c63e | 2001-02-19 18:24:21 +0000 | [diff] [blame] | 215 | execsql {SELECT * FROM test1 WHERE f1==8 ORDER BY f1,f2} |
| 216 | } {8 88 8 256 8 888} |
| 217 | |
| 218 | # Repeat the previous sequence of tests with a different index. |
| 219 | # |
drh | 4303fee | 2003-02-15 23:09:17 +0000 | [diff] [blame] | 220 | execsql {PRAGMA synchronous=FULL} |
drh | 4f3c63e | 2001-02-19 18:24:21 +0000 | [diff] [blame] | 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 | |
| 302 | # Repeat the previous sequence of tests with multiple |
| 303 | # indices |
| 304 | # |
| 305 | do_test update-7.0 { |
| 306 | execsql {CREATE INDEX idx2 ON test1(f2)} |
| 307 | execsql {CREATE INDEX idx3 ON test1(f1,f2)} |
| 308 | execsql {SELECT * FROM test1 ORDER BY f1,f2} |
| 309 | } {6 64 7 128 8 88 8 256 8 888 9 512 10 1024 77 128 777 128} |
| 310 | do_test update-7.1 { |
| 311 | execsql {UPDATE test1 SET f2=f2+1 WHERE f1==8} |
| 312 | execsql {SELECT * FROM test1 ORDER BY f1,f2} |
| 313 | } {6 64 7 128 8 89 8 257 8 889 9 512 10 1024 77 128 777 128} |
| 314 | do_test update-7.1.1 { |
| 315 | execsql {SELECT * FROM test1 WHERE f1==8 ORDER BY f1,f2} |
| 316 | } {8 89 8 257 8 889} |
| 317 | do_test update-7.1.2 { |
| 318 | execsql {SELECT * FROM test1 WHERE f2==89 ORDER BY f1,f2} |
| 319 | } {8 89} |
| 320 | do_test update-7.1.3 { |
| 321 | execsql {SELECT * FROM test1 WHERE f1==88 ORDER BY f1,f2} |
| 322 | } {} |
| 323 | do_test update-7.2 { |
| 324 | execsql {UPDATE test1 SET f2=f2-1 WHERE f1==8 and f2>800} |
| 325 | execsql {SELECT * FROM test1 ORDER BY f1,f2} |
| 326 | } {6 64 7 128 8 89 8 257 8 888 9 512 10 1024 77 128 777 128} |
| 327 | do_test update-7.3 { |
drh | 0409648 | 2001-11-09 22:41:44 +0000 | [diff] [blame] | 328 | # explain {UPDATE test1 SET f2=f2-1 WHERE f1==8 and F2<300} |
drh | 4f3c63e | 2001-02-19 18:24:21 +0000 | [diff] [blame] | 329 | execsql {UPDATE test1 SET f2=f2-1 WHERE f1==8 and f2<800} |
| 330 | execsql {SELECT * FROM test1 ORDER BY f1,f2} |
| 331 | } {6 64 7 128 8 88 8 256 8 888 9 512 10 1024 77 128 777 128} |
| 332 | do_test update-7.3.1 { |
| 333 | execsql {SELECT * FROM test1 WHERE f1==8 ORDER BY f1,f2} |
| 334 | } {8 88 8 256 8 888} |
| 335 | do_test update-7.3.2 { |
| 336 | execsql {SELECT * FROM test1 WHERE f2==89 ORDER BY f1,f2} |
| 337 | } {} |
| 338 | do_test update-7.3.3 { |
| 339 | execsql {SELECT * FROM test1 WHERE f2==88 ORDER BY f1,f2} |
| 340 | } {8 88} |
| 341 | do_test update-7.4 { |
| 342 | execsql {UPDATE test1 SET f1=f1+1 WHERE f2==128} |
| 343 | execsql {SELECT * FROM test1 ORDER BY f1,f2} |
| 344 | } {6 64 8 88 8 128 8 256 8 888 9 512 10 1024 78 128 778 128} |
| 345 | do_test update-7.4.1 { |
| 346 | execsql {SELECT * FROM test1 WHERE f1==78 ORDER BY f1,f2} |
| 347 | } {78 128} |
| 348 | do_test update-7.4.2 { |
| 349 | execsql {SELECT * FROM test1 WHERE f1==778 ORDER BY f1,f2} |
| 350 | } {778 128} |
| 351 | do_test update-7.4.3 { |
| 352 | execsql {SELECT * FROM test1 WHERE f1==8 ORDER BY f1,f2} |
| 353 | } {8 88 8 128 8 256 8 888} |
| 354 | do_test update-7.5 { |
| 355 | execsql {UPDATE test1 SET f1=f1-1 WHERE f1>100 and f2==128} |
| 356 | execsql {SELECT * FROM test1 ORDER BY f1,f2} |
| 357 | } {6 64 8 88 8 128 8 256 8 888 9 512 10 1024 78 128 777 128} |
| 358 | do_test update-7.5.1 { |
| 359 | execsql {SELECT * FROM test1 WHERE f1==78 ORDER BY f1,f2} |
| 360 | } {78 128} |
| 361 | do_test update-7.5.2 { |
| 362 | execsql {SELECT * FROM test1 WHERE f1==778 ORDER BY f1,f2} |
| 363 | } {} |
| 364 | do_test update-7.5.3 { |
| 365 | execsql {SELECT * FROM test1 WHERE f1==777 ORDER BY f1,f2} |
| 366 | } {777 128} |
| 367 | do_test update-7.5.4 { |
| 368 | execsql {SELECT * FROM test1 WHERE f1==8 ORDER BY f1,f2} |
| 369 | } {8 88 8 128 8 256 8 888} |
| 370 | do_test update-7.6 { |
| 371 | execsql {UPDATE test1 SET f1=f1-1 WHERE f1<=100 and f2==128} |
| 372 | execsql {SELECT * FROM test1 ORDER BY f1,f2} |
| 373 | } {6 64 7 128 8 88 8 256 8 888 9 512 10 1024 77 128 777 128} |
| 374 | do_test update-7.6.1 { |
| 375 | execsql {SELECT * FROM test1 WHERE f1==77 ORDER BY f1,f2} |
| 376 | } {77 128} |
| 377 | do_test update-7.6.2 { |
| 378 | execsql {SELECT * FROM test1 WHERE f1==778 ORDER BY f1,f2} |
| 379 | } {} |
| 380 | do_test update-7.6.3 { |
| 381 | execsql {SELECT * FROM test1 WHERE f1==777 ORDER BY f1,f2} |
| 382 | } {777 128} |
| 383 | do_test update-7.6.4 { |
| 384 | execsql {SELECT * FROM test1 WHERE f1==8 ORDER BY f1,f2} |
| 385 | } {8 88 8 256 8 888} |
drh | 1d37e28 | 2000-05-30 03:12:21 +0000 | [diff] [blame] | 386 | |
drh | c837e70 | 2000-06-08 16:26:24 +0000 | [diff] [blame] | 387 | # Error messages |
| 388 | # |
drh | 4f3c63e | 2001-02-19 18:24:21 +0000 | [diff] [blame] | 389 | do_test update-9.1 { |
drh | c837e70 | 2000-06-08 16:26:24 +0000 | [diff] [blame] | 390 | set v [catch {execsql { |
| 391 | UPDATE test1 SET x=11 WHERE f1=1025 |
| 392 | }} msg] |
| 393 | lappend v $msg |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 394 | } {1 {no such column: x}} |
drh | 4f3c63e | 2001-02-19 18:24:21 +0000 | [diff] [blame] | 395 | do_test update-9.2 { |
drh | c837e70 | 2000-06-08 16:26:24 +0000 | [diff] [blame] | 396 | set v [catch {execsql { |
| 397 | UPDATE test1 SET f1=x(11) WHERE f1=1025 |
| 398 | }} msg] |
| 399 | lappend v $msg |
| 400 | } {1 {no such function: x}} |
drh | 4f3c63e | 2001-02-19 18:24:21 +0000 | [diff] [blame] | 401 | do_test update-9.3 { |
drh | c837e70 | 2000-06-08 16:26:24 +0000 | [diff] [blame] | 402 | set v [catch {execsql { |
| 403 | UPDATE test1 SET f1=11 WHERE x=1025 |
| 404 | }} msg] |
| 405 | lappend v $msg |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 406 | } {1 {no such column: x}} |
drh | 4f3c63e | 2001-02-19 18:24:21 +0000 | [diff] [blame] | 407 | do_test update-9.4 { |
drh | c837e70 | 2000-06-08 16:26:24 +0000 | [diff] [blame] | 408 | set v [catch {execsql { |
| 409 | UPDATE test1 SET f1=11 WHERE x(f1)=1025 |
| 410 | }} msg] |
| 411 | lappend v $msg |
| 412 | } {1 {no such function: x}} |
drh | 1d37e28 | 2000-05-30 03:12:21 +0000 | [diff] [blame] | 413 | |
drh | 79b0c95 | 2002-05-21 12:56:43 +0000 | [diff] [blame] | 414 | # Try doing updates on a unique column where the value does not |
| 415 | # really change. |
| 416 | # |
| 417 | do_test update-10.1 { |
| 418 | execsql { |
| 419 | DROP TABLE test1; |
| 420 | CREATE TABLE t1( |
| 421 | a integer primary key, |
| 422 | b UNIQUE, |
| 423 | c, d, |
| 424 | e, f, |
| 425 | UNIQUE(c,d) |
| 426 | ); |
| 427 | INSERT INTO t1 VALUES(1,2,3,4,5,6); |
| 428 | INSERT INTO t1 VALUES(2,3,4,4,6,7); |
| 429 | SELECT * FROM t1 |
| 430 | } |
| 431 | } {1 2 3 4 5 6 2 3 4 4 6 7} |
| 432 | do_test update-10.2 { |
| 433 | catchsql { |
| 434 | UPDATE t1 SET a=1, e=9 WHERE f=6; |
| 435 | SELECT * FROM t1; |
| 436 | } |
| 437 | } {0 {1 2 3 4 9 6 2 3 4 4 6 7}} |
| 438 | do_test update-10.3 { |
| 439 | catchsql { |
| 440 | UPDATE t1 SET a=1, e=10 WHERE f=7; |
| 441 | SELECT * FROM t1; |
| 442 | } |
drh | 483750b | 2003-01-29 18:46:51 +0000 | [diff] [blame] | 443 | } {1 {PRIMARY KEY must be unique}} |
drh | 79b0c95 | 2002-05-21 12:56:43 +0000 | [diff] [blame] | 444 | do_test update-10.4 { |
| 445 | catchsql { |
| 446 | SELECT * FROM t1; |
| 447 | } |
| 448 | } {0 {1 2 3 4 9 6 2 3 4 4 6 7}} |
| 449 | do_test update-10.5 { |
| 450 | catchsql { |
| 451 | UPDATE t1 SET b=2, e=11 WHERE f=6; |
| 452 | SELECT * FROM t1; |
| 453 | } |
| 454 | } {0 {1 2 3 4 11 6 2 3 4 4 6 7}} |
| 455 | do_test update-10.6 { |
| 456 | catchsql { |
| 457 | UPDATE t1 SET b=2, e=12 WHERE f=7; |
| 458 | SELECT * FROM t1; |
| 459 | } |
drh | 37ed48e | 2003-08-05 13:13:38 +0000 | [diff] [blame^] | 460 | } {1 {column b is not unique}} |
drh | 79b0c95 | 2002-05-21 12:56:43 +0000 | [diff] [blame] | 461 | do_test update-10.7 { |
| 462 | catchsql { |
| 463 | SELECT * FROM t1; |
| 464 | } |
| 465 | } {0 {1 2 3 4 11 6 2 3 4 4 6 7}} |
| 466 | do_test update-10.8 { |
| 467 | catchsql { |
| 468 | UPDATE t1 SET c=3, d=4, e=13 WHERE f=6; |
| 469 | SELECT * FROM t1; |
| 470 | } |
| 471 | } {0 {1 2 3 4 13 6 2 3 4 4 6 7}} |
| 472 | do_test update-10.9 { |
| 473 | catchsql { |
| 474 | UPDATE t1 SET c=3, d=4, e=14 WHERE f=7; |
| 475 | SELECT * FROM t1; |
| 476 | } |
drh | 37ed48e | 2003-08-05 13:13:38 +0000 | [diff] [blame^] | 477 | } {1 {columns c, d are not unique}} |
drh | 79b0c95 | 2002-05-21 12:56:43 +0000 | [diff] [blame] | 478 | do_test update-10.10 { |
| 479 | catchsql { |
| 480 | SELECT * FROM t1; |
| 481 | } |
| 482 | } {0 {1 2 3 4 13 6 2 3 4 4 6 7}} |
drh | 1d37e28 | 2000-05-30 03:12:21 +0000 | [diff] [blame] | 483 | |
drh | 53e3fc7 | 2002-07-16 17:22:50 +0000 | [diff] [blame] | 484 | # Make sure we can handle a subquery in the where clause. |
| 485 | # |
| 486 | do_test update-11.1 { |
| 487 | execsql { |
| 488 | UPDATE t1 SET e=e+1 WHERE b IN (SELECT b FROM t1); |
| 489 | SELECT b,e FROM t1; |
| 490 | } |
| 491 | } {2 14 3 7} |
| 492 | do_test update-11.2 { |
| 493 | execsql { |
| 494 | UPDATE t1 SET e=e+1 WHERE a IN (SELECT a FROM t1); |
| 495 | SELECT a,e FROM t1; |
| 496 | } |
| 497 | } {1 15 2 8} |
| 498 | |
drh | ed717fe | 2003-06-15 23:42:24 +0000 | [diff] [blame] | 499 | integrity_check update-12.1 |
| 500 | |
drh | 1d37e28 | 2000-05-30 03:12:21 +0000 | [diff] [blame] | 501 | finish_test |