MySQL-错误代码1215,无法添加外键约束 [英] MySQL - Error Code 1215, cannot add foreign key constraint

查看:98
本文介绍了MySQL-错误代码1215,无法添加外键约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了两个成功的查询:

i got these two succesfull queries:

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

i get "Error Code: 1215, cannot add foreign key constraint"

我不知道我在做什么错.

i dont know what am i doing wrong.

推荐答案

在MySql中,外键引用需要引用索引(包括主键),其中索引的第一部分与外键字段匹配.如果您在condition_code上创建索引或更改主键st(首先是condition_code),则应该能够创建索引.

In MySql, a foreign key reference needs to reference to an index (including primary key), where the first part of the index matches the foreign key field. If you create an an index on condition_code or change the primary key st that condition_code is first you should be able to create the index.

这篇关于MySQL-错误代码1215,无法添加外键约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆