Avoid Upsert in YCQL

By default, inserts into a YCQL table overwrite data on primary key collisions. So INSERTs do an UPSERT. This an intended CQL feature.

To override this behavior, add the “IF NOT EXISTS” clause to the INSERT statement. 

This will cause the INSERT to fail if the row exists.

Example:

				
					cassandra@ycqlsh> CREATE KEYSPACE some_keyspace;

cassandra@ycqlsh> CREATE TABLE some_keyspace.some_table(c1 INT PRIMARY KEY, c2 VARCHAR);

cassandra@ycqlsh> INSERT INTO some_keyspace.some_table(c1, c2) VALUES (1, 'ABC');

cassandra@ycqlsh> SELECT * FROM some_keyspace.some_table;

 c1 | c2
----+-----
  1 | ABC

(1 rows)
				
			

Standard UPSERT operation:

				
					cassandra@ycqlsh> INSERT INTO some_keyspace.some_table(c1, c2) VALUES (1, 'XYZ');

cassandra@ycqlsh> SELECT * FROM some_keyspace.some_table;

 c1 | c2
----+-----
  1 | XYZ

(1 rows)
				
			

Override UPSERT mode, causing the INSERT to fail:

				
					cassandra@ycqlsh> INSERT INTO some_keyspace.some_table(c1, c2) VALUES (1, 'ABC') IF NOT EXISTS;

 [applied] | c1
-----------+----
     False |  1

cassandra@ycqlsh> SELECT * FROM some_keyspace.some_table;

 c1 | c2
----+-----
  1 | XYZ

(1 rows)
				
			

Have Fun!