MySQL 如何修改现有的MySQL列的数据类型?
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查询中,我们可以看到名称列的数据类型为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 教程