references
is a reserved word in MySQL. Either use backticks to escape it or use another name.
stmt.executeUpdate("CREATE TABLE `" + REFERENCE_TABLE + "` ("
^-----------------------^-----------here
문제
I am getting the following error when I try to create a table in an SQL database:
Failed to create table 'references'
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 'references(referenceID BIGINT NOT NULL AUTO_INCREMENT,
referenceText VARCHAR(255)'
Here is the code making the table:
public void createReferencesTable() {
System.out.println("Creating table '" + REFERENCE_TABLE + "'...");
Statement stmt = null;
try {
stmt = connection.createStatement();
stmt.executeUpdate("CREATE TABLE " + REFERENCE_TABLE + "("
+ REFERENCE_ID + " BIGINT NOT NULL AUTO_INCREMENT,"
+ REFERENCE_TEXT + " VARCHAR(255),"
+ REFERENCE_TWEET + " BIGINT NOT NULL,"
+ "PRIMARY KEY (" + REFERENCE_ID + ")"
+ ")");
System.out.println("Table '" + REFERENCE_TABLE + "' created");
} catch (SQLException e) {
System.err.println("Failed to create table '" + REFERENCE_TABLE + "'");
e.printStackTrace();
} finally {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
And here are the declarations of the variables used:
public static final String REFERENCE_TABLE = "references";
public static final String REFERENCE_ID = "referenceID";
public static final String REFERENCE_TEXT = "referenceText";
public static final String REFERENCE_TWEET = "referenceTweet";
Also, I am creating two tables before this one that successfully work with basically the exact same syntax as this one, which is failing. Also, the MySQL server I am putting this databases into is fresh and just set-up, so there shouldn't be any configuration issues, as my program is connecting to the database successfully.
My question is: Why is this giving an error?
Notes: I have tried to do the reference ID line with an identity, but that did not make any difference. As I said before, the two functions each creating a table before this used exactly the same syntax, but gave no errors and successfully created their tables.
해결책
references
is a reserved word in MySQL. Either use backticks to escape it or use another name.
stmt.executeUpdate("CREATE TABLE `" + REFERENCE_TABLE + "` ("
^-----------------------^-----------here