Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dropping then adding a constraint fails in oracle

Tags:

sql

oracle

I'm trying to move a primary key constraint to a different column in oracle. I tried this:

ALTER TABLE MY_TABLE
DROP CONSTRAINT c_name;

ALTER TABLE MY_TABLE
ADD CONSTRAINT c_name PRIMARY KEY
(
  "COLUMN_NAME"
) ENABLE;

This fails on the add constraint with an error saying the the constraint already exists even though i just dropped it. Any ideas why this is happening

like image 254
mR_fr0g Avatar asked May 09 '09 12:05

mR_fr0g


People also ask

Does dropping a constraint drop the index?

When you drop a constraint, any indexes created as a result of the constraint creation will be dropped.

How do I drop a constraint in Oracle?

The syntax for dropping a unique constraint in Oracle is: ALTER TABLE table_name DROP CONSTRAINT constraint_name; table_name.

Can we drop constraint?

To drop an existing constraint, specify the DROP CONSTRAINT keywords and the identifier of the constraint. To drop multiple constraints on the same table, the constraint names must be in comma-separated list that is delimited by parentheses. The constraint that you drop can have an ENABLED, DISABLED, or FILTERING mode.

What happens when you try to enable the constraint after the update is completed?

If a constraint is enabled, data is checked as it is entered or updated in the database, and data that does not conform to the constraint is prevented from being entered. If a constraint is disabled, then data that does not conform can be allowed to enter the database.


1 Answers

If the original constraint was a primary key constraint, Oracle creates an index to enforce the constraint. This index has the same name as the constraint (C_NAME in your example). You need to drop the index separately from the constraint. So you will need to do a :

ALTER TABLE <table1> DROP CONSTRAINT C_NAME;
DROP INDEX C_NAME;

ALTER TABLE <table1> ADD CONSTRAINT C_NAME PRIMARY KEY
( COLUMN_2 ) ENABLE;
like image 195
Thomas Jones-Low Avatar answered Nov 13 '22 14:11

Thomas Jones-Low