Bringing together the Apache Cassandra experts from the community and DataStax.

Want to learn? Have a question? Want to share your expertise? You are in the right place!

Not sure where to begin? Getting Started

 

question

mishra.anurag643_153409 avatar image
mishra.anurag643_153409 asked ·

Why is the clustering key not required when updating the value of column?

CREATE TABLE mytable (
    a int,
    b int,
    s int static,
    d text,
    PRIMARY KEY (a, b)
)
BEGIN BATCH
    INSERT INTO mytable (a, b, d) values (7, 7, 'a')
    UPDATE mytable SET s = 7 WHERE a = 7 IF s = NULL;
APPLY BATCH

table has partition key as a and clustering key as b but in the update statement only partition key is provided and above batch returns :

[applied]
-----------
True

I was just wondering how only mentioning partition key in update command it is not throwing an error , when primary key has both partition as well clustering key.

cql
10 |1000 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

1 Answer

Erick Ramirez avatar image
Erick Ramirez answered ·

You only need to specify the partition key when updating the value of the s column because it is a static type as it states in the CQL UPDATE doc page where you copied it from:

Note: To update a static column, you only need to specify the partition key.

The reason is that a static column has the same value for all rows in the partition hence the term "static". It doesn't matter which row in the partition you are modifying -- the value is the same for all rows so the clustering key is not required.

On the other hand, if the s column was not static:

CREATE TABLE community.notstatic (
    a int,
    b int,
    d text,
    s int,
    PRIMARY KEY (a, b)
)

Then you will not be able to update the value of s without specifying a clustering key:

cqlsh> UPDATE notstatic SET s = 7 WHERE a = 7 IF s = null;
InvalidRequest: Error from server: code=2200 [Invalid query] message="Some clustering keys are missing: b"

You need to include the clustering key in this instance because you need to specify the row in the partition you are updating. Cheers!

Share
10 |1000 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.