Apache Cassandra 1.2 Documentation

Creating a table

Continuing with the previous example, create a users table in the newly created keyspace:

CREATE TABLE users (
  user_name varchar,
  password varchar,
  gender varchar,
  session_token varchar,
  state varchar,
  birth_year bigint,
  PRIMARY KEY (user_name));

The users table has a single primary key.

Using compound primary keys

Use a compound primary key when you want to create columns that you can query to return sorted results. To create a table having a compound primary key, use two or more columns as the primary key:

CREATE TABLE emp (
  empID int,
  deptID int,
  first_name varchar,
  last_name varchar,
  PRIMARY KEY (empID, deptID));

The compound primary key is made up of the empID and deptID columns in this example. The empID acts as a partition key for distributing data in the table among the various nodes that comprise the cluster. The remaining component of the primary key, the deptID, acts as a clustering mechanism and ensures that the data is stored in ascending order on disk (much like a clustered index in Microsoft SQL Server). For more information about compound and clustering columns, see the Compound keys and clustering section.

Inserting data into the table

In production scenarios, inserting columns and column values programmatically is more practical than using cqlsh, but often, being able to test queries using this SQL-like shell is very convenient.

The following example shows how to use cqlsh to insert employee information for Jane Smith.

INSERT INTO emp (empID, deptID, first_name, last_name)
  VALUES (104, 15, 'jane', 'smith');