Start CockroachDB
If you haven’t already, install CockroachDB. Then run thecockroach demo command:
movr dataset pre-loaded.
Show tables
To see all tables in the active database, use theSHOW TABLES statement or the \dt shell command:
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 themovr database. To create a table, use CREATE TABLE followed by a table name, the column names, and the data type and constraint, if any, for each column:
rowid column as the primary key.
To avoid an error in case the table already exists, you can include IF NOT EXISTS:
SHOW COLUMNS FROM statement or the \d shell command:
Insert rows
To insert a row into a table, useINSERT INTO followed by the table name and then the column values listed in the order in which the columns appear in the table:
name, dl, and address entries each contain their default value, in this case NULL:
Create an index
Indexes help locate data without having to look through every row of a table. They’re automatically created for the primary key of a table and any columns with aUNIQUE constraint.
To create an index for non-unique columns, use CREATE INDEX 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).
INDEX keyword followed by an optional index name and the column(s) to index:
Show indexes
To show the indexes on a table, useSHOW INDEX FROM followed by the name of the table:
Query a table
To query a table, useSELECT 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 LIMIT clause to restrict the number of rows retrieved:
* wildcard:
WHERE clause identifying the columns and values to filter on:
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, useUPDATE 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:
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, useDELETE FROM followed by the table name and a WHERE clause identifying the rows to delete:
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, useDROP TABLE followed by the table name to remove the table and all its data:
What’s next?
- Explore all SQL Statements
- Use the built-in SQL client to execute statements from a shell or directly from the command line
- Install the client driver for your preferred language and build an app
- Explore CockroachDB features like automatic replication, rebalancing, and fault tolerance

