我有以下几张表。
具有以下属性的Product:
product
价格的<<名称
食谱具有以下属性:
Recipe
配方中。
具有以下属性的Ingredients:
上述表格的Sampel数据库:
产品:
| ProductID | ProductName | ProductPrice |
12 Bun 1.5
15 Ham Burger 5
13 Chicken Burger 7
成分:
| ingredientID | ingredientName | ingredientUnitType |
1 Salt gm
2 Yeast gm
3 Refined Wheat Flour gm
4 Milk ml
5 Chicken Meat gm
6 Onion gm
7 Tomatoes gm
配方:
| RecipeID | ProductID | ingredientID | productAsIngredientID | ingredientAmount |
1 13 12 1
2 13 5 20
3 13 6 7
4 13 7 10
5 12 1 5
6 12 2 2
7 12 3 10
8 . .
9 . .
在菜谱表中,ProductID不能等于同一行中的productAsIngredientID。但在表配方中,productID和productAsIngredientID都链接到Product.ProductID。
但是,将上述两个表与productID连接存在问题。如果我链接它们,它们中的任何一个都不能具有空值或任何不在成分或Product中的值。
我正在使用Microsoft (MDB)作为数据库,请建议我一个正确的方法来完成这个任务。如何将产品项目本身组织为一种成分本身。
发布于 2012-01-24 01:37:14
您要求的是如何在表之间创建多到多的关系。
这是通过一个单独的“链接表”来完成的,该表包含来自另外两个表的Id (在您的例子中,我们将向配料链接表中添加另一列,金额)。
因此,模式将如下所示(我更喜欢不以表名作为前缀的属性):
产品
成分
配方
RecipeProducts
Recipe.Id)
)
RecipeIngredients
Recipe.Id)
)
这就是我通常布局模式的方式,但我对Access并不那么熟悉。
这就是我从访问查询向导中获得的菜谱的所有成分。
SELECT Ingredient.IngredientName, RecipeIngredients.Amount, Ingredient.Unit
FROM Ingredient
INNER JOIN RecipeIngredients
ON Ingredient.[ID] = RecipeIngredients.[IngredientId]
WHERE RecipeIngredients.RecipeId = 1;
不是最好的SQL,我想有人可以提供一种更聪明的方法来获得配方的产品和成分。
productAsIngredientId不应该是必要的,因为当一个产品是一个食谱的一部分,它将是一个成分,对吗?否则,您需要将您的表名更改为更符合逻辑的名称。
https://stackoverflow.com/questions/8984300
复制