Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable all table constraints in Oracle

Tags:

sql

oracle

How can I disable all table constrains in Oracle with a single command? This can be either for a single table, a list of tables, or for all tables.

like image 900
oneself Avatar asked Sep 24 '08 17:09

oneself


People also ask

How do I turn off constraints in a table?

You can use the ALTER TABLE statement to enable, disable, modify, or drop a constraint. When the database is using a UNIQUE or PRIMARY KEY index to enforce a constraint, and constraints associated with that index are dropped or disabled, the index is dropped, unless you specify otherwise.

How do I disable all constraints in Oracle for one schema?

Best Answer begin for all_cons in ( select table_name, constraint_name from user_constraints where constraint_type in ('U', 'P', 'R') ) loop execute immediate 'alter table '||all_cons||' disable constraint '||all_cons. constraint_name; end loop; end; Note that this only does the relational constraints.

How we can disable constraint in Oracle?

There are multiple ways to disable constraints in Oracle. constraint_name; Another way to enable and disable constraints in Oracle would be to either use a plsql block or write a script. Execute Immediate 'alter table '||:tab_name||' disable constraint '||tabCons(numCount);

Is it possible to disable constraints on a table in SQL?

You can actually disable all database constraints in a single SQL command and the re-enable them calling another single command.


1 Answers

It is better to avoid writing out temporary spool files. Use a PL/SQL block. You can run this from SQL*Plus or put this thing into a package or procedure. The join to USER_TABLES is there to avoid view constraints.

It's unlikely that you really want to disable all constraints (including NOT NULL, primary keys, etc). You should think about putting constraint_type in the WHERE clause.

BEGIN   FOR c IN   (SELECT c.owner, c.table_name, c.constraint_name    FROM user_constraints c, user_tables t    WHERE c.table_name = t.table_name    AND c.status = 'ENABLED'    AND NOT (t.iot_type IS NOT NULL AND c.constraint_type = 'P')    ORDER BY c.constraint_type DESC)   LOOP     dbms_utility.exec_ddl_statement('alter table "' || c.owner || '"."' || c.table_name || '" disable constraint ' || c.constraint_name);   END LOOP; END; / 

Enabling the constraints again is a bit tricker - you need to enable primary key constraints before you can reference them in a foreign key constraint. This can be done using an ORDER BY on constraint_type. 'P' = primary key, 'R' = foreign key.

BEGIN   FOR c IN   (SELECT c.owner, c.table_name, c.constraint_name    FROM user_constraints c, user_tables t    WHERE c.table_name = t.table_name    AND c.status = 'DISABLED'    ORDER BY c.constraint_type)   LOOP     dbms_utility.exec_ddl_statement('alter table "' || c.owner || '"."' || c.table_name || '" enable constraint ' || c.constraint_name);   END LOOP; END; / 
like image 106
WW. Avatar answered Sep 24 '22 03:09

WW.