Tech Notes

Move a Database Column to a Different Position

A MySQL ALTER TABLE query that moves a column to a new position within a table.

Working with a database can be a headache when its columns aren’t arranged sensibly. The MySQL query below changes the position of a column within a table, so you can rearrange columns that weren’t ordered properly when the table was created.

"ALTER TABLE campaigns MODIFY COLUMN `column_name` varchar(255) AFTER `column_name_after`";

Note that MODIFY COLUMN redefines the column, so repeat its full definition (type, NULL/NOT NULL, default value) or you may lose those attributes. To move a column to the start of the table, use FIRST instead of AFTER ....