Home >Database >Mysql Tutorial >How Do I Execute SQL Queries on Tables with Reserved Keywords in MySQL?
Executing SQL Queries on Tables with Protected Keywords in MySQL
When attempting to execute an SQL query on a table that shares the same name as a protected keyword in MySQL, developers may encounter syntax errors. This is because protected keywords are reserved by the database system for specific purposes.
Example Issue:
The following query attempts to select data from a table named "order":
mysql_query("SELECT * FROM order WHERE orderID = 102;");
However, this query fails with the error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'order WHERE orderID = 102' at line 2
Solution: Escaping Keywords
To successfully query tables with names that conflict with reserved keywords, enclose the table name in escape characters, such as backticks (`). This signals to MySQL that the enclosed identifier should be interpreted as a table name, not a keyword.
mysql_query("SELECT * FROM `order` WHERE orderID = 102;");
Reserved Keywords in MySQL
To avoid potential conflicts, it's recommended to avoid using reserved keywords as table or field names. A comprehensive list of reserved keywords can be found at https://dev.mysql.com/doc/refman/5.5/en/keywords.html.
The above is the detailed content of How Do I Execute SQL Queries on Tables with Reserved Keywords in MySQL?. For more information, please follow other related articles on the PHP Chinese website!