sqlite/test/e_createtable.test
dan a3f5108c1f Add further tests to e_createtable.test.
FossilOrigin-Name: 0a4528d629018eae0b0f3e173ebda666c2e2d502
2010-09-30 18:43:14 +00:00

1287 lines
46 KiB
Plaintext

# 2010 September 25
#
# The author disclaims copyright to this source code. In place of
# a legal notice, here is a blessing:
#
# May you do good and not evil.
# May you find forgiveness for yourself and forgive others.
# May you share freely, never taking more than you give.
#
#***********************************************************************
#
# This file implements tests to verify that the "testable statements" in
# the lang_createtable.html document are correct.
#
set testdir [file dirname $argv0]
source $testdir/tester.tcl
set ::testprefix e_createtable
# Test organization:
#
# e_createtable-0.*: Test that the syntax diagrams are correct.
#
# e_createtable-1.*: Test statements related to table and database names,
# the TEMP and TEMPORARY keywords, and the IF NOT EXISTS clause.
#
# e_createtable-2.*: Test "CREATE TABLE AS" statements.
#
proc do_createtable_tests {nm args} {
uplevel do_select_tests [list e_createtable-$nm] $args
}
#-------------------------------------------------------------------------
# This command returns a serialized tcl array mapping from the name of
# each attached database to a list of tables in that database. For example,
# if the database schema is created with:
#
# CREATE TABLE t1(x);
# CREATE TEMP TABLE t2(x);
# CREATE TEMP TABLE t3(x);
#
# Then this command returns "main t1 temp {t2 t3}".
#
proc table_list {} {
set res [list]
db eval { pragma database_list } a {
set dbname $a(name)
set master $a(name).sqlite_master
if {$dbname == "temp"} { set master sqlite_temp_master }
lappend res $dbname [
db eval "SELECT DISTINCT tbl_name FROM $master ORDER BY tbl_name"
]
}
set res
}
# EVIDENCE-OF: R-25262-01881 -- syntax diagram type-name
#
do_createtable_tests 0.1.1 -repair {
drop_all_tables
} {
1 "CREATE TABLE t1(c1 one)" {}
2 "CREATE TABLE t1(c1 one two)" {}
3 "CREATE TABLE t1(c1 one two three)" {}
4 "CREATE TABLE t1(c1 one two three four)" {}
5 "CREATE TABLE t1(c1 one two three four(14))" {}
6 "CREATE TABLE t1(c1 one two three four(14, 22))" {}
7 "CREATE TABLE t1(c1 var(+14, -22.3))" {}
8 "CREATE TABLE t1(c1 var(1.0e10))" {}
}
do_createtable_tests 0.1.2 -error {
near "%s": syntax error
} {
1 "CREATE TABLE t1(c1 one(number))" {number}
}
# EVIDENCE-OF: R-18762-12428 -- syntax diagram column-constraint
#
# Note: Not shown in the syntax diagram is the "NULL" constraint. This
# is the opposite of "NOT NULL" - it implies that the column may
# take a NULL value. This is the default anyway, so this type of
# constraint is rarely used.
#
do_createtable_tests 0.2.1 -repair {
drop_all_tables
execsql { CREATE TABLE t2(x PRIMARY KEY) }
} {
1.1 "CREATE TABLE t1(c1 text PRIMARY KEY)" {}
1.2 "CREATE TABLE t1(c1 text PRIMARY KEY ASC)" {}
1.3 "CREATE TABLE t1(c1 text PRIMARY KEY DESC)" {}
1.4 "CREATE TABLE t1(c1 text CONSTRAINT cons PRIMARY KEY DESC)" {}
2.1 "CREATE TABLE t1(c1 text NOT NULL)" {}
2.2 "CREATE TABLE t1(c1 text CONSTRAINT nm NOT NULL)" {}
2.3 "CREATE TABLE t1(c1 text NULL)" {}
2.4 "CREATE TABLE t1(c1 text CONSTRAINT nm NULL)" {}
3.1 "CREATE TABLE t1(c1 text UNIQUE)" {}
3.2 "CREATE TABLE t1(c1 text CONSTRAINT un UNIQUE)" {}
4.1 "CREATE TABLE t1(c1 text CHECK(c1!=0))" {}
4.2 "CREATE TABLE t1(c1 text CONSTRAINT chk CHECK(c1!=0))" {}
5.1 "CREATE TABLE t1(c1 text DEFAULT 1)" {}
5.2 "CREATE TABLE t1(c1 text DEFAULT -1)" {}
5.3 "CREATE TABLE t1(c1 text DEFAULT +1)" {}
5.4 "CREATE TABLE t1(c1 text DEFAULT -45.8e22)" {}
5.5 "CREATE TABLE t1(c1 text DEFAULT (1+1))" {}
5.6 "CREATE TABLE t1(c1 text CONSTRAINT \"1 2\" DEFAULT (1+1))" {}
6.1 "CREATE TABLE t1(c1 text COLLATE nocase)" {}
6.2 "CREATE TABLE t1(c1 text CONSTRAINT 'a x' COLLATE nocase)" {}
7.1 "CREATE TABLE t1(c1 REFERENCES t2)" {}
7.2 "CREATE TABLE t1(c1 CONSTRAINT abc REFERENCES t2)" {}
8.1 {
CREATE TABLE t1(c1
PRIMARY KEY NOT NULL UNIQUE CHECK(c1 IS 'ten') DEFAULT 123 REFERENCES t1
);
} {}
8.2 {
CREATE TABLE t1(c1
REFERENCES t1 DEFAULT 123 CHECK(c1 IS 'ten') UNIQUE NOT NULL PRIMARY KEY
);
} {}
}
# EVIDENCE-OF: R-17905-31923 -- syntax diagram table-constraint
#
do_createtable_tests 0.3.1 -repair {
drop_all_tables
execsql { CREATE TABLE t2(x PRIMARY KEY) }
} {
1.1 "CREATE TABLE t1(c1, c2, PRIMARY KEY(c1))" {}
1.2 "CREATE TABLE t1(c1, c2, PRIMARY KEY(c1, c2))" {}
1.3 "CREATE TABLE t1(c1, c2, PRIMARY KEY(c1, c2) ON CONFLICT IGNORE)" {}
2.1 "CREATE TABLE t1(c1, c2, UNIQUE(c1))" {}
2.2 "CREATE TABLE t1(c1, c2, UNIQUE(c1, c2))" {}
2.3 "CREATE TABLE t1(c1, c2, UNIQUE(c1, c2) ON CONFLICT IGNORE)" {}
3.1 "CREATE TABLE t1(c1, c2, CHECK(c1 IS NOT c2))" {}
4.1 "CREATE TABLE t1(c1, c2, FOREIGN KEY(c1) REFERENCES t2)" {}
}
# EVIDENCE-OF: R-18765-31171 -- syntax diagram column-def
#
do_createtable_tests 0.4.1 -repair {
drop_all_tables
} {
1 {CREATE TABLE t1(
col1,
col2 TEXT,
col3 INTEGER UNIQUE,
col4 VARCHAR(10, 10) PRIMARY KEY,
"name with spaces" REFERENCES t1
);
} {}
}
# EVIDENCE-OF: R-59573-11075 -- syntax diagram create-table-stmt
#
do_createtable_tests 0.5.1 -repair {
drop_all_tables
execsql { CREATE TABLE t2(a, b, c) }
} {
1 "CREATE TABLE t1(a, b, c)" {}
2 "CREATE TEMP TABLE t1(a, b, c)" {}
3 "CREATE TEMPORARY TABLE t1(a, b, c)" {}
4 "CREATE TABLE IF NOT EXISTS t1(a, b, c)" {}
5 "CREATE TEMP TABLE IF NOT EXISTS t1(a, b, c)" {}
6 "CREATE TEMPORARY TABLE IF NOT EXISTS t1(a, b, c)" {}
7 "CREATE TABLE main.t1(a, b, c)" {}
8 "CREATE TEMP TABLE temp.t1(a, b, c)" {}
9 "CREATE TEMPORARY TABLE temp.t1(a, b, c)" {}
10 "CREATE TABLE IF NOT EXISTS main.t1(a, b, c)" {}
11 "CREATE TEMP TABLE IF NOT EXISTS temp.t1(a, b, c)" {}
12 "CREATE TEMPORARY TABLE IF NOT EXISTS temp.t1(a, b, c)" {}
13 "CREATE TABLE t1 AS SELECT * FROM t2" {}
14 "CREATE TEMP TABLE t1 AS SELECT c, b, a FROM t2" {}
15 "CREATE TABLE t1 AS SELECT count(*), max(b), min(a) FROM t2" {}
}
# EVIDENCE-OF: R-32138-02228 -- syntax diagram foreign-key-clause
#
# 1: Explicit parent-key columns.
# 2: Implicit child-key columns.
#
# 1: MATCH FULL
# 2: MATCH PARTIAL
# 3: MATCH SIMPLE
# 4: MATCH STICK
# 5:
#
# 1: ON DELETE SET NULL
# 2: ON DELETE SET DEFAULT
# 3: ON DELETE CASCADE
# 4: ON DELETE RESTRICT
# 5: ON DELETE NO ACTION
# 6:
#
# 1: ON UPDATE SET NULL
# 2: ON UPDATE SET DEFAULT
# 3: ON UPDATE CASCADE
# 4: ON UPDATE RESTRICT
# 5: ON UPDATE NO ACTION
# 6:
#
# 1: NOT DEFERRABLE INITIALLY DEFERRED
# 2: NOT DEFERRABLE INITIALLY IMMEDIATE
# 3: NOT DEFERRABLE
# 4: DEFERRABLE INITIALLY DEFERRED
# 5: DEFERRABLE INITIALLY IMMEDIATE
# 6: DEFERRABLE
# 7:
#
do_createtable_tests 0.6.1 -repair {
drop_all_tables
execsql { CREATE TABLE t2(x PRIMARY KEY, y) }
execsql { CREATE TABLE t3(i, j, UNIQUE(i, j) ) }
} {
11146 { CREATE TABLE t1(a
REFERENCES t2(x) MATCH FULL
ON DELETE SET NULL ON UPDATE RESTRICT DEFERRABLE
)} {}
11412 { CREATE TABLE t1(a
REFERENCES t2(x)
ON DELETE RESTRICT ON UPDATE SET NULL MATCH FULL
NOT DEFERRABLE INITIALLY IMMEDIATE
)} {}
12135 { CREATE TABLE t1(a
REFERENCES t2(x) MATCH PARTIAL
ON DELETE SET NULL ON UPDATE CASCADE DEFERRABLE INITIALLY IMMEDIATE
)} {}
12427 { CREATE TABLE t1(a
REFERENCES t2(x) MATCH PARTIAL
ON DELETE RESTRICT ON UPDATE SET DEFAULT
)} {}
12446 { CREATE TABLE t1(a
REFERENCES t2(x) MATCH PARTIAL
ON DELETE RESTRICT ON UPDATE RESTRICT DEFERRABLE
)} {}
12522 { CREATE TABLE t1(a
REFERENCES t2(x) MATCH PARTIAL
ON DELETE NO ACTION ON UPDATE SET DEFAULT NOT DEFERRABLE INITIALLY IMMEDIATE
)} {}
13133 { CREATE TABLE t1(a
REFERENCES t2(x) MATCH SIMPLE
ON DELETE SET NULL ON UPDATE CASCADE NOT DEFERRABLE
)} {}
13216 { CREATE TABLE t1(a
REFERENCES t2(x) MATCH SIMPLE
ON DELETE SET DEFAULT ON UPDATE SET NULL DEFERRABLE
)} {}
13263 { CREATE TABLE t1(a
REFERENCES t2(x) MATCH SIMPLE
ON DELETE SET DEFAULT NOT DEFERRABLE
)} {}
13421 { CREATE TABLE t1(a
REFERENCES t2(x) MATCH SIMPLE
ON DELETE RESTRICT ON UPDATE SET DEFAULT NOT DEFERRABLE INITIALLY DEFERRED
)} {}
13432 { CREATE TABLE t1(a
REFERENCES t2(x) MATCH SIMPLE
ON DELETE RESTRICT ON UPDATE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE
)} {}
13523 { CREATE TABLE t1(a
REFERENCES t2(x) MATCH SIMPLE
ON DELETE NO ACTION ON UPDATE SET DEFAULT NOT DEFERRABLE
)} {}
14336 { CREATE TABLE t1(a
REFERENCES t2(x) MATCH STICK
ON DELETE CASCADE ON UPDATE CASCADE DEFERRABLE
)} {}
14611 { CREATE TABLE t1(a
REFERENCES t2(x) MATCH STICK
ON UPDATE SET NULL NOT DEFERRABLE INITIALLY DEFERRED
)} {}
15155 { CREATE TABLE t1(a
REFERENCES t2(x)
ON DELETE SET NULL ON UPDATE NO ACTION DEFERRABLE INITIALLY IMMEDIATE
)} {}
15453 { CREATE TABLE t1(a
REFERENCES t2(x) ON DELETE RESTRICT ON UPDATE NO ACTION NOT DEFERRABLE
)} {}
15661 { CREATE TABLE t1(a
REFERENCES t2(x) NOT DEFERRABLE INITIALLY DEFERRED
)} {}
21115 { CREATE TABLE t1(a
REFERENCES t2 MATCH FULL
ON DELETE SET NULL ON UPDATE SET NULL DEFERRABLE INITIALLY IMMEDIATE
)} {}
21123 { CREATE TABLE t1(a
REFERENCES t2 MATCH FULL
ON DELETE SET NULL ON UPDATE SET DEFAULT NOT DEFERRABLE
)} {}
21217 { CREATE TABLE t1(a
REFERENCES t2 MATCH FULL ON DELETE SET DEFAULT ON UPDATE SET NULL
)} {}
21362 { CREATE TABLE t1(a
REFERENCES t2 MATCH FULL
ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE
)} {}
22143 { CREATE TABLE t1(a
REFERENCES t2 MATCH PARTIAL
ON DELETE SET NULL ON UPDATE RESTRICT NOT DEFERRABLE
)} {}
22156 { CREATE TABLE t1(a
REFERENCES t2 MATCH PARTIAL
ON DELETE SET NULL ON UPDATE NO ACTION DEFERRABLE
)} {}
22327 { CREATE TABLE t1(a
REFERENCES t2 MATCH PARTIAL ON DELETE CASCADE ON UPDATE SET DEFAULT
)} {}
22663 { CREATE TABLE t1(a
REFERENCES t2 MATCH PARTIAL NOT DEFERRABLE
)} {}
23236 { CREATE TABLE t1(a
REFERENCES t2 MATCH SIMPLE
ON DELETE SET DEFAULT ON UPDATE CASCADE DEFERRABLE
)} {}
24155 { CREATE TABLE t1(a
REFERENCES t2 MATCH STICK
ON DELETE SET NULL ON UPDATE NO ACTION DEFERRABLE INITIALLY IMMEDIATE
)} {}
24522 { CREATE TABLE t1(a
REFERENCES t2 MATCH STICK
ON DELETE NO ACTION ON UPDATE SET DEFAULT NOT DEFERRABLE INITIALLY IMMEDIATE
)} {}
24625 { CREATE TABLE t1(a
REFERENCES t2 MATCH STICK
ON UPDATE SET DEFAULT DEFERRABLE INITIALLY IMMEDIATE
)} {}
25454 { CREATE TABLE t1(a
REFERENCES t2
ON DELETE RESTRICT ON UPDATE NO ACTION DEFERRABLE INITIALLY DEFERRED
)} {}
}
#-------------------------------------------------------------------------
# Test cases e_createtable-1.* - test statements related to table and
# database names, the TEMP and TEMPORARY keywords, and the IF NOT EXISTS
# clause.
#
drop_all_tables
forcedelete test.db2 test.db3
do_execsql_test e_createtable-1.0 {
ATTACH 'test.db2' AS auxa;
ATTACH 'test.db3' AS auxb;
} {}
# EVIDENCE-OF: R-17899-04554 Table names that begin with "sqlite_" are
# reserved for internal use. It is an error to attempt to create a table
# with a name that starts with "sqlite_".
#
do_createtable_tests 1.1.1 -error {
object name reserved for internal use: %s
} {
1 "CREATE TABLE sqlite_abc(a, b, c)" sqlite_abc
2 "CREATE TABLE temp.sqlite_helloworld(x)" sqlite_helloworld
3 {CREATE TABLE auxa."sqlite__"(x, y)} sqlite__
4 {CREATE TABLE auxb."sqlite_"(z)} sqlite_
5 {CREATE TABLE "SQLITE_TBL"(z)} SQLITE_TBL
}
do_createtable_tests 1.1.2 {
1 "CREATE TABLE sqlit_abc(a, b, c)" {}
2 "CREATE TABLE temp.sqlitehelloworld(x)" {}
3 {CREATE TABLE auxa."sqlite"(x, y)} {}
4 {CREATE TABLE auxb."sqlite-"(z)} {}
5 {CREATE TABLE "SQLITE-TBL"(z)} {}
}
# EVIDENCE-OF: R-10195-31023 If a <database-name> is specified, it
# must be either "main", "temp", or the name of an attached database.
#
# EVIDENCE-OF: R-39822-07822 In this case the new table is created in
# the named database.
#
# Test cases 1.2.* test the first of the two requirements above. The
# second is verified by cases 1.3.*.
#
do_createtable_tests 1.2.1 -error {
unknown database %s
} {
1 "CREATE TABLE george.t1(a, b)" george
2 "CREATE TABLE _.t1(a, b)" _
}
do_createtable_tests 1.2.2 {
1 "CREATE TABLE main.abc(a, b, c)" {}
2 "CREATE TABLE temp.helloworld(x)" {}
3 {CREATE TABLE auxa."t 1"(x, y)} {}
4 {CREATE TABLE auxb.xyz(z)} {}
}
drop_all_tables
do_createtable_tests 1.3 -tclquery {
unset -nocomplain X
array set X [table_list]
list $X(main) $X(temp) $X(auxa) $X(auxb)
} {
1 "CREATE TABLE main.abc(a, b, c)" {abc {} {} {}}
2 "CREATE TABLE main.t1(a, b, c)" {{abc t1} {} {} {}}
3 "CREATE TABLE temp.tmp(a, b, c)" {{abc t1} tmp {} {}}
4 "CREATE TABLE auxb.tbl(x, y)" {{abc t1} tmp {} tbl}
5 "CREATE TABLE auxb.t1(k, v)" {{abc t1} tmp {} {t1 tbl}}
6 "CREATE TABLE auxa.next(c, d)" {{abc t1} tmp next {t1 tbl}}
}
# EVIDENCE-OF: R-18895-27365 If the "TEMP" or "TEMPORARY" keyword occurs
# between the "CREATE" and "TABLE" then the new table is created in the
# temp database.
#
drop_all_tables
do_createtable_tests 1.4 -tclquery {
unset -nocomplain X
array set X [table_list]
list $X(main) $X(temp) $X(auxa) $X(auxb)
} {
1 "CREATE TEMP TABLE t1(a, b)" {{} t1 {} {}}
2 "CREATE TEMPORARY TABLE t2(a, b)" {{} {t1 t2} {} {}}
}
# EVIDENCE-OF: R-49439-47561 It is an error to specify both a
# <database-name> and the TEMP or TEMPORARY keyword, unless the
# <database-name> is "temp".
#
drop_all_tables
do_createtable_tests 1.5.1 -error {
temporary table name must be unqualified
} {
1 "CREATE TEMP TABLE main.t1(a, b)" {}
2 "CREATE TEMPORARY TABLE auxa.t2(a, b)" {}
3 "CREATE TEMP TABLE auxb.t3(a, b)" {}
4 "CREATE TEMPORARY TABLE main.xxx(x)" {}
}
drop_all_tables
do_createtable_tests 1.5.2 -tclquery {
unset -nocomplain X
array set X [table_list]
list $X(main) $X(temp) $X(auxa) $X(auxb)
} {
1 "CREATE TEMP TABLE temp.t1(a, b)" {{} t1 {} {}}
2 "CREATE TEMPORARY TABLE temp.t2(a, b)" {{} {t1 t2} {} {}}
3 "CREATE TEMP TABLE TEMP.t3(a, b)" {{} {t1 t2 t3} {} {}}
4 "CREATE TEMPORARY TABLE TEMP.xxx(x)" {{} {t1 t2 t3 xxx} {} {}}
}
# EVIDENCE-OF: R-00917-09393 If no database name is specified and the
# TEMP keyword is not present then the table is created in the main
# database.
#
drop_all_tables
do_createtable_tests 1.6 -tclquery {
unset -nocomplain X
array set X [table_list]
list $X(main) $X(temp) $X(auxa) $X(auxb)
} {
1 "CREATE TABLE t1(a, b)" {t1 {} {} {}}
2 "CREATE TABLE t2(a, b)" {{t1 t2} {} {} {}}
3 "CREATE TABLE t3(a, b)" {{t1 t2 t3} {} {} {}}
4 "CREATE TABLE xxx(x)" {{t1 t2 t3 xxx} {} {} {}}
}
drop_all_tables
do_execsql_test e_createtable-1.7.0 {
CREATE TABLE t1(x, y);
CREATE INDEX i1 ON t1(x);
CREATE VIEW v1 AS SELECT * FROM t1;
CREATE TABLE auxa.tbl1(x, y);
CREATE INDEX auxa.idx1 ON tbl1(x);
CREATE VIEW auxa.view1 AS SELECT * FROM tbl1;
} {}
# EVIDENCE-OF: R-01232-54838 It is usually an error to attempt to create
# a new table in a database that already contains a table, index or view
# of the same name.
#
# Test cases 1.7.1.* verify that creating a table in a database with a
# table/index/view of the same name does fail. 1.7.2.* tests that creating
# a table with the same name as a table/index/view in a different database
# is Ok.
#
do_createtable_tests 1.7.1 -error { %s } {
1 "CREATE TABLE t1(a, b)" {{table t1 already exists}}
2 "CREATE TABLE i1(a, b)" {{there is already an index named i1}}
3 "CREATE TABLE v1(a, b)" {{table v1 already exists}}
4 "CREATE TABLE auxa.tbl1(a, b)" {{table tbl1 already exists}}
5 "CREATE TABLE auxa.idx1(a, b)" {{there is already an index named idx1}}
6 "CREATE TABLE auxa.view1(a, b)" {{table view1 already exists}}
}
do_createtable_tests 1.7.2 {
1 "CREATE TABLE auxa.t1(a, b)" {}
2 "CREATE TABLE auxa.i1(a, b)" {}
3 "CREATE TABLE auxa.v1(a, b)" {}
4 "CREATE TABLE tbl1(a, b)" {}
5 "CREATE TABLE idx1(a, b)" {}
6 "CREATE TABLE view1(a, b)" {}
}
# EVIDENCE-OF: R-33917-24086 However, if the "IF NOT EXISTS" clause is
# specified as part of the CREATE TABLE statement and a table or view of
# the same name already exists, the CREATE TABLE command simply has no
# effect (and no error message is returned).
#
drop_all_tables
do_execsql_test e_createtable-1.8.0 {
CREATE TABLE t1(x, y);
CREATE INDEX i1 ON t1(x);
CREATE VIEW v1 AS SELECT * FROM t1;
CREATE TABLE auxa.tbl1(x, y);
CREATE INDEX auxa.idx1 ON tbl1(x);
CREATE VIEW auxa.view1 AS SELECT * FROM tbl1;
} {}
do_createtable_tests 1.8 {
1 "CREATE TABLE IF NOT EXISTS t1(a, b)" {}
2 "CREATE TABLE IF NOT EXISTS auxa.tbl1(a, b)" {}
3 "CREATE TABLE IF NOT EXISTS v1(a, b)" {}
4 "CREATE TABLE IF NOT EXISTS auxa.view1(a, b)" {}
}
# EVIDENCE-OF: R-16465-40078 An error is still returned if the table
# cannot be created because of an existing index, even if the "IF NOT
# EXISTS" clause is specified.
#
do_createtable_tests 1.9 -error { %s } {
1 "CREATE TABLE IF NOT EXISTS i1(a, b)"
{{there is already an index named i1}}
2 "CREATE TABLE IF NOT EXISTS auxa.idx1(a, b)"
{{there is already an index named idx1}}
}
# EVIDENCE-OF: R-05513-33819 It is not an error to create a table that
# has the same name as an existing trigger.
#
drop_all_tables
do_execsql_test e_createtable-1.10.0 {
CREATE TABLE t1(x, y);
CREATE TABLE auxb.t2(x, y);
CREATE TRIGGER tr1 AFTER INSERT ON t1 BEGIN
SELECT 1;
END;
CREATE TRIGGER auxb.tr2 AFTER INSERT ON t2 BEGIN
SELECT 1;
END;
} {}
do_createtable_tests 1.10 {
1 "CREATE TABLE tr1(a, b)" {}
2 "CREATE TABLE tr2(a, b)" {}
3 "CREATE TABLE auxb.tr1(a, b)" {}
4 "CREATE TABLE auxb.tr2(a, b)" {}
}
# EVIDENCE-OF: R-22283-14179 Tables are removed using the DROP TABLE
# statement.
#
drop_all_tables
do_execsql_test e_createtable-1.11.0 {
CREATE TABLE t1(a, b);
CREATE TABLE t2(a, b);
CREATE TABLE auxa.t3(a, b);
CREATE TABLE auxa.t4(a, b);
} {}
do_execsql_test e_createtable-1.11.1.1 {
SELECT * FROM t1;
SELECT * FROM t2;
SELECT * FROM t3;
SELECT * FROM t4;
} {}
do_execsql_test e_createtable-1.11.1.2 { DROP TABLE t1 } {}
do_catchsql_test e_createtable-1.11.1.3 {
SELECT * FROM t1
} {1 {no such table: t1}}
do_execsql_test e_createtable-1.11.1.4 { DROP TABLE t3 } {}
do_catchsql_test e_createtable-1.11.1.5 {
SELECT * FROM t3
} {1 {no such table: t3}}
do_execsql_test e_createtable-1.11.2.1 {
SELECT name FROM sqlite_master;
SELECT name FROM auxa.sqlite_master;
} {t2 t4}
do_execsql_test e_createtable-1.11.2.2 { DROP TABLE t2 } {}
do_execsql_test e_createtable-1.11.2.3 { DROP TABLE t4 } {}
do_execsql_test e_createtable-1.11.2.4 {
SELECT name FROM sqlite_master;
SELECT name FROM auxa.sqlite_master;
} {}
#-------------------------------------------------------------------------
# Test cases e_createtable-2.* - test statements related to the CREATE
# TABLE AS ... SELECT statement.
#
# Three Tcl commands:
#
# select_column_names SQL
# The argument must be a SELECT statement. Return a list of the names
# of the columns of the result-set that would be returned by executing
# the SELECT.
#
# table_column_names TBL
# The argument must be a table name. Return a list of column names, from
# left to right, for the table.
#
# table_column_decltypes TBL
# The argument must be a table name. Return a list of column declared
# types, from left to right, for the table.
#
proc sci {select cmd} {
set res [list]
set STMT [sqlite3_prepare_v2 db $select -1 dummy]
for {set i 0} {$i < [sqlite3_column_count $STMT]} {incr i} {
lappend res [$cmd $STMT $i]
}
sqlite3_finalize $STMT
set res
}
proc tci {tbl cmd} { sci "SELECT * FROM $tbl" $cmd }
proc select_column_names {sql} { sci $sql sqlite3_column_name }
proc table_column_names {tbl} { tci $tbl sqlite3_column_name }
proc table_column_decltypes {tbl} { tci $tbl sqlite3_column_decltype }
# Create a database schema. This schema is used by tests 2.1.* through 2.3.*.
#
drop_all_tables
do_execsql_test e_createtable-2.0 {
CREATE TABLE t1(a, b, c);
CREATE TABLE t2(d, e, f);
CREATE TABLE t3(g BIGINT, h VARCHAR(10));
CREATE TABLE t4(i BLOB, j ANYOLDATA);
CREATE TABLE t5(k FLOAT, l INTEGER);
CREATE TABLE t6(m DEFAULT 10, n DEFAULT 5, PRIMARY KEY(m, n));
CREATE TABLE t7(x INTEGER PRIMARY KEY);
CREATE TABLE t8(o COLLATE nocase DEFAULT 'abc');
CREATE TABLE t9(p NOT NULL, q DOUBLE CHECK (q!=0), r STRING UNIQUE);
} {}
# EVIDENCE-OF: R-64828-59568 The table has the same number of columns as
# the rows returned by the SELECT statement. The name of each column is
# the same as the name of the corresponding column in the result set of
# the SELECT statement.
#
do_createtable_tests 2.1 -tclquery {
table_column_names x1
} -repair {
catchsql { DROP TABLE x1 }
} {
1 "CREATE TABLE x1 AS SELECT * FROM t1" {a b c}
2 "CREATE TABLE x1 AS SELECT c, b, a FROM t1" {c b a}
3 "CREATE TABLE x1 AS SELECT * FROM t1, t2" {a b c d e f}
4 "CREATE TABLE x1 AS SELECT count(*) FROM t1" {count(*)}
5 "CREATE TABLE x1 AS SELECT count(a) AS a, max(b) FROM t1" {a max(b)}
}
# EVIDENCE-OF: R-37111-22855 The declared type of each column is
# determined by the expression affinity of the corresponding expression
# in the result set of the SELECT statement, as follows: Expression
# Affinity Column Declared Type TEXT "TEXT" NUMERIC "NUM" INTEGER "INT"
# REAL "REAL" NONE "" (empty string)
#
do_createtable_tests 2.2 -tclquery {
table_column_decltypes x1
} -repair {
catchsql { DROP TABLE x1 }
} {
1 "CREATE TABLE x1 AS SELECT a FROM t1" {""}
2 "CREATE TABLE x1 AS SELECT * FROM t3" {INT TEXT}
3 "CREATE TABLE x1 AS SELECT * FROM t4" {"" NUM}
4 "CREATE TABLE x1 AS SELECT * FROM t5" {REAL INT}
}
# EVIDENCE-OF: R-16667-09772 A table created using CREATE TABLE AS has
# no PRIMARY KEY and no constraints of any kind. The default value of
# each column is NULL. The default collation sequence for each column of
# the new table is BINARY.
#
# The following tests create tables based on SELECT statements that read
# from tables that have primary keys, constraints and explicit default
# collation sequences. None of this is transfered to the definition of
# the new table as stored in the sqlite_master table.
#
# Tests 2.3.2.* show that the default value of each column is NULL.
#
do_createtable_tests 2.3.1 -query {
SELECT sql FROM sqlite_master ORDER BY rowid DESC LIMIT 1
} {
1 "CREATE TABLE x1 AS SELECT * FROM t6" {{CREATE TABLE x1(m,n)}}
2 "CREATE TABLE x2 AS SELECT * FROM t7" {{CREATE TABLE x2(x INT)}}
3 "CREATE TABLE x3 AS SELECT * FROM t8" {{CREATE TABLE x3(o)}}
4 "CREATE TABLE x4 AS SELECT * FROM t9" {{CREATE TABLE x4(p,q REAL,r NUM)}}
}
do_execsql_test e_createtable-2.3.2.1 {
INSERT INTO x1 DEFAULT VALUES;
INSERT INTO x2 DEFAULT VALUES;
INSERT INTO x3 DEFAULT VALUES;
INSERT INTO x4 DEFAULT VALUES;
} {}
db nullvalue null
do_execsql_test e_createtable-2.3.2.2 { SELECT * FROM x1 } {null null}
do_execsql_test e_createtable-2.3.2.3 { SELECT * FROM x2 } {null}
do_execsql_test e_createtable-2.3.2.4 { SELECT * FROM x3 } {null}
do_execsql_test e_createtable-2.3.2.5 { SELECT * FROM x4 } {null null null}
db nullvalue {}
drop_all_tables
do_execsql_test e_createtable-2.4.0 {
CREATE TABLE t1(x, y);
INSERT INTO t1 VALUES('i', 'one');
INSERT INTO t1 VALUES('ii', 'two');
INSERT INTO t1 VALUES('iii', 'three');
} {}
# EVIDENCE-OF: R-24153-28352 Tables created using CREATE TABLE AS are
# initially populated with the rows of data returned by the SELECT
# statement.
#
# EVIDENCE-OF: R-08224-30249 Rows are assigned contiguously ascending
# rowid values, starting with 1, in the order that they are returned by
# the SELECT statement.
#
# Each test case below is specified as the name of a table to create
# using "CREATE TABLE ... AS SELECT ..." and a SELECT statement to use in
# creating it. The table is created.
#
# Test cases 2.4.*.1 check that after it has been created, the data in the
# table is the same as the data returned by the SELECT statement executed as
# a standalone command, verifying the first testable statement above.
#
# Test cases 2.4.*.2 check that the rowids were allocated contiguously
# as required by the second testable statement above. That the rowids
# from the contiguous block were allocated to rows in the order rows are
# returned by the SELECT statement is verified by 2.4.*.1.
#
# EVIDENCE-OF: R-32365-09043 A "CREATE TABLE ... AS SELECT" statement
# creates and populates a database table based on the results of a
# SELECT statement.
#
# The above is also considered to be tested by the following. It is
# clear that tables are being created and populated by the command in
# question.
#
foreach {tn tbl select} {
1 x1 "SELECT * FROM t1"
2 x2 "SELECT * FROM t1 ORDER BY x DESC"
3 x3 "SELECT * FROM t1 ORDER BY x ASC"
} {
# Create the table using a "CREATE TABLE ... AS SELECT ..." command.
execsql [subst {CREATE TABLE $tbl AS $select}]
# Check that the rows inserted into the table, sorted in ascending rowid
# order, match those returned by executing the SELECT statement as a
# standalone command.
do_execsql_test e_createtable-2.4.$tn.1 [subst {
SELECT * FROM $tbl ORDER BY rowid;
}] [execsql $select]
# Check that the rowids in the new table are a contiguous block starting
# with rowid 1. Note that this will fail if SELECT statement $select
# returns 0 rows (as max(rowid) will be NULL).
do_execsql_test e_createtable-2.4.$tn.2 [subst {
SELECT min(rowid), count(rowid)==max(rowid) FROM $tbl
}] {1 1}
}
#--------------------------------------------------------------------------
# Test cases for column defintions in CREATE TABLE statements that do not
# use a SELECT statement. Not including data constraints. In other words,
# tests for the specification of:
#
# * declared types,
# * default values, and
# * default collation sequences.
#
# EVIDENCE-OF: R-27219-49057 Unlike most SQL databases, SQLite does not
# restrict the type of data that may be inserted into a column based on
# the columns declared type.
#
# Test this by creating a few tables with varied declared types, then
# inserting various different types of values into them.
#
drop_all_tables
do_execsql_test e_createtable-3.1.0 {
CREATE TABLE t1(x VARCHAR(10), y INTEGER, z DOUBLE);
CREATE TABLE t2(a DATETIME, b STRING, c REAL);
CREATE TABLE t3(o, t);
} {}
# value type -> declared column type
# ----------------------------------
# integer -> VARCHAR(10)
# string -> INTEGER
# blob -> DOUBLE
#
do_execsql_test e_createtable-3.1.1 {
INSERT INTO t1 VALUES(14, 'quite a lengthy string', X'555655');
SELECT * FROM t1;
} {14 {quite a lengthy string} UVU}
# string -> DATETIME
# integer -> STRING
# time -> REAL
#
do_execsql_test e_createtable-3.1.2 {
INSERT INTO t2 VALUES('not a datetime', 13, '12:41:59');
SELECT * FROM t2;
} {{not a datetime} 13 12:41:59}
# EVIDENCE-OF: R-10565-09557 The declared type of a column is used to
# determine the affinity of the column only.
#
# Affinities are tested in more detail elsewhere (see document
# datatype3.html). Here, just test that affinity transformations
# consistent with the expected affinity of each column (based on
# the declared type) appear to take place.
#
# Affinities of t1 (test cases 3.2.1.*): TEXT, INTEGER, REAL
# Affinities of t2 (test cases 3.2.2.*): NUMERIC, NUMERIC, REAL
# Affinities of t3 (test cases 3.2.3.*): NONE, NONE
#
do_execsql_test e_createtable-3.2.0 { DELETE FROM t1; DELETE FROM t2; } {}
do_createtable_tests 3.2.1 -query {
SELECT quote(x), quote(y), quote(z) FROM t1 ORDER BY rowid DESC LIMIT 1;
} {
1 "INSERT INTO t1 VALUES(15, '22.0', '14')" {'15' 22 14.0}
2 "INSERT INTO t1 VALUES(22.0, 22.0, 22.0)" {'22.0' 22 22.0}
}
do_createtable_tests 3.2.2 -query {
SELECT quote(a), quote(b), quote(c) FROM t2 ORDER BY rowid DESC LIMIT 1;
} {
1 "INSERT INTO t2 VALUES(15, '22.0', '14')" {15 22 14.0}
2 "INSERT INTO t2 VALUES(22.0, 22.0, 22.0)" {22 22 22.0}
}
do_createtable_tests 3.2.3 -query {
SELECT quote(o), quote(t) FROM t3 ORDER BY rowid DESC LIMIT 1;
} {
1 "INSERT INTO t3 VALUES('15', '22.0')" {'15' '22.0'}
2 "INSERT INTO t3 VALUES(15, 22.0)" {15 22.0}
}
# EVIDENCE-OF: R-42316-09582 If there is no explicit DEFAULT clause
# attached to a column definition, then the default value of the column
# is NULL.
#
# None of the columns in table t1 have an explicit DEFAULT clause.
# So testing that the default value of all columns in table t1 is
# NULL serves to verify the above.
#
do_createtable_tests 3.2.3 -query {
SELECT quote(x), quote(y), quote(z) FROM t1
} -repair {
execsql { DELETE FROM t1 }
} {
1 "INSERT INTO t1(x, y) VALUES('abc', 'xyz')" {'abc' 'xyz' NULL}
2 "INSERT INTO t1(x, z) VALUES('abc', 'xyz')" {'abc' NULL 'xyz'}
3 "INSERT INTO t1 DEFAULT VALUES" {NULL NULL NULL}
}
# EVIDENCE-OF: R-62940-43005 An explicit DEFAULT clause may specify that
# the default value is NULL, a string constant, a blob constant, a
# signed-number, or any constant expression enclosed in parentheses. An
# explicit default value may also be one of the special case-independent
# keywords CURRENT_TIME, CURRENT_DATE or CURRENT_TIMESTAMP.
#
do_execsql_test e_createtable-3.3.1 {
CREATE TABLE t4(
a DEFAULT NULL,
b DEFAULT 'string constant',
c DEFAULT X'424C4F42',
d DEFAULT 1,
e DEFAULT -1,
f DEFAULT 3.14,
g DEFAULT -3.14,
h DEFAULT ( substr('abcd', 0, 2) || 'cd' ),
i DEFAULT CURRENT_TIME,
j DEFAULT CURRENT_DATE,
k DEFAULT CURRENT_TIMESTAMP
);
} {}
# EVIDENCE-OF: R-10288-43169 For the purposes of the DEFAULT clause, an
# expression is considered constant provided that it does not contain
# any sub-queries or string constants enclosed in double quotes.
#
do_createtable_tests 3.4.1 -error {
default value of column [x] is not constant
} {
1 {CREATE TABLE t5(x DEFAULT ( (SELECT 1) ))} {}
2 {CREATE TABLE t5(x DEFAULT ( "abc" ))} {}
3 {CREATE TABLE t5(x DEFAULT ( 1 IN (SELECT 1) ))} {}
4 {CREATE TABLE t5(x DEFAULT ( EXISTS (SELECT 1) ))} {}
}
do_createtable_tests 3.4.2 -repair {
catchsql { DROP TABLE t5 }
} {
1 {CREATE TABLE t5(x DEFAULT ( 'abc' ))} {}
2 {CREATE TABLE t5(x DEFAULT ( 1 IN (1, 2, 3) ))} {}
}
# EVIDENCE-OF: R-18814-23501 Each time a row is inserted into the table
# by an INSERT statement that does not provide explicit values for all
# table columns the values stored in the new row are determined by their
# default values
#
# Verify this with some assert statements for which all, some and no
# columns lack explicit values.
#
set sqlite_current_time 1000000000
do_createtable_tests 3.5 -query {
SELECT quote(a), quote(b), quote(c), quote(d), quote(e), quote(f),
quote(g), quote(h), quote(i), quote(j), quote(k)
FROM t4 ORDER BY rowid DESC LIMIT 1;
} {
1 "INSERT INTO t4 DEFAULT VALUES" {
NULL {'string constant'} X'424C4F42' 1 -1 3.14 -3.14
'acd' '01:46:40' '2001-09-09' {'2001-09-09 01:46:40'}
}
2 "INSERT INTO t4(a, b, c) VALUES(1, 2, 3)" {
1 2 3 1 -1 3.14 -3.14 'acd' '01:46:40' '2001-09-09' {'2001-09-09 01:46:40'}
}
3 "INSERT INTO t4(k, j, i) VALUES(1, 2, 3)" {
NULL {'string constant'} X'424C4F42' 1 -1 3.14 -3.14 'acd' 3 2 1
}
4 "INSERT INTO t4(a,b,c,d,e,f,g,h,i,j,k) VALUES(1,2,3,4,5,6,7,8,9,10,11)" {
1 2 3 4 5 6 7 8 9 10 11
}
}
# EVIDENCE-OF: R-12572-62501 If the default value of the column is a
# constant NULL, text, blob or signed-number value, then that value is
# used directly in the new row.
#
do_execsql_test e_createtable-3.6.1 {
CREATE TABLE t5(
a DEFAULT NULL,
b DEFAULT 'text value',
c DEFAULT X'424C4F42',
d DEFAULT -45678.6,
e DEFAULT 394507
);
} {}
do_execsql_test e_createtable-3.6.2 {
INSERT INTO t5 DEFAULT VALUES;
SELECT quote(a), quote(b), quote(c), quote(d), quote(e) FROM t5;
} {NULL {'text value'} X'424C4F42' -45678.6 394507}
# EVIDENCE-OF: R-60616-50251 If the default value of a column is an
# expression in parentheses, then the expression is evaluated once for
# each row inserted and the results used in the new row.
#
# Test case 3.6.4 demonstrates that the expression is evaluated
# separately for each row if the INSERT is an "INSERT INTO ... SELECT ..."
# command.
#
set ::nextint 0
proc nextint {} { incr ::nextint }
db func nextint nextint
do_execsql_test e_createtable-3.7.1 {
CREATE TABLE t6(a DEFAULT ( nextint() ), b DEFAULT ( nextint() ));
} {}
do_execsql_test e_createtable-3.7.2 {
INSERT INTO t6 DEFAULT VALUES;
SELECT quote(a), quote(b) FROM t6;
} {1 2}
do_execsql_test e_createtable-3.7.3 {
INSERT INTO t6(a) VALUES('X');
SELECT quote(a), quote(b) FROM t6;
} {1 2 'X' 3}
do_execsql_test e_createtable-3.7.4 {
INSERT INTO t6(a) SELECT a FROM t6;
SELECT quote(a), quote(b) FROM t6;
} {1 2 'X' 3 1 4 'X' 5}
# EVIDENCE-OF: R-18683-56219 If the default value of a column is
# CURRENT_TIME, CURRENT_DATE or CURRENT_DATETIME, then the value used in
# the new row is a text representation of the current UTC date and/or
# time.
#
# This is difficult to test literally without knowing what time the
# user will run the tests. Instead, we test that the three cases
# above set the value to the current date and/or time according to
# the xCurrentTime() method of the VFS. Which is usually the same
# as UTC. In this case, however, we instrument it to always return
# a time equivalent to "2001-09-09 01:46:40 UTC".
#
set sqlite_current_time 1000000000
do_execsql_test e_createtable-3.8.1 {
CREATE TABLE t7(
a DEFAULT CURRENT_TIME,
b DEFAULT CURRENT_DATE,
c DEFAULT CURRENT_TIMESTAMP
);
} {}
do_execsql_test e_createtable-3.8.2 {
INSERT INTO t7 DEFAULT VALUES;
SELECT quote(a), quote(b), quote(c) FROM t7;
} {'01:46:40' '2001-09-09' {'2001-09-09 01:46:40'}}
# EVIDENCE-OF: R-62327-53843 For CURRENT_TIME, the format of the value
# is "HH:MM:SS".
#
# EVIDENCE-OF: R-03775-43471 For CURRENT_DATE, "YYYY-MM-DD".
#
# EVIDENCE-OF: R-07677-44926 The format for CURRENT_TIMESTAMP is
# "YYYY-MM-DD HH:MM:SS".
#
# The three above are demonstrated by tests 1, 2 and 3 below.
# Respectively.
#
do_createtable_tests 3.8.3 -query {
SELECT a, b, c FROM t7 ORDER BY rowid DESC LIMIT 1;
} {
1 "INSERT INTO t7(b, c) VALUES('x', 'y')" {01:46:40 x y}
2 "INSERT INTO t7(c, a) VALUES('x', 'y')" {y 2001-09-09 x}
3 "INSERT INTO t7(a, b) VALUES('x', 'y')" {x y {2001-09-09 01:46:40}}
}
# EVIDENCE-OF: R-55061-47754 The COLLATE clause specifies the name of a
# collating sequence to use as the default collation sequence for the
# column.
#
# EVIDENCE-OF: R-40275-54363 If no COLLATE clause is specified, the
# default collation sequence is BINARY.
#
do_execsql_test e_createtable-3-9.1 {
CREATE TABLE t8(a COLLATE nocase, b COLLATE rtrim, c COLLATE binary, d);
INSERT INTO t8 VALUES('abc', 'abc', 'abc', 'abc');
INSERT INTO t8 VALUES('abc ', 'abc ', 'abc ', 'abc ');
INSERT INTO t8 VALUES('ABC ', 'ABC ', 'ABC ', 'ABC ');
INSERT INTO t8 VALUES('ABC', 'ABC', 'ABC', 'ABC');
} {}
do_createtable_tests 3.9 {
2 "SELECT a FROM t8 ORDER BY a, rowid" {abc ABC {abc } {ABC }}
3 "SELECT b FROM t8 ORDER BY b, rowid" {{ABC } ABC abc {abc }}
4 "SELECT c FROM t8 ORDER BY c, rowid" {ABC {ABC } abc {abc }}
5 "SELECT d FROM t8 ORDER BY d, rowid" {ABC {ABC } abc {abc }}
}
# EVIDENCE-OF: R-25473-20557 The number of columns in a table is limited
# by the SQLITE_MAX_COLUMN compile-time parameter.
#
proc columns {n} {
set res [list]
for {set i 0} {$i < $n} {incr i} { lappend res "c$i" }
join $res ", "
}
do_execsql_test e_createtable-3.10.1 [subst {
CREATE TABLE t9([columns $::SQLITE_MAX_COLUMN]);
}] {}
do_catchsql_test e_createtable-3.10.2 [subst {
CREATE TABLE t10([columns [expr $::SQLITE_MAX_COLUMN+1]]);
}] {1 {too many columns on t10}}
# EVIDENCE-OF: R-27775-64721 Both of these limits can be lowered at
# runtime using the sqlite3_limit() C/C++ interface.
#
# A 30,000 byte blob consumes 30,003 bytes of record space. A record
# that contains 3 such blobs consumes (30,000*3)+1 bytes of space. Tests
# 3.11.4 and 3.11.5, which verify that SQLITE_MAX_LENGTH may be lowered
# at runtime, are based on this calculation.
#
sqlite3_limit db SQLITE_LIMIT_COLUMN 500
do_execsql_test e_createtable-3.11.1 [subst {
CREATE TABLE t10([columns 500]);
}] {}
do_catchsql_test e_createtable-3.11.2 [subst {
CREATE TABLE t11([columns 501]);
}] {1 {too many columns on t11}}
# Check that it is not possible to raise the column limit above its
# default compile time value.
#
sqlite3_limit db SQLITE_LIMIT_COLUMN [expr $::SQLITE_MAX_COLUMN+2]
do_catchsql_test e_createtable-3.11.3 [subst {
CREATE TABLE t11([columns [expr $::SQLITE_MAX_COLUMN+1]]);
}] {1 {too many columns on t11}}
sqlite3_limit db SQLITE_LIMIT_LENGTH 90010
do_execsql_test e_createtable-3.11.4 {
CREATE TABLE t12(a, b, c);
INSERT INTO t12 VALUES(randomblob(30000),randomblob(30000),randomblob(30000));
} {}
do_catchsql_test e_createtable-3.11.5 {
INSERT INTO t12 VALUES(randomblob(30001),randomblob(30000),randomblob(30000));
} {1 {string or blob too big}}
#-------------------------------------------------------------------------
# Tests for statements regarding constraints (PRIMARY KEY, UNIQUE, NOT
# NULL and CHECK constraints).
#
# EVIDENCE-OF: R-52382-54248 Each table in SQLite may have at most one
# PRIMARY KEY.
#
# EVIDENCE-OF: R-18080-47271 If there is more than one PRIMARY KEY
# clause in a single CREATE TABLE statement, it is an error.
#
# To test the two above, show that zero primary keys is Ok, one primary
# key is Ok, and two or more primary keys is an error.
#
drop_all_tables
do_createtable_tests 4.1.1 {
1 "CREATE TABLE t1(a, b, c)" {}
2 "CREATE TABLE t2(a PRIMARY KEY, b, c)" {}
3 "CREATE TABLE t3(a, b, c, PRIMARY KEY(a))" {}
4 "CREATE TABLE t4(a, b, c, PRIMARY KEY(c,b,a))" {}
}
do_createtable_tests 4.1.2 -error {
table "t5" has more than one primary key
} {
1 "CREATE TABLE t5(a PRIMARY KEY, b PRIMARY KEY, c)" {}
2 "CREATE TABLE t5(a, b PRIMARY KEY, c, PRIMARY KEY(a))" {}
3 "CREATE TABLE t5(a INTEGER PRIMARY KEY, b PRIMARY KEY, c)" {}
4 "CREATE TABLE t5(a INTEGER PRIMARY KEY, b, c, PRIMARY KEY(b, c))" {}
5 "CREATE TABLE t5(a PRIMARY KEY, b, c, PRIMARY KEY(a))" {}
6 "CREATE TABLE t5(a INTEGER PRIMARY KEY, b, c, PRIMARY KEY(a))" {}
}
proc table_pk {tbl} {
set pk [list]
db eval "pragma table_info($tbl)" a {
if {$a(pk)} { lappend pk $a(name) }
}
set pk
}
# EVIDENCE-OF: R-41411-18837 If the keywords PRIMARY KEY are added to a
# column definition, then the primary key for the table consists of that
# single column.
#
# The above is tested by 4.2.1.*
#
# EVIDENCE-OF: R-31775-48204 Or, if a PRIMARY KEY clause is specified as
# a table-constraint, then the primary key of the table consists of the
# list of columns specified as part of the PRIMARY KEY clause.
#
# The above is tested by 4.2.2.*
#
do_createtable_tests 4.2 -repair {
catchsql { DROP TABLE t5 }
} -tclquery {
table_pk t5
} {
1.1 "CREATE TABLE t5(a, b INTEGER PRIMARY KEY, c)" {b}
1.2 "CREATE TABLE t5(a PRIMARY KEY, b, c)" {a}
2.1 "CREATE TABLE t5(a, b, c, PRIMARY KEY(a))" {a}
2.2 "CREATE TABLE t5(a, b, c, PRIMARY KEY(c,b,a))" {a b c}
2.3 "CREATE TABLE t5(a, b INTEGER PRIMARY KEY, c)" {b}
}
# EVIDENCE-OF: R-33986-09410 Each row in a table with a primary key must
# feature a unique combination of values in its primary key columns.
#
# EVIDENCE-OF: R-39102-06737 If an INSERT or UPDATE statement attempts
# to modify the table content so that two or more rows feature identical
# primary key values, it is a constraint violation.
#
drop_all_tables
do_execsql_test 4.3.0 {
CREATE TABLE t1(x PRIMARY KEY, y);
INSERT INTO t1 VALUES(0, 'zero');
INSERT INTO t1 VALUES(45.5, 'one');
INSERT INTO t1 VALUES('brambles', 'two');
INSERT INTO t1 VALUES(X'ABCDEF', 'three');
CREATE TABLE t2(x, y, PRIMARY KEY(x, y));
INSERT INTO t2 VALUES(0, 'zero');
INSERT INTO t2 VALUES(45.5, 'one');
INSERT INTO t2 VALUES('brambles', 'two');
INSERT INTO t2 VALUES(X'ABCDEF', 'three');
} {}
do_createtable_tests 4.3.1 -error { %s not unique } {
1 "INSERT INTO t1 VALUES(0, 0)" {"column x is"}
2 "INSERT INTO t1 VALUES(45.5, 'abc')" {"column x is"}
3 "INSERT INTO t1 VALUES(0.0, 'abc')" {"column x is"}
4 "INSERT INTO t1 VALUES('brambles', 'abc')" {"column x is"}
5 "INSERT INTO t1 VALUES(X'ABCDEF', 'abc')" {"column x is"}
6 "INSERT INTO t2 VALUES(0, 'zero')" {"columns x, y are"}
7 "INSERT INTO t2 VALUES(45.5, 'one')" {"columns x, y are"}
8 "INSERT INTO t2 VALUES(0.0, 'zero')" {"columns x, y are"}
9 "INSERT INTO t2 VALUES('brambles', 'two')" {"columns x, y are"}
10 "INSERT INTO t2 VALUES(X'ABCDEF', 'three')" {"columns x, y are"}
}
do_createtable_tests 4.3.2 {
1 "INSERT INTO t1 VALUES(-1, 0)" {}
2 "INSERT INTO t1 VALUES(45.2, 'abc')" {}
3 "INSERT INTO t1 VALUES(0.01, 'abc')" {}
4 "INSERT INTO t1 VALUES('bramble', 'abc')" {}
5 "INSERT INTO t1 VALUES(X'ABCDEE', 'abc')" {}
6 "INSERT INTO t2 VALUES(0, 0)" {}
7 "INSERT INTO t2 VALUES(45.5, 'abc')" {}
8 "INSERT INTO t2 VALUES(0.0, 'abc')" {}
9 "INSERT INTO t2 VALUES('brambles', 'abc')" {}
10 "INSERT INTO t2 VALUES(X'ABCDEF', 'abc')" {}
}
do_createtable_tests 4.3.3 -error { %s not unique } {
1 "UPDATE t1 SET x=0 WHERE y='two'" {"column x is"}
2 "UPDATE t1 SET x='brambles' WHERE y='three'" {"column x is"}
3 "UPDATE t1 SET x=45.5 WHERE y='zero'" {"column x is"}
4 "UPDATE t1 SET x=X'ABCDEF' WHERE y='one'" {"column x is"}
5 "UPDATE t1 SET x=0.0 WHERE y='three'" {"column x is"}
6 "UPDATE t2 SET x=0, y='zero' WHERE y='two'" {"columns x, y are"}
7 "UPDATE t2 SET x='brambles', y='two' WHERE y='three'"
{"columns x, y are"}
8 "UPDATE t2 SET x=45.5, y='one' WHERE y='zero'" {"columns x, y are"}
9 "UPDATE t2 SET x=X'ABCDEF', y='three' WHERE y='one'"
{"columns x, y are"}
10 "UPDATE t2 SET x=0.0, y='zero' WHERE y='three'"
{"columns x, y are"}
}
# EVIDENCE-OF: R-52572-02078 For the purposes of determining the
# uniqueness of primary key values, NULL values are considered distinct
# from all other values, including other NULLs.
#
do_createtable_tests 4.4 {
1 "INSERT INTO t1 VALUES(NULL, 0)" {}
2 "INSERT INTO t1 VALUES(NULL, 0)" {}
3 "INSERT INTO t1 VALUES(NULL, 0)" {}
4 "INSERT INTO t2 VALUES(NULL, 'zero')" {}
5 "INSERT INTO t2 VALUES(NULL, 'one')" {}
6 "INSERT INTO t2 VALUES(NULL, 'two')" {}
7 "INSERT INTO t2 VALUES(NULL, 'three')" {}
8 "INSERT INTO t2 VALUES(0, NULL)" {}
9 "INSERT INTO t2 VALUES(45.5, NULL)" {}
10 "INSERT INTO t2 VALUES(0.0, NULL)" {}
11 "INSERT INTO t2 VALUES('brambles', NULL)" {}
12 "INSERT INTO t2 VALUES(X'ABCDEF', NULL)" {}
13 "INSERT INTO t2 VALUES(NULL, NULL)" {}
14 "INSERT INTO t2 VALUES(NULL, NULL)" {}
}
# EVIDENCE-OF: R-61866-38053 Unless the column is an INTEGER PRIMARY KEY
# SQLite allows NULL values in a PRIMARY KEY column.
#
# If the column is an integer primary key, attempting to insert a NULL
# into the column triggers the auto-increment behaviour. Attempting
# to use UPDATE to set an ipk column to a NULL value is an error.
#
do_createtable_tests 4.5.1 {
1 "SELECT count(*) FROM t1 WHERE x IS NULL" 3
2 "SELECT count(*) FROM t2 WHERE x IS NULL" 6
3 "SELECT count(*) FROM t2 WHERE y IS NULL" 7
4 "SELECT count(*) FROM t2 WHERE x IS NULL AND y IS NULL" 2
}
do_execsql_test 4.5.2 {
CREATE TABLE t3(s, u INTEGER PRIMARY KEY, v);
INSERT INTO t3 VALUES(1, NULL, 2);
INSERT INTO t3 VALUES('x', NULL, 'y');
SELECT u FROM t3;
} {1 2}
do_catchsql_test 4.5.3 {
INSERT INTO t3 VALUES(2, 5, 3);
UPDATE t3 SET u = NULL WHERE s = 2;
} {1 {datatype mismatch}}
finish_test