Let us find out which constraints are enabled on our parent table TEMP_JP1.
SQL> select owner, constraint_name, constraint_type, table_name, r_owner, r_constraint_name
2 from all_constraints where constraint_type in ('P','U') and table_name='TEMP_JP1';
OWNER CONSTRAINT_NAME C TABLE_NAME R_OWNER R_CONSTRAINT_NAME
---------- --------------- - ---------- ---------- --------------------
JP TEMP_JP1_PK P TEMP_JP1
JP TEMP_JP1_UK U TEMP_JP1
Now we know the primary/unique key constraints enabled on our parent table, TEMP_JP1.
The r_constraint_name( primary/unique constraint name in the parent table) column in the all_constraints view is referenced by the constraint_name (foreign key(referential integrity) constraint in the child table), when the constraint_type is R'. Using this definition, let us find out all the foreign key(referential integrity) constraints referencing the TEMP_JP1 table:
SQL> select owner, constraint_name, constraint_type, table_name, r_owner, r_constraint_name
2 from all_constraints
3 where constraint_type='R'
4 and r_constraint_name in (select constraint_name from all_constraints
5 where constraint_type in ('P','U') and table_name='TEMP_JP1');
OWNER CONSTRAINT_NAME C TABLE_NAME R_OWNER R_CONSTRAINT_NAME
---------- --------------- - ---------- ---------- --------------------
JP TEMP_JP2_FK R TEMP_JP2 JP TEMP_JP1_PK
JP TEMP_JP3_FK R TEMP_JP3 JP TEMP_JP1_UK
Here in my sub-query, I am supplying ONLY the primary and unique key constraint types with the qualifier where constraint_type in (P','U') , as we had seen earlier, that foreign key constraints can only be enabled on a child table, when a PRIMARY/UNIQUE KEY constraint is enabled on the parent table.
We had identified the foreign key constraints that are referencing the parent table TEMP_JP1.
Let us try to disable the primary/unique key constraints on the table.
SQL> alter table temp_jp1 disable constraint temp_jp1_pk;
alter table temp_jp1 disable constraint temp_jp1_pk
*
ERROR at line 1:
ORA-02297: cannot disable constraint (JP.TEMP_JP1_PK) - dependencies exist
Let us disable the unique key constraint from our parent table, TEMP_JP1.
SQL> alter table temp_jp1 disable constraint temp_jp1_uk;
alter table temp_jp1 disable constraint temp_jp1_uk
*
ERROR at line 1:
ORA-02297: cannot disable constraint (JP.TEMP_JP1_UK) - dependencies exist
Now we understand that as long as the foreign key constraints are enabled on the child tables, the data in the parent table is protected. |