数据库设计中的陷阱

时间:2013-03-10 13:58:33

标签: database database-design

我需要为某个地方设计一个数据库。我有以下实体: 'food' - > foodId,foodName '消费者' - > C_ID,cBreakfast,cLunch,cDinner 消费者正在吃三次。早餐,午餐和晚餐,这些领域的食物来自“食物”实体的ID。 但最终我陷入了陷阱。我不能把它分成三餐早餐,午餐和晚餐。 什么设计更适合这个问题?

1 个答案:

答案 0 :(得分:2)

以下架构可让您跟踪谁吃了什么以及吃了哪一餐。在我的例子中,你可以看到鲍勃有一个早餐煎蛋卷,午餐沙拉和晚餐牛排。玛丽错过了早餐,午餐吃牛排,晚餐吃沙拉。

consumers
    id              unsigned int(P)
    name            varchar(30)

+----+------+
| id | name |
+----+------+
|  1 | Bob  |
|  2 | Mary |
| .. | .... |
+----+------+

consumptions
    id              unsigned int(P)
    consumer_id     unsigned int(F consumers.id)
    mealtime_id     unsigned int(F mealtimes.id)
    food_id         unsigned int(F foods.id)

+----+-------------+-------------+---------+
| id | consumer_id | mealtime_id | food_id |
+----+-------------+-------------+---------+
|  1 |           1 |           1 |       1 |
|  2 |           1 |           2 |       2 |
|  3 |           1 |           3 |       3 |
|  4 |           2 |           2 |       3 |
|  5 |           2 |           3 |       2 |
| .. | ........... | ........... | ....... |
+----+-------------+-------------+---------+

foods
    id              unsigned int(P)
    name            varchar(20)

+----+--------+
| id | name   |
+----+--------+
|  1 | Omelet |
|  2 | Salad  |
|  3 | Steak  |
| .. | ...... |
+----+--------+

mealtimes
    id              unsigned int(P)
    name            varchar(10)

+----+-----------+
| id | name      |
+----+-----------+
|  1 | Breakfast |
|  2 | Lunch     |
|  3 | Dinner    |
| .. | ......... |
+----+-----------+