Sometimes you want to drop and recreate an Oracle table, and are in a fix because when you issue the DROP command Oracle responds with ORA-02449:
SQL> drop table agreement; drop table agreement * ERROR at line 1: ORA-02449: unique/primary keys in table referenced by foreign keys
Disabling constraints has no effect on ORA-02449, the only resolution is to drop constraints. Which presents another dilemma: the error does not specify which foreign keys are causing this problem, and from which tables.
Luckily, there is a quick workaround to ORA-02449, provided you are ready to heed the risks of the approach along with it.
How do you get past ORA-02449?
The shortest way out of this problem is the CASCADE CONSTRAINTS option (read the note “Cascade Constraints with Caution” listed below, before you try it!)
SQL> drop table agreement cascade constraints; Table dropped.
CASCADE CONSTRAINTS silently forces all related Foreign Key constraints to be dropped at the time of dropping a table.
Cascade Constraints with Caution
When you drop a table with CASCADE CONSTRAINTS, all related Foreign Key constraints get dropped. When you recreate the dropped table, the Foreign Key constraints do not reappear and must be recreated. There is a high risk of messing up referential integrity if the dropped and recreated table has many dependent tables.
To retain the Foreign Key details as-is when the referenced table is dropped and recreated, you will have to do a bit more along with CASCADE CONSTRAINTS to ascertain all the Foreign Keys getting impacted due to DROP TABLE, and recreate them later.
In the next post, we will see how to get a list of Foreign Keys referencing a specified table and how to easily recreate the dropped Foreign Keys.