SQL PostgreSQL 列名是否区分大小写?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20878932/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Are PostgreSQL column names case-sensitive?
提问by 5122014009
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.
我有一个 db 表,persons
在 Postgres 中由另一个具有列名的团队传下来,例如,"first_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?
不确定我是否在做一些愚蠢的事情,或者是否有解决我遗漏的问题的解决方法?
回答by Erwin Brandstetter
All identifiers(including column names) that are not double-quoted are folded to lower case in PostgreSQL. Column names that were created with double-quotes and thereby retained upper-case letters (and/or other syntax violations) have to be double-quoted for the rest of their life: ("first_Name"
)
所有没有被双引号引用的标识符(包括列名)在 PostgreSQL 中都被折叠成小写。使用双引号创建并因此保留大写字母(和/或其他语法违规)的列名必须在其余生中使用双引号:( "first_Name"
)
So, yes, PostgreSQL column names are case-sensitive:
所以,是的,PostgreSQL 列名是区分大小写的:
SELECT * FROM persons WHERE "first_Name" = 'xyz';
Also fix the incorrect double-quotes around 'xyz'
. Values(string literals) are enclosed in single quotes.
还要修复'xyz'
.周围不正确的双引号。值(字符串文字)用单引号括起来。
My standing advice is to use legal, lower-case names exclusively so double-quoting is not needed.
我的长期建议是只使用合法的小写名称,这样就不需要双引号了。
回答by Eugene Yarmash
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;
关键字和不带引号的标识符不区分大小写。所以:
UPDATE MY_TABLE SET A = 5;
可以等价地写为:
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.
引用标识符使其区分大小写,而未引用的名称始终折叠为小写(与 SQL 标准中未引用的名称折叠为大写不同)。例如,标识符FOO
、foo
和"foo"
被 PostgreSQL 认为是相同的,但"Foo"
和"FOO"
与这三个不同,并且彼此不同。
If you want to write portable applications you are advised to always quote a particular name or never quote it.
如果您想编写可移植的应用程序,建议您始终引用特定名称或从不引用它。
回答by randomness
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.
混合大小写或大写的列名必须在 PostgresQL 中用双引号引起来。所以最好的约定是用下划线跟随所有小案例。