xref: /sqlite-3.40.0/test/insert2.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 that takes is
13# result from a SELECT.
14#
15# $Id: insert2.test,v 1.10 2002/06/25 13:16:04 drh Exp $
16
17set testdir [file dirname $argv0]
18source $testdir/tester.tcl
19
20# Create some tables with data that we can select against
21#
22do_test insert2-1.0 {
23  execsql {CREATE TABLE d1(n int, log int);}
24  for {set i 1} {$i<=20} {incr i} {
25    for {set j 0} {pow(2,$j)<$i} {incr j} {}
26    execsql "INSERT INTO d1 VALUES($i,$j)"
27  }
28  execsql {SELECT * FROM d1 ORDER BY n}
29} {1 0 2 1 3 2 4 2 5 3 6 3 7 3 8 3 9 4 10 4 11 4 12 4 13 4 14 4 15 4 16 4 17 5 18 5 19 5 20 5}
30
31# Insert into a new table from the old one.
32#
33do_test insert2-1.1.1 {
34  execsql {
35    CREATE TABLE t1(log int, cnt int);
36    PRAGMA count_changes=on;
37    INSERT INTO t1 SELECT log, count(*) FROM d1 GROUP BY log;
38  }
39} {6}
40do_test insert2-1.1.2 {
41  db changes
42} {6}
43do_test insert2-1.1.3 {
44  execsql {SELECT * FROM t1 ORDER BY log}
45} {0 1 1 1 2 2 3 4 4 8 5 4}
46
47do_test insert2-1.2.1 {
48  catch {execsql {DROP TABLE t1}}
49  execsql {
50    CREATE TABLE t1(log int, cnt int);
51    INSERT INTO t1
52       SELECT log, count(*) FROM d1 GROUP BY log
53       EXCEPT SELECT n-1,log FROM d1;
54  }
55} {4}
56do_test insert2-1.2.2 {
57  execsql {
58    SELECT * FROM t1 ORDER BY log;
59  }
60} {0 1 3 4 4 8 5 4}
61do_test insert2-1.3.1 {
62  catch {execsql {DROP TABLE t1}}
63  execsql {
64    CREATE TABLE t1(log int, cnt int);
65    PRAGMA count_changes=off;
66    INSERT INTO t1
67       SELECT log, count(*) FROM d1 GROUP BY log
68       INTERSECT SELECT n-1,log FROM d1;
69  }
70} {}
71do_test insert2-1.3.2 {
72  execsql {
73    SELECT * FROM t1 ORDER BY log;
74  }
75} {1 1 2 2}
76do_test insert2-1.4 {
77  catch {execsql {DROP TABLE t1}}
78  set r [execsql {
79    CREATE TABLE t1(log int, cnt int);
80    CREATE INDEX i1 ON t1(log);
81    CREATE INDEX i2 ON t1(cnt);
82    INSERT INTO t1 SELECT log, count() FROM d1 GROUP BY log;
83    SELECT * FROM t1 ORDER BY log;
84  }]
85  lappend r [execsql {SELECT cnt FROM t1 WHERE log=3}]
86  lappend r [execsql {SELECT log FROM t1 WHERE cnt=4 ORDER BY log}]
87} {0 1 1 1 2 2 3 4 4 8 5 4 4 {3 5}}
88
89do_test insert2-2.0 {
90  execsql {
91    CREATE TABLE t3(a,b,c);
92    CREATE TABLE t4(x,y);
93    INSERT INTO t4 VALUES(1,2);
94    SELECT * FROM t4;
95  }
96} {1 2}
97do_test insert2-2.1 {
98  execsql {
99    INSERT INTO t3(a,c) SELECT * FROM t4;
100    SELECT * FROM t3;
101  }
102} {1 {} 2}
103do_test insert2-2.2 {
104  execsql {
105    DELETE FROM t3;
106    INSERT INTO t3(c,b) SELECT * FROM t4;
107    SELECT * FROM t3;
108  }
109} {{} 2 1}
110do_test insert2-2.3 {
111  execsql {
112    DELETE FROM t3;
113    INSERT INTO t3(c,a,b) SELECT x, 'hi', y FROM t4;
114    SELECT * FROM t3;
115  }
116} {hi 2 1}
117
118integrity_check insert2-3.0
119
120# File table t4 with lots of data
121#
122do_test insert2-3.1 {
123  execsql {
124    SELECT * from t4;
125  }
126} {1 2}
127do_test insert2-3.2 {
128  execsql {
129    BEGIN;
130    INSERT INTO t4 VALUES(2,4);
131    INSERT INTO t4 VALUES(3,6);
132    INSERT INTO t4 VALUES(4,8);
133    INSERT INTO t4 VALUES(5,10);
134    INSERT INTO t4 VALUES(6,12);
135    INSERT INTO t4 VALUES(7,14);
136    INSERT INTO t4 VALUES(8,16);
137    INSERT INTO t4 VALUES(9,18);
138    INSERT INTO t4 VALUES(10,20);
139    COMMIT;
140  }
141  db changes
142} {9}
143do_test insert2-3.2.1 {
144  execsql {
145    SELECT count(*) FROM t4;
146  }
147} {10}
148do_test insert2-3.3 {
149  execsql {
150    BEGIN;
151    INSERT INTO t4 SELECT x+(SELECT max(x) FROM t4),y FROM t4;
152    INSERT INTO t4 SELECT x+(SELECT max(x) FROM t4),y FROM t4;
153    INSERT INTO t4 SELECT x+(SELECT max(x) FROM t4),y FROM t4;
154    INSERT INTO t4 SELECT x+(SELECT max(x) FROM t4),y FROM t4;
155    COMMIT;
156    SELECT count(*) FROM t4;
157  }
158} {160}
159do_test insert2-3.4 {
160  execsql {
161    BEGIN;
162    UPDATE t4 SET y='lots of data for the row where x=' || x
163                     || ' and y=' || y || ' - even more data to fill space';
164    COMMIT;
165    SELECT count(*) FROM t4;
166  }
167} {160}
168do_test insert2-3.5 {
169  execsql {
170    BEGIN;
171    INSERT INTO t4 SELECT x+(SELECT max(x)+1 FROM t4),y FROM t4;
172    SELECT count(*) from t4;
173    ROLLBACK;
174  }
175} {320}
176do_test insert2-3.6 {
177  execsql {
178    SELECT count(*) FROM t4;
179  }
180} {160}
181do_test insert2-3.7 {
182  execsql {
183    BEGIN;
184    DELETE FROM t4 WHERE x!=123;
185    SELECT count(*) FROM t4;
186    ROLLBACK;
187  }
188} {1}
189do_test insert2-3.8 {
190  db changes
191} {159}
192integrity_check insert2-3.9
193
194finish_test
195