Oracle SQL约束where子句

Oracle SQL约束where子句

问题描述:

我在oracle上有一个表测试器,包含以下列:

I have the table Tester on oracle with the following columns:


  • TesterID

  • TesterName

  • IsDefault

  • Application_ID

  • TesterID
  • TesterName
  • IsDefault
  • Application_ID

TesterID首要的关键。
现在我想要只有一个默认测试器,这意味着只有一个测试器可以在ApplicationID的值为IsDefault = Y。

TesterID is the primary key. Now I want that there can only be one Default Tester, which means only one Tester can have the calues IsDefault =Y at an ApplicationID.

我试过了具有约束:

alter table Tester add constraint Tester_ISDEFAULT UNIQUE(IsDefault,Application_ID);

是否可以在isdefault = Y?上创建唯一键?

Is it possible to make the unique key on where isdefault= Y?

感谢您的帮助!

不是 UNIQUE 约束。但是,您可以改用 UNIQUE INDEX

Not with a UNIQUE constraint. However, you can use a UNIQUE INDEX instead:

CREATE UNIQUE INDEX ApplicationId_Default_Y ON tester (
  CASE WHEN IsDefault = 'Y'
       THEN ApplicationId
       ELSE NULL
  END
);

这里是 DEMO

Here's a DEMO.