将权重存储在数据库表中

发布于 2024-10-14 06:15:39 字数 440 浏览 3 评论 0原文

我正在学习 MVC,想要创建一个菜谱记录器应用程序来存储我的菜谱。

我正在将 .net 与 Sql Server 2008 R2 一起使用,但我认为这对我想要做的事情并不重要。

我希望能够记录我使用的所有措施。在我的国家,我们使用公制,但我希望人们能够在我的应用程序中使用英制。

我如何构建我的表来应对差异,我正在考虑将所有测量值存储为整数,并使用外键来存储这种重量。

理想情况下,我希望能够在人们之间分享食谱并以他们喜欢的方式显示测量结果。

这是正确的方式

IngredientID PK
Weight int
TypeOfWeight int e.g. tsp=1,tbl=2,kilogram=3,pound=4,litre=5,ounce=6 etc
UserID int 

还是偏离了轨道?任何建议都会很棒!

I am playing around with learning MVC and want to create a recipe recorder application to store my recipes.

I am using .net with Sql Server 2008 R2 however I don't think that really matters with what I am trying to do.

I want to be able to record all of the measures I use. In my country we use metric however I want people to be able to use imperial with my application.

How do I structure my table to cope with the differences, I was thinking of storing all of the measurements as ints and have a foreign key to store the kind of weight.

Ideally I would like to be able to share the recipes between people and display the measurements in their preferred way.

Is this the right kind of way

IngredientID PK
Weight int
TypeOfWeight int e.g. tsp=1,tbl=2,kilogram=3,pound=4,litre=5,ounce=6 etc
UserID int 

Or is this way off track? Any suggestions would be great!

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

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

发布评论

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

评论(2

前事休说 2024-10-21 06:15:39

我认为您应该将重量(公斤/磅)等存储为单一重量类型(公制),并使用用户的偏好以正确的转换方式简单地“显示”它们。如果用户将重量设置设置为英制,则输入系统的值也需要进行转换。无论如何,这应该会简化您的数据。

与日期类似,您可以存储每个日期及其所在时区,或者以其他方式将所有日期存储为相同的(或没有时区),然后根据用户的偏好使用偏移量在应用程序中显示它们

I think you should store the weights (Kilo/Pound) etc as a single weight type (metric) and simply "display" them in the correct conversion using the user's preference. If the user has there weight settings set to Imperial, values entered into the system would need to be converted as well. This should simplify your data anyway.

Similar to Dates, you could store every date and what timezone it is from, or otherwise store all dates as the same (or no timezone) and then display them in the application using offsets according to the user's preference

等风也等你 2024-10-21 06:15:39

如果您要存储权重(非离散值),我强烈建议使用数字或小数来存储该数据。您对 typeofweight 列的想法是正确的。在某处存储一个参考表,显示每个值的转换率(按照特定标准)。

当您想要将盎司显示为 TSP 时,这会变得非常棘手,因为转换取决于成分本身,因此您需要第三个表 - 成分:id、名称、体积重量比。

typeofweight 表示例,其中标准 单位为克

type   | conversion
gram   | 1
ounce  | 28.35
kg     | 1000
tsp    | 5            // assuming that 1 tsp = 5 grams of water
pound  | 453.59

成分体积到重量转换示例

type   | vol-to-weight
water  | 1
sugar  | 1.4          // i.e. 1 tsp holds 5g of water, but 7g of sugar

因此,要显示 tsp 中的 500 盎司糖,您可以使用公式

  units x ounce.conversion x sugar.vol-to-weight
= 500   x 28.35            x 1.4

另一个具有 2 个重量的示例

Ingredient is specified as 3 ounces of starch.  Show in grams
= 3 x 28.35    (straightforward isn't it)

或者

Ingredient is specified as 3 ounces of starch.  Show in pounds
= 3 * 28.35 / 453.59

If you are storing weights (a non-discrete value) I would strongly suggest using numeric or decimal for this data. You have the right idea with the typeofweight column. Store a reference table somewhere showing what the conversion ratio is for each (to a certain standard).

This gets quite tricky when you want to show ounces as TSP, because the conversion depends on the ingredient itself, so you need a 3rd table - ingredient: id, name, volume-to-weight ratio.

Example typeofweight table, where the standard unit is grams

type   | conversion
gram   | 1
ounce  | 28.35
kg     | 1000
tsp    | 5            // assuming that 1 tsp = 5 grams of water
pound  | 453.59

Example ingredient volume to weight conversion

type   | vol-to-weight
water  | 1
sugar  | 1.4          // i.e. 1 tsp holds 5g of water, but 7g of sugar

So to display 500 ounces of sugar in tsp, you would use the formula

  units x ounce.conversion x sugar.vol-to-weight
= 500   x 28.35            x 1.4

Another example with 2 weights

Ingredient is specified as 3 ounces of starch.  Show in grams
= 3 x 28.35    (straightforward isn't it)

or

Ingredient is specified as 3 ounces of starch.  Show in pounds
= 3 * 28.35 / 453.59
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文