patternsqlMajor
Custom unique constraint, only enforced if one column has a specific value
Viewed 0 times
uniquecolumnspecificvaluecustomenforcedonehasconstraintonly
Problem
Is it possible to have a custom unique constraint as follows? Suppose I have two cols,
If
subset and type, both strings (though the data types probably doesn't matter).If
type is 'true', then I want the combination of type and subset to be unique. Otherwise, there is no constraint. I'm using PostgreSQL 8.4 on Debian.Solution
In other words, you want values in the column
A partial unique index will do that:
Data type does matter. If the column
This way you can even make combinations with
subset to be unique among rows where the column type is 'true'.A partial unique index will do that:
CREATE UNIQUE INDEX tbl_some_name_idx ON tbl (subset) WHERE type = 'true';Data type does matter. If the column
type is boolean (likely should be), you can simplify:CREATE UNIQUE INDEX tbl_some_name_idx ON tbl (subset) WHERE type;This way you can even make combinations with
NULL unique, which wasn't possible before Postgres 15. See:- PostgreSQL multi-column unique constraint and NULL values
Code Snippets
CREATE UNIQUE INDEX tbl_some_name_idx ON tbl (subset) WHERE type = 'true';CREATE UNIQUE INDEX tbl_some_name_idx ON tbl (subset) WHERE type;Context
StackExchange Database Administrators Q#37427, answer score: 46
Revisions (0)
No revisions yet.