Using lightweight transactions
INSERT and UPDATE statements that use the IF clause support lightweight transactions, also known as Compare and Set (CAS).
INSERT and UPDATE statements using the IF clause, support lightweight transactions, also known as Compare and Set (CAS).
Procedure
-
Register a new user.
INSERT INTO users (login, email, name, login_count) VALUES ('jdoe', 'jdoe@abc.com', 'Jane Doe', 1) IF NOT EXISTS;
-
Perform a CAS operation against a row that does exist by adding the predicate
for the operation at the end of the query. For example, reset Jane Doe's
password.
UPDATE users SET email = ‘janedoe@abc.com’ WHERE login = 'jdoe' IF email = ‘jdoe@abc.com’;