Skip to content
BENIBIX Learn

Primary Key

Development🗃️ DatabasesDatabase Basics

Also known as PK, Primary Key Column

A column with a different value in each row. That is how one row is found.

Think of it like

Think of it like the number on a shirt in a team. Two kids can share a name, but not a number.

Example

Two customers are both called Jack. Their ids are 2 and 7, so the shop never sends the wrong one an order.

Why it matters

Without a unique key, you cannot safely change or delete one row. You might hit two.

Definition

A primary key is the column that gives each row in a table its own unique value. No two rows can have the same key, and it can never be empty. It is often a simple number, like id, that the database counts up by itself. Other tables use it to point at a row without confusion. Every table should have one.

Example code

sql
INSERT INTO customers (id, name) VALUES (1, 'Zoe');
-- Error: UNIQUE constraint failed: customers.id
There is a row 1 already, so a second one is refused.

Common mistake

Using something that can change, like an email, as the key. When it changes, every link to it breaks.

Key takeaways

  1. 1
    Unique. No two rows share it.
  2. 2
    Never empty. Every row must have one.
  3. 3
    Never changes. Other tables rely on it.

Compare