title: How to update foreign keys while inserting rows?
body: <p>I have two tables with the following layout ...</p>
  
  <pre><code>-------------    -------------------------
  | Master    |    | History               |
  -------------    -------------------------
  | id | name |    | id | name | master_id |
  -------------    -------------------------
  </code></pre>
  
  <p>The <code>history</code> table contains rows where the <code>master_id</code> is <code>NULL</code>. Those I copy into the <code>master</code> table.</p>
  
  <pre class="lang-sql prettyprint-override"><code>INSERT master (name)
  SELECT name
  FROM histories h
  WHERE h.master_id = NULL
  </code></pre>
  
  <p>How can I update the <code>master_id</code> in the <code>history</code> table with the <code>id</code> of the associated object in the <code>master</code> table? This update should happen in the same step as the insertion is made. I use MySQL if this is of interest.</p>