Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> MySQL

เปลี่ยนชนิดข้อมูลคอลัมน์จาก INT เป็น DOUBLE ใน 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)

ตรวจสอบโครงสร้างตาราง:

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 ตอนนี้เปลี่ยนเป็น 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

ผลลัพธ์:

*************************** 3. row *************************** Field: CylinderVolume Type: double Null: NO Key: Default: NULL Extra: 3 rows in set (0.00 sec)

คอลัมน์ CylinderVolume เปลี่ยนเป็นชนิด double เรียบร้อยแล้ว