{"id":146,"date":"2011-10-02T13:50:36","date_gmt":"2011-10-02T18:50:36","guid":{"rendered":"http:\/\/jamesroberts.name\/blog\/?p=146"},"modified":"2011-10-02T13:52:29","modified_gmt":"2011-10-02T18:52:29","slug":"mysql-alter-table-to-re-order-columns","status":"publish","type":"post","link":"https:\/\/jamesroberts.name\/blog\/2011\/10\/02\/mysql-alter-table-to-re-order-columns\/","title":{"rendered":"MySQL alter table to re-order columns"},"content":{"rendered":"<p>Sometimes because of my completely anal nature, I want the fields that I&#8217;ve already added to a table in MySQL to be in a different order.  To do this you can simply alter that table column, without changing any of its properties, then adding &#8220;AFTER column_3&#8221;.<\/p>\n<p>So for instance you had a table like this:<\/p>\n<pre>\r\nmysql> describe test;\r\n+-----------+---------+------+-----+---------+----------------+\r\n| Field     | Type    | Null | Key | Default | Extra          |\r\n+-----------+---------+------+-----+---------+----------------+\r\n| column_id | int(11) | NO   | PRI | NULL    | auto_increment |\r\n| column_1  | int(11) | NO   |     | NULL    |                |\r\n| column_2  | int(11) | NO   |     | NULL    |                |\r\n| column_3  | int(11) | NO   |     | NULL    |                |\r\n+-----------+---------+------+-----+---------+----------------+\r\n4 rows in set (0.00 sec)\r\n<\/pre>\n<p>But you really want column_1 to be ordered after column_3 because you are as ridiculous as I am.  You can run this query:<\/p>\n<pre lang=\"sql\">\r\nALTER TABLE `test` CHANGE `column_1` `column_1` INT( 11 ) NULL AFTER column_3;\r\n<\/pre>\n<p>Now your table looks like this:<\/p>\n<pre>\r\nmysql> describe test;\r\n+-----------+---------+------+-----+---------+----------------+\r\n| Field     | Type    | Null | Key | Default | Extra          |\r\n+-----------+---------+------+-----+---------+----------------+\r\n| column_id | int(11) | NO   | PRI | NULL    | auto_increment |\r\n| column_2  | int(11) | NO   |     | NULL    |                |\r\n| column_3  | int(11) | NO   |     | NULL    |                |\r\n| column_1  | int(11) | YES  |     | NULL    |                |\r\n+-----------+---------+------+-----+---------+----------------+\r\n4 rows in set (0.00 sec)\r\n<\/pre>\n<p>I find this to be useful for me on larger tables that have grown over time and I want some relevant columns grouped together for when I am viewing data in phpMyAdmin or whatever.<\/p>\n<p>Hope this helps someone =)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes because of my completely anal nature, I want the fields that I&#8217;ve already added to a table in MySQL to be in a different order. To do this you can simply alter that table column, without changing any of its properties, then adding &#8220;AFTER column_3&#8221;. So for instance you had a table like this: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/jamesroberts.name\/blog\/wp-json\/wp\/v2\/posts\/146"}],"collection":[{"href":"https:\/\/jamesroberts.name\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jamesroberts.name\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jamesroberts.name\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jamesroberts.name\/blog\/wp-json\/wp\/v2\/comments?post=146"}],"version-history":[{"count":5,"href":"https:\/\/jamesroberts.name\/blog\/wp-json\/wp\/v2\/posts\/146\/revisions"}],"predecessor-version":[{"id":151,"href":"https:\/\/jamesroberts.name\/blog\/wp-json\/wp\/v2\/posts\/146\/revisions\/151"}],"wp:attachment":[{"href":"https:\/\/jamesroberts.name\/blog\/wp-json\/wp\/v2\/media?parent=146"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jamesroberts.name\/blog\/wp-json\/wp\/v2\/categories?post=146"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jamesroberts.name\/blog\/wp-json\/wp\/v2\/tags?post=146"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}