Foreign Key Constraints
Foreign Key Constraints
----------------------------------------------------------
Author JP Vijaykumar Oracle DBA
Date 02-24-2007
Many a time, the developer folks, find it difficult in deleting records from parent tables, when child
records reference them through foreign key (referential integrity) constraints.
Constraints validate the data. Without constraints, we are just storing invalid data.
For a developer to identify foreign key constraints and disabling them is a difficult task. Most of
the time, the application’s ER diagrams are not available to the developers. A brief description on
the foreign key (referential integrity) constraints will go a long way in identifying and disabling
these constraints.
commit;
Created a second table TEMP_JP2. When I tried to create a foreign key(referential integrity)
constraint on the second table, I received error.
create table temp_jp2(col1 number);
Created foreign key(referential integrity) constraint on the second table successfully and
inserted one row.
commit;
Tried to add a foreign key(referential integrity) constraint to the third table, TEMP_JP3.
Again, make sure there are no duplicates in the parent key column, before enabling
foreign key(referential integrity constraints on a child key column.
Tried to add primary key index on the parent table TEMP_JP1.COL1. Duplicate primary keys are
not allowed in the parent table.
Ok, now I will add a unique key constraint on parent table TEMP_JP1’s
Col2. As the errors say, a table can have only one primary key constraint. A table can have
multiple unique key constraints.
Table altered.
The enabled foreign key constraint will not allow inserting child records in the table, unless a
matching record is found in the parent table
1 row created.
SQL> commit;
Commit complete.
In essence, constraints safeguard and validate the data.
Each parent record can have multiple child records, but each child can relate to ONLY one parent
record. That is why, Oracle want a PRIMARY/ UNIQUE KEY index created on the PRIMARY KEY
column of the PARENT TABLE. It is not mandatory to have such an index created on the CHILD
KEY column in the CHILD TABLE.
In our case, I could not enable foreign key constraints on TEMP_JP2 and TEMP_JP3 tables,
unless I first added a PRIMARY KEY constraint on TEMP_JP1.COL1 and a UNIQUE KEY
constraint on TEMP_JP1.COL2.
Now let us remove the records from the parent table TEMP_JP1.
SQL> truncate table temp_jp1;
truncate table temp_jp1
*
ERROR at line 1:
ORA-02266: unique/primary keys in table referenced by enabled foreign keys
The parent table can not be truncated, nor deleted, when foreign key(referential integrity)
constraint are referencing them.
Remember, when we tried to truncate the table TEMP_JP1, we received an error that says
“ORA-02266: unique/primary keys in table referenced by enabled foreign keys”
Let us find out the constraints enabled on our parent table TEMP_JP1
Here in my sub-query, I am supplying ONLY the primary and unique key constraints types with
the qualifier “where constraint_type in (‘P’,’U’) “, as we had seen earlier, tha 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 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 understood that, as long as the foreign key constraints are enabled on the child tables,
the data in the parent table is protected.
If we need to manipulate the data in the parent table TEMP_JP1, first disable the foreign
key(referential integrity) constraints on the child tables that are referencing the parent table.
Now let us disable the foreign key(referential integrity) constraints on the child tables, identified
from our earlier query.
Table altered.
Table truncated.
We could successfully truncate the parent table, after disabling the foreign key constraints on the
child tables, that were referencing the parent table.
A ready built script to identify and disable/enable foreign key constraints on child tables.
SQL> select 'alter table '||a.owner||'.'||a.table_name||
2 ' disable constraint '||a.constraint_name||';'
3 from all_constraints a, all_constraints b
4 where a.constraint_type = 'R'
5 and a.r_constraint_name = b.constraint_name
6 and a.r_owner = b.owner
7 and b.table_name = 'TEMP_JP1';
'ALTERTABLE'||A.OWNER||'.'||A.TABLE_NAME||'DISABLECONSTRAINT'||
A.CONSTRAINT_NAME
--------------------------------------------------------------------------------
alter table JP.TEMP_JP3 disable constraint TEMP_JP3_FK;
alter table JP.TEMP_JP2 disable constraint TEMP_JP2_FK;
Hope the narrative had given a clear picture about foreign key constraints and how to handle the
problems, while manipulating the data in the parent table.
1 row created.
1 row created.
SQL> INSERT INTO TEMP_JP3 VALUES(NULL);
1 row created.
Please be careful: Primary / Unique key indexes, both do not allow duplicates in the column. All
primary key indexes are unique. A table can have only one Primary key index and can have
multiple unique key indexes. One main difference is that you can not insert NULL values into a
primary key constraint enabled column, where as you can insert NULL values into a unique key
constraint enabled column.
For this reason, if your parent key in the parent table is indexed with a unique key constraint, then
enable NOT NULL constraint on the parent key column.
Table altered.