非唯一索引的唯一约束违规

时间:2014-04-02 14:20:46

标签: oracle oracle10g

有时,我会针对非唯一的索引获得唯一的密钥违规错误。

我有一个Oracle 10g数据库。

在不同的表上有两个索引发生。他们的详细信息是:

CREATED                    14-JAN-14 20.14.11
LAST_DDL_TIME              14-JAN-14 20.14.11
OWNER                      MEDDBA
INDEX_NAME                 STATUS_REQUEST
INDEX_TYPE                 NORMAL
TABLE_OWNER                MEDDBA
TABLE_NAME                 STATUS
TABLE_TYPE                 TABLE
UNIQUENESS                 NONUNIQUE
COMPRESSION                DISABLED
PREFIX_LENGTH    
TABLESPACE_NAME            MEDTBS
INI_TRANS                  2
MAX_TRANS                  255
INITIAL_EXTENT             65536
NEXT_EXTENT                1048576
MIN_EXTENTS                1
MAX_EXTENTS                2147483645
PCT_INCREASE    
PCT_THRESHOLD    
INCLUDE_COLUMN    
FREELISTS    
FREELIST_GROUPS    
PCT_FREE                   10
LOGGING                    YES
BLEVEL                     1
LEAF_BLOCKS                89
DISTINCT_KEYS              67
AVG_LEAF_BLOCKS_PER_KEY    1
AVG_DATA_BLOCKS_PER_KEY    2
CLUSTERING_FACTOR          190
STATUS                     VALID
NUM_ROWS                   8161
SAMPLE_SIZE                8161
LAST_ANALYZED              01-APR-14 22.00.09
DEGREE                     1
INSTANCES                  1
PARTITIONED                NO
TEMPORARY                  N
GENERATED                  N
SECONDARY                  N
BUFFER_POOL                DEFAULT
USER_STATS                 NO
DURATION                   
PCT_DIRECT_ACCESS          
ITYP_OWNER                 
ITYP_NAME                  
PARAMETERS                 
GLOBAL_STATS              YES
DOMIDX_STATUS    
DOMIDX_OPSTATUS    
FUNCIDX_STATUS    
JOIN_INDEX                NO
IOT_REDUNDANT_PKEY_ELIM   NO
DROPPED                   NO

CREATED                     14-JAN-14 20.14.04
LAST_DDL_TIME               14-JAN-14 20.14.04
OWNER                       MEDDBA
INDEX_NAME                  SEQUENCE_ID2
INDEX_TYPE                  NORMAL
TABLE_OWNER                 MEDDBA
TABLE_NAME                  SEQUENCE
TABLE_TYPE                  TABLE
UNIQUENESS                  NONUNIQUE
COMPRESSION                 DISABLED
PREFIX_LENGTH   
TABLESPACE_NAME             MEDTBS
INI_TRANS                   2
MAX_TRANS                   255
INITIAL_EXTENT              65536
NEXT_EXTENT                 1048576
MIN_EXTENTS                 1
MAX_EXTENTS                 2147483645
PCT_INCREASE    
PCT_THRESHOLD   
INCLUDE_COLUMN  
FREELISTS   
FREELIST_GROUPS 
PCT_FREE                    10
LOGGING                     YES
BLEVEL                      1
LEAF_BLOCKS                 43
DISTINCT_KEYS               1
AVG_LEAF_BLOCKS_PER_KEY     43
AVG_DATA_BLOCKS_PER_KEY     120
CLUSTERING_FACTOR           120
STATUS                      VALID
NUM_ROWS                    7795
SAMPLE_SIZE                 7795
LAST_ANALYZED               01-APR-14 22.00.10
DEGREE                      1
INSTANCES                   1
PARTITIONED                 NO
TEMPORARY                   N
GENERATED                   N
SECONDARY                   N
BUFFER_POOL                 DEFAULT
USER_STATS                  NO
DURATION    
PCT_DIRECT_ACCESS   
ITYP_OWNER  
ITYP_NAME   
PARAMETERS  
GLOBAL_STATS               YES
DOMIDX_STATUS   
DOMIDX_OPSTATUS 
FUNCIDX_STATUS  
JOIN_INDEX                     NO
IOT_REDUNDANT_PKEY_ELIM    NO
DROPPED                    NO

错误:

java.lang.RuntimeException: org.springframework.dao.DataIntegrityViolationException: PreparedStatementCallback; SQL []; ORA-00001: unique constraint (MEDDBA.STATUS_REQUEST) violated; nested exception is java.sql.SQLException: ORA-00001: unique constraint (MEDDBA.STATUS_REQUEST) violated

2 个答案:

答案 0 :(得分:0)

如果基于非uniuqe索引对表有唯一约束,那就不足为奇了 - Oracle允许它支持可延迟约束:

SQL> create table t (x int, y int);

SQL> create index t_x on t(x);

SQL> alter table t add constraint t_x unique (x)
  2  /

SQL> select uniqueness from user_indexes where index_name = 'T_X';

UNIQUENES                                                                       
---------                                                                       
NONUNIQUE                                                                       

SQL> insert into t values(1,2);

SQL> insert into t values(1,2);
insert into t values(1,2)
*
error in line:
ORA-00001: unique constraint (SCOTT.T_X) violated

所以你应该检查你的表可能有对应的约束。

答案 1 :(得分:0)

错误消息告诉您有唯一的 CONSTRAINT 。您可以使用具有相同名称的约束和索引,例如

create table t1(pk number);
create index t1_idx on t1(pk);
alter table t1 add constraint t1_idx unique(pk);

insert into t1 values(1);
insert into t1 values(1);

提出了你得到的完全相同的错误。

因此,如果此列不应该是唯一的,则可以使用

删除约束
alter table <table_name> drop constraint <constraint_name>;

而且,BTW:如果你真的有一个名为SEQUENCE的表,我强烈建议重命名它。 SEQUENCE是一个保留字,并且拥有一个带有此名称的表会在将来给您带来很多痛苦。