← Back to MySQL Course | Chapter 3: Data Types | Lesson 4 of 8

ENUM Data Type

What is ENUM?

ENUM restricts a column to one value chosen from a fixed list you define at table-creation time, such as small, medium, large — it's a lightweight alternative to a full lookup table for a small, stable set of options.

Example: What is ENUM?

sql
CREATE TABLE shirts (size ENUM('small', 'medium', 'large'));
INSERT INTO shirts VALUES ('medium');

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

Querying ENUM Fields

Even though you insert and query ENUM values as ordinary strings, MySQL actually stores them internally as compact integer indexes, which keeps the column small and fast to compare.

Example: Querying ENUM Fields

sql
CREATE TABLE shirts (size ENUM('small', 'medium', 'large'));
INSERT INTO shirts VALUES ('medium');
SELECT * FROM shirts WHERE size = 'medium';

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

ENUM Numeric Indexing

Each defined ENUM value gets an implicit index starting at 1 (with 0 reserved for invalid/empty entries), and you can technically insert or filter by that numeric index instead of the string.

Example: ENUM Numeric Indexing

sql
CREATE TABLE shirts (size ENUM('small', 'medium', 'large'));
INSERT INTO shirts VALUES ('medium');
SELECT * FROM shirts WHERE size + 0 = 2;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

Handling Invalid ENUM Values

In strict SQL mode, inserting a value not present in the ENUM's defined list raises an error and rejects the row; outside strict mode, MySQL may instead silently store an empty string.

Example: Handling Invalid ENUM Values

sql
CREATE TABLE shirts (size ENUM('small', 'medium', 'large'));
INSERT INTO shirts VALUES ('extra-large'); -- rejected in strict SQL mode

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

Modifying ENUM Options

Adding a new allowed value to an ENUM requires an ALTER TABLE that redefines the full column, which can be a slow, table-rewriting operation on very large tables — a real tradeoff against ENUM's compactness.

Example: Modifying ENUM Options

sql
ALTER TABLE shirts MODIFY size ENUM('small', 'medium', 'large', 'x-large');

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.