|
|
Tips for ORA-02292 Constraint violation - child records found
Oracle Database Tips by Donald Burleson |
Question: I defined a master/slave
table with a constraint using the "on delete restrict" clause.
When I try to delete a row from the master table I get the Oracle
error: ORA-02292: integrity constraint (user....) violated - child
record found.
Answer: When you use the "on delete" syntax you have
two choices "on delete restrict" and on delete cascade". The error message
will point you to the specific owner and constraint name:
ORA-02292: violated integrity constraint (owner.constraintname)- child
record found
You might also see these related errors:
ORA-02290: check constraint (owner.constraintname) violated
ORA-02291: integrity constraint (owner.constraintname) violated - parent key
not found
Avoiding ORA-02292 with ON
DELETE
This explanation from John Garmany's book "Easy
Oracle SQL" describes the process in detail:
Let's create the constraint.
SQL> alter table emp
2 add (constraint job_fk foreign key (job_key)
3 references job (job_key)
4 on delete cascade);
Now, when INSERT or UPDATE the job key column in the EMP table, the foreign key
constraint will check to insure that the job already exists in the JOB table (or
at least the job key exist in the JOB table).
Foreign key constraints can also be disabled, enabled and dropped.
alter table emp disable
constraint job_fk;
alter table emp enable constraint job_fk;
alter table emp drop constraint job_fk;
Now, let's talk about the ON DELETE part of the constraint. When we created the
foreign key constraint . we included ON DELETE SET NULL or ON DELETE CASCADE.
This clause tells the database what to do with the child records when the parent
record is deleted. In the example above, we created the job_fk constraint with
ON DELETE CASCADE.
This will cause the database to cascade the
deletes. If I go to the JOB table and DELETE a job, all the employees that have
that job will also be deleted as the DELETE will cascade to the child rows.
If we use the ON DELETE SET NULL, then when
we delete the parent record, the child records with that value will be set to
NULL.
|
If you like Oracle tuning, see the book "Oracle
Tuning: The Definitive Reference", with 950 pages of tuning tips and
scripts.
You can buy it direct from the publisher for 30%-off and get
instant access to the code depot of Oracle tuning scripts. |