title: MySQL: how to convert to EAV - Part 3?
body: <p>Previous Related Posts:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/7020661/mysql-how-to-convert-to-eav">MySQL: how to convert to EAV?</a></li>
  <li><a href="https://stackoverflow.com/questions/7034212/mysql-how-to-convert-to-eav-part-2">MySQL: how to convert to EAV - Part 2?</a></li>
  </ul>
  
  <p>Given a table:</p>
  
  <pre><code>TABLE: foo
  ===============================
  | id | first_name | last_name |
  ===============================
  | 1  | John       | Doe       |
  | 2  | Jane       | Smith     |
  | 3  | Ronald     | McDonald  |
  -------------------------------
  </code></pre>
  
  <p>How do I take this table and convert it to these tables (an EAV implementation)?:</p>
  
  <pre><code>TABLE: attribute
  ===========================
  | id | fk_id | attribute  |
  ===========================
  | 1  | 100   | first_name |
  | 2  | 100   | last_name  |
  ---------------------------
  
  TABLE: value
  =========================================
  | id | attribute_id | row_id | value    |
  =========================================
  | 1  | 1            | 1      | John     |
  | 2  | 2            | 1      | Doe      |
  | 3  | 1            | 2      | Jane     |
  | 4  | 2            | 2      | Smith    |
  | 5  | 1            | 3      | Ronald   |
  | 6  | 2            | 3      | McDonald |
  -----------------------------------------
  </code></pre>
  
  <p>NOTES:</p>
  
  <ul>
  <li>attribute.fk_id will be provided.</li>
  <li>value.row_id is used to identify how the values are grouped as records in the original table.</li>
  </ul>
  
  <p>UPDATE: Also, how do I query the EAV tables so that I can make it look like table <code>foo</code> again.</p>