user113531 user113531 - 1 year ago 104
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 Source

Create partial unique index:

create table test(col int);
create unique index on test (col) where col = 0;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download