Change column order in table of postgres

前端 未结 1 1541
时光取名叫无心
时光取名叫无心 2021-01-28 18:02

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

1条回答
  •  温柔的废话
    2021-01-28 18:38

    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.

    0 讨论(0)
提交回复
热议问题