708

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.

1
  • 10
    Just to make this explicit, the answers below (using ALTER TABLE) will, in fact, work even if the column already contains data. However, converting a float column into an integer column will cause any non-integer values in it to be rounded to the nearest integer. Commented Nov 10, 2017 at 20:49

9 Answers 9

1292

http://dev.mysql.com/doc/refman/5.1/en/alter-table.html

ALTER TABLE tablename MODIFY columnname INTEGER;

This will change the datatype of given column

Depending on how many columns you wish to modify it might be best to generate a script, or use some kind of mysql client GUI

Sign up to request clarification or add additional context in comments.

4 Comments

Friendly reminder - The default for columns is NULLABLE, so if you have a NOT NULL column, don't forget to use "MODIFY columnname INTEGER NOT NULL" or else you will change your column from NOT NULL to NULL.
Will alter table delete the data on the table or fail on the execution if something is not complaint with the new column type?
ALTER TABLE tablename MODIFY columnname INTEGER unsigned; <-- if you care about the new column being unsigned. Was my case.
I think @Despertars warning might also be relevant for keeping any specifications of CHARSET or COLLATE.
56
alter table table_name modify column_name int(5)

Comments

47

You can also use this:

ALTER TABLE [tablename] CHANGE [columnName] [columnName] DECIMAL (10,2)

Comments

19

If you want to change all columns of a certain type to another type, you can generate queries using a query like this:

select distinct concat('alter table ',
                       table_name,
                       ' modify ',
                       column_name,
                       ' <new datatype> ',
                       if(is_nullable = 'NO', ' NOT ', ''),
                       ' NULL;')
  from information_schema.columns
  where table_schema = '<your database>' 
    and column_type = '<old datatype>';

For instance, if you want to change columns from tinyint(4) to bit(1), run it like this:

select distinct concat('alter table ',
                       table_name,
                       ' modify ',
                       column_name,
                       ' bit(1) ',
                       if(is_nullable = 'NO', ' NOT ', ''),
                       ' NULL;')
  from information_schema.columns
  where table_schema = 'MyDatabase' 
    and column_type = 'tinyint(4)';

and get an output like this:

alter table table1 modify finished bit(1)  NOT  NULL;
alter table table2 modify canItBeTrue bit(1)  NOT  NULL;
alter table table3 modify canBeNull bit(1)  NULL;

!! Does not keep unique constraints, but should be easily fixed with another if-parameter to concat. I'll leave it up to the reader to implement that if needed..

Comments

13
Alter TABLE `tableName` MODIFY COLUMN `ColumnName` datatype(length);

Ex :

Alter TABLE `tbl_users` MODIFY COLUMN `dup` VARCHAR(120);

Comments

11

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]' 

Comments

8

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);

Comments

6

You use the alter table ... change ... method, for example:

mysql> create table yar (id int);
Query OK, 0 rows affected (0.01 sec)

mysql> insert into yar values(5);
Query OK, 1 row affected (0.01 sec)

mysql> alter table yar change id id varchar(255);
Query OK, 1 row affected (0.03 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> desc yar;
+-------+--------------+------+-----+---------+-------+
| Field | Type         | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id    | varchar(255) | YES  |     | NULL    |       |
+-------+--------------+------+-----+---------+-------+
1 row in set (0.00 sec)

1 Comment

I believe it's MODIFY rather than change, but maybe both works.
1

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)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.