user113531 user113531 - 3 months ago 8
SQL Question

SQL check constraint on column

I want to implement a constraint on a column to ensure that one particular value is only stored once in the column.

For example in a column values, I would like to have a constraint such that the value 0 can only be stored once.

How can I implement this in postgres?

Answer

Create partial unique index:

create table test(col int);
create unique index on test (col) where col = 0;
Comments