Skip to main content
This tutorial guides you through some of the most essential CockroachDB SQL statements using an interactive SQL shell connected to a temporary, single-node CockroachDB cluster. For a complete list of supported SQL statements and related details, see .

Start CockroachDB

If you haven’t already, . Then run the command:
This starts a single-node, temporary cluster with the dataset pre-loaded.

Show tables

To see all tables in the active database, use the statement or the \dt :

Create a table

Suppose that you want MovR to offer ride-sharing services, in addition to vehicle-sharing services. You’ll need to add a table for drivers to the movr database. To create a table, use followed by a table name, the column names, and the and , if any, for each column:
Table and column names must follow . Also, when you do not explicitly define a , CockroachDB will automatically add a hidden rowid column as the primary key. To avoid an error in case the table already exists, you can include IF NOT EXISTS:
To show all of the columns from a table, use the statement or the \d :

Insert rows

To insert a row into a table, use followed by the table name and then the column values listed in the order in which the columns appear in the table:
If you want to pass column values in a different order, list the column names explicitly and provide the column values in the corresponding order:
To insert multiple rows into a table, use a comma-separated list of parentheses, each containing column values for one row:
are used when you leave specific columns out of your statement, or when you explicitly request default values. For example, both of the following statements create a row where the name, dl, and address entries each contain their default value, in this case NULL:

Create an index

help locate data without having to look through every row of a table. They’re automatically created for the of a table and any columns with a . To create an index for non-unique columns, use followed by an optional index name and an ON clause identifying the table and column(s) to index. For each column, you can choose whether to sort ascending (ASC) or descending (DESC).
You can create indexes during table creation as well; just include the INDEX keyword followed by an optional index name and the column(s) to index:

Show indexes

To show the indexes on a table, use followed by the name of the table:

Query a table

To query a table, use followed by a comma-separated list of the columns to be returned and the table from which to retrieve the data. You can also use the clause to restrict the number of rows retrieved:
To retrieve all columns, use the * wildcard:
To filter the results, add a WHERE clause identifying the columns and values to filter on:
To sort the results, add an ORDER BY clause identifying the columns to sort by. For each column, you can choose whether to sort ascending (ASC) or descending (DESC).

Update rows

To update rows in a table, use followed by the table name, a SET clause identifying the columns to update and their new values, and a WHERE clause identifying the rows to update:
If a table has a primary key, you can use that in the WHERE clause to reliably update specific rows; otherwise, each row matching the WHERE clause is updated. When there’s no WHERE clause, all rows in the table are updated.

Delete rows

To delete rows from a table, use followed by the table name and a WHERE clause identifying the rows to delete:
Just as with the UPDATE statement, if a table has a primary key, you can use that in the WHERE clause to reliably delete specific rows; otherwise, each row matching the WHERE clause is deleted. When there’s no WHERE clause, all rows in the table are deleted.

Remove a table

When you no longer need a table, use followed by the table name to remove the table and all its data:

What’s next?

  • Explore all
  • to execute statements from a shell or directly from the command line
  • for your preferred language
  • like automatic replication, rebalancing, and fault tolerance