Systems & Backend

Primary Key

The column(s) that uniquely identify each row in a table — the anchor foreign keys point to.

A primary key is the column (or set of columns) that uniquely identifies each row in a table. It is guaranteed unique and non-null, is automatically indexed, and is what foreign keys in other tables reference.

Worked example: a users table has id as its primary key; every row has a distinct id, and an orders table’s customer_id foreign key points at it. Choosing a surrogate key (auto-increment or UUID) versus a natural key (email) is a real design decision. Gotcha: auto-increment integers are compact and fast but leak row counts and collide across shards; random UUIDs are globally unique and shard-friendly but larger and bad for index locality — which is why time-ordered IDs (UUIDv7, Snowflake IDs) exist to get uniqueness without wrecking index performance.