ORACLE触发器检查行的每个值是否彼此不同

时间:2013-05-02 18:13:52

标签: oracle plsql triggers

是否可以在插入和更新时进行比较,如果所有值彼此不同(NULL除外)?我有10列数字,并不想为每种可能性写一个if语句。

示例:

column_1 | column_2 | column_3
--------------------------------
   5           2          4        <- allowed to insert
   1           2          1        <- forbidden to insert/update, because there are two '1' in a row

1 个答案:

答案 0 :(得分:4)

我认为你需要进行明确的比较。您可以使用CHECK约束进行比较,如

CREATE TABLE SOME_TABLE
  (COL1   NUMBER,
   COL2   NUMBER,
   COL3   NUMBER,
   COL4   NUMBER,
   COL5   NUMBER,
   COL6   NUMBER,
   COL7   NUMBER,
   COL8   NUMBER,
   COL9   NUMBER,
   COL10  NUMBER,
   CONSTRAINT SOME_TABLE_CK1
     CHECK(COL1  NOT IN (COL2, COL3, COL4, COL5, COL6, COL7, COL8, COL9, COL10) AND
           COL2  NOT IN (COL3, COL4, COL5, COL6, COL7, COL8, COL9, COL10) AND
           COL3  NOT IN (COL4, COL5, COL6, COL7, COL8, COL9, COL10) AND
           COL4  NOT IN (COL5, COL6, COL7, COL8, COL9, COL10) AND
           COL5  NOT IN (COL6, COL7, COL8, COL9, COL10) AND
           COL6  NOT IN (COL7, COL8, COL9, COL10) AND
           COL7  NOT IN (COL8, COL9, COL10) AND
           COL8  NOT IN (COL9, COL10) AND
           COL9  NOT IN (COL10)));

分享并享受。