[英]How to include two foreign keys in a table in jdbc?
I'm using JDBC in eclipse IDE, i want to put two foreign keys in my table 3, one is referencing to the primary key in table 1 and one is referencing to the primary key in table 2. When i only put one foreign key constrains for any referencing table1 or table 2, it works fine but when i include two it gives me sql exception as stated below: I'm using JDBC in eclipse IDE, i want to put two foreign keys in my table 3, one is referencing to the primary key in table 1 and one is referencing to the primary key in table 2. When i only put one foreign key限制任何引用表 1 或表 2,它工作正常,但是当我包含两个时,它给了我 sql 异常,如下所述:
java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax;
java.sql.SQLSyntaxErrorException:您的 SQL 语法有错误; check the manual that corresponds to your MySQL server version for the right syntax to use near 'foreign key(
T2
) references Table2(T2
) )' at line 1检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的“外键(
T2
)引用表2(T2
))附近使用正确的语法
String createString =
// TABLE 1
"CREATE TABLE " + this.tableName + " ( " +
"T1 varchar(50) NOT NULL PRIMARY KEY )";
// TABLE 2
"CREATE TABLE " + this.tableName + " ( " +`enter code here`
"T2 varchar(50) NOT NULL PRIMARY KEY )";
// TABLE 3
"CREATE TABLE " + this.tableName + " ( " +
"T1 varchar(50) " +
"T2 varchar(50) " +
"foreign key(T1) references Table1 (T1)" +
"foreign key(T2) references Table2(T2) )";
First, this is actually a MySQL question, unrelated to Java/JDBC.首先,这实际上是一个 MySQL 问题,与 Java/JDBC 无关。 Secondly, and more importantly, you don't appear to be using the correct syntax, which would be...
其次,更重要的是,您似乎没有使用正确的语法,这将是......
CREATE TABLE TableName (
T1 varchar(50),
T2 varchar(50),
foreign key(T1) references Table1(T1),
foreign key(T2) references Table2(T2)
);
Formatted for your code, it would look like this:为您的代码格式化,它看起来像这样:
String createString = "CREATE TABLE " + this.tableName + " ( " +
" T1 varchar(50)," +
" T2 varchar(50)," +
" foreign key(T1) references Table1(T1)," +
" foreign key(T2) references Table2(T2));";
You were missing commas after each item in the items list for your CREATE TABLE
statement.您在
CREATE TABLE
语句的项目列表中的每个项目后都缺少逗号。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.