27

I'm working on a database. On most of the tables, the column order is not what I would expect, and I would like to change it (I have the permission). For example, the primary_key's id columns are rarely the first column!

Is there an easy method of moving columns with phpMyAdmin?

8 Answers 8

54

Use an ALTER TABLE ... MODIFY COLUMN statement.

ALTER TABLE table_name MODIFY COLUMN misplaced_column INT(11) AFTER other_column;
4
  • do i need the col type info? - INT(11)
    – Haroldo
    Nov 4, 2010 at 10:03
  • 1
    Yes, you need to specify the column type Nov 4, 2010 at 10:03
  • 1
    What if the misplaced_column is not of type INT(11). I am trying to reorder a DATETIME. I tried replacing "INT(11)" with "DATETIME" but it did not work.
    – skcin7
    Aug 22, 2012 at 22:42
  • Yeah I'm getting ERROR 1064 on ALTER TABLE participant COLUMN last_update DATETIME AFTER home_state ... but MODIFY worked stackoverflow.com/a/10256880/3018212 Feb 19, 2018 at 19:23
6

Here is the sql query
ALTER TABLE table_name MODIFY COLUMN misplaced_column Column-definition AFTER other_column; Here in Column-definition is full column definition. To see the column definition if you are using phpmyadmin click on structure tab. Then click on change link on desired column. Then withour modifyig any things click save. It will show you the sql. Copy the sql and just add *AFTER other_column* at the end. It will be all.

If you like to bring the *misplaced_column* to the first position then ALTER TABLE table_name MODIFY COLUMN misplaced_column Column-definition FIRST;

1
  • 2
    This is perfect and even allows for the movement of a datetime column. Thanks @zahid9i Jun 26, 2013 at 12:44
5

Since you mention phpMyAdmin, there is now a way to reorder columns in the most recent version (4.0 and up).

Go to the "Structure" view for a table, click the Edit (or Change) button on the appropriate field, then under "Move column" select where you would like the field to go.

3
  • There is no option like that.
    – Foreever
    Sep 19, 2014 at 10:47
  • @Foreever what version are you using? Sep 19, 2014 at 13:25
  • Well there's your problem. You need to download the latest version. Sep 22, 2014 at 14:56
4
ALTER TABLE `table`
CHANGE COLUMN `field` `field` 
INT(11) AFTER `field2`;
2

In phpMyAdmin version 3.5.5, go to the "Browse" tab, and drag the columns to your preferred location to reorder (e.g. if you have columns named A,B,C, all you need to do is drag column C between A and B to reorder it as A,C,B).

1

Another approach is to:

#CREATE TABLE original (
#    id INT
#    name TEXT
#    etc...
#);

CREATE TABLE temp (
    name TEXT
    id INT
    etc...
);

INSERT INTO temp SELECT name, id FROM original;

DROP TABLE original;

RENAME TABLE temp TO original;
7
  • Only if you want to do other modifications, such as types, names, order etc all in one go
    – Petah
    Nov 5, 2010 at 10:20
  • Why would you copy all your data around like this? When you don't need to? It's inefficient and you're just asking for trouble. Aug 14, 2011 at 14:18
  • @Tomalak, for example if you wanted to sync a schema from 2 different tables, but retain all the data. And these tables had multiple differences. This is how Toad works
    – Petah
    Aug 15, 2011 at 11:41
  • @Petah: OK, but he just wants to re-order his columns. That's it. That's all. Aug 15, 2011 at 11:50
  • 1
    @Tomalak, and Eran's answer satisfies that requirement. But for completeness and for anyone that stumbles across this question at a later date, my answer might be useful to them. Hence the reason I said "Another approach is..."
    – Petah
    Aug 15, 2011 at 21:05
0

SQL Maestro for MySQL offers tools to reorder fields as well with a GUI unfortunately it is not a drag and drop.

  1. Open table view
  2. Open Properties tab
  3. Click Reorder fields from the sidebar
  4. Click on the field you want moved and then click the up or down green arrows
  5. Click OK to submit database update

There are probably other programs and utilities to do this as well. I found this thread from a search so I thought I would share what I found for others.

0

Easy method for the newer version:

  1. Open the table you want to reorder.
  2. Go to structure tab.
  3. Chose move column link.
  4. Reorder the columns as you wish for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.