Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Can I do multi table insert when one table has one to many relationship?

361137Aug 1 2012 — edited Aug 1 2012
I have a situation with 3 tables:

Table A, B and C
Table A has a PK which is a FK on table B (lets call it A_ID) and a version number which represents a set of data (I would copy by this field which could be shared among many rows)
They have a one-to-one relationship

Table C has a FK to B's PK, call it B_ID. Its a many to one relationship

Table A
A_ID PK
A_Text
Version_Number

Table B
B_ID PK
A_ID FK to A
B_Text

Table C
C_ID PK
B_ID FK

I need to do a copy of all the data from those tables into a new set of rows with newly generated keys.

if it was just A & B it would be simply
insert all
into A (A_ID, A_Text, Version_Number) values (A_SEQ.nextval, A_Text, New_Version_Number)
into B (B_ID, A_ID, B_Text) values (B_SEQ,nextval, A_SEQ.nextval, B_Text)
select A_Text, B_Text, 2 as New_Version_Number
from A, B where a.A_ID = b.A_ID and a.Version_Number = 1

and this would work fine

But it doesn't account for the child records of C to B - how do I get those copied from version 1 to 2?

Is there any way in the syntax to get them into this one insert? or do I have to do a separate insert where I need to do some crazy join to match the right B copy rows to the original B rows?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 29 2012
Added on Aug 1 2012
3 comments
552 views