ENUM and SET
ENUM and SET columns: syntax, how values are matched and stored, the index form, and errors.
Syntax#
ENUM ('value1', 'value2', ...)
SET ('value1', 'value2', ...)Remarks#
- Matching ignores case and the canonical spelling from the definition is stored: inserting
'RED'intoENUM('red','green')storesred. ENUMholds one of the values, or the value at a number:2is the second value. The index of a value is its position in the definition, starting at 1. The default of aNOT NULLENUMis the first value.SETholds none, one or several of the values, written as a comma-separated text ('a,c'). The stored text lists the values in the order of the definition, each once.- Wrong value. A value that is not a member (or, for
SET, a text with a member that does not exist) is error 1265 (The value stored in column 'c' (row 1) was cut off) in both strict and non-strict mode. - Wire form. Clients see the types and flags drivers expect for these columns, so
ENUMandSETcolumns report as such inSHOW CREATE TABLE,DESCRIBEandinformation_schema.COLUMNS. ENUMorSETwithout values is an error.ALTER TABLE ... MODIFYof these columns works.
Examples#
CREATE TABLE shirts (id INT PRIMARY KEY, size ENUM('small', 'medium', 'large') NOT NULL, tags SET('new', 'sale', 'gift'));
INSERT INTO shirts VALUES (1, 'MEDIUM', 'gift,new'), (2, 3, NULL);
SELECT id, size, tags FROM shirts ORDER BY id; -- 1 medium new,gift ; 2 large NULL
INSERT INTO shirts VALUES (3, 'huge', NULL); -- error 1265