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
Release: 2023-08-23 15:17:02
forward
914 people have browsed it

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)
Copy after login

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)
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template