CREATE TABLE AS SELECT (CTAS)
Builds a new table from the rows of a SELECT. It copies data and, in most engines, almost nothing else: not primary keys, not indexes, not defaults. SQL Server spells the same move SELECT INTO.
- Категория
- Таблицы
- In this category
- 3 of 16
Синтаксис
CREATE TABLE new_table AS SELECT ... -- PostgreSQL, MySQL, SQLite, Oracle
SELECT ... INTO new_table FROM ... -- SQL Server
CREATE TABLE new_table (LIKE old_table [INCLUDING ALL]) -- PostgreSQL: structure from another table
Each block is a separate form. The support table and the examples show which engine takes which.
Поддержка движка
| Движок | Support | Заметки |
|---|---|---|
| SQL Server | Different syntax | spelled SELECT ... INTO new_table FROM ...; there is no CREATE TABLE AS SELECT |
| PostgreSQL | Да | also CREATE TEMP TABLE ... AS; (LIKE old INCLUDING ALL) copies structure more fully |
| MySQL | Да | CREATE TABLE ... AS SELECT copies data plus NOT NULL and DEFAULT; CREATE TABLE ... LIKE copies structure only |
| SQLite | Да | the new table has no constraints, and column types are cut down to their affinity (INT, TEXT, NUM, REAL, or none) |
| Oracle | Да | column types come from the query; NOT NULL is the only constraint carried over |
Примеры
PostgreSQL
CREATE TABLE orders_2026 AS SELECT * FROM orders WHERE placed_at >= DATE '2026-01-01';
SQL Server
SELECT * INTO orders_2026 FROM orders WHERE placed_at >= '2026-01-01';
MySQL
-- structure and indexes, no rows: CREATE TABLE orders_copy LIKE orders;
SQLite
CREATE TABLE orders_2026 AS SELECT * FROM orders WHERE placed_at >= '2026-01-01';
Oracle
CREATE TABLE orders_2026 AS SELECT * FROM orders WHERE placed_at >= DATE '2026-01-01';
Полезно знать
- None of the five copies primary keys, unique keys, checks or indexes: the new table is just the data. Add the constraints and indexes afterwards with ALTER TABLE and CREATE INDEX.
- On SQL Server, SELECT INTO inherits column nullability from the source and carries an identity column through only when the SELECT passes it unchanged. Defaults, keys and checks never come along.
- MySQL has two copy statements people mix up: CREATE TABLE new AS SELECT copies rows with no keys or indexes, while CREATE TABLE new LIKE old copies the structure (indexes and checks included, foreign keys not) with no rows.
- SQLite's CREATE TABLE AS keeps only each column's affinity as its declared type (INT, TEXT, NUM, REAL, or nothing for expressions and BLOBs) and drops every constraint, so an INTEGER PRIMARY KEY comes out as a plain INT column. When the types matter, declare the table yourself and fill it with INSERT ... SELECT.
Questions about CREATE TABLE AS
Does CREATE TABLE AS copy indexes and keys?
No, on all five engines. Only data carries over, plus NOT NULL flags on Oracle, SQL Server and MySQL (MySQL also keeps column defaults, and SQL Server keeps an unchanged IDENTITY column). Recreate the keys and indexes afterwards, which is also your chance to build them in bulk after the load instead of maintaining them row by row.
How do I copy a table's structure without its data?
MySQL: CREATE TABLE new LIKE old. PostgreSQL: CREATE TABLE new (LIKE old INCLUDING ALL). SQL Server: SELECT * INTO new FROM old WHERE 1 = 0. SQLite: declare the table yourself, because CREATE TABLE AS keeps only type affinities and no constraints.
Can CREATE TABLE AS be rolled back?
On SQL Server, PostgreSQL and SQLite, yes: the statement runs inside the transaction like any other. MySQL and Oracle commit implicitly around DDL, so once the statement succeeds the new table stays even if the surrounding transaction rolls back.
Browse every statement in the SQL DDL reference, or see what the SQLly desktop app does for your database.
DDL that matches the engine you're connected to
SQLly checks your statements against the dialect of the open connection, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle, so an ALTER TABLE that works on one engine gets flagged before it runs on another.