Columns of enum type are not recommended for the following reasons:
Ask questions
We use enumerations a lot in our work. For example, gender (male, female) and Week (Monday to Sunday). We all have code like this:
public enum GenderEnum {
MALE(1."Male"),
FEMALE(0."Female"),;private final int code;
private final String desc;
public GenderEnum(int code, String desc) {
this.code = code;
this.desc = desc; }}Copy the code
And this “gender” needs to be stored as the user information falls into DB, so in DB we store the digits 0 and 1, or the string male and female?
To analyze problems
Store the digits 0 and 1
It feels like that’s the way most people do it. This approach has several advantages: 1, save DB space (although the memory space is no longer care, but also a slight advantage) 2, do not need to pay attention to enumeration corresponding to the front of the display content, when the display content changes (such as: Mr., Ms.). Do you still need to brush DB history data? 3, the front end receives the numerical type, which is more convenient to judge, and there is no need to change the display content when it changes. If the judgment code is if (gender == ‘male ‘), you need to change this if you want to show Mr. And Ms.
The biggest drawback to this approach is that when you see the value, it’s not clear what it means (even if you’re an enumeration definer, it’s hard to remember).
The advantages and disadvantages of storing strings for men and women are just the opposite.
conclusion
For the sake of extensibility, maintainability, and so on, sacrifice readability and recommend storing numbers. Don’t take any chances and think the content won’t change. If it does change, you’re really screwed.