oracle删除unique key,【PK】Oracle 10g删除主键约束后无法删除唯一约束索引问题的模拟与分析...

当先创建唯一约束后再创建主键约束的情况下,如果使用普通方法删除主键约束后,唯一约束索引不会被删除,这是Oracle 10g的一个PROBLEM。本文通过一个实验给大家演示一下这个问题的出现过程及处理方法。【问题现象】在10g环境下,在删除主键约束后,在插入重复数据时候仍然报“ORA-00001: unique constraint (SEC.PK_T) violated”错误。现象是主键约束已经删除成功,但是唯一约束索引没有级联删除。【问题模拟】1.创建表Tsec@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 a5sec@ora10g> col CONSTRAINT_NAME for a30sec@ora10g> col TABLE_NAME for a10sec@ora10g> col INDEX_OWNER for a12sec@ora10g> col INDEX_NAME for a10sec@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_T5.查看索引信息sec@ora10g> select INDEX_NAME,INDEX_TYPE,GENERATED from user_indexes;INDEX_NAME INDEX_TYPE                  G---------- --------------------------- -PK_T       NORMAL                      NGENERATED字段说明: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 selectedsec@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【问题原因】这个问题的原因可以参考MOS的[ID 309821.1]文章,文章中给出了具体原因。Oracle 10G Does not Drop User Index Associated With Unique/Primary Key Constraints [ID 309821.1]原来是Oracle在的10g版本中对内部函数"atbdui"进行了调整,导致在删除约束的时候无法删除用户创建的索引。这个现象被Oracle分类到了“PROBLEM”。【处理方法】在删除约束的时候需要显示的指定“drop index”选项来完成索引的级链删除。sec@ora10g> alter table t drop constraint pk_t cascade drop index;Table altered.经确认,约束和索引均被删除后,尝试插入两条重复数据,成功。sec@ora10g> select OWNER,CONSTRAINT_NAME,CONSTRAINT_TYPE,TABLE_NAME,INDEX_OWNER,INDEX_NAME from user_constraints where table_name = 'T';no rows selectedsec@ora10g> select INDEX_NAME,INDEX_TYPE,GENERATED from user_indexes;no rows selectedsec@ora10g> insert into t values (1,1);1 row created.sec@ora10g> insert into t values (1,1);1 row created.【BTW】如果之前创建的是普通索引,如果使用“

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值