Using a counter

A counter is a special column for storing a number that is updated by increments or decrements.

To load data into a counter column, or to increase or decrease the value of the counter, use the UPDATE command. DataStax Enterprise rejects USING TIMESTAMP or USING TTL in the command to update a counter column.

Procedure

  • Create a table for the counter column.
    USE cycling;
    CREATE TABLE popular_count (
      id UUID PRIMARY KEY,
      popularity counter
      );
  • Loading data into a counter column is different than other tables. The data is updated rather than inserted.
    UPDATE cycling.popular_count
     SET popularity = popularity + 1
     WHERE id = 6ab09bec-e68e-48d9-a5f8-97e6fb4c9b47;
  • Look at the counter value and note that popularity has a value of 1.
    SELECT * FROM cycling.popular_count;
  • Additional increments or decrements changes the value of the counter column.