简体   繁体   中英

PostgreSQL: Conditional unique constraint

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?

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.

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