Changing the Value of a MySQL ENUM Value, Throughout a Table
I'm wondering if it is possibl开发者_StackOverflowe 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.
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.
精彩评论