如何在jdbc的表中包含两个外键?

问题描述 投票:0回答:1

我在eclipse IDE中使用JDBC,我想在表3中放入两个外键,一个在引用表1中的主键,一个在引用表2中的主键。当我只放置一个任何引用table1或table 2的外键约束,它都可以正常工作,但是当我包括两个表时,它给了我SQL异常,如下所示:

java.sql.SQLSyntaxErrorException:您的SQL错误句法;检查与您的MySQL服务器版本相对应的手册有关在'外键(path)引用附近使用的正确语法的信息第1行的Song(path))'

enter code here

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) )";
java database jdbc foreign-keys
1个回答
0
投票

您似乎没有使用正确的语法,这可能是...

CREATE TABLE TableName ( 
    T1 varchar(50),
    T2 varchar(50),
    foreign key(T1) references Table1(T1),
    foreign key(T2) references Table2(T2) 
);

为您的代码格式化,看起来像这样:

String createString = "CREATE TABLE " this.tableName + " ( " +
    "   T1 varchar(50)," +
    "   T2 varchar(50)," +
    "   foreign key(T1) references Table1(T1)," +
    "   foreign key(T2) references Table2(T2));";
© www.soinside.com 2019 - 2024. All rights reserved.