开发者

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

开发者 https://www.devze.com 2023-04-06 10:00 出处:网络
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

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消