Postgres列或表名称何时需要加引号,什么时候不需要引号? [英] When do Postgres column or table names need quotes and when don't they?
问题描述
让我们考虑以下postgres查询:
Let's consider the following postgres query:
SELECT *
FROM "MY_TABLE"
WHERE "bool_var"=FALSE
AND "str_var"='something';
当我删除 str_var <时,查询无法正确响应/ code>,但是当我在
bool_var
周围做同样的事情时,却不是。为什么?在这种情况下写查询的正确方法是什么,布尔列不带引号,文本列不带引号?
The query fails to respond properly when I remove quotes around "str_var"
but not when I do the same around "bool_var"
. Why? What is the proper way to write the query in that case, no quotes around the boolean column and quotes around the text column? Something else?
推荐答案
感谢@TimBiegeleisen的评论,我能够找到问题所在。我使用保留关键字(用户)作为列名。
Thanks to @TimBiegeleisen's comment, I was able to pinpoint the problem; I used a reserved keyword ("user") as a column name.
链接到文档中的保留关键字: https://www.postgresql.org/docs/current/sql-keywords-appendix.html 。
Link to reserved keywords in the doc: https://www.postgresql.org/docs/current/sql-keywords-appendix.html.
现在知道不要使用引号来查询列名,而要避免使用保留关键字作为列名。
Now I know not to use quotes to query column names, but rather to avoid reserved keywords as column names.
这篇关于Postgres列或表名称何时需要加引号,什么时候不需要引号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!