简体   繁体   English

在MySQL数据库中存储枚举

[英]Storing Enums in MySQL Database

I need the most clean and efficient solution for my case. 对于我的案例,我需要最干净,最有效的解决方案。 I'm trying to store Marks of student in a MySQL DB. 我正在尝试将学生的Marks存储在MySQL DB中。 A student may be "ABSENT" or "COPY CASE", I want to store such info also in DB. 学生可能是“缺席”或“复制案例”,我想在DB中存储此类信息。

I thought of assigning codes for above case like -1 for ABSENT, -2 for COPY CASE, etc. This codes will be stored in Marks column only. 我想为上面的情况分配代码,例如-1为ABSENT,-2为COPY CASE等。这些代码将仅存储在Marks列中。

More ever, while reading them with a select query, I should get their display values ie ABSENT , COPY CASE , etc only. 以往更多,而与选择查询读取它们,我应该得到它们的显示值,即ABSENTCOPY CASE ,只有等。

Can all these be achieved at DB end only? 所有这些只能在DB端实现吗?

Or do I need to implement these things at Application level only? 或者我是否只需要在应用程序级别实现这些功能?

Are there any API's available for Java for such functionality? 是否有可用于Java的API用于此类功能?

mysql supports enums: mysql支持枚举:

CREATE TABLE students (name varchar(64), mark ENUM('ABSENT','COPY CASE' ));
INSERT INTO students VALUES ('john', 'COPY CASE');

In java, you can treat this column as a string type column, but in the database, values are stored efficiently, and trying to store a value not contained in the enum will result in an error. 在java中,您可以将此列视为字符串类型列,但在数据库中,值会有效存储,并且尝试存储未包含在枚举中的值将导致错误。

如何将其name() (String版本)存储到DB并使用valueOf()从DB读取时创建它

If the enum string representations are pretty string heavy, and the database is large (checks against these enums are frequent), I'd probably map each enum to an integer constant (no, not the ordinal() , but your own custom integer value) and have methods on the enum to get and create an enum from these values. 如果枚举字符串表示非常重,并且数据库很大(对这些枚举的检查很频繁),我可能会将每个枚举映射到一个整数常量(不,不是ordinal() ,而是你自己的自定义整数值并且在枚举上有方法来从这些值中获取和创建枚举。 This way, you'll be able to store the enums pretty efficiently. 这样,您就可以非常有效地存储枚举。

public enum Status {

  ABSENT(1), COPY_PASTE(2);

  int value;

  private Status(int value) {
      this.value = value;
  }

  public int getValue() {
      return this.value;
  }

  public static Status ofStatusCode(int value) {
      // retrieve status from status code
  }

}

I thought of storing codes for above case like -1 for ABSENT, -2 for COPY CASE, etc. This codes will be stored in Marks column only. 我想过为上面的情况存储代码,例如-1表示ABSENT,-2表示COPY CASE等。这些代码只存储在Marks列中。

I'd rather prefer having a separate column, say status which holds values such as ABSENT , COPY CASE and a default value such as APPEARED 我宁愿选择一个单独的列,比如status ,它包含诸如ABSENTCOPY CASE和默认值(如APPEARED

And marks column would stay separate. 标记列将保持独立。

In the view layer, you could do something like 在视图层中,您可以执行类似的操作

if(status is 'APPEARED')
show marks field
else
show status field

MySQL has it own ENUM type . MySQL拥有自己的ENUM类型 And as @Jigar Pointed out, you can use Enum.getName to get Java's a String representing the enum for persistence, retrieve the field from the database as a String and use Enum.valueOf to load the enum constant back. 正如@Jigar指出的那样,您可以使用Enum.getName来获取Java表示持久性枚举的String,从数据库中检索字段作为String并使用Enum.valueOf加载枚举常量。

You can also use Enum.ordinal() + 1 to get a integer for persistence (MySQL also support using integer indexes directly with ENUM type) and issue a query like: 您还可以使用Enum.ordinal() + 1获取持久性的整数(MySQL也支持使用ENUM类型直接使用整数索引)并发出如下查询:

SELECT enum_col-1 FROM tbl_name;

to retrieve the ordinal back (it will retrieve -1 for rows with invalid enum values). 检索序数返回(对于具有无效枚举值的行,它将检索-1)。

Plus MyEnumType value = MyEnumType.values()[ordinal] to convert it back to Java enum type. 加上MyEnumType value = MyEnumType.values()[ordinal]将其转换回Java枚举类型。

Anyway, I think that the String based solution is cleaner and more elegant, plus safer on the long run (if you ever need to change the order of the java Enum, or add new enum values at the middle of the existing ones, you will thank yourself for going with String based strategy to begin with). 无论如何,我认为基于String的解决方案更干净,更优雅,从长远来看更安全(如果您需要更改java Enum的顺序,或者在现有的中间添加新的枚举值,您将感谢你开始使用基于字符串的策略)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM