Oracle 10g删除主键约束后无法删除唯一约束索引问题的模拟与分析

2014-11-24 18:05:30 · 作者: · 浏览: 0

本文通过一个实验给大家演示一下这个问题的出现过程及处理方法。


【问题现象】
在10g环境下,在删除主键约束后,在插入重复数据时候仍然报“ORA-00001: unique constraint (SEC.PK_T) violated”错误。
现象是主键约束已经删除成功,但是唯一约束索引没有级联删除。


【问题模拟】
1.创建表T
sec@ora10g> create table t (x int, y int);


Table created.


2.先创建惟一约束索引
sec@ora10g> create unique index pk_t on t (x);


Index created.


3.再创建主键约束
sec@ora10g> alter table t add (constraint pk_t primary key(x));


Table altered.


4.查看约束信息
sec@ora10g> col OWNER for a5
sec@ora10g> col CONSTRAINT_NAME for a30
sec@ora10g> col TABLE_NAME for a10
sec@ora10g> col INDEX_OWNER for a12
sec@ora10g> col INDEX_NAME for a10
sec@ora10g> select OWNER,CONSTRAINT_NAME,CONSTRAINT_TYPE,TABLE_NAME,INDEX_OWNER,INDEX_NAME from user_constraints where table_name = 'T';


OWNER CONSTRAINT_NAME C TABLE_NAME INDEX_OWNER INDEX_NAME
----- ------------------------------ - ---------- ------------ ----------
SEC PK_T P T SEC PK_T


5.查看索引信息
sec@ora10g> select INDEX_NAME,INDEX_TYPE,GENERATED from user_indexes;


INDEX_NAME INDEX_TYPE G
---------- --------------------------- -
PK_T NORMAL N


GENERATED字段说明:
GENERATED VARCHAR2(1) Indicates whether the name of the index is system generated (Y) or not (N)


6.删除主键约束
sec@ora10g> alter table t drop constraint pk_t cascade;


Table altered.


7.确认约束和索引删除情况
sec@ora10g> select OWNER,CONSTRAINT_NAME,CONSTRAINT_TYPE,TABLE_NAME,INDEX_OWNER,INDEX_NAME from user_constraints where table_name = 'T';


no rows selected


sec@ora10g> select INDEX_NAME,INDEX_TYPE,GENERATED from user_indexes;


INDEX_NAME INDEX_TYPE G
---------- --------------------------- -
PK_T NORMAL N


可见,此时索引没有被删除。
因此,此时如果插入重复的数据,还是会报违反“约束”
sec@ora10g> insert into t values (1,1);


1 row created.


sec@ora10g> insert into t values (1,1);
insert into t values (1,1)
*
ERROR at line 1:
ORA-00001: unique constraint (SEC.PK_T) violated