在数据库表中保持枚举

在数据库表中保持枚举

本文介绍了在数据库表中保持枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个订单,具有一个状态(在代码中是一个枚举)。问题是如何坚持这个。我可以:


  1. 在字段中保持字符串,然后映射回到枚举数据检索。

  2. 将它保持为整数,然后映射回数据检索的枚举。

  3. 为枚举值创建单独的表,并进行数据检索的连接。

想法?

解决方案

如果这是一个固定列表它似乎是,否则你不应该将它存储为一个枚举),我不会使用#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.

这篇关于在数据库表中保持枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 22:46