How to Use ENUM in SQL: A Practical Guide

How-to basics · Updated November 23, 2026

ENUM is a string object in SQL that lets you define a column with a fixed set of allowed values. It’s useful for storing data like statuses (‘pending’, ‘shipped’, ‘delivered’) or categories (‘small’, ‘medium’, ‘large’) where you want to enforce data integrity and save storage. This guide shows you how to create, use, and manage ENUM columns in MySQL (the most common SQL dialect supporting ENUM), with notes on alternatives for other databases.

What You’ll Need

  • A SQL database that supports ENUM — MySQL, MariaDB, or similar. (PostgreSQL and SQL Server do not support ENUM natively; see Tips for alternatives.)
  • Access to a SQL client or command line (e.g., mysql CLI, MySQL Workbench, or phpMyAdmin).
  • Basic familiarity with CREATE TABLE and INSERT statements.

Step-by-Step

  1. Understand ENUM syntax. An ENUM column is defined in a CREATE TABLE or ALTER TABLE statement like this: column_name ENUM('value1', 'value2', ...). The values are strings, and you can have up to 65,535 distinct values. Example: status ENUM('pending', 'active', 'inactive').
  2. Create a table with an ENUM column. Use the CREATE TABLE statement. For example:
    CREATE TABLE users (
        id INT AUTO_INCREMENT PRIMARY KEY,
        username VARCHAR(50) NOT NULL,
        status ENUM('active', 'inactive', 'banned') DEFAULT 'active'
    );

    This creates a ‘users’ table where ‘status’ can only be one of the three specified values, defaulting to ‘active’.

  3. Insert data into an ENUM column. Insert values exactly as defined (case-insensitive by default in MySQL, but stored as defined). Example:
    INSERT INTO users (username, status) VALUES ('alice', 'active');
    INSERT INTO users (username, status) VALUES ('bob', 'inactive');

    If you try to insert an invalid value, MySQL will throw an error (in strict mode) or store an empty string (in non-strict mode).

  4. Query data from an ENUM column. Use standard SELECT statements. You can also use the ENUM values in WHERE clauses:
    SELECT * FROM users WHERE status = 'active';

    You can also use numeric indexes: ENUM values are stored as integers starting at 1. So WHERE status = 1 returns rows with the first value (‘active’).

  5. Update an ENUM column. Use UPDATE with a valid value:
    UPDATE users SET status = 'inactive' WHERE username = 'alice';

    If you try to set an invalid value, it will fail or be coerced to an empty string depending on SQL mode.

  6. Alter an ENUM column to add or remove values. Use ALTER TABLE to modify the ENUM definition. To add a new value:
    ALTER TABLE users MODIFY status ENUM('active', 'inactive', 'banned', 'suspended') DEFAULT 'active';

    To remove a value, redefine without it. Be careful: existing rows with that value will become empty strings (or cause errors in strict mode).

  7. Use ENUM in comparisons and sorting. ENUM sorts by the order you defined, not alphabetically. For example, if you define priority ENUM('low', 'medium', 'high'), then ORDER BY priority returns low first, then medium, then high. This is a handy feature.
  8. Consider using ENUM for flags or small sets. It’s ideal for columns with a small, stable set of values. For larger or dynamic sets, a lookup table is better.

Common Mistakes

  • Using ENUM for values that change often. Altering an ENUM column is expensive on large tables and can lock the table. If your value list grows frequently, use a foreign key to a lookup table instead.
  • Inserting invalid values without strict mode. In MySQL’s default non-strict mode, inserting an invalid value stores an empty string ” instead of throwing an error, which can silently corrupt data. Enable strict mode (SQL mode STRICT_TRANS_TABLES) to catch errors.
  • Assuming alphabetical sorting. ENUM sorts by the order of values in the definition, not alphabetically. If you need alphabetical sorting, use ORDER BY CAST(column AS CHAR) or reorder the ENUM definition.
  • Using ENUM in other databases without checking. PostgreSQL and SQL Server don’t support ENUM as a column type. You’d need a CHECK constraint or a lookup table. Always verify your DBMS supports it.
  • Removing a value that’s in use. If you remove a value from the ENUM, existing rows with that value become invalid. In strict mode, queries might fail; in non-strict, they become empty strings. Always handle existing data first.

Tips

  • Use ENUM for small, stable sets. Good candidates: order status, user roles, day of week, gender, etc. Avoid for things like country names or product categories that can grow.
  • Set a default value. Always provide a DEFAULT for your ENUM column to avoid NULLs or empty strings. Example: DEFAULT 'pending'.
  • Enable strict SQL mode. In MySQL, run SET sql_mode = 'STRICT_TRANS_TABLES' or set it in your config to prevent silent invalid inserts.
  • Use the numeric index for fast queries. If you know the position, you can query WHERE status = 2 for the second value. But be careful: indexes can change if you reorder the ENUM definition.
  • For PostgreSQL, use a CHECK constraint. Example: status VARCHAR(10) CHECK (status IN ('active', 'inactive')). For SQL Server, similar CHECK constraint or a lookup table.
  • Document your ENUM values. Since the values are not stored as separate metadata, add comments to the column definition or in your schema documentation.

Troubleshooting

  • Insert fails with ‘Data truncated for column’. This means you’re trying to insert a value not in the ENUM list. Check your INSERT statement for typos or case mismatches (though MySQL is case-insensitive by default). If you’re in non-strict mode, it might silently store an empty string instead.
  • Queries return empty strings for ENUM columns. This usually happens when invalid values were inserted in non-strict mode. To fix, update those rows to valid values or clean the data. Also consider enabling strict mode to prevent future issues.
  • ALTER TABLE fails with ‘Duplicate entry’ or ‘Data too long’. When adding a value to an ENUM, ensure the new value doesn’t conflict with existing data. When removing a value, ensure no rows use it. You may need to update rows first.
  • Sorting doesn’t work as expected. Remember ENUM sorts by definition order, not alphabetically. If you need alphabetical, use ORDER BY CAST(status AS CHAR) or reorder the ENUM definition.
  • Getting ‘Unknown column’ in ALTER TABLE. Double-check the column name and that you’re using MODIFY, not ADD. For example: ALTER TABLE users MODIFY status ENUM(...).

Frequently Asked Questions

  • Can I use ENUM in PostgreSQL? No, PostgreSQL does not have an ENUM type like MySQL. You can create a custom type with CREATE TYPE, but a simpler approach is to use a CHECK constraint or a lookup table.
  • What is the maximum number of values in an ENUM? MySQL allows up to 65,535 values per ENUM column. However, practical limits are much lower for performance and maintainability.
  • Does ENUM save storage space? Yes, ENUM values are stored as integers (1, 2, 3…), which typically take less space than the string they represent. For example, ‘pending’ (7 bytes) stored as an integer (1 byte) saves space.
  • Can I add a value to an ENUM without rewriting the whole table? In MySQL, ALTER TABLE MODIFY will rebuild the table, which can be slow on large tables. There’s no way to add a value in-place in standard MySQL. In MariaDB, you can use ALTER TABLE … ADD ENUM VALUE in some versions, but it’s not universal.
  • What happens if I insert NULL into an ENUM column? If the column is NOT NULL, it will error. If it allows NULL, it will store NULL, which is fine. But if you insert an invalid value, it stores an empty string (non-strict) or errors (strict).
  • Can I use ENUM for booleans? You could, but MySQL has a BOOLEAN type (which is just TINYINT(1)). For clarity, use BOOLEAN or TINYINT for true/false, and reserve ENUM for multi-valued sets.

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.