Change column order in table of postgres

霸气de小男生 提交于 2021-02-05 09:38:22

问题


I am trying to change the column order in the table of postgressql. But I didn't get any clue or answer. I think this functionality is add in new version. I am using postgres 11. Ex

No column 
1     Id
2    Lastname
3    Firstname

Now i want to change Firstname on 2nd position.


回答1:


You would have to drop and re-create the table or at least the lastname column for that:

BEGIN;
ALTER TABLE atable RENAME lastname TO oldcol;
ALTER TABLE atable ADD lastname text NOT NULL;
UPDATE atable SET lastname = oldcol;
ALTER TABLE atable DROP oldcol;
COMMIT;

But the exercise is pretty pointless, since you can always determine the order in which you get the columns in the SELECT clause. You aren't using SELECT *, are you? That would be problematic for other reasons as well; it is only useful for ad-hoc queries.



来源:https://stackoverflow.com/questions/60145157/change-column-order-in-table-of-postgres

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!