UNIQUE constraints: prevent duplicate data and handle NULLs
Learn how database-enforced uniqueness differs from a primary key, how NULL and conditional rules behave, and how to add a rule safely when data already exists.
UNIQUE constraints: prevent duplicate data and handle NULLs
A UNIQUE constraint makes a database reject values in one or more columns when they duplicate values in another row. An employee number, for example, can be required to be unique. Checking in application code first is not enough: two concurrent requests could both see that a value is unused. The database constraint rejects the conflicting write.
UNIQUE versus a primary key
Both can prevent duplicates, but they serve different roles. A primary key is the table's principal row identifier and must be unique and non-null; a table can have at most one. A UNIQUE constraint represents an alternate identifier or another business rule and a table can have several. Add NOT NULL separately when a value is required. PostgreSQL automatically creates a unique B-tree index for a UNIQUE constraint, so do not add an identical index just to enforce the same rule.
Uniqueness across one or several columns
For example, an employee number can be unique across the company:
CREATE TABLE employees (
employee_id bigint PRIMARY KEY,
employee_number text NOT NULL UNIQUE,
name text NOT NULL
);A table-level constraint can make a combination unique. Suppose each branch may reuse seat numbers, but only one active reservation is allowed for each seat at a branch. PostgreSQL can express that conditional rule with a partial unique index:
CREATE TABLE reservations (
reservation_id bigint PRIMARY KEY,
branch_id bigint NOT NULL,
seat_number text NOT NULL,
status text NOT NULL
);
CREATE UNIQUE INDEX one_active_reservation_per_seat
ON reservations (branch_id, seat_number)
WHERE status = 'active';The index enforces uniqueness only for active reservations, so cancelled records can remain while a new active reservation uses the seat. This differs from an ordinary UNIQUE constraint over every row. The example is PostgreSQL-specific; check whether another DBMS supports the same feature and syntax.
Does NULL count as a duplicate?
NULL handling in UNIQUE varies by database product. PostgreSQL 18 treats NULL values as distinct by default, so a nullable column with UNIQUE (email) may contain several NULLs. NULLS NOT DISTINCT changes the rule so that multiple NULLs conflict. MySQL 8.4 also permits multiple NULL values in a nullable column with a unique index.
Therefore, “email must be unique” does not mean an email must be present. Add NOT NULL if it is required. Check how your DBMS handles NULLs in a multicolumn unique rule, and verify behavior for the exact product and version when portability matters.
Before adding the rule to existing data
Adding uniqueness may fail if existing rows already conflict. First define the comparison rule: case, whitespace, accents, normalized characters, and scope such as tenant or branch can matter. Find duplicate candidates, resolve them with the data owner, then add the rule and verify that conflicting concurrent writes are rejected. Do not blindly delete or merge rows; other records may refer to them.
This PostgreSQL query illustrates how to find repeated branch/seat/status combinations:
SELECT branch_id, seat_number, status, count(*)
FROM reservations
GROUP BY branch_id, seat_number, status
HAVING count(*) > 1;If a write is rejected, identify the conflicting value and decide whether the input is wrong or the uniqueness scope was defined incorrectly. An application-side pre-check can provide an early message, but the database constraint must be the final guard against races.
Choosing the rule
A required single-column alternate identifier: consider `NOT NULL UNIQUE`.
A combination that must be unique: consider a table-level `UNIQUE (a, b)`.
A rule that applies to only some rows in PostgreSQL: consider a partial unique index and confirm target DBMS support.
Optional values: check product-specific NULL semantics; use `NOT NULL` or a product-specific NULL option when needed.
Sources and scope
[PostgreSQL 18: Constraints — UNIQUE and NULL behavior](https://www.postgresql.org/docs/18/ddl-constraints.html)
[MySQL 8.4: CREATE TABLE — UNIQUE and NULL behavior](https://dev.mysql.com/doc/refman/8.4/en/create-table.html)
Documentation checked 2026-11-23. SQL is illustrative and was not executed. NULL and partial-index behavior can differ by product and version.