Cassandra Tables

Tables in Cassandra store your data, just as they do in any database. Each table has named columns with specific data types. What makes Cassandra tables different is how they store and organize data on disk — always optimized around the queries you plan to run, not around general-purpose normalization rules.

The Spreadsheet Analogy (and Its Limits)

Think of a Cassandra table like a giant spreadsheet. Rows run horizontally and columns run vertically. Each cell holds a value. Unlike a spreadsheet, a Cassandra table can hold billions of rows without slowing down. Also, rows in the same table do not need to have values for every column — absent values cost no storage space.

Table: employees

employee_id (PK) | name    | department | salary | city
─────────────────────────────────────────────────────────────
EMP001           | Alice   | Engineering| 85000  | New York
EMP002           | Bob     | Marketing  |        | Chicago
EMP003           | Carol   | Engineering| 92000  |

Bob has no salary recorded and Carol has no city — those absent cells take no space in Cassandra.

Creating a Table

CREATE TABLE keyspace_name.table_name (
  column_name1 data_type,
  column_name2 data_type,
  column_name3 data_type,
  PRIMARY KEY (column_name1)
);

Full Example: Products Table

USE ecommerce;

CREATE TABLE products (
  product_id   UUID,
  name         TEXT,
  category     TEXT,
  price        DECIMAL,
  stock        INT,
  added_on     TIMESTAMP,
  PRIMARY KEY (product_id)
);

Column Types Overview

Category        Data Types
─────────────────────────────────────────────────────────────
Text            TEXT, VARCHAR, ASCII
Numbers         INT, BIGINT, SMALLINT, TINYINT, FLOAT, DOUBLE, DECIMAL
Boolean         BOOLEAN
Date/Time       DATE, TIME, TIMESTAMP, TIMEUUID
Unique ID       UUID, TIMEUUID
Binary          BLOB
Network         INET
Collections     LIST, SET, MAP
Custom          User-Defined Types (UDT)

Common Column Rules

Column Names Are Case-Insensitive by Default

Cassandra lowercases all column names unless you wrap them in double quotes. Writing ProductName and productname refer to the same column.

-- These two columns are the same:
CREATE TABLE items (ProductName TEXT, PRIMARY KEY (ProductName));
CREATE TABLE items (productname TEXT, PRIMARY KEY (productname));

-- Use quotes to preserve case:
CREATE TABLE items ("ProductName" TEXT, PRIMARY KEY ("ProductName"));

NULL Values

Cassandra does not store explicit NULLs. When you read a column that was never written, Cassandra returns null at the application level, but nothing is stored on disk. This makes Cassandra very efficient when many columns are optional.

Inserting Data into a Table

INSERT INTO products (product_id, name, category, price, stock)
VALUES (uuid(), 'Wireless Mouse', 'Electronics', 29.99, 150);

Selecting Data from a Table

SELECT * FROM products;
SELECT name, price FROM products;
SELECT name, price FROM products WHERE product_id = [some-uuid];

Altering a Table

You can add or drop columns after a table is created. Renaming a primary key column is not allowed.

Add a Column

ALTER TABLE products ADD description TEXT;

Drop a Column

ALTER TABLE products DROP description;

Change Column Type (limited)

You can only change a column's type when the new type is compatible with the stored bytes. For example, INT can change to BIGINT.

ALTER TABLE products ALTER stock TYPE BIGINT;

Dropping a Table

Dropping a table removes the table definition and all its data permanently.

DROP TABLE products;

TRUNCATE a Table

TRUNCATE deletes all rows from a table but keeps the table structure intact. It runs faster than a DELETE of every row.

TRUNCATE products;

Table Properties

Cassandra tables accept optional properties that control storage and performance behavior.

CREATE TABLE products (
  product_id UUID PRIMARY KEY,
  name TEXT,
  price DECIMAL
)
WITH compaction = {
  'class': 'LeveledCompactionStrategy'
}
AND comment = 'Stores product catalog data'
AND gc_grace_seconds = 864000;

gc_grace_seconds

This setting tells Cassandra how long to keep tombstones (deletion markers) before removing them. The default is 10 days (864000 seconds). Setting it too low can cause deleted data to reappear if a node was offline when the deletion happened.

Viewing Table Definitions

DESCRIBE TABLE products;

CREATE TABLE ecommerce.products (
  product_id uuid PRIMARY KEY,
  added_on   timestamp,
  category   text,
  name       text,
  price      decimal,
  stock      int
) WITH ...

Listing All Tables in a Keyspace

USE ecommerce;
DESCRIBE TABLES;

products  orders  customers

IF NOT EXISTS and IF EXISTS

Use these clauses to avoid errors when a table already exists or does not exist yet.

CREATE TABLE IF NOT EXISTS products (
  product_id UUID PRIMARY KEY,
  name TEXT
);

DROP TABLE IF EXISTS products;

Summary

Cassandra tables store data in rows and columns. Columns have defined types and absent values cost zero storage. You create tables with a PRIMARY KEY clause that determines how data is distributed and sorted. You can add and drop columns after creation, and table properties let you control compaction and garbage collection behavior. Designing the right table structure starts with understanding your queries, which the upcoming topics cover in depth.

Leave a Comment

Your email address will not be published. Required fields are marked *