UNIQUE: no two alike, or no two among some
Step 1 of 100%
Concept · step 1 of 10
No two rows share it
A UNIQUE constraint says no two rows hold the same value. Like a key, it can span several columns.
- On one column.
email TEXT UNIQUE. - On several, written after the last column.
UNIQUE (team_id, user_id), where the pair must not repeat but each half may. - Enforced by a unique index Postgres builds for it, the lightning bolt on the board..
CREATE TABLE memberships (
id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
team_id BIGINT NOT NULL,
user_id BIGINT NOT NULL,
UNIQUE (team_id, user_id)
);User 1 is in teams 1 and 2, and team 1 has users 1 and 2: every half repeats, no pair does. The table also keeps a surrogate
PostgreSQL docs: Constraints, unique constraints ↗id as its primary key; a table can have one primary key and any number of UNIQUE constraints.Starting Postgres in your browser
About 5 MB the first time, cached after that.
PostgreSQL running in your browser