Sqlite3: how to reorder columns in a table?

You can always order the columns however you want to in your SELECT statement, like this:

SELECT column1,column5,column2,column3,column4
FROM mytable
WHERE ...

You shouldn't need to "order" them in the table itself.


This isn't a trivial task in any DBMS. You would almost certainly have to create a new table with the order that you want, and move your data from one table to the order. There is no alter table statement to reorder the columns, so either in sqlite manager or any other place, you will not find a way of doing this in the same table.

If you really want to change the order, you could do:

Assuming you have tableA:

create table tableA(
col1 int,
col3 int,
col2 int);

You could create a tableB with the columns sorted the way you want:

create table tableB(
col1 int,
col2 int,
col3 int);

Then move the data to tableB from tableA:

insert into tableB
SELECT col1,col2,col3 
FROM tableA;

Then remove the original tableA and rename tableB to TableA:

DROP table tableA;
ALTER TABLE tableB RENAME TO tableA;

sqlfiddle demo

Tags:

Sqlite