1

现在我遇到一个问题:

对于同一个表(类别)中的某些项目,它们可能具有不同的属性。例如,有一个名为 Product 的表,其中有两个名为 A 和 B 的项目。

A拥有的属性是id, name, attr01, attr03, attr04

B拥有的属性是id, name, attr02, attr03, attr05, attr06

可能还有更多这样的项目。

而对于每个 attrXX 属性,其格式如下:

    [attrbute_name, 
    {column_id: 01, column_name: xxx, relationship: xxx, value: xxx, unit: xxx},
    {column_id: 02, column_name: xxx, relationship: xxx, value: xxx, unit: xxx},
    {column_id: 03, column_name: xxx, relationship: xxx, value: xxx, unit: xxx},
    ...]

attr 中的列数不固定。

我有点困惑,因为这与我之前设计的数据库完全不同。所以想请教大家如何设计一组表来存储上述格式的数据。我要使用的数据库是 MySQL。所以我想要一个在 MySQL 环境中的解决方案。

另外我听说我可以通过使用 MongoDB 或 Cassandra 等 NoSQL 数据库来解决这个问题。你能告诉我如何使用这样的 NoSQL 数据库来解决我的问题吗?我不确定我的客户会接受它,但我想把它介绍给他。

4

1 回答 1

0

这看起来与我有关。

你会有这样的表:

Product (id, name)
Attribute (id, name)
ProductHasAttribute (ProductId, AttributeId)
Column (id, AttributeId, name, relationship, value, unit)

ProductHasAttribute 的内容是:

A, 1
A, 3
A, 4
B, 2
B, 3
B, 5
B, 6

获取产品 P 的所有属性:

select A.name
from ProductHasAttribute PA
inner join Attribute A on A.id = PA.AttributeID
where PA.ProductID = P

可能需要更改列,具体取决于值是仅取决于属性,还是还取决于产品。

于 2013-09-13T17:28:43.947 回答