I have a MySQL table "content
" which has a column page_type
of type ENUM
. The ENUM
values are NEWS
& PRESS_RELEASE
. I need to replace NEWS
with FEATURED_COVERAGE
:
ALTER TABLE `content` CHANGE `pagetype` `pagetype` ENUM('FEATURED_COVERAGE','PRESS_RELEASE') CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL;
But now the records in the table, which earlier had page_type NEWS
are now empty, and there is no way that I can identify which records are NEWS
, so that I can rename those to FEATURED_COVERAGE
.
How to resolve such issues?
Enum constants are implicitly static and final and you can not change their value once created. Enum in Java provides type-safety and can be used inside switch statements like int variables.
ENUM constraintAn ENUM is a string object with a value chosen from a list of permitted values. They are enumerated explicitly in the column specification at table creation time.
ENUM values are sorted based on their index numbers, which depend on the order in which the enumeration members were listed in the column specification. For example, 'b' sorts before 'a' for ENUM('b', 'a') . The empty string sorts before nonempty strings, and NULL values sort before all other enumeration values.
MySQL stores ENUM string values internally as decimal integers of values 1 through n for a column with n members in the enumeration. MySQL represents SET string values as a bitmap using one bit per value, thus the values are stored internally as 1, 2, 4, 8, ..... up to 65,535 for a maximum of 64 members.
If I understand your question, you want to rename the existing enum value NEWS
to FEATURED_COVERAGE
. If so, you need to follow below steps,
Alter the table and add the new enum value to the column, so that you will have 3 enums
ALTER TABLE `content` CHANGE `pagetype` `pagetype` ENUM('FEATURED_COVERAGE','PRESS_RELEASE', 'NEWS') CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL;
Set the old enum value to new value for all records.
UPDATE `content` set `pagetype` = 'FEATURED_COVERAGE' where `pagetype` = 'NEWS';
Alter the table and drop the old enum value.
ALTER TABLE `content` CHANGE `pagetype` `pagetype` ENUM('FEATURED_COVERAGE','PRESS_RELEASE') CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL;
MySQL's enum always has a hidden option which is 0 as integer and '' as string. When you try to assign an invalid value it uses the hidden one.
Assuming all your empty values were 'NEWS' before the update you can change them with
UPDATE content SET pagetype = 'FEATURED_COVERAGE' WHERE pagetype = 0
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