Escape column names in SQL database
Does SQL have a standardized way to escape column names? If not, what methods are supported by MySQL, SQLite and SQL Server?
SQL standards and identifier delimiters
The SQL:1999 standard states that identifiers, including column names, must be enclosed in double quotes (") as delimiters.
<code><delimited identifier=""> ::= <double quote=""><delimited body="" identifier=""><double quote=""></double></delimited></double></delimited></code>
Support for delimited column names
Most popular SQL databases, including Oracle, PostgreSQL, MySQL, MSSQL, and SQLite, support the use of double quotes as identifier delimiters. However, not all databases use this as the default setting.
MySQL
In MySQL, to use double quotes as column name delimiters, ANSI mode must be enabled.
SQL Server
In SQL Server, double quotes are supported as column name delimiters only when the QUOTED_IDENTIFIER option is set to ON.
Example usage
To use double quotes to escape column names in these databases, simply surround the column name with quotes.
<code>SELECT * FROM "table_name" WHERE "column_name" = 'value';</code>
Note:
Although double quotes are the standard way to escape column names, some databases may also support other delimiters, such as square brackets ([ and ]). However, it is generally recommended to use double quotes to ensure compatibility and consistency across different databases.
The above is the detailed content of How Do I Escape Column Names in MySQL, SQLite, and SQL Server?. For more information, please follow other related articles on the PHP Chinese website!