MySQL Enumeration Index Value



In MySQL, the ENUM data type enables you to define a column using only a collection of predetermined values. Each value in the ENUM list is assigned a position number known as an index (which begins with 1). These index numbers represent the positions of the values in the list, not the actual data.

For example, if the ENUM list is ('Male', 'Female', 'Other'), Male has an index of 1 while Female has an index of 2 and Other will have an index of 3. This further enables MySQL to store and compare the values more effectively, though actual values are visible in a query on the table.

Understanding ENUM Index Values

Each value in an ENUM column is assigned an index number based on its order in the ENUM list.

  • The first ENUM value gets the index 1.
  • The second value gets the index 2 and so on.
  • The empty value in the ENUM list has an index of 0.
  • The NULL has no index and it is represented as NULL.

Case 1: Inserting ENUM Values Using Index Numbers

We can insert the values in ENUM column with the help of their index numbers rather than writing the values. For example, we have two ENUM values ?pass' and ?fail'. As ?pass' is written first in the enumeration list hence got index number ?1' and it would be ?2 for ?fail'.

Creating an ENUM table

To begin, let's create a table named "Marks" with an ENUM column 'Result' having two values: 'Pass' and 'Fail'.

CREATE TABLE Marks ( ID INT PRIMARY KEY, Name VARCHAR(50), Result ENUM('Pass', 'Fail') );

From the above, query we can understand that the table stores the student name, ID, and their result.

Let us populate the table "Marks" with the sample data using the INSERT command.

INSERT INTO Marks(ID,Name,Result) Values(101,'Aarav','1'), (102,'Yashraj','2');
Verification

To fetch the data we use select statement.

SELECT * FROM Marks;

The contents of the "Marks" table will be as follows ?

ID Name Result
101 Aarav Pass
102 Yashraj Fail

In the queries above, we have used index numbers 1 and 2 for enumeration values "Pass" and "Fail" respectively.

Case 2: Inserting an Empty String in ENUM Column

The index value of the empty string is 0. Before inserting the empty string, the SQL mode must not be TRADITIONAL, STRICT_TRANS_TABLES, or STRICT_ALL_TABLES because it does not allow us to enter empty strings, if we enter it will throw an error. With the help of the following example, we can understand it ?

SET SESSION sql_mode = '';

For this example, let us consider the 'Marks' table that we have created for case 1.

Inserting an empty string into the table named 'Marks'. We usually use empty strings to handle invalid or unknown values in ENUM columns.

Insert into Marks(Id, Name, Result) values(103,'Daksh','');

The query above will insert the empty string at the place of an enumeration value. MySQL inserts the empty string with the following warning.

Show warnings;

Following is the output of the above code ?

Level Code Message
Warning 1265 Data truncated for column 'Result' at row 1

Now when we check the "Marks" table, we can see an empty string in ENUM field.

ID Name Result
101 Aarav Pass
102 Yashraj Fail
103 Daksh
Verification

To check the index value of empty string we use the following query -

SELECT result+0 As result_index from Marks;

Following is the output of the above code ;

result_index
1
2
0

From the output of above query, it is clear that the index value of the empty string is 0.

Case 3: Inserting NULL into the ENUM Column

We can insert NULL at the place of enumeration values when we do not specify a NOT NULL constraint in an ENUM column. The index value of NULL is NULL. Let us understand with an example where we insert NULL in ENUM column of table ?Result' and can check its index value.

Creating tables

To begin, first let us create a table called "Result". Following is the query to create a table -

CREATE TABLE Result ( ID INT PRIMARY KEY NOT NULL, Name VARCHAR(10), Grade ENUM('POOR', 'GOOD') );

Now let us insert null value into the ENUM column to check the index value.

INSERT INTO Result(ID, Name, Grade) values(100, 'Rahul', NULL);

The query above inserts the value NULL at the place of enumeration value which can be checked with the help query below using SELECT statement?

SELECT * FROM Result;

The contents of the "Result" table will be as follows ?

ID Name Grade
100 Rahul NULL
Verification

Now, with the help of next query, we can observe that the index value of NULL is NULL.

SELECT Grade+0 As Grade_index from result;

Following is the output of the above code -

Grade_index
NULL

Understanding these techniques is essential for handling ENUM data effectively, especially when managing large datasets with predefined values.

Updated on: 2025-02-04T16:18:36+05:30

458 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements