且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在数据库表中保持枚举

更新时间:2022-12-18 10:25:23

如果这是一个固定列表它似乎是,否则你不应该将它存储为一个枚举),我不会使用#1。



使用#3超过# 2是为了易于与自助服务查询实用程序一起使用。但是,我实际上会使用#2的变体:将值作为整数存储,并将数据映射到枚举数据检索。但是,还可以创建表示枚举类型的表,其值为PK,名称为另一列。这样,您的代码可以简单,快速,高效地使用,而且还可以轻松获取具有自助服务查询和其他不使用您的数据访问代码的用途的逻辑价值。


I have an order which has a status (which in code is an Enum). The question is how to persist this. I could:

  1. Persist the string in a field and then map back to enum on data retrieval.
  2. Persist this as an integer and then map back to enum on data retrieval.
  3. Create separate table for enum value and do a join on data retrieval.

Thoughts?

If this is a fixed list (which it seems it is, or else you shouldn't store it as an enum), I wouldn't use #1.

The main reason to use #3 over #2 is for ease of use with self-service querying utilities. However, I'd actually go with a variant of #2: Store the value as an integer and map to an enum on data retrieval. However, also create a table representing the enum type, with the value as the PK and the name as another column. That way it's simple, quick, and efficient to use with your code, but also easy to get the logical value with self-service querying and other uses that don't use your data access code.