在Postgres中转义类似关键字的列名

编程入门 行业动态 更新时间:2024-10-22 12:29:58
本文介绍了在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 ( ... );

从文档:

还有第二种标识符:带分隔符的标识符或 带引号的标识符.它是通过包含任意顺序的 双引号()中的字符.带分隔符的标识符始终是 标识符,而不是关键字.因此,选择"可用于指代 列或表名为选择",而未引用的选择将是 用作关键字,因此在以下情况时会引发解析错误: 用于需要表名或列名的地方.

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中转义类似关键字的列名

本文发布于:2023-10-25 08:22:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1526438.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:类似   关键字   Postgres

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!