ORA-02291: integrity constraint violated – parent key not found error occurs when a foreign key value in the child table does not have a matching primary key value in the parent table, as stated by a foreign key constraint. You try to insert a row into a child table that does not have a corresponding parent row. The column value you supplied for the child table did not match the primary key in the parent table.
You try to insert or update a row in the child table. The value in the child table’s reference column should be available in the parent table’s primary key column. If the primary key column does not have a value, the row cannot be inserted or updated in the child table. The parent key’s integrity constraint was violated.
The value of the child table’s foreign key column should be the same as the value of the parent table’s primary key column. If the value does not exist in the parent table, an error ORA-02291: integrity constraint violated – parent key not found will be thrown.
A foreign key value has no matching primary key value.
Delete the foreign key or add a matching primary key.
When two tables in a parent-child relationship are created, a referential foreign key constraint is generated and enforces the relationship between the two tables. The value of the foreign key column in the child table is decided by the value of the primary key column in the parent table.
A value that is not available in the parent table cannot be inserted or updated in the child table. If you try to insert or update a value in the foreign key column of a child table, Oracle will throw the parent key integrity constraint violation error.
create table dept ( id numeric(5) primary key, name varchar2(100) ); create table employee ( id numeric(5) primary key, name varchar2(100), deptid numeric(5) references dept(id) ); insert into employee values(1,'Yawin',1);
Error starting at line : 17 in command - insert into employee values(1,'Yawin',1) Error report - ORA-02291: integrity constraint (HR.SYS_C0012551) violated - parent key not found
If the integrity constraint is violated, knowing the parent and child tables involved in the foreign key relationship is important. The parent and child table names, as well as the column names, may be retrieved using the integrity constraint name. The parent table, child table, parent column name, child column name, and integrity constraint name will be shown in the following sql query.
select r.constraint_name Foreign_key_constraint, p.owner parent_owner, p.table_name parent_table, pc.column_name parent_column_name, r.owner child_owner, r.table_name child_table, rc.column_name child_colum_name from user_constraints p join user_cons_columns pc on p.owner=pc.owner and p.table_name=pc.table_name and p.constraint_name = pc.constraint_name and p.constraint_type='P' join user_constraints r on p.constraint_name=r.r_constraint_name and r.constraint_type='R' join user_cons_columns rc on r.owner=rc.owner and r.table_name=rc.table_name and r.constraint_name = rc.constraint_name and r.constraint_type='R' where r.constraint_name='SYS_C0012551' order by p.owner, p.table_name, pc.column_name, rc.position;
Foreign_key_constraint | parent_owner |parent_table | parent_column_name |child_owner | child_table | child_colum_name SYS_C0012548 HR DEPT ID HR EMPLOYEE DEPTID
The value you are trying to put into the child table reference column does not exist in the parent table. You must first enter the value that you intended to insert into the child table into the parent table. After inserting the value as a parent row, you may go back and enter it into the child table.
insert into dept values (1, 'sales'); insert into employee values(1,'Yawin',1)
1 row inserted. 1 row inserted.
You are attempting to insert a row into a child table for which the primary key does not exist in the parent table. Before you enter a child, make sure you have a parent key for that child in the parent table.
insert into employee values(1,'Yawin',1) insert into employee values(1,'Yawin',1) Error report - ORA-02291: integrity constraint (HR.SYS_C0012551) violated - parent key not found insert into employee values(1,'Yawin',100) -- the value 100 exist in the dept table.