问题描述
我出于好奇地问这个问题。基本上我的问题是,当你有一个数据库,需要一个行条目,以具有像标志的东西,最好的做法是什么?一个很好的例子是堆栈溢出的标志,或者bugzilla中的操作系统字段。可以为给定的条目设置任何子集的标志。
通常,我做c和c ++工作,所以我的反应是使用无符号整数字段作为可以翻转的位集合...但我知道这不是一个很好的解决方案有几个原因。其中最明显的就是扩展能力,我可以有多少标志的上限。
我也可以想到几个其他解决方案因为它们需要多个选择来获得所有的信息。
那么,正确的方法是什么? p>
如果你真的需要一个关闭的标志集合的无界选择(例如stackoverflow徽章),那么关系方式创建一个标志表和一个单独的表,将这些标志与您的目标实体相关联。因此,用户,标志和usersToFlags。
但是,如果空间效率是一个严重的问题,而查询能力不是,一个无符号掩码几乎一样。 p>
I am asking this out of a curiosity. Basically my question is when you have a database which needs a row entry to have things which act like flags, what is the best practice? A good example of this would be the badges on stack overflow, or the operating system field in bugzilla. Any subset of the flags may be set for a given entry.
Usually, I do c and c++ work, so my gut reaction is to use an unsigned integer field as a set of bits which can be flipped... But i know that isn't a good solution for several reasons. The most obvious of which is scale-ability, there will be a hard upper limit on how many flags I can have.
I can also think of a couple of other solutions which scale better but would have performance issues because they would require multiple selects to get all the information.
So, what is the "right" way to do this?
If you really need an unbounded selection from a closed set of flags (e.g. stackoverflow badges), then the "relational way" would be to create a table of flags and a separate table which relates those flags to your target entities. Thus, users, flags and usersToFlags.
However, if space efficiency is a serious concern and query-ability is not, an unsigned mask would work almost as well.
这篇关于标志在数据库中的行,最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!