SQL reference (MySQL and MariaDB dialects)

ENUM and SET

ENUM and SET columns: syntax, how values are matched and stored, the index form, and errors.

Syntax#

SQL
ENUM ('value1', 'value2', ...)
SET ('value1', 'value2', ...)

Remarks#

  • Matching ignores case and the canonical spelling from the definition is stored: inserting 'RED' into ENUM('red','green') stores red.
  • ENUM holds one of the values, or the value at a number: 2 is the second value. The index of a value is its position in the definition, starting at 1. The default of a NOT NULL ENUM is the first value.
  • SET holds 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 ENUM and SET columns report as such in SHOW CREATE TABLE, DESCRIBE and information_schema.COLUMNS.
  • ENUM or SET without values is an error. ALTER TABLE ... MODIFY of these columns works.

Examples#

SQL
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

See also#