更改 postgres 表中的列顺序 [英] Change column order in table of postgres

查看:278
本文介绍了更改 postgres 表中的列顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更改 postgressql 表中的列顺序.但我没有得到任何线索或答案.我认为此功能是在新版本中添加的.我正在使用 postgres 11.例

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.

推荐答案

您必须删除并重新创建表或至少 lastname 列:

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;

但是这个练习非常没有意义,因为您总是可以确定在 SELECT 子句中获取列的顺序.你没有使用 SELECT * 吧?由于其他原因,这也会有问题;它仅对临时查询有用.

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.

这篇关于更改 postgres 表中的列顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆