How can I modify an existing MySQL column’s data type?



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 above DESCRIBE query, we can see that the data type of Name column in 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).


Advertisements