简体   繁体   中英

Postgres DB constraint for a value of a column

I have a UNIQUE constraint for columns username + user_status, such that we never allow two users that have the same name and status. However I wish this to apply only to the case where the status field is 'ACTIVE' but allow any number of records with the same username and status of 'INACTIVE'. Is this possible?

You can create a filtered unique index:

create unique index on the_table (username)
where user_status = 'ACTIVE';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM