1

如果所有值彼此不同(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

4

1 回答 1

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)));

分享和享受。

于 2013-05-02T18:33:29.870 回答