如何创建高级检查条件

How to create advanced check condition

我有一个 Oracle table,我想创建这样的检查条件:

ALTER TABLE MyTable
    ADD CONSTRAINT MyTable_CHK2 CHECK (
    case Dimension 
        When 1 then 
            nvl(dimensiontype1,-1)<>-1
        when 2 then 
            nvl(dimensiontype1,-1)<>-1 and nvl(dimensiontype2,-1)<>-1
        when 3 then 
            nvl(dimensiontype1,-1)<>-1 and nvl(dimensiontype2,-1)<>-1 and nvl(dimensiontype3,-1)<>-1
        else
            true
        end
    )
disable

查询无效。我遇到错误:缺少关键字。

有人知道怎么解决吗?

谢谢。

您可能需要一个 AND / OR 表达式

 ALTER TABLE MyTable
    ADD CONSTRAINT MyTable_CHK2 CHECK 
 (
    (  Dimension  =  1 and  nvl(dimensiontype1,-1)  <> - 1 ) OR
    (  Dimension  =  2 and  (nvl(dimensiontype1,-1) <> - 1 and nvl(dimensiontype2,-1)<> -1 ) ) OR
    (  Dimension  =  3 and  (nvl(dimensiontype1,-1) <>  -1 and nvl(dimensiontype2,-1)<>  -1 and nvl(dimensiontype3,-1) <> -1))
) disable  ;  

检查约束应该是:

(dimension=1 and dimensiontype1 is not null)
or (dimension=2 and dimensiontype1 is not null and dimensiontype2 is not null)
or (dimension=3 and dimensiontyp1 is not null and dimensiontype2 is not null and dimensionType 3 is not null)