简体   繁体   中英

Alter column type of multiple columns

I'm trying to change type of multiple columns in a one table to varchar(50) (in a single SQL query). Can you please tell me what am I doing wrong, and why this query shows error.

ALTER TABLE 'my_table_name' 
    ALTER COLUMN 'col1' varchar(50), 
    ALTER COLUMN 'col2' varchar(50), 
    ALTER COLUMN 'col3' varchar(50)

Thank you!

Remove the single quotation marks and use MODIFY not CHANGE COLUMN

ALTER TABLE my_table_name 
    MODIFY col1 varchar(50), 
    MODIFY col2 varchar(50), 
    MODIFY col3 varchar(50)

You need to use MODIFY and get rid of the single quotes:

ALTER TABLE my_table_name 
    MODIFY col1 varchar(50), 
    MODIFY col2 varchar(50), 
    MODIFY col3 varchar(50)

The syntax you want is:

ALTER TABLE 'my_table_name'  
modify COLUMN 'col1' varchar(50),   
modify COLUMN 'col2' varchar(50),   
modify COLUMN 'col3' varchar(50)

Using CHANGE you would need to specify the new_col_name then col_definition :

ALTER TABLE `my_table_name` 
CHANGE COLUMN `col1` `col1` varchar(50),   
CHANGE COLUMN `col2` `col2` varchar(50),   
CHANGE COLUMN `col3` `col3` varchar(50)

Using MODIFY you only need specify the col_definition :

ALTER TABLE `my_table_name` 
MODIFY COLUMN `col1` varchar(50),   
MODIFY COLUMN `col2` varchar(50),   
MODIFY COLUMN `col3` varchar(50)

mysql_manual

If you want to change column definition only (without renaming), you can use this syntax:

ALTER TABLE 'my_table_name' 
    MODIFY 'col1' varchar(50)

else you must use this syntax:

ALTER TABLE 'my_table_name' 
    CHANGE 'col1old', 'col1new' varchar(50)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM