Apache Cassandra 1.0 Documentation

UPDATE

This document corresponds to an earlier product version. Make sure you are using the version that corresponds to your version.

Latest Cassandra documentation | Earlier Cassandra documentation

Updates one or more columns in the identified row of a column family.

Synopsis

UPDATE <column_family>
   [ USING <write_option> [ AND <write_option> [...] ] ];
     SET <column_name> = <column_value> [, ...]
       | <counter_column_name> = <counter_column_name> {+ | -} <integer>
     WHERE <row_specification>;

<write_option> is:

CONSISTENCY <consistency_level>
TTL <seconds>
TIMESTAMP <integer>

<row_specification> is:

KEY | <key_alias> = <key_value>
KEY | <key_alias> IN (<key_value> [,...])

Description

An UPDATE writes one or more columns to a record in a Cassandra column family. No results are returned. Row/column records are created if they do not exist, or overwritten if they do exist.

A statement begins with the UPDATE keyword followed by a Cassandra column family name. To update multiple columns, separate the name/value pairs using commas.

The SET clause specifies the new column name/value pairs to update or insert. Separate multiple name/value pairs using commas. If the named column exists, its value is updated, otherwise, its value is inserted. To update a counter column value in a counter column family, specify a value to increment or decrement value the current value of the counter column.

Each update statement requires a precise set of row keys to be specified using a WHERE clause and the KEY keyword or key alias.

UPDATE Movies SET col1 = val1, col2 = val2 WHERE KEY = key1;
UPDATE Movies SET col3 = val3 WHERE KEY IN (key1, key2, key3);
UPDATE Movies SET col4 = 22 WHERE keyalias = key4;
UPDATE NerdMovies USING CONSISTENCY ALL AND TTL 400
     SET 'A 1194' = 'The Empire Strikes Back',
         'B 1194' = 'Han Solo'
     WHERE KEY = B70DE1D0-9908-4AE3-BE34-5573E5B09F14;

UPDATE UserActionCounts SET total = total + 2 WHERE keyalias = 523;

You can specify these options:

TTL columns are automatically marked as deleted (with a tombstone) after the requested amount of time has expired.

Examples

Update a column in several rows at once:

UPDATE users USING CONSISTENCY QUORUM
     SET 'state' = 'TX'
     WHERE KEY IN (88b8fd18-b1ed-4e96-bf79-4280797cba80,
                   06a8913c-c0d6-477c-937d-6c1b69a95d43,
                   bc108776-7cb5-477f-917d-869c12dfffa8);

Update several columns in a single row:

UPDATE users USING CONSISTENCY QUORUM
  SET 'name' = 'John Smith', 'email' = 'jsmith@cassie.com'
  WHERE user_uuid = 88b8fd18-b1ed-4e96-bf79-4280797cba80;

Update the value of a counter column:

UPDATE page_views USING CONSISTENCY QUORUM AND TIMESTAMP=1318452291034
  SET 'index.html' = 'index.html' + 1
  WHERE KEY = 'www.datastax.com';