Monday, November 11, 2013

Drop unique CONSTRAINT and INDEX not getting DROPPED in Oracle Database

Issue :  When ever I imported the Oracle Database from (.DMP) file. I got unusual exception that is
If an unique CONSTRAINT on a table is been droped then it is not dropping the correspondence INDEX.

Say we have 'X' table and we created unique constrait say 'k'  on it , then what happens is an index is created on table 'X' with same name 'k'.

So now dropping Constraint 'k' should also drop the corresponding INDEX , but this is not happening

syntax being used is
"ALTER TABLE <TABLE NAME> DROP CONSTRAINT <CONSTRAINT NAME>"


Reasoning :

When ever we import the Oracle Database especially , It has it own sequence order for importing
that is say

1. It imports Schema , first then
2. It import Trigger
3. It imports Indexs
....

6. It imports Constraints
....

So above we can see that Indexes are imported before the constraints and the linkage between Constraint and the Index is lost.

If we normally create an Unique Constrait on the TABLE , an index is created on the table with same name and there would be linkage is such a way that if we drop the Constraint , it would automatically  drop the INDEX too.


Solution :

"ALTER TABLE <TABLE NAME> DROP CONSTRAINT <CONSTRAINT NAME> DROP INDEX"

add "DROP INDEX" to current syntax and it would also make sure we drop INDEX created while importing Oracle DMP file.
 

No comments:

Post a Comment