I have a table which contains the following columns and I am trying to split it as follows. How do I write a migration script for the following transformation in Oracle
source:
create table abc (id pk, col1, col2, col3, col4, col5, col6)
targets:
create table def (id pk, col1, col2)
create table ghi (id pk, def_id fk, value)
Given this starting point …
insert into table abc values (1, 1, 2, 3, 4, 5, 6)
insert into table abc values (2, 7, 8, 9, 10, 11, 12)
… the post translation the data gets mapped as follows
def contains (1, 1, 2)
ghi contains (1, 1, 3)
ghi contains (2, 1, 4)
ghi contains (3, 1, 5)
ghi contains (4, 1, 6)
def contains (2, 7, 8)
ghi contains (5, 2, 9)
ghi contains (6, 2, 10)
ghi contains (7, 2, 11)
ghi contains (8, 2, 12)
The most important restriction is that I want to generate rows in ghi only if the corresponding column values in abc are not null
I think the easiest way to do this is with two selects from your source table. The first one is a cinch:
The second one uses the INSERT ALL syntax:
Note, I am presuming a sequence SOME_SEQ as the source of primary key values for GHI. You may want to have some other mechanism.
Hmmm, having run my code I get this result:
As you can see, SOME_SEQ has only incremented for the two rows in ABC not the eight rows in GHI. This makes sense because two calls to NEXTVAL in the same statement return the same value, but I was hoping that each INSERT would count separately.
Oh well. This means for your migration you cannot enforce the primary key on GHI until after you’ve populated it. So
Alternatively you need to find some other way to populate the GHI ID column.