4 # The author disclaims copyright to this source code. In place of
5 # a legal notice, here is a blessing:
7 # May you do good and not evil.
8 # May you find forgiveness for yourself and forgive others.
9 # May you share freely, never taking more than you give.
11 #***********************************************************************
12 # This file implements regression tests for SQLite library. The
13 # focus of this file is testing the INSERT statement.
15 # $Id: insert.test,v 1.15 2003/06/15 23:42:25 drh Exp $
17 set testdir [file dirname $argv0]
18 source $testdir/tester.tcl
20 # Try to insert into a non-existant table.
23 set v [catch {execsql {INSERT INTO test1 VALUES(1,2,3)}} msg]
25 } {1 {no such table: test1}}
27 # Try to insert into sqlite_master
30 set v [catch {execsql {INSERT INTO sqlite_master VALUES(1,2,3,4)}} msg]
32 } {1 {table sqlite_master may not be modified}}
34 # Try to insert the wrong number of entries.
37 execsql {CREATE TABLE test1(one int, two int, three int)}
38 set v [catch {execsql {INSERT INTO test1 VALUES(1,2)}} msg]
40 } {1 {table test1 has 3 columns but 2 values were supplied}}
42 set v [catch {execsql {INSERT INTO test1 VALUES(1,2,3,4)}} msg]
44 } {1 {table test1 has 3 columns but 4 values were supplied}}
46 set v [catch {execsql {INSERT INTO test1(one,two) VALUES(1,2,3,4)}} msg]
48 } {1 {4 values for 2 columns}}
50 set v [catch {execsql {INSERT INTO test1(one,two) VALUES(1)}} msg]
52 } {1 {1 values for 2 columns}}
54 # Try to insert into a non-existant column of a table.
57 set v [catch {execsql {INSERT INTO test1(one,four) VALUES(1,2)}} msg]
59 } {1 {table test1 has no column named four}}
61 # Make sure the inserts actually happen
64 execsql {INSERT INTO test1 VALUES(1,2,3)}
65 execsql {SELECT * FROM test1}
68 execsql {INSERT INTO test1 VALUES(4,5,6)}
69 execsql {SELECT * FROM test1 ORDER BY one}
72 execsql {INSERT INTO test1 VALUES(7,8,9)}
73 execsql {SELECT * FROM test1 ORDER BY one}
77 execsql {DELETE FROM test1}
78 execsql {INSERT INTO test1(one,two) VALUES(1,2)}
79 execsql {SELECT * FROM test1 ORDER BY one}
82 execsql {INSERT INTO test1(two,three) VALUES(5,6)}
83 execsql {SELECT * FROM test1 ORDER BY one}
86 execsql {INSERT INTO test1(three,one) VALUES(7,8)}
87 execsql {SELECT * FROM test1 ORDER BY one}
88 } {{} 5 6 1 2 {} 8 {} 7}
90 # A table to use for testing default values
96 f2 real default +4.32,
101 execsql {SELECT * from test2}
104 execsql {INSERT INTO test2(f1,f3) VALUES(+10,-10)}
105 execsql {SELECT * FROM test2}
108 execsql {INSERT INTO test2(f2,f4) VALUES(1.23,-3.45)}
109 execsql {SELECT * FROM test2 WHERE f1==-111}
110 } {-111 1.23 222 -3.45}
112 execsql {INSERT INTO test2(f1,f2,f4) VALUES(77,+1.23,3.45)}
113 execsql {SELECT * FROM test2 WHERE f1==77}
115 do_test insert-2.10 {
120 f2 real default -4.32,
122 f4 text default 'abc-123',
126 execsql {SELECT * from test2}
128 do_test insert-2.11 {
129 execsql {INSERT INTO test2(f2,f4) VALUES(-2.22,'hi!')}
130 execsql {SELECT * FROM test2}
131 } {111 -2.22 hi hi! {}}
132 do_test insert-2.12 {
133 execsql {INSERT INTO test2(f1,f5) VALUES(1,'xyzzy')}
134 execsql {SELECT * FROM test2 ORDER BY f1}
135 } {1 -4.32 hi abc-123 xyzzy 111 -2.22 hi hi! {}}
137 # Do additional inserts with default values, but this time
138 # on a table that has indices. In particular we want to verify
139 # that the correct default values are inserted into the indices.
144 CREATE INDEX index9 ON test2(f1,f2);
145 CREATE INDEX indext ON test2(f4,f5);
150 execsql {INSERT INTO test2(f2,f4) VALUES(-3.33,'hum')}
151 execsql {SELECT * FROM test2 WHERE f1=111 AND f2=-3.33}
152 } {111 -3.33 hi hum {}}
154 execsql {INSERT INTO test2(f1,f2,f5) VALUES(22,-4.44,'wham')}
155 execsql {SELECT * FROM test2 WHERE f1=111 AND f2=-3.33}
156 } {111 -3.33 hi hum {}}
158 execsql {SELECT * FROM test2 WHERE f1=22 AND f2=-4.44}
159 } {22 -4.44 hi abc-123 wham}
160 integrity_check insert-3.5
162 # Test of expressions in the VALUES clause
166 CREATE TABLE t3(a,b,c);
167 INSERT INTO t3 VALUES(1+2+3,4,5);
173 INSERT INTO t3 VALUES((SELECT max(a) FROM t3)+1,5,6);
174 SELECT * FROM t3 ORDER BY a;
179 INSERT INTO t3 VALUES((SELECT max(a) FROM t3)+1,t3.a,6);
180 SELECT * FROM t3 ORDER BY a;
182 } {1 {no such column: t3.a}}
185 INSERT INTO t3 VALUES((SELECT b FROM t3 WHERE a=0),6,7);
186 SELECT * FROM t3 ORDER BY a;
188 } {{} 6 7 6 4 5 7 5 6}
191 SELECT b,c FROM t3 WHERE a IS NULL;
196 INSERT INTO t3 VALUES(notafunc(2,3),2,3);
198 } {1 {no such function: notafunc}}
201 INSERT INTO t3 VALUES(min(1,2,3),max(1,2,3),99);
202 SELECT * FROM t3 WHERE c=99;
206 # Test the ability to insert from a temporary table into itself.
211 CREATE TEMP TABLE t4(x);
212 INSERT INTO t4 VALUES(1);
218 INSERT INTO t4 SELECT x+1 FROM t4;
223 # verify that a temporary table is used to copy t4 to t4
225 EXPLAIN INSERT INTO t4 SELECT x+2 FROM t4;
227 expr {[lsearch $x OpenTemp]>0}
230 # Verify that table "test1" begins on page 3. This should be the same
231 # page number used by "t4" above.
233 SELECT rootpage FROM sqlite_master WHERE name='test1';
237 # Verify that "t4" begins on page 3.
239 SELECT rootpage FROM sqlite_temp_master WHERE name='t4';
243 # This should not use an intermediate temporary table.
245 INSERT INTO t4 SELECT one FROM test1 WHERE three=7;
250 # verify that no temporary table is used to copy test1 to t4
252 EXPLAIN INSERT INTO t4 SELECT one FROM test1;
254 expr {[lsearch $x OpenTemp]>0}
257 # Ticket #334: REPLACE statement corrupting indices.
261 CREATE TABLE t1(a INTEGER PRIMARY KEY, b UNIQUE);
262 INSERT INTO t1 VALUES(1,2);
263 INSERT INTO t1 VALUES(2,3);
264 SELECT b FROM t1 WHERE b=2;
269 REPLACE INTO t1 VALUES(1,4);
270 SELECT b FROM t1 WHERE b=2;
275 UPDATE OR REPLACE t1 SET a=2 WHERE b=4;
276 SELECT * FROM t1 WHERE b=4;
281 SELECT * FROM t1 WHERE b=3;
285 integrity_check insert-99.0