Apache Cassandra 1.0 Documentation

CREATE TABLE

This document corresponds to an earlier product version. Make sure you are using the version that corresponds to your version.

Latest Cassandra documentation | Earlier Cassandra documentation

Define a new column family.

Synopsis

CREATE TABLE <cf_name> (
 <key_column_name> <data_type> PRIMARY KEY
 [, <column_name> <data_type> [, ...] ] )
 [ WITH <storage_parameter> = <value>
 [AND <storage_parameter> = <value> [...] ] ];

Description

CREATE TABLE creates new column family namespaces under the current keyspace. You can also use the alias CREATE COLUMNFAMILY. Valid column family names are strings of alphanumeric characters and underscores, which begin with a letter.

The only schema information that must be defined for a column family is the primary key (or row key) and its associated data type. Other column metadata, such as the size of the associated row and key caches, can be defined.

CREATE TABLE Fish (KEY blob PRIMARY KEY);
CREATE TABLE FastFoodEatings (user text PRIMARY KEY)
  WITH comparator=timestamp AND default_validation=int;

Specifying the Key Type

When creating a new column family, specify the key type. The list of possible types is identical to column comparators/validators (see CQL Data Types), except it probably does not make sense to use counter for a key. The key type must be compatible with the partitioner in use. For example, OrderPreservingPartitioner and CollatingOrderPreservingPartitioner require UTF-8 keys. If you use an identifier for the primary key name, instead of the KEY keyword, a key alias is set automatically.

Specifying Column Types (optional)

You can assign columns a type during column family creation. These columns are validated when a write occurs, and intelligent CQL drivers and interfaces can decode the column values correctly when receiving them. Column types are specified as a parenthesized, comma-separated list of column term and type pairs. See CQL Data Types for the available types.

Column Family Options (not required)

A number of optional keyword arguments can be supplied to control the configuration of a new column family. See CQL Column Family Storage Parameters for the column family options you can define.

Define a default data type for column names using this syntax: WITH comparator=<data_type>. Define values using this syntax: WITH default_validation=<data_type>.

CREATE TABLE MonkeyTypes (
  KEY uuid PRIMARY KEY,
  species text,
  alias text,
  population varint
  ) WITH comment='Important biological records'
    AND read_repair_chance = 1.0;

Examples

Dynamic column family definition:

CREATE TABLE user_events (user text PRIMARY KEY)
   WITH comparator=timestamp AND default_validation=int;

Static column family definition:

CREATE TABLE users (
   KEY uuid PRIMARY KEY,
   username text,
   email text )
   WITH comment='user information'
   AND read_repair_chance = 1.0;