我需要添加一个检查约束,如果另一个值小于3,则需要一个值为0。 好到目前为止我得到了答案,但它没有安装到我的数据库中。这是我的测试表和数据:
CREATE TABLE dbo.YourTable(Col1 INT, Col2 INT);
insert into YourTable (Col1,Col2) SELECT 0, 5
insert into YourTable (Col1,Col2) SELECT 1, 5
insert into YourTable (Col1,Col2) SELECT 0, 3
insert into YourTable (Col1,Col2) SELECT 0, 11
insert into YourTable (Col1,Col2) SELECT 0, 4
insert into YourTable (Col1,Col2) SELECT 0, 145
insert into YourTable (Col1,Col2) SELECT 0, 10
insert into YourTable (Col1,Col2) SELECT 0, 1
insert into YourTable (Col1,Col2) SELECT 0, 6
insert into YourTable (Col1,Col2) SELECT 0, 6
insert into YourTable (Col1,Col2) SELECT 0, 2
insert into YourTable (Col1,Col2) SELECT 0, 5
insert into YourTable (Col1,Col2) SELECT 1, 3
insert into YourTable (Col1,Col2) SELECT 0, 10
insert into YourTable (Col1,Col2) SELECT 0, 10
ALTER TABLE dbo.YourTable ADD CONSTRAINT TestConstraint
CHECK ((Col1 = 0 AND Col2 <= 3) OR Col2 > 3)
答案 0 :(得分:4)
CREATE TABLE dbo.YourTable(Col1 INT, Col2 INT);
ALTER TABLE dbo.YourTable ADD CONSTRAINT TestConstraint
CHECK ((Col1 = 0 AND Col2 <= 3) OR Col2 > 3);