开发者

MySQL: how to convert to EAV - Part 3?

Previous Related Posts:

  • MySQL: how to convert to EAV?
  • MySQL: how to convert to EAV - Part 2?

Given a table:

TABLE: foo
===============================
| id | first_name | last_name |
===============================
| 1  | John       | Doe       |
| 2  | Jane       | Smith     |
| 3  | Ronald     | McDonald  |
------开发者_Python百科-------------------------

How do I take this table and convert it to these tables (an EAV implementation)?:

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 |
-----------------------------------------

NOTES:

  • attribute.fk_id will be provided.
  • value.row_id is used to identify how the values are grouped as records in the original table.

UPDATE: Also, how do I query the EAV tables so that I can make it look like table foo again.


I give +1 to @Phil's solution for populating the EAV table. Insert one attribute at a time.

Here's another solution to reverse an EAV transformation:

SELECT v.row_id AS id,
  MAX(IF(a.attribute='first_name',v.value,NULL)) AS first_name,
  MAX(IF(a.attribute='last_name',v.value,NULL)) AS last_name
FROM value v INNER JOIN attribute a
  ON v.attribute_id = a.id 
GROUP BY v.row_id

Except that by using EAV, you've put all your values into a column of VARCHAR(255) or whatever, so you have lost information about the respective data types of the original columns.

There's really no way to do it "dynamically" without hard-coding the attribute names, either as joins as @Phil shows, or as columns as I show. It's essentially the same problem as trying to write dynamic pivot queries.

I have written more about EAV in my presentation Practical Object-Oriented Models in SQL and in my book, SQL Antipatterns Volume 1: Avoiding the Pitfalls of Database Programming.


I think your only hope is if you use the foo table. bar is essentially useless without the ID.

Try something like this (assuming attribute.id is an auto-increment primary key)

INSERT INTO `attribute` (`fk_id`, `attribute`)
VALUES (100, 'first_name');

INSERT INTO `value` (`attribute_id`, `row_id`, `value`)
SELECT LAST_INSERT_ID(), `id`, `first_name`
FROM `foo`;

INSERT INTO `attribute` (`fk_id`, `attribute`)
VALUES (100, 'last_name');

INSERT INTO `value` (`attribute_id`, `row_id`, `value`)
SELECT LAST_INSERT_ID(), `id`, `last_name`
FROM `foo`;

To reconstruct the foo table, try this

SELECT
    `fn`.`row_id` AS `id`,
    `fn`.`value` AS `first_name`,
    `ln`.`value` AS `last_name`
FROM `value` `fn`
INNER JOIN `attribute` `fn_a`
    ON `fn`.`attribute_id` = `fn_a`.`id`
    AND `fn_a`.`attribute` = 'first_name'
INNER JOIN `value` `ln`
    ON `fn`.`row_id` = `ln`.`row_id`
INNER JOIN `attribute` `ln_a`
    ON `ln`.`attribute_id` = `ln_a`.`id`
    AND `ln_a`.`attribute` = 'last_name'

Ergh, thanks for reminding me why I hate this pattern

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜