如何设计这个 RDBMS 模式?

发布于 2024-09-07 11:05:54 字数 592 浏览 0 评论 0原文

因此,我们的食物通常是一个单元,而食谱则由不同数量的不同食物组成。

我们想要跟踪一些基本信息,例如价格和营养信息。

我们如何为此设计 RDBMS 模式?

所以,这可能是最明显的解决方案:

[Foods] : ID, Name, Carbs, Protein, Fat, Price
[RecipeIngredients] : ID, FoodID, RecipeID, AmountOfFood
[Recipes] : ID, Name

但是,由于食谱几乎具有与食物相同的操作,因此我需要创建大量重复的代码来支持两者。

这是另一种方法:

[Foods] : ID, Name, Carbs, Protein, Fat, Price, IsRecipe
[RecipeFoods] : ID, FoodID, RecipeID (which points to another Food), AmountOfFood

现在所有操作对两者都相同。但是,仅添加特定于食品或食谱的字段会为其他实体创建空列。

你会如何解决这个问题?你更喜欢哪一个?有更好的办法吗?

So we have foods, which are usually of one single unit and recipes, which consist of varying amount of different foods.

We want to track a few basic things like the price and the nutrition info.

How would we design RDBMS schema for this?

So, here is the probably most obvious solution:

[Foods] : ID, Name, Carbs, Protein, Fat, Price
[RecipeIngredients] : ID, FoodID, RecipeID, AmountOfFood
[Recipes] : ID, Name

However, since recipe has almost all the same actions as foods, I need to create a lot of duplicate code to support both.

Here is another approach:

[Foods] : ID, Name, Carbs, Protein, Fat, Price, IsRecipe
[RecipeFoods] : ID, FoodID, RecipeID (which points to another Food), AmountOfFood

Now all the actions work same for both. However, adding fields only specific to Food or Recipe creates empty columns for the other entity.

How would you solve this? Which one do you prefer? Is there a better way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夕嗳→ 2024-09-14 11:05:54

保持数据库规范化

正如您已经注意到的那样,Recipe 不是 Food,因此他们不应该共享一张桌子。

每个实体在数据库中都应该有自己的表。

Keep your database normalized.

A Recipe is not a Food as you had already noticed, so they shouldn't share a table.

Each entity should have its own table in the database.

筱果果 2024-09-14 11:05:54

如果您必须具有此数据库结构(+1 到 Oded),请确保未使用的列为 NULL 并且您的代码可以处理此问题。

If you must have this database structure (+1 to Oded), make sure the unused columns are NULL and that your code handles this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文