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.
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.
Enum constants are implicitly static and final and you can not change their value once created.
$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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With