xref: /sqlite-3.40.0/test/table.test (revision 6ab91a7a)
1b19a2bc6Sdrh# 2001 September 15
21b6a71feSdrh#
3b19a2bc6Sdrh# The author disclaims copyright to this source code.  In place of
4b19a2bc6Sdrh# a legal notice, here is a blessing:
51b6a71feSdrh#
6b19a2bc6Sdrh#    May you do good and not evil.
7b19a2bc6Sdrh#    May you find forgiveness for yourself and forgive others.
8b19a2bc6Sdrh#    May you share freely, never taking more than you give.
91b6a71feSdrh#
101b6a71feSdrh#***********************************************************************
111b6a71feSdrh# This file implements regression tests for SQLite library.  The
121b6a71feSdrh# focus of this file is testing the CREATE TABLE statement.
131b6a71feSdrh#
141b6a71feSdrh
151b6a71feSdrhset testdir [file dirname $argv0]
161b6a71feSdrhsource $testdir/tester.tcl
171b6a71feSdrh
181b6a71feSdrh# Create a basic table and verify it is added to sqlite_master
191b6a71feSdrh#
201b6a71feSdrhdo_test table-1.1 {
211b6a71feSdrh  execsql {
221b6a71feSdrh    CREATE TABLE test1 (
231b6a71feSdrh      one varchar(10),
241b6a71feSdrh      two text
251b6a71feSdrh    )
261b6a71feSdrh  }
271b6a71feSdrh  execsql {
282803757aSdrh    SELECT sql FROM sqlite_master WHERE type!='meta'
291b6a71feSdrh  }
301b6a71feSdrh} {{CREATE TABLE test1 (
311b6a71feSdrh      one varchar(10),
321b6a71feSdrh      two text
331b6a71feSdrh    )}}
341b6a71feSdrh
351b6a71feSdrh
361b6a71feSdrh# Verify the other fields of the sqlite_master file.
371b6a71feSdrh#
381b6a71feSdrhdo_test table-1.3 {
392803757aSdrh  execsql {SELECT name, tbl_name, type FROM sqlite_master WHERE type!='meta'}
401b6a71feSdrh} {test1 test1 table}
411b6a71feSdrh
421b6a71feSdrh# Close and reopen the database.  Verify that everything is
431b6a71feSdrh# still the same.
441b6a71feSdrh#
451b6a71feSdrhdo_test table-1.4 {
461b6a71feSdrh  db close
47ef4ac8f9Sdrh  sqlite3 db test.db
482803757aSdrh  execsql {SELECT name, tbl_name, type from sqlite_master WHERE type!='meta'}
491b6a71feSdrh} {test1 test1 table}
501b6a71feSdrh
511b6a71feSdrh# Drop the database and make sure it disappears.
521b6a71feSdrh#
531b6a71feSdrhdo_test table-1.5 {
541b6a71feSdrh  execsql {DROP TABLE test1}
552803757aSdrh  execsql {SELECT * FROM sqlite_master WHERE type!='meta'}
561b6a71feSdrh} {}
571b6a71feSdrh
581b6a71feSdrh# Close and reopen the database.  Verify that the table is
591b6a71feSdrh# still gone.
601b6a71feSdrh#
611b6a71feSdrhdo_test table-1.6 {
621b6a71feSdrh  db close
63ef4ac8f9Sdrh  sqlite3 db test.db
642803757aSdrh  execsql {SELECT name FROM sqlite_master WHERE type!='meta'}
651b6a71feSdrh} {}
661b6a71feSdrh
67982cef7eSdrh# Repeat the above steps, but this time quote the table name.
68982cef7eSdrh#
69982cef7eSdrhdo_test table-1.10 {
70982cef7eSdrh  execsql {CREATE TABLE "create" (f1 int)}
712803757aSdrh  execsql {SELECT name FROM sqlite_master WHERE type!='meta'}
72982cef7eSdrh} {create}
73982cef7eSdrhdo_test table-1.11 {
74982cef7eSdrh  execsql {DROP TABLE "create"}
752803757aSdrh  execsql {SELECT name FROM "sqlite_master" WHERE type!='meta'}
76982cef7eSdrh} {}
77982cef7eSdrhdo_test table-1.12 {
78982cef7eSdrh  execsql {CREATE TABLE test1("f1 ho" int)}
792803757aSdrh  execsql {SELECT name as "X" FROM sqlite_master WHERE type!='meta'}
80982cef7eSdrh} {test1}
81982cef7eSdrhdo_test table-1.13 {
82982cef7eSdrh  execsql {DROP TABLE "TEST1"}
832803757aSdrh  execsql {SELECT name FROM "sqlite_master" WHERE type!='meta'}
84982cef7eSdrh} {}
85982cef7eSdrh
86982cef7eSdrh
871b6a71feSdrh
881b6a71feSdrh# Verify that we cannot make two tables with the same name
891b6a71feSdrh#
901b6a71feSdrhdo_test table-2.1 {
914cfa7934Sdrh  execsql {CREATE TABLE TEST2(one text)}
9242b9d7c5Sdrh  catchsql {CREATE TABLE test2(two text default 'hi')}
931d37e284Sdrh} {1 {table test2 already exists}}
94e4df0e74Sdrhdo_test table-2.1.1 {
95e4df0e74Sdrh  catchsql {CREATE TABLE "test2" (two)}
96e4df0e74Sdrh} {1 {table "test2" already exists}}
971b6a71feSdrhdo_test table-2.1b {
981b6a71feSdrh  set v [catch {execsql {CREATE TABLE sqlite_master(two text)}} msg]
991b6a71feSdrh  lappend v $msg
100d8123366Sdanielk1977} {1 {object name reserved for internal use: sqlite_master}}
1011b6a71feSdrhdo_test table-2.1c {
1021b6a71feSdrh  db close
103ef4ac8f9Sdrh  sqlite3 db test.db
1041b6a71feSdrh  set v [catch {execsql {CREATE TABLE sqlite_master(two text)}} msg]
1051b6a71feSdrh  lappend v $msg
106d8123366Sdanielk1977} {1 {object name reserved for internal use: sqlite_master}}
1071b6a71feSdrhdo_test table-2.1d {
108faa59554Sdrh  catchsql {CREATE TABLE IF NOT EXISTS test2(x,y)}
109faa59554Sdrh} {0 {}}
110faa59554Sdrhdo_test table-2.1e {
111a6370df1Sdrh  catchsql {CREATE TABLE IF NOT EXISTS test2(x UNIQUE, y TEXT PRIMARY KEY)}
112a6370df1Sdrh} {0 {}}
113a6370df1Sdrhdo_test table-2.1f {
1142803757aSdrh  execsql {DROP TABLE test2; SELECT name FROM sqlite_master WHERE type!='meta'}
1151b6a71feSdrh} {}
1161b6a71feSdrh
1171b6a71feSdrh# Verify that we cannot make a table with the same name as an index
1181b6a71feSdrh#
119dcc581ccSdrhdo_test table-2.2a {
1208a1e594cSdrh  execsql {CREATE TABLE test2(one text)}
1218a1e594cSdrh  execsql {CREATE INDEX test3 ON test2(one)}
1228a1e594cSdrh  catchsql {CREATE TABLE test3(two text)}
1231d37e284Sdrh} {1 {there is already an index named test3}}
1241b6a71feSdrhdo_test table-2.2b {
1251b6a71feSdrh  db close
126ef4ac8f9Sdrh  sqlite3 db test.db
1271b6a71feSdrh  set v [catch {execsql {CREATE TABLE test3(two text)}} msg]
1281b6a71feSdrh  lappend v $msg
1291d37e284Sdrh} {1 {there is already an index named test3}}
1301b6a71feSdrhdo_test table-2.2c {
1313fc190ccSdrh  execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
1323fc190ccSdrh} {test2 test3}
1333fc190ccSdrhdo_test table-2.2d {
1341b6a71feSdrh  execsql {DROP INDEX test3}
1351b6a71feSdrh  set v [catch {execsql {CREATE TABLE test3(two text)}} msg]
1361b6a71feSdrh  lappend v $msg
1371b6a71feSdrh} {0 {}}
1383fc190ccSdrhdo_test table-2.2e {
1392803757aSdrh  execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
1401b6a71feSdrh} {test2 test3}
1413fc190ccSdrhdo_test table-2.2f {
1421b6a71feSdrh  execsql {DROP TABLE test2; DROP TABLE test3}
1432803757aSdrh  execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
1441b6a71feSdrh} {}
1451b6a71feSdrh
1461b6a71feSdrh# Create a table with many field names
1471b6a71feSdrh#
1481b6a71feSdrhset big_table \
1491b6a71feSdrh{CREATE TABLE big(
1501b6a71feSdrh  f1 varchar(20),
1511b6a71feSdrh  f2 char(10),
152dcc581ccSdrh  f3 varchar(30) primary key,
1531b6a71feSdrh  f4 text,
1541b6a71feSdrh  f5 text,
1551b6a71feSdrh  f6 text,
1561b6a71feSdrh  f7 text,
1571b6a71feSdrh  f8 text,
1581b6a71feSdrh  f9 text,
1591b6a71feSdrh  f10 text,
1601b6a71feSdrh  f11 text,
1611b6a71feSdrh  f12 text,
1621b6a71feSdrh  f13 text,
1631b6a71feSdrh  f14 text,
1641b6a71feSdrh  f15 text,
1651b6a71feSdrh  f16 text,
1661b6a71feSdrh  f17 text,
1671b6a71feSdrh  f18 text,
1681b6a71feSdrh  f19 text,
1691b6a71feSdrh  f20 text
1701b6a71feSdrh)}
1711b6a71feSdrhdo_test table-3.1 {
1721b6a71feSdrh  execsql $big_table
173adbca9cfSdrh  execsql {SELECT sql FROM sqlite_master WHERE type=='table'}
1741b6a71feSdrh} \{$big_table\}
1751b6a71feSdrhdo_test table-3.2 {
1761b6a71feSdrh  set v [catch {execsql {CREATE TABLE BIG(xyz foo)}} msg]
1771b6a71feSdrh  lappend v $msg
1781d37e284Sdrh} {1 {table BIG already exists}}
1791b6a71feSdrhdo_test table-3.3 {
1801b6a71feSdrh  set v [catch {execsql {CREATE TABLE biG(xyz foo)}} msg]
1811b6a71feSdrh  lappend v $msg
1821d37e284Sdrh} {1 {table biG already exists}}
1831b6a71feSdrhdo_test table-3.4 {
1841b6a71feSdrh  set v [catch {execsql {CREATE TABLE bIg(xyz foo)}} msg]
1851b6a71feSdrh  lappend v $msg
1861d37e284Sdrh} {1 {table bIg already exists}}
1871b6a71feSdrhdo_test table-3.5 {
1881b6a71feSdrh  db close
189ef4ac8f9Sdrh  sqlite3 db test.db
1901b6a71feSdrh  set v [catch {execsql {CREATE TABLE Big(xyz foo)}} msg]
1911b6a71feSdrh  lappend v $msg
1921d37e284Sdrh} {1 {table Big already exists}}
1931b6a71feSdrhdo_test table-3.6 {
1941b6a71feSdrh  execsql {DROP TABLE big}
1952803757aSdrh  execsql {SELECT name FROM sqlite_master WHERE type!='meta'}
1961b6a71feSdrh} {}
1971b6a71feSdrh
1981b6a71feSdrh# Try creating large numbers of tables
1991b6a71feSdrh#
2001b6a71feSdrhset r {}
2011b6a71feSdrhfor {set i 1} {$i<=100} {incr i} {
202a9e99aeeSdrh  lappend r [format test%03d $i]
2031b6a71feSdrh}
2041b6a71feSdrhdo_test table-4.1 {
2051b6a71feSdrh  for {set i 1} {$i<=100} {incr i} {
206a9e99aeeSdrh    set sql "CREATE TABLE [format test%03d $i] ("
2071b6a71feSdrh    for {set k 1} {$k<$i} {incr k} {
2081b6a71feSdrh      append sql "field$k text,"
2091b6a71feSdrh    }
2101b6a71feSdrh    append sql "last_field text)"
2111b6a71feSdrh    execsql $sql
2121b6a71feSdrh  }
2132803757aSdrh  execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
2141b6a71feSdrh} $r
2151b6a71feSdrhdo_test table-4.1b {
2161b6a71feSdrh  db close
217ef4ac8f9Sdrh  sqlite3 db test.db
2182803757aSdrh  execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
2191b6a71feSdrh} $r
2201b6a71feSdrh
221c4a3c779Sdrh# Drop the even numbered tables
2221b6a71feSdrh#
2231b6a71feSdrhset r {}
2241b6a71feSdrhfor {set i 1} {$i<=100} {incr i 2} {
225a9e99aeeSdrh  lappend r [format test%03d $i]
2261b6a71feSdrh}
2271b6a71feSdrhdo_test table-4.2 {
2281b6a71feSdrh  for {set i 2} {$i<=100} {incr i 2} {
229428ae8cbSdrh    # if {$i==38} {execsql {pragma vdbe_trace=on}}
230a9e99aeeSdrh    set sql "DROP TABLE [format TEST%03d $i]"
2311b6a71feSdrh    execsql $sql
2321b6a71feSdrh  }
2332803757aSdrh  execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
2341b6a71feSdrh} $r
2353fc190ccSdrh#exit
2361b6a71feSdrh
2371b6a71feSdrh# Drop the odd number tables
2381b6a71feSdrh#
2391b6a71feSdrhdo_test table-4.3 {
2401b6a71feSdrh  for {set i 1} {$i<=100} {incr i 2} {
241a9e99aeeSdrh    set sql "DROP TABLE [format test%03d $i]"
2421b6a71feSdrh    execsql $sql
2431b6a71feSdrh  }
2442803757aSdrh  execsql {SELECT name FROM sqlite_master WHERE type!='meta' ORDER BY name}
2451b6a71feSdrh} {}
2461b6a71feSdrh
2471b6a71feSdrh# Try to drop a table that does not exist
2481b6a71feSdrh#
249a073384fSdrhdo_test table-5.1.1 {
250a073384fSdrh  catchsql {DROP TABLE test009}
251a9e99aeeSdrh} {1 {no such table: test009}}
252a073384fSdrhdo_test table-5.1.2 {
253a073384fSdrh  catchsql {DROP TABLE IF EXISTS test009}
254a073384fSdrh} {0 {}}
2551b6a71feSdrh
2561b6a71feSdrh# Try to drop sqlite_master
2571b6a71feSdrh#
2581b6a71feSdrhdo_test table-5.2 {
259a073384fSdrh  catchsql {DROP TABLE IF EXISTS sqlite_master}
2601d37e284Sdrh} {1 {table sqlite_master may not be dropped}}
2611b6a71feSdrh
26208ccfaa1Sdrh# Dropping sqlite_statN tables is OK.
26308ccfaa1Sdrh#
26408ccfaa1Sdrhdo_test table-5.2.1 {
26508ccfaa1Sdrh  db eval {
26608ccfaa1Sdrh    ANALYZE;
26708ccfaa1Sdrh    DROP TABLE IF EXISTS sqlite_stat1;
26808ccfaa1Sdrh    DROP TABLE IF EXISTS sqlite_stat2;
2698ad169abSdan    DROP TABLE IF EXISTS sqlite_stat3;
270c55521a6Sdan    DROP TABLE IF EXISTS sqlite_stat4;
27108ccfaa1Sdrh    SELECT name FROM sqlite_master WHERE name GLOB 'sqlite_stat*';
27208ccfaa1Sdrh  }
27308ccfaa1Sdrh} {}
27408ccfaa1Sdrh
27597ab32bcSdrhdo_test table-5.2.2 {
27697ab32bcSdrh  db close
27797ab32bcSdrh  forcedelete test.db
27897ab32bcSdrh  sqlite3 db test.db
279*6ab91a7aSdrh  sqlite3_db_config db DEFENSIVE 0
28097ab32bcSdrh  db eval {
28197ab32bcSdrh    CREATE TABLE t0(a,b);
28297ab32bcSdrh    CREATE INDEX t ON t0(a);
28397ab32bcSdrh    PRAGMA writable_schema=ON;
28497ab32bcSdrh    UPDATE sqlite_master SET sql='CREATE TABLE a.b(a UNIQUE';
28597ab32bcSdrh    BEGIN;
28697ab32bcSdrh    CREATE TABLE t1(x);
28797ab32bcSdrh    ROLLBACK;
28897ab32bcSdrh    DROP TABLE IF EXISTS t99;
28997ab32bcSdrh  }
29097ab32bcSdrh} {}
29197ab32bcSdrhdb close
29297ab32bcSdrhforcedelete test.db
29397ab32bcSdrhsqlite3 db test.db
29497ab32bcSdrh
2951b6a71feSdrh# Make sure an EXPLAIN does not really create a new table
2961b6a71feSdrh#
2971b6a71feSdrhdo_test table-5.3 {
2986bf89570Sdrh  ifcapable {explain} {
2991b6a71feSdrh    execsql {EXPLAIN CREATE TABLE test1(f1 int)}
3006bf89570Sdrh  }
3012803757aSdrh  execsql {SELECT name FROM sqlite_master WHERE type!='meta'}
3021b6a71feSdrh} {}
3031b6a71feSdrh
3041b6a71feSdrh# Make sure an EXPLAIN does not really drop an existing table
3051b6a71feSdrh#
3061b6a71feSdrhdo_test table-5.4 {
3071b6a71feSdrh  execsql {CREATE TABLE test1(f1 int)}
3086bf89570Sdrh  ifcapable {explain} {
3091b6a71feSdrh    execsql {EXPLAIN DROP TABLE test1}
3106bf89570Sdrh  }
3112803757aSdrh  execsql {SELECT name FROM sqlite_master WHERE type!='meta'}
3121b6a71feSdrh} {test1}
3131b6a71feSdrh
3144cfa7934Sdrh# Create a table with a goofy name
3154cfa7934Sdrh#
3163fc190ccSdrh#do_test table-6.1 {
3173fc190ccSdrh#  execsql {CREATE TABLE 'Spaces In This Name!'(x int)}
3183fc190ccSdrh#  execsql {INSERT INTO 'spaces in this name!' VALUES(1)}
3193fc190ccSdrh#  set list [glob -nocomplain testdb/spaces*.tbl]
3203fc190ccSdrh#} {testdb/spaces+in+this+name+.tbl}
3214cfa7934Sdrh
322c4a3c779Sdrh# Try using keywords as table names or column names.
323c4a3c779Sdrh#
324c4a3c779Sdrhdo_test table-7.1 {
325c4a3c779Sdrh  set v [catch {execsql {
326c4a3c779Sdrh    CREATE TABLE weird(
327c4a3c779Sdrh      desc text,
328c4a3c779Sdrh      asc text,
3296bf89570Sdrh      key int,
33017f71934Sdrh      [14_vac] boolean,
33117f71934Sdrh      fuzzy_dog_12 varchar(10),
332d400728aSdrh      begin blob,
333d400728aSdrh      end clob
334c4a3c779Sdrh    )
335c4a3c779Sdrh  }} msg]
336c4a3c779Sdrh  lappend v $msg
337c4a3c779Sdrh} {0 {}}
338c4a3c779Sdrhdo_test table-7.2 {
339c4a3c779Sdrh  execsql {
340d400728aSdrh    INSERT INTO weird VALUES('a','b',9,0,'xyz','hi','y''all');
341c4a3c779Sdrh    SELECT * FROM weird;
342c4a3c779Sdrh  }
343d400728aSdrh} {a b 9 0 xyz hi y'all}
344c4a3c779Sdrhdo_test table-7.3 {
345c4a3c779Sdrh  execsql2 {
346c4a3c779Sdrh    SELECT * FROM weird;
347c4a3c779Sdrh  }
3486bf89570Sdrh} {desc a asc b key 9 14_vac 0 fuzzy_dog_12 xyz begin hi end y'all}
34978c2e6d8Sdanielk1977do_test table-7.3 {
35078c2e6d8Sdanielk1977  execsql {
35178c2e6d8Sdanielk1977    CREATE TABLE savepoint(release);
35278c2e6d8Sdanielk1977    INSERT INTO savepoint(release) VALUES(10);
35378c2e6d8Sdanielk1977    UPDATE savepoint SET release = 5;
35478c2e6d8Sdanielk1977    SELECT release FROM savepoint;
35578c2e6d8Sdanielk1977  }
35678c2e6d8Sdanielk1977} {5}
357c4a3c779Sdrh
358969fa7c1Sdrh# Try out the CREATE TABLE AS syntax
359969fa7c1Sdrh#
360969fa7c1Sdrhdo_test table-8.1 {
361969fa7c1Sdrh  execsql2 {
362969fa7c1Sdrh    CREATE TABLE t2 AS SELECT * FROM weird;
363969fa7c1Sdrh    SELECT * FROM t2;
364969fa7c1Sdrh  }
3656bf89570Sdrh} {desc a asc b key 9 14_vac 0 fuzzy_dog_12 xyz begin hi end y'all}
36617f71934Sdrhdo_test table-8.1.1 {
36717f71934Sdrh  execsql {
36817f71934Sdrh    SELECT sql FROM sqlite_master WHERE name='t2';
36917f71934Sdrh  }
37017f71934Sdrh} {{CREATE TABLE t2(
371c4a64facSdrh  "desc" TEXT,
372c4a64facSdrh  "asc" TEXT,
373c4a64facSdrh  "key" INT,
374c4a64facSdrh  "14_vac" NUM,
375c4a64facSdrh  fuzzy_dog_12 TEXT,
376c4a64facSdrh  "begin",
377c4a64facSdrh  "end" TEXT
37817f71934Sdrh)}}
379969fa7c1Sdrhdo_test table-8.2 {
380969fa7c1Sdrh  execsql {
381234c39dfSdrh    CREATE TABLE "t3""xyz"(a,b,c);
382234c39dfSdrh    INSERT INTO [t3"xyz] VALUES(1,2,3);
383234c39dfSdrh    SELECT * FROM [t3"xyz];
384969fa7c1Sdrh  }
385969fa7c1Sdrh} {1 2 3}
386969fa7c1Sdrhdo_test table-8.3 {
387969fa7c1Sdrh  execsql2 {
388234c39dfSdrh    CREATE TABLE [t4"abc] AS SELECT count(*) as cnt, max(b+c) FROM [t3"xyz];
389234c39dfSdrh    SELECT * FROM [t4"abc];
390969fa7c1Sdrh  }
39117f71934Sdrh} {cnt 1 max(b+c) 5}
39200e279d9Sdanielk1977
39300e279d9Sdanielk1977# Update for v3: The declaration type of anything except a column is now a
39400e279d9Sdanielk1977# NULL pointer, so the created table has no column types. (Changed result
395234c39dfSdrh# from {{CREATE TABLE 't4"abc'(cnt NUMERIC,"max(b+c)" NUMERIC)}}).
39617f71934Sdrhdo_test table-8.3.1 {
39717f71934Sdrh  execsql {
398234c39dfSdrh    SELECT sql FROM sqlite_master WHERE name='t4"abc'
39917f71934Sdrh  }
400234c39dfSdrh} {{CREATE TABLE "t4""abc"(cnt,"max(b+c)")}}
40153c0f748Sdanielk1977
40253c0f748Sdanielk1977ifcapable tempdb {
403969fa7c1Sdrh  do_test table-8.4 {
404969fa7c1Sdrh    execsql2 {
405234c39dfSdrh      CREATE TEMPORARY TABLE t5 AS SELECT count(*) AS [y'all] FROM [t3"xyz];
406969fa7c1Sdrh      SELECT * FROM t5;
407969fa7c1Sdrh    }
408969fa7c1Sdrh  } {y'all 1}
40953c0f748Sdanielk1977}
41053c0f748Sdanielk1977
411969fa7c1Sdrhdo_test table-8.5 {
412969fa7c1Sdrh  db close
413ef4ac8f9Sdrh  sqlite3 db test.db
414969fa7c1Sdrh  execsql2 {
415234c39dfSdrh    SELECT * FROM [t4"abc];
416969fa7c1Sdrh  }
41717f71934Sdrh} {cnt 1 max(b+c) 5}
418969fa7c1Sdrhdo_test table-8.6 {
419969fa7c1Sdrh  execsql2 {
420969fa7c1Sdrh    SELECT * FROM t2;
421969fa7c1Sdrh  }
4226bf89570Sdrh} {desc a asc b key 9 14_vac 0 fuzzy_dog_12 xyz begin hi end y'all}
423969fa7c1Sdrhdo_test table-8.7 {
424969fa7c1Sdrh  catchsql {
425969fa7c1Sdrh    SELECT * FROM t5;
426969fa7c1Sdrh  }
427969fa7c1Sdrh} {1 {no such table: t5}}
42817f71934Sdrhdo_test table-8.8 {
42917f71934Sdrh  catchsql {
43017f71934Sdrh    CREATE TABLE t5 AS SELECT * FROM no_such_table;
43117f71934Sdrh  }
43217f71934Sdrh} {1 {no such table: no_such_table}}
433969fa7c1Sdrh
4341b870de6Sdanielk1977do_test table-8.9 {
4351b870de6Sdanielk1977  execsql {
4361b870de6Sdanielk1977    CREATE TABLE t10("col.1" [char.3]);
4371b870de6Sdanielk1977    CREATE TABLE t11 AS SELECT * FROM t10;
4381b870de6Sdanielk1977    SELECT sql FROM sqlite_master WHERE name = 't11';
4391b870de6Sdanielk1977  }
440c4a64facSdrh} {{CREATE TABLE t11("col.1" TEXT)}}
4411b870de6Sdanielk1977do_test table-8.10 {
4421b870de6Sdanielk1977  execsql {
4431b870de6Sdanielk1977    CREATE TABLE t12(
4441b870de6Sdanielk1977      a INTEGER,
4451b870de6Sdanielk1977      b VARCHAR(10),
4461b870de6Sdanielk1977      c VARCHAR(1,10),
4471b870de6Sdanielk1977      d VARCHAR(+1,-10),
4481b870de6Sdanielk1977      e VARCHAR (+1,-10),
4491b870de6Sdanielk1977      f "VARCHAR (+1,-10, 5)",
4501b870de6Sdanielk1977      g BIG INTEGER
4511b870de6Sdanielk1977    );
4521b870de6Sdanielk1977    CREATE TABLE t13 AS SELECT * FROM t12;
4531b870de6Sdanielk1977    SELECT sql FROM sqlite_master WHERE name = 't13';
4541b870de6Sdanielk1977  }
4551b870de6Sdanielk1977} {{CREATE TABLE t13(
456c4a64facSdrh  a INT,
457c4a64facSdrh  b TEXT,
458c4a64facSdrh  c TEXT,
459c4a64facSdrh  d TEXT,
460c4a64facSdrh  e TEXT,
461c4a64facSdrh  f TEXT,
462c4a64facSdrh  g INT
4631b870de6Sdanielk1977)}}
4641b870de6Sdanielk1977
46597fc3d06Sdrh# Make sure we cannot have duplicate column names within a table.
46697fc3d06Sdrh#
46797fc3d06Sdrhdo_test table-9.1 {
46897fc3d06Sdrh  catchsql {
46997fc3d06Sdrh    CREATE TABLE t6(a,b,a);
47097fc3d06Sdrh  }
47197fc3d06Sdrh} {1 {duplicate column name: a}}
472d8919670Sdrhdo_test table-9.2 {
473d8919670Sdrh  catchsql {
474d8919670Sdrh    CREATE TABLE t6(a varchar(100), b blob, a integer);
475d8919670Sdrh  }
476d8919670Sdrh} {1 {duplicate column name: a}}
47797fc3d06Sdrh
47804738cb9Sdrh# Check the foreign key syntax.
47904738cb9Sdrh#
4806bf89570Sdrhifcapable {foreignkey} {
48104738cb9Sdrhdo_test table-10.1 {
48204738cb9Sdrh  catchsql {
48304738cb9Sdrh    CREATE TABLE t6(a REFERENCES t4(a) NOT NULL);
48404738cb9Sdrh    INSERT INTO t6 VALUES(NULL);
48504738cb9Sdrh  }
486f9c8ce3cSdrh} {1 {NOT NULL constraint failed: t6.a}}
48704738cb9Sdrhdo_test table-10.2 {
48804738cb9Sdrh  catchsql {
48904738cb9Sdrh    DROP TABLE t6;
49004738cb9Sdrh    CREATE TABLE t6(a REFERENCES t4(a) MATCH PARTIAL);
49104738cb9Sdrh  }
49204738cb9Sdrh} {0 {}}
49304738cb9Sdrhdo_test table-10.3 {
49404738cb9Sdrh  catchsql {
49504738cb9Sdrh    DROP TABLE t6;
49604738cb9Sdrh    CREATE TABLE t6(a REFERENCES t4 MATCH FULL ON DELETE SET NULL NOT NULL);
49704738cb9Sdrh  }
49804738cb9Sdrh} {0 {}}
49904738cb9Sdrhdo_test table-10.4 {
50004738cb9Sdrh  catchsql {
50104738cb9Sdrh    DROP TABLE t6;
50204738cb9Sdrh    CREATE TABLE t6(a REFERENCES t4 MATCH FULL ON UPDATE SET DEFAULT DEFAULT 1);
50304738cb9Sdrh  }
50404738cb9Sdrh} {0 {}}
50504738cb9Sdrhdo_test table-10.5 {
50604738cb9Sdrh  catchsql {
50704738cb9Sdrh    DROP TABLE t6;
50804738cb9Sdrh    CREATE TABLE t6(a NOT NULL NOT DEFERRABLE INITIALLY IMMEDIATE);
50904738cb9Sdrh  }
51004738cb9Sdrh} {0 {}}
51104738cb9Sdrhdo_test table-10.6 {
51204738cb9Sdrh  catchsql {
51304738cb9Sdrh    DROP TABLE t6;
51404738cb9Sdrh    CREATE TABLE t6(a NOT NULL DEFERRABLE INITIALLY DEFERRED);
51504738cb9Sdrh  }
51604738cb9Sdrh} {0 {}}
51704738cb9Sdrhdo_test table-10.7 {
51804738cb9Sdrh  catchsql {
51904738cb9Sdrh    DROP TABLE t6;
52004738cb9Sdrh    CREATE TABLE t6(a,
52104738cb9Sdrh      FOREIGN KEY (a) REFERENCES t4(b) DEFERRABLE INITIALLY DEFERRED
52204738cb9Sdrh    );
52304738cb9Sdrh  }
52404738cb9Sdrh} {0 {}}
52504738cb9Sdrhdo_test table-10.8 {
52604738cb9Sdrh  catchsql {
52704738cb9Sdrh    DROP TABLE t6;
52804738cb9Sdrh    CREATE TABLE t6(a,b,c,
52904738cb9Sdrh      FOREIGN KEY (b,c) REFERENCES t4(x,y) MATCH PARTIAL
53004738cb9Sdrh        ON UPDATE SET NULL ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
53104738cb9Sdrh    );
53204738cb9Sdrh  }
53304738cb9Sdrh} {0 {}}
534c2eef3b3Sdrhdo_test table-10.9 {
535c2eef3b3Sdrh  catchsql {
536c2eef3b3Sdrh    DROP TABLE t6;
537c2eef3b3Sdrh    CREATE TABLE t6(a,b,c,
538c2eef3b3Sdrh      FOREIGN KEY (b,c) REFERENCES t4(x)
539c2eef3b3Sdrh    );
540c2eef3b3Sdrh  }
541c2eef3b3Sdrh} {1 {number of columns in foreign key does not match the number of columns in the referenced table}}
542c2eef3b3Sdrhdo_test table-10.10 {
543c2eef3b3Sdrh  catchsql {DROP TABLE t6}
544c2eef3b3Sdrh  catchsql {
545c2eef3b3Sdrh    CREATE TABLE t6(a,b,c,
546c2eef3b3Sdrh      FOREIGN KEY (b,c) REFERENCES t4(x,y,z)
547c2eef3b3Sdrh    );
548c2eef3b3Sdrh  }
549c2eef3b3Sdrh} {1 {number of columns in foreign key does not match the number of columns in the referenced table}}
550c2eef3b3Sdrhdo_test table-10.11 {
551c2eef3b3Sdrh  catchsql {DROP TABLE t6}
552c2eef3b3Sdrh  catchsql {
553c2eef3b3Sdrh    CREATE TABLE t6(a,b, c REFERENCES t4(x,y));
554c2eef3b3Sdrh  }
555c2eef3b3Sdrh} {1 {foreign key on c should reference only one column of table t4}}
556c2eef3b3Sdrhdo_test table-10.12 {
557c2eef3b3Sdrh  catchsql {DROP TABLE t6}
558c2eef3b3Sdrh  catchsql {
559c2eef3b3Sdrh    CREATE TABLE t6(a,b,c,
560c2eef3b3Sdrh      FOREIGN KEY (b,x) REFERENCES t4(x,y)
561c2eef3b3Sdrh    );
562c2eef3b3Sdrh  }
563c2eef3b3Sdrh} {1 {unknown column "x" in foreign key definition}}
564c2eef3b3Sdrhdo_test table-10.13 {
565c2eef3b3Sdrh  catchsql {DROP TABLE t6}
566c2eef3b3Sdrh  catchsql {
567c2eef3b3Sdrh    CREATE TABLE t6(a,b,c,
568c2eef3b3Sdrh      FOREIGN KEY (x,b) REFERENCES t4(x,y)
569c2eef3b3Sdrh    );
570c2eef3b3Sdrh  }
571c2eef3b3Sdrh} {1 {unknown column "x" in foreign key definition}}
5726bf89570Sdrh} ;# endif foreignkey
57304738cb9Sdrh
57435bb9d02Sdanielk1977# Test for the "typeof" function. More tests for the
57535bb9d02Sdanielk1977# typeof() function are found in bind.test and types.test.
57638640e15Sdrh#
57738640e15Sdrhdo_test table-11.1 {
57838640e15Sdrh  execsql {
57938640e15Sdrh    CREATE TABLE t7(
58038640e15Sdrh       a integer primary key,
58138640e15Sdrh       b number(5,10),
58238640e15Sdrh       c character varying (8),
58338640e15Sdrh       d VARCHAR(9),
58438640e15Sdrh       e clob,
58538640e15Sdrh       f BLOB,
58638640e15Sdrh       g Text,
58738640e15Sdrh       h
58838640e15Sdrh    );
58938640e15Sdrh    INSERT INTO t7(a) VALUES(1);
59038640e15Sdrh    SELECT typeof(a), typeof(b), typeof(c), typeof(d),
59138640e15Sdrh           typeof(e), typeof(f), typeof(g), typeof(h)
59238640e15Sdrh    FROM t7 LIMIT 1;
59338640e15Sdrh  }
59435bb9d02Sdanielk1977} {integer null null null null null null null}
59538640e15Sdrhdo_test table-11.2 {
59638640e15Sdrh  execsql {
59738640e15Sdrh    SELECT typeof(a+b), typeof(a||b), typeof(c+d), typeof(c||d)
59838640e15Sdrh    FROM t7 LIMIT 1;
59938640e15Sdrh  }
60035bb9d02Sdanielk1977} {null null null null}
60197fc3d06Sdrh
60200e279d9Sdanielk1977# Test that when creating a table using CREATE TABLE AS, column types are
60300e279d9Sdanielk1977# assigned correctly for (SELECT ...) and 'x AS y' expressions.
60400e279d9Sdanielk1977do_test table-12.1 {
6053e8c37e7Sdanielk1977  ifcapable subquery {
60600e279d9Sdanielk1977    execsql {
60700e279d9Sdanielk1977      CREATE TABLE t8 AS SELECT b, h, a as i, (SELECT f FROM t7) as j FROM t7;
60800e279d9Sdanielk1977    }
6093e8c37e7Sdanielk1977  } else {
6103e8c37e7Sdanielk1977    execsql {
6113e8c37e7Sdanielk1977      CREATE TABLE t8 AS SELECT b, h, a as i, f as j FROM t7;
6123e8c37e7Sdanielk1977    }
6133e8c37e7Sdanielk1977  }
61400e279d9Sdanielk1977} {}
61500e279d9Sdanielk1977do_test table-12.2 {
61600e279d9Sdanielk1977  execsql {
61700e279d9Sdanielk1977    SELECT sql FROM sqlite_master WHERE tbl_name = 't8'
61800e279d9Sdanielk1977  }
619c4a64facSdrh} {{CREATE TABLE t8(b NUM,h,i INT,j)}}
62000e279d9Sdanielk1977
621e6efa74bSdanielk1977#--------------------------------------------------------------------
622e6efa74bSdanielk1977# Test cases table-13.*
623e6efa74bSdanielk1977#
6247977a17fSdanielk1977# Test the ability to have default values of CURRENT_TIME, CURRENT_DATE
6257977a17fSdanielk1977# and CURRENT_TIMESTAMP.
6267977a17fSdanielk1977#
6277977a17fSdanielk1977do_test table-13.1 {
6287977a17fSdanielk1977  execsql {
6297977a17fSdanielk1977    CREATE TABLE tablet8(
6307977a17fSdanielk1977       a integer primary key,
6317977a17fSdanielk1977       tm text DEFAULT CURRENT_TIME,
6327977a17fSdanielk1977       dt text DEFAULT CURRENT_DATE,
6337977a17fSdanielk1977       dttm text DEFAULT CURRENT_TIMESTAMP
6347977a17fSdanielk1977    );
6357977a17fSdanielk1977    SELECT * FROM tablet8;
6367977a17fSdanielk1977  }
6377977a17fSdanielk1977} {}
6387977a17fSdanielk1977set i 0
639d0b2677bSdrhunset -nocomplain date time seconds
6409645d8d4Sdrhforeach {date time seconds} {
6419645d8d4Sdrh  1976-07-04 12:00:00 205329600
6429645d8d4Sdrh  1994-04-16 14:00:00 766504800
6439645d8d4Sdrh  2000-01-01 00:00:00 946684800
6449645d8d4Sdrh  2003-12-31 12:34:56 1072874096
6457977a17fSdanielk1977} {
6467977a17fSdanielk1977  incr i
6479645d8d4Sdrh  set sqlite_current_time $seconds
6487977a17fSdanielk1977  do_test table-13.2.$i {
6497977a17fSdanielk1977    execsql "
6507977a17fSdanielk1977      INSERT INTO tablet8(a) VALUES($i);
6517977a17fSdanielk1977      SELECT tm, dt, dttm FROM tablet8 WHERE a=$i;
6527977a17fSdanielk1977    "
6537977a17fSdanielk1977  } [list $time $date [list $date $time]]
6547977a17fSdanielk1977}
6557977a17fSdanielk1977set sqlite_current_time 0
6567977a17fSdanielk1977
657e6efa74bSdanielk1977#--------------------------------------------------------------------
658e6efa74bSdanielk1977# Test cases table-14.*
659e6efa74bSdanielk1977#
660e6efa74bSdanielk1977# Test that a table cannot be created or dropped while other virtual
661e6efa74bSdanielk1977# machines are active. This is required because otherwise when in
662e6efa74bSdanielk1977# auto-vacuum mode the btree-layer may need to move the root-pages of
663e6efa74bSdanielk1977# a table for which there is an open cursor.
664e6efa74bSdanielk1977#
665b7af4452Sdrh# 2007-05-02:  A open btree cursor no longer blocks CREATE TABLE.
666b7af4452Sdrh# But DROP TABLE is still prohibited because we do not want to
667b7af4452Sdrh# delete a table out from under a running query.
668b7af4452Sdrh#
669e6efa74bSdanielk1977
670a21c6b6fSdanielk1977# db eval {
671a21c6b6fSdanielk1977#   pragma vdbe_trace = 0;
672a21c6b6fSdanielk1977# }
673e6efa74bSdanielk1977# Try to create a table from within a callback:
674251b067cSdrhunset -nocomplain result
675e6efa74bSdanielk1977do_test table-14.1 {
676e6efa74bSdanielk1977  set rc [
677e6efa74bSdanielk1977    catch {
678e6efa74bSdanielk1977      db eval {SELECT * FROM tablet8 LIMIT 1} {} {
679e6efa74bSdanielk1977        db eval {CREATE TABLE t9(a, b, c)}
680e6efa74bSdanielk1977      }
681e6efa74bSdanielk1977    } msg
682e6efa74bSdanielk1977  ]
683e6efa74bSdanielk1977  set result [list $rc $msg]
684b7af4452Sdrh} {0 {}}
685e6efa74bSdanielk1977
686e6efa74bSdanielk1977# Try to drop a table from within a callback:
6875a8f9374Sdanielk1977do_test table-14.2 {
688e6efa74bSdanielk1977  set rc [
689e6efa74bSdanielk1977    catch {
690e6efa74bSdanielk1977      db eval {SELECT * FROM tablet8 LIMIT 1} {} {
691e6efa74bSdanielk1977        db eval {DROP TABLE t9;}
692e6efa74bSdanielk1977      }
693e6efa74bSdanielk1977    } msg
694e6efa74bSdanielk1977  ]
695e6efa74bSdanielk1977  set result [list $rc $msg]
696e6efa74bSdanielk1977} {1 {database table is locked}}
697e6efa74bSdanielk1977
6985a8f9374Sdanielk1977ifcapable attach {
699e6efa74bSdanielk1977  # Now attach a database and ensure that a table can be created in the
700e6efa74bSdanielk1977  # attached database whilst in a callback from a query on the main database.
7015a8f9374Sdanielk1977  do_test table-14.3 {
702fda06befSmistachkin    forcedelete test2.db
703fda06befSmistachkin    forcedelete test2.db-journal
704e6efa74bSdanielk1977    execsql {
7055a8f9374Sdanielk1977      ATTACH 'test2.db' as aux;
706e6efa74bSdanielk1977    }
707e6efa74bSdanielk1977    db eval {SELECT * FROM tablet8 LIMIT 1} {} {
708e6efa74bSdanielk1977      db eval {CREATE TABLE aux.t1(a, b, c)}
709e6efa74bSdanielk1977    }
710e6efa74bSdanielk1977  } {}
711e6efa74bSdanielk1977
712e6efa74bSdanielk1977  # On the other hand, it should be impossible to drop a table when any VMs
713e6efa74bSdanielk1977  # are active. This is because VerifyCookie instructions may have already
714e6efa74bSdanielk1977  # been executed, and btree root-pages may not move after this (which a
715e6efa74bSdanielk1977  # delete table might do).
716e6efa74bSdanielk1977  do_test table-14.4 {
717e6efa74bSdanielk1977    set rc [
718e6efa74bSdanielk1977      catch {
719e6efa74bSdanielk1977        db eval {SELECT * FROM tablet8 LIMIT 1} {} {
720e6efa74bSdanielk1977          db eval {DROP TABLE aux.t1;}
721e6efa74bSdanielk1977        }
722e6efa74bSdanielk1977      } msg
723e6efa74bSdanielk1977    ]
724e6efa74bSdanielk1977    set result [list $rc $msg]
725e6efa74bSdanielk1977  } {1 {database table is locked}}
7265a8f9374Sdanielk1977}
727e6efa74bSdanielk1977
728ae82558bSdanielk1977# Create and drop 2000 tables. This is to check that the balance_shallow()
729ae82558bSdanielk1977# routine works correctly on the sqlite_master table. At one point it
730ae82558bSdanielk1977# contained a bug that would prevent the right-child pointer of the
731ae82558bSdanielk1977# child page from being copied to the root page.
732ae82558bSdanielk1977#
733ae82558bSdanielk1977do_test table-15.1 {
734ae82558bSdanielk1977  execsql {BEGIN}
735ae82558bSdanielk1977  for {set i 0} {$i<2000} {incr i} {
736ae82558bSdanielk1977    execsql "CREATE TABLE tbl$i (a, b, c)"
737ae82558bSdanielk1977  }
738ae82558bSdanielk1977  execsql {COMMIT}
739ae82558bSdanielk1977} {}
740ae82558bSdanielk1977do_test table-15.2 {
741ae82558bSdanielk1977  execsql {BEGIN}
742ae82558bSdanielk1977  for {set i 0} {$i<2000} {incr i} {
743ae82558bSdanielk1977    execsql "DROP TABLE tbl$i"
744ae82558bSdanielk1977  }
745ae82558bSdanielk1977  execsql {COMMIT}
746ae82558bSdanielk1977} {}
747ae82558bSdanielk1977
7481cfc9aa9Sdrh# Ticket 3a88d85f36704eebe134f7f48aebf00cd6438c1a (2014-08-05)
7491cfc9aa9Sdrh# The following SQL script segfaults while running the INSERT statement:
7501cfc9aa9Sdrh#
7511cfc9aa9Sdrh#    CREATE TABLE t1(x DEFAULT(max(1)));
7521cfc9aa9Sdrh#    INSERT INTO t1(rowid) VALUES(1);
7531cfc9aa9Sdrh#
7541cfc9aa9Sdrh# The problem appears to be the use of an aggregate function as part of
7551cfc9aa9Sdrh# the default value for a column. This problem has been in the code since
7561cfc9aa9Sdrh# at least 2006-01-01 and probably before that. This problem was detected
7571cfc9aa9Sdrh# and reported on the [email protected] mailing list by Zsbán Ambrus.
7581cfc9aa9Sdrh#
7591cfc9aa9Sdrhdo_execsql_test table-16.1 {
7601cfc9aa9Sdrh  CREATE TABLE t16(x DEFAULT(max(1)));
7611cfc9aa9Sdrh  INSERT INTO t16(x) VALUES(123);
7621cfc9aa9Sdrh  SELECT rowid, x FROM t16;
7631cfc9aa9Sdrh} {1 123}
7641cfc9aa9Sdrhdo_catchsql_test table-16.2 {
7651cfc9aa9Sdrh  INSERT INTO t16(rowid) VALUES(4);
7660c4de2d9Sdrh} {1 {unknown function: max()}}
7671cfc9aa9Sdrhdo_execsql_test table-16.3 {
7681cfc9aa9Sdrh  DROP TABLE t16;
7691cfc9aa9Sdrh  CREATE TABLE t16(x DEFAULT(abs(1)));
7701cfc9aa9Sdrh  INSERT INTO t16(rowid) VALUES(4);
7711cfc9aa9Sdrh  SELECT rowid, x FROM t16;
7721cfc9aa9Sdrh} {4 1}
7731cfc9aa9Sdrhdo_catchsql_test table-16.4 {
7741cfc9aa9Sdrh  DROP TABLE t16;
7751cfc9aa9Sdrh  CREATE TABLE t16(x DEFAULT(avg(1)));
7761cfc9aa9Sdrh  INSERT INTO t16(rowid) VALUES(123);
7771cfc9aa9Sdrh  SELECT rowid, x FROM t16;
7780c4de2d9Sdrh} {1 {unknown function: avg()}}
7791cfc9aa9Sdrhdo_catchsql_test table-16.5 {
7801cfc9aa9Sdrh  DROP TABLE t16;
7811cfc9aa9Sdrh  CREATE TABLE t16(x DEFAULT(count()));
7821cfc9aa9Sdrh  INSERT INTO t16(rowid) VALUES(123);
7831cfc9aa9Sdrh  SELECT rowid, x FROM t16;
7840c4de2d9Sdrh} {1 {unknown function: count()}}
7851cfc9aa9Sdrhdo_catchsql_test table-16.6 {
7861cfc9aa9Sdrh  DROP TABLE t16;
7871cfc9aa9Sdrh  CREATE TABLE t16(x DEFAULT(group_concat('x',',')));
7881cfc9aa9Sdrh  INSERT INTO t16(rowid) VALUES(123);
7891cfc9aa9Sdrh  SELECT rowid, x FROM t16;
7900c4de2d9Sdrh} {1 {unknown function: group_concat()}}
7910c4de2d9Sdrhdo_catchsql_test table-16.7 {
7920c4de2d9Sdrh  INSERT INTO t16 DEFAULT VALUES;
7930c4de2d9Sdrh} {1 {unknown function: group_concat()}}
7941cfc9aa9Sdrh
7956b5631e0Sdrh# Ticket [https://www.sqlite.org/src/info/094d39a4c95ee4abbc417f04214617675ba15c63]
7966b5631e0Sdrh# describes a assertion fault that occurs on a CREATE TABLE .. AS SELECT statement.
7976b5631e0Sdrh# the following test verifies that the problem has been fixed.
7986b5631e0Sdrh#
7996b5631e0Sdrhdo_execsql_test table-17.1 {
8006b5631e0Sdrh  DROP TABLE IF EXISTS t1;
8016b5631e0Sdrh  CREATE TABLE t1(a TEXT);
8026b5631e0Sdrh  INSERT INTO t1(a) VALUES(1),(2);
8036b5631e0Sdrh  DROP TABLE IF EXISTS t2;
8046b5631e0Sdrh  CREATE TABLE t2(x TEXT, y TEXT);
8056b5631e0Sdrh  INSERT INTO t2(x,y) VALUES(3,4);
8066b5631e0Sdrh  DROP TABLE IF EXISTS t3;
8076b5631e0Sdrh  CREATE TABLE t3 AS
8086b5631e0Sdrh    SELECT a AS p, coalesce(y,a) AS q FROM t1 LEFT JOIN t2 ON a=x;
8096b5631e0Sdrh  SELECT p, q, '|' FROM t3 ORDER BY p;
8106b5631e0Sdrh} {1 1 | 2 2 |}
8116b5631e0Sdrh
8120dd5cdaeSdrh# 2015-06-16
8130dd5cdaeSdrh# Ticket [https://www.sqlite.org/src/tktview/873cae2b6e25b1991ce5e9b782f9cd0409b96063]
8140dd5cdaeSdrh# Make sure a CREATE TABLE AS statement correctly rolls back partial changes to the
8150dd5cdaeSdrh# sqlite_master table when the SELECT on the right-hand side aborts.
8160dd5cdaeSdrh#
8170dd5cdaeSdrhdo_catchsql_test table-18.1 {
8180dd5cdaeSdrh  DROP TABLE IF EXISTS t1;
8190dd5cdaeSdrh  BEGIN;
8200dd5cdaeSdrh  CREATE TABLE t1 AS SELECT zeroblob(2e20);
8210dd5cdaeSdrh} {1 {string or blob too big}}
8220dd5cdaeSdrhdo_execsql_test table-18.2 {
8230dd5cdaeSdrh  COMMIT;
8240dd5cdaeSdrh  PRAGMA integrity_check;
8250dd5cdaeSdrh} {ok}
8260dd5cdaeSdrh
8273c03afd3Sdrh# 2015-09-09
8283c03afd3Sdrh# Ticket [https://www.sqlite.org/src/info/acd12990885d9276]
8293c03afd3Sdrh# "CREATE TABLE ... AS SELECT ... FROM sqlite_master" fails because the row
8303c03afd3Sdrh# in the sqlite_master table for the next table is initially populated
8313c03afd3Sdrh# with a NULL instead of a record created by OP_Record.
8323c03afd3Sdrh#
8333c03afd3Sdrhdo_execsql_test table-19.1 {
8343c03afd3Sdrh  CREATE TABLE t19 AS SELECT * FROM sqlite_master;
8353c03afd3Sdrh  SELECT name FROM t19 ORDER BY name;
8363c03afd3Sdrh} {{} savepoint t10 t11 t12 t13 t16 t2 t3 t3\"xyz t4\"abc t7 t8 t9 tablet8 test1 weird}
8373c03afd3Sdrh
8383c03afd3Sdrh
8390dd5cdaeSdrh
8401b6a71feSdrhfinish_test
841