61

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
11
  • 3
    Be less vague. What do you want to do? Commented Jun 2, 2011 at 19:28
  • @Tomalak Geret'kal: I have edited the question
    – karto
    Commented Jun 2, 2011 at 19:32
  • Adding the word "specific" into your question does not make it so. Commented Jun 2, 2011 at 19:44
  • @Tomalak Geret'kal: Its more clear now.
    – karto
    Commented Jun 2, 2011 at 19:54
  • sounds like, you don't know sql and how to use ORDER BY? Commented Jan 26, 2013 at 22:46

5 Answers 5

75

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.

1
50

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

3
  • 2
    The move column feature wasn't available yet at the time of the question. Great they brought it.
    – karto
    Commented Sep 30, 2015 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
    Commented Jan 11, 2016 at 3:22
  • 1
    Thanks, @BeNice for this! I didn't see that option.
    – shenn
    Commented Feb 8, 2018 at 20:03
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.

1
  • ↓↓↓ 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
    Commented Aug 14, 2022 at 12:08
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.

1
  • Thanks; but can it help re-arrange the order of the columns?
    – karto
    Commented Jun 2, 2011 at 19:51
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
0

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.