Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing the Value of a MySQL ENUM Value, Throughout a Table

I'm wondering if it is possible to change an ENUM value throughout a table, so that in all the rows where said ENUM value is represented, the change is made as well.

like image 704
stefmikhail Avatar asked Sep 22 '11 05:09

stefmikhail


People also ask

How can I change enum value in MySQL?

You can add a new value to a column of data type enum using ALTER MODIFY command. If you want the existing value of enum, then you need to manually write the existing enum value at the time of adding a new value to column of data type enum.

Can you change the value of an enum?

Enum constants are implicitly static and final and you can not change their value once created.

How can I get enum possible values in a MySQL database?

$syntax = mysql_query("SELECT COLUMN_TYPY FROM information_schema. `COLUMNS` WHERE TABLE_NAME = '$table' AND COLUMN_NAME ='$colm'"); if (! mysql_error()){ //Get a array possible values from table and colm.

What is correct usage of enum in MySQL?

The ENUM data type in MySQL is a string object. It allows us to limit the value chosen from a list of permitted values in the column specification at the time of table creation. It is short for enumeration, which means that each column may have one of the specified possible values.


1 Answers

If you want to change the value of an enum:

Suppose your old enum was:

ENUM('English', 'Spanish', 'Frenchdghgshd', 'Chinese', 'German', 'Japanese') 

To change that use:

--  Add a new enum value
ALTER TABLE `tablename` CHANGE `fieldname` `fieldname` ENUM   
('English', 'Spanish', 'Frenchdghgshd', 'Chinese', 'German', 'Japanese', 'French');
-- Update the table to change all the values around.
UPDATE tablename SET fieldname = 'French' WHERE fieldname = 'Frenchdghgshd';
-- Remove the wrong enum from the definition
ALTER TABLE `tablename` CHANGE `fieldname` `fieldname` ENUM   
('English', 'Spanish', 'Chinese', 'German', 'Japanese', 'French');

MySQL will probably go through all the rows in your table trying to update stuff, I've heard stories of a planned optimization around that, but I'm not sure if that actually happened.

like image 58
Johan Avatar answered Oct 29 '22 09:10

Johan