An ENUM
is a string object with a value chosen from a list of permitted values that are enumerated explicitly in the column specification at table creation time.ENUM
是一个字符串对象,其值从表创建时列规范中显式枚举的允许值列表中选择。
See Section 11.3.1, “String Data Type Syntax” for 有关ENUM
type syntax and length limits.ENUM
类型语法和长度限制,请参阅第11.3.1节,“字符串数据类型语法”。
The ENUM
type has these advantages:ENUM
类型具有以下优点:
Compact data storage in situations where a column has a limited set of possible values. 在列的可能值集有限的情况下压缩数据存储。The strings you specify as input values are automatically encoded as numbers. 指定为输入值的字符串将自动编码为数字。See Section 11.7, “Data Type Storage Requirements” for storage requirements for the 有关ENUM
type.ENUM
类型的存储要求,请参阅第11.7节,“数据类型存储要求”。
Readable queries and output. 可读的查询和输出。The numbers are translated back to the corresponding strings in query results.这些数字被翻译回查询结果中相应的字符串。
and these potential issues to consider:以及需要考虑的潜在问题:
If you make enumeration values that look like numbers, it is easy to mix up the literal values with their internal index numbers, as explained in Enumeration Limitations.如果使枚举值看起来像数字,则很容易将文字值与其内部索引号混淆,如枚举限制中所述。
Using 在ENUM
columns in ORDER BY
clauses requires extra care, as explained in Enumeration Sorting.ORDER BY
子句中使用ENUM
列需要格外小心,如枚举排序中所述。
An enumeration value must be a quoted string literal. 枚举值必须是带引号的字符串文字。For example, you can create a table with an 例如,您可以创建一个具有如下ENUM
column like this:ENUM
列的表:
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;
Inserting 1 million rows into this table with a value of 如果将值为'medium'
would require 1 million bytes of storage, as opposed to 6 million bytes if you stored the actual string 'medium'
in a VARCHAR
column.'medium'
的100万行插入此表,则需要100万字节的存储空间,而如果将实际字符串'medium'
存储在VARCHAR
列中,则需要600万字节的存储空间。
Each enumeration value has an index:每个枚举值都有一个索引:
The elements listed in the column specification are assigned index numbers, beginning with 1.列规范中列出的元素被分配了索引号,从1开始。
The index value of the empty string error value is 0. 空字符串错误值的索引值为0。This means that you can use the following 这意味着您可以使用以下SELECT
statement to find rows into which invalid ENUM
values were assigned:SELECT
语句查找分配了无效枚举值的行:
mysql> SELECT * FROM tbl_name
WHERE enum_col
=0;
The index of the NULL
value is NULL
.NULL
值的索引是NULL
。
The term “index” here refers to a position within the list of enumeration values. 这里的“索引”是指枚举值列表中的位置。It has nothing to do with table indexes.它与表索引无关。
For example, a column specified as 例如,指定为ENUM('Mercury', 'Venus', 'Earth')
can have any of the values shown here. ENUM('Mercury', 'Venus', 'Earth')
的列可以具有此处显示的任何值。The index of each value is also shown.还显示了每个值的索引。
NULL | NULL |
'' | 0 |
'Mercury' | 1 |
'Venus' | 2 |
'Earth' | 3 |
An ENUM
column can have a maximum of 65,535 distinct elements.ENUM
列最多可以有65535个不同的元素。
If you retrieve an 如果在数值上下文中检索ENUM
value in a numeric context, the column value's index is returned. ENUM
值,将返回列值的索引。For example, you can retrieve numeric values from an 例如,可以从ENUM
column like this:ENUM
列中检索数值,如下所示:
mysql> SELECT enum_col
+0 FROM tbl_name
;
Functions such as 如果需要,SUM()
or AVG()
that expect a numeric argument cast the argument to a number if necessary. SUM()
或AVG()
等需要数字参数的函数会将参数转换为数字。For 对于ENUM
values, the index number is used in the calculation.ENUM
值,在计算中使用索引号。
Trailing spaces are automatically deleted from 创建表时,将自动从表定义中的ENUM
member values in the table definition when a table is created.ENUM
成员值中删除尾随空格。
When retrieved, values stored into an 检索时,存储到ENUM
column are displayed using the lettercase that was used in the column definition. ENUM
列中的值将使用列定义中使用的大小写显示。Note that 请注意,可以为ENUM
columns can be assigned a character set and collation. ENUM
列分配字符集和排序规则。For binary or case-sensitive collations, lettercase is taken into account when assigning values to the column.对于二进制或区分大小写的排序规则,在为列赋值时会考虑大小写。
If you store a number into an 如果将数字存储到ENUM
column, the number is treated as the index into the possible values, and the value stored is the enumeration member with that index. ENUM
列中,则该数字将被视为可能值的索引,并且存储的值是具有该索引的枚举成员。(However, this does not work with (但是,这不适用于将所有输入视为字符串的LOAD DATA
, which treats all input as strings.) LOAD DATA
。)If the numeric value is quoted, it is still interpreted as an index if there is no matching string in the list of enumeration values. 如果该数值加了引号,则如果枚举值列表中没有匹配的字符串,该数值仍将被解释为索引。For these reasons, it is not advisable to define an 由于这些原因,不建议使用类似于数字的枚举值定义ENUM
column with enumeration values that look like numbers, because this can easily become confusing. ENUM
列,因为这样很容易造成混淆。For example, the following column has enumeration members with string values of 例如,以下列的枚举成员的字符串值为'0'
, '1'
, and '2'
, but numeric index values of 1
, 2
, and 3
:'0'
、'1'
和'2'
,但数字索引值为1
、2
和3
:
numbers ENUM('0','1','2')
If you store 如果存储2
, it is interpreted as an index value, and becomes '1'
(the value with index 2). 2
,它将被解释为索引值,并变为'1'
(索引为2的值)。If you store 如果存储'2'
, it matches an enumeration value, so it is stored as '2'
. '2'
,则它与枚举值匹配,因此存储为'2'
。If you store 如果存储'3'
, it does not match any enumeration value, so it is treated as an index and becomes '2'
(the value with index 3).'3'
,则它与任何枚举值都不匹配,因此它被视为索引并变为'2'
(索引为3的值)。
mysql>INSERT INTO t (numbers) VALUES(2),('2'),('3');
mysql>SELECT * FROM t;
+---------+ | numbers | +---------+ | 1 | | 2 | | 2 | +---------+
To determine all possible values for an 若要确定ENUM
column, use SHOW COLUMNS FROM
and parse the tbl_name
LIKE 'enum_col
'ENUM
definition in the Type
column of the output.ENUM
列的所有可能的值,请使用SHOW COLUMNS FROM
,并将tbl_name
LIKE 'enum_col
'ENUM
定义解析为输出的TYPE
列的形式。
In the C API, 在C API中,ENUM
values are returned as strings. ENUM
值作为字符串返回。For information about using result set metadata to distinguish them from other strings, see C API Basic Data Structures.有关使用结果集元数据将其与其他字符串区分开来的信息,请参见C API基本数据结构。
An enumeration value can also be the empty string (在某些情况下,枚举值也可以是空字符串(''
) or NULL
under certain circumstances:''
)或NULL
:
If you insert an invalid value into an 如果在ENUM
(that is, a string not present in the list of permitted values), the empty string is inserted instead as a special error value. ENUM
中插入无效值(即,允许值列表中不存在的字符串),则会将空字符串作为特殊错误值插入。This string can be distinguished from a “normal” empty string by the fact that this string has the numeric value 0. 此字符串与“普通”空字符串的区别在于此字符串的数值为0。See Index Values for Enumeration Literals for details about the numeric indexes for the enumeration values.有关枚举值的数字索引的详细信息,请参见枚举文字的索引值。
If strict SQL mode is enabled, attempts to insert invalid 如果启用了严格SQL模式,则尝试插入无效的ENUM
values result in an error.ENUM
值将导致错误。
If an 如果ENUM
column is declared to permit NULL
, the NULL
value is a valid value for the column, and the default value is NULL
. ENUM
列被声明为允许NULL
,则NULL
值是该列的有效值,默认值为NULL
。If an 如果枚举列声明为ENUM
column is declared NOT NULL
, its default value is the first element of the list of permitted values.NOT NULL
,则其默认值是允许值列表的第一个元素。
ENUM
values are sorted based on their index numbers, which depend on the order in which the enumeration members were listed in the column specification. ENUM
值根据其索引号进行排序,索引号取决于列规范中列出枚举成员的顺序。For example, 例如,对于'b'
sorts before 'a'
for ENUM('b', 'a')
. ENUM('b','a')
,'b'
排序在'a'
之前。The empty string sorts before nonempty strings, and 空字符串排序在非空字符串之前,NULL
values sort before all other enumeration values.NULL
值排序在所有其他枚举值之前。
To prevent unexpected results when using the 要防止在枚举列上使用ORDER BY
clause on an ENUM
column, use one of these techniques:ORDER BY
子句时出现意外结果,请使用以下技术之一:
Specify the 按字母顺序指定ENUM
list in alphabetic order.ENUM
列表。
Make sure that the column is sorted lexically rather than by index number by coding 确保该列是按词汇进行排序的,而不是按ORDER BY CAST(
or col
AS CHAR)ORDER BY CONCAT(
.col
)ORDER BY CAST(
或按col
AS CHAR)ORDER BY CONCAT(
进行编码的索引号排序的。col
)
An enumeration value cannot be an expression, even one that evaluates to a string value.枚举值不能是表达式,即使计算结果是字符串值。
For example, this 例如,此CREATE TABLE
statement does not work because the CONCAT
function cannot be used to construct an enumeration value:CREATE TABLE
语句不起作用,因为CONCAT
函数不能用于构造枚举值:
CREATE TABLE sizes ( size ENUM('small', CONCAT('med','ium'), 'large') );
You also cannot employ a user variable as an enumeration value. 也不能将用户变量用作枚举值。This pair of statements do not work:这对语句不起作用:
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 on storage over the appropriate 我们强烈建议您不要使用数字作为枚举值,因为它不会在适当的TINYINT
or SMALLINT
type, and it is easy to mix up the strings and the underlying number values (which might not be the same) if you quote the ENUM
values incorrectly. TINYINT
或SMALLINT
类型上节省存储空间,而且如果您错误地引用枚举值,则很容易混淆字符串和基础数字值(可能不相同)。If you do use a number as an enumeration value, always enclose it in quotation marks. 如果使用数字作为枚举值,请始终将其括在引号中。If the quotation marks are omitted, the number is regarded as an index. 如果省略引号,则该数字被视为索引。See Handling of Enumeration Literals to see how even a quoted number could be mistakenly used as a numeric index value.请参阅枚举文字的处理,以了解如何将带引号的数字错误地用作数字索引值。
Duplicate values in the definition cause a warning, or an error if strict SQL mode is enabled.如果启用了严格SQL模式,定义中的重复值将导致警告或错误。