Uncategorized

Shifting Columns in a MySQL database

One of the popular posts here is the cursor reset for MySQL. I originally wrote that so I could refer to that easily, since I don’t use it enough to have it memorized. Well, I came across another MySQL command not often used by me.

Yesterday I was working on a PHP application where I needed to do some database conversion (SQL Server to MySQL). The original schema really bugged me because the primary key field was the third column in the schema, and that was really annoying me. I wanted to figure out a way to shift the columns within my phpMyAdmin. Unfortunately, there isn’t a way with phpMyAdmin, but it is possible using SQL commands, specifically ALTER TABLE. This is how it’s done.


ALTER TABLE tableName MODIFY column column_2 int AFTER column_1

You have to remember to provide the datatype of the column you want to move or it will throw an exception.

One of the popular posts here is the cursor reset for MySQL. I originally wrote that so I could refer to that easily, since I don’t use it enough to have it memorized. Well, I came across another MySQL command not often used by me.

Yesterday I was working on a PHP application where I needed to do some database conversion (SQL Server to MySQL). The original schema really bugged me because the primary key field was the third column in the schema, and that was really annoying me. I wanted to figure out a way to shift the columns within my phpMyAdmin. Unfortunately, there isn’t a way with phpMyAdmin, but it is possible using SQL commands, specifically ALTER TABLE. This is how it’s done.


ALTER TABLE tableName MODIFY column column_2 int AFTER column_1

You have to remember to provide the datatype of the column you want to move or it will throw an exception.