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

我认为配料应该放在一个主表和另一个表中,以将配料映射到食谱。以下是基本思路:
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)
请注意,由于语法原因,上面的脚本可能存在一些错误。
这种方法的优点:
希望这会有所帮助。