在 Postgres 中转义类似关键字的列名 [英] Escaping keyword-like column names in Postgres
问题描述
如果 Postgres 表中的列名称为 year
,那么 INSERT
查询应该如何设置该列的值?
If the column in Postgres' table has the name year
, how should look INSERT
query to set the value for that column?
例如:INSERT INTO table (id, name, year) VALUES ( ... );
在 year 词附近给出错误.
E.g.: INSERT INTO table (id, name, year) VALUES ( ... );
gives an error near the year word.
推荐答案
只需将 year
括在双引号中即可防止它被解释为 关键字:
Simply enclose year
in double quotes to stop it being interpreted as a keyword:
INSERT INTO table (id, name, "year") VALUES ( ... );
来自文档:
还有第二种标识符:分隔标识符或引用标识符.它是由包含任意序列的双引号 (") 中的字符.分隔标识符始终是标识符,从来都不是关键字.所以选择"可以用来指代一个名为select"的列或表,而未加引号的选择将是作为关键字,因此会在以下情况下引发解析错误在需要表名或列名的地方使用.
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.
这篇关于在 Postgres 中转义类似关键字的列名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!