将 MySQL 中的列从 int 更改为 double?
mysqlmysqli database
要将 MySQL 中的列从 int 更改为 double,您需要使用 ALTER TABLE 命令。
语法如下:
ALTER TABLE yourTableName modify column yourColumnName DOUBLE NOT NULL;
如果您想要 NULL 值,请从上述语法中删除 NOT NULL。语法如下:
ALTER TABLE yourTableName modify column yourColumnName DOUBLE;
为了理解上述语法,让我们创建一个表。创建表的查询如下:
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)
使用 DESC 命令查看表的描述。语法如下:
DESC yourTableName;
对您的表应用上述查询以获取表的描述:
mysql> desc IntToDoubleDemo;
输出结果如下:
+----------------+-------------+------+-----+---------+----------------+ | 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)
查看上面的示例输出,字段"CylinderVolume"是 int 类型。现在您可以从 int 转换为 double。
将 MySQL 中的列从 int 更改为 double。查询如下:
mysql> alter table IntToDoubleDemo MODIFY COLUMN CylinderVolume double NOT NULL; Query OK, 0 rows affected (2.79 sec) Records: 0 Duplicates: 0 Warnings: 0
再次检查表的描述。查询如下:
mysql> desc IntToDoubleDemo\G
输出结果如下:
*************************** 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)