xref: /sqlite-3.40.0/test/insert.test (revision c023e03e)
1# 2001 September 15
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# This file implements regression tests for SQLite library.  The
12# focus of this file is testing the INSERT statement.
13#
14# $Id: insert.test,v 1.15 2003/06/15 23:42:25 drh Exp $
15
16set testdir [file dirname $argv0]
17source $testdir/tester.tcl
18
19# Try to insert into a non-existant table.
20#
21do_test insert-1.1 {
22  set v [catch {execsql {INSERT INTO test1 VALUES(1,2,3)}} msg]
23  lappend v $msg
24} {1 {no such table: test1}}
25
26# Try to insert into sqlite_master
27#
28do_test insert-1.2 {
29  set v [catch {execsql {INSERT INTO sqlite_master VALUES(1,2,3,4)}} msg]
30  lappend v $msg
31} {1 {table sqlite_master may not be modified}}
32
33# Try to insert the wrong number of entries.
34#
35do_test insert-1.3 {
36  execsql {CREATE TABLE test1(one int, two int, three int)}
37  set v [catch {execsql {INSERT INTO test1 VALUES(1,2)}} msg]
38  lappend v $msg
39} {1 {table test1 has 3 columns but 2 values were supplied}}
40do_test insert-1.3b {
41  set v [catch {execsql {INSERT INTO test1 VALUES(1,2,3,4)}} msg]
42  lappend v $msg
43} {1 {table test1 has 3 columns but 4 values were supplied}}
44do_test insert-1.3c {
45  set v [catch {execsql {INSERT INTO test1(one,two) VALUES(1,2,3,4)}} msg]
46  lappend v $msg
47} {1 {4 values for 2 columns}}
48do_test insert-1.3d {
49  set v [catch {execsql {INSERT INTO test1(one,two) VALUES(1)}} msg]
50  lappend v $msg
51} {1 {1 values for 2 columns}}
52
53# Try to insert into a non-existant column of a table.
54#
55do_test insert-1.4 {
56  set v [catch {execsql {INSERT INTO test1(one,four) VALUES(1,2)}} msg]
57  lappend v $msg
58} {1 {table test1 has no column named four}}
59
60# Make sure the inserts actually happen
61#
62do_test insert-1.5 {
63  execsql {INSERT INTO test1 VALUES(1,2,3)}
64  execsql {SELECT * FROM test1}
65} {1 2 3}
66do_test insert-1.5b {
67  execsql {INSERT INTO test1 VALUES(4,5,6)}
68  execsql {SELECT * FROM test1 ORDER BY one}
69} {1 2 3 4 5 6}
70do_test insert-1.5c {
71  execsql {INSERT INTO test1 VALUES(7,8,9)}
72  execsql {SELECT * FROM test1 ORDER BY one}
73} {1 2 3 4 5 6 7 8 9}
74
75do_test insert-1.6 {
76  execsql {DELETE FROM test1}
77  execsql {INSERT INTO test1(one,two) VALUES(1,2)}
78  execsql {SELECT * FROM test1 ORDER BY one}
79} {1 2 {}}
80do_test insert-1.6b {
81  execsql {INSERT INTO test1(two,three) VALUES(5,6)}
82  execsql {SELECT * FROM test1 ORDER BY one}
83} {{} 5 6 1 2 {}}
84do_test insert-1.6c {
85  execsql {INSERT INTO test1(three,one) VALUES(7,8)}
86  execsql {SELECT * FROM test1 ORDER BY one}
87} {{} 5 6 1 2 {} 8 {} 7}
88
89# A table to use for testing default values
90#
91do_test insert-2.1 {
92  execsql {
93    CREATE TABLE test2(
94      f1 int default -111,
95      f2 real default +4.32,
96      f3 int default +222,
97      f4 int default 7.89
98    )
99  }
100  execsql {SELECT * from test2}
101} {}
102do_test insert-2.2 {
103  execsql {INSERT INTO test2(f1,f3) VALUES(+10,-10)}
104  execsql {SELECT * FROM test2}
105} {10 4.32 -10 7.89}
106do_test insert-2.3 {
107  execsql {INSERT INTO test2(f2,f4) VALUES(1.23,-3.45)}
108  execsql {SELECT * FROM test2 WHERE f1==-111}
109} {-111 1.23 222 -3.45}
110do_test insert-2.4 {
111  execsql {INSERT INTO test2(f1,f2,f4) VALUES(77,+1.23,3.45)}
112  execsql {SELECT * FROM test2 WHERE f1==77}
113} {77 1.23 222 3.45}
114do_test insert-2.10 {
115  execsql {
116    DROP TABLE test2;
117    CREATE TABLE test2(
118      f1 int default 111,
119      f2 real default -4.32,
120      f3 text default hi,
121      f4 text default 'abc-123',
122      f5 varchar(10)
123    )
124  }
125  execsql {SELECT * from test2}
126} {}
127do_test insert-2.11 {
128  execsql {INSERT INTO test2(f2,f4) VALUES(-2.22,'hi!')}
129  execsql {SELECT * FROM test2}
130} {111 -2.22 hi hi! {}}
131do_test insert-2.12 {
132  execsql {INSERT INTO test2(f1,f5) VALUES(1,'xyzzy')}
133  execsql {SELECT * FROM test2 ORDER BY f1}
134} {1 -4.32 hi abc-123 xyzzy 111 -2.22 hi hi! {}}
135
136# Do additional inserts with default values, but this time
137# on a table that has indices.  In particular we want to verify
138# that the correct default values are inserted into the indices.
139#
140do_test insert-3.1 {
141  execsql {
142    DELETE FROM test2;
143    CREATE INDEX index9 ON test2(f1,f2);
144    CREATE INDEX indext ON test2(f4,f5);
145    SELECT * from test2;
146  }
147} {}
148do_test insert-3.2 {
149  execsql {INSERT INTO test2(f2,f4) VALUES(-3.33,'hum')}
150  execsql {SELECT * FROM test2 WHERE f1=111 AND f2=-3.33}
151} {111 -3.33 hi hum {}}
152do_test insert-3.3 {
153  execsql {INSERT INTO test2(f1,f2,f5) VALUES(22,-4.44,'wham')}
154  execsql {SELECT * FROM test2 WHERE f1=111 AND f2=-3.33}
155} {111 -3.33 hi hum {}}
156do_test insert-3.4 {
157  execsql {SELECT * FROM test2 WHERE f1=22 AND f2=-4.44}
158} {22 -4.44 hi abc-123 wham}
159integrity_check insert-3.5
160
161# Test of expressions in the VALUES clause
162#
163do_test insert-4.1 {
164  execsql {
165    CREATE TABLE t3(a,b,c);
166    INSERT INTO t3 VALUES(1+2+3,4,5);
167    SELECT * FROM t3;
168  }
169} {6 4 5}
170do_test insert-4.2 {
171  execsql {
172    INSERT INTO t3 VALUES((SELECT max(a) FROM t3)+1,5,6);
173    SELECT * FROM t3 ORDER BY a;
174  }
175} {6 4 5 7 5 6}
176do_test insert-4.3 {
177  catchsql {
178    INSERT INTO t3 VALUES((SELECT max(a) FROM t3)+1,t3.a,6);
179    SELECT * FROM t3 ORDER BY a;
180  }
181} {1 {no such column: t3.a}}
182do_test insert-4.4 {
183  execsql {
184    INSERT INTO t3 VALUES((SELECT b FROM t3 WHERE a=0),6,7);
185    SELECT * FROM t3 ORDER BY a;
186  }
187} {{} 6 7 6 4 5 7 5 6}
188do_test insert-4.5 {
189  execsql {
190    SELECT b,c FROM t3 WHERE a IS NULL;
191  }
192} {6 7}
193do_test insert-4.6 {
194  catchsql {
195    INSERT INTO t3 VALUES(notafunc(2,3),2,3);
196  }
197} {1 {no such function: notafunc}}
198do_test insert-4.7 {
199  execsql {
200    INSERT INTO t3 VALUES(min(1,2,3),max(1,2,3),99);
201    SELECT * FROM t3 WHERE c=99;
202  }
203} {1 3 99}
204
205# Test the ability to insert from a temporary table into itself.
206# Ticket #275.
207#
208do_test insert-5.1 {
209  execsql {
210    CREATE TEMP TABLE t4(x);
211    INSERT INTO t4 VALUES(1);
212    SELECT * FROM t4;
213  }
214} {1}
215do_test insert-5.2 {
216  execsql {
217    INSERT INTO t4 SELECT x+1 FROM t4;
218    SELECT * FROM t4;
219  }
220} {1 2}
221do_test insert-5.3 {
222  # verify that a temporary table is used to copy t4 to t4
223  set x [execsql {
224    EXPLAIN INSERT INTO t4 SELECT x+2 FROM t4;
225  }]
226  expr {[lsearch $x OpenTemp]>0}
227} {1}
228do_test insert-5.4 {
229  # Verify that table "test1" begins on page 3.  This should be the same
230  # page number used by "t4" above.
231  execsql {
232    SELECT rootpage FROM sqlite_master WHERE name='test1';
233  }
234} {3}
235do_test insert-5.5 {
236  # Verify that "t4" begins on page 3.
237  execsql {
238    SELECT rootpage FROM sqlite_temp_master WHERE name='t4';
239  }
240} {3}
241do_test insert-5.6 {
242  # This should not use an intermediate temporary table.
243  execsql {
244    INSERT INTO t4 SELECT one FROM test1 WHERE three=7;
245    SELECT * FROM t4
246  }
247} {1 2 8}
248do_test insert-5.7 {
249  # verify that no temporary table is used to copy test1 to t4
250  set x [execsql {
251    EXPLAIN INSERT INTO t4 SELECT one FROM test1;
252  }]
253  expr {[lsearch $x OpenTemp]>0}
254} {0}
255
256# Ticket #334:  REPLACE statement corrupting indices.
257#
258do_test insert-6.1 {
259  execsql {
260    CREATE TABLE t1(a INTEGER PRIMARY KEY, b UNIQUE);
261    INSERT INTO t1 VALUES(1,2);
262    INSERT INTO t1 VALUES(2,3);
263    SELECT b FROM t1 WHERE b=2;
264  }
265} {2}
266do_test insert-6.2 {
267  execsql {
268    REPLACE INTO t1 VALUES(1,4);
269    SELECT b FROM t1 WHERE b=2;
270  }
271} {}
272do_test insert-6.3 {
273  execsql {
274    UPDATE OR REPLACE t1 SET a=2 WHERE b=4;
275    SELECT * FROM t1 WHERE b=4;
276  }
277} {2 4}
278do_test insert-6.4 {
279  execsql {
280    SELECT * FROM t1 WHERE b=3;
281  }
282} {}
283
284integrity_check insert-99.0
285
286finish_test
287