2 #pragma ident "%Z%%M% %I% %E% SMI"
6 # The author disclaims copyright to this source code. In place of
7 # a legal notice, here is a blessing:
9 # May you do good and not evil.
10 # May you find forgiveness for yourself and forgive others.
11 # May you share freely, never taking more than you give.
13 #***********************************************************************
14 # This file implements regression tests for SQLite library. The
15 # focus of this file is testing the INSERT statement.
17 # $Id: insert.test,v 1.15 2003/06/15 23:42:25 drh Exp $
19 set testdir [file dirname $argv0]
20 source $testdir/tester.tcl
22 # Try to insert into a non-existant table.
25 set v [catch {execsql {INSERT INTO test1 VALUES(1,2,3)}} msg]
27 } {1 {no such table: test1}}
29 # Try to insert into sqlite_master
32 set v [catch {execsql {INSERT INTO sqlite_master VALUES(1,2,3,4)}} msg]
34 } {1 {table sqlite_master may not be modified}}
36 # Try to insert the wrong number of entries.
39 execsql {CREATE TABLE test1(one int, two int, three int)}
40 set v [catch {execsql {INSERT INTO test1 VALUES(1,2)}} msg]
42 } {1 {table test1 has 3 columns but 2 values were supplied}}
44 set v [catch {execsql {INSERT INTO test1 VALUES(1,2,3,4)}} msg]
46 } {1 {table test1 has 3 columns but 4 values were supplied}}
48 set v [catch {execsql {INSERT INTO test1(one,two) VALUES(1,2,3,4)}} msg]
50 } {1 {4 values for 2 columns}}
52 set v [catch {execsql {INSERT INTO test1(one,two) VALUES(1)}} msg]
54 } {1 {1 values for 2 columns}}
56 # Try to insert into a non-existant column of a table.
59 set v [catch {execsql {INSERT INTO test1(one,four) VALUES(1,2)}} msg]
61 } {1 {table test1 has no column named four}}
63 # Make sure the inserts actually happen
66 execsql {INSERT INTO test1 VALUES(1,2,3)}
67 execsql {SELECT * FROM test1}
70 execsql {INSERT INTO test1 VALUES(4,5,6)}
71 execsql {SELECT * FROM test1 ORDER BY one}
74 execsql {INSERT INTO test1 VALUES(7,8,9)}
75 execsql {SELECT * FROM test1 ORDER BY one}
79 execsql {DELETE FROM test1}
80 execsql {INSERT INTO test1(one,two) VALUES(1,2)}
81 execsql {SELECT * FROM test1 ORDER BY one}
84 execsql {INSERT INTO test1(two,three) VALUES(5,6)}
85 execsql {SELECT * FROM test1 ORDER BY one}
88 execsql {INSERT INTO test1(three,one) VALUES(7,8)}
89 execsql {SELECT * FROM test1 ORDER BY one}
90 } {{} 5 6 1 2 {} 8 {} 7}
92 # A table to use for testing default values
98 f2 real default +4.32,
103 execsql {SELECT * from test2}
106 execsql {INSERT INTO test2(f1,f3) VALUES(+10,-10)}
107 execsql {SELECT * FROM test2}
110 execsql {INSERT INTO test2(f2,f4) VALUES(1.23,-3.45)}
111 execsql {SELECT * FROM test2 WHERE f1==-111}
112 } {-111 1.23 222 -3.45}
114 execsql {INSERT INTO test2(f1,f2,f4) VALUES(77,+1.23,3.45)}
115 execsql {SELECT * FROM test2 WHERE f1==77}
117 do_test insert-2.10 {
122 f2 real default -4.32,
124 f4 text default 'abc-123',
128 execsql {SELECT * from test2}
130 do_test insert-2.11 {
131 execsql {INSERT INTO test2(f2,f4) VALUES(-2.22,'hi!')}
132 execsql {SELECT * FROM test2}
133 } {111 -2.22 hi hi! {}}
134 do_test insert-2.12 {
135 execsql {INSERT INTO test2(f1,f5) VALUES(1,'xyzzy')}
136 execsql {SELECT * FROM test2 ORDER BY f1}
137 } {1 -4.32 hi abc-123 xyzzy 111 -2.22 hi hi! {}}
139 # Do additional inserts with default values, but this time
140 # on a table that has indices. In particular we want to verify
141 # that the correct default values are inserted into the indices.
146 CREATE INDEX index9 ON test2(f1,f2);
147 CREATE INDEX indext ON test2(f4,f5);
152 execsql {INSERT INTO test2(f2,f4) VALUES(-3.33,'hum')}
153 execsql {SELECT * FROM test2 WHERE f1=111 AND f2=-3.33}
154 } {111 -3.33 hi hum {}}
156 execsql {INSERT INTO test2(f1,f2,f5) VALUES(22,-4.44,'wham')}
157 execsql {SELECT * FROM test2 WHERE f1=111 AND f2=-3.33}
158 } {111 -3.33 hi hum {}}
160 execsql {SELECT * FROM test2 WHERE f1=22 AND f2=-4.44}
161 } {22 -4.44 hi abc-123 wham}
162 integrity_check insert-3.5
164 # Test of expressions in the VALUES clause
168 CREATE TABLE t3(a,b,c);
169 INSERT INTO t3 VALUES(1+2+3,4,5);
175 INSERT INTO t3 VALUES((SELECT max(a) FROM t3)+1,5,6);
176 SELECT * FROM t3 ORDER BY a;
181 INSERT INTO t3 VALUES((SELECT max(a) FROM t3)+1,t3.a,6);
182 SELECT * FROM t3 ORDER BY a;
184 } {1 {no such column: t3.a}}
187 INSERT INTO t3 VALUES((SELECT b FROM t3 WHERE a=0),6,7);
188 SELECT * FROM t3 ORDER BY a;
190 } {{} 6 7 6 4 5 7 5 6}
193 SELECT b,c FROM t3 WHERE a IS NULL;
198 INSERT INTO t3 VALUES(notafunc(2,3),2,3);
200 } {1 {no such function: notafunc}}
203 INSERT INTO t3 VALUES(min(1,2,3),max(1,2,3),99);
204 SELECT * FROM t3 WHERE c=99;
208 # Test the ability to insert from a temporary table into itself.
213 CREATE TEMP TABLE t4(x);
214 INSERT INTO t4 VALUES(1);
220 INSERT INTO t4 SELECT x+1 FROM t4;
225 # verify that a temporary table is used to copy t4 to t4
227 EXPLAIN INSERT INTO t4 SELECT x+2 FROM t4;
229 expr {[lsearch $x OpenTemp]>0}
232 # Verify that table "test1" begins on page 3. This should be the same
233 # page number used by "t4" above.
235 SELECT rootpage FROM sqlite_master WHERE name='test1';
239 # Verify that "t4" begins on page 3.
241 SELECT rootpage FROM sqlite_temp_master WHERE name='t4';
245 # This should not use an intermediate temporary table.
247 INSERT INTO t4 SELECT one FROM test1 WHERE three=7;
252 # verify that no temporary table is used to copy test1 to t4
254 EXPLAIN INSERT INTO t4 SELECT one FROM test1;
256 expr {[lsearch $x OpenTemp]>0}
259 # Ticket #334: REPLACE statement corrupting indices.
263 CREATE TABLE t1(a INTEGER PRIMARY KEY, b UNIQUE);
264 INSERT INTO t1 VALUES(1,2);
265 INSERT INTO t1 VALUES(2,3);
266 SELECT b FROM t1 WHERE b=2;
271 REPLACE INTO t1 VALUES(1,4);
272 SELECT b FROM t1 WHERE b=2;
277 UPDATE OR REPLACE t1 SET a=2 WHERE b=4;
278 SELECT * FROM t1 WHERE b=4;
283 SELECT * FROM t1 WHERE b=3;
287 integrity_check insert-99.0