In this article I will demonstrate a method where the INSERTALL statement can be used to insert rows into two tables that are related by a foreign key constraint.
The following example was developed and tested using the Pre-Built Oracle Database App Development VM which at the time of this post used Oracle Database 188.8.131.52.0 & SQL Developer 184.108.40.206. The example was created within the standard HR schema.
First I create two tables:
CREATE TABLE parent_tbl (the_pk NUMBER PRIMARY KEY, object_id NUMBER); CREATE TABLE child_tbl (the_pk NUMBER PRIMARY KEY, the_fk NUMBER, object_name VARCHAR2(30));
Next I add a foreign to define the relationship between these tables and two sequences used to populate the primary keys:
ALTER TABLE child_tbl ADD CONSTRAINT child_tbl_fk1 FOREIGN KEY (the_fk) REFERENCES parent_tbl (the_pk); CREATE SEQUENCE parent_tbl_seq; CREATE SEQUENCE child_tbl_seq START WITH 500;
Next I ensure that the foreign key relationship is working as expected by trying to insert a record into the child table with value for the_fk column that doesn’t exist in parent_tbl:
INSERT INTO child_tbl (the_pk, the_fk, object_name) VALUES (child_tbl_seq.nextval, 999, 'SomeObject');
Attempting to run this statement results in the expected error message:
SQL Error: ORA-02291: integrity constraint (HR.CHILD_TBL_FK1) violated - parent key not found
With the tables and relationship between them in place I can now demostrate how to use INSERTALL to insert information from user_objects into the parent and child tables.
INSERT ALL INTO parent_tbl(the_pk, object_id) VALUES (parent_tbl_seq.nextval, object_id) INTO child_tbl(the_pk, the_fk, object_name) VALUES (child_tbl_seq.nextval, parent_tbl_seq.currval, object_name) SELECT uo.object_id, uo.object_name FROM user_objects uo /
Lines 2 – 4 insert into the parent table, note the use of the sequence to populate the primary key.
Lines 5 – 8 insert into the child table, the important part (and the focus of this article) is to understand how the foreign key column, the_fk is populated. It uses currval of the sequence that was used to populate the parent table.
Thanks go to Tom Kyte for coming up with this elegant solution.
In this article I have demonstrated how to use INSERTALL to insert into two tables that are related by a foreign key.
The idea for this article came from Tom Kyte’s answer to this Ask Tom question.