Skip to main content
This page has instructions for updating existing rows of data in CockroachDB, using the following :
  • UPDATE, which updates existing rows in a table.
  • UPSERT, which inserts new rows in a table, updating existing rows that conflict on a primary key.
  • INSERT... ON CONFLICT... DO UPDATE, which inserts new rows in a table, updating existing rows in the event of a conflict on specified, UNIQUE -constrained columns.

Before you begin

Before reading this page, do the following:
  • or .
  • .
  • .
  • .
  • that you now want to update. In the examples on this page, we use sample data imported with the .

Use UPDATE

To update existing rows in a table, use an with a WHERE clause that filters on the columns that identify the rows that you want to update.
To update a large number of rows (i.e., tens of thousands of rows or more), we recommend iteratively updating subsets of the rows that you want to update, until all of the rows have been updated. You can write a script to do this, or you can write a loop into your application.For guidance and an example, see .

UPDATE SQL syntax

In SQL, UPDATE statements generally take the following form:
Where:
  • is the table to update.
  • is the column to update.
  • is the new value of the column in the matching row.
  • is the column to filter on.
  • is the matching value for the filter.
For detailed reference documentation on the UPDATE statement, including additional examples, see the .

UPDATE best practices

Here are some best practices to follow when updating rows:
  • Always specify a WHERE clause in UPDATE queries. If no WHERE clause is specified, CockroachDB will update all of the rows in the specified table.
  • To update a large number of rows (i.e., tens of thousands of rows or more), use a .
  • When executing UPDATE statements from an application, make sure that you wrap the SQL-executing functions in that can occur under .

UPDATE example

Suppose you want to change the status of all of the vehicles owned by a particular MovR user. To update all rows in the vehicles table with the owner_id equal to bd70a3d7-0a3d-4000-8000-000000000025:
SQL
Go (lib/pq)
Java (JDBC)
Python (psycopg2)

Use UPSERT

To insert new rows into a table, and update rows that conflict with the primary key value(s) of the new rows, use an .

UPSERT SQL syntax

In SQL, UPSERT statements take the following form:
Where:
  • is the table to update.
  • is a comma-separated list of the columns to which you want to insert values.
  • is a comma-separated list of values that you want to insert.
For detailed reference documentation on the UPSERT statement, including additional examples, see the .

UPSERT best practices

Here are some best practices to follow when using UPSERT:
  • Limit the number of UPSERT statements that you execute. It’s more efficient to insert multiple rows with a single statement than to execute multiple UPSERT statements that each insert a single row.
  • When executing UPSERT statements from an application, make sure that you wrap the SQL-executing functions in that can occur under contention.

UPSERT example

Suppose you want to add some promo codes to the MovR platform, and overwrite any existing promos with the same code. To insert new rows into the promo_codes table, and update any rows that have the same primary key code value:
SQL
Go (lib/pq)
Java (JDBC)
Python (psycopg2)

Use INSERT ON CONFLICT

To insert new rows into a table, and to update rows with UNIQUE-constrained values that conflict with any values of the new rows, use an INSERT... ON CONFLICT... DO UPDATE statement. INSERT... ON CONFLICT... DO UPDATE is semantically identical to UPSERT, when the conflicting values are in the primary key and the action to take on conflict is to update the conflicting rows with the new rows. INSERT... ON CONFLICT is more flexible than UPSERT, and can be used to consider uniqueness for columns not in the primary key. With INSERT... ON CONFLICT, you can also control how to update rows in the event of a conflict. This contrasts with the behavior of an UPSERT statement, which just overwrites conflicting rows with new rows. can be used for uniqueness checks.
Note that if you are inserting to/updating all columns of a table, and the table has no secondary indexes, UPSERT will be faster than the equivalent INSERT ON CONFLICT statement, as it will write without first reading.

INSERT ON CONFLICT SQL syntax

In SQL, INSERT... ON CONFLICT... DO UPDATE statements take the following form:
Where:
  • is the table to update.
  • is a comma-separated list of the columns to which you want to insert values.
  • is a comma-separated list of values that you want to insert.
  • is a comma-separated list of the columns to evaluate for a conflict.
  • is the column to update, on conflict.
  • is the value to assign the update column.
Note that the statement contains an UPDATE clause, which is semantically identical to an UPDATE statement.

INSERT ON CONFLICT best practices

Here are some best practices to follow when using INSERT... ON CONFLICT... DO UPDATE:
  • Limit the number of INSERT statements that you execute. It’s more efficient to insert multiple rows with a single statement than to execute multiple INSERT statements that each insert a single row.
  • When executing INSERT statements from an application, make sure that you wrap the SQL-executing functions in that can occur under contention.
  • Follow the page.

INSERT ON CONFLICT example

Suppose you want to record a particular user’s promo code usage count. The user_promo_codes table keeps track of user promo usage. If no usage counter exists, you want to insert a new row, and if one does exist, you want to increment the usage_count column by 1.
SQL
Go (lib/pq)
Java (JDBC)
Python (psycopg2)

See also

Reference information related to this task:
Other common tasks: