ENUM in MySQL
Last Updated :
17 May, 2024
An ENUM in MySQL is a string object whose value is decided from a set of permitted literals(Values) that are enumerated explicitly at the time of column creation.
ENUM is short for Enumeration, meaning it consists of a predefined list of values that a variable or column can take.
Using ENUM ensures data integrity by restricting the allowed values to the specified list. It uses numeric indexes (starting from 1) for representing string values.
Syntax
The syntax to define an ENUM column is:
CREATE TABLE table_name ( col… col ENUM (‘value_1′,’value_2′,’value_3’, ….), col… );
MySQL allows us to define the ENUM data type with the following three attributes:
- NOT NULL: If we don’t want NULL values, it is required to use the NOT NULL property in the ENUM column.
- NULL: It is a synonym for DEFAULT NULL, and its index value is always NULL.
- DEFAULT: By default, the ENUM data type is NULL, if the user doesn’t want to pass any value to it.
MySQL ENUM Example
Let’s look at an example of MySQL ENUM and learn how to use the ENUM column in MySQL.
Suppose, we want to store the student data in the table Student_grade in order to store the grades of students in the corresponding columns (High, Medium, Low). We use the priority statement to assign the priority to the Enum column.
CREATE TABLE Student_grade(
id INT PRIMARY KEY AUTO_INCREMENT, Grade VARCHAR(250) NOT NULL,
priority ENUM('Low', 'Medium', 'High') NOT NULL
);
The prioritized column will accept only three columns. Here, the order of numbering Low->1, Medium->2, High->3.
Insert data into the table
Insert a new row into the table named Student_grade, the statement is as follows –
INSERT INTO Student_grade(Grade, priority)
VALUES('Good grades', 'High');
Instead of using the enumeration values, you can also use the numerical indexes too, in order to insert the values into the Enum column of the table
INSERT INTO
Student_grade(Grade, priority),
VALUES
('Poor grades', 1);
Here we use 1 instead of using ‘Low’ enumeration value, since 1 is mapped to ‘Low’ implicitly.
Let’s add more rows into the table Student_grade
INSERT INTO Student_grade(Grade, priority)
VALUES('Mediocre grade', 'Medium');
INSERT INTO Student_grade(Grade)
VALUES('Poor grades',1);
INSERT INTO Student_grade(Grade)
VALUES('Good grades','High');
Note : ENUM column can also store NULL values if it is defined as a null-able column.
Query:
The following statement brought all the high grades student results
SELECT * FROM Student_grade
WHERE priority = 'High';
The same result you can get through this My SQL query
SELECT * FROM Student_grade
WHERE priority = 3;
Output:

Query
The query below selects the Student_grade and sorts them by the priority from High to Low –
SELECT Grade, priority FROM Student_grade
ORDER BY priority DESC;

Sort_data
MySQL ENUM Advantages
- ENUM values are stored as integers, which takes up less space compared to storing the full string values. It reduces the overall storage requirements and can lead to faster data retrieval.
- ENUM columns ensure data integrity by restricting the allowed values to a predefined set. This helps prevent invalid or incorrect data from being inserted into the column.
- ENUM values are represented by their corresponding string literals, making the data more readable and understandable for developers and users.
- Each ENUM value is automatically assigned an index number based on its position in the definition. This implicit indexing allows for efficient sorting and searching of ENUM values.
MySQL ENUM Disadvantages
- If you are thinking of modifying enumeration members then you need to rebuild the entire table using the ALTER TABLE command, which has quite overhead in terms of used resources and time.
- It is very complex to get the complete enumeration list because in that case, you need to access the inform_schema database
SELECT colmn_type FROM inform_schema
WHERE TABLE_NAME = 'Student_grade' AND COLUMN_NAME = 'priority';
- Porting it to other RDBMS could be a hard task because ENUM is not an SQL’s standard datatype and not many database systems provide support to it.
- It isn’t possible to insert more values to the enumerated column. Suppose, you are interested to insert a service-based agreement for every priority e.x., High (48hrs), Medium (4-3 days), Low (1 week), but it is not as easy as it looks and pragmatic with ENUM data type.
- Enumerated list is not reusable. Because if, you want to create a new table named “Emp-List” and interested to reuse its priority list, so it is not possible.
Important Points About MySQL ENUM
- The ENUM data type in MySQL is a string object that allows us to limit the value chosen from a list of permitted values in the column specification at the time of column creation.
- ENUM values are stored as integers (1, 2, 3, …) behind the scenes, which is more space-efficient than storing strings.
- MySQL sorts ENUM values based on their index numbers, not the actual strings.
- ENUM columns can have a maximum of 65,535 distinct elements.
- ENUM columns can accept values of various data types, including integers, floating-point numbers, decimals, and strings.
- If we want to modify the enumeration values/members, it can be done by rebuilding the entire table using the ALTER TABLE command.
- ENUM in MySQL is best suited for columns with a limited and unchanging set of values
Please Login to comment...