Problem solve Get help with specific problems with your technologies, process and projects.

Enable/disable foreign key constraints

This script is useful for automatically enabling or disabling all the foreign keys in a schema.

This script allows you to enable all foreign key constraints. Also, if you replace the keyword "enable" with DISABLE it will disable all the foreign key constraints. It has been tested in Oracle 8.1.7 and Oracle 9i.

set linesize 200
 set serveroutput on SIZE 1000000
 DECLARE
 CURSOR CUR_CONSTRAINTS
 IS
  SELECT
  TABLE_NAME,
  CONSTRAINT_NAME
  FROM user_constraints
  WHERE constraint_type in ('R', 'C')
  ORDER BY constraint_type DESC;
 BEGIN
 DBMS_OUTPUT.PUT_LINE('Enabling Foreign Key Constraints');
 FOR REC_CONSTRAINTS IN  CUR_CONSTRAINTS LOOP
 DBMS_OUTPUT.PUT_LINE('alter table '||REC_CONSTRAINTS.table_name||' 
    ENABLE constraint '||REC_CONSTRAINTS.constraint_name||';');
 EXECUTE IMMEDIATE('alter table '||REC_CONSTRAINTS.table_name||' 
    ENABLE constraint '||REC_CONSTRAINTS.constraint_name);
 END LOOP;
 END;
 /

For More Information

  • Feedback: E-mail the editor with your thoughts about this tip.
  • More tips: Hundreds of free Oracle tips and scripts.
  • Tip contest: Have an Oracle tip to offer your fellow DBAs and developers? The best tips submitted will receive a cool prize -- submit your tip today!
  • Ask the Experts: Our SQL, database design, Oracle, SQL Server, DB2, metadata, and data warehousing gurus are waiting to answer your toughest questions.
  • Forums: Ask your technical Oracle questions--or help out your peers by answering them--in our active forums.
  • Best Web Links: Oracle tips, tutorials, and scripts from around the Web.

Dig Deeper on Oracle database design and architecture

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.

-ADS BY GOOGLE

SearchDataManagement

SearchBusinessAnalytics

SearchSAP

SearchSQLServer

TheServerSide.com

SearchDataCenter

SearchContentManagement

SearchHRSoftware

Close