简体   繁体   English

在MS SQL约束中创建唯一检查+其他检查

[英]Create Unique Check + Additional Check in MS SQL Constraint

I need to add a constraint to the following table: 我需要向下表添加约束:

Table(
CategoryId INT,
CustomerId INT,
IsActive BIT
....
)

I want to have only one combination of (CategoryId, CustomerId, IsActive = 1) just have no idea how to put that in one constraint. 我只想拥有(CategoryId,CustomerId,IsActive = 1)的一种组合,只是不知道如何将其置于一个约束中。 Only one combination of Category and Customer to be active (Active = 1) at the time, and be able to have multiple with active = 0 类别和客户中只有一个组合处于活动状态(活动= 1),并且可以具有多个活动状态= 0

I know it can be done with function, but I'm trying to find a way without it. 我知道可以通过函数来​​完成,但是我正在尝试找到一种没有函数的方法。 Something like: 就像是:

CONSTRAINT UN_CK_Table UNIQUE(CategoryId, CustomerId, IsActive = 1) CONSTRAINT UN_CK_Table UNIQUE(CategoryId,CustomerId,IsActive = 1)

您可以使用过滤后的唯一索引:

CREATE UNIQUE INDEX YourTableUi1 ON YourTable (CategoryId, CustomerId) WHERE (IsActive = 1);

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

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