SQL reference (MySQL and MariaDB dialects)

CREATE TABLE ... SELECT and CREATE TABLE ... LIKE

Making a table from the columns of a query or from the definition of another table, with the type rules and the known gaps.

Two ways to make a table from something that exists (D71).

Syntax#

SQL
CREATE [OR REPLACE] [TEMPORARY] TABLE [IF NOT EXISTS] new_tbl LIKE old_tbl
CREATE [OR REPLACE] [TEMPORARY] TABLE [IF NOT EXISTS] new_tbl (LIKE old_tbl)

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

query_expression:
    SELECT ... | ( SELECT ... ) | SELECT ... UNION ... | WITH ... SELECT ... | VALUES ... | TABLE t

Arguments#

NameMeaningDefault
LIKE old_tblCopy the columns, defaults, indexes and checks. Not the rows, not the foreign keys (as MySQL).none
create_definitionColumn definitions and keys written here come first; a query column of the same name takes the written definition.none
IGNORE, REPLACEWhat to do with rows of the query that break a unique or primary key.error

Remarks#

  • LIKE takes the text of SHOW CREATE TABLE old_tbl, drops the foreign-key lines and the counter, and creates the new table from it. Checks are copied under new names (new_chk_n). Generated columns, invisible columns and functional indexes are carried. Prefix lengths of indexes are not carried.
  • AS SELECT runs the query once, makes the table from its columns, and adds the rows with the INSERT machinery (so IGNORE, REPLACE, unique and primary keys and AUTO_INCREMENT behave as for INSERT). If the rows cannot be added, the table is dropped again.
  • Types. A column of a table or an aggregate keeps the type of the result column. A computed number is typed from its values: a whole number is bigint, a fixed-point value decimal(p,s) with the digits and scale of the values. A NOT NULL number from an AUTO_INCREMENT column or a computed value gets DEFAULT 0 (as MySQL). Generated columns become plain columns.
  • IF NOT EXISTS on an existing table does nothing but warn and does not run the query.
  • A duplicate result column name is an error (SELECT id, id).
  • A table made with OR REPLACE is dropped first (MariaDB form).
  • Privileges: CREATE on the new table and SELECT on what the query reads (SELECT on the old table for LIKE).
  • CREATE TEMPORARY TABLE ... LIKE | AS SELECT work.

Not supported yet (known gaps)#

  • A computed fixed-point column can be narrower than MySQL's (price + 1 over DECIMAL(8,2) is decimal(9,2) in MySQL and decimal(5,2)-ish from the values here).
  • A computed text column is varchar sized from the result column.

Examples#

Example:

SQL
CREATE TABLE like1 LIKE src;
CREATE TABLE like2 (LIKE src);
CREATE TABLE ct_a AS SELECT id, name FROM src WHERE qty >= 2;
CREATE TABLE ct_b SELECT name, qty * 2 AS dbl, price AS p1 FROM src;
CREATE TABLE ct_c (PRIMARY KEY (id)) AS SELECT id, name FROM src;
CREATE TABLE ct_d (extra INT DEFAULT 7) SELECT id FROM src;
CREATE TABLE ct_f AS SELECT 1 AS one, 'two' AS two, 3.5 AS three;
CREATE TABLE ct_j (PRIMARY KEY (qty)) IGNORE AS SELECT qty FROM src WHERE qty IS NOT NULL UNION ALL SELECT 1;
CREATE TABLE ct_l AS WITH w AS (SELECT id FROM src WHERE id < 3) SELECT * FROM w;

See also#