Database design &引用;“出行偏好”;,数据库设计
我正在寻找建立一个数据库的商店之旅。行程具有Database design &引用;“出行偏好”;,数据库设计,database-design,Database Design,我正在寻找建立一个数据库的商店之旅。行程具有位置、模式和首选项。(它们都是实体或类)。现在我们假设每个行程只能有一个首选项,一个首选项可以是多个行程的一部分 现在,我以这种方式对其进行建模: Trips(id, attr1, attr2, ..., prefs); Preferences(id, pref1, pref2, pref3); 其中“prefs”是FK(而pref1、pref2和pref3是布尔类型) 好的,当我将一次行程(id=1)的pref1、pref2和pref3存储为tru
位置
、模式
和首选项
。(它们都是实体或类)。现在我们假设每个行程
只能有一个首选项
,一个首选项
可以是多个行程的一部分
现在,我以这种方式对其进行建模:
Trips(id, attr1, attr2, ..., prefs);
Preferences(id, pref1, pref2, pref3);
其中“prefs”是FK(而pref1、pref2和pref3是布尔类型)
好的,当我将一次行程(id=1)的pref1、pref2和pref3存储为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 |
+-------------+------+---------------+---------+
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.
)
问题是:不是有很多冗余?假设我存储了100个具有相同首选项值的行程,那么我将在首选项
表中有100行具有相同值
可能是一个与我的应用程序有关而与我的数据库设计无关的问题
谢谢
(对不起,我的基础英语很差)。看起来您的设计没有得到正确的规范化,因此可能效率低下,或者给您带来一些数据维护方面的麻烦 从你的问题中有点难分辨,但似乎
pref1
,pref2
和pref3
是不同的事物或状态,可能适用于或不适用于每次旅行。如果每次旅行都有或没有有限数量的东西,那么最好将这些东西(首选项)视为它们自己表中的行,并使用交集表来指示哪些东西适用于每次旅行
大概是这样的:
+------+----+-------+-----+---------------+
| Trip | id | attr1 | ... | prefs |
+------+----+-------+-----+---------------+
| | 1 | X | ... | 10 |
+------+----+-------+-----+---------------+
| | 2 | X | ... | 20 |
+------+----+-------+-----+---------------+
+-------------+------+-------+-------+---------+
| Preferences | id | pref1 | pref2 | pref3 |
+-------------+------+---------------+---------+
| | 10 | True | True | True |
+-------------+------+---------------+---------+
| | 20 | True | True | True |
+-------------+------+---------------+---------+
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.
)
这样的设计是一个更好的选择,因为它消除了冗余,而且如果您决定扩大适用于旅行的首选项数量,它在未来将更加灵活。谢谢!我认为你的设计很好。