Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle - drop table constraints without dropping tables

I'm doing some bulk migration of a large Oracle database. The first step of this involves renaming a whole load of tables as a preparation for dropping them later (but I need to keep the data in them around for now). Any foreign key constraints on them need to be dropped - they shouldn't be connected to the rest of the database at all. If I were dropping them now I could CASCADE CONSTRAINTS, but rename simply alters the constraints.

Is there a way I can drop all of the constraints that CASCADE CONSTRAINTS would drop without dropping the table itself?

like image 528
Submonoid Avatar asked Sep 13 '10 14:09

Submonoid


People also ask

Does dropping table drop constraint Oracle?

The CASCADE CONSTRAINTS clause drops the table and any foreign keys referencing it. The child tables remain otherwise intact. Views (and also any PL/SQL) referencing the table are left but in an invalid state.

How do I drop a constraint in Oracle?

To explicitly drop unique constraints, use the DROP UNIQUE clause of the ALTER TABLE statement. The DROP UNIQUE clause of the ALTER TABLE statement drops the definition of the unique constraint constraint-name and all referential constraints that are dependent upon this unique constraint.

What is purge in DROP TABLE?

Specify PURGE if you want to drop the table and release the space associated with it in a single step. If you specify PURGE , then the database does not place the table and its dependent objects into the recycle bin.

How do I purge a table in Oracle?

When issuing a DROP TABLE statement in Oracle, you can specify the PURGE option. The PURGE option will purge the table and its dependent objects so that they do not appear in the recycle bin. The risk of specifying the PURGE option is that you will not be able to recover the table.


1 Answers

You can do it with dynamic SQL and the data dictionary:

begin
    for r in ( select table_name, constraint_name
               from user_constraints
               where constraint_type = 'R' )
    loop
        execute immediate 'alter table '|| r.table_name
                          ||' drop constraint '|| r.constraint_name;
    end loop;
end;

If the tables are owned by more than one user you'll need to drive from DBA_CONSTRAINTS and include OWNER in the projection and the executed statement. If you want to touch less than all the tables I'm afraid you'll need to specify the list in the WHERE clause, unless there's some pattern to their names.

like image 161
APC Avatar answered Oct 26 '22 09:10

APC