How to move a column in MySQL

Move a column after some other column

mysql> ALTER TABLE mytable MODIFY COLUMN mycolumn INT AFTER someothercolumn;

Move a column to the first position

mysql> ALTER TABLE mytable MODIFY COLUMN mycolumn INT FIRST;


This entry was posted in Uncategorized. Bookmark the permalink.

7 Responses to How to move a column in MySQL

  1. Tom Buchok says:

    Thanks a lot for this. Exactly what I needed.

  2. Paul says:

    Thanks!

    So basically, if your column is of a type other than INT, you’ll need to remake it the way it was AFTER you move it. For ENUMs, copy-paste :)

    • Liviu S says:

      Actually it could be done in the same query. For example:
      ALTER TABLE mytable MODIFY COLUMN mycolumn VARCHAR(1000) AFTER someothercolumn;

      If you have data in the column you intend to move (varchar for example) you (may) lose it completely when moving and changing to INT.

      Thanks Masao :)

  3. TeamOneJ says:

    More accurately stated…

    Move a column after some other column

    mysql> ALTER TABLE `mytable` MODIFY COLUMN `mycolumn` [mycolumn definition] AFTER `someothercolumn`;

    Move a column to the first position

    mysql> ALTER TABLE `mytable` MODIFY COLUMN `mycolumn` [mycolumn definition] FIRST;

    ALTER TABLE Syntax

  4. Fitz says:

    Thanks very much bro!

  5. siba prasad says:

    THANK U MASAO…………..

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>