I recently upgraded the character set of my MySQL database from utf8mb3 to utf8mb4. One of my table columns is defined as an ENUM(‘saved’, ‘updated’, ‘deleted’) NOT NULL DEFAULT ‘saved’.
However, after performing the charset upgrade, I noticed that the column definition changed unexpectedly to ENUM(‘saved’, ‘updated’, ‘deleted’) DEFAULT NULL, making it nullable with a default value of NULL.
It should retain the not null and default to saved
2
Answers
Thank you for the response. I have fixed this issue, the issue is that the script that i wrote is changing the columns to utf8mb4 but not retaining their old values for enum and default values
To cope with this issue, you will need to create a table with the new data format, so if anything goes wrong, you can recover your data without having to roll back or to run some backup. First, run
Now, you see your table’s definition, copy the
create table yourtable
command entirely into a text editor, change three things in this new script:yourtable
->yourtable_temp
id
and the field you are changingNow, run this script, having something like
create table yourtable_temp ...
And
insert
your data:Now
alter
your original table and make sure it looks likeAnd now check for your data. If it’s good, then you can
drop
your temporarily created table. If not, thencheck your data, it should be correct. Now you can drop your temporary table.