现在我遇到了一个问题:
对于同一表(类别)中的某些项目,它们可能具有不同的属性。例如,有一个名为 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},
...]
属性中的列数不固定。
我有点困惑,因为这与我之前设计的数据库完全不同。所以想请教各位,如何设计一套表来存储上述格式的数据。我要使用的数据库是MySQL。所以我想要一个MySQL环境下的解决方案。
我还听说我可以通过使用像 MongoDB 或 Cassandra 这样的 NoSQL 数据库来解决这个问题。你能告诉我如何使用这样的 NoSQL 数据库来解决我的问题吗?我不确定我的客户是否会接受,但我想介绍给他。
最佳答案
这看起来与我有关。
你会有这样的表格:
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
可能需要更改列,具体取决于值是仅取决于属性,还是还取决于产品。
关于mysql - 如何设计一组具有动态属性的数据库表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18771853/