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