# What is the difference between Clustering, Primary, Partition, and Composite (or Compound) Keys in ScyllaDB?

**URL:** https://forum.scylladb.com/t/what-is-the-difference-between-clustering-primary-partition-and-composite-or-compound-keys-in-scylladb/41
**Category:** Knowledge Base
**Created:** [November 2, 2022, 11:04am UTC](https://forum.scylladb.com/t/what-is-the-difference-between-clustering-primary-partition-and-composite-or-compound-keys-in-scylladb/41 "2022-11-02T11:04:26Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![Guy](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.scylladb.com/guy/32/9_2.png) [@Guy](https://forum.scylladb.com/u/Guy)
#### Post date: [November 2, 2022, 11:04am UTC](https://forum.scylladb.com/t/what-is-the-difference-between-clustering-primary-partition-and-composite-or-compound-keys-in-scylladb/41/1 "2022-11-02T11:04:26Z")

</div>

In ScyllaDB (and Apache Cassandra for that matter) A Primary Key is defined within a table. It is one or more columns used to identify a row. All tables must include a definition for a Primary Key. For example, in the table:

```auto
CREATE TABLE heartrate_v1 (
pet_chip_id uuid,
time timestamp,
heart_rate int,
PRIMARY KEY (pet_chip_id)
);

```

The Primary Key is a single column – the pet\_chip\_id. If a Primary Key is made up of a single column, it is called a Simple Primary Key.

 ![row-1](https://us1.discourse-cdn.com/flex016/uploads/scylladb/original/1X/6f8d9473c4aa0ccc98799e57453dfd3db7b77128.png)

It’s also possible to define the Primary Key to include more than one column, in which case it is called a Composite (or Compound) key. For example:

```auto
CREATE TABLE heartrate_v2 (
pet_chip_id uuid,
time timestamp,
heart_rate int,
PRIMARY KEY (pet_chip_id, time)
);

```

In this case, the first part of the Primary Key is called the Partition Key (pet\_chip\_id in the above example) and the second part is called the Clustering Key (time).  
The Partition Key is responsible for data distribution across the nodes. It determines which node will store a given row. It can be one or more columns.  
 ![architecture_ring_modified](https://us1.discourse-cdn.com/flex016/uploads/scylladb/original/1X/5f008ef468526b6ef0cb5bd8a0710b77d64571ca.png)

The Clustering Key is responsible for sorting the rows within the partition. It can be zero or more columns.

 ![primary_key-2](https://us1.discourse-cdn.com/flex016/uploads/scylladb/original/1X/784eb62016b95c3ab9e306c61b7f38f80f911bd1.png)

If a table has more than one column defined as the Primary Key, for example:

```auto
CREATE TABLE heartrate_v3 (
pet_chip_id uuid,
time timestamp,
heart_rate int,
pet_name text,
PRIMARY KEY ((pet_chip_id, time), pet_name)
);

```

In this case, the Partition Key includes two columns: pet\_chip\_id and time, and the Clustering Key is pet\_name. Every query must include all the columns defined in the Partition Key (pet\_chip\_id and time) in this case.

Look at another example:

```auto
CREATE TABLE heartrate_v4 (
pet_chip_id uuid,
time timestamp,
heart_rate int,
pet_name text,
PRIMARY KEY (pet_chip_id, pet_name, heart_rate)
);

```

If there is more than one column in the Clustering Key (pet\_name and heart\_rate in the example above), the order of these columns defines the clustering order. For a given partition, all the rows are physically ordered inside ScyllaDB by the clustering order. This order determines what select queries you can efficiently run on this partition.  
In this example, the ordering is first by pet\_name and then by heart\_rate.

In addition to the Partition Key columns, a query may include the Clustering Key. If it does include the Clustering Key columns, they must be used in the same order as they were defined.

**Additional Resources:**

- [Architecture lesson](https://university.scylladb.com/courses/scylla-essentials-overview/lessons/architecture/) on ScyllaDB University
- [Basic Data Modeling lesson](https://university.scylladb.com/courses/data-modeling/lessons/basic-data-modeling-2/) on ScyllaDB University
- [Scylla Ring Architecture - Overview](https://docs.scylladb.com/stable/architecture/ringarchitecture/) on ScyllaDB Docs
