根据 SQL 中的值添加唯一约束 [英] Add unique constraint depending on value in SQL

查看:28
本文介绍了根据 SQL 中的值添加唯一约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种情况,只有当其他字段中有特定值时,我才想添加唯一约束例如表是ID分类名称姓名价值类别 ID

i have a situation where i want to add unique constaint only when there is a certain value in other field for example the table is ID CategoryName Name Value CategoryID

约束将在 ID、CategoryName 和 Name 上,仅 CategoryID 为 0

the constraint will be on ID, CategoryName and Name only is CategoryID is 0

有可能吗?

推荐答案

CREATE UNIQUE NONCLUSTERED INDEX [UQ_ID_CategoryName_Name, CategoryName, Name] ON TableName 
(
    ID, CategoryName, Name
)
WHERE (CategoryID = 0)

这篇关于根据 SQL 中的值添加唯一约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆