【问题标题】:How to design a set of database tables with dynamic attributes?如何设计一组具有动态属性的数据库表?
【发布时间】:2013-09-17 06:54:18
【问题描述】:

现在我遇到了一个问题:

对于同一个表(类别)中的某些项目,它们可能具有不同的属性。例如,有一个名为 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 数据库来解决我的问题吗?我不确定我的客户是否会接受,但我想介绍给他。

【问题讨论】:

  • 我建议您在education.mongodb.com 注册课程并了解 MongoDB。我警告你,MongoDB 中没有 SQL,但还有其他一些获取数据的可能性,比如聚合框架。另一方面,您可以在 MySQL 中使用 EAV 模式 (en.wikipedia.org/wiki/…) 并使用 SQL

标签: mysql sql mongodb cassandra database


【解决方案1】:

这看起来与我有关。

你会有这样的表格:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-22
    • 2017-03-29
    • 2011-02-12
    • 1970-01-01
    • 2010-11-29
    • 2011-01-18
    • 1970-01-01
    相关资源
    最近更新 更多