我得到了这两个成功的查询:
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,无法添加外键约束"
我不知道我做错了什么。
答案 0 :(得分:10)
在MySql中,外键引用需要引用索引(包括主键),其中索引的第一部分与外键字段匹配。如果你在condition_code上创建一个索引或者更改主键st,那么condition_code是第一个你应该能够创建索引。
答案 1 :(得分:4)
要定义foreign key
,引用的父字段必须在其上定义索引。
根据foreign key
约束的文档:
REFERENCES tbl_name(index_col_name,...)
在父表INDEX
中的condition_code
上定义Donors_Medical_Condition
,它应该正常工作。
create table Donors_Medical_Condition (
donor_id int not null,
condition_code int not null,
seriousness text,
KEY ( condition_code ), -- <---- this is newly added index key
primary key(donor_id, condition_code),
foreign key(donor_id) references Donors(donor_id) );
但似乎你错误地定义了你的表顺序和引用。
您应该在foreign key
表中定义Donors_Medical_Condition
,而不是在Donors_Medical_Conditions
表中定义-- create parent table first ( general practice )
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)
);
-- child table of Medical_Conditions
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),
foreign key(condition_code)
references Donors_Medical_Condition(condition_code)
);
。后者似乎是父。
相应地修改您的脚本。
他们应该写成:
{{1}}
请参阅:
[CONSTRAINT [symbol]] FOREIGN KEY
[index_name](index_col_name,...)
参考文献tbl_name(index_col_name,...)
[ON DELETE reference_option]
[ON UPDATE reference_option]reference_option:
限制| CASCADE | SET NULL |没有行动
答案 2 :(得分:1)
解决方法适用于需要快速操作方法的人:
仅供参考:我的问题不是由列的数据类型/大小,排序规则或InnoDB存储引擎的不一致引起的。
如何:
下载MySQL工作台并使用它的GUI添加外键。而已!
为什么:
错误与索引有关。我是从MySQL工作台自动生成的DML脚本中学到的。这也帮助我排除了所有这些不一致的可能性。它适用于外键定义主题的一个条件。这就是:“MySQL需要外键和引用键的索引,这样外键检查可以很快,不需要进行表扫描。”这是官方声明:http://dev.mysql.com/doc/refman/5.7/en/create-table-foreign-keys.html
我没有想到在外键列上添加索引(在子表中),只关注引用的TO列(在父表中)。
这是自动生成的脚本(PHONE.PERSON_ID最初没有索引):
.ovf
答案 3 :(得分:0)
我认为你的桌子有点落后了。我假设Donors_Medical_Condtion将捐赠者和医疗条件联系起来,所以你想要捐赠者的外键和那张桌子上的条件。
已更新
好的,你也是以错误的顺序创建表。这是整个脚本:
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 Medical_Conditions (
condition_code int not null,
condition_name varchar(50) not null,
condition_description text,
other_details text,
primary key(condition_code) );
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),
foreign key(condition_code) references Medical_Conditions(condition_code) );
答案 4 :(得分:0)
我遇到了同样的问题,根据给定的答案,我验证了所有数据类型和引用,但每次重新创建表时都会出现此错误。花了几个小时后,我开始知道下面的命令,它给了我错误 -
SHOW ENGINE INNODB状态;
LATEST FOREIGN KEY ERROR
------------------------
2015-05-16 00:55:24 12af3b000 Error in foreign key constraint of table letmecall/lmc_service_result_ext:
there is no index in referenced table which would contain
the columns as the first columns, or the data types in the
referenced table do not match the ones in table. Constraint:
,
CONSTRAINT "fk_SERVICE_RESULT_EXT_LMC_SERVICE_RESULT1" FOREIGN KEY ("FK_SERVICE_RESULT") REFERENCES "LMC_SERVICE_RESULT" ("SERVICE_RESULT") ON DELETE NO ACTION ON UPDATE NO ACTION
我使用mysql workbench删除了所有关系,但我仍然看到同样的错误。花了几分钟后,我执行下面的语句来查看DB中可用的所有约束 -
从information_schema.table_constraints中选择* constraint_schema =&#39; XXXXX&#39;
我想知道我已经使用mysql工作台删除了所有关系,但仍然存在约束。原因是因为这个约束已经在db中创建了。
因为它是我的测试数据库所以我删除了数据库,当我重新创建所有表和这个表时,它工作。所以解决方案是在创建新表之前必须从DB中删除此约束。
答案 5 :(得分:0)
检查两个字段的大小是否相同,如果引用的字段是无符号的,则引用字段也应该是无符号的。