PostgreSQL 中的单引号和双引号有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41396195/
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
What is the difference between single quotes and double quotes in PostgreSQL?
提问by CelinVeronicca
I am new to PostgresSQL.I tried
我是 PostgresSQL 的新手。我试过了
select * from employee where employee_name="elina";
But that results error as follows:
但结果错误如下:
ERROR: column "elina" does not exist.
Then I tried by replacing double quotes with single quotes as follows:
然后我尝试用单引号替换双引号,如下所示:
select * from employee where employee_name='elina';
It result fine..So what is the difference between single quotes and double quotes in postgresql.If we can't use double quotes in postgres query,then if any other use for this double quotes in postgreSQL?
结果很好..那么 postgresql 中的单引号和双引号有什么区别。如果我们不能在 postgres 查询中使用双引号,那么在 postgreSQL 中这个双引号还有其他用途吗?
回答by Michas
Double quotes are for names of tables or fields. Sometimes You can omit them. The single quotes are for string constants. This is the SQL standard. In the verbose form your query look like this:
双引号用于表或字段的名称。有时您可以省略它们。单引号用于字符串常量。这是 SQL 标准。在详细形式中,您的查询如下所示:
select * from "employee" where "employee_name"='elina';
回答by melpomene
As explained in the PostgreSQL manual:
A string constant in SQL is an arbitrary sequence of characters bounded by single quotes (
'
), for example'This is a string'
. To include a single-quote character within a string constant, write two adjacent single quotes, e.g.,'Dianne''s horse'
. Note that this is not the same as a double-quote character ("
).
SQL 中的字符串常量是由单引号 (
'
)包围的任意字符序列,例如'This is a string'
。要在字符串常量中包含单引号字符,请编写两个相邻的单引号,例如'Dianne''s horse'
. 请注意,这与双引号字符 ("
) 不同。
Elsewhere on the same page:
在同一页面的其他地方:
There is a second kind of identifier: the delimited identifieror quoted identifier. It is formed by enclosing an arbitrary sequence of characters in double-quotes (
"
). A delimited identifier is always an identifier, never a key word. So"select"
could be used to refer to a column or table named "select", whereas an unquotedselect
would be taken as a key word and would therefore provoke a parse error when used where a table or column name is expected.
还有第二种标识符:分隔标识符或带引号的标识符。它是通过将任意字符序列括在双引号 (
"
) 中形成的。分隔标识符始终是标识符,而不是关键字。So"select"
可用于引用名为“select”的列或表,而未加引号的select
将被视为关键字,因此在需要表名或列名的地方使用时会引发解析错误。
TL;DR: Single quotes for string constants, double quotes for table/column names.
TL;DR:字符串常量用单引号,表/列名用双引号。
回答by Rahul
Well single quotes are used for string literals and double quotes are used for escaping DB objects like table name / column name etc.
单引号用于字符串文字,双引号用于转义数据库对象,如表名/列名等。
Specifically, double quotes are used for escaping a column/table name if it's resemble to any reserve/key word. Though every RDBMS have their own way of escaping the same (like backtique in MySQL
or square bracket in SQL Server
) but using double quotes is ANSI standard.
具体来说,双引号用于转义列/表名称,如果它类似于任何保留/关键字。尽管每个 RDBMS 都有自己的转义方式(如反引号MySQL
或方括号SQL Server
),但使用双引号是 ANSI 标准。