PostgreSQL: Conditional unique constraint

postgresql

Solution

Yes partial index is your choice.

create unique index unique__vin on table (vin) where is_owner;

Here index covers only rows where is_owner is true and withing this rows vin should be unique.

Problem

Given: ``` ---------------------------------- vin | driver | is_owner --------------+--------+---------- 231431cxzv87 | bob | true 231431cxzv87 | jeff | false 231431cxzv87 | greg | false 32342klj234s | jeff | true ``` Is there a way to add a constraint so that there is only one owner per vin? Edit: I found this question. Is adding a partial unique index meant to suit this purpose?

Original source