Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6765321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:41:54+00:00 2026-05-26T14:41:54+00:00

I have a table which contains the following columns and I am trying to

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T14:41:55+00:00Added an answer on May 26, 2026 at 2:41 pm

    I think the easiest way to do this is with two selects from your source table. The first one is a cinch:

    insert into def
    select id, col1, col2 from abc
    

    The second one uses the INSERT ALL syntax:

    INSERT ALL
       WHEN col3 is not null THEN
          INTO ghi values (some_seq.nextval, id, col3)
       WHEN col4 is not null THEN
          INTO ghi values (some_seq.nextval, id, col4)
       WHEN col5 is not null THEN
          INTO ghi values (some_seq.nextval, id, col5)
       WHEN col6 is not null THEN
          INTO ghi values (some_seq.nextval, id, col6)
       SELECT id, col3, col4, col5, col6
          FROM abc;
    

    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:

    SQL> select * from ghi;
    
            ID     DEF_ID      VALUE
    ---------- ---------- ----------
             1          1          3
             2          2          9
             1          1          4
             2          2         10
             1          1          5
             2          2         11
             1          1          6
             2          2         12
    
    8 rows selected.
    
    SQL> 
    

    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

    1. disable the primary key (or create the table without one).
    2. populate it with data from ABC
    3. populate the primary key column (in whatever fashion)
    4. enable the primary key constraint

    Alternatively you need to find some other way to populate the GHI ID column.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table of surveys which contains (amongst others) the following columns survey_id
I have a table which contains the following columns: UniqueID remote_ip_addr platform I want
i have a table Students which contains the following colums: Id,FirstName,LastName,Adress . The colum
I have the following problem: In mysql I have a table which contains two
I have a table with the following columns [itemdate][itemID][itemspecialid] itemID contains a string with
I have a database with a single table right now, which contains the following
I have a data.table which contains multiple columns, which is well represented by the
Hi I have a table which contains filed name as OBJECT. I am trying
I have a table called order which contains columns id , user_id , price
I have a table with around 19 columns which contains reasonable large amount of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.