在食品数据库中存储成分的最有效方法

3 database-design

我有一个我称之为“食物”的数据库,但我对如何存储食谱的成分感到有些困惑。我应该为成分创建一个单独的表格,还是只是将成分作为逗号分隔的列表插入到配方表中?不同的食谱不会使用“相同的成分”,因为它们不会共享诸如洋葱之类的价值。如果 10 种不同的食谱有洋葱,那么洋葱将在数据库中出现 10 次(这很聪明吗?)。

我的数据库中的一些表

Isa*_*oho 7

我认为配料应该放在一个主表和另一个表中,以将配料映射到食谱。以下是基本思路:

Create table recipe
(
    recipe_id int not null,
    recipe_name varchar2(50),
    constraint pk_recipe primary key (recipe_id)
);
Create table ingredient
(
    ingredient_id int not null,
    ingredient_name varchar2(50),
    constraint pk_ingredient primary key (ingredient_id)
);
Create table food_ingredient
(
    fk_recipe int not null,
    fk_ingredient int not null,
    measure number,
    unit_of_measurement varchar2(10)
);
Run Code Online (Sandbox Code Playgroud)

请注意,由于语法原因,上面的脚本可能存在一些错误。

这种方法的优点:

  1. 寻找具有相似成分的食物或食谱
  2. 查找具有特定成分的食物或食谱
  3. 转换度量,那么你应该合并一些更多的表来存储度量单位和转换。你有这个想法

希望这会有所帮助。