title: Split Table and Re-Insert all data properly
body: <p><strong>Question</strong></p>
  <p>Consider the Table &quot;ProductsStored&quot;</p>
  <pre><code>        |           ProductsStored               |
          ------------------------------------------
          | id | product | type      | description |
          ------------------------------------------
          | 1  |  AX     | Cleaning  | bla bla bla |
          | 2  |  AB     | Food      | bla bla bla |
          | 3  |  AJ     | Cleaning  | bla bla bla |
          | 4  |  KR     | Food      | bla bla bla |
  </code></pre>
  <p><strong>I want to split this table into two:</strong></p>
  <pre><code>|  ProductsType  |
  -------------------
  | id |  type     | 
  ------------------
  | 1  | Cleaning  | 
  | 2  | Food      | 
  
  
  |           Products                   |
  ----------------------------------------
  | id | product | idType  | description |
  ----------------------------------------
  | 1  |  AX     | 1       | bla bla bla |
  | 2  |  AB     | 2       | bla bla bla |
  | 3  |  AJ     | 1       | bla bla bla |
  | 4  |  KR     | 2       | bla bla bla |
  </code></pre>
  <p><strong>I have several schemas with these kind of tables. The data must be inserted and kept the ID intact in the new two tables.</strong></p>
  <p>I came up with the following code but im missing a part:</p>
  <h1>Insert the type of products</h1>
  <pre><code>INSERT INTO `ProductsType` (id, type) SELECT DISTINCT `type` FROM `ProductsStored`
  </code></pre>
  <h1>insert the products and make the relationship with its type from &quot;ProductsType&quot;</h1>
  <pre><code>INSERT INTO `Products` (id, product, description, idType) SELECT id,product,description ...(how do I get the idType?) 
  </code></pre>
  <p>I dont understand how I can get the idType properly. Also, is this approach the correct way to do it?</p>
  <p><strong>References</strong></p>
  <p><a href="https://stackoverflow.com/questions/53350034/how-split-data-from-table-and-insert-to-new-relation">How split data from table and insert to new relation?</a></p>
  <p>This topic almost answers my question but it uses @rownr which I dont understand why he does that. Also it seems he doesnt use proper PK's.</p>