我必须保留历史记录,所以我使用的是is_deleted列,它可以有’Y’或’N’.但对于任何is_deleted’N’的实例,我应该为(a,b,c)复合列提供unwue条目.
@H_301_4@CREATE UNIQUE INDEX fn_unique_idx ON table1 (CASE WHEN is_deleted='N' then (id,name,type) end);
第1行的错误:
ORA-00907:缺少右括号
请帮忙.
谢谢
解决方法
你需要类似的东西
@H_301_4@CREATE UNIQUE INDEX fn_unique_idx
ON table1 (CASE WHEN is_deleted='N' THEN id ELSE null END,CASE WHEN is_deleted='N' THEN name ELSE null END,CASE WHEN is_deleted='N' THEN type ELSE null END);
行动中的约束的一个例子
@H_301_4@sql> create table table1 ( 2 id number,3 name varchar2(10),4 type varchar2(10),5 is_deleted varchar2(1) 6 ); Table created. sql> CREATE UNIQUE INDEX fn_unique_idx 2 ON table1 (CASE WHEN is_deleted='N' THEN id ELSE null END,3 CASE WHEN is_deleted='N' THEN name ELSE null END,4 CASE WHEN is_deleted='N' THEN type ELSE null END); Index created. sql> insert into table1 values( 1,'Foo','Bar','N' ); 1 row created. sql> insert into table1 values( 1,'Y' ); 1 row created. sql> insert into table1 values( 1,'N' ); insert into table1 values( 1,'N' ) * ERROR at line 1: ORA-00001: unique constraint (SCOTT.FN_UNIQUE_IDX) violated sql> insert into table1 values( 1,'Zee','N' ); 1 row created.