Create a table

Define tables, types, constraints, and defaults that actually work in v0.1.0.

Version
Latest
v0.1.0 · latest 1 min read
On this page
  1. Minimal
  2. Realistic
  3. Syntax (implemented subset)
  4. What works
  5. What does not
  6. Alter / inspect

Goal: define a table developers can insert into, query, index, and transact on.

Minimal#

sqlsource
CREATE TABLE users (id BIGINT PRIMARY KEY, name TEXT NOT NULL);

Realistic#

sqlsource
CREATE TABLE IF NOT EXISTS orders (
  id         BIGINT PRIMARY KEY,
  user_id    BIGINT NOT NULL,
  total      NUMERIC(12,2) NOT NULL DEFAULT 0.00,
  status     TEXT NOT NULL DEFAULT 'pending',
  payload    JSONB,
  placed_at  TIMESTAMPTZ NOT NULL DEFAULT now(),
  CONSTRAINT positive_total CHECK (total >= 0)
);

Syntax (implemented subset)#

textsource
CREATE [TEMP|TEMPORARY] TABLE [IF NOT EXISTS] name (
  col TYPE [constraints...], ...
  [, table constraints]
) [WITH (...)] [TABLESPACE name]

Source: crates/sql/src/parser/create.rs:parse_create_table.

What works#

  • Types in Types, incl. TYPE[] arrays (tags TEXT[]) and (n[,m]) typmod; serial/bigserial create a sequence.
  • NOT NULL, DEFAULT, UNIQUE, PRIMARY KEY (column-level and composite table-level: PRIMARY KEY (warehouse_id, product_id)), CHECK (incl. IN-lists: CHECK (status IN ('a','b'))).
  • REFERENCES parent(col) parses inline and table-level to document intent, but is not enforced — see e-commerce integrity checks for the anti-join replacement pattern.
  • ALTER TABLE … RENAME TO | RENAME COLUMN | ADD COLUMN | ADD CONSTRAINT | DROP COLUMN; DROP TABLE [IF EXISTS] … [CASCADE|RESTRICT]; TRUNCATE.

What does not#

  • Foreign-key enforcement, RENAME DATABASE, storage parameters beyond parsing. See Compatibility.

Alter / inspect#

sqlsource
ALTER TABLE orders ADD COLUMN note TEXT;
ALTER TABLE orders RENAME COLUMN note TO memo;
DESCRIBE orders;      -- or SHOW tables
DROP TABLE IF EXISTS orders_tmp;

Next: Insert data · Reference: DDL

Was this page helpful?