3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
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.
10 #***********************************************************************
11 # This file implements regression tests for SQLite library. The
12 # focus of this file is testing the CREATE TABLE statement.
15 set testdir [file dirname $argv0]
16 source $testdir/tester.tcl
18 # Create a basic table and verify it is added to sqlite_master
28 SELECT sql FROM sqlite_master WHERE type!='meta'
30 } {{CREATE TABLE test1 (
36 # Verify the other fields of the sqlite_master file.
39 execsql {SELECT name, tbl_name, type FROM sqlite_master WHERE type!='meta'}
42 # Close and reopen the database. Verify that everything is
48 execsql {SELECT name, tbl_name, type from sqlite_master WHERE type!='meta'}
51 # Drop the database and make sure it disappears.
54 execsql {DROP TABLE test1}
55 execsql {SELECT * FROM sqlite_master WHERE type!='meta'}
58 # Close and reopen the database. Verify that the table is
64 execsql {SELECT name FROM sqlite_master WHERE type!='meta'}
67 # Repeat the above steps, but this time quote the table name.
70 execsql {CREATE TABLE "create" (f1 int)}
71 execsql {SELECT name FROM sqlite_master WHERE type!='meta'}
74 execsql {DROP TABLE "create"}
75 execsql {SELECT name FROM "sqlite_master" WHERE type!='meta'}
78 execsql {CREATE TABLE test1("f1 ho" int)}
79 execsql {SELECT name as "X" FROM sqlite_master WHERE type!='meta'}
82 execsql {DROP TABLE "TEST1"}
83 execsql {SELECT name FROM "sqlite_master" WHERE type!='meta'}
88 # Verify that we cannot make two tables with the same name
91 execsql {CREATE TABLE TEST2(one text)}
92 catchsql {CREATE TABLE test2(two text default 'hi')}
93 } {1 {table test2 already exists}}
95 catchsql {CREATE TABLE "test2" (two)}
96 } {1 {table "test2" already exists}}
98 set v [catch {execsql {CREATE TABLE sqlite_master(two text)}} msg]
100 } {1 {object name reserved for internal use: sqlite_master}}
104 set v [catch {execsql {CREATE TABLE sqlite_master(two text)}} msg]
106 } {1 {object name reserved for internal use: sqlite_master}}
108 catchsql {CREATE TABLE IF NOT EXISTS test2(x,y)}
111 catchsql {CREATE TABLE IF NOT EXISTS test2(x UNIQUE, y TEXT PRIMARY KEY)}
114 execsql {DROP TABLE test2; SELECT name FROM sqlite_master WHERE type!='meta'}
117 # Verify that we cannot make a table with the same name as an index
120 execsql {CREATE TABLE test2(one text)}
121 execsql {CREATE INDEX test3 ON test2(one)}
122 catchsql {CREATE TABLE test3(two text)}
123 } {1 {there is already an index named test3}}
127 set v [catch {execsql {CREATE TABLE test3(two text)}} msg]
129 } {1 {there is already an index named test3}}
131 execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
134 execsql {DROP INDEX test3}
135 set v [catch {execsql {CREATE TABLE test3(two text)}} msg]
139 execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
142 execsql {DROP TABLE test2; DROP TABLE test3}
143 execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
146 # Create a table with many field names
152 f3 varchar(30) primary key,
173 execsql {SELECT sql FROM sqlite_master WHERE type=='table'}
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}}
184 set v [catch {execsql {CREATE TABLE bIg(xyz foo)}} msg]
186 } {1 {table bIg already exists}}
190 set v [catch {execsql {CREATE TABLE Big(xyz foo)}} msg]
192 } {1 {table Big already exists}}
194 execsql {DROP TABLE big}
195 execsql {SELECT name FROM sqlite_master WHERE type!='meta'}
198 # Try creating large numbers of tables
201 for {set i 1} {$i<=100} {incr i} {
202 lappend r [format test%03d $i]
205 for {set i 1} {$i<=100} {incr i} {
206 set sql "CREATE TABLE [format test%03d $i] ("
207 for {set k 1} {$k<$i} {incr k} {
208 append sql "field$k text,"
210 append sql "last_field text)"
213 execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
218 execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
221 # Drop the even numbered tables
224 for {set i 1} {$i<=100} {incr i 2} {
225 lappend r [format test%03d $i]
228 for {set i 2} {$i<=100} {incr i 2} {
229 # if {$i==38} {execsql {pragma vdbe_trace=on}}
230 set sql "DROP TABLE [format TEST%03d $i]"
233 execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
237 # Drop the odd number tables
240 for {set i 1} {$i<=100} {incr i 2} {
241 set sql "DROP TABLE [format test%03d $i]"
244 execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
247 # Try to drop a table that does not exist
249 do_test table-5.1.1 {
250 catchsql {DROP TABLE test009}
251 } {1 {no such table: test009}}
252 do_test table-5.1.2 {
253 catchsql {DROP TABLE IF EXISTS test009}
256 # Try to drop sqlite_master
259 catchsql {DROP TABLE IF EXISTS sqlite_master}
260 } {1 {table sqlite_master may not be dropped}}
262 # Dropping sqlite_statN tables is OK.
264 do_test table-5.2.1 {
267 DROP TABLE IF EXISTS sqlite_stat1;
268 DROP TABLE IF EXISTS sqlite_stat2;
269 DROP TABLE IF EXISTS sqlite_stat3;
270 DROP TABLE IF EXISTS sqlite_stat4;
271 SELECT name FROM sqlite_master WHERE name GLOB 'sqlite_stat*';
275 do_test table-5.2.2 {
279 sqlite3_db_config db DEFENSIVE 0
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';
288 DROP TABLE IF EXISTS t99;
295 # Make sure an EXPLAIN does not really create a new table
298 ifcapable {explain} {
299 execsql {EXPLAIN CREATE TABLE test1(f1 int)}
301 execsql {SELECT name FROM sqlite_master WHERE type!='meta'}
304 # Make sure an EXPLAIN does not really drop an existing table
307 execsql {CREATE TABLE test1(f1 int)}
308 ifcapable {explain} {
309 execsql {EXPLAIN DROP TABLE test1}
311 execsql {SELECT name FROM sqlite_master WHERE type!='meta'}
314 # Create a table with a goofy name
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}
322 # Try using keywords as table names or column names.
325 set v [catch {execsql {
331 fuzzy_dog_12 varchar(10),
340 INSERT INTO weird VALUES('a','b',9,0,'xyz','hi','y''all');
343 } {a b 9 0 xyz hi y'all}
348 } {desc a asc b key 9 14_vac 0 fuzzy_dog_12 xyz begin hi end y'all}
351 CREATE TABLE savepoint(release);
352 INSERT INTO savepoint(release) VALUES(10);
353 UPDATE savepoint SET release = 5;
354 SELECT release FROM savepoint;
358 # Try out the CREATE TABLE AS syntax
362 CREATE TABLE t2 AS SELECT * FROM weird;
365 } {desc a asc b key 9 14_vac 0 fuzzy_dog_12 xyz begin hi end y'all}
366 do_test table-8.1.1 {
368 SELECT sql FROM sqlite_master WHERE name='t2';
381 CREATE TABLE "t3""xyz"(a,b,c);
382 INSERT INTO [t3"xyz] VALUES(1,2,3);
383 SELECT * FROM [t3"xyz];
388 CREATE TABLE [t4"abc] AS SELECT count(*) as cnt, max(b+c) FROM [t3"xyz];
389 SELECT * FROM [t4"abc];
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
395 # from {{CREATE TABLE 't4"abc'(cnt NUMERIC,"max(b+c)" NUMERIC)}}).
396 do_test table-8.3.1 {
398 SELECT sql FROM sqlite_master WHERE name='t4"abc'
400 } {{CREATE TABLE "t4""abc"(cnt,"max(b+c)")}}
405 CREATE TEMPORARY TABLE t5 AS SELECT count(*) AS [y'all] FROM [t3"xyz];
415 SELECT * FROM [t4"abc];
422 } {desc a asc b key 9 14_vac 0 fuzzy_dog_12 xyz begin hi end y'all}
427 } {1 {no such table: t5}}
430 CREATE TABLE t5 AS SELECT * FROM no_such_table;
432 } {1 {no such table: no_such_table}}
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';
440 } {{CREATE TABLE t11("col.1" TEXT)}}
449 f "VARCHAR (+1,-10, 5)",
452 CREATE TABLE t13 AS SELECT * FROM t12;
453 SELECT sql FROM sqlite_master WHERE name = 't13';
455 } {{CREATE TABLE t13(
465 # Make sure we cannot have duplicate column names within a table.
469 CREATE TABLE t6(a,b,a);
471 } {1 {duplicate column name: a}}
474 CREATE TABLE t6(a varchar(100), b blob, a integer);
476 } {1 {duplicate column name: a}}
478 # Check the foreign key syntax.
480 ifcapable {foreignkey} {
483 CREATE TABLE t6(a REFERENCES t4(a) NOT NULL);
484 INSERT INTO t6 VALUES(NULL);
486 } {1 {NOT NULL constraint failed: t6.a}}
490 CREATE TABLE t6(a REFERENCES t4(a) MATCH PARTIAL);
496 CREATE TABLE t6(a REFERENCES t4 MATCH FULL ON DELETE SET NULL NOT NULL);
502 CREATE TABLE t6(a REFERENCES t4 MATCH FULL ON UPDATE SET DEFAULT DEFAULT 1);
508 CREATE TABLE t6(a NOT NULL NOT DEFERRABLE INITIALLY IMMEDIATE);
514 CREATE TABLE t6(a NOT NULL DEFERRABLE INITIALLY DEFERRED);
521 FOREIGN KEY (a) REFERENCES t4(b) DEFERRABLE INITIALLY DEFERRED
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
537 CREATE TABLE t6(a,b,c,
538 FOREIGN KEY (b,c) REFERENCES t4(x)
541 } {1 {number of columns in foreign key does not match the number of columns in the referenced table}}
542 do_test table-10.10 {
543 catchsql {DROP TABLE t6}
545 CREATE TABLE t6(a,b,c,
546 FOREIGN KEY (b,c) REFERENCES t4(x,y,z)
549 } {1 {number of columns in foreign key does not match the number of columns in the referenced table}}
550 do_test table-10.11 {
551 catchsql {DROP TABLE t6}
553 CREATE TABLE t6(a,b, c REFERENCES t4(x,y));
555 } {1 {foreign key on c should reference only one column of table t4}}
556 do_test table-10.12 {
557 catchsql {DROP TABLE t6}
559 CREATE TABLE t6(a,b,c,
560 FOREIGN KEY (b,x) REFERENCES t4(x,y)
563 } {1 {unknown column "x" in foreign key definition}}
564 do_test table-10.13 {
565 catchsql {DROP TABLE t6}
567 CREATE TABLE t6(a,b,c,
568 FOREIGN KEY (x,b) REFERENCES t4(x,y)
571 } {1 {unknown column "x" in foreign key definition}}
572 } ;# endif foreignkey
574 # Test for the "typeof" function. More tests for the
575 # typeof() function are found in bind.test and types.test.
580 a integer primary key,
582 c character varying (8),
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)
594 } {integer null null null null null null null}
597 SELECT typeof(a+b), typeof(a||b), typeof(c+d), typeof(c||d)
600 } {null null null null}
602 # Test that when creating a table using CREATE TABLE AS, column types are
603 # assigned correctly for (SELECT ...) and 'x AS y' expressions.
607 CREATE TABLE t8 AS SELECT b, h, a as i, (SELECT f FROM t7) as j FROM t7;
611 CREATE TABLE t8 AS SELECT b, h, a as i, f as j FROM t7;
617 SELECT sql FROM sqlite_master WHERE tbl_name = 't8'
619 } {{CREATE TABLE t8(b NUM,h,i INT,j)}}
621 #--------------------------------------------------------------------
622 # Test cases table-13.*
624 # Test the ability to have default values of CURRENT_TIME, CURRENT_DATE
625 # and CURRENT_TIMESTAMP.
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
635 SELECT * FROM tablet8;
639 unset -nocomplain date time seconds
640 foreach {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
647 set sqlite_current_time $seconds
648 do_test table-13.2.$i {
650 INSERT INTO tablet8(a) VALUES($i);
651 SELECT tm, dt, dttm FROM tablet8 WHERE a=$i;
653 } [list $time $date [list $date $time]]
655 set sqlite_current_time 0
657 #--------------------------------------------------------------------
658 # Test cases table-14.*
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.
665 # 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.
671 # pragma vdbe_trace = 0;
673 # Try to create a table from within a callback:
674 unset -nocomplain result
678 db eval {SELECT * FROM tablet8 LIMIT 1} {} {
679 db eval {CREATE TABLE t9(a, b, c)}
683 set result [list $rc $msg]
686 # Try to drop a table from within a callback:
690 db eval {SELECT * FROM tablet8 LIMIT 1} {} {
691 db eval {DROP TABLE t9;}
695 set result [list $rc $msg]
696 } {1 {database table is locked}}
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.
703 forcedelete test2.db-journal
705 ATTACH 'test2.db' as aux;
707 db eval {SELECT * FROM tablet8 LIMIT 1} {} {
708 db eval {CREATE TABLE aux.t1(a, b, c)}
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).
719 db eval {SELECT * FROM tablet8 LIMIT 1} {} {
720 db eval {DROP TABLE aux.t1;}
724 set result [list $rc $msg]
725 } {1 {database table is locked}}
728 # 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.
735 for {set i 0} {$i<2000} {incr i} {
736 execsql "CREATE TABLE tbl$i (a, b, c)"
742 for {set i 0} {$i<2000} {incr i} {
743 execsql "DROP TABLE tbl$i"
748 # Ticket 3a88d85f36704eebe134f7f48aebf00cd6438c1a (2014-08-05)
749 # The following SQL script segfaults while running the INSERT statement:
751 # CREATE TABLE t1(x DEFAULT(max(1)));
752 # INSERT INTO t1(rowid) VALUES(1);
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.
759 do_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;
764 do_catchsql_test table-16.2 {
765 INSERT INTO t16(rowid) VALUES(4);
766 } {1 {unknown function: max()}}
767 do_execsql_test table-16.3 {
769 CREATE TABLE t16(x DEFAULT(abs(1)));
770 INSERT INTO t16(rowid) VALUES(4);
771 SELECT rowid, x FROM t16;
773 do_catchsql_test table-16.4 {
775 CREATE TABLE t16(x DEFAULT(avg(1)));
776 INSERT INTO t16(rowid) VALUES(123);
777 SELECT rowid, x FROM t16;
778 } {1 {unknown function: avg()}}
779 do_catchsql_test table-16.5 {
781 CREATE TABLE t16(x DEFAULT(count()));
782 INSERT INTO t16(rowid) VALUES(123);
783 SELECT rowid, x FROM t16;
784 } {1 {unknown function: count()}}
785 do_catchsql_test table-16.6 {
787 CREATE TABLE t16(x DEFAULT(string_agg('x',',')));
788 INSERT INTO t16(rowid) VALUES(123);
789 SELECT rowid, x FROM t16;
790 } {1 {unknown function: string_agg()}}
791 do_catchsql_test table-16.7 {
792 INSERT INTO t16 DEFAULT VALUES;
793 } {1 {unknown function: string_agg()}}
795 # 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.
799 do_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;
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;
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.
817 do_catchsql_test table-18.1 {
818 DROP TABLE IF EXISTS t1;
820 CREATE TABLE t1 AS SELECT zeroblob(2e20);
821 } {1 {string or blob too big}}
822 do_execsql_test table-18.2 {
824 PRAGMA integrity_check;
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.
833 do_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}