3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
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.
10 #***********************************************************************
12 # The majority of this file implements tests to verify that the "testable
13 # statements" in the lang_insert.html document are correct.
15 # Also, it contains tests to verify the statements in (the very short)
18 set testdir [file dirname $argv0]
19 source $testdir/tester.tcl
26 # Organization of tests:
28 # e_insert-0.*: Test the syntax diagram.
30 # e_insert-1.*: Test statements of the form "INSERT ... VALUES(...)".
32 # e_insert-2.*: Test statements of the form "INSERT ... SELECT ...".
34 # e_insert-3.*: Test statements of the form "INSERT ... DEFAULT VALUES".
36 # e_insert-4.*: Test statements regarding the conflict clause.
38 # e_insert-5.*: Test that the qualified table name and "DEFAULT VALUES"
39 # syntaxes do not work in trigger bodies.
42 do_execsql_test e_insert-0.0 {
43 CREATE TABLE a1(a, b);
44 CREATE TABLE a2(a, b, c DEFAULT 'xyz');
45 CREATE TABLE a3(x DEFAULT 1.0, y DEFAULT 'string', z);
46 CREATE TABLE a4(c UNIQUE, d);
49 proc do_insert_tests {args} {
50 uplevel do_select_tests $args
53 # -- syntax diagram insert-stmt
55 do_insert_tests e_insert-0 {
56 1 "INSERT INTO a1 DEFAULT VALUES" {}
57 2 "INSERT INTO main.a1 DEFAULT VALUES" {}
58 3 "INSERT OR ROLLBACK INTO main.a1 DEFAULT VALUES" {}
59 4 "INSERT OR ROLLBACK INTO a1 DEFAULT VALUES" {}
60 5 "INSERT OR ABORT INTO main.a1 DEFAULT VALUES" {}
61 6 "INSERT OR ABORT INTO a1 DEFAULT VALUES" {}
62 7 "INSERT OR REPLACE INTO main.a1 DEFAULT VALUES" {}
63 8 "INSERT OR REPLACE INTO a1 DEFAULT VALUES" {}
64 9 "INSERT OR FAIL INTO main.a1 DEFAULT VALUES" {}
65 10 "INSERT OR FAIL INTO a1 DEFAULT VALUES" {}
66 11 "INSERT OR FAIL INTO main.a1 DEFAULT VALUES" {}
67 12 "INSERT OR IGNORE INTO a1 DEFAULT VALUES" {}
68 13 "REPLACE INTO a1 DEFAULT VALUES" {}
69 14 "REPLACE INTO main.a1 DEFAULT VALUES" {}
70 15 "INSERT INTO a1 VALUES(1, 2)" {}
71 16 "INSERT INTO main.a1 VALUES(1, 2)" {}
72 17 "INSERT OR ROLLBACK INTO main.a1 VALUES(1, 2)" {}
73 18 "INSERT OR ROLLBACK INTO a1 VALUES(1, 2)" {}
74 19 "INSERT OR ABORT INTO main.a1 VALUES(1, 2)" {}
75 20 "INSERT OR ABORT INTO a1 VALUES(1, 2)" {}
76 21 "INSERT OR REPLACE INTO main.a1 VALUES(1, 2)" {}
77 22 "INSERT OR REPLACE INTO a1 VALUES(1, 2)" {}
78 23 "INSERT OR FAIL INTO main.a1 VALUES(1, 2)" {}
79 24 "INSERT OR FAIL INTO a1 VALUES(1, 2)" {}
80 25 "INSERT OR FAIL INTO main.a1 VALUES(1, 2)" {}
81 26 "INSERT OR IGNORE INTO a1 VALUES(1, 2)" {}
82 27 "REPLACE INTO a1 VALUES(1, 2)" {}
83 28 "REPLACE INTO main.a1 VALUES(1, 2)" {}
84 29 "INSERT INTO a1 (b, a) VALUES(1, 2)" {}
85 30 "INSERT INTO main.a1 (b, a) VALUES(1, 2)" {}
86 31 "INSERT OR ROLLBACK INTO main.a1 (b, a) VALUES(1, 2)" {}
87 32 "INSERT OR ROLLBACK INTO a1 (b, a) VALUES(1, 2)" {}
88 33 "INSERT OR ABORT INTO main.a1 (b, a) VALUES(1, 2)" {}
89 34 "INSERT OR ABORT INTO a1 (b, a) VALUES(1, 2)" {}
90 35 "INSERT OR REPLACE INTO main.a1 (b, a) VALUES(1, 2)" {}
91 36 "INSERT OR REPLACE INTO a1 (b, a) VALUES(1, 2)" {}
92 37 "INSERT OR FAIL INTO main.a1 (b, a) VALUES(1, 2)" {}
93 38 "INSERT OR FAIL INTO a1 (b, a) VALUES(1, 2)" {}
94 39 "INSERT OR FAIL INTO main.a1 (b, a) VALUES(1, 2)" {}
95 40 "INSERT OR IGNORE INTO a1 (b, a) VALUES(1, 2)" {}
96 41 "REPLACE INTO a1 (b, a) VALUES(1, 2)" {}
97 42 "REPLACE INTO main.a1 (b, a) VALUES(1, 2)" {}
98 43 "INSERT INTO a1 SELECT c, b FROM a2" {}
99 44 "INSERT INTO main.a1 SELECT c, b FROM a2" {}
100 45 "INSERT OR ROLLBACK INTO main.a1 SELECT c, b FROM a2" {}
101 46 "INSERT OR ROLLBACK INTO a1 SELECT c, b FROM a2" {}
102 47 "INSERT OR ABORT INTO main.a1 SELECT c, b FROM a2" {}
103 48 "INSERT OR ABORT INTO a1 SELECT c, b FROM a2" {}
104 49 "INSERT OR REPLACE INTO main.a1 SELECT c, b FROM a2" {}
105 50 "INSERT OR REPLACE INTO a1 SELECT c, b FROM a2" {}
106 51 "INSERT OR FAIL INTO main.a1 SELECT c, b FROM a2" {}
107 52 "INSERT OR FAIL INTO a1 SELECT c, b FROM a2" {}
108 53 "INSERT OR FAIL INTO main.a1 SELECT c, b FROM a2" {}
109 54 "INSERT OR IGNORE INTO a1 SELECT c, b FROM a2" {}
110 55 "REPLACE INTO a1 SELECT c, b FROM a2" {}
111 56 "REPLACE INTO main.a1 SELECT c, b FROM a2" {}
112 57 "INSERT INTO a1 (b, a) SELECT c, b FROM a2" {}
113 58 "INSERT INTO main.a1 (b, a) SELECT c, b FROM a2" {}
114 59 "INSERT OR ROLLBACK INTO main.a1 (b, a) SELECT c, b FROM a2" {}
115 60 "INSERT OR ROLLBACK INTO a1 (b, a) SELECT c, b FROM a2" {}
116 61 "INSERT OR ABORT INTO main.a1 (b, a) SELECT c, b FROM a2" {}
117 62 "INSERT OR ABORT INTO a1 (b, a) SELECT c, b FROM a2" {}
118 63 "INSERT OR REPLACE INTO main.a1 (b, a) SELECT c, b FROM a2" {}
119 64 "INSERT OR REPLACE INTO a1 (b, a) SELECT c, b FROM a2" {}
120 65 "INSERT OR FAIL INTO main.a1 (b, a) SELECT c, b FROM a2" {}
121 66 "INSERT OR FAIL INTO a1 (b, a) SELECT c, b FROM a2" {}
122 67 "INSERT OR FAIL INTO main.a1 (b, a) SELECT c, b FROM a2" {}
123 68 "INSERT OR IGNORE INTO a1 (b, a) SELECT c, b FROM a2" {}
124 69 "REPLACE INTO a1 (b, a) SELECT c, b FROM a2" {}
125 70 "REPLACE INTO main.a1 (b, a) SELECT c, b FROM a2" {}
126 71 "INSERT INTO a1 (b, a) VALUES(1, 2),(3,4)" {}
127 72 "INSERT INTO main.a1 (b, a) VALUES(1, 2),(3,4)" {}
128 73 "INSERT OR ROLLBACK INTO main.a1 (b, a) VALUES(1, 2),(3,4)" {}
129 74 "INSERT OR ROLLBACK INTO a1 (b, a) VALUES(1, 2),(3,4)" {}
130 75 "INSERT OR ABORT INTO main.a1 (b, a) VALUES(1, 2),(3,4)" {}
131 76 "INSERT OR ABORT INTO a1 (b, a) VALUES(1, 2),(3,4)" {}
132 77 "INSERT OR REPLACE INTO main.a1 (b, a) VALUES(1, 2),(3,4)" {}
133 78 "INSERT OR REPLACE INTO a1 (b, a) VALUES(1, 2),(3,4)" {}
134 79 "INSERT OR FAIL INTO main.a1 (b, a) VALUES(1, 2),(3,4)" {}
135 80 "INSERT OR FAIL INTO a1 (b, a) VALUES(1, 2),(3,4)" {}
136 81 "INSERT OR FAIL INTO main.a1 (b, a) VALUES(1, 2),(3,4)" {}
137 82 "INSERT OR IGNORE INTO a1 (b, a) VALUES(1, 2),(3,4)" {}
138 83 "REPLACE INTO a1 (b, a) VALUES(1, 2),(3,4)" {}
139 84 "REPLACE INTO main.a1 (b, a) VALUES(1, 2),(3,4)" {}
144 # EVIDENCE-OF: R-21490-41092 The first form (with the "VALUES" keyword)
145 # creates one or more new rows in an existing table.
147 do_insert_tests e_insert-1.1 {
148 0 "SELECT count(*) FROM a2" {0}
150 1a "INSERT INTO a2 VALUES(1, 2, 3)" {}
151 1b "SELECT count(*) FROM a2" {1}
153 2a "INSERT INTO a2(a, b) VALUES(1, 2)" {}
154 2b "SELECT count(*) FROM a2" {2}
156 3a "INSERT INTO a2(a) VALUES(3),(4)" {}
157 3b "SELECT count(*) FROM a2" {4}
160 # EVIDENCE-OF: R-19218-01018 If the column-name list after table-name is
161 # omitted then the number of values inserted into each row must be the
162 # same as the number of columns in the table.
164 # A test in the block above verifies that if the VALUES list has the
165 # correct number of columns (for table a2, 3 columns) works. So these
166 # tests just show that other values cause an error.
168 do_insert_tests e_insert-1.2 -error {
169 table %s has %d columns but %d values were supplied
171 1 "INSERT INTO a2 VALUES(1)" {a2 3 1}
172 2 "INSERT INTO a2 VALUES(1,2)" {a2 3 2}
173 3 "INSERT INTO a2 VALUES(1,2,3,4)" {a2 3 4}
174 4 "INSERT INTO a2 VALUES(1,2,3,4,5)" {a2 3 5}
177 # EVIDENCE-OF: R-29730-42609 In this case the result of evaluating the
178 # left-most expression from each term of the VALUES list is inserted
179 # into the left-most column of each new row, and so forth for each
180 # subsequent expression.
183 do_insert_tests e_insert-1.3 {
184 1a "INSERT INTO a2 VALUES(1, 2, 3)" {}
185 1b "SELECT * FROM a2 WHERE oid=last_insert_rowid()" {1 2 3}
187 2a "INSERT INTO a2 VALUES('abc', NULL, 3*3+1)" {}
188 2b "SELECT * FROM a2 WHERE oid=last_insert_rowid()" {abc {} 10}
190 3a "INSERT INTO a2 VALUES((SELECT count(*) FROM a2), 'x', 'y')" {}
191 3b "SELECT * FROM a2 WHERE oid=last_insert_rowid()" {2 x y}
194 # EVIDENCE-OF: R-21115-58321 If a column-name list is specified, then
195 # the number of values in each term of the VALUE list must match the
196 # number of specified columns.
198 do_insert_tests e_insert-1.4 -error {
199 %d values for %d columns
201 1 "INSERT INTO a2(a, b, c) VALUES(1)" {1 3}
202 2 "INSERT INTO a2(a, b, c) VALUES(1,2)" {2 3}
203 3 "INSERT INTO a2(a, b, c) VALUES(1,2,3,4)" {4 3}
204 4 "INSERT INTO a2(a, b, c) VALUES(1,2,3,4,5)" {5 3}
206 5 "INSERT INTO a2(c, a) VALUES(1)" {1 2}
207 6 "INSERT INTO a2(c, a) VALUES(1,2,3)" {3 2}
208 7 "INSERT INTO a2(c, a) VALUES(1,2,3,4)" {4 2}
209 8 "INSERT INTO a2(c, a) VALUES(1,2,3,4,5)" {5 2}
212 # EVIDENCE-OF: R-07016-26442 Each of the named columns of the new row is
213 # populated with the results of evaluating the corresponding VALUES
216 # EVIDENCE-OF: R-12183-43719 Table columns that do not appear in the
217 # column list are populated with the default column value (specified as
218 # part of the CREATE TABLE statement), or with NULL if no default value
222 do_insert_tests e_insert-1.5 {
223 1a "INSERT INTO a2(b, c) VALUES('b', 'c')" {}
224 1b "SELECT * FROM a2" {{} b c}
226 2a "INSERT INTO a2(a, b) VALUES('a', 'b')" {}
227 2b "SELECT * FROM a2" {{} b c a b xyz}
230 # EVIDENCE-OF: R-52173-30215 A new entry is inserted into the table for
231 # each row of data returned by executing the SELECT statement.
234 do_insert_tests e_insert-2.1 {
235 0 "SELECT count(*) FROM a1" {0}
237 1a "SELECT count(*) FROM (SELECT 1, 2)" {1}
238 1b "INSERT INTO a1 SELECT 1, 2" {}
239 1c "SELECT count(*) FROM a1" {1}
241 2a "SELECT count(*) FROM (SELECT b, a FROM a1)" {1}
242 2b "INSERT INTO a1 SELECT b, a FROM a1" {}
243 2c "SELECT count(*) FROM a1" {2}
245 3a "SELECT count(*) FROM (SELECT b, a FROM a1)" {2}
246 3b "INSERT INTO a1 SELECT b, a FROM a1" {}
247 3c "SELECT count(*) FROM a1" {4}
249 4a "SELECT count(*) FROM (SELECT b, a FROM a1)" {4}
250 4b "INSERT INTO a1 SELECT b, a FROM a1" {}
251 4c "SELECT count(*) FROM a1" {8}
253 4a "SELECT count(*) FROM (SELECT min(b), min(a) FROM a1)" {1}
254 4b "INSERT INTO a1 SELECT min(b), min(a) FROM a1" {}
255 4c "SELECT count(*) FROM a1" {9}
259 # EVIDENCE-OF: R-63614-47421 If a column-list is specified, the number
260 # of columns in the result of the SELECT must be the same as the number
261 # of items in the column-list.
263 do_insert_tests e_insert-2.2 -error {
264 %d values for %d columns
266 1 "INSERT INTO a3(x, y) SELECT a, b, c FROM a2" {3 2}
267 2 "INSERT INTO a3(x, y) SELECT * FROM a2" {3 2}
268 3 "INSERT INTO a3(x, y) SELECT * FROM a2 CROSS JOIN a1" {5 2}
269 4 "INSERT INTO a3(x, y) SELECT * FROM a2 NATURAL JOIN a1" {3 2}
270 5 "INSERT INTO a3(x, y) SELECT a2.a FROM a2,a1" {1 2}
272 6 "INSERT INTO a3(z) SELECT a, b, c FROM a2" {3 1}
273 7 "INSERT INTO a3(z) SELECT * FROM a2" {3 1}
274 8 "INSERT INTO a3(z) SELECT * FROM a2 CROSS JOIN a1" {5 1}
275 9 "INSERT INTO a3(z) SELECT * FROM a2 NATURAL JOIN a1" {3 1}
276 10 "INSERT INTO a3(z) SELECT a1.* FROM a2,a1" {2 1}
279 # EVIDENCE-OF: R-58951-07798 Otherwise, if no column-list is specified,
280 # the number of columns in the result of the SELECT must be the same as
281 # the number of columns in the table.
283 do_insert_tests e_insert-2.3 -error {
284 table %s has %d columns but %d values were supplied
286 1 "INSERT INTO a1 SELECT a, b, c FROM a2" {a1 2 3}
287 2 "INSERT INTO a1 SELECT * FROM a2" {a1 2 3}
288 3 "INSERT INTO a1 SELECT * FROM a2 CROSS JOIN a1" {a1 2 5}
289 4 "INSERT INTO a1 SELECT * FROM a2 NATURAL JOIN a1" {a1 2 3}
290 5 "INSERT INTO a1 SELECT a2.a FROM a2,a1" {a1 2 1}
293 # EVIDENCE-OF: R-31074-37730 Any SELECT statement, including compound
294 # SELECTs and SELECT statements with ORDER BY and/or LIMIT clauses, may
295 # be used in an INSERT statement of this form.
298 do_execsql_test e_insert-2.3.0 {
299 INSERT INTO a1 VALUES('x', 'y');
301 do_insert_tests e_insert-2.3 {
302 1 "INSERT INTO a1 SELECT a,b FROM a1 UNION SELECT b,a FROM a1 ORDER BY 1" {}
303 2 "INSERT INTO a1(b, a) SELECT * FROM a1 LIMIT 1" {}
304 3 "INSERT INTO a1 SELECT 'a'||a, 'b'||b FROM a1 LIMIT 2 OFFSET 1" {}
305 4 "INSERT INTO a1 SELECT * FROM a1 ORDER BY b, a" {}
306 S "SELECT * FROM a1" {
311 ay bx ax by y x y x x y x y
315 # EVIDENCE-OF: R-25149-22012 The INSERT ... DEFAULT VALUES statement
316 # inserts a single new row into the named table.
319 do_insert_tests e_insert-3.1 {
320 1 "SELECT count(*) FROM a3" {0}
321 2a "INSERT INTO a3 DEFAULT VALUES" {}
322 2b "SELECT count(*) FROM a3" {1}
325 # EVIDENCE-OF: R-18927-01951 Each column of the new row is populated
326 # with its default value, or with a NULL if no default value is
327 # specified as part of the column definition in the CREATE TABLE
331 do_insert_tests e_insert-3.2 {
332 1.1 "INSERT INTO a3 DEFAULT VALUES" {}
333 1.2 "SELECT * FROM a3" {1.0 string {}}
335 2.1 "INSERT INTO a3 DEFAULT VALUES" {}
336 2.2 "SELECT * FROM a3" {1.0 string {} 1.0 string {}}
338 3.1 "INSERT INTO a2 DEFAULT VALUES" {}
339 3.2 "SELECT * FROM a2" {{} {} xyz}
341 4.1 "INSERT INTO a2 DEFAULT VALUES" {}
342 4.2 "SELECT * FROM a2" {{} {} xyz {} {} xyz}
344 5.1 "INSERT INTO a1 DEFAULT VALUES" {}
345 5.2 "SELECT * FROM a1" {{} {}}
347 6.1 "INSERT INTO a1 DEFAULT VALUES" {}
348 6.2 "SELECT * FROM a1" {{} {} {} {}}
351 # EVIDENCE-OF: R-00267-47727 The initial "INSERT" keyword can be
352 # replaced by "REPLACE" or "INSERT OR action" to specify an alternative
353 # constraint conflict resolution algorithm to use during that one INSERT
356 # EVIDENCE-OF: R-23110-47146 the parser allows the use of the single
357 # keyword REPLACE as an alias for "INSERT OR REPLACE".
359 # The two requirements above are tested by e_select-4.1.* and
360 # e_select-4.2.*, respectively.
362 # EVIDENCE-OF: R-03421-22330 The REPLACE command is an alias for the
363 # "INSERT OR REPLACE" variant of the INSERT command.
365 # This is a dup of R-23110-47146. Therefore it is also verified
366 # by e_select-4.2.*. This requirement is the only one from
369 do_execsql_test e_insert-4.1.0 {
370 INSERT INTO a4 VALUES(1, 'a');
371 INSERT INTO a4 VALUES(2, 'a');
372 INSERT INTO a4 VALUES(3, 'a');
374 foreach {tn sql error ac data } {
375 1.1 "INSERT INTO a4 VALUES(2,'b')" {UNIQUE constraint failed: a4.c} 1 {1 a 2 a 3 a}
376 1.2 "INSERT OR REPLACE INTO a4 VALUES(2, 'b')" {} 1 {1 a 3 a 2 b}
377 1.3 "INSERT OR IGNORE INTO a4 VALUES(3, 'c')" {} 1 {1 a 3 a 2 b}
378 1.4 "BEGIN" {} 0 {1 a 3 a 2 b}
379 1.5 "INSERT INTO a4 VALUES(1, 'd')" {UNIQUE constraint failed: a4.c} 0 {1 a 3 a 2 b}
380 1.6 "INSERT OR ABORT INTO a4 VALUES(1, 'd')"
381 {UNIQUE constraint failed: a4.c} 0 {1 a 3 a 2 b}
382 1.7 "INSERT OR ROLLBACK INTO a4 VALUES(1, 'd')"
383 {UNIQUE constraint failed: a4.c} 1 {1 a 3 a 2 b}
384 1.8 "INSERT INTO a4 SELECT 4, 'e' UNION ALL SELECT 3, 'e'"
385 {UNIQUE constraint failed: a4.c} 1 {1 a 3 a 2 b}
386 1.9 "INSERT OR FAIL INTO a4 SELECT 4, 'e' UNION ALL SELECT 3, 'e'"
387 {UNIQUE constraint failed: a4.c} 1 {1 a 3 a 2 b 4 e}
389 2.1 "INSERT INTO a4 VALUES(2,'f')"
390 {UNIQUE constraint failed: a4.c} 1 {1 a 3 a 2 b 4 e}
391 2.2 "REPLACE INTO a4 VALUES(2, 'f')" {} 1 {1 a 3 a 4 e 2 f}
393 do_catchsql_test e_insert-4.1.$tn.1 $sql [list [expr {$error!=""}] $error]
394 do_execsql_test e_insert-4.1.$tn.2 {SELECT * FROM a4} [list {*}$data]
395 do_test e_insert-4.1.$tn.3 {sqlite3_get_autocommit db} $ac
398 # EVIDENCE-OF: R-59829-49719 The optional "schema-name." prefix on the
399 # table-name is supported for top-level INSERT statements only.
401 # EVIDENCE-OF: R-05731-00924 The table name must be unqualified for
402 # INSERT statements that occur within CREATE TRIGGER statements.
404 set err {1 {qualified table names are not allowed on INSERT, UPDATE, and DELETE statements within triggers}}
406 do_catchsql_test e_insert-5.1.1 {
407 CREATE TRIGGER AFTER UPDATE ON a1 BEGIN
408 INSERT INTO main.a4 VALUES(new.a, new.b);
411 do_catchsql_test e_insert-5.1.2 {
412 CREATE TEMP TABLE IF NOT EXISTS tmptable(a, b);
413 CREATE TRIGGER AFTER DELETE ON a3 BEGIN
414 INSERT INTO temp.tmptable VALUES(1, 2);
418 # EVIDENCE-OF: R-15888-36326 Similarly, the "DEFAULT VALUES" form of the
419 # INSERT statement is supported for top-level INSERT statements only and
420 # not for INSERT statements within triggers.
422 do_catchsql_test e_insert-5.2.1 {
423 CREATE TRIGGER AFTER UPDATE ON a1 BEGIN
424 INSERT INTO a4 DEFAULT VALUES;
426 } {1 {near "DEFAULT": syntax error}}