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

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

WBOY
WBOYOriginal
2016-06-07 17:27:481059browse

当先创建唯一约束后再创建主键约束的情况下,如果使用普通方法删除主键约束后,唯一约束索引不会被删除,这是Oracle 10g的一个PR

当先创建唯一约束后再创建主键约束的情况下,如果使用普通方法删除主键约束后,唯一约束索引不会被删除,这是Oracle 10g的一个PROBLEM。

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

【问题现象】
在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

linux

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn