The Wayback Machine - https://web.archive.org/web/20240614165652/https://www.geeksforgeeks.org/enumerator-enum-in-mysql/
Open In App

ENUM in MySQL

Last Updated : 17 May, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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:

mysql enum example 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 

Similar Reads

MySQL | Common MySQL Queries
MySQL server is a open-source relational database management system which is a major support for web based applications. Databases and related tables are the main component of many websites and applications as the data is stored and exchanged over the web. Even all social networking websites mainly Facebook, Twitter, and Google depends on MySQL dat
9 min read
MySQL | LEAD() and LAG() Function
The LEAD() and LAG() function in MySQL are used to get preceding and succeeding value of any row within its partition. These functions are termed as nonaggregate Window functions. The Window functions are those functions which perform operations for each row of the partition or window. These functions produce the result for each query row unlikely
3 min read
PHP | MySQL UPDATE Query
The MySQL UPDATE query is used to update existing records in a table in a MySQL database. It can be used to update one or more field at the same time. It can be used to specify any condition using the WHERE clause. Syntax : The basic syntax of the Update Query is - Implementation of Where Update Query : Let us consider the following table "Data" wi
2 min read
PHP | MySQL Database Introduction
What is MySQL? MySQL is an open-source relational database management system (RDBMS). It is the most popular database system used with PHP. MySQL is developed, distributed, and supported by Oracle Corporation. The data in a MySQL database are stored in tables which consists of columns and rows.MySQL is a database system that runs on a server.MySQL
4 min read
PHP | MySQL ( Creating Database )
What is a database? Database is a collection of inter-related data which helps in efficient retrieval, insertion and deletion of data from database and organizes the data in the form of tables, views, schemas, reports etc. For Example, university database organizes the data about students, faculty, and admin staff etc. which helps in efficient retr
3 min read
PHP | MySQL ( Creating Table )
What is a table? In relational databases, and flat file databases, a table is a set of data elements using a model of vertical columns and horizontal rows, the cell being the unit where a row and column intersect. A table has a specified number of columns, but can have any number of rows. Creating a MySQL Table Using MySQLi and PDO We have already
3 min read
PHP | Inserting into MySQL database
Prerequisites : MySQL introduction, Creating MySQL database INSERT INTO statement is used to insert new rows in a database table. Let's see the syntax how to insert into table, considering database already exists. SYNTAX : INSERT INTO TABLE_NAME (column1, column2, column3, ... columnN) VALUES (value1, value2, value3, ...valueN); Here, column1, colu
4 min read
PHP | MySQL Select Query
The SQL SELECT statement is used to select the records from database tables. Syntax : The basic syntax of the select clause is - To select all columns from the table, the [TEX] * [/TEX] character is used. Implementation of the Select Query : Let us consider the following table ' Data ' with three columns ' FirstName ', ' LastName ' and ' Age '. To
3 min read
PHP | MySQL Delete Query
The DELETE query is used to delete records from a database table. It is generally used along with the "Select" statement to delete only those records that satisfy a specific condition. Syntax : The basic syntax of the Delete Query is - Let us consider the following table "Data" with four columns ' ID ', ' FirstName ', ' LastName ' and ' Age '. To d
2 min read
PHP | MySQL ORDER BY Clause
The ORDER BY Clause can be used along with the SELECT statement to sort the data of specific fields in an ordered way. It is used to sort the result-set in ascending or descending order. Syntax : The basic syntax of the Order By clause is - Implementation of the Order By Clause : Let us consider the following table "Data" with three columns 'FirstN
3 min read
Article Tags :