![](/img/trans.png)
[英]SQL Server - Is creating an index with a unique constraint as one of the columns necessary?
[英]UNIQUE and OR in one SQL Server constraint?
我有一個表,我想要一個檢查以下內容的約束(字母是列名,W 是“位”數據類型):
If and only if 'W' = 0, check that X, Y & Z are unique.
Else, allow all new data.
我可以做'W' is False
並且Check X, Y & Z are unique
,但合並邏輯以允許上述情況是我無法弄清楚的。
我試過了
W = 1 OR UNIQUE (X, Y, Z)
但是 SSMS 說它無效。
提前感謝幫助:)
你不能通過約束來做到這一點。 相反,使用過濾的唯一索引:
create unique index unq_x_w on t(x) where w = 0;
create unique index unq_y_w on t(y) where w = 0;
create unique index unq_z_w on t(z) where w = 0;
實際上,上面將您的問題解釋為每列都是唯一的。 如果您希望三元組是唯一的:
create unique index unq_xyz_w on t(x, y, z) where w = 0;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.