Renaming multiple columns in one statement with PostgreSQL

戏子无情 提交于 2019-11-30 14:31:17

问题


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;

回答1:


No.

While other actions can be combined, that's not possible with RENAME. The manual:

All the forms of ALTER TABLE that act on a single table, except RENAME, SET SCHEMA, ATTACH PARTITION, and DETACH PARTITION can be combined into a list of multiple alterations to be applied together.

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.

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.



来源:https://stackoverflow.com/questions/23274679/renaming-multiple-columns-in-one-statement-with-postgresql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!