← Database & SQL
MODULE 05 · DATABASE & SQL

Primary Key

A column, or group of columns, that uniquely identifies each row in a table.

01 · What is it?

A primary key is a column, or group of columns, that uniquely identifies each row in a table.

Every row has one primary-key value. That value lets software refer to one exact record without guessing which row it means.

Simple definition: A primary key is the table's unique label for each row.

02 · Think of it like…

Think of a primary key like a passport number or library card number.

Many people can share a name, but each person can have a different official number. A table uses its primary key in the same way: it points to one specific row.

TableUsersMany rows
RowRebeccaOne record
Primary keyuser_id = 42Unique value
ResultOne exact userNo ambiguity

03 · What does it do?

A primary key gives the database and application a stable way to:

Identify a row

Find one exact record in a table.

Prevent duplicates

Stop two rows from using the same key.

Support relationships

Give other tables a value they can reference.

Make updates safe

Change or delete the intended row.

04 · Where is it used?

Primary keys are used in relational tables whenever software needs to keep records distinct and addressable.

Users

user_id identifies one account.

Orders

order_id identifies one purchase.

Products

product_id identifies one catalog item.

Payments

payment_id identifies one payment record.

05 · Example

This table uses user_id as its primary key. The value 42 identifies exactly one row.

CREATE TABLE users (
  user_id INTEGER PRIMARY KEY,
  email TEXT NOT NULL,
  display_name TEXT NOT NULL
);

SELECT * FROM users WHERE user_id = 42;
user_idemaildisplay_name
41alex@example.comAlex
42rebecca@example.comRebecca
43sam@example.comSam

The query can safely target Rebecca's row because user_id = 42 is unique.

06 · How it connects

ApplicationEdit user
SQLWHERE user_id = 42
Primary keyMatch 42
RowOne user record
Related dataOrders, profile

A foreign key in another table can store the primary-key value to connect related records.

07 · Do Not Confuse

Primary Key

Uniquely identifies each row in its own table.

Foreign Key

Stores a key from another table to represent a relationship.

Unique Constraint

Prevents duplicate values in a column, but it is not automatically the table's primary identity.

09 · What to Remember

A primary key uniquely identifies each row in a table.

It helps software find, update, delete, and connect one exact record.

Other tables can refer to it with a foreign key.

↑