Vol. I · A Postgres HandbookTwelve chapters · · 756 queries

The Query & the Schema

A reading-room reference for writing PostgreSQL by hand — selects to window functions, with the schema in plain view.

FrontChaptersSchemaColophon
Chapters · Basic SELECT · Query 054 / 136
AdvancedBasic SELECT

Multi-column UNIQUE constraint

The query

SQL
ALTER TABLE project_assignments 
ADD CONSTRAINT unique_assignment UNIQUE (emp_id, project_id);
Tested against PostgreSQL 16

Note

“An employee cannot be assigned to the same project twice, but can have multiple projects, and a project can have multiple employees.

Tables referenced

  • project_assignments6 cols

← Previous

Add a CHECK constraint

Next →

Add constraint NOT VALID (for large tables)

The Query & the Schema

Set in Fraunces and Inter Tight. Printed on parchment, in oxblood and ink. No tracking, no accounts, no JavaScript on the answer pages beyond what your browser needs to copy a query.

Compiled for Postgres 16 · MMXXVI