Constraints

Columns can be constrained in two ways:

The values of a constrained column must comply with the constraint.

Primary Key

The primary key constraint combines a unique constraint and a not-null constraint. It also defines the default routing value used for sharding.

Example:

  1. cr> create table my_table1 (
  2. ... first_column integer primary key,
  3. ... second_column string
  4. ... );
  5. CREATE OK, 1 row affected (... sec)

Currently primary keys cannot be auto generated and have to be specified if data is inserted, otherwise an error is returned.

Defining multiple columns with a primary key constraint is also supported:

  1. cr> create table my_table1pk (
  2. ... first_column integer primary key,
  3. ... second_column string primary key,
  4. ... third_column string
  5. ... );
  6. CREATE OK, 1 row affected (... sec)

Or using a alternate syntax:

  1. cr> create table my_table1pk1 (
  2. ... first_column integer,
  3. ... second_column string,
  4. ... third_column string,
  5. ... primary key (first_column, second_column)
  6. ... );
  7. CREATE OK, 1 row affected (... sec)

Note

Not all column types can be used as PRIMARY KEY.

See Tables Constraints.

Not Null

The not null constraint can be used on any table column and it prevents null values from being inserted.

Example:

  1. cr> create table my_table2 (
  2. ... first_column integer primary key,
  3. ... second_column string not null
  4. ... );
  5. CREATE OK, 1 row affected (... sec)

For further details see NOT NULL.