Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Varchar to eNum

Tags:

mysql

I would like to convert a column in mysql from varchar to enum. If my new enum values include the existing values, will I lose existing values in that column?

like image 353
MonOve Avatar asked Jul 29 '11 18:07

MonOve


2 Answers

For copy paste junkies like myself, given the following output from PROCEDURE ANALYSE()

SELECT status FROM post PROCEDURE ANALYSE()\G
*************************** 1. row ***************************
             Field_name: crawling.post.status
              Min_value: done
              Max_value: pulled
             Min_length: 3
             Max_length: 6
       Empties_or_zeros: 0
                  Nulls: 0
Avg_value_or_avg_length: 3.7880
                    Std: NULL
      Optimal_fieldtype: ENUM('done','new','pulled') NOT NULL

And using the following command to convert my status column to enum:

ALTER TABLE post MODIFY COLUMN status ENUM('done', 'new', 'pulled') DEFAULT 'new';

It's ok to skip the DEFAULT 'new'.

like image 91
Fredrik Erlandsson Avatar answered Oct 14 '22 23:10

Fredrik Erlandsson


No, if the values are included in the enum you won't lose data.

like image 27
heisenberg Avatar answered Oct 14 '22 22:10

heisenberg