Column ordering in MySQL using ALTER TABLE – with examples.

MySQL allows table columns to be placed in a specific location among the other present columns. You likely don’t care one iota about column ordering. But, if you do – and your table is already established – you can still have your cake and eat it too by using simple ALTER TABLE commands in conjunction with certain keywords and place those columns in a position to your liking. Continue reading to see example queries…

mulit-colored-pins-on-orange-mat
Photo by Jeff Frenette on Unsplash

Note: All data, names or naming found within the database presented in this post, are strictly used for practice, learning, instruction, and testing purposes. It by no means depicts actual data belonging to or being used by any party or organization.

OS and DB used:

  • OpenSuse Leap 15.1
  • MySQL 8.0.18


Self-Promotion:

If you enjoy the content written here, by all means, share this blog and your favorite post(s) with others who may benefit from or like it as well. Since coffee is my favorite drink, you can even buy me one if you would like!


Below I have this description of a fictitious ‘friends’ table:

1
2
3
4
5
6
7
8
9
10
11
mysql> DESC friends;
+------------+-------------+------+-----+---------+-------+
| Field      | Type        | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| first_name | varchar(25) | NO   |     | NULL    |       |
| last_name  | varchar(25) | NO   |     | NULL    |       |
| phone_num  | char(12)    | YES  |     | NULL    |       |
| birthday   | date        | YES  |     | NULL    |       |
| age        | int(11)     | YES  |     | NULL    |       |
+------------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

Imagine I now have the need for a column to store information concerning a friends residence state, which is easily accomplished with this ALTER TABLE command:

1
2
3
mysql> ALTER TABLE friends ADD COLUMN state CHAR(2);
Query OK, 0 rows affected (1.06 sec)
Records: 0  Duplicates: 0  Warnings: 0

The ‘state’ column has been added:

1
2
3
4
5
6
7
8
9
10
11
12
mysql> DESC friends;
+------------+-------------+------+-----+---------+-------+
| Field      | Type        | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| first_name | varchar(25) | NO   |     | NULL    |       |
| last_name  | varchar(25) | NO   |     | NULL    |       |
| phone_num  | char(12)    | YES  |     | NULL    |       |
| birthday   | date        | YES  |     | NULL    |       |
| age        | int(11)     | YES  |     | NULL    |       |
| state      | char(2)     | YES  |     | NULL    |       |
+------------+-------------+------+-----+---------+-------+
6 rows in set (0.00 sec)

For whatever reason, suppose I want the ‘state’ column between the ‘last_name’ and ‘phone_num’ columns. Using MODIFY in conjunction with ALTER TABLE and the AFTER keyword, this is quite simple to accomplish:

1
2
3
mysql> ALTER TABLE friends MODIFY state CHAR(2) AFTER last_name;
Query OK, 0 rows affected (2.48 sec)
Records: 0  Duplicates: 0  Warnings: 0

Now the ‘state’ column is in the position specified by the above command:

1
2
3
4
5
6
7
8
9
10
11
12
mysql> DESC friends;
+------------+-------------+------+-----+---------+-------+
| Field      | Type        | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| first_name | varchar(25) | NO   |     | NULL    |       |
| last_name  | varchar(25) | NO   |     | NULL    |       |
| state      | char(2)     | YES  |     | NULL    |       |
| phone_num  | char(12)    | YES  |     | NULL    |       |
| birthday   | date        | YES  |     | NULL    |       |
| age        | int(11)     | YES  |     | NULL    |       |
+------------+-------------+------+-----+---------+-------+
6 rows in set (0.00 sec)


Recommended Reading

Are you interested in an introductory exploration of the ALTER TABLE command with the MODIFY and CHANGE clauses? I wrote the post,ALTER TABLE: Examples with MySQL – Beginner Series, and you should definitely give it a read.


Besides specifying a columns’ specific position with the AFTER keyword, using the FIRST keyword places it in that location position:

1
2
3
mysql> ALTER TABLE friends ADD COLUMN country CHAR(3) first;
Query OK, 0 rows affected (2.46 sec)
Records: 0  Duplicates: 0  Warnings: 0

Note: Column positioning in the initial ALTER TABLE command when adding a new column for the first time is perfectly valid. You do not necessarily have to add the column first, then execute an additional ALTER TABLE command to indicate its position. As I have shown in the above command, it’s totally possible in one go.

The newly added ‘country’ column is now in the first position:

1
2
3
4
5
6
7
8
9
10
11
12
13
mysql> DESC friends;
+------------+-------------+------+-----+---------+-------+
| Field      | Type        | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| country    | char(3)     | YES  |     | NULL    |       |
| first_name | varchar(25) | NO   |     | NULL    |       |
| last_name  | varchar(25) | NO   |     | NULL    |       |
| state      | char(2)     | YES  |     | NULL    |       |
| phone_num  | char(12)    | YES  |     | NULL    |       |
| birthday   | date        | YES  |     | NULL    |       |
| age        | int(11)     | YES  |     | NULL    |       |
+------------+-------------+------+-----+---------+-------+
7 rows in set (0.00 sec)

Perhaps column position order is of little to no consequence in your daily work-flow. However, should you need to move column ordering around within tables, ALTER TABLE can line you right up.

Thanks for reading!!!

Like what you have read? See anything incorrect? Please comment below and thanks for reading!!!

A Call To Action!

Thank you for taking the time to read this post. I truly hope you discovered something interesting and enlightening. Please share your findings here, with someone else you know who would get the same value out of it as well.

Visit the Portfolio-Projects page to see blog post/technical writing I have completed for clients.

Have I mentioned how much I love a cup of coffee?!?!

To receive email notifications (Never Spam) from this blog (“Digital Owl’s Prose”) for the latest blog posts as they are published, please subscribe (of your own volition) by clicking the ‘Click To Subscribe!’ button in the sidebar on the homepage! (Feel free at any time to review the Digital Owl’s Prose Privacy Policy Page for any questions you may have about: email updates, opt-in, opt-out, contact forms, etc…)

Be sure and visit the “Best Of” page for a collection of my best blog posts.


Josh Otwell has a passion to study and grow as a SQL Developer and blogger. Other favorite activities find him with his nose buried in a good book, article, or the Linux command line. Among those, he shares a love of tabletop RPG games, reading fantasy novels, and spending time with his wife and two daughters.

Disclaimer: The examples presented in this post are hypothetical ideas of how to achieve similar types of results. They are not the utmost best solution(s). The majority, if not all, of the examples provided, is performed on a personal development/learning workstation-environment and should not be considered production quality or ready. Your particular goals and needs may vary. Use those practices that best benefit your needs and goals. Opinions are my own.

Advertisements

Hey thanks for commenting! Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.