Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Drop Unknown Constraints

ClancyDamonMar 20 2012 — edited Mar 21 2012
I'm working with a nearly ten year old Oracle database that we've finally talked a client into updating and modifying. As part of the extensive modifications, we're dropping several dozens of columns from numerous tables that are now superfluous. Our problem is that for some of these tables there are constraints that are applied to the columns resulting in "ORA-12991: column is referenced in a multi-column constraint" error. After some research, I found a good way through SQL to find the names of these unknown constraints (again, 10 years old I didn't build it) through the column names which we do know:

SELECT
c.table_name,
c.column_name,
c.constraint_name
FROM
user_tab_columns t,
user_cons_columns c
WHERE
t.table_name = 'tb1'
AND t.table_name = c.table_name
AND t.column_name = 'column1'
AND c.column_name = t.column_name
AND t.nullable = 'N';

This provides a list of all of the constraints applied to the column. I've verified this using SQL Developer to look directly.

My problem is that I need to use this list to drop all the constraints returned by the above query so I can then drop the column entirely. So far the best I've come up with is:

ALTER TABLE
tb1
DROP CONSTRAINT IN
(SELECT
c.constraint_name
FROM
user_tab_columns t,
user_cons_columns c
WHERE
t.table_name = 'tb1'
AND t.table_name = c.table_name
AND t.column_name = 'column1'
AND c.column_name = t.column_name
AND t.nullable = 'N');

This and everything else I've thus far tried only results in a "ORA-02250: missing or invalid constraint name" error. I have to remove these contraints (and afterwards the columns) via a script, as the client simply will not let us touch their production server. They want a script for their DBA to run, and their DBA is lazy and won't clean these himself. There has to be a way to utilize this in SQL so I can make the necessary changes. Thank you in advance for any advice.
This post has been answered by unknown-7404 on Mar 20 2012
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 18 2012
Added on Mar 20 2012
3 comments
2,311 views