Escaping keyword-like column names in Postgres

前端 未结 3 754
一整个雨季
一整个雨季 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

    If you are not providing quotes in any Fields/Columns, It will be lowercased by Postgres by default. And Postgres will skip checking keyword when it comes to the column name.

    In your case, I don't think it's mandatory to add quotes when it comes to the columns. But if you are using keywords (registered by Postgres) as the name of Table, Schema, Function or Trigger etc, you must have to use either double quotes, or you can specify schema name with dot concatenation.

    Let's Suppose, order is the keyword registered by Postgres. And in some scenarios, you must have to use this keyword as a table name.

    At that time, Postgres will allow you to create a table with keywords. That is the beauty of Postgres.

    To access the order table, Either you have to use a Double quote or you can you schema name before table name.

    E.G.

    1.

    select * from schema_name.order;
    

    2.

    select * from "order";
    

    Likewise, you can use this type of combination. Hope this will help you.

提交回复
热议问题