Define a new, secondary index on a single column of a table.
CREATE INDEX creates a new, automatic secondary index on the given table for the named column. Optionally, specify a name for the index itself before the ON keyword. Enclose a single column name in parentheses. It is not necessary for the column to exist on any current rows. The column and its data type must be specified when the table is created, or added afterward by altering the table.
If data already exists for the column, Cassandra indexes the data during the execution of this statement. After the index is created, Cassandra indexes new data for the column automatically when new data is inserted.
In this release, Cassandra supports creating an index on a table having a compound primary key. You cannot create a secondary index on the primary key itself. Cassandra does not support secondary indexes on collections.
Define a table and then create a secondary index on two of its named columns:
CREATE TABLE myschema.users (
userID uuid,
fname text,
lname text,
email text,
address text,
zip int,
state text,
PRIMARY KEY (userID)
);
CREATE INDEX user_state
ON myschema.users (state);
CREATE INDEX ON myschema.users (zip);
Define a table having a compound primary key and create a secondary index on it.
USE myschema;
CREATE TABLE timeline (
user_id varchar,
email_id uuid,
author varchar,
body varchar,
PRIMARY KEY (user_id, email_id)
);
CREATE INDEX ON timeline (author);