Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to rename a mysql table and column in a single query?

I’m writing a mysql workbench. I’d like to update a table and columns with a single query. Would I need to use a nested query or is what I’m trying to do not possible?

ALTER TABLE MyTable CHANGE id id2 int; // works

ALTER TABLE MyTable TO|RENAME MyTable2 CHANGE id id2 int; // fails

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Yes. You can make multiple changes in one ALTER TABLE statement.

The syntax reference shows:

ALTER TABLE tbl_name
    [alter_option [, alter_option] ...]
    [partition_options]

Note the optional additional alter_option separated by comma.

Demo:

mysql> create table mytable (id serial primary key, x int);
Query OK, 0 rows affected (0.01 sec)

mysql> alter table mytable rename column x to y, rename as myothertable;
Query OK, 0 rows affected (0.01 sec)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading