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#
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 tArguments#
| Name | Meaning | Default |
|---|---|---|
LIKE old_tbl | Copy the columns, defaults, indexes and checks. Not the rows, not the foreign keys (as MySQL). | none |
create_definition | Column definitions and keys written here come first; a query column of the same name takes the written definition. | none |
IGNORE, REPLACE | What 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
INSERTmachinery (soIGNORE,REPLACE, unique and primary keys andAUTO_INCREMENTbehave as forINSERT). 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 valuedecimal(p,s)with the digits and scale of the values. ANOT NULLnumber from anAUTO_INCREMENTcolumn or a computed value getsDEFAULT 0(as MySQL). Generated columns become plain columns. IF NOT EXISTSon 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 REPLACEis dropped first (MariaDB form). - Privileges:
CREATEon the new table andSELECTon what the query reads (SELECTon the old table forLIKE). CREATE TEMPORARY TABLE ... LIKE | AS SELECTwork.
Not supported yet (known gaps)#
- A computed fixed-point column can be narrower than MySQL's (
price + 1overDECIMAL(8,2)isdecimal(9,2)in MySQL anddecimal(5,2)-ish from the values here). - A computed text column is
varcharsized from the result column.
Examples#
Example:
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;