Sunday, May 9, 2010

SQL table change order of column if we have already populated table

Example if table TAB has c1 - int(11) , c2 - varchar(40) , c3 - varchar (40)

/ -------- Table name - TAB ---------- ---/
--------------------------------------------------------
C1 INT (11) l C2 VARCHAR (11) l C3 VARCHAR (11)
--------------------------------------------------------

But now we want order C1, C2, C3 as C1,C3,C2 ... ofcourse you may go to views without changing physical data table . But if we want change in physical data table then ??

sample querry -

ALTER TABLE TAB MODIFY COLUMN C2 varchar(60) AFTER C3


ALTER TABLE TABLE_NAME MODIFY COLUMN COLUMN_NAME_TO_BE SHIFTED DATA_TYPE_OF_THE_COLUMN AFTER COLUMN_NAME

** Replace red colored fields with real values of your table
AFTER Querry


/ -------- Table name - TAB ---------- ---/
--------------------------------------------------------
C1 INT (11) l C3 VARCHAR (11) l C2 VARCHAR (11)
--------------------------------------------------------

No comments: