The "CREATE TABLE" command is used to create a new table in an SQLite database. A CREATE TABLE command specifies the following attributes of the new table:
The name of the new table.
The database in which the new table is created. Tables may be created in the main database, the temp database, or in any attached database.
The name of each column in the table.
The declared type of each column in the table.
A default value or expression for each column in the table.
A default collation sequence to use with each column.
Optionally, a PRIMARY KEY for the table. Both single column and composite (multiple column) primary keys are supported.
A set of SQL constraints for each table. SQLite supports UNIQUE, NOT NULL, CHECK and FOREIGN KEY constraints.
Optionally, a generated column constraint.
Whether the table is a WITHOUT ROWID table.
Whether the table is subject to strict type checking.
Every CREATE TABLE statement must specify a name for the new table. 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_". ]
R-18448-33677:[If a schema-name is specified, it must be either "main", "temp", or the name of an attached database. ] R-39822-07822:[In this case the new table is created in the named database. ] 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. ] R-23976-43329:[It is an error to specify both a schema-name and the TEMP or TEMPORARY keyword, unless the schema-name is "temp". ] R-31997-24564:[If no schema name is specified and the TEMP keyword is not present then the table is created in the main database. ]
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. ] 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). ] 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. ]
R-05513-33819:[It is not an error to create a table that has the same name as an existing trigger. ]
R-22283-14179:[Tables are removed using the DROP TABLE statement. ]
R-32365-09043:[A "CREATE TABLE ... AS SELECT" statement creates and populates a database table based on the results of a SELECT statement. ] 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. ] R-55407-45319:[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" |
BLOB (a.k.a "NONE") | "" (empty string) |
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. ]
R-24153-28352:[Tables created using CREATE TABLE AS are initially populated with the rows of data returned by the SELECT statement. ] R-08224-30249:[Rows are assigned contiguously ascending rowid values, starting with 1, in the order that they are returned by the SELECT statement. ]
Unless it is a CREATE TABLE ... AS SELECT statement, a CREATE TABLE includes one or more column definitions, optionally followed by a list of table constraints. Each column definition consists of the name of the column, optionally followed by the declared type of the column, then one or more optional column constraints. Included in the definition of "column constraints" for the purposes of the previous statement are the COLLATE and DEFAULT clauses, even though these are not really constraints in the sense that they do not restrict the data that the table may contain. The other constraints - NOT NULL, CHECK, UNIQUE, PRIMARY KEY and FOREIGN KEY constraints - impose restrictions on the table data.
R-25473-20557:[The number of columns in a table is limited by the SQLITE_MAX_COLUMN compile-time parameter. ] R-55351-62604:[A single row of a table cannot store more than SQLITE_MAX_LENGTH bytes of data. ] R-27775-64721:[Both of these limits can be lowered at runtime using the sqlite3_limit() C/C++ interface. ]
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. ] Instead, SQLite uses dynamic typing. R-10565-09557:[The declared type of a column is used to determine the affinity of the column only. ]
The DEFAULT clause specifies a default value to use for the column if no value is explicitly provided by the user when doing an INSERT. R-42316-09582:[If there is no explicit DEFAULT clause attached to a column definition, then the default value of the column is NULL. ] R-07343-35026:[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. A default value may also be one of the special case-independent keywords CURRENT_TIME, CURRENT_DATE or CURRENT_TIMESTAMP. ] R-33440-07331:[For the purposes of the DEFAULT clause, an expression is considered constant if it contains no sub-queries, column or table references, bound parameters, or string literals enclosed in double-quotes instead of single-quotes. ]
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 ] , as follows:
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. ]
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. ]
R-15363-55230:[If the default value of a column is CURRENT_TIME, CURRENT_DATE or CURRENT_TIMESTAMP, then the value used in the new row is a text representation of the current UTC date and/or time. ] R-62327-53843:[For CURRENT_TIME, the format of the value is "HH:MM:SS". ] R-03775-43471:[For CURRENT_DATE, "YYYY-MM-DD". ] R-07677-44926:[The format for CURRENT_TIMESTAMP is "YYYY-MM-DD HH:MM:SS". ]
R-55061-47754:[The COLLATE clause specifies the name of a collating sequence to use as the default collation sequence for the column. ] R-40275-54363:[If no COLLATE clause is specified, the default collation sequence is BINARY. ]
A column that includes a GENERATED ALWAY AS clause is a generated column. Generated columns are supported beginning with SQLite version 3.31.0 (2020-01-22). See the separate documentation for details on the capabilities and limitations of generated columns.
R-52382-54248:[Each table in SQLite may have at most one PRIMARY KEY. ] 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. ] 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. ] R-59051-35706:[The PRIMARY KEY clause must contain only column names — the use of expressions in an indexed-column of a PRIMARY KEY is not supported. ] R-31826-01813:[An error is raised if more than one PRIMARY KEY clause appears in a CREATE TABLE statement. ] R-54755-39291:[The PRIMARY KEY is optional for ordinary tables but is required for WITHOUT ROWID tables. ]
If a table has a single column primary key and the declared type of that column is "INTEGER" and the table is not a WITHOUT ROWID table, then the column is known as an INTEGER PRIMARY KEY. See below for a description of the special properties and behaviors associated with an INTEGER PRIMARY KEY.
R-59124-61339:[Each row in a table with a primary key must have a unique combination of values in its primary key columns. ] 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. ] R-06471-16287:[If an INSERT or UPDATE statement attempts to modify the table content so that two or more rows have identical primary key values, that is a constraint violation. ]
According to the SQL standard, PRIMARY KEY should always imply NOT NULL. Unfortunately, due to a bug in some early versions, this is not the case in SQLite. R-40010-16873:[Unless the column is an INTEGER PRIMARY KEY or the table is a WITHOUT ROWID table or a STRICT table or the column is declared NOT NULL, SQLite allows NULL values in a PRIMARY KEY column. ] SQLite could be fixed to conform to the standard, but doing so might break legacy applications. Hence, it has been decided to merely document the fact that SQLite allows NULLs in most PRIMARY KEY columns.
R-00227-21080:[A UNIQUE constraint is similar to a PRIMARY KEY constraint, except that a single table may have any number of UNIQUE constraints. ] R-30981-64168:[For each UNIQUE constraint on the table, each row must contain a unique combination of values in the columns identified by the UNIQUE constraint. ] R-00404-17670:[For the purposes of UNIQUE constraints, NULL values are considered distinct from all other values, including other NULLs. ] R-55072-00299:[As with PRIMARY KEYs, a UNIQUE table-constraint clause must contain only column names — the use of expressions in an indexed-column of a UNIQUE table-constraint is not supported. ]
R-55820-29984:[In most cases, UNIQUE and PRIMARY KEY constraints are implemented by creating a unique index in the database. ] (The exceptions are INTEGER PRIMARY KEY and PRIMARY KEYs on WITHOUT ROWID tables.) Hence, the following schemas are logically equivalent:
CREATE TABLE t1(a, b UNIQUE);
CREATE TABLE t1(a, b PRIMARY KEY);
CREATE TABLE t1(a, b);
CREATE UNIQUE INDEX t1b ON t1(b);
R-45493-35653:[A CHECK constraint may be attached to a column definition or specified as a table constraint. In practice it makes no difference. ] R-55435-14303:[Each time a new row is inserted into the table or an existing row is updated, the expression associated with each CHECK constraint is evaluated and cast to a NUMERIC value in the same way as a CAST expression. If the result is zero (integer value 0 or real value 0.0), then a constraint violation has occurred. ] R-34109-39108:[If the CHECK expression evaluates to NULL, or any other non-zero value, it is not a constraint violation. ] R-44163-39688:[The expression of a CHECK constraint may not contain a subquery. ]
CHECK constraints are only verified when the table is written, not when it is read. Furthermore, verification of CHECK constraints can be temporarily disabled using the "PRAGMA ignore_check_constraints=ON;" statement. Hence, it is possible that a query might produce results that violate the CHECK constraints.
R-02060-64547:[A NOT NULL constraint may only be attached to a column definition, not specified as a table constraint. ] Not surprisingly, R-31795-57643:[a NOT NULL constraint dictates that the associated column may not contain a NULL value. Attempting to set the column value to NULL when inserting a new row or updating an existing one causes a constraint violation. ] NOT NULL constraints are not verified during queries, so a query of a column might produce a NULL value even though the column is marked as NOT NULL, if the database file is corrupt.
Constraints are checked during INSERT and UPDATE and by PRAGMA integrity_check and PRAGMA quick_check and sometimes by ALTER TABLE. Queries and DELETE statements do not normally verify constraints. Hence, if a database file has been corrupted (perhaps by an external program making direct changes to the database file without going through the SQLite library) a query might return data that violates a constraint. For example:
CREATE TABLE t1(x INT CHECK( x>3 )); /* Insert a row with X less than 3 by directly writing into the ** database file using an external program */ PRAGMA integrity_check; -- Reports row with x less than 3 as corrupt INSERT INTO t1(x) VALUES(2); -- Fails with SQLITE_CORRUPT SELECT x FROM t1; -- Returns an integer less than 3 in spite of the CHECK constraint
Enforcement of CHECK constraints can be temporarily disabled using the PRAGMA ignore_check_constraints=ON; statement.
The response to a constraint violation is determined by the constraint conflict resolution algorithm. Each PRIMARY KEY, UNIQUE, NOT NULL and CHECK constraint has a default conflict resolution algorithm. R-34093-09213:[PRIMARY KEY, UNIQUE and NOT NULL constraints may be explicitly assigned another default conflict resolution algorithm by including a conflict-clause in their definitions. ] R-17539-59899:[Or, if a constraint definition does not include a conflict-clause, the default conflict resolution algorithm is ABORT. ] The conflict resolution algorithm for CHECK constraints is always ABORT. ( R-29031-55583:[For historical compatibility only, table CHECK constraints are allowed to have a conflict resolution clause, but that has no effect. ] ) R-19114-56113:[Different constraints within the same table may have different default conflict resolution algorithms. ] See the section titled ON CONFLICT for additional information.
R-51414-32910:[Except for WITHOUT ROWID tables, all rows within SQLite tables have a 64-bit signed integer key that uniquely identifies the row within its table. ] This integer is usually called the "rowid". R-52584-04009:[The rowid value can be accessed using one of the special case-independent names "rowid", "oid", or "_rowid_" in place of a column name. ] R-26501-17306:[If a table contains a user defined column named "rowid", "oid" or "_rowid_", then that name always refers the explicitly declared column and cannot be used to retrieve the integer rowid value. ]
The rowid (and "oid" and "_rowid_") is omitted in WITHOUT ROWID tables. WITHOUT ROWID tables are only available in SQLite version 3.8.2 (2013-12-06) and later. A table that lacks the WITHOUT ROWID clause is called a "rowid table".
The data for rowid tables is stored as a B-Tree structure containing one entry for each table row, using the rowid value as the key. This means that retrieving or sorting records by rowid is fast. Searching for a record with a specific rowid, or for all records with rowids within a specified range is around twice as fast as a similar search made by specifying any other PRIMARY KEY or indexed value.
R-47901-33947:[With one exception noted below, if a rowid table has a primary key that consists of a single column and the declared type of that column is "INTEGER" in any mixture of upper and lower case, then the column becomes an alias for the rowid. ] Such a column is usually referred to as an "integer primary key". A PRIMARY KEY column only becomes an integer primary key if the declared type name is exactly "INTEGER". R-41444-49665:[Other integer type names like "INT" or "BIGINT" or "SHORT INTEGER" or "UNSIGNED INTEGER" causes the primary key column to behave as an ordinary table column with integer affinity and a unique index, not as an alias for the rowid. ]
The exception mentioned above is that R-45951-08347:[if the declaration of a column with declared type "INTEGER" includes an "PRIMARY KEY DESC" clause, it does not become an alias for the rowid and is not classified as an integer primary key. ] This quirk is not by design. It is due to a bug in early versions of SQLite. But fixing the bug could result in backwards incompatibilities. Hence, the original behavior has been retained (and documented) because odd behavior in a corner case is far better than a compatibility break. This means that R-56094-57830:[the following three table declarations all cause the column "x" to be an alias for the rowid (an integer primary key):
But R-20149-25884:[the following declaration does not result in "x" being an alias for the rowid:
R-03733-29734:[Rowid values may be modified using an UPDATE statement in the same way as any other column value can, either using one of the built-in aliases ("rowid", "oid" or "_rowid_") or by using an alias created by an integer primary key. ] R-58706-14229:[Similarly, an INSERT statement may provide a value to use as the rowid for each row inserted. ] R-32326-44592:[Unlike normal SQLite columns, an integer primary key or rowid column must contain integer values. Integer primary key or rowid columns are not able to hold floating point values, strings, BLOBs, or NULLs. ]
R-64224-62578:[If an UPDATE statement attempts to set an integer primary key or rowid column to a NULL or blob value, or to a string or real value that cannot be losslessly converted to an integer, a "datatype mismatch" error occurs and the statement is aborted. ] R-05734-13629:[If an INSERT statement attempts to insert a blob value, or a string or real value that cannot be losslessly converted to an integer into an integer primary key or rowid column, a "datatype mismatch" error occurs and the statement is aborted. ]
R-07986-46024:[If an INSERT statement attempts to insert a NULL value into a rowid or integer primary key column, the system chooses an integer value to use as the rowid automatically. ] A detailed description of how this is done is provided separately.
R-60116-07512:[The parent key of a foreign key constraint is not allowed to use the rowid. The parent key must used named columns only. ]
This page last modified on 2023-01-17 23:02:51 UTC
*** DRAFT ***