Home » SQL & PL/SQL » SQL & PL/SQL » How To Disable or Enable All Constraints in a Table (Oracle 9i)
How To Disable or Enable All Constraints in a Table [message #450571] Thu, 08 April 2010 01:36 Go to next message
sivaora
Messages: 119
Registered: October 2009
Location: Hyderabad
Senior Member
Hi All,

I knew, how to enable or disable a constraint on a table.

>alter table <table_name> disable constraint <constraint_name>;

But if we know all the data is correct, it's better to disable all constraints to improve the performance.

We are allowed to Disable or Enable only a single constraint.

can any one give me the solution to Enable or Disable All Constraints.

Thanks in Advance.
Re: How To Disable or Enable All Constraints in a Table [message #450573 is a reply to message #450571] Thu, 08 April 2010 01:42 Go to previous messageGo to next message
adit_me1
Messages: 49
Registered: October 2007
Location: BANGALORE
Member
Disable all constraints and triggers --

begin	
BEGIN
      FOR rec IN ( SELECT trigger_name FROM user_triggers WHERE TRIGGER_NAME NOT LIKE 'BIN$%' )
      LOOP
            EXECUTE IMMEDIATE 'alter trigger '||rec.trigger_name||' disable';
      END LOOP;
    END;
    DBMS_OUTPUT.PUT_LINE('TRIGGERS DISABLED');
    BEGIN
      FOR rec IN ( SELECT constraint_name, table_name FROM user_constraints WHERE constraint_type = 'R' )
      LOOP
        BEGIN
        EXECUTE IMMEDIATE 'alter table '||rec.table_name||' disable constraint '||rec.constraint_name;
         EXCEPTION WHEN OTHERS THEN
             dbms_output.put_line( 'ERROR: alter table '||rec.table_name||' DISABLE constraint '||rec.constraint_name||';' );
        END;      
      END LOOP;
      DBMS_OUTPUT.PUT_LINE('Constraints DISABLED');
    END;
end;    
/



Enable all constraints and triggers

begin
  BEGIN
      FOR rec IN ( SELECT constraint_name, table_name FROM user_constraints where status = 'DISABLED' )
      LOOP
        BEGIN
          EXECUTE IMMEDIATE 'alter table '||rec.table_name||' enable NOVALIDATE constraint '||rec.constraint_name;
          dbms_output.put_line('alter table '||rec.table_name||' enable NOVALIDATE constraint '||rec.constraint_name);
        EXCEPTION
          WHEN OTHERS THEN
            dbms_output.put_line('ERROR: alter table '||rec.table_name||' enable NOVALIDATE constraint '||rec.constraint_name||' ;' );
            
        END;
      END LOOP;
    END;
                DBMS_OUTPUT.PUT_LINE('CONSTRAINTS ENABLED');
    BEGIN
      FOR rec IN ( SELECT trigger_name FROM user_triggers WHERE TRIGGER_NAME NOT LIKE 'BIN$%' )
      LOOP
          EXECUTE IMMEDIATE 'alter trigger '||rec.trigger_name||' enable';
      END LOOP;
    END;
                DBMS_OUTPUT.PUT_LINE('TRIGGERS ENABLED');
END;
/
Re: How To Disable or Enable All Constraints in a Table [message #450574 is a reply to message #450571] Thu, 08 April 2010 01:43 Go to previous messageGo to next message
John Watson
Messages: 9000
Registered: January 2010
Location: Global Village
Senior Member
Are you sure that disabling constraints will improve performance? Constraints can improve performance: they give the optimizer a great deal of information, which can help it generate better execution plans.
Re: How To Disable or Enable All Constraints in a Table [message #450578 is a reply to message #450571] Thu, 08 April 2010 01:59 Go to previous messageGo to next message
John Watson
Messages: 9000
Registered: January 2010
Location: Global Village
Senior Member
Here's a typical example of disabling a constraint causing a problem
SQL> set autotrace on exp
SQL> select count(*) from emp;

  COUNT(*)
----------
        14


Execution Plan
----------------------------------------------------------
Plan hash value: 2937609675

-------------------------------------------------------------------
| Id  | Operation        | Name   | Rows  | Cost (%CPU)| Time     |
-------------------------------------------------------------------
|   0 | SELECT STATEMENT |        |     1 |     1   (0)| 00:00:01 |
|   1 |  SORT AGGREGATE  |        |     1 |            |          |
|   2 |   INDEX FULL SCAN| PK_EMP |    14 |     1   (0)| 00:00:01 |
-------------------------------------------------------------------

SQL> alter table emp disable constraint pk_emp;

Table altered.

SQL> select count(*) from emp;

  COUNT(*)
----------
        14


Execution Plan
----------------------------------------------------------
Plan hash value: 2083865914

-------------------------------------------------------------------
| Id  | Operation          | Name | Rows  | Cost (%CPU)| Time     |
-------------------------------------------------------------------
|   0 | SELECT STATEMENT   |      |     1 |     3   (0)| 00:00:01 |
|   1 |  SORT AGGREGATE    |      |     1 |            |          |
|   2 |   TABLE ACCESS FULL| EMP  |    14 |     3   (0)| 00:00:01 |
-------------------------------------------------------------------

SQL>
Re: How To Disable or Enable All Constraints in a Table [message #450579 is a reply to message #450574] Thu, 08 April 2010 02:04 Go to previous messageGo to next message
sivaora
Messages: 119
Registered: October 2009
Location: Hyderabad
Senior Member
Thank you Adit for your quick reply.
Yours code performing right.But is there any way to do the same thing in sql other than writing the pl/sql code.

Hi John,
I am not sure about it, i think that if we are inserting the correct data then no need to check it through constraints, and it may take time to validate.

Re: How To Disable or Enable All Constraints in a Table [message #450580 is a reply to message #450571] Thu, 08 April 2010 02:15 Go to previous messageGo to next message
Yasir Hashmi
Messages: 304
Registered: April 2006
Senior Member
spool cons.sql
select 'alter table '||table_name ||' disable constraint '||constraint_name||'
/'
from user_constraints
/
spool off
Re: How To Disable or Enable All Constraints in a Table [message #450581 is a reply to message #450579] Thu, 08 April 2010 02:16 Go to previous messageGo to next message
Yasir Hashmi
Messages: 304
Registered: April 2006
Senior Member
And what is the likelihood that you will nevere insert wrong data?
Re: How To Disable or Enable All Constraints in a Table [message #450586 is a reply to message #450581] Thu, 08 April 2010 02:29 Go to previous messageGo to next message
Littlefoot
Messages: 21826
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
It is just a matter of days when the OP will return with another question: "I wanted to enable constraints I disabled the other day, but I keep getting those annoying errors. What should I do?"
Re: How To Disable or Enable All Constraints in a Table [message #450595 is a reply to message #450579] Thu, 08 April 2010 03:41 Go to previous messageGo to next message
JRowbottom
Messages: 5933
Registered: June 2006
Location: Sunny North Yorkshire, ho...
Senior Member
There's no 'may' about it - it WILL take time to validate. The question is, when do you want to spend that time.

Disabling the constraints will make the import process run faster, assuming that you're just doing straight forward inserts.

Re-anabling the constraints afterwards is going to be time consuming, as the constraints will be checked for all the previously existing rows, as well as the rows that you just added.
Re: How To Disable or Enable All Constraints in a Table [message #450597 is a reply to message #450571] Thu, 08 April 2010 03:49 Go to previous messageGo to next message
John Watson
Messages: 9000
Registered: January 2010
Location: Global Village
Senior Member
My posted example is simple proof that the constraint improves the performance of the query.
In a datawarehouse, it is often worth adding as many not null, unique, and foreign key constraints as you can. Options such as RELY or ENABLED NOVALIDATE may help.
Re: How To Disable or Enable All Constraints in a Table [message #450598 is a reply to message #450597] Thu, 08 April 2010 03:53 Go to previous messageGo to next message
Frank
Messages: 7901
Registered: March 2000
Senior Member
John Watson wrote on Thu, 08 April 2010 10:49
My posted example is simple proof that the constraint improves the performance of the query.

To be more precise, your example proved that (at least) in certain cases constraints can influence the behaviour of the optimizer. This does in no way proof that having constraints could or would improve the performance of straight inserts.
Re: How To Disable or Enable All Constraints in a Table [message #450599 is a reply to message #450598] Thu, 08 April 2010 04:04 Go to previous message
John Watson
Messages: 9000
Registered: January 2010
Location: Global Village
Senior Member
@Frank
Thank you for the correction, I should have qualified my post to make it clear that I was referring only to SELECT performance, not necessarily DML.
@sivaora, is your concern the performance of DML or SELECT?
Previous Topic: Update table using loop
Next Topic: ORA-06532 AND ORA-06512
Goto Forum:
  


Current Time: Wed Apr 08 22:25:33 CDT 2026