PostgreSQL中单引号和双引号有什么区别?

时间:2022-09-15 13:06:42

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中使用这个双引号有什么其他用途吗?

3 个解决方案

#1


13  

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';

#2


16  

As explained in the PostgreSQL manual:

正如PostgreSQL手册中所述:

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'的马'。请注意,这与双引号字符(“)不同。

Elsewhere on the same page:

在同一页面的其他地方:

There is a second kind of identifier: the delimited identifier or 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 unquoted select would be taken as a key word and would therefore provoke a parse error when used where a table or column name is expected.

存在第二种标识符:分隔标识符或带引号的标识符。它是通过用双引号(“)括起任意字符序列而形成的。分隔标识符始终是标识符,绝不是关键字。因此,”select“可用于引用名为”select“的列或表,而不带引号的选择将被视为关键字,因此在预期使用表或列名称时会引发解析错误。

TL;DR: Single quotes for string constants, double quotes for table/column names.

TL; DR:字符串常量的单引号,表/列名称的双引号。

#3


1  

Well single quotes are used for string literals and double quotes are used for escaping DB objects like table name / column name etc.

单引号用于字符串文字,双引号用于转义DB对象,如表名/列名等。

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中的backtique或SQL Server中的方括号),但使用双引号是ANSI标准。

#1


13  

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';

#2


16  

As explained in the PostgreSQL manual:

正如PostgreSQL手册中所述:

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'的马'。请注意,这与双引号字符(“)不同。

Elsewhere on the same page:

在同一页面的其他地方:

There is a second kind of identifier: the delimited identifier or 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 unquoted select would be taken as a key word and would therefore provoke a parse error when used where a table or column name is expected.

存在第二种标识符:分隔标识符或带引号的标识符。它是通过用双引号(“)括起任意字符序列而形成的。分隔标识符始终是标识符,绝不是关键字。因此,”select“可用于引用名为”select“的列或表,而不带引号的选择将被视为关键字,因此在预期使用表或列名称时会引发解析错误。

TL;DR: Single quotes for string constants, double quotes for table/column names.

TL; DR:字符串常量的单引号,表/列名称的双引号。

#3


1  

Well single quotes are used for string literals and double quotes are used for escaping DB objects like table name / column name etc.

单引号用于字符串文字,双引号用于转义DB对象,如表名/列名等。

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中的backtique或SQL Server中的方括号),但使用双引号是ANSI标准。