Add a New Value to ENUM Column in MySQL



In MySQL, the ENUM is a data type that is used to create a column with a fixed set of values. It is helpful for the fields that have limited alternatives, such as categories, statuses, and color codes.

However, as your application evolves, you might need to add more values to the list such as new colors. MySQL allows us to modify ENUM values without dropping and recreating the column.

Adding New Value to an ENUM column

For adding new values in an ENUM column in MySQL, you shall use the ALTER TABLE command with the MODIFY Keyword. For an alteration of an ENUM column, you must not only specify new values but also existing values.

Syntax

Following is the syntax to add a new value to the column of ENUM data type.

ALTER TABLE table_name
MODIFY COLUMN column_name 
ENUM('val1','val2',........N,'neval1','newval2',.....N);

Example

Adding new values to an ENUM column

Creating Table

To understand the above syntax, let us create a table. The following query creates a table named ColorsTable with an ENUM column 'Colors' containing the initial values 'RED', 'GREEN', and 'BLUE'. ?

CREATE TABLE ColorsTable (
   Id int NOT NULL AUTO_INCREMENT,
   Colors ENUM('RED','GREEN','BLUE'),
   PRIMARY KEY(Id)
);
View the Structure

To check the structure of the above table, or to check the current values in the ENUM column use the SHOW CREATE command. The query is as follows ?

SHOW CREATE TABLE ColorsTable\G

Following is the output of the above code ?

mysql> SHOW CREATE TABLE ColorsTable\G
*************************** 1. row ***************************
       Table: ColorsTable
Create Table: CREATE TABLE `colorstable` (
  `Id` int NOT NULL AUTO_INCREMENT,
  `Colors` enum('RED','GREEN','BLUE') DEFAULT NULL,
  PRIMARY KEY (`Id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci

Look at the enum column ?Colors' with the value 'RED', 'GREEN', 'BLUE' and if you want to add another value, then use the ALTER command as discussed above.

Adding New Values

Now, let us modify the ENUM column by adding new values i.e. 'YELLOW', 'ORANGE', 'PINK'. Following is the query to add a new value to enum column ?Colors' ?

ALTER TABLE ColorsTable 
MODIFY COLUMN Colors 
ENUM ('RED','GREEN','BLUE','YELLOW','ORANGE','PINK');
Verifying the changes

Check the description of the table to ensure that the values are added or not. We use DESC command as shown below -

DESC yourTableName;

To understand the above syntax let us describe the table ?ColorsTable' using DESC ?

DESC ColorsTable;

Following is the output of the above query ?

Field Type Null Key Default Extra
Id int NO PRI NULL auto_increment
Colors enum('RED','GREEN','BLUE','YELLOW','ORANGE',' PINK') YES NULL

Conclusion

We are going to conclude that adding new values to an ENUM column in MySQL is easy but requires careful use of the ALTER TABLE command. You can extend the ENUM without affecting the current data by specifying all existing values as well as adding new ones. Pay close attention to this because the ENUM column directly affects data validation on your table.

Updated on: 2025-01-22T16:14:27+05:30

24K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements