PostgreSQL列名称案例敏感吗?

发布于 2025-01-25 11:05:51 字数 293 浏览 3 评论 0 原文

我有一个DB表, Persons 在Postgres中由另一个具有列名称的团队say,“ first_name” 。现在,我正在尝试使用PG指挥官在此列名上查询此表。

select * from persons where first_Name="xyz";

它只是返回

错误:列“ first_name”不存在

我是在做愚蠢的事情还是我缺少这个问题的解决方法?

I have a db table say, persons in Postgres handed down by another team that has a column name say, "first_Name". Now am trying to use PG commander to query this table on this column-name.

select * from persons where first_Name="xyz";

And it just returns

ERROR: column "first_Name" does not exist

Not sure if I am doing something silly or is there a workaround to this problem that I am missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

归属感 2025-02-01 11:05:51

标识符(包括列名称) 双引号被折叠为PostgreSQL中的较低情况。用 双引号创建的标识符 保留上层案例字母(和/或语法违规),必须在其余生中双重引用:

"first_Name"                 -- upper-case "N" preserved
"1st_Name"                   -- leading digit preserved
"AND"                        -- reserved word preserved

但是(没有双引号):

first_Name   → first_name    -- upper-case "N" folded to lower-case "n"
1st_Name     → Syntax error! -- leading digit
AND          → Syntax error! -- reserved word

value (字符串文字/常数)包含在 单引号 中:

'xyz'

So,是YES ,PostgreSQL列名称(双引号时):

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

我的常规建议是专门使用合法的,低案例的名称,因此不需要双重引用。

系统目录,例如 pg_class 商店名称以案例敏感的方式 - 在双重引用时提供(显然没有封闭引号)或较低的效果。

Identifiers (including column names) that are not double-quoted are folded to lower case in PostgreSQL. Identifiers created with double quotes retain upper case letters (and/or syntax violations) and have to be double-quoted for the rest of their life:

"first_Name"                 -- upper-case "N" preserved
"1st_Name"                   -- leading digit preserved
"AND"                        -- reserved word preserved

But (without double-quotes):

first_Name   → first_name    -- upper-case "N" folded to lower-case "n"
1st_Name     → Syntax error! -- leading digit
AND          → Syntax error! -- reserved word

Values (string literals / constants) are enclosed in single quotes:

'xyz'

So, yes, PostgreSQL column names are case-sensitive (when double-quoted):

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

The manual on identifiers.

My standing advice is to use legal, lower-case names exclusively, so double-quoting is never required.

System catalogs like pg_class store names in case-sensitive fashion - as provided when double-quoted (without enclosing quotes, obviously), or lower-cased if not.

木格 2025-02-01 11:05:51

引用

关键词和未引用的标识符是不敏感的。因此:

 更新my_table设置a = 5;
 

可以等效地写为:

 更新my_table设置a = 5;
 

您也可以使用引用的标识符

UPDATE "my_table" SET "a" = 5;

引用标识符使其对案例敏感,而未引用的名称总是折叠为较低的情况折叠到上箱)。例如,标识符 foo foo “ foo” 被PostgreSQL认为相同,但是“ foo” “ foo” 与这三个和彼此不同。

如果要编写便携式应用程序,建议您始终引用特定名称或永远不要引用它。

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). 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.

If you want to write portable applications you are advised to always quote a particular name or never quote it.

记忆消瘦 2025-02-01 11:05:51

混合案例或大写的列名必须在PostgreSQL中进行双重引用。因此,最好的惯例是遵循所有小型案例。

示例:

create table mytable (a int, "B" int);

select a from mytable; -- works
select "a" from mytable; -- also works

select "B" from mytable; -- works
select b from mytable; -- ERROR: column "b" does not exist
select "b" from mytable; -- ERROR: column "b" does not exist

如您所见,如果该列包含一个上案例字符,则必须在引用它时始终引用它。

用PostgreSQL 17。测试

The column names which are mixed case or uppercase have to be double quoted in PostgresQL. So best convention will be to follow all small case with underscore.

Example:

create table mytable (a int, "B" int);

select a from mytable; -- works
select "a" from mytable; -- also works

select "B" from mytable; -- works
select b from mytable; -- ERROR: column "b" does not exist
select "b" from mytable; -- ERROR: column "b" does not exist

As you can see, if the column contains an upper-case character, it must always be quoted when referencing it.

Tested with PostgreSQL 17.

谁与争疯 2025-02-01 11:05:51

如果使用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

更改模式名称:

ALTER SCHEMA "XXXXX" RENAME TO xxxxx;

更改表名称:

ALTER TABLE xxxxx."AAAAA" RENAME TO aaaaa;

更改列名:

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

if use JPA I recommend change to lowercase schema, table and column names, you can use next intructions for help you:

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;
知足的幸福 2025-02-01 11:05:51

您可以尝试以大写字母命名的表和列命名。 (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))";

You can try this example for table and column naming in capital letters. (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))";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文