SQL Statements

CockroachDB supports the following SQL statements. Click a statement for more details.

Tip:

In the built-in SQL shell, use \h [statement] to get inline help about a specific statement.

Data manipulation statements

StatementUsage
CREATE TABLE ASCreate a new table in a database using the results from a selection query.
DELETEDelete specific rows from a table.
EXPORTExport an entire table's data, or the results of a SELECT statement, to CSV files. This statement is available only to enterprise users.
IMPORTImport an entire table's data via CSV files.
INSERTInsert rows into a table.
SELECTSelect specific rows and columns from a table and optionally compute derived values.
TABLESelect all rows and columns from a table.
TRUNCATEDelete all rows from specified tables.
UPDATEUpdate rows in a table.
UPSERTInsert rows that do not violate uniqueness constraints; update rows that do.
VALUESReturn rows containing specific values.

Data definition statements

StatementUsage
ADD COLUMNAdd columns to a table.
ADD CONSTRAINTAdd a constraint to a column.
ALTER COLUMNChange a column's Default constraint or drop the NOT NULL constraint.
ALTER DATABASEApply a schema change to a database.
ALTER INDEXApply a schema change to an index.
ALTER RANGEChange an existing system range.
ALTER SEQUENCEApply a schema change to a sequence.
ALTER TABLEApply a schema change to a table.
ALTER TYPEChange a column's data type.
ALTER USERAdd or change a user's password.
ALTER VIEWRename a view.
COMMENT ONAssociate a comment to a database, table, or column.
CONFIGURE ZONEAdd, modify, reset, and remove replication zones.
CREATE DATABASECreate a new database.
CREATE INDEXCreate an index for a table.
CREATE SEQUENCECreate a new sequence.
CREATE TABLECreate a new table in a database.
CREATE TABLE ASCreate a new table in a database using the results from a selection query.
CREATE VIEWCreate a new view in a database.
DROP COLUMNRemove columns from a table.
DROP CONSTRAINTRemove constraints from a column.
DROP DATABASERemove a database and all its objects.
DROP INDEXRemove an index for a table.
DROP SEQUENCERemove a sequence.
DROP TABLERemove a table.
DROP VIEWRemove a view.
EXPERIMENTAL_AUDITTurn SQL audit logging on or off for a table.
RENAME COLUMNRename a column in a table.
RENAME CONSTRAINTRename a constraint on a column.
RENAME DATABASERename a database.
RENAME INDEXRename an index for a table.
RENAME SEQUENCERename a sequence.
RENAME TABLERename a table or move a table between databases.
SHOW COLUMNSView details about columns in a table.
SHOW CONSTRAINTSList constraints on a table.
SHOW CREATEView the CREATE statement for a table, view, or sequence.
SHOW DATABASESList databases in the cluster.
SHOW INDEXView index information for a table.
SHOW SCHEMASList the schemas in a database.
SHOW SEQUENCESList the sequences in a database.
SHOW TABLESList tables or views in a database or virtual schema.
SHOW EXPERIMENTAL_RANGESShow range information about a specific table or index.
SHOW ZONE CONFIGURATIONSList details about existing replication zones.
SPLIT ATForce a key-value layer range split at the specified row in the table or index.
VALIDATE CONSTRAINTCheck whether values in a column match a constraint on the column.

Transaction management statements

StatementUsage
BEGINInitiate a transaction.
COMMITCommit the current transaction.
RELEASE SAVEPOINTWhen using the CockroachDB-provided function for client-side transaction retries, commit the transaction's changes once there are no retry errors.
ROLLBACKDiscard all updates made by the current transaction or, when using the CockroachDB-provided function for client-side transaction retries, rollback to the savepoint and retry the transaction.
SAVEPOINTWhen using the CockroachDB-provided function for client-side transaction retries, start a retryable transaction.
SET TRANSACTIONSet the priority for the session or for an individual transaction.
SHOWView the current transaction settings.

Access management statements

StatementUsage
CREATE ROLECreate SQL roles, which are groups containing any number of roles and users as members.
CREATE USERCreate SQL users, which lets you control privileges on your databases and tables.
DROP ROLERemove one or more SQL roles.
DROP USERRemove one or more SQL users.
GRANT <privileges>Grant privileges to users or roles.
GRANT <roles>Add a role or user as a member to a role.
REVOKE <privileges>Revoke privileges from users or roles.
REVOKE <roles>Revoke a role or user's membership to a role.
SHOW GRANTSView privileges granted to users.
SHOW ROLESLists the roles for all databases.
SHOW USERSLists the users for all databases.

Session management statements

StatementUsage
RESETReset a session variable to its default value.
SETSet a current session variable.
SET TRANSACTIONSet the priority for an individual transaction.
SHOW TRACE FOR SESSIONReturn details about how CockroachDB executed a statement or series of statements recorded during a session.
SHOWList the current session or transaction settings.

Cluster management statements

StatementUsage
RESET CLUSTER SETTINGReset a cluster setting to its default value.
SET CLUSTER SETTINGSet a cluster-wide setting.
SHOW ALL CLUSTER SETTINGSList the current cluster-wide settings.
SHOW SESSIONSList details about currently active sessions.
CANCEL SESSIONCancel a long-running session.

Query management statements

StatementUsage
CANCEL QUERYCancel a running SQL query.
SHOW QUERIESList details about current active SQL queries.

Query planning statements

StatementUsage
CREATE STATISTICSCreate table statistics for the cost-based optimizer to use.
EXPLAINView debugging and analysis details for a statement that operates over tabular data.
EXPLAIN ANALYZEExecute the query and generate a physical query plan with execution statistics.
SHOW STATISTICSList table statistics used by the cost-based optimizer.

Job management statements

Jobs in CockroachDB represent tasks that might not complete immediately, such as schema changes or enterprise backups or restores.

StatementUsage
CANCEL JOBCancel a BACKUP, RESTORE, IMPORT, or CHANGEFEED job.
PAUSE JOBPause a BACKUP, RESTORE, IMPORT, or CHANGEFEED job.
RESUME JOBResume a paused BACKUP, RESTORE, IMPORT, or CHANGEFEED job.
SHOW JOBSView information on jobs.

Backup and restore statements (Enterprise)

The following statements are available only to enterprise users.

Note:

For non-enterprise users, see Back up Data and Restore Data.

StatementUsage
BACKUPCreate disaster recovery backups of databases and tables.
RESTORERestore databases and tables using your backups.
SHOW BACKUPList the contents of a backup.

Changefeed statements (Enterprise)

Change data capture (CDC) provides row-level change feeds into Apache Kafka for downstream processing.

Note:

CDC is an enterprise feature. There will be a core version in a future release.

StatementUsage
CREATE CHANGEFEED(Enterprise) Create a new changefeed, which provides row-level change subscriptions.
EXPERIMENTAL CHANGEFEED FOR(Core) Create a new core changefeed, which provides row-level change subscriptions.

Was this page helpful?
YesNo