我想创建一个具有如下操作细节的表:
CRAETED_ON DATETIME NOT NULL DEFAULT GETDATE(),
CREATED_BY VARCHAR(10) NOT NULL,
DELETED_ON DATETIME NULL,
DELETED_BY VARCHAR(10) NULL
我想放置约束IF DELETED_ON
,然后他们应该提供DELETED_BY
。
BOTH应该为NULL或两者都不应该为NULL。不允许一个NULL和另一个NOT NULL。
CREATE TABLE dbo.Example(
CREATED_ON DATETIME NOT NULL DEFAULT GETDATE(),
CREATED_BY VARCHAR(10) NOT NULL,
DELETED_ON DATETIME NULL,
DELETED_BY VARCHAR(10) NULL
,CONSTRAINT IF_DELETED_ON CHECK ((DELETED_ON IS NULL AND DELETED_BY IS NULL) OR (DELETED_ON IS NOT NULL AND DELETED_BY IS NOT NULL))
);