Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL Change a column ENUM value

Tags:

enums

mysql

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?

like image 563
Sangram Anand Avatar asked Mar 26 '13 16:03

Sangram Anand


People also ask

Can you change the value of an enum?

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.

Is enum a constraint in MySQL?

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.

How does enum work in MySQL?

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.

How do you represent an enum and SET internally in MySQL?

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.


2 Answers

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,

  1. 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; 
  2. Set the old enum value to new value for all records.

    UPDATE `content` set `pagetype` = 'FEATURED_COVERAGE' where `pagetype` = 'NEWS'; 
  3. 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; 
like image 146
Abimaran Kugathasan Avatar answered Sep 19 '22 23:09

Abimaran Kugathasan


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 
like image 26
Vatev Avatar answered Sep 18 '22 23:09

Vatev