可以“挂钩”到多对多关系表吗?

时间:2020-11-30 18:56:57

I got 3 tables: Recipe R, Ingredient I and IngredientAmount IA

我有3个表:配方R,成分I和IngredientAmount IA

R and I got a many-to-many relation connection table.

R和我有一个多对多关系连接表。

Now, i need to chain IA to I, so that i know that fx a recipe needs potato (I) and the amount is 2 kg (IA)

现在,我需要将IA链接到I,这样我知道fx配方需要马铃薯(I),量为2 kg(IA)

Should i make a FK to I from IA or would it be okay to hook the IA FK into the many-to-many relation table between I and R?

我应该从IA获得FK,还是可以将IA FK挂钩到I和R之间的多对多关系表?

Or is there a better way to do this?

或者有更好的方法吗?

Thanks in advance

提前致谢

1 个解决方案

#1


3  

I think it gets much easier if you do it the other way around

我认为如果你以相反的方式做到这一点会变得容易得多

I would read it as this:

我会这样读:

A recipe has a set of ingredient amounts
an ingredient amount has one ingredient

So that would mean you have a recipe table like

所以这意味着你有一个食谱表

RecipeId Title

And an ingredient amount table that references a recipe as well as an ingredient via foreign keys for example

还有一个配料量表,例如通过外键引用配方和配料

IngredientAmountId RecipeId IngredientId Amount

And the ingredient amounts finally reference the ingredient itself

成分量最终参考成分本身

IngredientId Title

So only a few one to many relationships (one recipe has many ingredient amounts, one ingredient can belong to many ingredient amounts) needed no many to many relationship imho.

因此,只有少数一对多的关系(一个配方有很多成分量,一个成分可以属于许多成分量)需要没有多对多的关系imho。

TL;DR

In your question you stated that you already "connected" recipes with ingredients, in my opinion this is flawed; there should really be a connection between recipe and ingredientamounts and another connection between ingredientamounts and ingredients.

在你的问题中,你说你已经将食谱与食谱“联系起来”,在我看来这是有缺陷的;配方和配料之间应该有一个联系,而配料和配料之间应该有另一种联系。

#1


3  

I think it gets much easier if you do it the other way around

我认为如果你以相反的方式做到这一点会变得容易得多

I would read it as this:

我会这样读:

A recipe has a set of ingredient amounts
an ingredient amount has one ingredient

So that would mean you have a recipe table like

所以这意味着你有一个食谱表

RecipeId Title

And an ingredient amount table that references a recipe as well as an ingredient via foreign keys for example

还有一个配料量表,例如通过外键引用配方和配料

IngredientAmountId RecipeId IngredientId Amount

And the ingredient amounts finally reference the ingredient itself

成分量最终参考成分本身

IngredientId Title

So only a few one to many relationships (one recipe has many ingredient amounts, one ingredient can belong to many ingredient amounts) needed no many to many relationship imho.

因此,只有少数一对多的关系(一个配方有很多成分量,一个成分可以属于许多成分量)需要没有多对多的关系imho。

TL;DR

In your question you stated that you already "connected" recipes with ingredients, in my opinion this is flawed; there should really be a connection between recipe and ingredientamounts and another connection between ingredientamounts and ingredients.

在你的问题中,你说你已经将食谱与食谱“联系起来”,在我看来这是有缺陷的;配方和配料之间应该有一个联系,而配料和配料之间应该有另一种联系。