On Sun, 05 Oct 1997 09:34:25 +0200, Yochi Wieman <ordnkv_at_shani.net> wrote:
>Problem : Inability to drop table due to unknown enables
>FK which references the table.
>
>Situation : All known FK referencing the table were disable.
>All data in FK tables was deleted.
>Drop table failed due to 'unique/primary key referenced by enabled
>foreign keys'.
>
>Questions :
>1. In which table can we find all FK referencing a particular
>table ?
*_constraints (user_, all_, dba_
>2. Does DROP TABLE require dropping all the FK or just disabling
>them is enough ?
dropping the foreign keys
>3. If there is no table with FK references, what do you suggest
>as a solution to our problem ? :
I would use the script below before you drop the parent table. If you run it
and capture the output, it will generate all of the alter table "child_table"
add constraint clauses to rebuild the foreign keys.
What you would do is:
1 - run @cons PARENT_TABLE_NAME and spool the output to a temp file.
2 - drop table PARENT_TABLE_NAME cascade constraints
3 - recreate table PARENT_TABLE_NAME
4 - run temp.sql from 1
restriction: since cons.sql below uses only the user_constraints table, it will
not see foreign keys placed from other schemas on your table. this only works
if all the foreign keys are owned by the same owner as the table itself.
- begin cons.sql ----------------------
column fkey format a80 word_wrapped
select
'alter table "' || child_tname || '"' || chr(10) ||
'add constraint "' || child_cons_name || '"' || chr(10) ||
'foreign key ( ' || child_columns || ' ) ' || chr(10) ||
'references "' || parent_tname || '" ( ' || parent_columns || ');' fkey
from
( select a.table_name child_tname, a.constraint_name child_cons_name,
b.r_constraint_name parent_cons_name,
max(decode(position, 1, '"'||column_name||'"',NULL)) ||
max(decode(position, 2,', '||'"'||column_name||'"',NULL)) ||
max(decode(position, 3,', '||'"'||column_name||'"',NULL)) ||
max(decode(position, 4,', '||'"'||column_name||'"',NULL)) ||
max(decode(position, 5,', '||'"'||column_name||'"',NULL)) ||
max(decode(position, 6,', '||'"'||column_name||'"',NULL)) ||
max(decode(position, 7,', '||'"'||column_name||'"',NULL)) ||
max(decode(position, 8,', '||'"'||column_name||'"',NULL)) ||
max(decode(position, 9,', '||'"'||column_name||'"',NULL)) ||
max(decode(position,10,', '||'"'||column_name||'"',NULL)) ||
max(decode(position,11,', '||'"'||column_name||'"',NULL)) ||
max(decode(position,12,', '||'"'||column_name||'"',NULL)) ||
max(decode(position,13,', '||'"'||column_name||'"',NULL)) ||
max(decode(position,14,', '||'"'||column_name||'"',NULL)) ||
max(decode(position,15,', '||'"'||column_name||'"',NULL)) ||
max(decode(position,16,', '||'"'||column_name||'"',NULL))
child_columns
from user_cons_columns a, user_constraints b
where a.constraint_name = b.constraint_name
and b.constraint_type = 'R'
group by a.table_name, a.constraint_name, b.r_constraint_name ) child,
( select a.constraint_name parent_cons_name, a.table_name parent_tname,
max(decode(position, 1, '"'||column_name||'"',NULL)) ||
max(decode(position, 2,', '||'"'||column_name||'"',NULL)) ||
max(decode(position, 3,', '||'"'||column_name||'"',NULL)) ||
max(decode(position, 4,', '||'"'||column_name||'"',NULL)) ||
max(decode(position, 5,', '||'"'||column_name||'"',NULL)) ||
max(decode(position, 6,', '||'"'||column_name||'"',NULL)) ||
max(decode(position, 7,', '||'"'||column_name||'"',NULL)) ||
max(decode(position, 8,', '||'"'||column_name||'"',NULL)) ||
max(decode(position, 9,', '||'"'||column_name||'"',NULL)) ||
max(decode(position,10,', '||'"'||column_name||'"',NULL)) ||
max(decode(position,11,', '||'"'||column_name||'"',NULL)) ||
max(decode(position,12,', '||'"'||column_name||'"',NULL)) ||
max(decode(position,13,', '||'"'||column_name||'"',NULL)) ||
max(decode(position,14,', '||'"'||column_name||'"',NULL)) ||
max(decode(position,15,', '||'"'||column_name||'"',NULL)) ||
max(decode(position,16,', '||'"'||column_name||'"',NULL))
parent_columns
from user_cons_columns a, user_constraints b
where a.constraint_name = b.constraint_name
and b.constraint_type in ( 'P', 'U' )
group by a.table_name, a.constraint_name ) parent
where child.parent_cons_name = parent.parent_cons_name
and parent.parent_tname = upper('&1')
/
- end cons.sql ---------------------------
Thomas Kyte
tkyte_at_us.oracle.com
Oracle Government
Bethesda MD
http://govt.us.oracle.com/ -- downloadable utilities
Opinions are mine and do not necessarily reflect those of Oracle Corporation
Received on Sun Oct 05 1997 - 00:00:00 CDT