Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

changign column order

Status
Not open for further replies.

rs51

Technical User
Oct 13, 2001
163
PT
hi
i read this in the manual:

If you want to change the order of columns anyway, you can do it as follows:
Create a new table with the columns in the right order.
Execute INSERT INTO new_table SELECT fields-in-new_table-order FROM old_table.
Drop or rename old_table.
ALTER TABLE new_table RENAME old_table.

And also read this:

ALTER [IGNORE] TABLE tbl_name alter_specification [, alter_specification ...]
alter_specification:
ADD [COLUMN] create_definition [FIRST | AFTER column_name ]

but somehow i cant get it working with 2nd option, while first is very laborious.
Can anyone come with a clearer idea?
thanks
 
ok, thanks
maybe i'm too tired/lazy to write&run the apropriate script
 
what i dont grasp/understand from second way is how the command is written
exemple (of how doesnt work)
alter table Turma add collumn Id
int not null auto_increment primary key FIRST;
 
amazing!!!
worked!!!
mysql> use test;
Database changed
mysql> alter table turma add column Id
-> int not null primary key FIRST;
ERROR 1060: Duplicate column name 'Id'
mysql> alter table turma drop column id;
Query OK, 5 rows affected (0.18 sec)
Records: 5 Duplicates: 0 Warnings: 0

mysql> alter table turma add column Id
-> int not null primary key FIRST;
ERROR 1062: Duplicate entry '0' for key 1
mysql> alter table turma add column Id
-> int not null auto_increment primary key FIRST;
Query OK, 5 rows affected (0.10 sec)
Records: 5 Duplicates: 0 Warnings: 0

mysql> select * from turma;
+----+-----+------+----------+----------+
| Id | Ano | Nome | Epoca_id | Ciclo_id |
+----+-----+------+----------+----------+
| 1 | 5 | A | 6 | 1 |
| 2 | 5 | B | 6 | 1 |
| 3 | 7 | A | 6 | 2 |
| 4 | 7 | B | 6 | 2 |
| 5 | 7 | l | 6 | 2 |
+----+-----+------+----------+----------+
5 rows in set (0.00 sec)
thanks a lot
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top