CREATE TABLE
Create a table. The first column is always the primary key, and columns are NOT NULL unless declared otherwise.
In this article
Creates a table. Every table is stored as a B+ tree ordered by its primary key, which is always the first column.
Syntax
CREATE TABLE [<namespace>.]<table> (
<column> <type> [NULL | NOT NULL] [REFERENCES <parent_table>] [CABIN | CABIN AUTO | NO CABIN]
[, ...]
) [BTREE] [SPARE <n>]
Arguments
| Argument | Description |
|---|---|
<namespace>. | Optional. Creates the table in an existing namespace. Without it, the table goes into public. |
<table> | Table name, unique across the whole instance. |
<column> <type> | A column and its data type. At least one column is required. The first column is the primary key and must be an integer type. Column names must differ ignoring case. |
NULL / NOT NULL | Whether the column accepts NULL. The default is NOT NULL. |
REFERENCES <parent_table> | Declares a foreign key to the parent table’s primary key. No parent column is written. |
CABIN, CABIN AUTO, NO CABIN | Per-column policy for Cabins. Not allowed on the primary key. |
BTREE | Optional; it is the default and the only storage form for new tables. |
SPARE <n> | Optional. Reserves n column slots (0 to 64, default 0) for columns added later with ALTER TABLE ... ADD COLUMN. See Spare column slots. |
The column suffixes must appear in the order shown: nullability, then REFERENCES, then the Cabin policy.
Examples
A simple table
CREATE TABLE coupons (id int64, code char(8), percent_off int64, expires date);
CREATED oid=4047
A table with a foreign key and a nullable column
CREATE TABLE reviews (
id int64,
item_id int64 REFERENCES items,
account_id int64 REFERENCES accounts,
stars int64,
written_on date,
body varchar NULL
);
A table in a namespace
The namespace must already exist. Here it is shop, one of the two namespaces in the sandbox:
CREATE TABLE shop.wishlists (id int64, account_id int64 REFERENCES accounts, item_id int64 REFERENCES items);
A table that can take more columns later
CREATE TABLE notes (id int64, title varchar(32), body varchar) SPARE 4;
Spare column slots
Every row of a table has the same size, fixed when the table is created. SPARE n reserves n slots of 16 bytes in every row, so that ALTER TABLE ... ADD COLUMN can add up to n columns later without rewriting a row.
- Each slot costs 16 bytes per row, plus one bit in the row’s null bitmap, whether it is used or not.
- A slot holds a column up to 16 bytes wide: any fixed-width type,
char(N)up to 16, orvarchar(16). - A dropped column keeps its slot, so
nis the total number of columns the table can ever add. SELECT * FROM sys.tablesshows each table’sspare_slotsandspare_free.- A table created without
SPAREhas no slot, andADD COLUMNon it is refused.
The primary key
The first column is the row’s primary key, and it is the row’s identity rather than an ordinary field:
- Issued or named. If an
INSERTleaves the key out, the engine issues one. If it supplies one, that value is the key. Both work on every table, and one statement can mix them. See INSERT. - Range. Keys are integers from 1 to 2⁴⁸ − 1 (281,474,976,710,655).
- Bound once. A key whose row was committed stays taken, even after
DELETE, until PURGE frees it. Issued keys are never reused. - Never updated. An
UPDATEthat assigns the primary key is refused. - Free ordering. Rows are kept in key order whatever order keys arrive in, so
ORDER BY <primary key>costs nothing.
There is no SERIAL, AUTO_INCREMENT or key-mode clause: every table already behaves this way. EXPLICIT is accepted for compatibility and does nothing.
NULL and NOT NULL
Columns are NOT NULL by default, the opposite of standard SQL. Write NULL after the type to allow nulls.
- The primary key, index key columns and covered columns cannot be nullable.
- A stored
NULLis different from0and from''. It is found byIS NULL, never by= NULL. - Nullable columns cost one byte per eight nullable columns in each row; a table with none pays nothing.
Foreign keys
REFERENCES <parent_table> makes the column point at a row of the parent table by primary key.
-
INSERTandUPDATEcheck that the parent row exists.DELETEof a parent checks that no live child row refers to it. -
A violation is refused with
FK_VIOLATION:kdbs> INSERT INTO purchases VALUES (10, 1, 9, 1, '2026-10-09', 'x') ^ ERR FK_VIOLATION retryable=0 'item_id' references row id=9 of 'items', which does not exist (byte 37) -
If the parent row is being written by another transaction, the check waits for that transaction to finish instead of failing.
-
Only
RESTRICTbehavior exists.ON DELETE CASCADEandSET NULLare not available. -
SHOW FKEYSlists the declared foreign keys.
Remarks
CREATE TABLEis transactional: insideBEGIN ... COMMIT, other sessions cannot see the table until the commit, andROLLBACKremoves it.- While one transaction holds an uncommitted
CREATE TABLE, another transaction cannot create the same name. CREATE TABLEruns in a real transaction and its catalog writes go to the write-ahead log, so a committed table survives a crash and one that had not committed is rolled back when the server restarts.SHOW METAreportsddl_durableandcatalog_recoveredas1.
Limitations
HEAPstorage is suspended. WritingHEAPis refused; existing heap tables still work.- Columns cannot be retyped or widened later. They can be added only into slots reserved with
SPARE, and dropped. See ALTER TABLE. - There is no
UNIQUEcolumn or table constraint. Declare a unique index with CREATE UNIQUE INDEX. IF NOT EXISTSis not supported.