For Cassandra, do UPDATE
s become an implied INSERT
if the selected row does not exist? That is, if I say
UPDATE users SET name = "Raedwald" WHERE id = 545127
and id
is the PRIMARY KEY
of the users
table, and the table has no row with a key of 545127, will that be equivalent to
INSERT INTO users (id, name) VALUES (545127, "Raedwald")
I know that the opposite is true: an INSERT
for an id
that already exists becomes an UPDATE
of the row with that id
. Older Cassandra documentation talked about inserts actually being "upserts" for that reason.
I'm interested in the case for CQL3, Cassandra version 1.2+.
Yes, for Cassandra UPDATE
is synonymous with INSERT
, as explained in the CQL documentation where it says the following about UPDATE
:
Note that unlike in SQL,
UPDATE
does not check the prior existence of the row: the row is created if none existed before, and updated otherwise. Furthermore, there is no mean to know which of creation or update happened. In fact, the semantic ofINSERT
andUPDATE
are identical.
For the semantics to be different, Cassandra would need to do a read to know if the row already exists. Cassandra is write optimized, so you can always assume it doesn't do a read before write on any write operation. The only exception is counters (unlessreplicate_on_write = false
), in which case replication on increment involves a read.