Escaping keyword-like column names in Postgres

前端 未结 3 748
一整个雨季
一整个雨季 2020-11-27 12:44

If the column in Postgres\' table has the name year, how should look INSERT query to set the value for that column?

E.g.: INSERT INTO

3条回答
  •  我在风中等你
    2020-11-27 13:20

    Simply enclose year in double quotes to stop it being interpreted as a keyword:

    INSERT INTO table (id, name, "year") VALUES ( ... );
    

    From the documentation:

    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.

提交回复
热议问题