SQL reference (MySQL and MariaDB dialects)

CREATE TABLE statement

The CREATE TABLE grammar that the Raft DB parser accepts, with columns, keys, constraints, table options and the MySQL and MariaDB differences.

CREATE TABLE creates a table in the current or a named database. The table is stored by Raft DB as a collection of JSON documents; the column definitions are kept with the table and checked on every write.

Syntax#

SQL
CREATE [OR REPLACE] [TEMPORARY] TABLE [IF NOT EXISTS] [db_name.]tbl_name
    ( create_definition [, create_definition] ... )
    [table_option] ...
    [ [IGNORE | REPLACE] [AS] query_expression ]

CREATE [OR REPLACE] [TEMPORARY] TABLE [IF NOT EXISTS] [db_name.]tbl_name
    { LIKE old_tbl | (LIKE old_tbl) }

CREATE [OR REPLACE] [TEMPORARY] TABLE [IF NOT EXISTS] [db_name.]tbl_name
    [ ( create_definition, ... ) ]
    [ [IGNORE | REPLACE] [AS] query_expression ]

create_definition:
    col_name column_definition
  | [CONSTRAINT [symbol]] PRIMARY KEY [index_name] [USING type] (key_part, ...)
  | { INDEX | KEY } [index_name] (key_part, ...) [index_option] ...
  | [CONSTRAINT [symbol]] UNIQUE [INDEX | KEY] [index_name] (key_part, ...) [index_option] ...
  | [CONSTRAINT [symbol]] FOREIGN KEY [index_name] (col_name, ...) reference_definition
  | [CONSTRAINT [symbol]] CHECK (expr) [[NOT] ENFORCED]

column_definition:
    data_type [column_attribute] ...

column_attribute:
    NOT NULL | NULL
  | DEFAULT { literal | (expr) | CURRENT_TIMESTAMP[(n)] | NEXTVAL(seq) | UUID() }
  | AUTO_INCREMENT
  | [PRIMARY] KEY
  | UNIQUE [KEY]
  | [GENERATED ALWAYS] AS (expr) [VIRTUAL | STORED]
  | INVISIBLE | VISIBLE
  | CHECK (expr) [[NOT] ENFORCED]
  | COMMENT 'string'
  | COLLATE collation_name
  | { CHARACTER SET | CHARSET } charset_name

key_part:
    { col_name [(length)] | (expr) } [ASC | DESC]

index_option:
    USING { BTREE | HASH } | COMMENT 'string' | VISIBLE | INVISIBLE

reference_definition:
    REFERENCES tbl_name [(col_name, ...)] [MATCH match_type]
        [ON DELETE reference_option] [ON UPDATE reference_option]

reference_option:
    RESTRICT | CASCADE | SET NULL | NO ACTION | SET DEFAULT

Arguments#

NameMeaningDefault
OR REPLACEDrop the table first (DROP TABLE IF EXISTS), then create it. MariaDB form.off
TEMPORARYThe table belongs to the session. See temporary tables.off
IF NOT EXISTSDo nothing, with a warning, if the table exists (the query of AS SELECT is not run).off
tbl_nameAny name MySQL allows; __rdb_*, __tmp* and __u_* are reserved.none
LIKE old_tblCopy columns, defaults, indexes and checks of old_tbl; no rows, no foreign keys. See CREATE TABLE ... SELECT and LIKE.none
query_expressionA SELECT, UNION, VALUES, TABLE or WITH query whose rows fill the table.none
IGNORE, REPLACEWhat to do with rows of the query that break a unique key.error
NOT NULL / NULLColumn cannot / can hold NULL. A primary key column is always NOT NULL.NULL
DEFAULTA literal, a parenthesised expression, CURRENT_TIMESTAMP[(n)], NEXTVAL(seq) or UUID(). Worked out per row.none
AUTO_INCREMENTThe column takes the next counter value when no value is given. SERIAL is BIGINT UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE.off
key_part (length)Prefix length for a text column. Kept; shown by SHOW INDEX.whole column
(expr) as key partA functional index, see functional indexes.none

Remarks#

  • Table options are skipped. After the closing parenthesis the parser reads and ignores every word up to the end of the statement, or up to AS, SELECT, WITH, VALUES, TABLE, IGNORE, REPLACE or (. ENGINE=..., DEFAULT CHARSET=..., AUTO_INCREMENT=..., ROW_FORMAT, COMMENT=... are therefore accepted and have no effect; there is one storage engine and SHOW ENGINES says so. The words are not validated.
  • Character sets are ignored. CHARACTER SET, CHARSET and COLLATE on a column or after a type are read and dropped; every text is utf8mb4.
  • PRIMARY KEY on a column and as a table key are the same. A name after PRIMARY KEY is accepted and ignored (the key is always PRIMARY).
  • CONSTRAINT symbol may precede PRIMARY KEY, UNIQUE, FOREIGN KEY and CHECK. For UNIQUE, the symbol is the index name when no index name is written.
  • Column types: the type grammar is on the data types pages. The parser also reads the other names MariaDB takes (CHAR VARYING, NVARCHAR, FLOAT4, FLOAT8, DOUBLE PRECISION, LONG, CLOB, NUMBER, RAW, VARCHAR2, SERIAL). UNSIGNED, SIGNED and ZEROFILL are read; ZEROFILL has no effect.
  • Foreign keys are enforced; see foreign keys. An index on the referring columns is added when there is none.
  • CHECK, generated columns, invisible columns and indexes, functional indexes: see their own pages.
  • MariaDB forms. The parser does not test the dialect for CREATE OR REPLACE TABLE; it is checked on MariaDB only.
  • SHOW CREATE TABLE prints what the chosen dialect's server prints: MySQL writes DEFAULT NULL for nullable non-text columns, MariaDB also for text.
  • Privileges: CREATE on the table; CREATE TEMPORARY TABLES on the database for a temporary table.
  • CREATE TABLE ends an open transaction (implicit commit).

Not supported yet#

Refused with error 1235:

  • FULLTEXT indexes are accepted and not kept; SPATIAL indexes are kept as definitions only.
  • Partitioning (PARTITION BY ...).
  • REFERENCES written inside a column definition (use FOREIGN KEY (...) REFERENCES ...).
  • ON UPDATE CURRENT_TIMESTAMP on a column.

Not done and not refused: foreign keys to a table of another database (keys stay inside one database).

Examples#

Example:

SQL
CREATE TABLE src (
  id INT PRIMARY KEY AUTO_INCREMENT,
  name VARCHAR(20) NOT NULL,
  qty INT DEFAULT 5,
  price DECIMAL(8,2),
  UNIQUE KEY u_name (name),
  KEY k_qty (qty)
);

CREATE TABLE child_cascade (
  id INT NOT NULL AUTO_INCREMENT,
  pid INT NOT NULL,
  v INT,
  PRIMARY KEY (id),
  KEY pid (pid),
  CONSTRAINT fk_cc FOREIGN KEY (pid) REFERENCES parent (id)
    ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB;

CREATE TABLE t (
  id INT PRIMARY KEY,
  qty INT CHECK (qty >= 0),
  price DECIMAL(8,2),
  CONSTRAINT price_pos CHECK (price > 0)
);

See also#