Escaping Keyword Column Names in PostgreSQL using Double Quotes
PostgreSQL requires special handling when dealing with column names that are also SQL keywords. To avoid syntax errors, always enclose such column names in double quotes.
For example, if your table has a column named year
, the correct INSERT
statement would be:
<code class="language-sql">INSERT INTO my_table (id, name, "year") VALUES (1, 'Example', 2024);</code>
The double quotes around "year"
explicitly tell PostgreSQL to treat it as a column name, not the keyword YEAR
.
As the PostgreSQL documentation states, delimited identifiers (those enclosed in double quotes) are always treated as identifiers, never keywords. This allows you to use column or table names that match reserved words without conflicts. Failing to quote the column name will result in a parsing error.
The above is the detailed content of How Do I Handle Keyword-Like Column Names in PostgreSQL?. For more information, please follow other related articles on the PHP Chinese website!