Enum in SQL: Meaning, Usage, and Alternatives

How-to basics · Updated November 23, 2026

In SQL, ENUM is a string object that restricts a column’s value to a predefined list of allowed values, stored internally as integers. It is a MySQL-specific data type (not part of standard SQL) that enforces data integrity at the database level, making it useful for columns with a fixed set of possible values, such as statuses, types, or categories.

What Exactly Is an ENUM?

An ENUM column is defined with a list of allowed values, for example:

CREATE TABLE orders (
  id INT PRIMARY KEY,
  status ENUM('pending', 'shipped', 'delivered', 'cancelled')
);

When you insert a row, the value must be one of those strings, or an error occurs. Internally, MySQL stores each value as an integer index (1 for the first, 2 for the second, etc.), but when you query the table, it returns the string. This storage method makes ENUM compact and fast for comparisons, but it also introduces quirks, such as the ability to insert an integer that corresponds to a value, and the fact that the ordering of results is based on the index, not alphabetical order.

How ENUM Works: Storage and Indexing

Each ENUM value is stored as a 1- or 2-byte integer, depending on the number of values (up to 255 values uses 1 byte, up to 65,535 uses 2 bytes). This makes ENUM more space-efficient than storing the full string in a VARCHAR column. For example, a column with 10 statuses would store each value as a single byte, whereas a VARCHAR(20) would store up to 20 bytes per row. However, the trade-off is that any change to the list of allowed values requires an ALTER TABLE statement, which can be costly on large tables.

Indexing on an ENUM column works like indexing on an integer, which is faster than indexing on a string. But be careful: when you use ORDER BY on an ENUM column, MySQL sorts by the internal index, not alphabetically. To sort alphabetically, you must cast the column to a string: ORDER BY CAST(status AS CHAR).

How to Define and Use ENUM

Defining an ENUM column is straightforward in a CREATE TABLE or ALTER TABLE statement. Here are the key points:

  • List values as strings, separated by commas, inside parentheses.
  • Values can be any string, but they cannot contain commas (unless you escape them, which is not recommended).
  • The first value is index 1; the empty string ” is index 0 (used for invalid values when strict mode is off).
  • You can specify a default value; if not, the default is the first value in the list.
  • Use ENUM('value1','value2',...) in column definition.

Example with a default:

CREATE TABLE users (
  id INT PRIMARY KEY,
  role ENUM('admin', 'editor', 'viewer') DEFAULT 'viewer'
);

To insert data, you use the string values:

INSERT INTO users (id, role) VALUES (1, 'admin');

If you try to insert a value not in the list, MySQL throws an error (in strict mode) or stores an empty string (in non-strict mode).

Choosing Between ENUM and Other Data Types

ENUM is not the only way to enforce a set of allowed values. Here are common alternatives and when to use them:

  • VARCHAR with CHECK constraint: Works in MySQL 8.0.16+ and standard SQL. It stores the actual string, so ordering is alphabetical, and you can add values without altering the table (just change the constraint). However, it uses more storage.
  • Foreign key to a lookup table: The most flexible and normalized approach. Create a table with allowed values and reference it via a foreign key. This allows adding values without schema changes and supports additional attributes (e.g., descriptions).
  • TINYINT with a mapping in the application: Saves space but requires the application to know the mapping, and you lose the self-documenting nature of ENUM.

Use ENUM when the list is short, static, and unlikely to change, and when you want the database to enforce the values without extra joins. Use a lookup table when the list may grow or change, or when you need to store extra information about each value.

Common Problems and Pitfalls with ENUM

ENUM has several quirks that can trip up developers:

  • Adding a new value requires ALTER TABLE, which locks the table (in some MySQL versions) and can be slow on large tables.
  • Sorting is by index, not alphabetically. If you forget this, you may get unexpected order in results.
  • You can insert an integer that matches the index (e.g., 2 for the second value), which can be confusing if you accidentally pass an integer from a parameter.
  • Empty string handling: In non-strict mode, an invalid value becomes an empty string, which is stored as index 0. This can lead to silent data corruption if you don’t have strict mode enabled.
  • ENUM values are case-insensitive by default, but they are stored as the exact case in the definition. So ‘Admin’ is not allowed if you defined ‘admin’.
  • Changing the order of values changes the index, which can break existing data if you reorder the list.

To avoid these issues, many developers prefer a lookup table, especially for values that are likely to change.

ENUM vs. CHECK Constraint vs. Lookup Table: A Comparison

Let’s compare the three main approaches with a concrete example: a column for a user’s status (‘active’, ‘inactive’, ‘banned’).

  • ENUM: status ENUM('active','inactive','banned'). Pros: compact, fast, self-documenting. Cons: altering the list is painful, sorting is by index.
  • VARCHAR + CHECK: status VARCHAR(10) CHECK (status IN ('active','inactive','banned')). Pros: standard SQL, easy to modify the constraint, alphabetical sorting. Cons: more storage, slightly slower comparisons.
  • Lookup table: CREATE TABLE statuses (id INT PRIMARY KEY, name VARCHAR(10) UNIQUE) and status_id INT FOREIGN KEY REFERENCES statuses(id). Pros: flexible, can add values anytime, can store extra metadata. Cons: requires a JOIN to get the string, more complex queries.

In practice, if you are using MySQL and the list is truly fixed (e.g., days of the week), ENUM is fine. If there’s any chance of change, choose a lookup table.

Performance and Storage Considerations

ENUM’s main performance benefit is space savings and faster indexing because the column is stored as an integer. This can reduce table size and improve cache efficiency. However, the performance gain is often negligible for small tables, and the cost of ALTER TABLE when adding a new value can be significant for large tables (it rebuilds the table). Also, if you frequently filter on the ENUM column, the index works well, but if you need to sort alphabetically, you lose the benefit because you must cast to string.

Another consideration: when you use ENUM in a query, MySQL may need to convert between string and integer, which adds a tiny overhead. In most applications, this is irrelevant.

Frequently Asked Questions

Can I use ENUM in SQL Server or PostgreSQL?

No, ENUM is MySQL-specific. SQL Server has no direct ENUM type; you would use a CHECK constraint or a lookup table. PostgreSQL has a native CREATE TYPE ... AS ENUM that works similarly but is more flexible (you can add values with ALTER TYPE ... ADD VALUE).

How do I add a new value to an ENUM column?

Use ALTER TABLE table_name MODIFY column_name ENUM('old1','old2','newvalue'). You must list all existing values plus the new one. This can be slow on large tables.

What happens if I insert an invalid value into an ENUM column?

In strict SQL mode, MySQL rejects the row with an error. In non-strict mode, it stores an empty string (”), which is index 0. To avoid silent data issues, enable strict mode.

Can I use NULL in an ENUM column?

Yes, unless you define the column as NOT NULL. NULL is a separate value and is allowed by default. Note that NULL is not the same as an empty string.

Is ENUM better than using a lookup table for performance?

For read-heavy queries with a fixed set of values, ENUM can be slightly faster because it avoids a JOIN. However, the difference is usually minor, and the flexibility of a lookup table often outweighs the tiny performance gain.

How do I get the integer index of an ENUM value?

You can use the ENUM column in an arithmetic context, or use CAST(column AS UNSIGNED) to get the index. For example, SELECT status+0 FROM orders returns the index.

Related

Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.

More in How-to basics

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.