Mysql 数据建模/规范化问题

Mysql 数据建模/规范化问题,mysql,database,database-design,database-schema,database-normalization,Mysql,Database,Database Design,Database Schema,Database Normalization,我正在编写一个厨房食谱脚本,在访问联接表中的字段时遇到问题。我的表格设置如下: ingredients ----------- ingredient_id (pk) ingredient_name (pk) ingredient_calories (pk) recipes ------- recipe_id (pk) recipe_name (pk) meals ----- meal_id (pk) meal_name (pk) recipe_has_ingredients -------

我正在编写一个厨房食谱脚本,在访问联接表中的字段时遇到问题。我的表格设置如下:

ingredients
-----------
ingredient_id (pk)
ingredient_name (pk)
ingredient_calories (pk)

recipes
-------
recipe_id (pk)
recipe_name (pk)

meals
-----
meal_id (pk)
meal_name (pk)

recipe_has_ingredients
----------------------
recipe_id (fk)
ingredient_id (fk)

meal_has_recipes
----------------
meal_id (fk)
recipe_id (fk)
食谱包含许多配料,而膳食包含许多食谱

问题是:

虽然我可以从配方中选择总热量(配料热量),但我无法通过膳食配方表(计算膳食中的总热量)


非常感谢您的帮助:)

您对事实表配料、膳食和食谱上的主键进行了过度约束。只需使用id字段作为pk

可以在任何名称字段上创建唯一约束

recipe has Component表应将recipe_id和Components_id作为复合主键

“膳食有食谱”表应将“膳食id”和“食谱id”作为复合主键

现在可以查询一餐的卡路里了

 select meal_name, sum ( ingredients_calories)   from

    meals, meal_has_recipes, recipes_has_ingredients, recipes, ingredients

   where

      meal_name = ' good   food'   

      and   meal_has_recipes.meal_id =  meals.meal_id 

      and   meals_has_recipes.recipe_id = recipes.recipe_id

      and   recipes.recipe_id = recipes_has_ingredients.recipe_id

      and    ingredients.ingredients_id = recipes_has_ingredients.ingredients_id;