57

Any help on re-ordering the columns in MySQL using phpMyAdmin? Is it called cardinality? I have created tables, but need to re-arrange the order of the columns due to an export script i have. It exports based on the arrangements. E.g. I want columns:

Apple | Cherry | Banana

changed to:

Apple | Banana | Cherry
Bob Stein
  • 16,271
  • 10
  • 88
  • 101
karto
  • 3,538
  • 8
  • 43
  • 68

5 Answers5

70

phpMyAdmin has finally included this feature in the most recent version (4.0 and up).

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

ajcw
  • 23,604
  • 6
  • 30
  • 47
DisgruntledGoat
  • 70,219
  • 68
  • 205
  • 290
43

Use the ALTER TABLE with MODIFY COLUMN command. Something like:

ALTER TABLE foo MODIFY COLUMN Hobby VARCHAR(20) FIRST;

I don't know whether or not there's a GUI way to do it in phpmyadmin, but normal SQL queries should work, too.

King Skippus
  • 3,801
  • 1
  • 24
  • 24
  • ↓↓↓ There now is a very very easy GUI solution to this in phpMyAdmin now! (As I noted in my answer below ↓↓↓. 7 YEARS ago!!! ↓↓↓) I'm just adding this as this is a LONG lived question that people are STILL coming to this and phpMyAdmin makes it very very easy to reorder columns now. **(PS I hope this is considered OK. If this is bad form please delete this message. *No disrespect meant to @King Skippus who gave an excellent answer in 2011.*)** – BeNice Aug 14 '22 at 12:08
42

OP asked how to change column order in phpMyAdmin.

NB: phpMyAdmin keeps making changes but, as of Nov 2019, this is still correct.

1) Click on "Structure".

enter image description here

2) Next click on "Move columns" at the bottom.

enter image description here

3) and voila just drag and drop! (Very modern for dear old myPhpAdmin!)

enter image description here

BeNice
  • 2,165
  • 2
  • 23
  • 38
  • 2
    The move column feature wasn't available yet at the time of the question. Great they brought it. – karto Sep 30 '15 at 11:48
  • 1
    Yep I just added that in case another poor soul in trouble like me happened by. That is why I called it the ever changing answer. Lots has changed since the OP. Oh and I added a pic cause I could not see the damned thing. – BeNice Jan 11 '16 at 03:22
  • 1
    Thanks, @BeNice for this! I didn't see that option. – shenn Feb 08 '18 at 20:03
22

To reorder columns, pop-up a query window and use the statement:

ALTER TABLE ... MODIFY COLUMN ... FIRST|AFTER ...

Unfortunately you will have to retype the entire column definition. See http://dev.mysql.com/doc/refman/5.1/en/alter-table.html Example:

ALTER TABLE t MODIFY COLUMN cherry VARCHAR(255) NULL AFTER banana;

May vary depending on your MySQL version, but this syntax appears to work since version 3.23.

Bob Stein
  • 16,271
  • 10
  • 88
  • 101
Halcyon
  • 57,230
  • 10
  • 89
  • 128
6

Unfortunately, you will have to (1) pop up a query window, and (2) respecify the attributes of each column you rearrange. For example:

ALTER TABLE test.`new table`
  MODIFY COLUMN cherry unsigned int(10) NOT NULL AUTOINCREMENT PRIMARY KEY 
  AFTER banana

Table layout before change:

`apple`  varchar(45) NOT NULL,
`cherry` int(10) unsigned NOT NULL AUTO_INCREMENT,
`banana` varchar(45) NOT NULL

Table layout after change:

`apple`  varchar(45) NOT NULL,
`banana` varchar(45) NOT NULL,
`cherry` int(10) unsigned NOT NULL AUTO_INCREMENT
Bob Stein
  • 16,271
  • 10
  • 88
  • 101
Johan
  • 74,508
  • 24
  • 191
  • 319