Home >Database >Mysql Tutorial >How do I modify the data type of an existing MySQL column?

How do I modify the data type of an existing MySQL column?

WBOY
WBOYforward
2023-08-23 15:17:02926browse

How do I modify the data type of an existing MySQL column?

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)

From the above DESCRIBE query, we can see that the data type of the Name column is CHAR(30). Now with the help of the following query we can change it to 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).

The above is the detailed content of How do I modify the data type of an existing MySQL column?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete