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#
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 DEFAULTArguments#
| Name | Meaning | Default |
|---|---|---|
OR REPLACE | Drop the table first (DROP TABLE IF EXISTS), then create it. MariaDB form. | off |
TEMPORARY | The table belongs to the session. See temporary tables. | off |
IF NOT EXISTS | Do nothing, with a warning, if the table exists (the query of AS SELECT is not run). | off |
tbl_name | Any name MySQL allows; __rdb_*, __tmp* and __u_* are reserved. | none |
LIKE old_tbl | Copy columns, defaults, indexes and checks of old_tbl; no rows, no foreign keys. See CREATE TABLE ... SELECT and LIKE. | none |
query_expression | A SELECT, UNION, VALUES, TABLE or WITH query whose rows fill the table. | none |
IGNORE, REPLACE | What to do with rows of the query that break a unique key. | error |
NOT NULL / NULL | Column cannot / can hold NULL. A primary key column is always NOT NULL. | NULL |
DEFAULT | A literal, a parenthesised expression, CURRENT_TIMESTAMP[(n)], NEXTVAL(seq) or UUID(). Worked out per row. | none |
AUTO_INCREMENT | The 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 part | A 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,REPLACEor(.ENGINE=...,DEFAULT CHARSET=...,AUTO_INCREMENT=...,ROW_FORMAT,COMMENT=...are therefore accepted and have no effect; there is one storage engine andSHOW ENGINESsays so. The words are not validated. - Character sets are ignored.
CHARACTER SET,CHARSETandCOLLATEon a column or after a type are read and dropped; every text isutf8mb4. PRIMARY KEYon a column and as a table key are the same. A name afterPRIMARY KEYis accepted and ignored (the key is alwaysPRIMARY).CONSTRAINT symbolmay precedePRIMARY KEY,UNIQUE,FOREIGN KEYandCHECK. ForUNIQUE, 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,SIGNEDandZEROFILLare read;ZEROFILLhas 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 TABLEprints what the chosen dialect's server prints: MySQL writesDEFAULT NULLfor nullable non-text columns, MariaDB also for text.- Privileges:
CREATEon the table;CREATE TEMPORARY TABLESon the database for a temporary table. CREATE TABLEends an open transaction (implicit commit).
Not supported yet#
Refused with error 1235:
FULLTEXTindexes are accepted and not kept;SPATIALindexes are kept as definitions only.- Partitioning (
PARTITION BY ...). REFERENCESwritten inside a column definition (useFOREIGN KEY (...) REFERENCES ...).ON UPDATE CURRENT_TIMESTAMPon a column.
Not done and not refused: foreign keys to a table of another database (keys stay inside one database).
Examples#
Example:
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)
);