使用PostgreSQL在一条语句中重命名多列 [英] Renaming multiple columns in one statement with PostgreSQL
问题描述
是否可以在单个语句中重命名多列,类似于以下内容:
Is it possible to rename multiple columns in a single statement, something along the lines of:
ALTER TABLE Users
RENAME COLUMN userName TO user_name,
RENAME COLUMN realName TO real_name;
推荐答案
否。
虽然可以组合其他操作,但使用 RENAME
是不可能的。 手册:
While other actions can be combined, that's not possible with RENAME
. The manual:
所有在单个表上起作用的
ALTER TABLE
形式,除了
RENAME
,设置模式
,附件分区
和分离分区
可以是
组合成要一起应用的多个变更列表。
All the forms of
ALTER TABLE
that act on a single table, exceptRENAME
,SET SCHEMA
,ATTACH PARTITION
, andDETACH PARTITION
can be combined into a list of multiple alterations to be applied together.
因为 RENAME
是系统目录上的微小操作,对运行多个语句没有害处。
Since RENAME
is a tiny operation on a system catalog, there is no harm in running multiple statements. Do it in a single transaction to minimize locking overhead.
其他操作如 ALTER COLUMN ... SET TYPE
是可能很昂贵,因为他们可能不得不重写整个表。对于大型表,明智的做法是在单个语句中尽可能多地执行操作。
Other actions like ALTER COLUMN ... SET TYPE
are potentially expensive because they may have to rewrite the whole table. With big tables it would be wise to do as much as possible in a single statement.
这篇关于使用PostgreSQL在一条语句中重命名多列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!