简体   繁体   English

PostgreSQL 列名是否区分大小写?

[英]Are PostgreSQL column names case-sensitive?

I have a db table say, persons in Postgres handed down by another team that has a column name say, "first_Name" .我有一个 db 表说,Postgres 中的persons由另一个团队传下来,列名是"first_Name" Now am trying to use PG commander to query this table on this column-name.现在我正在尝试使用 PG 指挥官在此列名上查询此表。

select * from persons where first_Name="xyz";

And it just returns它只是返回

ERROR: column "first_Name" does not exist错误:列“first_Name”不存在

Not sure if I am doing something silly or is there a workaround to this problem that I am missing?不确定我是否在做一些愚蠢的事情,或者是否有解决这个问题的方法?

Identifiers (including column names) that are not double-quoted are folded to lowercase in PostgreSQL.没有双引号的标识符(包括列名)在 PostgreSQL 中被折叠为小写。 Column names that were created with double-quotes and thereby retained uppercase letters (and/or other syntax violations) have to be double-quoted for the rest of their life:使用双引号创建并因此保留大写字母(和/或其他语法违规)的列名必须在其余生中使用双引号:

"first_Name"

Values (string literals / constants) are enclosed in single quotes :(字符串文字/常量)用单引号括起来

'xyz'

So, yes , PostgreSQL column names are case-sensitive (when double-quoted):所以,的,PostgreSQL 列名区分大小写(双引号时):

SELECT * FROM persons WHERE "first_Name" = 'xyz';

Read the manual on identifiers here. 在此处阅读有关标识符的手册。

My standing advice is to use legal, lower-case names exclusively so double-quoting is not needed.我的常规建议是仅使用合法的小写名称,因此不需要双引号。

To quote the documentation :引用文档

Key words and unquoted identifiers are case insensitive.关键字和未加引号的标识符不区分大小写。 Therefore:所以:

 UPDATE MY_TABLE SET A = 5;

can equivalently be written as:等价地可以写成:

 uPDaTE my_TabLE SeT a = 5;

You could also write it using quoted identifiers :您也可以使用带引号的标识符来编写它:

UPDATE "my_table" SET "a" = 5;

Quoting an identifier makes it case-sensitive, whereas unquoted names are always folded to lower case (unlike the SQL standard where unquoted names are folded to upper case).引用标识符使其区分大小写,而未引用的名称始终折叠为小写(与未引用的名称折叠为大写的 SQL 标准不同)。 For example, the identifiers FOO , foo , and "foo" are considered the same by PostgreSQL, but "Foo" and "FOO" are different from these three and each other.例如,标识符FOOfoo"foo"被 PostgreSQL 认为是相同的,但是"Foo""FOO"与这三个不同并且彼此不同。

If you want to write portable applications you are advised to always quote a particular name or never quote it.如果您想编写可移植的应用程序,建议您始终引用特定名称或永远不要引用它。

The column names which are mixed case or uppercase have to be double quoted in PostgresQL.在 PostgresQL 中混合大小写或大写的列名必须用双引号引起来。 So best convention will be to follow all small case with underscore.所以最好的惯例是用下划线跟随所有小写。

if use JPA I recommend change to lowercase schema, table and column names, you can use next intructions for help you:如果使用 JPA,我建议更改为小写模式、表和列名,您可以使用下一个指令来帮助您:

select
    psat.schemaname,
    psat.relname,
    pa.attname,
    psat.relid
from
    pg_catalog.pg_stat_all_tables psat,
    pg_catalog.pg_attribute pa
where
    psat.relid = pa.attrelid

change schema name:更改架构名称:

ALTER SCHEMA "XXXXX" RENAME TO xxxxx;

change table names:更改表名:

ALTER TABLE xxxxx."AAAAA" RENAME TO aaaaa;

change column names:更改列名:

ALTER TABLE xxxxx.aaaaa RENAME COLUMN "CCCCC" TO ccccc;

You can try this example for table and column naming in capital letters.您可以尝试使用此示例以大写字母命名表和列。 (postgresql) (postgresql)

//Sql;
      create table "Test"
        (
        "ID" integer,
        "NAME" varchar(255)
        )



//C#
  string sqlCommand = $@"create table ""TestTable"" (
                                ""ID"" integer GENERATED BY DEFAULT AS IDENTITY primary key, 
                                ""ExampleProperty"" boolean,
                                ""ColumnName"" varchar(255))";

always_use_lower_case_identifiers_with_underscore_separators_i_e_camel_case ! always_use_lower_case_identifiers_with_underscore_separators_i_e_camel_case

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

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