Skip to content
UniCircle
EasyConceptualConstraints

PRIMARY KEY vs UNIQUE constraints

Question PG-SQL-002

How do PRIMARY KEY and UNIQUE constraints differ in PostgreSQL? How do NULLs behave in a UNIQUE column?

Show the explanation

Both reject duplicates and both create a unique B-tree index automatically.

PRIMARY KEYUNIQUE
Per tableat most oneany number
NULLsnot allowed (implies NOT NULL)allowed
Purposethe row's identity; target of foreign keys by defaultan alternate key (email, roll number)
SQL
create table users (
    id    bigint generated always as identity primary key,
    email text not null unique,
    phone text unique              -- many rows may have a NULL phone
);

By default PostgreSQL treats NULLs as distinct in a UNIQUE constraint, so many rows can have phone = NULL. Since PostgreSQL 15 you can write unique nulls not distinct (phone) to allow at most one NULL. For case-insensitive uniqueness, use an expression index: create unique index on users (lower(email));.

Original UniCircle question. Last reviewed 22 Oct 2026.