17

我已经看到可以使用 psql 中的以下命令将所有转换为不区分大小写的名称:

\o /tmp/go_to_lower
select 'ALTER TABLE '||'"'||tablename||'"'||' RENAME TO ' ||
lower(tablename)||';' from pg_tables where schemaname = 'public';
psql -U username database < /tmp/go_to_lower

我一直无法找到以相同方式将所有转换为不区分大小写的命令。如何做到这一点?

编辑:显然上面的代码只将表名转换为小写。我知道此代码 ALTER TABLE "YourTableName" RENAME TO YourTableName; 将转换为表名不区分大小写。有没有办法对列名的质量执行类似的功能?

4

5 回答 5

21

按照与原版相同的思路,您应该能够执行以下操作。这通过从 information_schema 中提取它们,为更改生成 SQL,将其存储到文件然后再次执行 SQL 来重命名所有尚未使用小写的列。

\t on
select 'ALTER TABLE '||'"'||table_name||'"'||' RENAME COLUMN '||'"'||column_name||'"'||' TO ' || lower(column_name)||';' 
from information_schema.columns 
where table_schema = 'public' and lower(column_name) != column_name
\g /tmp/go_to_lower
\i /tmp/go_to_lower
于 2012-04-10T11:58:23.667 回答
19

By default, all you identifiers are case insensitive, and internally PostgreSQL stores them in lowercase. In case you need to have:

  • case sensitive
  • non-ASCII characters
  • special characters

within your identifiers, you should use double quotes (") around your identifiers.

Please, check this bit of the PostgreSQL documentation.

EDIT: After your clarification, you can use:

SELECT 'ALTER TABLE '||quote_ident(t.relname)||' RENAME TO '||t.relname||';'
  FROM pg_class t, pg_namespace s
 WHERE s.oid = t.relnamespace AND s.nspname = 'public'
   AND t.relkind='r' AND t.relname != lower(t.relname)
 ORDER BY 1;

and for columns:

SELECT 'ALTER TABLE '||quote_ident(t.relname)||
       ' RENAME COLUMN '||quote_ident(a.attname)||
       ' TO '||a.attname||';'
  FROM pg_class t, pg_namespace s, pg_attribute a
 WHERE s.oid = t.relnamespace AND s.nspname = 'public'
   AND t.relkind='r'
   AND a.attrelid = t.oid AND NOT a.attisdropped AND a.attnum > 0
   AND a.attname != lower(a.attname)
 ORDER BY 1;

Then copy-paste the output into your client.

If you're using psql, you can use \t to enable rows-only mode, \o <full_file_path> to save output into the temporary file and, finally, \i <full_file_path> to execute actual statements.

于 2012-04-10T10:46:38.227 回答
1

我在 Database Administrators 上创建了一个 SQL 查询来执行此操作。

  1. 将所有标识符转换为小写
  2. 将空格转换' ''_'
  3. 对所有架构、表和列名执行此操作

有关更多信息,请参阅,

于 2018-03-27T19:32:23.630 回答
0
do language plpgsql $$
declare
    r record;
begin
    for r in
        select relname, attname
        from pg_attribute a
        inner join pg_class c on a.attrelid = c.oid
        inner join pg_namespace n on c.relnamespace = n.oid
        where 
            n.nspname = 'public'
            and
            attname != lower(attname)
            and
            not attisdropped
    loop
        execute format('
            alter table %1$I rename column %2$I to %3$s
        ', r.relname, r.attname, lower(r.attname));
    end loop;
end;
$$;

begin;在尝试这个之前发出一个。检查是否正确。然后才发出一个commit;. 如果您使用的是命名空间,请在where子句中替换它。

于 2012-04-10T14:15:16.910 回答
0

让我为像我这样的初学者和不习惯像 psql 这样的命令行工具的初学者添加一个使用 PgAdmin 的分步指南:

在 PgAdmin 中执行以下查询:

SELECT  'ALTER TABLE ' || quote_ident(c.table_schema) || '.'
  || quote_ident(c.table_name) || ' RENAME "' || c.column_name || '" TO ' || quote_ident(lower(c.column_name)) || ';' As ddlsql
  FROM information_schema.columns As c
  WHERE c.table_schema NOT IN('information_schema', 'pg_catalog') 
      AND c.column_name <> lower(c.column_name) 
  ORDER BY c.table_schema, c.table_name, c.column_name;

*来源:https ://www.postgresonline.com/article_pfriendly/141.html 。请注意,您无需在此处更改任何内容。

现在将结果导出到文本文件。

在此处输入图像描述

使用 Excel、记事本或您选择的任何文本编辑器打开 .csv 文件。复制除第一行(“ddlsql”)之外的所有行并将它们粘贴到 PgAdmin 中的新查询中。确保删除双引号。运行它并完成。

于 2021-04-03T17:18:09.580 回答