Nat*_*coe 1 sql database-schema
我正在为我正在使用的食谱网站构建 SQL 数据库。我是多对多风格的新手,对我目前拥有的东西没有信心。不可避免地,我希望最终结果是建立一个可靠的设置,用户可以:
将配方添加到数据库
按准备时间、烹饪时间、菜肴、类别、食物类别和课程排序
通过添加所选食谱中的成分总量,能够从每周膳食计划中生成购物清单。(这是最重要的!)
我将挑战自己找出如何添加食谱中的成分以生成购物清单,但这篇文章的问题是“我应该如何构建我的表格并连接所有内容?”
这是我迄今为止在我的 SQL 数据库中所拥有的:
如果您认为自己有答案,请构建与我的表格类似的表格。我是一个视觉学习者,将不胜感激。此外,如果您能解释我可能遗漏或可能不会遗漏的内容,将不胜感激。
- Recipe(table)
- recipe_id (primary key)
- recipe_name
- recipe_description
- course
- food_category
- cuisine
- prep_time
- cook_time
- Ingredients(table)
- ingredient_id (primary key)
- ingredient_name
- recipe_id (foreign key)
- Quantity(table)
- quantity_id (primary key)
- recipe_id (foreign key)
- ingredient_id (foreign key)
- ingredient_quantity
- ingredient_measurement
- Recipe Steps(table)
- step_id (primary key)
- step_description
- recipe_id (foreign key)
- Join(table)*
- join_id (primary key)
- recipes_id(foreign key)
- ingredients_id(foreign key)
- quantity
Run Code Online (Sandbox Code Playgroud)
// 我不知道我是否需要这个 'join' 表,因为 'quantity' 似乎很相似
先感谢您!
ctw*_*els 10
因为我不是 100% 确定你想要什么。我假设您在问什么可能是您项目的最佳数据库和表结构。由于我不完全确定项目的范围需要什么,我只能根据字段名称的假设和项目结构的假设提供以下内容。
Cook_time时间
食品分类(表格)
food_category_id int primary_key auto_increment
food_category_name varchar
课程(表)
course_id int primary_key auto_increment
其他课程属性,例如一天中的时间?
配料(表)
成分id int primary_key auto_increment
其他成分特性可能吗?
数量(台)
quantity_id int primary_key auto_increment
成分_数量浮点
测量(表)
measurement_id INT primary_key AUTO_INCREMENT
测量名称varchar
配方步骤(表)
step_id int primary_key auto_increment
Join 表是无用的,因为您将加入SELECT查询中的表,即以下查询
SELECT
r.recipe_id AS recipeID,
r.recipe_name AS recipeName,
r.recipe_description AS recipeDescription,
r.cuisine AS cuisine,
r.prep_time AS prepTime,
r.cook_time AS cookTime,
c.course_name AS course,
f.food_category_name AS foodCategory
FROM Recipe AS r
JOIN course AS c ON c.course_id = r.course_id
JOIN food_category AS fc ON fc.food_category_id = r.food_category_id
WHERE r.recipe_name LIKE '%chocolate%'
AND c.course_id = 1 -- i.e. breakfast as course_name
ORDER BY r.prep_time -- for example
Run Code Online (Sandbox Code Playgroud)
假设用户已123在名称中显示为“巧克力”的食谱列表中选择了一个食谱(带有 ID = )
列出成分(使用用户选择的配方中的配方 ID)
SELECT
i.ingredient_name AS ingredientName,
q.ingredient_quantity AS ingredientQuantity,
m.measurement_name AS measurementName
FROM ingredients AS i
JOIN quantity AS q ON q.ingredient_id = i.ingredient_id
JOIN measurements AS m ON m.measurement_id = q.ingredient_measurement_id
JOIN recipe AS r ON r.recipe_id = q.recipe_id
WHERE r.recipe_id = 123
ORDER BY i.ingredient_name ASC
Run Code Online (Sandbox Code Playgroud)
列出步骤(使用用户选择的配方中的配方 ID)
SELECT
s.step_number AS stepNo,
s.step_description AS stepDescription
FROM steps AS s
JOIN recipe AS r ON r.recipe_id = s.recipe_id
WHERE r.recipe_id = 123
ORDER BY s.step_number ASC
Run Code Online (Sandbox Code Playgroud)