mysql

MySQL Query to change column type

If you want to change the data type of a column in a MySQL table, you need to use the ALTER TABLE statement. To do this, you need to specify the name of the table, the name of the column, and the new data type for that column. For example, if you want to change the data type of the column username to VARCHAR(255), you would use the following statement:

ALTER TABLE users
MODIFY COLUMN username varchar(255);

The ALTER TABLE query above is used to modify the column username in the users' table to have a data type of varchar(255).

The basic syntax to change the column type in MySQL will be as below.

ALTER TABLE table_name
MODIFY COLUMN column_name TYPE

To change a column to TEXT data type we can use the below MySQL query.

ALTER TABLE employees
MODIFY COLUMN about_employee TEXT;

Change data types of multiple columns MySQL Query

This query is used to change the data type for multiple columns in a MySQL database.

ALTER TABLE table_name    
MODIFY COLUMN column_name_1 TYPE,  
MODIFY COLUMN column_name_2 TYPE;  

In the above MySQL query:

1. The ALTER TABLE keyword is used to change the structure of an existing table.

2. The MODIFY COLUMN keyword is used to change the data type of an existing column.

3. In this query, the data type of column_name_1 and column_name_2 is being changed.

MySQL query example

ALTER TABLE users  
MODIFY COLUMN user_id INT(10),  
MODIFY COLUMN user_email VARCHAR(50);  
Was this helpful?