4

Ok I have a very simple mysql database but when i try to run this query via mysql-admin i get weird errors

INSERT INTO customreports (study, type, mode, select, description) VALUES ('1', '2', '3', '4', '5');

Error:

1064 - 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 'select, description) VALUES ('1', '2', '3', '4', '5')' at line 1

7 Answers 7

15

You're having problems because you're using SQL reserved words as column names and not escaping them. Try like this:

INSERT INTO `customreports`
(`study`, `type`, `mode`, `select`, `description`)
VALUES
('1', '2', '3', '4', '5');
Sign up to request clarification or add additional context in comments.

2 Comments

+1 this is the correct answer, but I would definitely change the field names to non-reserved words. Using reserved words for object names leads to headaches for you and everyone who comes after you.
This is indeed the correct answer. However, I wouldn't advise changing the column names just for the sake of not using the reserved words, it is good practice to escape all identifiers and it will save you a ton of trouble. Although I must admit, that 'select' as a column name is a bit, you know, over the top.
3

Yeah, I would rewrite as:

INSERT INTO [customreports] ([study], [type], [mode], [select], [description]) VALUES ('1', '2', '3', '4', '5');

Comments

0

just a guess, but is reserved word "select" (listed as a column name) causing a problem?

2 Comments

This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post.
@il_guru, look at the selected answer and then mine, notice any similarity?
0

"SELECT" is a reserved word in SQL... use a different word than "select"

Comments

0

The word 'select' is reserved in sql. The interpreter thinks you're trying to use a SELECT statement inside the INSERT INTO statement. You need to enclose the field names so that the interpreter doesn't read them as commands.

Update for MySQL:

insert into customreports ('study','type','mode','select','description') values...

Comments

0

Correct per Chaos... but the critical thing to remember is you should do your best to NOT use RESERVED words as column names in a table... such as SELECT and TYPE.... not positive of the other three, but that's where your conflict is. Yes, by being explicit with quotes around the fields will tell the SQL you mean the field within, but its also best to know WHY its failing, and not just a work-around resolution... helps prevent similar issues in the future.

Comments

0

Ditto the above, but I believe you need double quotes around the column names, not single quotes. Perhaps some flavors of SQL will process the single quotes correctly, but I think the standard says double quotes for field names, single quotes for field values.

1 Comment

No, this is mysql and mysql accepts backticks as identifier escape characters.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.