<a id="update-statement"></a>

# UPDATE

Updating a row is done using an `UPDATE` statement:

```cql
update_statement: UPDATE `table_name`
                : [ USING `update_parameter` ( AND `update_parameter` )* ]
                : SET `assignment` ( ',' `assignment` )*
                : WHERE `where_clause`
                : [ IF ( EXISTS | `condition` ( AND `condition` )*) ]
update_parameter: ( TIMESTAMP `int_value` | TTL  `int_value` | TIMEOUT `duration` )
int_value: ( `integer` | `bind_marker` )
assignment: `simple_selection` '=' `term`
          : | `column_name` '=' `column_name` ( '+' | '-' ) `term`
          : | `column_name` '=' `list_literal` '+' `column_name`
simple_selection: `column_name`
                : | `column_name` '[' `term` ']'
                : | `column_name` '.' `field_name`
condition: `simple_selection` `operator` `term`
```

For instance:

```cql
UPDATE NerdMovies USING TTL 400
   SET director   = 'Joss Whedon',
       main_actor = 'Nathan Fillion',
       year       = 2005
 WHERE movie = 'Serenity';

UPDATE UserActions
   SET total = total + 2
   WHERE user = B70DE1D0-9908-4AE3-BE34-5573E5B09F14
     AND action = 'click';
```

The `UPDATE` statement writes one or more columns for a given row in a table. The `where_clause` is used to
select the row to update and must include all columns composing the `PRIMARY KEY`. Non-primary key columns are then
set using the `SET` keyword.

Note that unlike in SQL, `UPDATE` does not check the prior existence of the row by default,

(except through [IF](), see below):

the row is created if none existed before, and updated otherwise. Furthermore, there is no way to know
whether creation or update occurred.

In an `UPDATE` statement, all updates within the same partition key are applied atomically,
meaning either all provided values are stored or none at all.

Similarly to `INSERT`, `UPDATE` statement happening concurrently at different
cluster nodes proceed without coordination. Cell values
supplied by a statement with the highest timestamp will prevail.
If two `UPDATE` statements or `UPDATE` and `INSERT`
statements have the same timestamp, a conflict resolution algorithm determines which cells prevails
(see [update ordering](https://docs.scylladb.com/manual/master/cql/dml.md#update-ordering)).

Regarding the `assignment`:

- `c = c + 3` is used to increment/decrement counters. The column name after the ‘=’ sign **must** be the same as
  the one before the ‘=’ sign. Note that increment/decrement is only allowed on counters, and are the *only* update
  operations allowed on counters. See the section on [counters](https://docs.scylladb.com/manual/master/cql/types.md#counters) for details.
- `id = id + <some-collection>` and `id[value1] = value2` are for collections, see the [relevant section](https://docs.scylladb.com/manual/master/cql/types.md#collections) for details.
- `id.field = 3` is for setting the value of a field on non-frozen user-defined types.

Please refer to the [update parameters](https://docs.scylladb.com/manual/master/cql/dml.md#update-parameters) section for more information on the `update_parameter`.

<a id="if"></a>

## IF condition

It is, however, possible to use the conditions on some columns through `IF`, in which case the row will not be updated
unless the conditions are met. Each such `UPDATE` gets a globally unique timestamp.
But, please note that using `IF` conditions will incur a non-negligible performance
cost (internally, Paxos will be used), so this should be used sparingly.

[Apache Cassandra Query Language (CQL) Reference](https://docs.scylladb.com/manual/master/cql/index.md)

Copyright

© 2016, The Apache Software Foundation.

Apache®, Apache Cassandra®, Cassandra®, the Apache feather logo and the Apache Cassandra® Eye logo are either registered trademarks or trademarks of the Apache Software Foundation in the United States and/or other countries. No endorsement by The Apache Software Foundation is implied by the use of these marks.

<!-- Licensed to the Apache Software Foundation (ASF) under one -->
<!-- or more contributor license agreements.  See the NOTICE file -->
<!-- distributed with this work for additional information -->
<!-- regarding copyright ownership.  The ASF licenses this file -->
<!-- to you under the Apache License, Version 2.0 (the -->
<!-- "License"); you may not use this file except in compliance -->
<!-- with the License.  You may obtain a copy of the License at -->
<!-- http://www.apache.org/licenses/LICENSE-2.0 -->
<!-- Unless required by applicable law or agreed to in writing, software -->
<!-- distributed under the License is distributed on an "AS IS" BASIS, -->
<!-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -->
<!-- See the License for the specific language governing permissions and -->
<!-- limitations under the License. -->
