Creating a table
How to create CQL tables.
WITH
clause and
keyword arguments to configure table properties (caching, compaction, etc.). See the table_options page for details. Create schema using cqlsh
Create table schema using cqlsh
. Cassandra does not support dynamic schema
generation — collision can occur if multiple clients attempt to generate tables
simultaneously. To recover from collisions, follow the instructions in schema collision fix.
Primary Key
A primary key identifies the location and order of stored data. The primary key is defined when the table is created and cannot be altered. If you must change the primary key, create a new table schema and write the existing data to the new table. See ALTER TABLE for details on altering a table after creation.
The definition of a table's primary key is critical in Cassandra. Carefully model how data in a table will be inserted and retrieved before choosing which columns to define in the primary key. The size of the partitions, the order of the data within partitions, the distribution of the partitions among the nodes of the cluster — you must consider all of these when selecting the table's primary key.
Table characteristics
- To specify the keyspace that contains the table, put the keyspace name followed by a period before the table name: keyspace_name.table_name. This allows you to create a new table in a keyspace that is different from the one set for the current session (by the USE command, for example).
- To create a table in the current keyspace, just use the new table name.
Column characteristics
CREATE TABLE cycling.cyclist_alt_stats ( id UUID PRIMARY KEY, lastname text, birthday timestamp, nationality text, weight text, height text );
CREATE TABLE cycling.whimsey ( id UUID PRIMARY KEY, lastname text, cyclist_teams set<text>, events list<text>, teams map<int,text> );
Collection types cannot be nested. Collections can include frozen data
types. For examples and usage, see Collection type.CREATE TABLE cycling.route (race_id int, race_name text, point_id int, lat_long tuple<text, tuple<float,float>>, PRIMARY KEY (race_id, point_id));
frozen <tuple <int, tuple<text, double>>>
Create a User-defined type (UDTs) as a data type of several fields, using CREATE TYPE. It is best to create a
UDT for use with multiple table definitions. The user-defined column type (UDT) requires the
frozen keyword. A frozen value serializes multiple components into a single value.
Non-frozen types allow updates to individual fields. Cassandra treats the value of a frozen
type as a blob. The entire value must be overwritten.
The scope of a user-defined type is the keyspace in which you define it. Use dot
notation to access a type from a keyspace outside its scope: keyspace name followed by a
period followed the name of the type, for example:
test.myType
where test
is the keyspace name and myType
is the type
name.
Cassandra accesses the type in the specified keyspace, but does not change the current
keyspace; otherwise, if you do not specify a keyspace, Cassandra accesses the type within the
current keyspace. For examples and usage information, see "Using a user-defined type".
A counter is a special column used to store a number that is changed in increments. A counter can only be used in a dedicated table that includes a column of counter data type. For more examples and usage information, see "Using a counter".