Database Reference
In-Depth Information
•
Composite partition key
: Optionally, CQL lets you define a composite partition
key (the first part of a composite key). This key helps you distribute data across
nodes if any part of the composite partition key differs. Let's take a look at the
following example:
CREATE TABLE customers (
id uuid,
email text,
PRIMARY KEY (id)
)
In the preceding example,
id
is the primary key and also the partition key. There is no
clustering. It is a simple key. Let's add a twist to the primary key:
CREATE TABLE country_states (
country text,
state text,
population int,
PRIMARY KEY (country, state)
)
In the preceding example, we have a composite key that uses
country
and
state
to
uniquely define a CQL row. The
country
column is the partition key, so all the rows
with the same country node will belong to the same node/machine. The rows within a par-
tition will be sorted by the
state
names. So, when you query for states in the US, you
will encounter the row with California before the one with New York. What if I want to
partition by composition? Let's take a look at the following example:
CREATE TABLE country_chiefs (
country text,
prez_name text,
num_states int,
capital text,
ruling_year int,
PRIMARY KEY ((country, prez_name), num_states, capital)
)
The preceding example has a composite key involving four columns:
country
,
prez_name
,
num_states
, and
capital
, with
country
and
prez_name
consti-