简体   繁体   English

在 PostgreSQL 中重命名多个列

[英]Renaming multiple columns in PostgreSQL

My table has a bunch of columns in the following format:我的表有一堆以下格式的列:

_settingA
_settingB
_settingB

And I want to rename them simply to add a prefix as follows:我想简单地重命名它们以添加前缀,如下所示:

_1_settingA
_1_settingB
_1_settingC

I have a lot more than three columns to rename in this way.我有很多比三列更用这种方式来命名。 If I had just three, I'd just do it manually one by one.如果我只有三个,我会一个一个地手动完成。

What is the quickest / most efficient way to achieve this?实现这一目标的最快/最有效的方法是什么?

There's no single command aproach.没有单一的命令方法。 Obviously you could type multiple comands for RENAME by your self, but let me intoduce some improvement:) As I said in this answer显然,您可以自己为RENAME键入多个命令,但让我进行一些改进:) 正如我在此答案中所说

...for all such bulk-admin-operations you could use PostgreSQL system tables to generate queries for you instead of writing them by hand ...对于所有此类批量管理操作,您可以使用 PostgreSQL 系统表为您生成查询,而不是手动编写它们

In your case it would be:在您的情况下,它将是:

SELECT
    'ALTER TABLE ' || tab_name || ' RENAME COLUMN '
    || quote_ident(column_name) || ' TO '
    || quote_ident( '_1' || column_name) || ';'
FROM (
    SELECT
        quote_ident(table_schema) || '.' || quote_ident(table_name) as tab_name,
        column_name
    FROM information_schema.columns  
    WHERE 
            table_schema = 'schema_name'
            AND table_name = 'table_name'
            AND column_name LIKE '\_%'
) sub;

That'll give you set of strings which are SQL commands like:这将为您提供一组字符串,这些字符串是 SQL 命令,例如:

ALTER TABLE  schema_name.table_name RENAME COLUMN "_settingA" TO "_1_settingA";
ALTER TABLE  schema_name.table_name RENAME COLUMN "_settingB" TO "_1_settingB";
...

There no need using table_schema in WHERE clause if your table is in public schema.如果您的表在public架构中,则table_schemaWHERE子句中使用table_schema Also remember using function quote_ident() -- read my original answer for more explanation.还记得使用函数quote_ident() - 阅读我的原始答案以获得更多解释。

Edit:编辑:

I've change my query so now it works for all columns with name begining with underscore _ .我已经更改了我的查询,所以现在它适用于名称以下划线_开头的所有列。 Because underscore is special character in SQL pattern matching, we must escape it (using \\ ) to acctually find it.因为下划线是 SQL 模式匹配中的特殊字符,我们必须对其进行转义(使用\\ )才能实际找到它。

Something simple like this will work.像这样简单的事情会起作用。

SELECT FORMAT(
  'ALTER TABLE %I.%I.%I RENAME %I TO %I;',
  table_catalog,
  table_schema,
  table_name,
  column_name,
  '_PREFIX_' + column_name
)
FROM information_schema.columns
WHERE table_name = 'foo';

%I will do quote_ident , which is substantially nicer. %I会做quote_ident ,这要好得多。 If you're in PSQL you can run it with \\gexec如果您使用的是 PSQL,则可以使用\\gexec运行它

You can use the following function : (I use this to add prefix on tables wiches have more than 50 columns)您可以使用以下函数:(我用它在超过 50 列的表上添加前缀)

First create the function :首先创建函数:

CREATE OR REPLACE FUNCTION rename_cols( schema_name_ text,table_name_ text, prefix varchar(4))
    RETURNS bool AS
     $BODY$
     DECLARE
       rec_selection record;
        BEGIN
          FOR rec_selection IN (
              SELECT column_name FROM information_schema.columns WHERE table_schema = schema_name_ AND table_name = table_name_) LOOP
            EXECUTE 'ALTER TABLE '||schema_name_||'.'||table_name_||' RENAME COLUMN "'|| rec_selection.column_name ||'" TO "'||prefix|| rec_selection.column_name ||'" ;';
          END LOOP;
        RETURN True;
       END;
     $BODY$
    LANGUAGE plpgsql VOLATILE
    COST 100;

Then execute function :然后执行函数:

SELECT rename_cols('public','test','d');

Hope it will be usefull,希望有用,

You can't do that.你不能那样做。

All the actions except RENAME and SET SCHEMA can be combined into a list of multiple alterations to apply in parallel.除了RENAMESET SCHEMA之外的所有操作都可以组合成多个更改的列表以并行应用。

most efficient way is using ActiveRecord.最有效的方法是使用 ActiveRecord。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM