xref: /sqlite-3.40.0/test/e_update.test (revision af1dcab2)
1# 2010 September 20
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#
12# This file implements tests to verify that the "testable statements" in
13# the lang_update.html document are correct.
14#
15set testdir [file dirname $argv0]
16source $testdir/tester.tcl
17
18forcedelete test.db2
19
20do_execsql_test e_update-0.0 {
21  CREATE TABLE t1(a, b);
22
23  ATTACH 'test.db2' AS aux;
24  CREATE TABLE aux.t1(a, b);
25
26  CREATE TABLE t2(a, b, c);
27
28  CREATE TABLE t3(a, b UNIQUE);
29} {}
30
31proc do_update_tests {args} {
32  uplevel do_select_tests $args
33}
34
35# EVIDENCE-OF: R-05685-44205 -- syntax diagram update-stmt
36#
37do_update_tests e_update-0 {
38  1    "UPDATE t1 SET a=10" {}
39  2    "UPDATE t1 SET a=10, b=5" {}
40  3    "UPDATE t1 SET a=10 WHERE b=5" {}
41  4    "UPDATE t1 SET b=5,a=10 WHERE 1" {}
42  5    "UPDATE main.t1 SET a=10" {}
43  6    "UPDATE main.t1 SET a=10, b=5" {}
44  7    "UPDATE main.t1 SET a=10 WHERE b=5" {}
45  9    "UPDATE OR ROLLBACK t1 SET a=10" {}
46  10   "UPDATE OR ROLLBACK t1 SET a=10, b=5" {}
47  11   "UPDATE OR ROLLBACK t1 SET a=10 WHERE b=5" {}
48  12   "UPDATE OR ROLLBACK t1 SET b=5,a=10 WHERE 1" {}
49  13   "UPDATE OR ROLLBACK main.t1 SET a=10" {}
50  14   "UPDATE OR ROLLBACK main.t1 SET a=10, b=5" {}
51  15   "UPDATE OR ROLLBACK main.t1 SET a=10 WHERE b=5" {}
52  16   "UPDATE OR ROLLBACK main.t1 SET b=5,a=10 WHERE 1" {}
53  17   "UPDATE OR ABORT t1 SET a=10" {}
54  18   "UPDATE OR ABORT t1 SET a=10, b=5" {}
55  19   "UPDATE OR ABORT t1 SET a=10 WHERE b=5" {}
56  20   "UPDATE OR ABORT t1 SET b=5,a=10 WHERE 1" {}
57  21   "UPDATE OR ABORT main.t1 SET a=10" {}
58  22   "UPDATE OR ABORT main.t1 SET a=10, b=5" {}
59  23   "UPDATE OR ABORT main.t1 SET a=10 WHERE b=5" {}
60  24   "UPDATE OR ABORT main.t1 SET b=5,a=10 WHERE 1" {}
61  25   "UPDATE OR REPLACE t1 SET a=10" {}
62  26   "UPDATE OR REPLACE t1 SET a=10, b=5" {}
63  27   "UPDATE OR REPLACE t1 SET a=10 WHERE b=5" {}
64  28   "UPDATE OR REPLACE t1 SET b=5,a=10 WHERE 1" {}
65  29   "UPDATE OR REPLACE main.t1 SET a=10" {}
66  30   "UPDATE OR REPLACE main.t1 SET a=10, b=5" {}
67  31   "UPDATE OR REPLACE main.t1 SET a=10 WHERE b=5" {}
68  32   "UPDATE OR REPLACE main.t1 SET b=5,a=10 WHERE 1" {}
69  33   "UPDATE OR FAIL t1 SET a=10" {}
70  34   "UPDATE OR FAIL t1 SET a=10, b=5" {}
71  35   "UPDATE OR FAIL t1 SET a=10 WHERE b=5" {}
72  36   "UPDATE OR FAIL t1 SET b=5,a=10 WHERE 1" {}
73  37   "UPDATE OR FAIL main.t1 SET a=10" {}
74  38   "UPDATE OR FAIL main.t1 SET a=10, b=5" {}
75  39   "UPDATE OR FAIL main.t1 SET a=10 WHERE b=5" {}
76  40   "UPDATE OR FAIL main.t1 SET b=5,a=10 WHERE 1" {}
77  41   "UPDATE OR IGNORE t1 SET a=10" {}
78  42   "UPDATE OR IGNORE t1 SET a=10, b=5" {}
79  43   "UPDATE OR IGNORE t1 SET a=10 WHERE b=5" {}
80  44   "UPDATE OR IGNORE t1 SET b=5,a=10 WHERE 1" {}
81  45   "UPDATE OR IGNORE main.t1 SET a=10" {}
82  46   "UPDATE OR IGNORE main.t1 SET a=10, b=5" {}
83  47   "UPDATE OR IGNORE main.t1 SET a=10 WHERE b=5" {}
84  48   "UPDATE OR IGNORE main.t1 SET b=5,a=10 WHERE 1" {}
85}
86
87# EVIDENCE-OF: R-38515-45264 An UPDATE statement is used to modify a
88# subset of the values stored in zero or more rows of the database table
89# identified by the qualified-table-name specified as part of the UPDATE
90# statement.
91#
92#     Test cases e_update-1.1.1.* test the "identified by the
93#     qualified-table-name" part of the statement above. Tests
94#     e_update-1.1.2.* show that the "zero or more rows" part is
95#     accurate.
96#
97do_execsql_test e_update-1.1.0 {
98  INSERT INTO main.t1 VALUES(1, 'i');
99  INSERT INTO main.t1 VALUES(2, 'ii');
100  INSERT INTO main.t1 VALUES(3, 'iii');
101
102  INSERT INTO aux.t1 VALUES(1, 'I');
103  INSERT INTO aux.t1 VALUES(2, 'II');
104  INSERT INTO aux.t1 VALUES(3, 'III');
105} {}
106do_update_tests e_update-1.1 {
107  1.1  "UPDATE t1 SET a = a+1; SELECT * FROM t1"             {2 i  3 ii  4 iii}
108  1.2  "UPDATE main.t1 SET a = a+1; SELECT * FROM main.t1"   {3 i  4 ii  5 iii}
109  1.3  "UPDATE aux.t1 SET a = a+1; SELECT * FROM aux.t1"     {2 I  3 II  4 III}
110
111  2.1  "UPDATE t1 SET a = a+1 WHERE a = 1; SELECT * FROM t1" {3 i  4 ii  5 iii}
112  2.2  "UPDATE t1 SET a = a+1 WHERE a = 4; SELECT * FROM t1" {3 i  5 ii  5 iii}
113}
114
115# EVIDENCE-OF: R-55869-30521 If the UPDATE statement does not have a
116# WHERE clause, all rows in the table are modified by the UPDATE.
117#
118do_execsql_test e_update-1.2.0 {
119  DELETE FROM main.t1;
120  INSERT INTO main.t1 VALUES(1, 'i');
121  INSERT INTO main.t1 VALUES(2, 'ii');
122  INSERT INTO main.t1 VALUES(3, 'iii');
123} {}
124do_update_tests e_update-1.2 {
125  1  "UPDATE t1 SET b = 'roman' ; SELECT * FROM t1"
126     {1 roman  2 roman  3 roman}
127
128  2  "UPDATE t1 SET a = 'greek' ; SELECT * FROM t1"
129     {greek roman  greek roman  greek roman}
130}
131
132# EVIDENCE-OF: R-41754-00978 Otherwise, the UPDATE affects only those
133# rows for which evaluating the WHERE clause expression and casting the
134# result to a NUMERIC value produces a value other than NULL or zero
135# (integer value 0 or real value 0.0).
136#
137do_execsql_test e_update-1.3.0 {
138  DELETE FROM main.t1;
139  INSERT INTO main.t1 VALUES(NULL, '');
140  INSERT INTO main.t1 VALUES(1, 'i');
141  INSERT INTO main.t1 VALUES(2, 'ii');
142  INSERT INTO main.t1 VALUES(3, 'iii');
143} {}
144do_update_tests e_update-1.3 {
145  1  "UPDATE t1 SET b = 'roman' WHERE a<2 ; SELECT * FROM t1"
146     {{} {}  1 roman  2 ii  3 iii}
147
148  2  "UPDATE t1 SET b = 'egyptian' WHERE (a-3)/10.0 ; SELECT * FROM t1"
149     {{} {}  1 egyptian  2 egyptian  3 iii}
150
151  3  "UPDATE t1 SET b = 'macedonian' WHERE a; SELECT * FROM t1"
152     {{} {}  1 macedonian  2 macedonian  3 macedonian}
153
154  4  "UPDATE t1 SET b = 'lithuanian' WHERE a IS NULL; SELECT * FROM t1"
155     {{} lithuanian  1 macedonian  2 macedonian  3 macedonian}
156}
157
158# EVIDENCE-OF: R-61178-36001 It is not an error if the WHERE clause does
159# not evaluate to a non-NULL, non-zero value for any row in the table -
160# this just means that the UPDATE statement affects zero rows.
161#
162do_execsql_test e_update-1.4.0 {
163  DELETE FROM main.t1;
164  INSERT INTO main.t1 VALUES(NULL, '');
165  INSERT INTO main.t1 VALUES(1, 'i');
166  INSERT INTO main.t1 VALUES(2, 'ii');
167  INSERT INTO main.t1 VALUES(3, 'iii');
168} {}
169do_update_tests e_update-1.4 -query {
170  SELECT * FROM t1
171} {
172  1  "UPDATE t1 SET b = 'burmese' WHERE a=5" {{} {}  1 i  2 ii  3 iii}
173
174  2  "UPDATE t1 SET b = 'burmese' WHERE length(b)<1 AND a IS NOT NULL"
175     {{} {}  1 i  2 ii  3 iii}
176
177  3  "UPDATE t1 SET b = 'burmese' WHERE 0" {{} {}  1 i  2 ii  3 iii}
178
179  4  "UPDATE t1 SET b = 'burmese' WHERE (SELECT a FROM t1 WHERE rowid=1)"
180     {{} {}  1 i  2 ii  3 iii}
181}
182
183# EVIDENCE-OF: R-40598-36595 For each affected row, the named columns
184# are set to the values found by evaluating the corresponding scalar
185# expressions.
186#
187# EVIDENCE-OF: R-40472-60438 Columns that do not appear in the list of
188# assignments are left unmodified.
189#
190do_execsql_test e_update-2.1.0 {
191  INSERT INTO t2(rowid, a, b, c) VALUES(1,  3, 1, 4);
192  INSERT INTO t2(rowid, a, b, c) VALUES(2,  1, 5, 9);
193  INSERT INTO t2(rowid, a, b, c) VALUES(3,  2, 6, 5);
194} {}
195do_update_tests e_update-2.1 -query {
196  SELECT * FROM t2
197} {
198  1   "UPDATE t2 SET c = 1+1 WHERE a=2"
199      {3 1 4   1 5 9   2 6 2}
200
201  2   "UPDATE t2 SET b = 4/2, c=CAST((0.4*5) AS INTEGER) WHERE a<3"
202      {3 1 4   1 2 2   2 2 2}
203
204  3   "UPDATE t2 SET a = 1"
205      {1 1 4   1 2 2   1 2 2}
206
207  4   "UPDATE t2 SET b = (SELECT count(*)+2 FROM t2), c = 24/3+1 WHERE rowid=2"
208      {1 1 4   1 5 9   1 2 2}
209
210  5   "UPDATE t2 SET a = 3 WHERE c = 4"
211      {3 1 4   1 5 9   1 2 2}
212
213  6   "UPDATE t2 SET a = b WHERE rowid>2"
214      {3 1 4   1 5 9   2 2 2}
215
216  6   "UPDATE t2 SET b=6, c=5 WHERE a=b AND b=c"
217      {3 1 4   1 5 9   2 6 5}
218}
219
220# EVIDENCE-OF: R-09060-20018 If a single column-name appears more than
221# once in the list of assignment expressions, all but the rightmost
222# occurence is ignored.
223#
224do_update_tests e_update-2.1 -query {
225  SELECT * FROM t2
226} {
227  1   "UPDATE t2 SET c=5, c=6, c=7 WHERE rowid=1" {3 1 7   1 5 9   2 6 5}
228  2   "UPDATE t2 SET c=7, c=6, c=5 WHERE rowid=1" {3 1 5   1 5 9   2 6 5}
229  3   "UPDATE t2 SET c=5, b=6, c=7 WHERE rowid=1" {3 6 7   1 5 9   2 6 5}
230}
231
232# EVIDENCE-OF: R-36239-04077 The scalar expressions may refer to columns
233# of the row being updated.
234#
235# EVIDENCE-OF: R-04558-24451 In this case all scalar expressions are
236# evaluated before any assignments are made.
237#
238do_execsql_test e_update-2.2.0 {
239  DELETE FROM t2;
240  INSERT INTO t2(rowid, a, b, c) VALUES(1,  3, 1, 4);
241  INSERT INTO t2(rowid, a, b, c) VALUES(2,  1, 5, 9);
242  INSERT INTO t2(rowid, a, b, c) VALUES(3,  2, 6, 5);
243} {}
244do_update_tests e_update-2.2 -query {
245  SELECT * FROM t2
246} {
247  1   "UPDATE t2 SET a=b+c"          {5 1 4     14 5 9   11  6 5}
248  2   "UPDATE t2 SET a=b, b=a"       {1 5 4     5 14 9    6 11 5}
249  3   "UPDATE t2 SET a=c||c, c=NULL" {44 5 {}  99 14 {}  55 11 {}}
250}
251
252# EVIDENCE-OF: R-12619-24112 The optional conflict-clause allows the
253# user to nominate a specific constraint conflict resolution algorithm
254# to use during this one UPDATE command.
255#
256do_execsql_test e_update-2.2.0 {
257  DELETE FROM t3;
258  INSERT INTO t3 VALUES(1, 'one');
259  INSERT INTO t3 VALUES(2, 'two');
260  INSERT INTO t3 VALUES(3, 'three');
261  INSERT INTO t3 VALUES(4, 'four');
262} {}
263foreach {tn sql error ac data } {
264  1  "UPDATE t3 SET b='one' WHERE a=3"
265     {column b is not unique} 1 {1 one 2 two 3 three 4 four}
266
267  2  "UPDATE OR REPLACE t3 SET b='one' WHERE a=3"
268     {} 1 {2 two 3 one 4 four}
269
270  3  "UPDATE OR FAIL t3 SET b='three'"
271     {column b is not unique} 1 {2 three 3 one 4 four}
272
273  4  "UPDATE OR IGNORE t3 SET b='three' WHERE a=3"
274     {} 1 {2 three 3 one 4 four}
275
276  5  "UPDATE OR ABORT t3 SET b='three' WHERE a=3"
277     {column b is not unique} 1 {2 three 3 one 4 four}
278
279  6  "BEGIN" {} 0 {2 three 3 one 4 four}
280
281  7  "UPDATE t3 SET b='three' WHERE a=3"
282     {column b is not unique} 0 {2 three 3 one 4 four}
283
284  8  "UPDATE OR ABORT t3 SET b='three' WHERE a=3"
285     {column b is not unique} 0 {2 three 3 one 4 four}
286
287  9  "UPDATE OR FAIL t3 SET b='two'"
288     {column b is not unique} 0 {2 two 3 one 4 four}
289
290  10 "UPDATE OR IGNORE t3 SET b='four' WHERE a=3"
291     {} 0 {2 two 3 one 4 four}
292
293  11 "UPDATE OR REPLACE t3 SET b='four' WHERE a=3"
294     {} 0 {2 two 3 four}
295
296  12 "UPDATE OR ROLLBACK t3 SET b='four'"
297     {column b is not unique} 1 {2 three 3 one 4 four}
298} {
299  do_catchsql_test e_update-2.3.$tn.1 $sql [list [expr {$error!=""}] $error]
300  do_execsql_test  e_update-2.3.$tn.2 {SELECT * FROM t3} [list {*}$data]
301  do_test          e_update-2.3.$tn.3 {sqlite3_get_autocommit db} $ac
302}
303
304finish_test
305