简体   繁体   English

在数据库中存储无限/最大值的最佳实践

[英]best practice to store Unlimited/Max value in DB

I have a few scenarios where I need to store an unlimited value (or maximum, whatever you like to call it), which represents no limitation in business. 在某些情况下,我需要存储一个无限的值(或最大值,无论您喜欢用什么名称),这对业务没有限制。

A few options I considered: 我考虑了一些选择:

  • Make the field Nullable, and Use DB NULL to represent such case. 使字段为Nullable,并使用DB NULL表示这种情况。 but the problem is I have to check it anywhere I need to do a comparison or display it. 但是问题是我必须在需要比较或显示的地方进行检查。

  • Use actual Maximum value of the given type (for example, integer, i can use the largest Int32 value), but this need some tweaks at DB level - I have to write a constraint at the field (as I could use fixed length of decimal or Integer DB type) to limit the maximum value, and it could have no meaning to business either. 使用给定类型的实际最大值(例如,整数,我可以使用最大的Int32值),但是这需要在数据库级别进行一些调整-我必须在该字段上写一个约束(因为我可以使用固定的十进制长度)或Integer DB类型)来限制最大值,这对业务也没有意义。

  • Use a predefined big value (that might make sense to the business) to represent it and store it at DB level, again, i have to write a constraint to the db field. 使用预定义的大值(可能对业务有意义)来表示它并将其存储在数据库级别,同样,我必须将约束写入db字段。

I have used all of them before for different scenarios, and all are not too bad, but you know, it's a pain to handle some specific cases. 我以前在不同的情况下都使用过它们,并且它们都还不错,但是要知道,处理一些特定的案例是很痛苦的。

My question is a bit broad: what do you guys suggest for this? 我的问题有点宽泛:你们对此有何建议? what good/best practices are available? 有哪些良好/最佳实践?

Any help/suggestions are appreciated. 任何帮助/建议表示赞赏。

I would think that storing it as a separate column, IsXyzUnlimited , may be a good alternate practice. 我认为将其存储为单独的列IsXyzUnlimited可能是一个很好的替代做法。

Since it doesn't mean null, it may not be best to represent it as null. 由于它并不表示null,所以最好将其表示为null。 As you mentioned, there is also the problem of checking it before you invoke it. 如您所述,在调用它之前还存在检查它的问题。

Also, as you mentioned, the other 2 values could have business meaning. 而且,正如您提到的,其他两个值可能具有商业意义。 If you want the data to be self-revealing about the business, explicitly say "hey business, this thing is unlimited when this box is checked". 如果您希望数据能够自动显示有关业务的信息,请明确地说“嘿,业务,当选中此框时,这是无限的”。 No magic values. 没有神奇的价值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM