Search code examples
oracle-databasenormalizationdata-migration

Oracle: Normalizing data during migration


I have a table with a lot of repeated data that I'd like to refactor as 3 tables.

Current structure looks like:

meeting_desc
meeting_date
topic_desc
...

And the data in the current_table looks like:

meeting1,2/3/2009,abc
meeting1,2/3/2009,efg
meeting1,2/3/2009,xyz
meeting2,4/5/2009,aaa
meeting2,4/5/2009,bbb

I would like to create a meeting table and a topic table, with PKs coming from a sequence:

MEETING: 
  id
  meeting_desc
  meeting_date

TOPIC:
  id
  meeting_id
  topic_desc

What I can't figure out is how to insert data into the new tables. I've tried:

insert into MEETING select distinct 
  seq.nextval, meeting_desc, meeting_date from current_table

but of course that doesn't work. Is there an easy way to normalize the data?


Solution

  • The DISTINCT placed in a subquery should work:

    SQL> INSERT INTO meeting
      2     SELECT seq.nextval, meeting_desc, meeting_date
      3       FROM (SELECT DISTINCT meeting_desc, meeting_date
      4               FROM current_table);
    
    2 rows inserted
    

    Once this is done, you would join this newly created table with the old table to associate the generated ids to the children tables:

    SQL>   INSERT INTO topic
      2       SELECT m.id, topic_seq.NEXTVAL, ct.topic_desc
      3         FROM current_table ct
      4         JOIN meeting m ON (ct.meeting_desc = m.meeting_desc 
      5                            AND ct.meeting_date = m.meeting_date);
    
    5 rows inserted