Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL standard to escape column names?

Tags:

sql

People also ask

How do you write SQL queries with columns in space names?

To select a column name with spaces, use the back tick symbol with column name. The symbol is ( ` `).

How do you handle SQL column names that look like SQL keywords?

In MySQL, alternatively to using back quotes (`), you can use the UI to alter column names. Right click the table > Alter table > Edit the column name that contains sql keyword > Commit.

How do you escape keywords in SQL?

To escape reserved keywords in SQL SELECT statements and in queries on views, enclose them in double quotes ('').


Quotation Mark "

The SQL:1999 standard specifies that double quote (") (QUOTATION MARK) is used to delimit identifiers.

<delimited identifier> ::= <double quote> <delimited identifier body> <double quote>

Oracle, PostgreSQL, MySQL, MSSQL and SQlite all support " as the identifier delimiter.

They don't all use " as the 'default'. For example, you have to be running MySQL in ANSI mode and SQL Server only supports it when QUOTED_IDENTIFIER is ON.


According to SQLite,

  • 'foo' is an SQL string
  • "foo" is an SQL identifier (column/table/etc)
  • [foo] is an identifier in MS SQL
  • `foo` is an identifier in MySQL

For qualified names, the syntax is: "t"."foo" or [t].[foo], etc.

MySQL supports the standard "foo" when the ANSI_QUOTES option is enabled.


For MySQL, use back ticks `.

For instance:

SELECT `column`, `column2` FROM `table`

For MS SQL use [ and ]

SELECT [COLUMN], [COLUMN 2] FROM [TABLE]