MonOve
MonOve

Reputation: 1111

Varchar to eNum

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?

Upvotes: 14

Views: 10685

Answers (2)

Fredrik Erlandsson
Fredrik Erlandsson

Reputation: 1527

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'.

Upvotes: 22

heisenberg
heisenberg

Reputation: 9759

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

Upvotes: 13

Related Questions