11.3.5 ENUM Type


Official document address: 11.3.5 The ENUM Type


An enumeration is a string object whose value is selected from a list of allowed values. These values ​​are explicitly listed in the column specification when the table is created.

Please refer to 11.3.1 String Data Type Syntax for the ENUMtype syntax and length limitation.

ENUMThe type has the following advantages:

  • Compress data storage when the set of possible values ​​for a column is limited. The string specified as the input value will be automatically encoded as a number. ENUMFor storage requirements of types, see 11.7 Data Type Storage Requirements .
  • Readable query and output. These numbers are converted back to the corresponding string in the query results.

These potential issues need to be considered:

  • If you make the enumeration values ​​look like numbers, as explained in the enumeration restrictions, it is easy to confuse the literal values ​​and their internal index numbers.
  • The ORDER BYuse of ENUMcolumns in clauses requires special care, as explained in Enumeration Sorting.

Create and use ENUM columns

The enumeration value must be a quoted string literal. For example, you can create a ENUMtable with columns like this :

CREATE TABLE shirts (
    name VARCHAR(40),
    size ENUM('x-small', 'small', 'medium', 'large', 'x-large')
);

INSERT INTO shirts (name, size) VALUES ('dress shirt','large'), ('t-shirt','medium'), ('polo shirt','small');

SELECT name, size FROM shirts WHERE size = 'medium';
+---------+--------+
| name    | size   |
+---------+--------+
| t-shirt | medium |
+---------+--------+

UPDATE shirts SET size = 'small' WHERE size = 'large';
COMMIT;

The 100million lines value 'medium'data into the table requires 100one million bytes of memory space, and if the actual string 'medium'in the storage VARCHARcolumn is required 600one million bytes.

Index value of enum literal

Each enumeration value has an index:

  • The elements listed in the column specification are assigned index numbers, starting from the 1beginning.
  • The index value of the empty string error value is 0. This means that you can use the following SELECTstatement to find ENUMrows that specify invalid values:
mysql> SELECT * FROM tbl_name WHERE enum_col=0;
  • NULLThe index of the value is NULL.
  • The term "index" here refers to a position in the list of enumerated values. It has nothing to do with the index of the table.

For example, ENUM('Mercury', 'Venus', 'Earth')the column designated as can have any of the values ​​shown in the following table. The following table also shows the index of each value.

value index
NULL NULL
'' 0
'Mercury' 1
'Venus' 2
'Earth' 3

A ENUMcolumn can have at most 65,535one different element.

If the value is retrieved in a numeric context, ENUMthe index of the column value is returned. For example, you can ENUMretrieve values from a column like this :

mysql> SELECT enum_col+0 FROM tbl_name;

Functions that require numeric parameters such as SUM()or AVG()convert the parameters to numbers when necessary. For the ENUMvalue, the index number is used in the calculation.

Handling enumerated literals

When creating a table, trailing spaces are automatically removed from the ENUMmember values ​​in the table definition .

When retrieving, ENUMthe values stored in the column will be displayed using the letters used in the column definition. Note that the ENUM column can be assigned a character set and collation. For binary or case-sensitive collations, letters should be considered when assigning values ​​to columns.

If you store a number in a ENUMcolumn, the number will be treated as an index of possible values, and the stored value is the enumeration member with that index. (However, this LOAD DATAdoes not work, LOAD DATAall inputs are treated as strings.) If the value is referenced, if an enumerated list of values does not match the string, it remains to be interpreted as an index. For these reasons, it is not recommended to define an enumeration column whose enumeration values ​​look like numbers, as this can easily cause confusion. For example, the string values ​​of the following enumeration members are '0', '1'and '2', but the numeric index value is 1, 2and 3:

numbers ENUM('0','1','2')

If stored 2, it will be interpreted as the index value and become '1'( 2the value of index ). If stored '2', it will match an enumerated value, so store it as '2'. If stored '3', it does not match any enumeration value, so it is treated as an index and becomes '2'( 3value of index ).

mysql> INSERT INTO t (numbers) VALUES(2),('2'),('3');
mysql> SELECT * FROM t;
+---------+
| numbers |
+---------+
| 1       |
| 2       |
| 2       |
+---------+

To determine ENUMall possible values ​​of SHOW COLUMNS FROM tbl_name LIKE 'enum_col'a Typecolumn , use ENUMthe definition that can be seen in the output column .

mysql> SHOW COLUMNS FROM shirts LIKE 'size';
+-------+----------------------------------------------------+------+-----+---------+-------+
| Field | Type                                               | Null | Key | Default | Extra |
+-------+----------------------------------------------------+------+-----+---------+-------+
| size  | enum('x-small','small','medium','large','x-large') | YES  |     | NULL    |       |
+-------+----------------------------------------------------+------+-----+---------+-------+
1 row in set

mysql> 

Empty or NULL enumeration value

Enumeration values ​​can also be empty strings ( '') or NULL:

  • If you insert an invalid value into ENUM(that is, a string that does not appear in the list of allowed values), the empty string will be inserted as a special error value, which is the same as the "normal" empty string The difference lies in the index value of this string 0. For more information about the numerical index of enumeration values, see Index value of enum literals.

    If strict SQL mode is enabled, attempts to insert invalid ENUMvalues ​​will cause an error.
  • If the ENUMcolumn is declared as allowed NULL, the NULLvalue is a valid value for the column, and the default value is NULL. If a ENUMcolumn is declared as negative NULL, its default value is the first element in the list of allowed values.

Enumeration sort

ENUMThe values ​​are sorted by index number, which depends on the order in which the enumeration members are listed in the column specification. For example, ENUM('b','a'), 'b'the 'a'sort before. Empty strings are sorted before non-empty strings, and NULLvalues ​​are sorted before all other enumerated values.

To prevent unexpected results when ENUMusing ORDER BYclauses on columns , you can use one of the following techniques:

  • Specify the ENUMlist in alphabetical order .
  • Make sure that the columns are sorted lexically, not by index number. That is ORDER BY CAST(col AS CHAR)or ORDER BY CONCAT(col).

Enumeration limit

The enumeration value cannot be an expression, even if the calculation result of the expression is a string value.

For example, the following CREATE TABLEstatement will not work because CONCATfunctions cannot be used to construct enumeration values:

CREATE TABLE sizes (
    size ENUM('small', CONCAT('med','ium'), 'large')
);

Nor can you use user variables as enumeration values. This has no effect on the statement:

SET @mysize = 'medium';

CREATE TABLE sizes (
    size ENUM('small', @mysize, 'large')
);

We strongly recommend that you do not use numbers as enumeration values, because it does not save storage space compared to the appropriate TINYINTor SMALLINTtype, and it is easy to confuse the string and the underlying numeric value (or maybe not) if you don’t quote the enumeration correctly value. If you use a number as an enumeration value, always enclose it in quotation marks. If you omit the quotation marks, the number will be treated as an index. Please refer to the handling of enumerated literals to understand that even quoted numbers may be incorrectly used as numerical index values.

If strict SQL mode is enabled, duplicate values ​​in the definition will cause warnings or errors.

Guess you like

Origin blog.csdn.net/wb1046329430/article/details/114786368