【问题标题】:"Trip preferences", database design“旅行偏好”,数据库设计
【发布时间】:2011-12-20 21:34:34
【问题描述】:

我正在寻找为商店旅行创建一个数据库。这些行程有LocationModePreferences。 (它们都是实体或类)。现在假设每个Trip 只能有一个Preferences,而Preferences 可以是多个行程的一部分。

现在,我是这样建模的:

Trips(id, attr1, attr2, ..., prefs);
Preferences(id, pref1, pref2, pref3);

其中 'prefs' 是一个 FK(pref1、pref2 和 pref3 是布尔类型)。

好的,当我将 pref1、pref2 和 pref3 的行程(id = 1)存储为 true,以及另一个行程(id = 2)具有相同的首选项值时,我会得到这样的结果:

+------+----+-------+-----+---------------+
| Trip | id | attr1 | ... | prefs         |
+------+----+-------+-----+---------------+
|      | 1  |   X   | ... | 10            |
+------+----+-------+-----+---------------+
|      | 2  |   X   | ... | 20            |
+------+----+-------+-----+---------------+


+-------------+------+-------+-------+---------+
| Preferences | id   | pref1 | pref2 | pref3   |
+-------------+------+---------------+---------+
|             |  10  |  True | True  | True    |
+-------------+------+---------------+---------+
|             |  20  |  True | True  | True    |
+-------------+------+---------------+---------+

问题是:不是有很多冗余吗?假设我存储了 100 条具有相同偏好值的行程,那么我将在 Preferences 表中有 100 行具有相同的值。

也许是我的应用程序而不是我的数据库设计的问题?

谢谢。

(对不起我的基本英语)。

【问题讨论】:

    标签: database-design


    【解决方案1】:

    您的设计似乎没有正确规范化,因此可能效率低下或给您带来一些数据维护问题。

    从您的问题中很难判断,但似乎pref1pref2pref3 是可能适用于或不适用于每次旅行的不同事物或状态。如果每次旅行中存在或不存在的事物数量有限,那么您最好将事物(偏好)视为它们自己表的行,并使用交叉表来指示哪些事物适用于每次旅行。

    类似这样的:

    Trips
    ( id
    , attr1
    , attr2
    )
    
    Preferences
    ( id
    , description
    )
    
    Trip_Preferences
    ( trip_id
    , preference_id
    , value  -- If the value is true/false, then you have the option of leaving this out
             -- and just using the presence or absense of a record in this table as the
             -- indication of the value.
    )
    

    这样的设计是一个更好的选择,因为它消除了冗余,并且如果您决定扩大适用于旅行的偏好数量,它对未来更加灵活。

    【讨论】:

    • 谢谢!我觉得你的设计不错。