在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.
推荐答案
Simply enclose year
in double quotes to stop it being interpreted as a keyword:
INSERT INTO table (id, name, "year") VALUES ( ... );
从文档:
还有第二种标识符:带分隔符的标识符或 带引号的标识符.它是通过包含任意顺序的 双引号()中的字符.带分隔符的标识符始终是 标识符,而不是关键字.因此,选择"可用于指代 列或表名为选择",而未引用的选择将是 用作关键字,因此在以下情况时会引发解析错误: 用于需要表名或列名的地方.
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屋!