Counter Table in Cassandra

Category: Cassandra   Tags: Cassandra, Learning, Beginners, Basics, NoSQL Database, Cassandra CQL, Counter Table

A counter is a special column used to store an integer value that can be incremented. A counter columns can only be created in dedicated table that has only primary key and counter column. All non-counter columns in the table must be defined as part of the primary key.

    CREATE TABLE upvote_counter (
        id UUID PRIMARY KEY,
        upvotes counter
    );
                            

Use UPDATE command to add/update value to a counter table:

UPDATE upvote_counter
SET upvotes = upvotes + 1
WHERE id = 495ede98-0060-11e8-ba89-0ed5f89f718b;
                            

See the counter table data:

SELECT * FROM upvote_counter;