How do I change the data type for a column in MySQL?

Viewed 828424

I want to change the data type of multiple columns from float to int. What is the simplest way to do this?

There is no data to worry about, yet.

9 Answers

To change column data type there are change method and modify method

ALTER TABLE student_info CHANGE roll_no roll_no VARCHAR(255);

ALTER TABLE student_info MODIFY roll_no VARCHAR(255);

To change the field name also use the change method

ALTER TABLE student_info CHANGE roll_no identity_no VARCHAR(255);

https://dev.mysql.com/doc/refman/8.0/en/alter-table.html

You can also set a default value for the column just add the DEFAULT keyword followed by the value.

ALTER TABLE [table_name] MODIFY [column_name] [NEW DATA TYPE] DEFAULT [VALUE];

This is also working for MariaDB (tested version 10.2)

If you want to alter the column details, set default value and add a comment, use this

ALTER TABLE [table_name] MODIFY [column_name] [new data type] 
DEFAULT [VALUE] COMMENT '[column comment]' 
Related