如果(不)满足条件

时间:2017-02-16 19:06:23

标签: sql oracle ddl check-constraints

我有一张表status

enter image description here

如果completionDate字段的值不是status.status

,我希望每条记录的completed字段为空
alter table status
        add constraint ck_completion
                check ( 
                        -- status.completiondate's value should be null
                    -- if (Lower(status.status) != 'complete')
                );

因为如果状态不完整或不完整,则不应该是完成日期。我该怎么办?

1 个答案:

答案 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
        )
    )