开发者

Creating a constraint in Oracle that depends on other columns

开发者 https://www.devze.com 2023-03-25 00:50 出处:网络
Please consider the following table structure on Oracle: create table DOCS ( DOC_NONUMBER not null, DOC_TYPE VARCHAR2(5) not null,

Please consider the following table structure on Oracle:

create table DOCS
(
  DOC_NO   NUMBER not null,
  DOC_TYPE VARCHAR2(5) not null,
  PMT_NO   NUMBER not null
);

In this table, the 开发者_如何学JAVAPMT_NO column has to be unique except when DOC_NO is the same and DOC_TYPE is different:

    DOC_NO DOC_TYPE     PMT_NO
---------- -------- ----------
         1 A                10 <-- good
         1 B                10 <-- good, DOC_NO is the same
         2 C                10 <-- NOT good, DOC_NO is different

PMT_NO cannot repeat and cannot have "holes" (i.e. 1, 2, 3, 5), so a sequence would not work. And there are many users inserting data at the same time.

Is there a way to create a unique key / unique index / function-based index for that condition?

Thanks!


Maybe this is a normalization problem.

You could pull out the relevant tuple into another table such that the row would be unique.

In this case link doc_no to pmt_no, once (not repeated as you have shown).

Then you can make a unique index on the pmt_no column of this link table.

0

精彩评论

暂无评论...
验证码 换一张
取 消