MYSQL error with Foreign Key -
i trying create simple table using jdbc, gives me error. code :
public class test { public static void main(string[] args) throws classnotfoundexception, sqlexception { class.forname("com.mysql.jdbc.driver"); connection con = drivermanager.getconnection("jdbc:mysql://localhost/javabook","scott","tiger"); system.out.println("part 1:done !"); statement statement = con.createstatement(); string query = "create table enrollment (ssn char(9), courseid char(5), dateregistered date, grade char(2)"+ "primary key (ssn, courseid), foreign key (ssn) references student (ssn), foreign key (courseid) references course(courseid)"; statement.executeupdate(query); system.out.println("part 2:done !"); } }
i getting error :
exception in thread "main" com.mysql.jdbc.exceptions.mysqlsyntaxerrorexception: have error in sql syntax; check manual corresponds mysql server version right syntax use near '' @ line 1 @ com.mysql.jdbc.sqlerror.createsqlexception(sqlerror.java:936) @ com.mysql.jdbc.mysqlio.checkerrorpacket(mysqlio.java:2985) @ com.mysql.jdbc.mysqlio.sendcommand(mysqlio.java:1631) @ com.mysql.jdbc.mysqlio.sqlquerydirect(mysqlio.java:1723) @ com.mysql.jdbc.connection.execsql(connection.java:3277) @ com.mysql.jdbc.statement.executeupdate(statement.java:1402) @ com.mysql.jdbc.statement.executeupdate(statement.java:1317) @ test.main(test.java:17)
you have missed closing parenthesis ')' in code. use below statement in code.
string query = "create table enrollment (ssn char(9), courseid char(5), dateregistered date, grade char(2)"+ "primary key (ssn, courseid), foreign key (ssn) references student (ssn), foreign key (courseid) references course(courseid))";
please make sure closing parenthesis.
Comments
Post a Comment