[英]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.