我想嘗試一些顯然很簡單,但我只是繼續有同樣的錯誤「無法添加外鍵約束」,任何人都可以幫助我嗎?我用的工作臺與SQL無法添加外鍵約束簡單的代碼幫助請
drop table if exists table2;
create table if not exists table2(
id_kind int not null,
id_bod int not null,
id_doc int not null,
primary key (id_kind, id_bod, id_doc)
)engine=InnoDB default charset=latin1;
drop table if exists table1;
create table if not exists table1(
id_mov int not null,
id_kind int not null,
id_prod int,
id_bod int not null,
id_doc int not null,
primary key (id_mov),
key id_kind (id_kind),
key id_bod (id_bod),
key id_doc (id_doc),
foreign key table1 (id_kind) references table2 (id_kind),
foreign key table1 (id_bod) references table2 (id_bod),
foreign key table1 (id_doc) references table2 (id_doc)
)engine=InnoDB default charset=latin1;
id_kind需要爲表1的主鍵,這是不是讓你無法創建FK約束 –
應該是'外鍵(id_kind)參考表1(id_mov)' –
@RichardHansell這是通常的做法,但我很確定外鍵可以引用任何相同類型的列。 – Arth