gpt4 book ai didi

MYSQL 外键错误

转载 作者:行者123 更新时间:2023-11-29 22:30:21 25 4
gpt4 key购买 nike

我正在尝试使用 jdbc 创建一个简单的表,但它给了我一个错误。这是我的代码:

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 !");
}
}

我收到此错误:

Exception in thread "main" com.mysql.jdbc.exceptions.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 '' at line 1
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2985)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1631)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1723)
at com.mysql.jdbc.Connection.execSQL(Connection.java:3277)
at com.mysql.jdbc.Statement.executeUpdate(Statement.java:1402)
at com.mysql.jdbc.Statement.executeUpdate(Statement.java:1317)
at Test.main(Test.java:17)

最佳答案

您在代码中漏掉了右括号“)”。在代码中使用以下语句。

字符串查询=“创建表Enrollment(ssn char(9),courseId char(5),date注册日期,grade char(2)”+ “主键(ssn,courseId),外键(ssn)引用Student(ssn),外键(courseId)引用Course(courseId))”;

请确保右括号。

关于MYSQL 外键错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29864601/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com