在数据库中存储枚举值
仅供参考:我明确指的是 SQL Server 2000-8 和 C#。因此,像 MySql 这样具有枚举支持的 DBMS 不是我的问题的主题。
我知道这个问题已在 SO 中被问过多次。但是,我仍然在答案中看到采用不同的方法将枚举值存储在数据库中。
在数据库中将枚举保存为 int 并在代码中提取枚举值(或使用反射的枚举描述属性):
这是我通常使用的方法。问题是当我尝试从 SSMS 中的数据库查询时,检索到的数据很难理解。将枚举保存为数据库中的字符串(varchar)并在代码中转换回 int。
事实上,这可能是最好的解决方案。但是(别笑!)感觉不对。我不确定缺点。 (除了数据库中的更多空间,这通常是可以接受的)那么还有什么反对这种方法的吗?在数据库中有一个单独的表,该表与代码的枚举定义同步,并在主表和枚举表之间建立外键关系。
问题是当稍后添加另一个枚举值时,代码和数据库都需要更新。另外,可能会有拼写错误,这可能会很痛苦!
因此,一般来说,当我们可以接受第二个解决方案中数据库的开销时,在数据库中存储枚举值的最佳方法是什么?是否有关于此的一般明确的设计模式规则?
谢谢。
FYI: I explicitly mean SQL Server 2000-8 and C#. So DBMSs with enum support like MySql is not the subject of my question.
I know this question has been asked multiple times in SO. But still, I see in answers that different approaches are taken to store enum values in db.
Save enum as int in db and extract the enum value (or enum description attribute using reflection) in code:
this is the approach I usually use. The problem is when I try to query from database in SSMS, the retrieved data is hard to understand.Save enum as string (varchar) in db and cast back to int in code.
Actually, this might the best solution. But (don't laugh!) it doesn't feel right. I'm not sure about the cons. (Except more space in db which is usually acceptable) So anything else against this approach?Have a separate table in db which is synchronized with code's enum definition and make a foreign key relationship between your main table and the enum table.
The problem is when another enum value should be added later, Both code and db need to get updated. Also, there might be typos which can be a pain!
So in general when we can accept the overhead on db in 2nd solution, What would be the best way to store enum values in db? Is there a general definite design pattern rule about this?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
没有明确的设计规则(据我所知),但我更喜欢方法#1。
编辑:
克里斯在评论中提出了一个很好的观点:如果您确实采用数字方法,则应该显式分配值,以便也可以对它们重新排序。例如:
There is no definite design rule (that I know of), but I prefer approach #1.
EDIT:
Chris in the comments had a good point: If you do go down the numeric approach, you should explicitly assign values so you can re-order them as well. For example:
我之前见过的一个想法是你的选项3或多或少
数据库表表可以有触发器或检查约束以降低更改风险。它不应该有任何写入权限,因为数据与客户端代码版本相关联,但它增加了一个安全因素,以防 DBA 崩溃。
如果您有其他客户端读取代码(这很常见),那么数据库已完成数据。
One idea I've seen before which is your option 3 more or less
The database table table can have a trigger or check constraint to reduce risk of changes. It shouldn't have any write permissions because the data is tied to a client code release, but it adds a safety factor in case the DBA bollixes up
If you have other clients reading the code (which is very common) then the database has complete data.
我参考了这里的答案以及这个 问题。我想总结一下这些答案并添加我的观点。所以,这就是我的想法,
选项 1:将 Enum 值存储在查找表中,但不在代码中声明 Enum
选项 2:将 Enum 值存储在查找表中,并在代码中声明 Enum
选项 3:在代码中声明 Enum,但不为其创建查找表
I referred to the answers here and on this question. I would like to summarize these answers and also add my perspective. So, here's what I think,
Option 1: Store the Enum values in a lookup table, but don't declare Enum in code
Option 2: Store the Enum values in a lookup table and also declare Enum in code
Option 3: Declare the Enum in code, but don't create a lookup table for it