Search notes:

ORA-02449: unique/primary keys in table referenced by foreign keys

The error message ORA-02449: unique/primary keys in table referenced by foreign keys is caused by the drop table statement if the table that is attempted to be dropped is referenced by one or more tables with foreign keys that reference the to-be-dropped table's primary key.

DROP TABLE … CASCADE CONSTRAINTS

create table tq84_A (
   id number primary key
);

create table tq84_B (
   id   number primary key,
   id_a        references TQ84_A
);

alter table tq84_A add id_b references tq84_B;

--
--   The following statements both cause ORA-02449: unique/primary keys in table referenced by foreign keys
--
drop table tq84_A;
drop table tq84_B;

--
--   Tables can be dropped with DROP TABLE … CASCADE CONSTRAINTS:
--
drop table tq84_A cascade constraints;
drop table tq84_B cascade constraints;

Remove foreign keys (Brute force)

The following script is quite a brute force attempt to remove all foreign keys which reference a table so that the table can be dropped.
If this script should be used, of course, is in the eye of the beholder.
create table tq84_A (
   id number primary key
);

create table tq84_B (
   id   number primary key,
   id_a        references TQ84_A
);

alter table tq84_A add id_b references tq84_B;

drop table tq84_A;
-- ORA-02449: unique/primary keys in table referenced by foreign keys

--
-- Brute force!
-- Remove all foreign key constraints that
-- refer to table tq84_A.
--
declare
   tab_name varchar2(30) := 'TQ84_A';
begin
   for r in (
       select
          fk.constraint_name  fk_cons_name,
          fk.table_name       fk_tab_name
       from
          user_constraints pk   join
          user_constraints fk on pk.constraint_name = fk.r_constraint_name
       where
          pk.table_name      =  tab_name and
          pk.constraint_type = 'P'
   ) loop

   -- dbms_output.put_line(r.fk_cons_name || ': ' || r.fk_tab_name);
      execute immediate 'alter table ' || r.fk_tab_name || ' drop constraint ' || r.fk_cons_name;

   end loop;
end;
/

drop table tq84_A;
drop table tq84_B;
Github repository Oracle-Patterns, path: /errors/ORA/02449_unique-primary-keys-in-table-referenced-by-foreign-keys/go.sql
Compare the effect of this script with alter table disable primary key cascade.

See also

Error messages related to referential integrity.
Foreign keys and primary keys.
Other Oracle error messages

Index