Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alter multiple columns in a single statement [duplicate]

I am using a query to alter the charset of a column

ALTER TABLE `media_value_report` 
    CHANGE `index_page_body` `index_page_body` TEXT CHARACTER  
    SET utf8 NULL DEFAULT NULL

i want to do this for other columns main_title, landing_page_body as well. But am getting a #1064 error while executing. Can i alter-change multiple columns in a single query?

I tried but i found in goog search that is not possible to alter in a single query.

like image 936
Sangram Anand Avatar asked Jun 01 '12 07:06

Sangram Anand


People also ask

How do you add multiple columns in a single ALTER statement?

First, you specify the table name after the ALTER TABLE clause. Second, you put the new column and its definition after the ADD COLUMN clause. Note that COLUMN keyword is optional so you can omit it. Third, MySQL allows you to add the new column as the first column of the table by specifying the FIRST keyword.

Can we modify multiple columns in SQL?

In SQL, sometimes we need to write a single query to update the values of all columns in a table. We will use the UPDATE keyword to achieve this.

Can we modify more than one column using a single ALTER TABLE?

It is not possible to do multiple ALTER column for a table.

How do I change multiple columns in MySQL?

MySQL UPDATE multiple columnsMySQL UPDATE command can be used to update multiple columns by specifying a comma separated list of column_name = new_value. Where column_name is the name of the column to be updated and new_value is the new value with which the column will be updated.


1 Answers

The documentation suggests you can chain alter_specifications with a comma:

ALTER TABLE `media_value_report` 
    CHANGE col1_old col1_new varchar(10),
    CHANGE col1_old col1_new varchar(10),
    ...
like image 134
Andomar Avatar answered Sep 25 '22 02:09

Andomar