blob: 7be6b376955a3177b82f103d391763474185487e [file] [log] [blame]
drhb19a2bc2001-09-16 00:13:26 +00001# 2001 September 15
drh348784e2000-05-29 20:41:49 +00002#
drhb19a2bc2001-09-16 00:13:26 +00003# The author disclaims copyright to this source code. In place of
4# a legal notice, here is a blessing:
drh348784e2000-05-29 20:41:49 +00005#
drhb19a2bc2001-09-16 00:13:26 +00006# 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.
drh348784e2000-05-29 20:41:49 +00009#
10#***********************************************************************
11# This file implements regression tests for SQLite library. The
12# focus of this file is testing the CREATE TABLE statement.
13#
drh348784e2000-05-29 20:41:49 +000014
15set testdir [file dirname $argv0]
16source $testdir/tester.tcl
17
18# Create a basic table and verify it is added to sqlite_master
19#
drh1b6a71f2000-05-29 23:58:11 +000020do_test table-1.1 {
drh348784e2000-05-29 20:41:49 +000021 execsql {
22 CREATE TABLE test1 (
23 one varchar(10),
24 two text
25 )
26 }
27 execsql {
drh28037572000-08-02 13:47:41 +000028 SELECT sql FROM sqlite_master WHERE type!='meta'
drh348784e2000-05-29 20:41:49 +000029 }
30} {{CREATE TABLE test1 (
31 one varchar(10),
32 two text
33 )}}
34
drh348784e2000-05-29 20:41:49 +000035
36# Verify the other fields of the sqlite_master file.
37#
drh1b6a71f2000-05-29 23:58:11 +000038do_test table-1.3 {
drh28037572000-08-02 13:47:41 +000039 execsql {SELECT name, tbl_name, type FROM sqlite_master WHERE type!='meta'}
drh348784e2000-05-29 20:41:49 +000040} {test1 test1 table}
41
42# Close and reopen the database. Verify that everything is
43# still the same.
44#
drh1b6a71f2000-05-29 23:58:11 +000045do_test table-1.4 {
drh348784e2000-05-29 20:41:49 +000046 db close
drhef4ac8f2004-06-19 00:16:31 +000047 sqlite3 db test.db
drh28037572000-08-02 13:47:41 +000048 execsql {SELECT name, tbl_name, type from sqlite_master WHERE type!='meta'}
drh348784e2000-05-29 20:41:49 +000049} {test1 test1 table}
50
51# Drop the database and make sure it disappears.
52#
drh1b6a71f2000-05-29 23:58:11 +000053do_test table-1.5 {
drh348784e2000-05-29 20:41:49 +000054 execsql {DROP TABLE test1}
drh28037572000-08-02 13:47:41 +000055 execsql {SELECT * FROM sqlite_master WHERE type!='meta'}
drh348784e2000-05-29 20:41:49 +000056} {}
57
drh348784e2000-05-29 20:41:49 +000058# Close and reopen the database. Verify that the table is
59# still gone.
60#
drh1b6a71f2000-05-29 23:58:11 +000061do_test table-1.6 {
drh348784e2000-05-29 20:41:49 +000062 db close
drhef4ac8f2004-06-19 00:16:31 +000063 sqlite3 db test.db
drh28037572000-08-02 13:47:41 +000064 execsql {SELECT name FROM sqlite_master WHERE type!='meta'}
drh348784e2000-05-29 20:41:49 +000065} {}
66
drh982cef72000-05-30 16:27:03 +000067# Repeat the above steps, but this time quote the table name.
68#
69do_test table-1.10 {
70 execsql {CREATE TABLE "create" (f1 int)}
drh28037572000-08-02 13:47:41 +000071 execsql {SELECT name FROM sqlite_master WHERE type!='meta'}
drh982cef72000-05-30 16:27:03 +000072} {create}
73do_test table-1.11 {
74 execsql {DROP TABLE "create"}
drh28037572000-08-02 13:47:41 +000075 execsql {SELECT name FROM "sqlite_master" WHERE type!='meta'}
drh982cef72000-05-30 16:27:03 +000076} {}
77do_test table-1.12 {
78 execsql {CREATE TABLE test1("f1 ho" int)}
drh28037572000-08-02 13:47:41 +000079 execsql {SELECT name as "X" FROM sqlite_master WHERE type!='meta'}
drh982cef72000-05-30 16:27:03 +000080} {test1}
81do_test table-1.13 {
82 execsql {DROP TABLE "TEST1"}
drh28037572000-08-02 13:47:41 +000083 execsql {SELECT name FROM "sqlite_master" WHERE type!='meta'}
drh982cef72000-05-30 16:27:03 +000084} {}
85
86
drhb24fcbe2000-05-29 23:30:50 +000087
88# Verify that we cannot make two tables with the same name
89#
drh1b6a71f2000-05-29 23:58:11 +000090do_test table-2.1 {
drh4cfa7932000-06-08 15:10:46 +000091 execsql {CREATE TABLE TEST2(one text)}
drh42b9d7c2005-08-13 00:56:27 +000092 catchsql {CREATE TABLE test2(two text default 'hi')}
drh1d37e282000-05-30 03:12:21 +000093} {1 {table test2 already exists}}
drhe4df0e72006-03-29 00:24:06 +000094do_test table-2.1.1 {
95 catchsql {CREATE TABLE "test2" (two)}
96} {1 {table "test2" already exists}}
drh1b6a71f2000-05-29 23:58:11 +000097do_test table-2.1b {
drhb24fcbe2000-05-29 23:30:50 +000098 set v [catch {execsql {CREATE TABLE sqlite_master(two text)}} msg]
99 lappend v $msg
danielk1977d8123362004-06-12 09:25:12 +0000100} {1 {object name reserved for internal use: sqlite_master}}
drh1b6a71f2000-05-29 23:58:11 +0000101do_test table-2.1c {
drhb24fcbe2000-05-29 23:30:50 +0000102 db close
drhef4ac8f2004-06-19 00:16:31 +0000103 sqlite3 db test.db
drhb24fcbe2000-05-29 23:30:50 +0000104 set v [catch {execsql {CREATE TABLE sqlite_master(two text)}} msg]
105 lappend v $msg
danielk1977d8123362004-06-12 09:25:12 +0000106} {1 {object name reserved for internal use: sqlite_master}}
drh1b6a71f2000-05-29 23:58:11 +0000107do_test table-2.1d {
drhfaa59552005-12-29 23:33:54 +0000108 catchsql {CREATE TABLE IF NOT EXISTS test2(x,y)}
109} {0 {}}
110do_test table-2.1e {
drha6370df2006-01-04 21:40:06 +0000111 catchsql {CREATE TABLE IF NOT EXISTS test2(x UNIQUE, y TEXT PRIMARY KEY)}
112} {0 {}}
113do_test table-2.1f {
drh28037572000-08-02 13:47:41 +0000114 execsql {DROP TABLE test2; SELECT name FROM sqlite_master WHERE type!='meta'}
drhb24fcbe2000-05-29 23:30:50 +0000115} {}
116
117# Verify that we cannot make a table with the same name as an index
118#
drhdcc581c2000-05-30 13:44:19 +0000119do_test table-2.2a {
drh8a1e5942009-04-28 15:43:45 +0000120 execsql {CREATE TABLE test2(one text)}
121 execsql {CREATE INDEX test3 ON test2(one)}
122 catchsql {CREATE TABLE test3(two text)}
drh1d37e282000-05-30 03:12:21 +0000123} {1 {there is already an index named test3}}
drh1b6a71f2000-05-29 23:58:11 +0000124do_test table-2.2b {
drhb24fcbe2000-05-29 23:30:50 +0000125 db close
drhef4ac8f2004-06-19 00:16:31 +0000126 sqlite3 db test.db
drhb24fcbe2000-05-29 23:30:50 +0000127 set v [catch {execsql {CREATE TABLE test3(two text)}} msg]
128 lappend v $msg
drh1d37e282000-05-30 03:12:21 +0000129} {1 {there is already an index named test3}}
drh1b6a71f2000-05-29 23:58:11 +0000130do_test table-2.2c {
drh3fc190c2001-09-14 03:24:23 +0000131 execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
132} {test2 test3}
133do_test table-2.2d {
drhb24fcbe2000-05-29 23:30:50 +0000134 execsql {DROP INDEX test3}
135 set v [catch {execsql {CREATE TABLE test3(two text)}} msg]
136 lappend v $msg
137} {0 {}}
drh3fc190c2001-09-14 03:24:23 +0000138do_test table-2.2e {
drh28037572000-08-02 13:47:41 +0000139 execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
drhb24fcbe2000-05-29 23:30:50 +0000140} {test2 test3}
drh3fc190c2001-09-14 03:24:23 +0000141do_test table-2.2f {
drhb24fcbe2000-05-29 23:30:50 +0000142 execsql {DROP TABLE test2; DROP TABLE test3}
drh28037572000-08-02 13:47:41 +0000143 execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
drhb24fcbe2000-05-29 23:30:50 +0000144} {}
145
146# Create a table with many field names
147#
148set big_table \
149{CREATE TABLE big(
150 f1 varchar(20),
151 f2 char(10),
drhdcc581c2000-05-30 13:44:19 +0000152 f3 varchar(30) primary key,
drhb24fcbe2000-05-29 23:30:50 +0000153 f4 text,
154 f5 text,
155 f6 text,
156 f7 text,
157 f8 text,
158 f9 text,
159 f10 text,
160 f11 text,
161 f12 text,
162 f13 text,
163 f14 text,
164 f15 text,
165 f16 text,
166 f17 text,
167 f18 text,
168 f19 text,
169 f20 text
170)}
drh1b6a71f2000-05-29 23:58:11 +0000171do_test table-3.1 {
drhb24fcbe2000-05-29 23:30:50 +0000172 execsql $big_table
drhadbca9c2001-09-27 15:11:53 +0000173 execsql {SELECT sql FROM sqlite_master WHERE type=='table'}
drhb24fcbe2000-05-29 23:30:50 +0000174} \{$big_table\}
drh1b6a71f2000-05-29 23:58:11 +0000175do_test table-3.2 {
drhb24fcbe2000-05-29 23:30:50 +0000176 set v [catch {execsql {CREATE TABLE BIG(xyz foo)}} msg]
177 lappend v $msg
drh1d37e282000-05-30 03:12:21 +0000178} {1 {table BIG already exists}}
drh1b6a71f2000-05-29 23:58:11 +0000179do_test table-3.3 {
drhb24fcbe2000-05-29 23:30:50 +0000180 set v [catch {execsql {CREATE TABLE biG(xyz foo)}} msg]
181 lappend v $msg
drh1d37e282000-05-30 03:12:21 +0000182} {1 {table biG already exists}}
drh1b6a71f2000-05-29 23:58:11 +0000183do_test table-3.4 {
drhb24fcbe2000-05-29 23:30:50 +0000184 set v [catch {execsql {CREATE TABLE bIg(xyz foo)}} msg]
185 lappend v $msg
drh1d37e282000-05-30 03:12:21 +0000186} {1 {table bIg already exists}}
drh1b6a71f2000-05-29 23:58:11 +0000187do_test table-3.5 {
drhb24fcbe2000-05-29 23:30:50 +0000188 db close
drhef4ac8f2004-06-19 00:16:31 +0000189 sqlite3 db test.db
drhb24fcbe2000-05-29 23:30:50 +0000190 set v [catch {execsql {CREATE TABLE Big(xyz foo)}} msg]
191 lappend v $msg
drh1d37e282000-05-30 03:12:21 +0000192} {1 {table Big already exists}}
drh1b6a71f2000-05-29 23:58:11 +0000193do_test table-3.6 {
drhb24fcbe2000-05-29 23:30:50 +0000194 execsql {DROP TABLE big}
drh28037572000-08-02 13:47:41 +0000195 execsql {SELECT name FROM sqlite_master WHERE type!='meta'}
drhb24fcbe2000-05-29 23:30:50 +0000196} {}
197
198# Try creating large numbers of tables
199#
200set r {}
201for {set i 1} {$i<=100} {incr i} {
drha9e99ae2002-08-13 23:02:57 +0000202 lappend r [format test%03d $i]
drhb24fcbe2000-05-29 23:30:50 +0000203}
drh1b6a71f2000-05-29 23:58:11 +0000204do_test table-4.1 {
drhb24fcbe2000-05-29 23:30:50 +0000205 for {set i 1} {$i<=100} {incr i} {
drha9e99ae2002-08-13 23:02:57 +0000206 set sql "CREATE TABLE [format test%03d $i] ("
drhb24fcbe2000-05-29 23:30:50 +0000207 for {set k 1} {$k<$i} {incr k} {
208 append sql "field$k text,"
209 }
210 append sql "last_field text)"
211 execsql $sql
212 }
drh28037572000-08-02 13:47:41 +0000213 execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
drhb24fcbe2000-05-29 23:30:50 +0000214} $r
drh1b6a71f2000-05-29 23:58:11 +0000215do_test table-4.1b {
drhb24fcbe2000-05-29 23:30:50 +0000216 db close
drhef4ac8f2004-06-19 00:16:31 +0000217 sqlite3 db test.db
drh28037572000-08-02 13:47:41 +0000218 execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
drhb24fcbe2000-05-29 23:30:50 +0000219} $r
220
drhc4a3c772001-04-04 11:48:57 +0000221# Drop the even numbered tables
drhb24fcbe2000-05-29 23:30:50 +0000222#
223set r {}
224for {set i 1} {$i<=100} {incr i 2} {
drha9e99ae2002-08-13 23:02:57 +0000225 lappend r [format test%03d $i]
drhb24fcbe2000-05-29 23:30:50 +0000226}
drh1b6a71f2000-05-29 23:58:11 +0000227do_test table-4.2 {
drhb24fcbe2000-05-29 23:30:50 +0000228 for {set i 2} {$i<=100} {incr i 2} {
drh428ae8c2003-01-04 16:48:09 +0000229 # if {$i==38} {execsql {pragma vdbe_trace=on}}
drha9e99ae2002-08-13 23:02:57 +0000230 set sql "DROP TABLE [format TEST%03d $i]"
drhb24fcbe2000-05-29 23:30:50 +0000231 execsql $sql
232 }
drh28037572000-08-02 13:47:41 +0000233 execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
drhb24fcbe2000-05-29 23:30:50 +0000234} $r
drh3fc190c2001-09-14 03:24:23 +0000235#exit
drhb24fcbe2000-05-29 23:30:50 +0000236
237# Drop the odd number tables
238#
drh1b6a71f2000-05-29 23:58:11 +0000239do_test table-4.3 {
drhb24fcbe2000-05-29 23:30:50 +0000240 for {set i 1} {$i<=100} {incr i 2} {
drha9e99ae2002-08-13 23:02:57 +0000241 set sql "DROP TABLE [format test%03d $i]"
drhb24fcbe2000-05-29 23:30:50 +0000242 execsql $sql
243 }
drh28037572000-08-02 13:47:41 +0000244 execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
drhb24fcbe2000-05-29 23:30:50 +0000245} {}
246
drh1b6a71f2000-05-29 23:58:11 +0000247# Try to drop a table that does not exist
248#
drha0733842005-12-29 01:11:36 +0000249do_test table-5.1.1 {
250 catchsql {DROP TABLE test009}
drha9e99ae2002-08-13 23:02:57 +0000251} {1 {no such table: test009}}
drha0733842005-12-29 01:11:36 +0000252do_test table-5.1.2 {
253 catchsql {DROP TABLE IF EXISTS test009}
254} {0 {}}
drh1b6a71f2000-05-29 23:58:11 +0000255
256# Try to drop sqlite_master
257#
258do_test table-5.2 {
drha0733842005-12-29 01:11:36 +0000259 catchsql {DROP TABLE IF EXISTS sqlite_master}
drh1d37e282000-05-30 03:12:21 +0000260} {1 {table sqlite_master may not be dropped}}
drh1b6a71f2000-05-29 23:58:11 +0000261
drh08ccfaa2011-10-07 23:52:25 +0000262# Dropping sqlite_statN tables is OK.
263#
264do_test table-5.2.1 {
265 db eval {
266 ANALYZE;
267 DROP TABLE IF EXISTS sqlite_stat1;
268 DROP TABLE IF EXISTS sqlite_stat2;
dan8ad169a2013-08-12 20:14:04 +0000269 DROP TABLE IF EXISTS sqlite_stat3;
danc55521a2013-08-05 05:34:30 +0000270 DROP TABLE IF EXISTS sqlite_stat4;
drh08ccfaa2011-10-07 23:52:25 +0000271 SELECT name FROM sqlite_master WHERE name GLOB 'sqlite_stat*';
272 }
273} {}
274
drh97ab32b2015-04-17 18:22:53 +0000275do_test table-5.2.2 {
276 db close
277 forcedelete test.db
278 sqlite3 db test.db
drh6ab91a72018-11-07 02:17:01 +0000279 sqlite3_db_config db DEFENSIVE 0
drh97ab32b2015-04-17 18:22:53 +0000280 db eval {
281 CREATE TABLE t0(a,b);
282 CREATE INDEX t ON t0(a);
283 PRAGMA writable_schema=ON;
284 UPDATE sqlite_master SET sql='CREATE TABLE a.b(a UNIQUE';
285 BEGIN;
286 CREATE TABLE t1(x);
287 ROLLBACK;
288 DROP TABLE IF EXISTS t99;
289 }
290} {}
291db close
292forcedelete test.db
293sqlite3 db test.db
294
drh1b6a71f2000-05-29 23:58:11 +0000295# Make sure an EXPLAIN does not really create a new table
296#
297do_test table-5.3 {
drh6bf89572004-11-03 16:27:01 +0000298 ifcapable {explain} {
299 execsql {EXPLAIN CREATE TABLE test1(f1 int)}
300 }
drh28037572000-08-02 13:47:41 +0000301 execsql {SELECT name FROM sqlite_master WHERE type!='meta'}
drh1b6a71f2000-05-29 23:58:11 +0000302} {}
303
304# Make sure an EXPLAIN does not really drop an existing table
305#
306do_test table-5.4 {
307 execsql {CREATE TABLE test1(f1 int)}
drh6bf89572004-11-03 16:27:01 +0000308 ifcapable {explain} {
309 execsql {EXPLAIN DROP TABLE test1}
310 }
drh28037572000-08-02 13:47:41 +0000311 execsql {SELECT name FROM sqlite_master WHERE type!='meta'}
drh1b6a71f2000-05-29 23:58:11 +0000312} {test1}
313
drh4cfa7932000-06-08 15:10:46 +0000314# Create a table with a goofy name
315#
drh3fc190c2001-09-14 03:24:23 +0000316#do_test table-6.1 {
317# execsql {CREATE TABLE 'Spaces In This Name!'(x int)}
318# execsql {INSERT INTO 'spaces in this name!' VALUES(1)}
319# set list [glob -nocomplain testdb/spaces*.tbl]
320#} {testdb/spaces+in+this+name+.tbl}
drh4cfa7932000-06-08 15:10:46 +0000321
drhc4a3c772001-04-04 11:48:57 +0000322# Try using keywords as table names or column names.
323#
324do_test table-7.1 {
325 set v [catch {execsql {
326 CREATE TABLE weird(
327 desc text,
328 asc text,
drh6bf89572004-11-03 16:27:01 +0000329 key int,
drh17f71932002-02-21 12:01:27 +0000330 [14_vac] boolean,
331 fuzzy_dog_12 varchar(10),
drhd4007282001-04-12 23:21:58 +0000332 begin blob,
333 end clob
drhc4a3c772001-04-04 11:48:57 +0000334 )
335 }} msg]
336 lappend v $msg
337} {0 {}}
338do_test table-7.2 {
339 execsql {
drhd4007282001-04-12 23:21:58 +0000340 INSERT INTO weird VALUES('a','b',9,0,'xyz','hi','y''all');
drhc4a3c772001-04-04 11:48:57 +0000341 SELECT * FROM weird;
342 }
drhd4007282001-04-12 23:21:58 +0000343} {a b 9 0 xyz hi y'all}
drhc4a3c772001-04-04 11:48:57 +0000344do_test table-7.3 {
345 execsql2 {
346 SELECT * FROM weird;
347 }
drh6bf89572004-11-03 16:27:01 +0000348} {desc a asc b key 9 14_vac 0 fuzzy_dog_12 xyz begin hi end y'all}
danielk197778c2e6d2009-01-16 11:04:58 +0000349do_test table-7.3 {
350 execsql {
351 CREATE TABLE savepoint(release);
352 INSERT INTO savepoint(release) VALUES(10);
353 UPDATE savepoint SET release = 5;
354 SELECT release FROM savepoint;
355 }
356} {5}
drhc4a3c772001-04-04 11:48:57 +0000357
drh969fa7c2002-02-18 18:30:32 +0000358# Try out the CREATE TABLE AS syntax
359#
360do_test table-8.1 {
361 execsql2 {
362 CREATE TABLE t2 AS SELECT * FROM weird;
363 SELECT * FROM t2;
364 }
drh6bf89572004-11-03 16:27:01 +0000365} {desc a asc b key 9 14_vac 0 fuzzy_dog_12 xyz begin hi end y'all}
drh17f71932002-02-21 12:01:27 +0000366do_test table-8.1.1 {
367 execsql {
368 SELECT sql FROM sqlite_master WHERE name='t2';
369 }
370} {{CREATE TABLE t2(
drhc4a64fa2009-05-11 20:53:28 +0000371 "desc" TEXT,
372 "asc" TEXT,
373 "key" INT,
374 "14_vac" NUM,
375 fuzzy_dog_12 TEXT,
376 "begin",
377 "end" TEXT
drh17f71932002-02-21 12:01:27 +0000378)}}
drh969fa7c2002-02-18 18:30:32 +0000379do_test table-8.2 {
380 execsql {
drh234c39d2004-07-24 03:30:47 +0000381 CREATE TABLE "t3""xyz"(a,b,c);
382 INSERT INTO [t3"xyz] VALUES(1,2,3);
383 SELECT * FROM [t3"xyz];
drh969fa7c2002-02-18 18:30:32 +0000384 }
385} {1 2 3}
386do_test table-8.3 {
387 execsql2 {
drh234c39d2004-07-24 03:30:47 +0000388 CREATE TABLE [t4"abc] AS SELECT count(*) as cnt, max(b+c) FROM [t3"xyz];
389 SELECT * FROM [t4"abc];
drh969fa7c2002-02-18 18:30:32 +0000390 }
drh17f71932002-02-21 12:01:27 +0000391} {cnt 1 max(b+c) 5}
danielk197700e279d2004-06-21 07:36:32 +0000392
393# Update for v3: The declaration type of anything except a column is now a
394# NULL pointer, so the created table has no column types. (Changed result
drh234c39d2004-07-24 03:30:47 +0000395# from {{CREATE TABLE 't4"abc'(cnt NUMERIC,"max(b+c)" NUMERIC)}}).
drh17f71932002-02-21 12:01:27 +0000396do_test table-8.3.1 {
397 execsql {
drh234c39d2004-07-24 03:30:47 +0000398 SELECT sql FROM sqlite_master WHERE name='t4"abc'
drh17f71932002-02-21 12:01:27 +0000399 }
drh234c39d2004-07-24 03:30:47 +0000400} {{CREATE TABLE "t4""abc"(cnt,"max(b+c)")}}
danielk197753c0f742005-03-29 03:10:59 +0000401
402ifcapable tempdb {
403 do_test table-8.4 {
404 execsql2 {
405 CREATE TEMPORARY TABLE t5 AS SELECT count(*) AS [y'all] FROM [t3"xyz];
406 SELECT * FROM t5;
407 }
408 } {y'all 1}
409}
410
drh969fa7c2002-02-18 18:30:32 +0000411do_test table-8.5 {
412 db close
drhef4ac8f2004-06-19 00:16:31 +0000413 sqlite3 db test.db
drh969fa7c2002-02-18 18:30:32 +0000414 execsql2 {
drh234c39d2004-07-24 03:30:47 +0000415 SELECT * FROM [t4"abc];
drh969fa7c2002-02-18 18:30:32 +0000416 }
drh17f71932002-02-21 12:01:27 +0000417} {cnt 1 max(b+c) 5}
drh969fa7c2002-02-18 18:30:32 +0000418do_test table-8.6 {
419 execsql2 {
420 SELECT * FROM t2;
421 }
drh6bf89572004-11-03 16:27:01 +0000422} {desc a asc b key 9 14_vac 0 fuzzy_dog_12 xyz begin hi end y'all}
drh969fa7c2002-02-18 18:30:32 +0000423do_test table-8.7 {
424 catchsql {
425 SELECT * FROM t5;
426 }
427} {1 {no such table: t5}}
drh17f71932002-02-21 12:01:27 +0000428do_test table-8.8 {
429 catchsql {
430 CREATE TABLE t5 AS SELECT * FROM no_such_table;
431 }
432} {1 {no such table: no_such_table}}
drh969fa7c2002-02-18 18:30:32 +0000433
danielk19771b870de2009-03-14 08:37:23 +0000434do_test table-8.9 {
435 execsql {
436 CREATE TABLE t10("col.1" [char.3]);
437 CREATE TABLE t11 AS SELECT * FROM t10;
438 SELECT sql FROM sqlite_master WHERE name = 't11';
439 }
drhc4a64fa2009-05-11 20:53:28 +0000440} {{CREATE TABLE t11("col.1" TEXT)}}
danielk19771b870de2009-03-14 08:37:23 +0000441do_test table-8.10 {
442 execsql {
443 CREATE TABLE t12(
444 a INTEGER,
445 b VARCHAR(10),
446 c VARCHAR(1,10),
447 d VARCHAR(+1,-10),
448 e VARCHAR (+1,-10),
449 f "VARCHAR (+1,-10, 5)",
450 g BIG INTEGER
451 );
452 CREATE TABLE t13 AS SELECT * FROM t12;
453 SELECT sql FROM sqlite_master WHERE name = 't13';
454 }
455} {{CREATE TABLE t13(
drhc4a64fa2009-05-11 20:53:28 +0000456 a INT,
457 b TEXT,
458 c TEXT,
459 d TEXT,
460 e TEXT,
461 f TEXT,
462 g INT
danielk19771b870de2009-03-14 08:37:23 +0000463)}}
464
drh97fc3d02002-05-22 21:27:03 +0000465# Make sure we cannot have duplicate column names within a table.
466#
467do_test table-9.1 {
468 catchsql {
469 CREATE TABLE t6(a,b,a);
470 }
471} {1 {duplicate column name: a}}
drhd8919672005-09-10 15:35:06 +0000472do_test table-9.2 {
473 catchsql {
474 CREATE TABLE t6(a varchar(100), b blob, a integer);
475 }
476} {1 {duplicate column name: a}}
drh97fc3d02002-05-22 21:27:03 +0000477
drh04738cb2002-06-02 18:19:00 +0000478# Check the foreign key syntax.
479#
drh6bf89572004-11-03 16:27:01 +0000480ifcapable {foreignkey} {
drh04738cb2002-06-02 18:19:00 +0000481do_test table-10.1 {
482 catchsql {
483 CREATE TABLE t6(a REFERENCES t4(a) NOT NULL);
484 INSERT INTO t6 VALUES(NULL);
485 }
drhf9c8ce32013-11-05 13:33:55 +0000486} {1 {NOT NULL constraint failed: t6.a}}
drh04738cb2002-06-02 18:19:00 +0000487do_test table-10.2 {
488 catchsql {
489 DROP TABLE t6;
490 CREATE TABLE t6(a REFERENCES t4(a) MATCH PARTIAL);
491 }
492} {0 {}}
493do_test table-10.3 {
494 catchsql {
495 DROP TABLE t6;
496 CREATE TABLE t6(a REFERENCES t4 MATCH FULL ON DELETE SET NULL NOT NULL);
497 }
498} {0 {}}
499do_test table-10.4 {
500 catchsql {
501 DROP TABLE t6;
502 CREATE TABLE t6(a REFERENCES t4 MATCH FULL ON UPDATE SET DEFAULT DEFAULT 1);
503 }
504} {0 {}}
505do_test table-10.5 {
506 catchsql {
507 DROP TABLE t6;
508 CREATE TABLE t6(a NOT NULL NOT DEFERRABLE INITIALLY IMMEDIATE);
509 }
510} {0 {}}
511do_test table-10.6 {
512 catchsql {
513 DROP TABLE t6;
514 CREATE TABLE t6(a NOT NULL DEFERRABLE INITIALLY DEFERRED);
515 }
516} {0 {}}
517do_test table-10.7 {
518 catchsql {
519 DROP TABLE t6;
520 CREATE TABLE t6(a,
521 FOREIGN KEY (a) REFERENCES t4(b) DEFERRABLE INITIALLY DEFERRED
522 );
523 }
524} {0 {}}
525do_test table-10.8 {
526 catchsql {
527 DROP TABLE t6;
528 CREATE TABLE t6(a,b,c,
529 FOREIGN KEY (b,c) REFERENCES t4(x,y) MATCH PARTIAL
530 ON UPDATE SET NULL ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
531 );
532 }
533} {0 {}}
drhc2eef3b2002-08-31 18:53:06 +0000534do_test table-10.9 {
535 catchsql {
536 DROP TABLE t6;
537 CREATE TABLE t6(a,b,c,
538 FOREIGN KEY (b,c) REFERENCES t4(x)
539 );
540 }
541} {1 {number of columns in foreign key does not match the number of columns in the referenced table}}
542do_test table-10.10 {
543 catchsql {DROP TABLE t6}
544 catchsql {
545 CREATE TABLE t6(a,b,c,
546 FOREIGN KEY (b,c) REFERENCES t4(x,y,z)
547 );
548 }
549} {1 {number of columns in foreign key does not match the number of columns in the referenced table}}
550do_test table-10.11 {
551 catchsql {DROP TABLE t6}
552 catchsql {
553 CREATE TABLE t6(a,b, c REFERENCES t4(x,y));
554 }
555} {1 {foreign key on c should reference only one column of table t4}}
556do_test table-10.12 {
557 catchsql {DROP TABLE t6}
558 catchsql {
559 CREATE TABLE t6(a,b,c,
560 FOREIGN KEY (b,x) REFERENCES t4(x,y)
561 );
562 }
563} {1 {unknown column "x" in foreign key definition}}
564do_test table-10.13 {
565 catchsql {DROP TABLE t6}
566 catchsql {
567 CREATE TABLE t6(a,b,c,
568 FOREIGN KEY (x,b) REFERENCES t4(x,y)
569 );
570 }
571} {1 {unknown column "x" in foreign key definition}}
drh6bf89572004-11-03 16:27:01 +0000572} ;# endif foreignkey
drh04738cb2002-06-02 18:19:00 +0000573
danielk197735bb9d02004-05-24 12:55:54 +0000574# Test for the "typeof" function. More tests for the
575# typeof() function are found in bind.test and types.test.
drh38640e12002-07-05 21:42:36 +0000576#
577do_test table-11.1 {
578 execsql {
579 CREATE TABLE t7(
580 a integer primary key,
581 b number(5,10),
582 c character varying (8),
583 d VARCHAR(9),
584 e clob,
585 f BLOB,
586 g Text,
587 h
588 );
589 INSERT INTO t7(a) VALUES(1);
590 SELECT typeof(a), typeof(b), typeof(c), typeof(d),
591 typeof(e), typeof(f), typeof(g), typeof(h)
592 FROM t7 LIMIT 1;
593 }
danielk197735bb9d02004-05-24 12:55:54 +0000594} {integer null null null null null null null}
drh38640e12002-07-05 21:42:36 +0000595do_test table-11.2 {
596 execsql {
597 SELECT typeof(a+b), typeof(a||b), typeof(c+d), typeof(c||d)
598 FROM t7 LIMIT 1;
599 }
danielk197735bb9d02004-05-24 12:55:54 +0000600} {null null null null}
drh97fc3d02002-05-22 21:27:03 +0000601
danielk197700e279d2004-06-21 07:36:32 +0000602# Test that when creating a table using CREATE TABLE AS, column types are
603# assigned correctly for (SELECT ...) and 'x AS y' expressions.
604do_test table-12.1 {
danielk19773e8c37e2005-01-21 03:12:14 +0000605 ifcapable subquery {
606 execsql {
607 CREATE TABLE t8 AS SELECT b, h, a as i, (SELECT f FROM t7) as j FROM t7;
608 }
609 } else {
610 execsql {
611 CREATE TABLE t8 AS SELECT b, h, a as i, f as j FROM t7;
612 }
danielk197700e279d2004-06-21 07:36:32 +0000613 }
614} {}
615do_test table-12.2 {
616 execsql {
617 SELECT sql FROM sqlite_master WHERE tbl_name = 't8'
618 }
drhc4a64fa2009-05-11 20:53:28 +0000619} {{CREATE TABLE t8(b NUM,h,i INT,j)}}
danielk197700e279d2004-06-21 07:36:32 +0000620
danielk1977e6efa742004-11-10 11:55:10 +0000621#--------------------------------------------------------------------
622# Test cases table-13.*
623#
danielk19777977a172004-11-09 12:44:37 +0000624# Test the ability to have default values of CURRENT_TIME, CURRENT_DATE
625# and CURRENT_TIMESTAMP.
626#
627do_test table-13.1 {
628 execsql {
629 CREATE TABLE tablet8(
630 a integer primary key,
631 tm text DEFAULT CURRENT_TIME,
632 dt text DEFAULT CURRENT_DATE,
633 dttm text DEFAULT CURRENT_TIMESTAMP
634 );
635 SELECT * FROM tablet8;
636 }
637} {}
638set i 0
drhd0b26772010-05-03 19:20:46 +0000639unset -nocomplain date time seconds
drh9645d8d2006-09-01 15:49:05 +0000640foreach {date time seconds} {
641 1976-07-04 12:00:00 205329600
642 1994-04-16 14:00:00 766504800
643 2000-01-01 00:00:00 946684800
644 2003-12-31 12:34:56 1072874096
danielk19777977a172004-11-09 12:44:37 +0000645} {
646 incr i
drh9645d8d2006-09-01 15:49:05 +0000647 set sqlite_current_time $seconds
danielk19777977a172004-11-09 12:44:37 +0000648 do_test table-13.2.$i {
649 execsql "
650 INSERT INTO tablet8(a) VALUES($i);
651 SELECT tm, dt, dttm FROM tablet8 WHERE a=$i;
652 "
653 } [list $time $date [list $date $time]]
654}
655set sqlite_current_time 0
656
danielk1977e6efa742004-11-10 11:55:10 +0000657#--------------------------------------------------------------------
658# Test cases table-14.*
659#
660# Test that a table cannot be created or dropped while other virtual
661# machines are active. This is required because otherwise when in
662# auto-vacuum mode the btree-layer may need to move the root-pages of
663# a table for which there is an open cursor.
664#
drhb7af4452007-05-02 17:54:55 +0000665# 2007-05-02: A open btree cursor no longer blocks CREATE TABLE.
666# But DROP TABLE is still prohibited because we do not want to
667# delete a table out from under a running query.
668#
danielk1977e6efa742004-11-10 11:55:10 +0000669
danielk1977a21c6b62005-01-24 10:25:59 +0000670# db eval {
671# pragma vdbe_trace = 0;
672# }
danielk1977e6efa742004-11-10 11:55:10 +0000673# Try to create a table from within a callback:
drh251b0672004-11-23 22:16:39 +0000674unset -nocomplain result
danielk1977e6efa742004-11-10 11:55:10 +0000675do_test table-14.1 {
676 set rc [
677 catch {
678 db eval {SELECT * FROM tablet8 LIMIT 1} {} {
679 db eval {CREATE TABLE t9(a, b, c)}
680 }
681 } msg
682 ]
683 set result [list $rc $msg]
drhb7af4452007-05-02 17:54:55 +0000684} {0 {}}
danielk1977e6efa742004-11-10 11:55:10 +0000685
686# Try to drop a table from within a callback:
danielk19775a8f9372007-10-09 08:29:32 +0000687do_test table-14.2 {
danielk1977e6efa742004-11-10 11:55:10 +0000688 set rc [
689 catch {
690 db eval {SELECT * FROM tablet8 LIMIT 1} {} {
691 db eval {DROP TABLE t9;}
692 }
693 } msg
694 ]
695 set result [list $rc $msg]
696} {1 {database table is locked}}
697
danielk19775a8f9372007-10-09 08:29:32 +0000698ifcapable attach {
699 # Now attach a database and ensure that a table can be created in the
700 # attached database whilst in a callback from a query on the main database.
701 do_test table-14.3 {
mistachkinfda06be2011-08-02 00:57:34 +0000702 forcedelete test2.db
703 forcedelete test2.db-journal
danielk19775a8f9372007-10-09 08:29:32 +0000704 execsql {
705 ATTACH 'test2.db' as aux;
706 }
707 db eval {SELECT * FROM tablet8 LIMIT 1} {} {
708 db eval {CREATE TABLE aux.t1(a, b, c)}
709 }
710 } {}
711
712 # On the other hand, it should be impossible to drop a table when any VMs
713 # are active. This is because VerifyCookie instructions may have already
714 # been executed, and btree root-pages may not move after this (which a
715 # delete table might do).
716 do_test table-14.4 {
717 set rc [
718 catch {
719 db eval {SELECT * FROM tablet8 LIMIT 1} {} {
720 db eval {DROP TABLE aux.t1;}
721 }
722 } msg
723 ]
724 set result [list $rc $msg]
725 } {1 {database table is locked}}
726}
danielk1977e6efa742004-11-10 11:55:10 +0000727
danielk1977ae825582004-11-23 09:06:55 +0000728# Create and drop 2000 tables. This is to check that the balance_shallow()
729# routine works correctly on the sqlite_master table. At one point it
730# contained a bug that would prevent the right-child pointer of the
731# child page from being copied to the root page.
732#
733do_test table-15.1 {
734 execsql {BEGIN}
735 for {set i 0} {$i<2000} {incr i} {
736 execsql "CREATE TABLE tbl$i (a, b, c)"
737 }
738 execsql {COMMIT}
739} {}
740do_test table-15.2 {
741 execsql {BEGIN}
742 for {set i 0} {$i<2000} {incr i} {
743 execsql "DROP TABLE tbl$i"
744 }
745 execsql {COMMIT}
746} {}
747
drh1cfc9aa2014-08-05 21:31:08 +0000748# Ticket 3a88d85f36704eebe134f7f48aebf00cd6438c1a (2014-08-05)
749# The following SQL script segfaults while running the INSERT statement:
750#
751# CREATE TABLE t1(x DEFAULT(max(1)));
752# INSERT INTO t1(rowid) VALUES(1);
753#
754# The problem appears to be the use of an aggregate function as part of
755# the default value for a column. This problem has been in the code since
756# at least 2006-01-01 and probably before that. This problem was detected
757# and reported on the sqlite-users@sqlite.org mailing list by Zsbán Ambrus.
758#
759do_execsql_test table-16.1 {
760 CREATE TABLE t16(x DEFAULT(max(1)));
761 INSERT INTO t16(x) VALUES(123);
762 SELECT rowid, x FROM t16;
763} {1 123}
764do_catchsql_test table-16.2 {
765 INSERT INTO t16(rowid) VALUES(4);
drh0c4de2d2014-08-06 00:29:06 +0000766} {1 {unknown function: max()}}
drh1cfc9aa2014-08-05 21:31:08 +0000767do_execsql_test table-16.3 {
768 DROP TABLE t16;
769 CREATE TABLE t16(x DEFAULT(abs(1)));
770 INSERT INTO t16(rowid) VALUES(4);
771 SELECT rowid, x FROM t16;
772} {4 1}
773do_catchsql_test table-16.4 {
774 DROP TABLE t16;
775 CREATE TABLE t16(x DEFAULT(avg(1)));
776 INSERT INTO t16(rowid) VALUES(123);
777 SELECT rowid, x FROM t16;
drh0c4de2d2014-08-06 00:29:06 +0000778} {1 {unknown function: avg()}}
drh1cfc9aa2014-08-05 21:31:08 +0000779do_catchsql_test table-16.5 {
780 DROP TABLE t16;
781 CREATE TABLE t16(x DEFAULT(count()));
782 INSERT INTO t16(rowid) VALUES(123);
783 SELECT rowid, x FROM t16;
drh0c4de2d2014-08-06 00:29:06 +0000784} {1 {unknown function: count()}}
drh1cfc9aa2014-08-05 21:31:08 +0000785do_catchsql_test table-16.6 {
786 DROP TABLE t16;
787 CREATE TABLE t16(x DEFAULT(group_concat('x',',')));
788 INSERT INTO t16(rowid) VALUES(123);
789 SELECT rowid, x FROM t16;
drh0c4de2d2014-08-06 00:29:06 +0000790} {1 {unknown function: group_concat()}}
791do_catchsql_test table-16.7 {
792 INSERT INTO t16 DEFAULT VALUES;
793} {1 {unknown function: group_concat()}}
drh1cfc9aa2014-08-05 21:31:08 +0000794
drh6b5631e2014-11-05 15:57:39 +0000795# Ticket [https://www.sqlite.org/src/info/094d39a4c95ee4abbc417f04214617675ba15c63]
796# describes a assertion fault that occurs on a CREATE TABLE .. AS SELECT statement.
797# the following test verifies that the problem has been fixed.
798#
799do_execsql_test table-17.1 {
800 DROP TABLE IF EXISTS t1;
801 CREATE TABLE t1(a TEXT);
802 INSERT INTO t1(a) VALUES(1),(2);
803 DROP TABLE IF EXISTS t2;
804 CREATE TABLE t2(x TEXT, y TEXT);
805 INSERT INTO t2(x,y) VALUES(3,4);
806 DROP TABLE IF EXISTS t3;
807 CREATE TABLE t3 AS
808 SELECT a AS p, coalesce(y,a) AS q FROM t1 LEFT JOIN t2 ON a=x;
809 SELECT p, q, '|' FROM t3 ORDER BY p;
810} {1 1 | 2 2 |}
811
drh0dd5cda2015-06-16 16:39:01 +0000812# 2015-06-16
813# Ticket [https://www.sqlite.org/src/tktview/873cae2b6e25b1991ce5e9b782f9cd0409b96063]
814# Make sure a CREATE TABLE AS statement correctly rolls back partial changes to the
815# sqlite_master table when the SELECT on the right-hand side aborts.
816#
817do_catchsql_test table-18.1 {
818 DROP TABLE IF EXISTS t1;
819 BEGIN;
820 CREATE TABLE t1 AS SELECT zeroblob(2e20);
821} {1 {string or blob too big}}
822do_execsql_test table-18.2 {
823 COMMIT;
824 PRAGMA integrity_check;
825} {ok}
826
drh3c03afd2015-09-09 13:28:06 +0000827# 2015-09-09
828# Ticket [https://www.sqlite.org/src/info/acd12990885d9276]
829# "CREATE TABLE ... AS SELECT ... FROM sqlite_master" fails because the row
830# in the sqlite_master table for the next table is initially populated
831# with a NULL instead of a record created by OP_Record.
832#
833do_execsql_test table-19.1 {
834 CREATE TABLE t19 AS SELECT * FROM sqlite_master;
835 SELECT name FROM t19 ORDER BY name;
836} {{} savepoint t10 t11 t12 t13 t16 t2 t3 t3\"xyz t4\"abc t7 t8 t9 tablet8 test1 weird}
837
838
drh0dd5cda2015-06-16 16:39:01 +0000839
drh348784e2000-05-29 20:41:49 +0000840finish_test