Sometimes it is necessary to move the order of a column in an SQL table. However, in the interface of PhpMyAdmin there is no easy way to change the order of the columns. This little tutorial explains how to change the order of columns in MySQL.
To serve as a concrete example, we will perform a column order change on a table that lists countries. The goal of this article is to move the “nom_en_gb” column after the “nom_fr_fr” column.
You should use the ALTER TABLE command to move the column order, as shown in the example below:
ALTER TABLE pays MODIFY COLUMN nom_en_gb VARCHAR(45) AFTER nom_fr_fr
This SQL query will move the “nom_en_gb” column just after the “nom_fr_fr” column. After running the query, the table will then look like the example below.
Move a column to the start
In the previous example it was possible to move a column AFTER another. This theoretically allows a column to be moved anywhere except at the beginning. The following query moves any column to the start of a table.
ALTER TABLE ma_table MODIFY COLUMN colonne_a_deplacer type_donnees FIRST
This query is used to move the “column_to_move” column with the type “data_type” (example: VARCHAR, TEXT, INT, DATE…) at the start.