Unique and Primary Key Indexes
Understand how PostgreSQL automatically backs primary keys and unique constraints with indexes, and how to use unique indexes to enforce data integrity efficiently.
Unique and Primary Key Indexes is a free Advanced PostgreSQL: Indexing, Partitioning, Replication lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Advanced PostgreSQL: Indexing, Partitioning, Replication learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Constraints Need Indexes
To enforce uniqueness, PostgreSQL must check existing rows fast — so it auto-builds a unique index behind every unique constraint and primary key.
Primary Key Index
Declaring a primary key creates a unique B-tree index on those columns and marks them NOT NULL. The code shows it in action.
CREATE TABLE users (
id SERIAL PRIMARY KEY,
email TEXT
);The Hidden Index
That primary key silently builds an index called users_pkey. You never wrote CREATE INDEX, yet lookups by id are already fast.
Unique Constraints
A UNIQUE constraint also creates a supporting index and rejects duplicate values — the ALTER TABLE below adds one.
ALTER TABLE users ADD CONSTRAINT users_email_key UNIQUE (email);Standalone Unique Index
You can build a unique index directly, without a named constraint — it still enforces uniqueness, as the code shows.
CREATE UNIQUE INDEX idx_users_email ON users (email);Multi-Column Uniqueness
Index multiple columns together for multi-column uniqueness — like one membership per user per group. The code makes the pair unique.
CREATE UNIQUE INDEX idx_membership ON memberships (user_id, group_id);NULLs and Uniqueness
PostgreSQL treats NULLs as distinct by default, so a unique column allows many NULLs. Add NULLS NOT DISTINCT to forbid more than one.
CREATE UNIQUE INDEX idx_code ON items (code) NULLS NOT DISTINCT;Partial Unique Index
A partial unique index enforces uniqueness only on rows matching a WHERE clause — for example, one active session per user.
CREATE UNIQUE INDEX idx_active ON sessions (user_id) WHERE active;Dual Benefit
A unique index gives you two things at once: data integrity and fast lookups. The planner uses it like any other B-tree index.
Constraint vs Index
The constraint is the logical rule; the index enforces it. You can't drop the index without the constraint, and foreign keys can reference these columns.
Cost Awareness
Unique indexes add write overhead since every insert checks for duplicates. Index only the columns that genuinely need to be unique.
Quick Check
Primary keys, NULLs, partial indexes — let's see what stuck about unique indexes.
Recap
Recap: every primary key and unique constraint is backed by a unique B-tree index for integrity plus speed. You can also go standalone, multi-column, or partial — but each adds write cost.
Frequently asked questions
Is the “Unique and Primary Key Indexes” lesson free?
Yes — the full text of “Unique and Primary Key Indexes” is free to read here on the web, and the Advanced PostgreSQL: Indexing, Partitioning, Replication course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Advanced PostgreSQL: Indexing, Partitioning, Replication course, upgrade to CoddyKit PRO.
What will I learn in “Unique and Primary Key Indexes”?
Understand how PostgreSQL automatically backs primary keys and unique constraints with indexes, and how to use unique indexes to enforce data integrity efficiently. You practise Advanced PostgreSQL: Indexing, Partitioning, Replication with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Advanced PostgreSQL: Indexing, Partitioning, Replication?
No prior experience is required. Advanced PostgreSQL: Indexing, Partitioning, Replication on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Unique and Primary Key Indexes” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Advanced PostgreSQL: Indexing, Partitioning, Replication lesson?
Yes. Every Advanced PostgreSQL: Indexing, Partitioning, Replication lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Why Indexes Matter
- B-Tree Index Basics
- Creating and Dropping Indexes
- Unique and Primary Key Indexes