title: Convert One to Many to One to One Relationship MySQL
body: <p>I have two MySQL tables we can call <code>Foo</code> and <code>Bar</code>.</p>
  
  <p>Both tables have a column called <code>PrizeGroupId</code>. the goal is to create a one-to-one relationship between these columns, and I have created stored procedures to add/edit <code>Foo</code> that update the corresponding row via the one-to-one relationship in <code>Bar</code>.</p>
  
  <p>The problem lies in the fact that the data wasn't always structured this way and I need to write a script to convert the data from it's previous state (which I'm about to describe) to a one-to-one relationship based off of <code>PrizeGroupId</code>.</p>
  
  <p>Previously, multiple rows in <code>Foo</code> could have the same <code>PrizeGroupId</code> such that there was a one-to-many relationship between entries in <code>Bar</code> to <code>Foo</code> based off of <code>PrizeGroupId</code>. The script that I need to write has to break apart every one-to-many instance of this nature into many (almost identical) one-to-one relationships between <code>Foo</code> and <code>Bar</code>.</p>
  
  <p>In principle, I want to:</p>
  
  <ol>
  <li>Iterate through <code>Foo</code></li>
  <li>See if the current row's <code>PrizeGroupId</code> is not unique in <code>Foo</code>. </li>
  <li>Assign it a unique value (perhaps the current items primary key) </li>
  <li>Add a row in <code>Bar</code> with the new <code>PrizeGroupId</code>. Copy over all of the old row's other data into this new row such that it is "nearly identical".</li>
  <li>After all is said and done, remove the old one-to-many row from <code>Bar</code>.</li>
  </ol>
  
  <p>I understand the problem and how I could do this in pseudocode in a programming language, however I am still learning MySQL and am not sure how to go about solving a problem of this nature. </p>
  
  <p>If you can provide me with help through MySQL code and/or what steps I can take/read about to go about solving this problem that would be, or at least point me to the kind of reading/SO question related to this kind of problem that would be appreciated, although I had a difficult time finding particular resources on my own.</p>