Field enum vs varchar, envelope in mysql
-
There's a table that has a field like
enum
There was a challenge to change the field and decided to make it work.varchar
♪When attempting to change the field type, all the values recorded in the table in this field are replaced by void and the values were assumed to remain.
Is that what I'm doing wrong, or is there a direct change like this?
-
inside a pole type enum The numbers are not stored, but the numbers that demonstrate well the second request set out below.
a The lines are stored in the table description (see request number three).
The example of " conversion " is also given (a non-necessary column can be deleted and renamed):
alter table t add column v text; update t set v=e;
The results are visible in the fourth request.
p.s. I used the type. textNot varcharthat there is no role in this case.
http://sqlfiddle.com/#!9/5ccf1/2
MySQL 5.6 Schema Setup:
create table t (e enum('раз','два','три'));
insert into t values
('три')
,('раз')
,('два')
,(1)
;alter table t add column v text;
update t set v=e;
Query 1:
select e from t
http://sqlfiddle.com/#!9/5ccf1/2/0 :
| e |
|-----|
| три |
| раз |
| два |
| раз |
Query 2:
select e+0 from t
http://sqlfiddle.com/#!9/5ccf1/2/1 :
| e+0 |
|-----|
| 3 |
| 1 |
| 2 |
| 1 |
Query 3:
show columns from t
http://sqlfiddle.com/#!9/5ccf1/2/2 :
| COLUMN_NAME | COLUMN_TYPE | IS_NULLABLE | COLUMN_KEY | COLUMN_DEFAULT | EXTRA |
|-------------|-------------------------|-------------|------------|----------------|-------|
| e | enum('раз','два','три') | YES | | (null) | |
| v | text | YES | | (null) | |
Query 4:
select * from t
http://sqlfiddle.com/#!9/5ccf1/2/3 :
| e | v |
|-----|-----|
| три | три |
| раз | раз |
| два | два |
| раз | раз |