Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Migrating Varchar to Text in Mysql

I am converting one of the "Varchar" column in mysql table to a text field. I am simply using single alter command to convert the type. When i read about text and varchar came to know that it had difference in storing mechanisms.

Am I supposed to write any migration script to change the type of column and move data or a single type altering command is enough ?

like image 878
ted Avatar asked Jun 23 '16 11:06

ted


People also ask

Can I change VARCHAR to TEXT MySQL?

There is no a big problem to change varchar to text because text supports more data length than varchar , but if the field has a index it must be drop and create new index with prefix col_name(length) (see CREATE INDEX syntax).

Can you TEXT VARCHAR?

More Details. TEXT has a fixed max size of 2¹⁶-1 = 65535 characters. VARCHAR has a variable max size M up to M = 2¹⁶-1 . So you cannot choose the size of TEXT but you can for a VARCHAR .

Which is better VARCHAR or TEXT in MySQL?

In most circumstances, VARCHAR provides better performance, it's more flexible, and can be fully indexed. If you need to store longer strings, use MEDIUMTEXT or LONGTEXT, but be aware that very large amounts of data can be stored in columns of these types.

Is VARCHAR faster than TEXT?

Some Differences Between VARCHAR and TEXT A VARCHAR can be part of an index whereas a TEXT field requires you to specify a prefix length, which can be part of an index. VARCHAR is stored inline with the table (at least for the MyISAM storage engine), making it potentially faster when the size is reasonable.


2 Answers

ALTER TABLE table_name MODIFY column_name TEXT NOT NULL; 
like image 61
Shusen Yi Avatar answered Sep 29 '22 17:09

Shusen Yi


There is no a big problem to change varchar to text because text supports more data length than varchar, but if the field has a index it must be drop and create new index with prefix col_name(length) (see CREATE INDEX syntax).

According to your data content maybe would be a good idea use fulltext indexes but that implies change your search expressions on that field.

If you are in production environment, the table will be locked meanwhile the migration is in progress to prevent data loss.

like image 38
Ivan Cachicatari Avatar answered Sep 29 '22 16:09

Ivan Cachicatari