我需要为某个地方设计一个数据库。我有以下实体:'food' -> foodId, foodName 'consumers'-> c_Id,cBreakfast,cLunch,cDinner 消费者吃了三顿。早餐、午餐和晚餐,这些字段从带有其 ID 的“食物”实体中获取食物。但最终我会落入陷阱。我不能把它分成三餐早餐、午餐和晚餐呢。什么样的设计更适合这个问题?
问问题
191 次
1 回答
2
下面的模式可以让你跟踪谁吃了什么,吃了哪顿饭。在我的示例中,您可以看到 Bob 早餐吃煎蛋卷,午餐吃沙拉,晚餐吃牛排。玛丽错过了早餐,午餐吃了牛排,晚餐吃了沙拉。
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 |
| .. | ......... |
+----+-----------+
于 2013-10-23T15:26:01.927 回答