1.The primary key is a general concept to indicate one or more columns used to retrieve data from a Table.
- the primary key may be
SIMPLE
create table stackoverflow (
key text PRIMARY KEY,
data text
);- the primary key can also be
COMPOSITE(akaCOMPOUND), generated from more columns.
create table stackoverflow (
key_part_one text,
key_part_two int,
data text,
PRIMARY KEY(key_part_one, key_part_two)
);In a situation of COMPOSITE primary key, the "first part" of the key is called PARTITION KEY (in this example key_part_one is the partition key) and the second part of the key is the CLUSTERING KEY (key_part_two)
Please note that the both partition and clustering key can be made by more columns
create table stackoverflow (
k_part_one text,
k_part_two int,
k_clust_one text,
k_clust_two int,
k_clust_three uuid,
data text,
PRIMARY KEY((k_part_one,k_part_two), k_clust_one, k_clust_two, k_clust_three)
);The Partition Key is responsible for data distribution accross your nodes. The Clustering Key is responsible for data sorting within the partition. The Primary Key is equivalent to the Partition Key in a single-field-key table. The Composite/Compund Key is just a multiple-columns key.
Example:
SIMPLE KEY:
insert into stackoverflow (key, data) VALUES ('han', 'solo');
select * from stackoverflow where key='han';
key | data
----+------
han | solo
COMPOSITE/COMPOUND KEY can retrieve "wide rows"
insert into stackoverflow (key_part_one, key_part_two, data) VALUES ('ronaldo', 9, 'football player');
insert into stackoverflow (key_part_one, key_part_two, data) VALUES ('ronaldo', 10, 'ex-football player');
select * from stackoverflow where key_part_one = 'ronaldo';
key_part_one | key_part_two | data
--------------+--------------+--------------------
ronaldo | 9 | football player
ronaldo | 10 | ex-football player
select * from stackoverflow where key_part_one = 'ronaldo' and key_part_two = 10;
key_part_one | key_part_two | data
--------------+--------------+--------------------
ronaldo | 10 | ex-football player
Important note: the partition key is the minimum-specifier needed to perform a query using where clause. If you have a composite partition key, like the following
eg: PRIMARY KEY((col1, col2), col10, col4))
You can perform query only passing at least both col1 and col2, these are the 2 columns that defines the partition key. The "general" rule to make query is you have to pass at least all partition key columns, then you can add each key in the order they're set.
so the valid queries are (excluding secondary indexes)
- col1 and col2
- col1 and col2 and col10
- col1 and col2 and col10 and col 4
invalid:
- col1 and col2 and col4
- anything that does not contain both col1 and col2