Database Reference
In-Depth Information
Updates can create new rows
The INSERT
statements aren't the only write operations in CQL that can have unexpec-
ted effects. To see how
UPDATE
can take us by surprise, let's update
gina
's row to add a
location:
UPDATE "users"
SET "location" = 'Houston, TX'
WHERE "username" = 'gina';
Wait a second—who's
gina
? So far, we haven't had a user with the username
gina
in our
table. We might expect the preceding update statement to have no effect: after all, our
WHERE
statement specifies a row that does not exist.
The contents of the
users
table, however, tell a different story:
There is now a row with the primary key
gina
. It turns out that an
UPDATE
statement, just
like an
INSERT
statement, is in fact an upsert operation: if the specified row exists, it will
update values in the row, but if it doesn't, a new row will be created.
As it turns out, in their basic forms, the
INSERT
and
UPDATE
queries are syntactic vari-
ants of the exact same underlying operation. For instance, the following two queries are
functionally identical to one another:
INSERT INTO "users"
("username", "email", "encrypted_password")
VALUES
('alice', 'alice@gmail.com',
0x8914977ed729792e403da53024c6069a9158b8c4);