232

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.

3 Answers 3

347

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.

Sign up to request clarification or add additional context in comments.

8 Comments

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.
Better yet, obstain from using reserved words or mixed case as identifiers and you will never have to use the double quotes or get strange error messages.
@ErwinBrandstetter Problem is when you work on an established project.
@HoàngLong yes it does. update "user" set "password" = 'value...'; works perfectly fine...
|
11

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.

Comments

-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"});

8 Comments

@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.
@questionto42 I am not sure if I understand. You want, that I move the comment into the answer?
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."
Throwing Java code at this question is not only not useful, it's terrifying...
I think adding some utility code (in whatever language) to aid in avoiding issues with the OPs question in hand is actually very useful.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.