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.
Required privileges
The user must have theCREATE privilege on the parent database.
Synopsis
- Basic
- Expanded
Parameters
Limitations
The default rules for column families apply. 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
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 MovR: A Global Vehicle-sharing App. To follow along, runcockroach demo to start a temporary, in-memory cluster with the movr dataset preloaded:

