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 CREATE TABLE statement.
17 # $Id: table.test,v 1.22 2003/01/29 18:46:54 drh Exp $
19 set testdir [file dirname $argv0]
20 source $testdir/tester.tcl
22 # Create a basic table and verify it is added to sqlite_master
32 SELECT sql FROM sqlite_master WHERE type!='meta'
34 } {{CREATE TABLE test1 (
40 # Verify the other fields of the sqlite_master file.
43 execsql {SELECT name, tbl_name, type FROM sqlite_master WHERE type!='meta'}
46 # Close and reopen the database. Verify that everything is
52 execsql {SELECT name, tbl_name, type from sqlite_master WHERE type!='meta'}
55 # Drop the database and make sure it disappears.
58 execsql {DROP TABLE test1}
59 execsql {SELECT * FROM sqlite_master WHERE type!='meta'}
62 # Close and reopen the database. Verify that the table is
68 execsql {SELECT name FROM sqlite_master WHERE type!='meta'}
71 # Repeat the above steps, but this time quote the table name.
74 execsql {CREATE TABLE "create" (f1 int)}
75 execsql {SELECT name FROM sqlite_master WHERE type!='meta'}
78 execsql {DROP TABLE "create"}
79 execsql {SELECT name FROM "sqlite_master" WHERE type!='meta'}
82 execsql {CREATE TABLE test1("f1 ho" int)}
83 execsql {SELECT name as "X" FROM sqlite_master WHERE type!='meta'}
86 execsql {DROP TABLE "TEST1"}
87 execsql {SELECT name FROM "sqlite_master" WHERE type!='meta'}
92 # Verify that we cannot make two tables with the same name
95 execsql {CREATE TABLE TEST2(one text)}
96 set v [catch {execsql {CREATE TABLE test2(two text)}} msg]
98 } {1 {table test2 already exists}}
100 set v [catch {execsql {CREATE TABLE sqlite_master(two text)}} msg]
102 } {1 {table sqlite_master already exists}}
106 set v [catch {execsql {CREATE TABLE sqlite_master(two text)}} msg]
108 } {1 {table sqlite_master already exists}}
110 execsql {DROP TABLE test2; SELECT name FROM sqlite_master WHERE type!='meta'}
113 # Verify that we cannot make a table with the same name as an index
116 execsql {CREATE TABLE test2(one text); CREATE INDEX test3 ON test2(one)}
117 set v [catch {execsql {CREATE TABLE test3(two text)}} msg]
119 } {1 {there is already an index named test3}}
123 set v [catch {execsql {CREATE TABLE test3(two text)}} msg]
125 } {1 {there is already an index named test3}}
127 execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
130 execsql {DROP INDEX test3}
131 set v [catch {execsql {CREATE TABLE test3(two text)}} msg]
135 execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
138 execsql {DROP TABLE test2; DROP TABLE test3}
139 execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
142 # Create a table with many field names
148 f3 varchar(30) primary key,
169 execsql {SELECT sql FROM sqlite_master WHERE type=='table'}
172 set v [catch {execsql {CREATE TABLE BIG(xyz foo)}} msg]
174 } {1 {table BIG already exists}}
176 set v [catch {execsql {CREATE TABLE biG(xyz foo)}} msg]
178 } {1 {table biG already exists}}
180 set v [catch {execsql {CREATE TABLE bIg(xyz foo)}} msg]
182 } {1 {table bIg already exists}}
186 set v [catch {execsql {CREATE TABLE Big(xyz foo)}} msg]
188 } {1 {table Big already exists}}
190 execsql {DROP TABLE big}
191 execsql {SELECT name FROM sqlite_master WHERE type!='meta'}
194 # Try creating large numbers of tables
197 for {set i 1} {$i<=100} {incr i} {
198 lappend r [format test%03d $i]
201 for {set i 1} {$i<=100} {incr i} {
202 set sql "CREATE TABLE [format test%03d $i] ("
203 for {set k 1} {$k<$i} {incr k} {
204 append sql "field$k text,"
206 append sql "last_field text)"
209 execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
214 execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
217 # Drop the even numbered tables
220 for {set i 1} {$i<=100} {incr i 2} {
221 lappend r [format test%03d $i]
224 for {set i 2} {$i<=100} {incr i 2} {
225 # if {$i==38} {execsql {pragma vdbe_trace=on}}
226 set sql "DROP TABLE [format TEST%03d $i]"
229 execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
233 # Drop the odd number tables
236 for {set i 1} {$i<=100} {incr i 2} {
237 set sql "DROP TABLE [format test%03d $i]"
240 execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
243 # Try to drop a table that does not exist
246 set v [catch {execsql {DROP TABLE test009}} msg]
248 } {1 {no such table: test009}}
250 # Try to drop sqlite_master
253 set v [catch {execsql {DROP TABLE sqlite_master}} msg]
255 } {1 {table sqlite_master may not be dropped}}
257 # Make sure an EXPLAIN does not really create a new table
260 execsql {EXPLAIN CREATE TABLE test1(f1 int)}
261 execsql {SELECT name FROM sqlite_master WHERE type!='meta'}
264 # Make sure an EXPLAIN does not really drop an existing table
267 execsql {CREATE TABLE test1(f1 int)}
268 execsql {EXPLAIN DROP TABLE test1}
269 execsql {SELECT name FROM sqlite_master WHERE type!='meta'}
272 # Create a table with a goofy name
275 # execsql {CREATE TABLE 'Spaces In This Name!'(x int)}
276 # execsql {INSERT INTO 'spaces in this name!' VALUES(1)}
277 # set list [glob -nocomplain testdb/spaces*.tbl]
278 #} {testdb/spaces+in+this+name+.tbl}
280 # Try using keywords as table names or column names.
283 set v [catch {execsql {
289 fuzzy_dog_12 varchar(10),
298 INSERT INTO weird VALUES('a','b',9,0,'xyz','hi','y''all');
301 } {a b 9 0 xyz hi y'all}
306 } {desc a asc b explain 9 14_vac 0 fuzzy_dog_12 xyz begin hi end y'all}
308 # Try out the CREATE TABLE AS syntax
312 CREATE TABLE t2 AS SELECT * FROM weird;
315 } {desc a asc b explain 9 14_vac 0 fuzzy_dog_12 xyz begin hi end y'all}
316 do_test table-8.1.1 {
318 SELECT sql FROM sqlite_master WHERE name='t2';
331 CREATE TABLE 't3''xyz'(a,b,c);
332 INSERT INTO [t3'xyz] VALUES(1,2,3);
333 SELECT * FROM [t3'xyz];
338 CREATE TABLE [t4'abc] AS SELECT count(*) as cnt, max(b+c) FROM [t3'xyz];
339 SELECT * FROM [t4'abc];
342 do_test table-8.3.1 {
344 SELECT sql FROM sqlite_master WHERE name='t4''abc'
346 } {{CREATE TABLE 't4''abc'(cnt,'max(b+c)')}}
349 CREATE TEMPORARY TABLE t5 AS SELECT count(*) AS [y'all] FROM [t3'xyz];
357 SELECT * FROM [t4'abc];
364 } {desc a asc b explain 9 14_vac 0 fuzzy_dog_12 xyz begin hi end y'all}
369 } {1 {no such table: t5}}
372 CREATE TABLE t5 AS SELECT * FROM no_such_table;
374 } {1 {no such table: no_such_table}}
376 # Make sure we cannot have duplicate column names within a table.
380 CREATE TABLE t6(a,b,a);
382 } {1 {duplicate column name: a}}
384 # Check the foreign key syntax.
388 CREATE TABLE t6(a REFERENCES t4(a) NOT NULL);
389 INSERT INTO t6 VALUES(NULL);
391 } {1 {t6.a may not be NULL}}
395 CREATE TABLE t6(a REFERENCES t4(a) MATCH PARTIAL);
401 CREATE TABLE t6(a REFERENCES t4 MATCH FULL ON DELETE SET NULL NOT NULL);
407 CREATE TABLE t6(a REFERENCES t4 MATCH FULL ON UPDATE SET DEFAULT DEFAULT 1);
413 CREATE TABLE t6(a NOT NULL NOT DEFERRABLE INITIALLY IMMEDIATE);
419 CREATE TABLE t6(a NOT NULL DEFERRABLE INITIALLY DEFERRED);
426 FOREIGN KEY (a) REFERENCES t4(b) DEFERRABLE INITIALLY DEFERRED
433 CREATE TABLE t6(a,b,c,
434 FOREIGN KEY (b,c) REFERENCES t4(x,y) MATCH PARTIAL
435 ON UPDATE SET NULL ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
442 CREATE TABLE t6(a,b,c,
443 FOREIGN KEY (b,c) REFERENCES t4(x)
446 } {1 {number of columns in foreign key does not match the number of columns in the referenced table}}
447 do_test table-10.10 {
448 catchsql {DROP TABLE t6}
450 CREATE TABLE t6(a,b,c,
451 FOREIGN KEY (b,c) REFERENCES t4(x,y,z)
454 } {1 {number of columns in foreign key does not match the number of columns in the referenced table}}
455 do_test table-10.11 {
456 catchsql {DROP TABLE t6}
458 CREATE TABLE t6(a,b, c REFERENCES t4(x,y));
460 } {1 {foreign key on c should reference only one column of table t4}}
461 do_test table-10.12 {
462 catchsql {DROP TABLE t6}
464 CREATE TABLE t6(a,b,c,
465 FOREIGN KEY (b,x) REFERENCES t4(x,y)
468 } {1 {unknown column "x" in foreign key definition}}
469 do_test table-10.13 {
470 catchsql {DROP TABLE t6}
472 CREATE TABLE t6(a,b,c,
473 FOREIGN KEY (x,b) REFERENCES t4(x,y)
476 } {1 {unknown column "x" in foreign key definition}}
479 # Test for the "typeof" function.
484 a integer primary key,
486 c character varying (8),
493 INSERT INTO t7(a) VALUES(1);
494 SELECT typeof(a), typeof(b), typeof(c), typeof(d),
495 typeof(e), typeof(f), typeof(g), typeof(h)
498 } {numeric numeric text text text text text numeric}
501 SELECT typeof(a+b), typeof(a||b), typeof(c+d), typeof(c||d)
504 } {numeric text numeric text}