• Flickr
  • Last.fm
  • Twitter
  • Linkr
  • About
  • Favourites
  • Comments
  • XBox

Re-arrange Table Columns

Posted on September 28, 2005 at 05:00PM

There might be a time where the columns in a database aren't what they should be. This could either come from a new version of software, you made a mistake or a project you've taken over. Whatever the reason, there is an easy way to do it.

If you're not sure what I'm meaning by this think of it like this. You have a table with fields like this:

id
name
email
password

But they're in the wrong order, you want them like this:

id
name
password
email

(For the slow, you want password and e-mail to switch places).

There's a very simple query that will let you do this, which is especially handy if there's several rows of data or even hundreds. Here's the query:

ALTER TABLE `tablename` MODIFY COLUMN email text AFTER name

This will do the wanted change. Please note that where it says 'text', that is the type (eg: int, blob, text, longtext, etc) and is required. Also, 'email' is the column we want to move and we want to move it after 'name'.

There you have it, you've fixed the database with very minimal effort. Kudos to Madman solving this problem.

Comments

it is great!

Add comment

Comments for this post have been disabled.