省略双引号对 PostgreSQL 进行查询
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6331504/
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
Omitting the double quote to do query on PostgreSQL
提问by zfm
Simple question, is there any way to omit the double quote in PostgreSQL?
简单的问题,有什么办法可以省略PostgreSQL中的双引号吗?
Here is an example, giving select * from A;
, I will retrieve ERROR: relation "a" does not exist
, and I would have to give select * from "A";
to get the real result.
这是一个例子,给予select * from A;
,我将检索ERROR: relation "a" does not exist
,我必须给予select * from "A";
才能获得真正的结果。
Is there any way not to do the second and instead do the first on PostgreSQL?
有没有办法不做第二个,而是在 PostgreSQL 上做第一个?
回答by Steve Prentice
Your problem with this query started when you created your table. When you create your table, don't use quotes.
当您创建表时,此查询的问题就开始了。创建表时,不要使用引号。
Use this:
用这个:
CREATE TABLE a ( ... );
Not this:
不是这个:
CREATE TABLE "A" ( ... );
The latter will make it so that you always have to quote it later. The former makes it a normal name and you can use SELECT * FROM a;
or SELECT * FROM A;
后者将使您以后总是必须引用它。前者使其成为普通名称,您可以使用SELECT * FROM a;
或SELECT * FROM A;
If you can't just recreate your table, use the ALTER TABLE
syntax:
如果您不能只是重新创建表,请使用以下ALTER TABLE
语法:
ALTER TABLE "A" RENAME TO a;
回答by David Chan
double quotes are required if you include capital letters in your table name in postgres
如果在 postgres 中的表名中包含大写字母,则需要双引号
to avoid the requirements name your table "a"
避免要求将您的表命名为“a”
回答by leonbloy
Postgresql has some particular behaviourin regard to quoting and case sentivity: it folds every non-quoted identifier to lower case (also at creation time) and then works case-sensitively.
Postgresql在引用和区分大小写方面有一些特殊的行为:它将每个未引用的标识符折叠为小写(也在创建时),然后区分大小写。
Double quotes in identifiers are only needed when the identifier (table name, column name, etc) was defined (at schema creation time) with uppercase letters (some or all) and between double quotes.
标识符中的双引号仅在标识符(表名、列名等)被定义(在模式创建时)使用大写字母(部分或全部)并在双引号之间时才需要。
In that case (which I advice against), when you use that identifier, you must type it in the same way: case sensitively (type upper/lower case letter exactly as defined) and between double quotes.
在那种情况下(我建议反对),当您使用该标识符时,您必须以相同的方式输入它:区分大小写(完全按照定义输入大写/小写字母)和双引号之间。
In other cases, you can use non-quoted identifiers and work always case-insensitively.
在其他情况下,您可以使用不带引号的标识符并始终不区分大小写。
回答by Sandy
Don't use upper case letter in your table name or it's column name, if you are using such thing then the postgres will required double quote for accessing it.
不要在你的表名或列名中使用大写字母,如果你使用这样的东西,那么 postgres 将需要双引号来访问它。
回答by Karatheodory
Please see the detailed description of what is happening here.
请参阅此处发生的情况的详细说明。
The PostgreSQL server table names are case-sensitive, but forced to be lower-case by default: when you type CREATE TABLE AAA
, it will become CREATE TABLE aaa
before the query execution.
PostgreSQL 服务器表名区分大小写,但默认强制为小写:当您键入 时CREATE TABLE AAA
,它将CREATE TABLE aaa
在查询执行之前变为。
Double-quoted names keep their case as it was, so after CREATE TABLE "AaA"
you get the table AaA
and have to write it double-quoted again and again.
双引号名称保持原样,因此在CREATE TABLE "AaA"
您拿到表格后AaA
,必须一次又一次地将其写成双引号。
Have no idea why did they do so :)
不知道他们为什么这样做:)