如何设计这个RDBMS架构? [英] How to design this RDBMS schema?

查看:155
本文介绍了如何设计这个RDBMS架构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我们有食物,通常是一个单一的单位和食谱,由不同数量的不同食物组成。

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.

我们将如何设计RDBMS模式?

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?

推荐答案

保留数据库归一化

A 食谱不是一个食物已经注意到,所以他们不应该共享一个表。

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.

这篇关于如何设计这个RDBMS架构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆