我必须创建一个表,仅在满足其他条件时才需要检查条件。
准确地说,我有一个带有“已解决”和“价格”列的表“修复”,如果“已解决”列的值为0,我需要检查“价格”列是否也等于0。
如何在create table语句中执行此操作?
答案 0 :(得分:0)
我认为您正在寻找
ALTER TABLE Repair
ADD CONSTRAINT MyConstraint CHECK((Solved = 0 AND Price = 0) OR (Solved <> 0 AND Price <> 0));
如何在create table语句中执行此操作?
CREATE TABLE Repair(
Solved BIT NOT NULL,
Price DECIMAL(19,4) NOT NULL,
CONSTRAINT MyConstraint CHECK((Solved = 0 AND Price = 0) OR (Solved <> 0 AND Price <> 0))
);