Changing Column in MySQL from int to double?

MySQLMySQLi Database

To change the column in MySQL from int to double you need to use ALTER TABLE command.

The syntax is as follows:

ALTER TABLE yourTableName modify column yourColumnName DOUBLE NOT NULL;

If you want NULL value then remove NOT NULL from the above syntax. The syntax is as follows:

ALTER TABLE yourTableName modify column yourColumnName DOUBLE;

To understand the above syntax, let us create a table. The query to create a table is as follows:

mysql> create table IntToDoubleDemo
   -> (
   -> Id int NOT NULL AUTO_INCREMENT,
   -> Name varchar(10),
   -> CylinderVolume int,
   -> PRIMARY KEY(Id)
   -> );
Query OK, 0 rows affected (0.63 sec)

Check the description of the table using DESC command. The syntax is as follows:

DESC yourTableName;

Apply the above query for your table to get the description of the table:

mysql> desc IntToDoubleDemo;

The following is the output:

+----------------+-------------+------+-----+---------+----------------+
| Field          | Type        | Null | Key | Default | Extra          |
+----------------+-------------+------+-----+---------+----------------+
| Id             | int(11)     | NO   | PRI | NULL    | auto_increment |
| Name           | varchar(10) | YES  |     | NULL    |                |
| CylinderVolume | int(11)     | YES  |     | NULL    |                |
+----------------+-------------+------+-----+---------+----------------+
3 rows in set (0.18 sec)

Look at the above sample output, the field ‘CylinderVolume’ is a type of int. Now you can convert from int to double.

Change the column in MySQL from int to double. The query is as follows:

mysql> alter table IntToDoubleDemo MODIFY COLUMN CylinderVolume double NOT NULL;
Query OK, 0 rows affected (2.79 sec)
Records: 0 Duplicates: 0 Warnings: 0

Once again check the description of the table. The query is as follows:

mysql> desc IntToDoubleDemo\G

The following is the output:

*************************** 1. row ***************************
Field: Id
Type: int(11)
Null: NO
Key: PRI
Default: NULL
Extra: auto_increment
*************************** 2. row ***************************
Field: Name
Type: varchar(10)
Null: YES
Key:
Default: NULL
Extra:
*************************** 3. row ***************************
Field: CylinderVolume
Type: double
Null: NO
Key:
Default: NULL
Extra:
3 rows in set (0.00 sec)
raja
Published on 26-Feb-2019 07:21:24
Advertisements