4
votes

I've read that lightweight transactions just support update and insert statements with an "if" and "if exists" clause. Do they also support delete statement with "if exists" clause.

Eg : create table user(userid text,email text, primary key(email))

delete from user where userid='kris' if exists

Do lightweight transactions support the above delete statement?

2

2 Answers

4
votes

Yes, the CQL DELETE statement does support the IF EXISTS clause. From the DELETE documentation:

In Cassandra 2.0.7 and later, you can conditionally delete columns using IF or IF EXISTS. Deleting a column is similar to making an insert or update conditionally. Conditional deletions incur a non-negligible performance cost and should be used sparingly.

However, to Carlo's point, take note of that last sentence. From a performance standpoint, the conditional delete is not free.

1
votes

The real question is: why do you need it? The compare and set is useful to handle race conditions -- e.g.: I don't want two users to register with same username. In this way a second attempt to register with same username will fail. But why would you delete a data if it exists when two delete operations are idempotent on your data? a delete has implicit IF IT EXISTS condition