How to convert MySQL field types?

I have already come across a conversion function. As far as I understand, the basic syntax is:

select convert(columnName, targetFieldType) as newColumnName from table;

Running this command does not give me any errors, but when I check the data types, they do not change. Even when I use commit; the data remains unchanged. In particular, I am trying to convert data with a long type to varchar. Any ideas?

+3
source share
2 answers

This query SELECTreturns a value in a new type, but does not change the type of the table field in your database.

If you want to constantly change the table on disk, use:

ALTER TABLE table CHANGE columnName newColumnName targetFieldType NOT NULL;

Please note that for large tables this may take some time because MySQL rewrites the entire table.

: NOT NULL, NULL .

. ALTER TABLE MySQL.

+4

, - CONCAT:

select concat(columnName,'') as newColumnName from table;

, CONVERT, :

select convert(columnName, char) as newColumnName from table;

varchar, CONVERT CAST, char , .

+1

Source: https://habr.com/ru/post/1723001/


All Articles