Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Usenet -> c.d.o.server -> Re: script to dynmically create all foreign key constraints

Re: script to dynmically create all foreign key constraints

From: Karsten Farrell <kfarrell_at_belgariad.com>
Date: Wed, 11 Dec 2002 18:14:51 GMT
Message-ID: <vgLJ9.3426$TH.95190361@newssvr21.news.prodigy.com>


Steve wrote:
> hey there,
> anyone have a script handy that generates sql to create all the
> foreign key's in the database? Our client does not like ON DELETE
> CASCADE to be left on. However, once in a while we need to use that
> functionlity so I as going to drop the FK's in restricted session,
> recreate them with ON DELETE CASCADE, perform the operation what
> requires DELETE CASCADE, and then drop and recreate the FK's without
> ON DELETE CASCADE. I know this sounds kind of hokey but the ON DELETE
> CASCADE functionality is only ever required once every odd month so. I
> know I can get the constraints from import export but I'd like a
> dynaminc sql that I can just edit one and run.
The following lists all the PK/FK constraints. You can probably modify the routine to generate the ALTER TABLE commands:

set echo off feedback off pause off pagesize 0 set serveroutput on size 100000

DECLARE
   --
   cursor c_user_tables is

     select table_name
     from   user_tables
     order by table_name;

   --
   cursor c_user_constraints ( vin_table_name in varchar2 ) is
     select table_name,
            constraint_type,
            constraint_name,
            r_constraint_name,
            decode ( delete_rule,
              'CASCADE', '-YES-',
              'No'
            )                      decode_delete_rule,
            status
     from   user_constraints
     where  constraint_type in ('P', 'R')
     and    table_name = vin_table_name
     order by table_name, constraint_type, constraint_name;
   --
   cursor c_user_cons_columns ( vin_constraint_name in varchar2 ) is
     select constraint_name,
            column_name
     from   user_cons_columns
     where  constraint_name = vin_constraint_name
     order by position;

   --
BEGIN
   --
   dbms_output.enable ( 100000 );
   --

set serveroutput off
set feedback on pagesize 24 Received on Wed Dec 11 2002 - 12:14:51 CST

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US