Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easy way to re-order columns?

I'm working on a database. On most of the tables, the column order is not what I would expect, and I would like to change it (I have the permission). For example, the primary_key's id columns are rarely the first column!

Is there an easy method of moving columns with phpMyAdmin?

like image 250
Haroldo Avatar asked Nov 04 '10 09:11

Haroldo


People also ask

How do I automatically rearrange columns in Google Sheets?

On your computer, open a spreadsheet in Google Sheets. Select the rows or columns to move. At the top, click Edit. Select the direction you want to move the row or column, like Move row up.

How do I reorder columns in SQL?

Using SQL Server Management StudioIn Object Explorer, right-click the table with columns you want to reorder and select Design. Select the box to the left of the column name that you want to reorder. Drag the column to another location within the table.


2 Answers

Use an ALTER TABLE ... MODIFY COLUMN statement.

ALTER TABLE table_name MODIFY COLUMN misplaced_column INT(11) AFTER other_column; 
like image 124
Eran Galperin Avatar answered Oct 04 '22 14:10

Eran Galperin


Here is the sql query
ALTER TABLE table_name MODIFY COLUMN misplaced_column Column-definition AFTER other_column; Here in Column-definition is full column definition. To see the column definition if you are using phpmyadmin click on structure tab. Then click on change link on desired column. Then withour modifyig any things click save. It will show you the sql. Copy the sql and just add *AFTER other_column* at the end. It will be all.

If you like to bring the *misplaced_column* to the first position then ALTER TABLE table_name MODIFY COLUMN misplaced_column Column-definition FIRST;

like image 24
zahid9i Avatar answered Oct 04 '22 14:10

zahid9i