繁体   English   中英

Postgres添加唯一约束

[英]Postgres add unique constraint

我需要向表x添加一个约束,该约束与其他表具有多对一的关系。 因此,表x具有字段other_table_id。

x还有另一列称为primary的布尔类型。

我想确保每个other_table_id none or only one primary=true

多个行可以具有other_table_id等于某个相同的值,并且primary=false但每个other_table_id只有一个true

如何创建此约束?

为此,您需要一个局部唯一索引:

create unique index idx_unique_other 
   on table_x (other_table_id)
   where primary;

这只会索引primary列值为true 对于这些, other_table_id必须是唯一的。

暂无
暂无

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

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