繁体   English   中英

Oracle:我可以创建一个约束来检查组内的值吗

[英]Oracle: can I create a constraint that checks a value within a group

在此先感谢您的帮助。 在 Oracle 中,是否可以创建一个约束,以便每个组只能有一个“领导者”:

如果一个组中已经存在一个“领导者”,我如何创建一个规则来阻止插入额外的“领导者”?

在此处输入图像描述

这有意义吗? 基于函数的唯一索引:

SQL> create table test (col1 varchar2(10), col2 varchar2(10), col3 varchar2(10));

Table created.

SQL> create unique index ui1t on test (case when col2 = 'leader' then col1||col2 end);

Index created.

测试:

SQL> insert into test values ('group1', 'leader', 'joe');

1 row created.

SQL> insert into test values ('group1', null    , 'diane');

1 row created.

SQL> insert into test values ('group1', null    , 'john');

1 row created.

SQL> insert into test values ('group1', null    , 'diane');

1 row created.

SQL> insert into test values ('group1', 'leader', 'mike');   --> another LEADER for GROUP1
insert into test values ('group1', 'leader', 'mike')
*
ERROR at line 1:
ORA-00001: unique constraint (SCOTT.UI1T) violated


SQL> insert into test values ('group1', 'leader', null);   --> another LEADER for GROUP1
insert into test values ('group1', 'leader', null)
*
ERROR at line 1:
ORA-00001: unique constraint (SCOTT.UI1T) violated


SQL>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM