CREATE TABLE ... AS statement creates a new table from a selection query.
The
CREATE TABLE AS statement performs a schema change. For more information about how online schema changes work in CockroachDB, see Online Schema Changes.Intended use
Tables created withCREATE TABLE ... AS are intended to persist the result of a query for later reuse.
This can be more efficient than a view 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.
The default rules for column families apply.
Required privileges
The user must have theCREATE privilege on the parent database.
Synopsis
- Basic
- Expanded
Parameters
Known limitations
The primary key of tables created withCREATE TABLE ... AS is not automatically derived from the query results. You must specify new primary keys at table creation. For examples, see Specify a primary key.
Examples
Setup
To follow along, runcockroach demo to start a temporary, in-memory cluster with the movr sample dataset preloaded:

