Rechercher dans le manuel MySQL
11.4.4 The ENUM Type
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.
It has these advantages:
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.8, “Data Type Storage Requirements” for the storage requirements for
ENUM
types.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 inORDER BY
clauses requires extra care, as explained in Enumeration Sorting.
Creating and Using ENUM Columns
An enumeration value must be a quoted string literal. For
example, you can create a table with an
ENUM
column like this:
- );
- ('polo shirt','small');
- +---------+--------+
- | name | size |
- +---------+--------+
- | t-shirt | medium |
- +---------+--------+
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.
Each enumeration value has an index:
The elements listed in the column specification are assigned index numbers, beginning with 1.
The index value of the empty string error value is 0. This means that you can use the following
SELECT
statement to find rows into which invalidENUM
values were assigned:The index of the
NULL
value isNULL
.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. The index of each value is also shown.
Value | Index |
---|---|
NULL |
NULL |
'' |
0 |
'Mercury' |
1 |
'Venus' |
2 |
'Earth' |
3 |
An ENUM
column can have a
maximum of 65,535 distinct elements.
If you retrieve an ENUM
value in a numeric
context, the column value's index is returned. For example,
you can retrieve numeric values from an
ENUM
column like this:
Functions such as SUM()
or
AVG()
that expect a numeric
argument cast the argument to a number if necessary. For
ENUM
values, the index number is used in
the calculation.
Trailing spaces are automatically deleted from
ENUM
member values in the table definition
when a table is created.
When retrieved, values stored into an ENUM
column are displayed using the lettercase that was used in the
column definition. Note that ENUM
columns
can be assigned a character set and collation. 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. (However, this does not work with
LOAD DATA
, which treats all
input as strings.) 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. 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
:
If you store 2
, it is interpreted as an
index value, and becomes '1'
(the value
with index 2). If you store '2'
, it matches
an enumeration value, so it is stored as
'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).
- +---------+
- | 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.
In the C API, ENUM
values are returned as
strings. For information about using result set metadata to
distinguish them from other strings, see
Section 28.7.5, “C API Data Structures”.
An enumeration value can also be the empty string
(''
) or NULL
under
certain circumstances:
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. This string can be distinguished from a “normal” empty string by the fact that this string has the numeric value 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
ENUM
values result in an error.If an
ENUM
column is declared to permitNULL
, theNULL
value is a valid value for the column, and the default value isNULL
. If anENUM
column is declaredNOT NULL
, its default value is the first element of the list of permitted values.
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. For example,
'b'
sorts before 'a'
for
ENUM('b', 'a')
. The empty string sorts
before nonempty strings, and NULL
values
sort before all other enumeration values.
To prevent unexpected results when using the ORDER
BY
clause on an ENUM
column, use
one of these techniques:
Specify the
ENUM
list in alphabetic order.Make sure that the column is sorted lexically rather than by index number by coding
ORDER BY CAST(
orcol
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:
You also cannot employ a user variable as an enumeration value. This pair of statements do not work:
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. 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.
Traduction non disponible
Le manuel MySQL n'est pas encore traduit en français sur l'infobrol. Seule la version anglaise est disponible pour l'instant.
Document créé le 26/06/2006, dernière modification le 26/10/2018
Source du document imprimé : https://www.gaudry.be/mysql-rf-enum.html
L'infobrol est un site personnel dont le contenu n'engage que moi. Le texte est mis à disposition sous licence CreativeCommons(BY-NC-SA). Plus d'info sur les conditions d'utilisation et sur l'auteur.
Références
Ces références et liens indiquent des documents consultés lors de la rédaction de cette page, ou qui peuvent apporter un complément d'information, mais les auteurs de ces sources ne peuvent être tenus responsables du contenu de cette page.
L'auteur de ce site est seul responsable de la manière dont sont présentés ici les différents concepts, et des libertés qui sont prises avec les ouvrages de référence. N'oubliez pas que vous devez croiser les informations de sources multiples afin de diminuer les risques d'erreurs.