Cassandra UPDATE Data
The UPDATE statement modifies column values in an existing row. In Cassandra, UPDATE and INSERT share the same underlying write mechanism — both are upserts. If you UPDATE a row that does not exist, Cassandra creates it. This is intentional and is a consequence of Cassandra's log-structured storage model.
Basic UPDATE Syntax
UPDATE keyspace_name.table_name [USING TTL seconds] [USING TIMESTAMP microseconds] SET column1 = value1, column2 = value2, ... WHERE primary_key_condition;
Simple UPDATE
UPDATE customers SET email = 'alice.new@example.com', loyalty_points = 500 WHERE customer_id = 11111111-1111-1111-1111-111111111111;
Before: customer_id | first_name | email | loyalty_points ────────────────────────────────────────────────────────────────── uuid-A | Alice | alice@example.com | 0 After: customer_id | first_name | email | loyalty_points ────────────────────────────────────────────────────────────────── uuid-A | Alice | alice.new@example.com| 500
UPDATE with Full Composite Primary Key
When a table has clustering columns, you must provide all primary key columns in the WHERE clause.
-- Table PRIMARY KEY (customer_id, order_date, order_id) UPDATE orders_by_customer SET status = 'shipped' WHERE customer_id = [uuid] AND order_date = '2024-05-01 10:00:00' AND order_id = [order-uuid];
Increment and Decrement Counter Columns
Counter columns work differently from regular columns. You cannot SET a counter to a fixed value — you can only add or subtract from it.
UPDATE page_views SET views = views + 1 WHERE page_id = '/home'; UPDATE page_views SET views = views - 1 WHERE page_id = '/home';
Updating Collection Columns
Update a LIST — Append an Item
UPDATE playlists SET songs = songs + ['New Track'] WHERE playlist_id = [uuid];
Update a SET — Add and Remove Elements
UPDATE articles SET tags = tags + {'advanced'}
WHERE article_id = [uuid];
UPDATE articles SET tags = tags - {'beginner'}
WHERE article_id = [uuid];
Update a MAP — Set a Specific Key
UPDATE user_prefs SET settings['theme'] = 'light' WHERE user_id = [uuid];
UPDATE with TTL
You can assign a TTL to individual columns during an UPDATE. The column expires independently of other columns in the same row.
UPDATE session_data USING TTL 3600 SET access_token = 'abc123' WHERE session_id = [uuid];
UPDATE with Custom Timestamp
Override the automatic timestamp to control which version of a value wins when replicas conflict.
UPDATE customers USING TIMESTAMP 1710000000000000 SET email = 'restored@example.com' WHERE customer_id = [uuid];
UPDATE IF EXISTS (Conditional)
The IF EXISTS clause runs the update only when the row already exists. It uses a lightweight transaction and is slower than a standard UPDATE.
UPDATE customers SET email = 'new@example.com' WHERE customer_id = [uuid] IF EXISTS;
UPDATE with IF condition (Compare and Swap)
You can update a column only when its current value matches a specific condition. This is Cassandra's compare-and-set operation, also called a lightweight transaction (LWT).
UPDATE products SET stock = 49 WHERE product_id = [uuid] IF stock = 50; -- Only update if current stock is 50 [applied] | stock -----------+------- True | -- applied because stock was 50 False | 47 -- not applied; actual stock was 47
NULL vs Tombstones in UPDATE
Setting a column to NULL in an UPDATE writes a tombstone — a deletion marker. Tombstones consume storage and slow down reads if accumulated in large numbers. Instead of setting columns to null, delete the column explicitly or redesign the data model to avoid the need for null values.
-- Creates a tombstone (avoid this): UPDATE customers SET phone = null WHERE customer_id = [uuid]; -- Better: use DELETE on the specific column: DELETE phone FROM customers WHERE customer_id = [uuid];
Summary
UPDATE in Cassandra is an upsert — it creates the row if it does not exist. Always supply the full primary key in the WHERE clause. Use collection-specific syntax to modify list, set, and map columns. Counter columns use arithmetic updates, not direct assignment. Prefer DELETE over null assignments to avoid tombstone buildup. Lightweight transactions (IF EXISTS, IF condition) provide conditional updates at the cost of higher latency.
