如何修改现有 MySQL 列的数据类型?
mysqlmysqli database更新于 2025/4/6 6:04:17
ALTER COMMAND 用于修改现有 MySQL 列的数据类型。以下示例演示了如何使用此命令修改列的数据类型 −
mysql> describe testing\G *************************** 1. row *************************** Field: id1 Type: int(11) Null: NO Key: PRI Default: 0 Extra: *************************** 2. row *************************** Field: name Type: char(30) Null: YES Key: Default: NULL Extra: 2 rows in set (0.05 sec)
从上面的 DESCRIBE 查询中,我们可以看到 Name 列的数据类型为 CHAR(30)。现在借助以下查询,我们可以将其更改为 VARCHAR(20) −
mysql> ALTER TABLE Testing MODIFY Name Varchar(20); Query OK, 4 rows affected (0.60 sec) Records: 4 Duplicates: 0 Warnings: 0 mysql> Describe Testing\G; *************************** 1. row *************************** Field: id1 Type: int(11) Null: NO Key: PRI Default: 0 Extra: *************************** 2. row *************************** Field: Name Type: varchar(20) Null: YES Key: Default: NULL Extra: 2 rows in set (0.15 sec)
现在数据类型已经修改为VARCHAR(20)。
相关文章
有用资源
mysql 参考教程 - 该教程包含有关 mysql 的更多信息:https://www.cainiaomax.com/mysql/

