0PricingLogin
SQL Academy · Lesson

Primary Keys and Uniqueness

Understand primary keys, surrogate vs natural keys, composite keys, and why every row needs a unique identifier you can trust.

Why Every Row Needs a Unique ID

To update or delete a specific row safely, you must be able to point at it unambiguously.

  • Two users named "Alice Adams" can exist
  • But two users with id = 42 must NOT exist
  • The unique identifier is called the primary key

Declaring a Primary Key

You mark a column as the primary key when you create the table:

CREATE TABLE products (
  id    BIGSERIAL PRIMARY KEY,
  sku   VARCHAR(50) NOT NULL,
  name  VARCHAR(200) NOT NULL,
  price NUMERIC(10,2) NOT NULL
);

All lessons in this course

  1. What an RDBMS Is and Why Tables Matter
  2. Primary Keys and Uniqueness
  3. NULL: The Third Truth Value
  4. Data Types Overview: INT VARCHAR DATE BOOLEAN
← Back to SQL Academy