在数据库中存储枚举常量[duplicate]

时间:2022-06-17 17:00:45

Possible Duplicates:
Should I store Enum ID/values in the db or a C# enumeration?
Persisting Enums in database tables

可能的重复:我是否应该在db或c#枚举中存储Enum ID/值?在数据库表中持久化枚举

Quick and easy question which will probably be fodder for discussion:

快速而简单的问题,可能会成为讨论的素材:

Let's say my application has an enum with 2-3 values which are basically never going to change.

假设我的应用程序有一个包含2-3个值的enum,基本上不会改变。

Do you think those should be represented in the database in their own table and then referenced from other tables via foreign key relationships? Or should they be stored in the appropriate tables as simple integer fields containing the value of the enum?

您认为这些表应该在数据库中自己的表中表示,然后通过外键关系从其他表中引用吗?还是应该将它们作为包含枚举值的简单整数字段存储在适当的表中?

Please explain your answer, also with respect to advocates of adherance to full third normal form design.

请解释一下你的答案,同时也要考虑到支持完全第三范式设计的人。

5 个解决方案

#1


1  

What I do is store the enum integer value in the appropriate table column but also have a table in the database that represents the enumeration (with matching ids that match the enumeration values). Yes, it means you need to keep both the enum and the enum table up to date but like you said they are rarely going to change so it is not a big deal. The benefit is that you can use real names in your code with the enum and then also get the name of that enum value when querying the database.

我所做的是将enum整型值存储在适当的表列中,但也在数据库中有一个表来表示枚举值(使用匹配的id来匹配枚举值)。是的,这意味着您需要保持enum和enum表格的最新状态,但正如您所说,它们很少会改变,因此这不是什么大问题。好处是,您可以在代码中使用enum,然后在查询数据库时获取枚举值的名称。

#2


1  

Simple integer fields (no additional table) would do the work. It keeps everything simple.

简单的整数字段(没有附加的表)就可以完成这项工作。它使一切简单。

#3


1  

Store them as simple integer fields, but also have a table for the enum with the integer and description. This can be joined or referred to as necessary.

将它们存储为简单的整型字段,但也有一个包含整型和描述的枚举表。这可以在必要时加入或引用。

#4


0  

If this issue happens only once, you should use integer fields (KISS strategy), but if you have a lot of enumerated values, you could have a table with a tree structure for storing them.

如果这个问题只发生一次,那么您应该使用整数字段(KISS策略),但是如果您有很多枚举值,那么您就可以有一个具有树结构的表来存储它们。

#5


0  

I'd store them as simple integer fields containing the value of the enum in the appropriate tables. You could for reference, have a lookup table with each enum value listed along with it's corresponding description. My reason for this opinion, is that you don't want to introduce an extra unneccessary join, which you'd have to do if you didn't store the enum value in the table itself.

我将它们作为包含枚举值的简单整数字段存储在适当的表中。您可以使用一个查找表,其中列出了每个enum值及其相应的描述。我的理由是,您不想引入额外的不必要的连接,如果您没有将枚举值存储到表本身中,那么您将不得不这样做。

#1


1  

What I do is store the enum integer value in the appropriate table column but also have a table in the database that represents the enumeration (with matching ids that match the enumeration values). Yes, it means you need to keep both the enum and the enum table up to date but like you said they are rarely going to change so it is not a big deal. The benefit is that you can use real names in your code with the enum and then also get the name of that enum value when querying the database.

我所做的是将enum整型值存储在适当的表列中,但也在数据库中有一个表来表示枚举值(使用匹配的id来匹配枚举值)。是的,这意味着您需要保持enum和enum表格的最新状态,但正如您所说,它们很少会改变,因此这不是什么大问题。好处是,您可以在代码中使用enum,然后在查询数据库时获取枚举值的名称。

#2


1  

Simple integer fields (no additional table) would do the work. It keeps everything simple.

简单的整数字段(没有附加的表)就可以完成这项工作。它使一切简单。

#3


1  

Store them as simple integer fields, but also have a table for the enum with the integer and description. This can be joined or referred to as necessary.

将它们存储为简单的整型字段,但也有一个包含整型和描述的枚举表。这可以在必要时加入或引用。

#4


0  

If this issue happens only once, you should use integer fields (KISS strategy), but if you have a lot of enumerated values, you could have a table with a tree structure for storing them.

如果这个问题只发生一次,那么您应该使用整数字段(KISS策略),但是如果您有很多枚举值,那么您就可以有一个具有树结构的表来存储它们。

#5


0  

I'd store them as simple integer fields containing the value of the enum in the appropriate tables. You could for reference, have a lookup table with each enum value listed along with it's corresponding description. My reason for this opinion, is that you don't want to introduce an extra unneccessary join, which you'd have to do if you didn't store the enum value in the table itself.

我将它们作为包含枚举值的简单整数字段存储在适当的表中。您可以使用一个查找表,其中列出了每个enum值及其相应的描述。我的理由是,您不想引入额外的不必要的连接,如果您没有将枚举值存储到表本身中,那么您将不得不这样做。