Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

I have a table with a SET column type that need to be changed to ENUM type. Can I do it with a simple ALTER or will this mess up my data? NB: there are no multiple values (a,b,c) in the table.

share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

Technicaly YES, I just did the test :

mysql> CREATE TABLE my_t (my_c SET("one","two"));
Query OK, 0 rows affected (0.44 sec)

mysql> INSERT INTO my_t VALUES ("one");
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO my_t VALUES ("one");
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO my_t VALUES ("one");
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO my_t VALUES ("two");
Query OK, 1 row affected (0.00 sec)

mysql> SELECT * FROM my_t;
+------+
| my_c |
+------+
| one  |
| one  |
| one  |
| two  |
+------+
4 rows in set (0.00 sec)

mysql> ALTER TABLE my_t MODIFY my_c ENUM("one","two");
Query OK, 4 rows affected (1.13 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> SELECT * FROM my_t;
+------+
| my_c |
+------+
| one  |
| one  |
| one  |
| two  |
+------+
4 rows in set (0.00 sec)

You've mentioned it but it will mess if you have multiple values (replace by an empty set in ENUM):

mysql> CREATE TABLE my_t (my_c SET("one","two"));
Query OK, 0 rows affected (0.15 sec)

mysql> INSERT INTO my_t VALUES ("two,one");
Query OK, 1 row affected (0.00 sec)

mysql> SELECT * FROM my_t;
+---------+
| my_c    |
+---------+
| one,two |
+---------+
1 row in set (0.00 sec)

mysql> ALTER TABLE my_t MODIFY my_c ENUM("one","two");
Query OK, 1 row affected, 1 warning (0.87 sec)
Records: 1  Duplicates: 0  Warnings: 1

mysql> SELECT * FROM my_t;
+------+
| my_c |
+------+
|      |
+------+
1 row in set (0.00 sec)

Max.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.