Article Directory
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 ENUM
type syntax and length limitation.
ENUM
The 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.
ENUM
For 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 BY
use ofENUM
columns 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 ENUM
table 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 100
million lines value 'medium'
data into the table requires 100
one million bytes of memory space, and if the actual string 'medium'
in the storage VARCHAR
column is required 600
one 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
1
beginning. - The index value of the empty string error value is
0
. This means that you can use the followingSELECT
statement to findENUM
rows that specify invalid values:
mysql> SELECT * FROM tbl_name WHERE enum_col=0;
NULL
The index of the value isNULL
.- 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 ENUM
column can have at most 65,535
one different element.
If the value is retrieved in a numeric context, ENUM
the index of the column value is returned. For example, you can ENUM
retrieve 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 ENUM
value, the index number is used in the calculation.
Handling enumerated literals
When creating a table, trailing spaces are automatically removed from the ENUM
member values in the table definition .
When retrieving, ENUM
the 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 ENUM
column, 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 DATA
does not work, LOAD DATA
all 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
, 2
and 3
:
numbers ENUM('0','1','2')
If stored 2
, it will be interpreted as the index value and become '1'
( 2
the 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'
( 3
value of index ).
mysql> INSERT INTO t (numbers) VALUES(2),('2'),('3');
mysql> SELECT * FROM t;
+---------+
| numbers |
+---------+
| 1 |
| 2 |
| 2 |
+---------+
To determine ENUM
all possible values of SHOW COLUMNS FROM tbl_name LIKE 'enum_col'
a Type
column , use ENUM
the 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 string0
. 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 invalidENUM
values will cause an error. - If the
ENUM
column is declared as allowedNULL
, theNULL
value is a valid value for the column, and the default value isNULL
. If aENUM
column is declared as negativeNULL
, its default value is the first element in the list of allowed values.
Enumeration sort
ENUM
The 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 NULL
values are sorted before all other enumerated values.
To prevent unexpected results when ENUM
using ORDER BY
clauses on columns , you can use one of the following techniques:
- Specify the
ENUM
list in alphabetical order . - Make sure that the columns are sorted lexically, not by index number. That is
ORDER BY CAST(col AS CHAR)
orORDER 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 TABLE
statement will not work because CONCAT
functions 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 TINYINT
or SMALLINT
type, 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.