Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two tables reference each other: How to insert row in an Oracle database?

Tags:

sql

oracle

I have two tables

  1. Department
  2. Professor

in which Department has an attribute called HeadID referencing Professor and Professor has an attribute called DeptID referencing Department

They form a circular relationship.

But the problem is that, how to insert a row to any of these tables?

Oracle complained "parent key not found" after I tried insert a row.

like image 847
sc1013 Avatar asked Mar 25 '12 09:03

sc1013


People also ask

How do I insert a row from one table to another in Oracle?

The simplest way to create an Oracle INSERT query to list the values using the VALUES keyword. For example: INSERT INTO suppliers (supplier_id, supplier_name) VALUES (5000, 'Apple'); This Oracle INSERT statement would result in one record being inserted into the suppliers table.

How do I insert values in tables which are related to each other by foreign key?

If you are inserting data into a dependent table with foreign keys: Each non-null value you insert into a foreign key column must be equal to some value in the corresponding parent key of the parent table. If any column in the foreign key is null, the entire foreign key is considered null.


1 Answers

You can define one of the foreign key constraints as DEFERRABLE and defer constraint checking until the end of your transaction (instead of checking at the end of statement which ends with "parent key not found"). Read here

like image 109
Marcin Wroblewski Avatar answered Oct 06 '22 18:10

Marcin Wroblewski