如何在数据库中最好地表示具有变量属性的项目? [英] How to best represent items with variable # of attributes in a database?

查看:132
本文介绍了如何在数据库中最好地表示具有变量属性的项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设您要创建窗口小部件列表

Lets say you want to create a listing of widgets

窗口小部件制造商都创建具有不同数量和类型的属性的窗口小部件。并且Widget卖家对于他们想要存储在数据库和显示中的属性的类型和数量有不同的偏好。

The Widget Manufacturers all create widgets with different number and types of attributes. And the Widget sellers all have different preferences on what type and number of attributes they want to store in the database and display.

现在的问题是,每次添加在新的窗口小部件中,它可能具有对于任何其他窗口小部件当前不存在的属性,并且当前通过修改表并且为该属性添加新的列,然后修改所有窗体和报告以反映该改变来实现这一点。

The problem here now is that each time you add in a new widget, it may have attributes on it that donot currently exist for any other widget, and currently you accomplish this by modifying the table and adding in a new column for that attribute and then modifying all forms and reports to reflect this change.

如何创建一个数据库,它考虑到了一个小部件上的属性是流动的,并且可以从小部件改变为小部件。

How do you go about creating a database which takes into account that attributes on a widget are fluid and can change from widget to widget.

理想情况下,widget属性应该是用户根据自己的偏好和需要定义的属性。

Ideally the widget attributes should be something the user can define according to his/her preference and needs

推荐答案

我会有一个表的窗口小部件和一个窗口小部件的属性。例如:

I would have a table for widgets and one for widget attributes. For example:

Widgets
   - Id
   - Name

WidgetAttributes
   - Id
   - Name

然后,窗口小部件具有以下属性:

Then, you would have another table which has what widgets have which attributes:

WidgetAttributeMap
  - Id
  - WidgetId
      (a value from the Id column in the Widget table)
  - WidgetAttributeId
      (a value from the Id column in the WidgetAttribute table)

这样,您可以通过修改WidgetAttributeMap表中的行,而不是修改窗口小部件表的结构,来向窗口小部件添加属性。

This way, you can add attributes to widgets by modifying rows in the WidgetAttributeMap table, not by modifying the structure of your widget table.

这篇关于如何在数据库中最好地表示具有变量属性的项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆