Home  >  Q&A  >  body text

mysql - 问一个简单的数据表设计问题(多对一、一对多)?

举个栗子
现在有三张表:食材、灶具、食品

业务场景
选购某些食材,通过某种灶具,做成了一些食品。(取葱花、面粉->用平底锅->烙了白面饼和葱花饼)

用数据库关系描述
用数据库正常描述是这样的:食材(n)->食品(1),食品(n)->灶具(1),看起来很简单,多对一、多对一。但是注意业务场景,是先有食材,再将食材放入灶具,然后烹制出多个食品。也就是说不会事先知道食材(n)->食品(1)这个关系

我的使用场景
在食品烹制过程中,会根据食材查询灶具,会根据灶具查询食品;在食品烹制结束,会根据食材查食品,也会根据食品查食材。也许大家也想到了,还存在一层关系食材(n)->灶具(1),那就有了我下面的问题

问题
我怎么建立这三张表的关系?是否将三张表都建立关联关系,如下面所示:

食材表(例子有点不合适,把每个食材看做一份,用完就没有了):

id name goods_id pan_id
1 面粉 1 1
2 葱花 1 1
3 花生油 2 3

灶具表:

id name
1 平底锅
2 蒸笼
3 炒锅

食品表:

id name pan_id
1 葱花饼 1
2 油条 3

第一次在这里提问,望大神指导,谢谢!

大家讲道理大家讲道理2728 days ago659

reply all(2)I'll reply

  • PHP中文网

    PHP中文网2017-04-17 16:08:15

    Food(n)->Stove(1)

    I don’t quite understand this. Can one stove cook a variety of foods?

    In addition, will the food require multiple stoves to process?

    By my understanding

    Ingredients list

    Field
    sc_id
    name

    Stove table

    Field
    zj_id
    name

    Food List

    Field
    sp_id
    name

    Recipe sheet

    The first step of
    Field Explanation
    id Primary key
    sp_id Make this food
    step
    sc_id You need to use the sc_id material
    zj_id Cooking in zj_id

    reply
    0
  • 怪我咯

    怪我咯2017-04-17 16:08:15

    ps: The design of the data table is one entity and one table, and then use the association table to establish a direct connection between the entities! Once you understand this concept, see if your current problem is simpler! There is a table for kitchen utensils, a table for ingredients, a table for food, and then the recipe table is a relationship table that establishes three entities! Typing on the phone is so tiring...

    reply
    0
  • Cancelreply