202

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 table (id, name, year) VALUES ( ... ); gives an error near the year word.

shybovycha
  • 11,556
  • 6
  • 52
  • 82

3 Answers3

309

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.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • 63
    Some warning: Without the quotes PostgreSQL folds all identifiers to lowercase. `MyTable`, `myTable` and `mytable` are just the same. _With_ the quotes this folding is not done. So `"MyTable"` is no more the same as `mytable`. – A.H. Oct 04 '11 at 18:31
  • 28
    Better yet, obstain from using [reserved words](http://www.postgresql.org/docs/current/interactive/sql-keywords-appendix.html) or mixed case as identifiers and you will never have to use the double quotes or get strange error messages. – Erwin Brandstetter Oct 04 '11 at 21:15
  • I thought `SELECT "year"` would have returned the string `year`, but I guess that was a MySQL thing. – djjeck Dec 11 '12 at 19:25
  • 2
    @djjeck: http://stackoverflow.com/questions/1992314/what-is-the-difference-between-single-and-double-quotes-in-sql – NPE Dec 11 '12 at 19:31
  • 1
    Better not use reserved words at all. The trick here doesn't work with UPDATE table SET .. – Hoàng Long Mar 30 '13 at 13:58
  • 14
    @ErwinBrandstetter Problem is when you work on an established project. – ceruleus Aug 19 '16 at 08:06
  • 6
    @HoàngLong yes it does. `update "user" set "password" = 'value...';` works perfectly fine... – Phill Feb 09 '17 at 04:34
  • 1
    Mostly I got bumped with column name as timestamp, this helped a lot. – Deep LF Aug 16 '18 at 06:46
9

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.

Mayur
  • 4,345
  • 3
  • 26
  • 40
-5

To be on the safe side: Always quote identifiers! For this you have to build the insert statement with delimited identifiers.

SQL 2003 specifies that a delimited identifier should be quoted with double quotes " and if a double quote occurs in the identifier, the double quote must be duplicated. See the BNF:

https://ronsavage.github.io/SQL/sql-2003-2.bnf.html#delimited%20identifier

This is Java code to quote the identifier:

static String delimited_identifier (String identifier)
{
  return "\"" + identifier.replaceAll ("\"", "\"\"") + "\"";
}

And this is the code to build the insert:

static String build_insert (String table, String[] columns)
{
  StringBuilder sql = new StringBuilder ();
  StringBuilder values = new StringBuilder ();

  sql.append ("INSERT INTO ");
  sql.append (delimited_identifier (table));
  sql.append (" (");
  int c = 0;
  if (columns.length > 0) {
    sql.append (delimited_identifier (columns[c]));
    values.append ("?");
  }
  for (++c; c < columns.length; c++) {
    sql.append (", ");
    sql.append (delimited_identifier (columns[c]));
    values.append (", ?");
  }
  sql.append (") VALUES (");
  sql.append (values.toString ());
  sql.append (")");

  return sql.toString ();
}

Example:

String sql = build_insert ("Person", new String[]{"First name", "Last name"});
ceving
  • 21,900
  • 13
  • 104
  • 178
  • @questionto42 There is no need to distinguish between two cases. Just quote every identifier. It is almost impossible to know all reserved words of all databases. And I am not sure what you mean by "inverted commas". The right characters to quote are "double quotes" (`"`). MySQL violates the SQL standard by the use of backquotes (`\``). My answer covers the correct way to quote. – ceving Jun 02 '21 at 09:02
  • @questionto42 I am not sure if I understand. You want, that I move the comment into the answer? – ceving Jun 02 '21 at 19:15
  • I would understand your answer much better when there was an introduction like "There is no need to distinguish between two cases. Just quote every identifier. It is almost impossible to know all reserved words of all databases." – questionto42 Jun 04 '21 at 17:15
  • Throwing Java code at this question is not only not useful, it's terrifying... – cpursley Dec 02 '21 at 22:11