我有一张表status
:
如果completionDate
字段的值不是status.status
completed
字段为空
alter table status
add constraint ck_completion
check (
-- status.completiondate's value should be null
-- if (Lower(status.status) != 'complete')
);
因为如果状态不完整或不完整,则不应该是完成日期。我该怎么办?
答案 0 :(得分:2)
您可以创建一个表级约束,如果状态未完成则只允许空值,状态完成时只允许任何值。
alter table status add constraint ck_completion check (
lower(status) = 'complete'
or completiondate is null
)
如果状态完成,如果您不想在完成日期接受空值,请尝试以下操作:
alter table status add constraint ck_completion check (
(
lower(status) <> 'complete'
and completiondate is null
)
or (
lower(status) = 'complete'
and completiondate is not null
)
)