This question's answers are a community effort. Edit existing answers to improve this post. It is not currently accepting new answers or interactions.
I'm trying to execute a simple MySQL query as below:
INSERT INTO user_details (username, location, key)
VALUES ('Tim', 'Florida', 42)
But I'm getting the following error:
ERROR 1064 (42000): 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 'key) VALUES ('Tim', 'Florida', 42)' at line 1
How can I fix the issue?
The Problem
In MySQL, certain words like SELECT, INSERT, DELETE etc. are reserved words. Since they have a special meaning, MySQL treats it as a syntax error whenever you use them as a table name, column name, or other kind of identifier - unless you surround the identifier with backticks.
As noted in the official docs, in section 10.2 Schema Object Names (emphasis added):
Certain objects within MySQL, including database, table, index, column, alias, view, stored procedure, partition, tablespace, and other object names are known as identifiers.
...
If an identifier contains special characters or is a reserved word, you must quote it whenever you refer to it.
...
The identifier quote character is the backtick ("`"):
A complete list of keywords and reserved words can be found in section 10.3 Keywords and Reserved Words. In that page, words followed by "(R)" are reserved words. Some reserved words are listed below, including many that tend to cause this issue.
ADD
AND
BEFORE
BY
CALL
CASE
CONDITION
DELETE
DESC
DESCRIBE
FROM
GROUP
IN
INDEX
INSERT
INTERVAL
IS
KEY
LIKE
LIMIT
LONG
MATCH
NOT
OPTION
OR
ORDER
PARTITION
RANK
REFERENCES
SELECT
TABLE
TO
UPDATE
WHERE
The Solution
You have two options.
1. Don't use reserved words as identifiers
The simplest solution is simply to avoid using reserved words as identifiers. You can probably find another reasonable name for your column that is not a reserved word.
Doing this has a couple of advantages:
It eliminates the possibility that you or another developer using your database will accidentally write a syntax error due to forgetting - or not knowing - that a particular identifier is a reserved word. There are many reserved words in MySQL and most developers are unlikely to know all of them. By not using these words in the first place, you avoid leaving traps for yourself or future developers.
The means of quoting identifiers differs between SQL dialects. While MySQL uses backticks for quoting identifiers by default, ANSI-compliant SQL (and indeed MySQL in ANSI SQL mode, as noted here) uses double quotes for quoting identifiers. As such, queries that quote identifiers with backticks are less easily portable to other SQL dialects.
Purely for the sake of reducing the risk of future mistakes, this is usually a wiser course of action than backtick-quoting the identifier.
2. Use backticks
If renaming the table or column isn't possible, wrap the offending identifier in backticks (`) as described in the earlier quote from 10.2 Schema Object Names.
An example to demonstrate the usage (taken from 10.3 Keywords and Reserved Words):
mysql> CREATE TABLE interval (begin INT, end INT);
ERROR 1064 (42000): You have an error in your SQL syntax.
near 'interval (begin INT, end INT)'
mysql> CREATE TABLE `interval` (begin INT, end INT);
Query OK, 0 rows affected (0.01 sec)
Similarly, the query from the question can be fixed by wrapping the keyword key in backticks, as shown below:
INSERT INTO user_details (username, location, `key`)
VALUES ('Tim', 'Florida', 42)"; ^ ^
I have BigQuery with column name order.
In this case when I try to select this column I have the error: Syntax error: Unexpected keyword ORDER.
If it some way around this error, the way where BigQuery takes it as the name of the column and not a function.
From (Oops! used a reserved word to name a column) use either backticks (``) for standard sql or square brackets ([]) for legacy sql to escape the name.
I am trying to update a table in pgadmin 3 (postgres 9.4) called: assay.Luminex.Luminex_GT_shared.Analyte
I kept getting the error:
ERROR: improper qualified name (too many dotted names):
assay.luminex.luminex_gt_shared.analyte
I have tested backquotes and brackets to no avail, keep getting syntax errors:
SELECT * FROM `assay.Luminex.Luminex_GT_shared.Analyte`;
SELECT * FROM [assay.Luminex.Luminex_GT_shared.Analyte];
I am a newb to SQL, I am just trying to make a simple update to a table and getting stuck on this.
As documented in the manual in SQL, non-standard identifiers have to be quoted with double quotes:
SELECT *
FROM "assay.Luminex.Luminex_GT_shared.Analyte";
But you should really avoid names like that.
When I try to create a table with the following as a column name: "Admin?" in my SQLite Database, I get a SQL error saying:
SQL error or missing database (near "?": syntax error)
Here is my SQL statement:
CREATE TABLE test (admin? TEXT PRIMARY KEY);
I am assuming I cannot have question marks inside column names, but I cannot find any documentation about it. If this is the issue, is there any workaround here, or a better solution? Also, I am creating the table from Kotlin code, so I am not sure if that has any effect on it.
Escape the column name:
CREATE TABLE test ("admin?" TEXT PRIMARY KEY);
I would like to add a field with:
ALTER TABLE Table_name ADD COLUMN ABCD012345(en) Memo;
Here is described what to do in that case:
To work arond this problem, do not use special characters. If you must use special characters in query expressions, enclose the special characters in brackets ([]). For example, if you want to use the greater than sign (>), use [>].
As I understand it I would Need to Change it to:
ALTER TABLE Table_name ADD COLUMN ABCD012345[(]en[)] Memo;
But this still leads to the same error: Syntax error in field Definition. If you Need a little more context: I do this with a ADODB.Recordset in a VBA macro.
Where do I go wrong? How can I solve it?
You need to surround the entire name with brackets, not just the special characters:
ALTER TABLE Table_name ADD COLUMN [ABCD012345(en)] Memo;
-- Here --------------------------^--------------^