Skip to main content
The CREATE TABLE ... AS creates a new table from a .
The “ statement performs a schema change. For more information about how online schema changes work in CockroachDB, see .

Intended use

Tables created with CREATE TABLE ... AS are intended to persist the result of a query for later reuse. This can be more efficient than a when the following two conditions are met:
  • The result of the query is used as-is multiple times.
  • The copy needs not be kept up-to-date with the original table over time.
When the results of a query are reused multiple times within a larger query, a view is advisable instead. The query optimizer can “peek” into the view and optimize the surrounding query using the primary key and indices of the tables mentioned in the view query. A view is also advisable when the results must be up-to-date; a view always retrieves the current data from the tables that the view query mentions.

Required privileges

The user must have the CREATE on the parent database.

Synopsis

create_table_as syntax diagram

Parameters

ParameterDescription
IF NOT EXISTSCreate a new table only if a table of the same name does not already exist in the database; if one does exist, do not return an error.

Note that IF NOT EXISTS checks the table name only; it does not check if an existing table has the same columns, indexes, constraints, etc., of the new table.
table_nameThe name of the table to create, which must be unique within its database and follow these . When the parent database is not set as the default, the name must be formatted as database.name.

The and statements use a temporary table called excluded to handle uniqueness conflicts during execution. It’s therefore not recommended to use the name excluded for any of your tables.
column_nameThe name of the column you want to use instead of the name of the column from select_stmt.
create_as_col_qual_listAn optional column definition, which may include and .
family_defAn optional . Column family names must be unique within the table but can have the same name as columns, constraints, or indexes.
create_as_constraint_defAn optional .
select_stmtA to provide the data.
opt_persistence_temp_tableDefines the table as a session-scoped temporary table. For more information, see .

Note that the LOCAL, GLOBAL, and UNLOGGED options are no-ops, allowed by the parser for PostgreSQL compatibility.

Support for temporary tables is .
opt_with_storage_parameter_listA comma-separated list of . Supported parameters include fillfactor, s2_max_level, s2_level_mod, s2_max_cells, geometry_min_x, geometry_max_x, geometry_min_y, and geometry_max_y. The fillfactor parameter is a no-op, allowed for PostgreSQL-compatibility.

For details, see . For an example, see .
ON COMMIT PRESERVE ROWSThis clause is a no-op, allowed by the parser for PostgreSQL compatibility. CockroachDB only supports session-scoped , and does not support the clauses ON COMMIT DELETE ROWS and ON COMMIT DROP, which are used to define transaction-scoped temporary tables in PostgreSQL.

Limitations

The default rules for apply. The of tables created with CREATE TABLE ... AS is not automatically derived from the query results. You must specify new primary keys at table creation. For examples, see .

Examples

Setup

The following examples use MovR, a fictional vehicle-sharing application, to demonstrate CockroachDB SQL statements. For more information about the MovR example application and dataset, see . To follow along, run to start a temporary, in-memory cluster with the movr dataset preloaded:

Create a table from a SELECT query

Change column names

This statement creates a copy of an existing table but with changed column names:

Create a table from a VALUES clause

Create a copy of an existing table

When a table copy is created this way, the copy is not associated to any primary key, secondary index, or constraint that was present on the original table.

Specify a primary key

You can specify the of a new table created from a selection query:

Define column families

You can define the of a new table created from a selection query:

See also