简体   繁体   English

PostgreSQL:条件唯一约束

[英]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? 有没有办法添加约束,以便每个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. 这里索引仅涵盖is_owner为true的行,并且这些行vin应该是唯一的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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