Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> MySQL

Thay đổi cột từ INT sang DOUBLE trong MySQL

Để đổi kiểu cột từ INT sang DOUBLE, dùng lệnh ALTER TABLE.

Cú pháp:

ALTER TABLE yourTableName modify column yourColumnName DOUBLE NOT NULL;

Nếu cho phép NULL, bỏ NOT NULL:

ALTER TABLE yourTableName modify column yourColumnName DOUBLE;

Tạo bảng demo:

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)

Kiểm tra cấu trúc bảng:

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)

Thay đổi kiểu cột CylinderVolume sang 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

Kiểm tra lại cấu trúc:

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)