xref: /sqlite-3.40.0/test/e_insert.test (revision 2f56da3f)
1# 2010 September 18
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# The majority of this file implements tests to verify that the "testable
13# statements" in the lang_insert.html document are correct.
14#
15# Also, it contains tests to verify the statements in (the very short)
16# lang_replace.html.
17#
18set testdir [file dirname $argv0]
19source $testdir/tester.tcl
20
21ifcapable !compound {
22  finish_test
23  return
24}
25
26# Organization of tests:
27#
28#   e_insert-0.*: Test the syntax diagram.
29#
30#   e_insert-1.*: Test statements of the form "INSERT ... VALUES(...)".
31#
32#   e_insert-2.*: Test statements of the form "INSERT ... SELECT ...".
33#
34#   e_insert-3.*: Test statements of the form "INSERT ... DEFAULT VALUES".
35#
36#   e_insert-4.*: Test statements regarding the conflict clause.
37#
38#   e_insert-5.*: Test that the qualified table name and "DEFAULT VALUES"
39#                 syntaxes do not work in trigger bodies.
40#
41
42do_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);
47} {}
48
49proc do_insert_tests {args} {
50  uplevel do_select_tests $args
51}
52
53# EVIDENCE-OF: R-55375-41353 -- syntax diagram insert-stmt
54#
55do_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}
127
128delete_all_data
129
130# EVIDENCE-OF: R-20288-20462 The first form (with the "VALUES" keyword)
131# creates a single new row in an existing table.
132#
133do_insert_tests e_insert-1.1 {
134    0    "SELECT count(*) FROM a2"           {0}
135
136    1a   "INSERT INTO a2 VALUES(1, 2, 3)"    {}
137    1b   "SELECT count(*) FROM a2"           {1}
138
139    2a   "INSERT INTO a2(a, b) VALUES(1, 2)" {}
140    2b   "SELECT count(*) FROM a2"           {2}
141}
142
143# EVIDENCE-OF: R-36040-20870 If no column-list is specified then the
144# number of values must be the same as the number of columns in the
145# table.
146#
147#   A test in the block above verifies that if the VALUES list has the
148#   correct number of columns (for table a2, 3 columns) works. So these
149#   tests just show that other values cause an error.
150#
151do_insert_tests e_insert-1.2 -error {
152  table %s has %d columns but %d values were supplied
153} {
154    1    "INSERT INTO a2 VALUES(1)"         {a2 3 1}
155    2    "INSERT INTO a2 VALUES(1,2)"       {a2 3 2}
156    3    "INSERT INTO a2 VALUES(1,2,3,4)"   {a2 3 4}
157    4    "INSERT INTO a2 VALUES(1,2,3,4,5)" {a2 3 5}
158}
159
160# EVIDENCE-OF: R-04006-57648 In this case the result of evaluating the
161# left-most expression in the VALUES list is inserted into the left-most
162# column of the new row, and so on.
163#
164delete_all_data
165do_insert_tests e_insert-1.3 {
166    1a   "INSERT INTO a2 VALUES(1, 2, 3)"    {}
167    1b   "SELECT * FROM a2 WHERE oid=last_insert_rowid()" {1 2 3}
168
169    2a   "INSERT INTO a2 VALUES('abc', NULL, 3*3+1)"      {}
170    2b   "SELECT * FROM a2 WHERE oid=last_insert_rowid()" {abc {} 10}
171
172    3a   "INSERT INTO a2 VALUES((SELECT count(*) FROM a2), 'x', 'y')" {}
173    3b   "SELECT * FROM a2 WHERE oid=last_insert_rowid()" {2 x y}
174}
175
176# EVIDENCE-OF: R-62524-00361 If a column-list is specified, then the
177# number of values must match the number of specified columns.
178#
179do_insert_tests e_insert-1.4 -error {
180  %d values for %d columns
181} {
182    1    "INSERT INTO a2(a, b, c) VALUES(1)"         {1 3}
183    2    "INSERT INTO a2(a, b, c) VALUES(1,2)"       {2 3}
184    3    "INSERT INTO a2(a, b, c) VALUES(1,2,3,4)"   {4 3}
185    4    "INSERT INTO a2(a, b, c) VALUES(1,2,3,4,5)" {5 3}
186
187    5    "INSERT INTO a2(c, a) VALUES(1)"            {1 2}
188    6    "INSERT INTO a2(c, a) VALUES(1,2,3)"        {3 2}
189    7    "INSERT INTO a2(c, a) VALUES(1,2,3,4)"      {4 2}
190    8    "INSERT INTO a2(c, a) VALUES(1,2,3,4,5)"    {5 2}
191}
192
193# EVIDENCE-OF: R-07016-26442 Each of the named columns of the new row is
194# populated with the results of evaluating the corresponding VALUES
195# expression.
196#
197# EVIDENCE-OF: R-12183-43719 Table columns that do not appear in the
198# column list are populated with the default column value (specified as
199# part of the CREATE TABLE statement), or with NULL if no default value
200# is specified.
201#
202delete_all_data
203do_insert_tests e_insert-1.5 {
204    1a   "INSERT INTO a2(b, c) VALUES('b', 'c')"     {}
205    1b   "SELECT * FROM a2"                          {{} b c}
206
207    2a   "INSERT INTO a2(a, b) VALUES('a', 'b')"     {}
208    2b   "SELECT * FROM a2"                          {{} b c  a b xyz}
209}
210
211# EVIDENCE-OF: R-52173-30215 A new entry is inserted into the table for
212# each row of data returned by executing the SELECT statement.
213#
214delete_all_data
215do_insert_tests e_insert-2.1 {
216    0    "SELECT count(*) FROM a1"            {0}
217
218    1a   "SELECT count(*) FROM (SELECT 1, 2)" {1}
219    1b   "INSERT INTO a1 SELECT 1, 2"         {}
220    1c   "SELECT count(*) FROM a1"            {1}
221
222    2a   "SELECT count(*) FROM (SELECT b, a FROM a1)"           {1}
223    2b   "INSERT INTO a1 SELECT b, a FROM a1"                   {}
224    2c   "SELECT count(*) FROM a1"                              {2}
225
226    3a   "SELECT count(*) FROM (SELECT b, a FROM a1)"           {2}
227    3b   "INSERT INTO a1 SELECT b, a FROM a1"                   {}
228    3c   "SELECT count(*) FROM a1"                              {4}
229
230    4a   "SELECT count(*) FROM (SELECT b, a FROM a1)"           {4}
231    4b   "INSERT INTO a1 SELECT b, a FROM a1"                   {}
232    4c   "SELECT count(*) FROM a1"                              {8}
233
234    4a   "SELECT count(*) FROM (SELECT min(b), min(a) FROM a1)" {1}
235    4b   "INSERT INTO a1 SELECT min(b), min(a) FROM a1"         {}
236    4c   "SELECT count(*) FROM a1"                              {9}
237}
238
239
240# EVIDENCE-OF: R-63614-47421 If a column-list is specified, the number
241# of columns in the result of the SELECT must be the same as the number
242# of items in the column-list.
243#
244do_insert_tests e_insert-2.2 -error {
245  %d values for %d columns
246} {
247    1    "INSERT INTO a3(x, y) SELECT a, b, c FROM a2"            {3 2}
248    2    "INSERT INTO a3(x, y) SELECT * FROM a2"                  {3 2}
249    3    "INSERT INTO a3(x, y) SELECT * FROM a2 CROSS JOIN a1"    {5 2}
250    4    "INSERT INTO a3(x, y) SELECT * FROM a2 NATURAL JOIN a1"  {3 2}
251    5    "INSERT INTO a3(x, y) SELECT a2.a FROM a2,a1"            {1 2}
252
253    6    "INSERT INTO a3(z) SELECT a, b, c FROM a2"               {3 1}
254    7    "INSERT INTO a3(z) SELECT * FROM a2"                     {3 1}
255    8    "INSERT INTO a3(z) SELECT * FROM a2 CROSS JOIN a1"       {5 1}
256    9    "INSERT INTO a3(z) SELECT * FROM a2 NATURAL JOIN a1"     {3 1}
257    10   "INSERT INTO a3(z) SELECT a1.* FROM a2,a1"               {2 1}
258}
259
260# EVIDENCE-OF: R-58951-07798 Otherwise, if no column-list is specified,
261# the number of columns in the result of the SELECT must be the same as
262# the number of columns in the table.
263#
264do_insert_tests e_insert-2.3 -error {
265  table %s has %d columns but %d values were supplied
266} {
267    1    "INSERT INTO a1 SELECT a, b, c FROM a2"            {a1 2 3}
268    2    "INSERT INTO a1 SELECT * FROM a2"                  {a1 2 3}
269    3    "INSERT INTO a1 SELECT * FROM a2 CROSS JOIN a1"    {a1 2 5}
270    4    "INSERT INTO a1 SELECT * FROM a2 NATURAL JOIN a1"  {a1 2 3}
271    5    "INSERT INTO a1 SELECT a2.a FROM a2,a1"            {a1 2 1}
272}
273
274# EVIDENCE-OF: R-31074-37730 Any SELECT statement, including compound
275# SELECTs and SELECT statements with ORDER BY and/or LIMIT clauses, may
276# be used in an INSERT statement of this form.
277#
278delete_all_data
279do_execsql_test e_insert-2.3.0 {
280  INSERT INTO a1 VALUES('x', 'y');
281} {}
282do_insert_tests e_insert-2.3 {
283  1  "INSERT INTO a1 SELECT a,b FROM a1 UNION SELECT b,a FROM a1 ORDER BY 1" {}
284  2  "INSERT INTO a1(b, a) SELECT * FROM a1 LIMIT 1"                         {}
285  3  "INSERT INTO a1 SELECT 'a'||a, 'b'||b FROM a1 LIMIT 2 OFFSET 1"         {}
286  4  "INSERT INTO a1 SELECT * FROM a1 ORDER BY b, a"                         {}
287  S  "SELECT * FROM a1" {
288      x y
289      x y y x
290      y x
291      ax by ay bx
292      ay bx ax by y x y x x y x y
293  }
294}
295
296# EVIDENCE-OF: R-25149-22012 The INSERT ... DEFAULT VALUES statement
297# inserts a single new row into the named table.
298#
299delete_all_data
300do_insert_tests e_insert-3.1 {
301    1    "SELECT count(*) FROM a3"           {0}
302    2a   "INSERT INTO a3 DEFAULT VALUES"     {}
303    2b   "SELECT count(*) FROM a3"           {1}
304}
305
306# EVIDENCE-OF: R-18927-01951 Each column of the new row is populated
307# with its default value, or with a NULL if no default value is
308# specified as part of the column definition in the CREATE TABLE
309# statement.
310#
311delete_all_data
312do_insert_tests e_insert-3.2 {
313    1.1    "INSERT INTO a3 DEFAULT VALUES"     {}
314    1.2    "SELECT * FROM a3"                  {1.0 string {}}
315
316    2.1    "INSERT INTO a3 DEFAULT VALUES"     {}
317    2.2    "SELECT * FROM a3"                  {1.0 string {} 1.0 string {}}
318
319    3.1    "INSERT INTO a2 DEFAULT VALUES"     {}
320    3.2    "SELECT * FROM a2"                  {{} {} xyz}
321
322    4.1    "INSERT INTO a2 DEFAULT VALUES"     {}
323    4.2    "SELECT * FROM a2"                  {{} {} xyz {} {} xyz}
324
325    5.1    "INSERT INTO a1 DEFAULT VALUES"     {}
326    5.2    "SELECT * FROM a1"                  {{} {}}
327
328    6.1    "INSERT INTO a1 DEFAULT VALUES"     {}
329    6.2    "SELECT * FROM a1"                  {{} {} {} {}}
330}
331
332# EVIDENCE-OF: R-46928-50290 The optional conflict-clause allows the
333# specification of an alternative constraint conflict resolution
334# algorithm to use during this one INSERT command.
335#
336# EVIDENCE-OF: R-23110-47146 the parser allows the use of the single
337# keyword REPLACE as an alias for "INSERT OR REPLACE".
338#
339#    The two requirements above are tested by e_select-4.1.* and
340#    e_select-4.2.*, respectively.
341#
342# EVIDENCE-OF: R-03421-22330 The REPLACE command is an alias for the
343# "INSERT OR REPLACE" variant of the INSERT command.
344#
345#    This is a dup of R-23110-47146. Therefore it is also verified
346#    by e_select-4.2.*. This requirement is the only one from
347#    lang_replace.html.
348#
349do_execsql_test e_insert-4.1.0 {
350  INSERT INTO a4 VALUES(1, 'a');
351  INSERT INTO a4 VALUES(2, 'a');
352  INSERT INTO a4 VALUES(3, 'a');
353} {}
354foreach {tn sql error ac data } {
355  1.1  "INSERT INTO a4 VALUES(2,'b')"  {column c is not unique}  1 {1 a 2 a 3 a}
356  1.2  "INSERT OR REPLACE INTO a4 VALUES(2, 'b')"            {}  1 {1 a 3 a 2 b}
357  1.3  "INSERT OR IGNORE INTO a4 VALUES(3, 'c')"             {}  1 {1 a 3 a 2 b}
358  1.4  "BEGIN" {} 0 {1 a 3 a 2 b}
359  1.5  "INSERT INTO a4 VALUES(1, 'd')" {column c is not unique}  0 {1 a 3 a 2 b}
360  1.6  "INSERT OR ABORT INTO a4 VALUES(1, 'd')"
361        {column c is not unique}  0 {1 a 3 a 2 b}
362  1.7  "INSERT OR ROLLBACK INTO a4 VALUES(1, 'd')"
363        {column c is not unique}  1 {1 a 3 a 2 b}
364  1.8  "INSERT INTO a4 SELECT 4, 'e' UNION ALL SELECT 3, 'e'"
365        {column c is not unique}  1 {1 a 3 a 2 b}
366  1.9  "INSERT OR FAIL INTO a4 SELECT 4, 'e' UNION ALL SELECT 3, 'e'"
367        {column c is not unique}  1 {1 a 3 a 2 b 4 e}
368
369  2.1  "INSERT INTO a4 VALUES(2,'f')"
370        {column c is not unique}  1 {1 a 3 a 2 b 4 e}
371  2.2  "REPLACE INTO a4 VALUES(2, 'f')" {}  1 {1 a 3 a 4 e 2 f}
372} {
373  do_catchsql_test e_insert-4.1.$tn.1 $sql [list [expr {$error!=""}] $error]
374  do_execsql_test  e_insert-4.1.$tn.2 {SELECT * FROM a4} [list {*}$data]
375  do_test          e_insert-4.1.$tn.3 {sqlite3_get_autocommit db} $ac
376}
377
378# EVIDENCE-OF: R-64196-02418 The optional "database-name." prefix on the
379# table-name is support for top-level INSERT statements only.
380#
381# EVIDENCE-OF: R-05731-00924 The table name must be unqualified for
382# INSERT statements that occur within CREATE TRIGGER statements.
383#
384set err {1 {qualified table names are not allowed on INSERT, UPDATE, and DELETE statements within triggers}}
385
386do_catchsql_test e_insert-5.1.1 {
387  CREATE TRIGGER AFTER UPDATE ON a1 BEGIN
388    INSERT INTO main.a4 VALUES(new.a, new.b);
389  END;
390} $err
391do_catchsql_test e_insert-5.1.2 {
392  CREATE TEMP TABLE IF NOT EXISTS tmptable(a, b);
393  CREATE TRIGGER AFTER DELETE ON a3 BEGIN
394    INSERT INTO temp.tmptable VALUES(1, 2);
395  END;
396} $err
397
398# EVIDENCE-OF: R-15888-36326 Similarly, the "DEFAULT VALUES" form of the
399# INSERT statement is supported for top-level INSERT statements only and
400# not for INSERT statements within triggers.
401#
402do_catchsql_test e_insert-5.2.1 {
403  CREATE TRIGGER AFTER UPDATE ON a1 BEGIN
404    INSERT INTO a4 DEFAULT VALUES;
405  END;
406} {1 {near "DEFAULT": syntax error}}
407
408
409delete_all_data
410
411finish_test
412