Using ENUM in MySQL 8: A Practical Guide (with Examples)

Updated: February 1, 2024 By: Guest Contributor Post a comment

Introduction

ENUM data type in MySQL is a useful feature that allows column values to be restricted to a certain set of predefined values. This can be beneficial when you know in advance all possible values a column can hold. It can help with data integrity and even performance in some cases. While ENUMs have their drawbacks and benefits, understanding when and how to use them can be a key skill for any database designer. This guide provides a practical look at ENUMs in MySQL 8 with examples to illustrate their usage.

Understanding ENUM Data Type

The ENUM data type stands for enumeration which is a set of predefined values and can be used as a column type. Its main purpose is to limit the possible values that can be inserted into a column to a fixed list specified during table creation. Here is an example:

CREATE TABLE shirts (
    size ENUM('small', 'medium', 'large') NOT NULL
);

Now, only ‘small’, ‘medium’, or ‘large’ can be inserted into the ‘size’ column. Anything outside of that defined set will cause an error or result in a special error value.

Creating ENUM Columns

When defining an ENUM column, values are provided as a comma-separated list:

CREATE TABLE pets (
    species ENUM('dog', 'cat', 'bird') NOT NULL
);

Inserting Data into ENUM Columns

While inserting data, you may refer to the index of the value within the ENUM list:

INSERT INTO shirts(size) VALUES('small'); -- By value
INSERT INTO shirts(size) VALUES(1); -- By index (same result)

However, using the numerical index of the ENUM values is generally not recommended as it reduces readability and can cause issues if the ENUM list changes.

Advantages of Using ENUM

  • Validation: MySQL itself ensures that only values defined in the ENUM list can be stored in the column.
  • Efficiency: ENUMs are stored very efficiently internally, especially when the list of possible values is short.
  • Easy to Read: ENUM values are easier to read and understand than some numerical codes or separate checker tables.

Disadvantages of Using ENUM

  • Flexibility: Adding or changing values requires altering the table’s structure using an ALTER TABLE statement.
  • Portability: ENUM is somewhat specific to MySQL and may not be easily portable to other database systems without some extra effort.
  • Sorting: ENUM columns are sorted according to their internal index, which may not be intuitive (often if the ENUM values might suggest another sort order).

Replacing ENUMs with Lookup Tables

Due to the flexibility issues with ENUM, an alternative approach involves creating a separate lookup table. Here is how you might do it:

CREATE TABLE shirt_sizes (
    id INT AUTO_INCREMENT PRIMARY KEY,
    size_name VARCHAR(10) NOT NULL
);

INSERT INTO shirt_sizes(size_name) VALUES('small'), ('medium'), ('large');

CREATE TABLE shirts (
    size_id INT NOT NULL,
    FOREIGN KEY(size_id) REFERENCES shirt_sizes(id)
);

Manipulating ENUM Values

You can query the numeric indexes of an ENUM column using the string values:

SELECT size+0 FROM shirts WHERE size = 'medium';

To find the string value from the numeric index:

SELECT ELT(2, 'small', 'medium', 'large');

When updating, you can simply set the column to a different value from within the defined list:

UPDATE shirts SET size = 'large' WHERE size = 'medium';

Note that ENUM values are case-insensitive.

Best Practices for Using ENUM

Although ENUM can be a powerful tool, it is important to use it wisely:

  • Use ENUMs when the list of values is short and unlikely to change often.
  • Avoid using numeric indexing for ENUMs to prevent confusion.
  • Consider alternative data modeling strategies, such as lookup tables, if the set does not seem to be rigid.

In conclusion, ENUMs in MySQL offer a controlled list of values for a column. Their use can offer benefits such as data integrity and space efficiency. However, they should be used considering drawbacks such as rigidity and limited portability. This practical guide should set a good foundation for understanding ENUMs and incorporating them into your MySQL designs where appropriate.