Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the column position of MySQL table without losing column data?

Tags:

sql

mysql

I want to change the column positions of my database table without losing data.

For example:

Current table:

+----+------+-------+----------+ | id | name | email | password | +----+------+-------+----------+ 

to

+----+----------+------+-------+ | id | password | name | email | +----+----------+------+-------+ 
like image 560
D S Avatar asked May 23 '12 11:05

D S


People also ask

How do I rearrange columns in MySQL table?

Go to Structure and click Move Columns and then just drag the columns to rearrange.


2 Answers

Try this:

ALTER TABLE table_name MODIFY password varchar(20) AFTER id 
like image 180
Hearaman Avatar answered Oct 16 '22 17:10

Hearaman


Hearaman's answer is correct; but if you are using phpMyAdmin, there is a visual and practical way to do that.

  1. Open the table
  2. Choose the "Structure" tab
  3. Click on "Move columns"
  4. Drag and drop column names

Move columns link, middle of the Structure tab Move columns popup

like image 40
Arman Ozak Avatar answered Oct 16 '22 16:10

Arman Ozak