
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 TableTo 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 ValuesNow, 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.