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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 01:42:48  来源:igfitidea点击:

Renaming multiple columns in one statement with PostgreSQL

sqlpostgresqlddlalter-tabletable-rename

提问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 TABLEthat act on a single table, except RENAME, SET SCHEMA, ATTACH PARTITION, and DETACH PARTITIONcan be combined into a list of multiple alterations to be applied together.

所有形式ALTER TABLE在单个表该行为,除了 RENAMESET SCHEMAATTACH PARTITION,和DETACH PARTITION可以组合成多种变更的列表要被施加到一起。

Since RENAMEis 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 TYPEare 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可能会很昂贵,因为它们可能必须重写整个表。对于大表,在单个语句中尽可能多地做是明智的。