Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Disable all table Constraints Oracle

Posted on 2008-10-02
9
2,108 Views
Last Modified: 2013-12-07
Below I have attached a script I use to disable all scripts in the specified table, which works perfect. I am unsure on how I would modify this so it would disable all constraints in the entire database.

-- DISABLE --
accept tab prompt "Enter the name of the table to disable its constraints: "
set serveroutput on
declare
    sql_stm    varchar2(2000);
begin
    dbms_output.enable(1000000);
    for x in (select constraint_name
                    ,decode(constraint_type,'P',' cascade') casc
               from user_constraints
               where table_name = upper('&tab')) loop
        sql_stm := 'alter table &tab disable constraint '||
            x.constraint_name||x.casc;
        dbms_output.put_line(sql_stm);
        execute immediate sql_stm;
    end loop;
end;
/

Open in new window

0
Comment
Question by:CurtinProp
9 Comments
 
LVL 7

Accepted Solution

by:
fluglash earned 100 total points
ID: 22631583
add outer loop like that:


for y in (select table_name 
             from user_tables) loop
    for x in (select constraint_name
              ,decode(constraint_type,'P',' cascade') casc
              from user_constraints
              where table_name = y.table_name) loop
        sql_stm := 'alter table ' || y.table_name || ' disable constraint '||
            x.constraint_name||x.casc;
        dbms_output.put_line(sql_stm);
        execute immediate sql_stm;
    end loop;
end loop;

Open in new window

0
 
LVL 27

Assisted Solution

by:sujith80
sujith80 earned 100 total points
ID: 22631598
Disabling all the constraints in the database could break a lot and lot of things.
Are you sure you want to do that?

See the changed script. It disables all the constraints of the current database schema.
You can change user_constraints to dba_constraints for all the constraints in the database. But check all the constraints to make sure that they are supposed to be dropped.
set serveroutput on
declare
    sql_stm    varchar2(2000);
begin
    dbms_output.enable(1000000);
    for x in (select table_name, constraint_name
                    ,decode(constraint_type,'P',' cascade') casc
               from user_constraints ) loop
        sql_stm := 'alter table '||x.table_name||' disable constraint '||
            x.constraint_name||x.casc;
        dbms_output.put_line(sql_stm);
        execute immediate sql_stm;
    end loop;
end;
/

Open in new window

0
 
LVL 22

Assisted Solution

by:Ivo Stoykov
Ivo Stoykov earned 100 total points
ID: 22631623
Hi

Here is a sample. It will create a script you could run in sqlplus or Command window

HTH

I
set linesize 120;
SET head off;
SET echo off;
SET pagesize 0;
SET feedback off;
 
SPOOL disable_all_constraints.sql;
SELECT 'ALTER TABLE ' ||
       owner ||
       '.' ||
       table_name ||
       ' DISABLE CONSTRAINT ' ||
       constraint_name ||
       ';' "ALTER Statement"
  FROM dba_constraints
/
 
SPOOL off;

Open in new window

0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 34

Assisted Solution

by:johnsone
johnsone earned 100 total points
ID: 22633309
If you are going to attempt all tables in the database, it would be much safer to specify a list of owners.  At a minimum, you should exclude SYS and SYSTEM.  Disabling constraints in those schemas would most likely render your database unsupportable.
0
 
LVL 27

Expert Comment

by:sujith80
ID: 22634469
@johnsone:
Very good point.
0
 
LVL 10

Assisted Solution

by:dbmullen
dbmullen earned 100 total points
ID: 22647001
you provided your own answer
just remove the accept and the "where clause"
-- DISABLE --
--accept tab prompt "Enter the name of the table to disable its constraints: "
set serveroutput on
declare
    sql_stm    varchar2(2000);
begin
    dbms_output.enable(1000000);
    for x in (select constraint_name
                    ,decode(constraint_type,'P',' cascade') casc
               from user_constraints
--               where table_name = upper('&tab')
               ) loop
        sql_stm := 'alter table &tab disable constraint '||
            x.constraint_name||x.casc;
        dbms_output.put_line(sql_stm);
        execute immediate sql_stm;
    end loop;
end;
/

Open in new window

0
 

Expert Comment

by:sakthikumar
ID: 32343065
Is there any single alter option to disable all constraints that belong to a table
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Truncate is a DDL Command where as Delete is a DML Command. Both will delete data from table, but what is the difference between these below statements truncate table <table_name> ?? delete from <table_name> ?? The first command cannot be …
I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question