简体   繁体   English

内部唯一检查约束

[英]UNIQUE inside CHECK constraint

This question is new twist of An IF inside a check constraint SQL . 这个问题是检查约束SQL的IF的新形式。 I want to do something similar to the following check (which throws an ORA-00936: missing expression exception): 我想做类似以下检查的事情(抛出ORA-00936: missing expression异常):

ALTER TABLE t_table
    ADD CONSTRAINT chk_unique_active CHECK
    ( 
        ( tb_active = 0 ) OR  
        ( tb_active = -1 AND UNIQUE(tb_active, tb_img, tb_objid)) 
    );

The objetive is to be sure (at DBMS level) that only one row with the same objid is active although inactive rows can be duplicated (an historical view of the rows). 确保(在DBMS级别上)目标对象是唯一具有相同objid的行是活动的,尽管可以复制非活动的行(这些行的历史视图)。

It can be done in a trigger but it seems to be better using the check as explained in UNIQUE constraint vs checking before INSERT question. 可以在触发器中完成,但是使用UNIQUE约束中说明的检查似乎比插入问题之前检查要好。

is this possible? 这可能吗?

Do this with a unique index: 使用unique索引执行此操作:

create unique index unq_table_active 
    on ( (case when tb_active = -1 then tb_img end),
         (case when tb_active = -1 then tb_objid end)
       ) 

Oracle allows multiple rows with NULL values in a unique index. Oracle允许在唯一索引中使用NULL值的多行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM