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,无法添加外键约束”

我不知道我做错了什么.

在MySql中,外键引用需要引用索引(包括主键),其中索引的第一部分与外键字段匹配.如果在condition_code上创建索引或更改主键st,则条件码首先应该能够创建索引.
https://stackoverflow.com/questions/22799001/mysql-error-code-1215-cannot-add-foreign-key-constraint

转载注明原文:MySQL – 错误代码1215,无法添加外键约束