我如何修改现有的MySQL列的数据类型?

数据库数据库 2023-08-31 03:46:34 692
摘要: ALTERCOMMANDisusedtomodifyanexistingMySQLcolumn’sdatatype.Followingisanexamplewhichdemonstratesthathowwecanusethiscommandtomodifycolumn’sdatatype−mysql>...

我如何修改现有的MySQL列的数据类型?

ALTER COMMAND is used to modify an existing MySQL column’s data type. Following is an example which demonstrates that how we can use this command to modify column’s data type −

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)

Now the data type has been modified to VARCHAR(20).

以上就是我如何修改现有的MySQL列的数据类型?的详细内容,更多请关注其它相关文章!