Define a new, secondary index on a single, typed column of a column family.
CREATE INDEX [<index_name>]
ON <cf_name> (<column_name>);
CREATE INDEX creates a new, automatic secondary index on the given column family 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 because Cassandra is schema-optional. The column must already have a type specified when the family was created, or added afterward by altering the column family.
CREATE INDEX userIndex ON NerdMovies (user);
CREATE INDEX ON Mutants (abilityId);
Define a static column family and then create a secondary index on two of its named columns:
CREATE TABLE users (
userID uuid,
firstname text,
lastname text,
email text,
address text,
zip int,
state text,
PRIMARY KEY (userID)
);
CREATE INDEX user_state
ON users (state);
CREATE INDEX ON users (zip);
| CQL Commands | CQL Shell Commands |
|---|---|
| ALTER TABLE | ASSUME |
| ALTER KEYSPACE | CAPTURE |
| BATCH | COPY |
| CREATE TABLE | DESCRIBE |
| CREATE INDEX | EXIT |
| CREATE KEYSPACE | SHOW |
| DELETE | SOURCE |
| DROP TABLE | |
| DROP INDEX | |
| DROP KEYSPACE | |
| INSERT | |
| SELECT | |
| TRUNCATE | |
| UPDATE | |
| USE |