Skip to content

Instantly share code, notes, and snippets.

@frankwis
Last active April 11, 2016 10:31
Show Gist options
  • Save frankwis/b2c92189c04227b9609cd0650e012d88 to your computer and use it in GitHub Desktop.
Save frankwis/b2c92189c04227b9609cd0650e012d88 to your computer and use it in GitHub Desktop.
Datastax’ CQL documentation gives the impression that INSERT and UPDATE statements are identical, but in fact they are not.
CREATE KEYSPACE IF NOT EXISTS test
WITH replication = {'class':'SimpleStrategy', 'replication_factor' : 1};
USE test;
CREATE TABLE IF NOT EXISTS test (
id int,
data text,
PRIMARY KEY (id)
);
//---- Create an entry via UPDATE and update with null value ----
UPDATE test SET data = 'hello world' WHERE id=1;
SELECT * FROM test;
/*
Query returns expected result:
id | data
----+-------------
1 | hello world
*/
UPDATE test SET data = null WHERE id=1;
SELECT * FROM test;
/*
Returns zero rows!!!
(0 rows)
*/
UPDATE test SET data='hello world' WHERE id=1 IF EXISTS;
/*
Yup, C* doesn't know this row any more
[applied]
-----------
False
*/
//---- Create an entry via INSERT and update with null value ----
INSERT INTO test (id, data) VALUES (2, 'hello');
UPDATE test SET data=null WHERE id=2;
SELECT * FROM test;
/*
Row is still present:
id | data
----+------
2 | null
(1 rows)
*/
DROP KEYSPACE IF EXISTS test;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment