SQL 使用 PostgreSQL 在一个语句中重命名多个列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23274679/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Renaming multiple columns in one statement with PostgreSQL
提问by Rovanion
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;
回答by Erwin Brandstetter
No.
不。
While other actions can be combined, that's not possible with RENAME
. The manual:
虽然可以组合其他操作,但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.
所有形式
ALTER TABLE
在单个表该行为,除了RENAME
,SET SCHEMA
,ATTACH PARTITION
,和DETACH PARTITION
可以组合成多种变更的列表要被施加到一起。
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.
由于RENAME
是对系统目录的微小操作,因此运行多条语句没有坏处。在单个事务中执行以最小化锁定开销。
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.
其他操作ALTER COLUMN ... SET TYPE
可能会很昂贵,因为它们可能必须重写整个表。对于大表,在单个语句中尽可能多地做是明智的。