I'm working on Oracle11g, and trying to redefine a table with dbms_redefinition. It works ok but when trying to drop the interim table it throws an ORA-02449: unique/primary keys in table referenced by foreign keys
error.
I found a query to look for references right here in SO,
select table_name, constraint_name, status, owner
from all_constraints
where r_owner = 'MYSCHEMA'
and constraint_type = 'R'
and r_constraint_name in
(
select constraint_name from all_constraints
where constraint_type in ('P', 'U')
and table_name = 'INTERIM_TABLE'
and owner = 'MYSCHEMA'
)
order by table_name, constraint_name
which gives
table_name |constraint_name |status |owner
---------------------------------------------------------
anotherTable|TMP$$_anotherTable_JOB_ID0|DISABLED|MYSCHEMA
I suppose that this constraint was created during the redefinition process, that's ok, but I also expected that it must be deleted by the same process. Is this wrong? I say, is part of normal behavior that this constraint were not went deleted?
It's safe to just drop the constraint with
alter table anotherTable
drop constraint TMP$$_anotherTable_JOB_ID0
without a loss of data?
Thanks in advance.
-- EDIT -- After thinking about this, I've decided just to delete the constraint to let the interim table be droped.
I've modified the query to drop the constraints of other tables that point to the tables which I want to drop, almost automatically.
DECLARE
my_table varchar2(100);
my_constraint varchar2(100);
BEGIN
select table_name , constraint_name into my_table,my_constraint
from all_constraints
where r_owner = 'MYSCHEMA'
and constraint_type = 'R'
and r_constraint_name in
(
select constraint_name from all_constraints
where constraint_type in ('P', 'U')
and table_name = 'INTERIM_TABLE'
and owner = 'MYSCHEMA'
)
order by table_name, constraint_name;
execute immediate 'ALTER TABLE '||my_table||' DROP CONSTRAINT '|| my_constraint;
END;
/
DROP TABLE MYSCHEMA.INTERIM_TABLE;
This worked for me, but I must note that in my case that query throws just one row (only one dependant table), so this must be modified to drop many constraints by a loop or another method if you know someone.
It would be good if someone can figure out and explain why that constraint was not deleted by the process itself (or if this is a normal behavior).