17,086
社区成员
发帖
与我相关
我的任务
分享
SQL> create table t(a int, b int);
Table created
SQL> create unique index iux_t_ab on t(decode(b, 0, null,a));
Index created
SQL> -- b = 0 时,a可以有重复值
SQL> insert into t values(100, 1);
1 row inserted
SQL> insert into t values(100, 0);
1 row inserted
SQL> insert into t values(100, 0);
1 row inserted
SQL> insert into t values(200, 1);
1 row inserted
SQL> insert into t values(200, 1); -- 这行失败
insert into t values(200, 1)
ORA-00001: 违反唯一约束条件 (ORACLE.IUX_T_AB)
SQL> insert into t values(200, 0);
1 row inserted
SQL> select * from t;
A B
---- ----
100 1
100 0
100 0
200 1
200 0
SQL> drop table t purge;
Table dropped
SQL>