Oracle SQL约束where子句 [英] Oracle SQL Constraint where clause

查看:229
本文介绍了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.

这篇关于Oracle SQL约束where子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆