Forum Moderators: coopster

Message Too Old, No Replies

Can I alter Field posistion in a Table?

         

AthlonInside

3:12 pm on Jan 6, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If I have 3 fields, Name, Email, Age as created in a table.

Is there a SQL query that can move the field aroud, such as making it to become

Name, Age, Email

jatar_k

4:34 pm on Jan 6, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



There is ALTER TABLE [mysql.com] but I am not sure if it moves columns around. You can drop and recreate one but I would imagine data would be lost.

I have to admit I don't see the necessity for moving them, you can select them in anyorder you want.

AthlonInside

5:13 pm on Jan 6, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think recreate is the only solution and phpmyadmin will be useful since it can generate sql to backup a table, then we can rearrage the order for the create table query.

coopster

5:14 pm on Jan 6, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I usually alter tables from a command line interface but I'm sure you can write your own query statements and execute them. What database are you using and what version? If it is MySQL 4.01 or later, you can CHANGE a column:
In MySQL Version 3.22 or later, you can use
FIRST
or
ADD ... AFTER col_name
to add a column at a specific position
within a table row. The default is to add the column last.
From MySQL Version 4.0.1, you can also use the
FIRST
and
AFTER
keywords in
CHANGE
or
MODIFY
.

However, in this particular case, you may want to use this type of approach:
How To Change the Order of Columns in a Table [mysql.com]

AthlonInside

10:22 pm on Jan 6, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Good Info Coopster! :)