我得到了這兩個succesfull查詢:MySQL的 - 錯誤代碼1215,不能添加外鍵約束
create table Donors (
donor_id int not null auto_increment primary key,
gender varchar(1) not null,
date_of_birth date not null,
first_name varchar(20) not null,
middle_name varchar(20),
last_name varchar(30) not null,
home_phone tinyint(10),
work_phone tinyint(10),
cell_mobile_phone tinyint(10),
medical_condition text,
other_details text);
和
create table Donors_Medical_Condition (
donor_id int not null,
condition_code int not null,
seriousness text,
primary key(donor_id, condition_code),
foreign key(donor_id) references Donors(donor_id) );
但是當我嘗試這一個:
create table Medical_Conditions (
condition_code int not null,
condition_name varchar(50) not null,
condition_description text,
other_details text,
primary key(condition_code),
foreign key(condition_code) references Donors_Medical_Condition(condition_code));
我得到「錯誤代碼:1215,無法添加外鍵約束」
我不知道我做錯了什麼。
您正在嘗試引用複合主鍵的一部分,在最後一個表的外鍵。 –
爲什麼要將條件代碼外鍵放在醫療條件表上?它不應該在Donors_Medical_Condition表上嗎? – Jim
可能重複的[錯誤代碼:1215.無法添加外鍵約束(外鍵)](https://stackoverflow.com/questions/17691282/error-code-1215-cannot-add-foreign-key-constraint-foreign -keys) – Alexander