4

以下条件约束根本不起作用。是否有可能以一种可行的方式制定?

ALTER TABLE eni_trasc_voci_pwr_fatt
ADD CONSTRAINT tvp_constraint_1 CHECK (
CASE WHEN TVP_CODICE_ASSOGGETAMEN = '-' THEN tvp_regione IS NULL
END);
4

3 回答 3

6

Try the following:

ALTER TABLE eni_trasc_voci_pwr_fatt
ADD CONSTRAINT tvp_constraint_1 CHECK (
CASE WHEN TVP_CODICE_ASSOGGETAMEN = '-' THEN tvp_regione else null end IS NULL);
于 2012-05-23T08:26:08.403 回答
3

看起来你想要这里的逻辑含义(“如果 X 则 Y”),这在逻辑上等同于“(不是 X)或 Y”。CASE 用于创建有限映射。

您的约束应该类似于

TVP_CODICE_ASSOGGETAMEN != '-' OR TVP_REGIONE IS NULL

于 2012-05-23T08:28:21.960 回答
2

我认为你可以在没有 case 语句的情况下做你想做的事:

create table t1 (c1 varchar2(10), c2 varchar2(10));

alter table t1 add constraint t1_chk1 check ( (c1 = '-' and c2 is null) or (c1 != '-' and c2 is not null) );

现在尝试插入一些值:

SQL> insert into t1 values ('-', 'reject');
insert into t1 values ('-', 'reject')  
*
ERROR at line 1:
ORA-02290: check constraint (SODONNEL.T1_CHK1) violated


SQL>
SQL> insert into t1 values ('-', null);

1 row created.

SQL>
SQL> insert into t1 values ('a', null);
insert into t1 values ('a', null)
*
ERROR at line 1:
ORA-02290: check constraint (SODONNEL.T1_CHK1) violated


SQL>
SQL> insert into t1 values ('a', 'accept');

1 row created.
于 2012-05-23T08:36:21.963 回答