0

I'm trying to create a table using mySQL and Java. What I have is:

String sql = "CREATE TABLE IF NOT EXISTS " + Userinput.getTableName2() +
" (participant INT(255), " +
" 0 INT(255),"+ 
" name INT(3), " + 
" occurances INT(255))";
stmt.executeUpdate(sql);

The Zero naming the second column is arbitrary, but I will need to have the column name be an integer. The error I'm getting is:

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 '0 INT(255), name INT(3), occurances INT(255))' at line 1

I would appreciate any and all help! Thanks!

2 Answers 2

4

According to this http://dev.mysql.com/doc/refman/5.0/en/identifiers.html

Identifiers may begin with a digit but unless quoted may not consist solely of digits.

so you have to quote the 0

String sql = "CREATE TABLE IF NOT EXISTS " + Userinput.getTableName2() +
" (participant INT(255), " +
" `0` INT(255),"+                     // using backticks
" name INT(3), " + 
" occurances INT(255))";
stmt.executeUpdate(sql);

Although, it seems a dumb name for a column.

Sign up to request clarification or add additional context in comments.

4 Comments

The better way to quote the identifier is using backticks. To quote the documentation: "The identifier quote character is the backtick (“`”)" (dev.mysql.com/doc/refman/5.7/en/identifiers.html).
Hi, I had tried adding the single quotes in before as per the documentation, but I receive a very similar error when i do so. "MySQL server version for the right syntax to use near ''0' INT(255), name INT(3), occurances INT(255))' at line 1"
@GordonLinoff Using backsticks seemed to fix my error! Thanks! and thank you user2310289 for your suggestion too!
+1 For being right, but mostly because 0 really does seem like a dumb name for a column.
0

Please read mysql documentation:

http://dev.mysql.com/doc/refman/5.1/en/identifiers.html

Identifiers may begin with a digit but unless quoted may not consist solely of digits.

Comments

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.