liveBook · Manning

Primary key

As explained in ScyllaDB in Action

A primary key is a table key that uniquely identifies rows and determines their key-based ordering.

A primary key is one or more columns that uniquely identify a row: no two rows in a table can share the same complete key. In ScyllaDB, it does more than label a row. It also determines partitioning, data placement across the cluster, and key-based row ordering, so choosing it is a schema and performance decision rather than a final administrative detail. section 2.2.1 section 2.5

Figure 2.3 ScyllaDB consists of rows, identified by primary keys and grouped into partitions, which are grouped into tables, which are grouped into keyspaces.
figure

Use this hierarchy to place the primary key in context: it identifies rows, rows form partitions, partitions live in tables, and tables belong to keyspaces. section 2.2.1

Write the key in a CREATE TABLE statement. The first key field is the partition key; rows with the same value are grouped together, while the remaining fields distinguish rows within that group. For a food-review table, restaurant alone is not enough because a restaurant can have many visits and foods. Combining restaurant, ordered_at, and food makes each review row distinct. section 2.2.2 section 2.3.1

A food-review key

`PRIMARY KEY (restaurant, ordered_at, food)` uses `restaurant` as the partition key. Two reviews from Ernie’s Eats remain separate when their order time or food differs; a review for Really Good Ramen belongs to a different partition. This layout groups a restaurant’s reviews while preserving distinct entries. section 2.2.2 section 2.3.1

Start with the query you need to serve, then select a unique key that supports it. A stable ID is often a strong choice when a reader or web service must retrieve one article without carrying several fields in a URL. In denormalized tables, the same article ID can be paired with a different first key field—such as author, date bucket, or score—so each table supports a different lookup. section 3.3.2 section 5.3.2

Practical rules for defining a primary key:

  • Include enough columns to make a row unique; the values in all primary-key columns determine whether an insert represents a separate row. section 2.3.1
  • Put the field that should group data for a query first, because the first primary-key value is the partition key. section 2.2.2 section 3.3.2
  • Specify clustering order when the desired order differs from the default; summary tables use `id DESC` to put recent articles first. section 5.3.2
  • Supply a primary-key value when inserting a row, even when other non-key columns are not yet available. section 6.1.1

How ScyllaDB in Action covers it

How deep it goes
Explains definition, schema syntax, partition-key position, clustering order, inserts, and denormalized table variants without detailing identifier-generation drawbacks.
Examples
Uses restaurant reviews keyed by restaurant, order time, and food, plus article and article-summary tables keyed by IDs and query fields.
What it assumes
Introduces primary keys from rows, columns, and tables while briefly comparing familiar relational database terminology.
What you can do after
Apply it via CQL by defining keys that uniquely identify rows and align partitions and ordering with intended reads.
How it connects
Connects primary keys with partition keys, clustering order, denormalization, CQL INSERT statements, and TIMEUUID values.

Also explained in

Compare how 21 books explain primary key →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ScyllaDB in Action. The section links point to the parts of the book it draws on.