简体   繁体   English

无法在mysql表上创建外键

[英]Can't create foreign key on mysql table

When I try this 当我试试这个

ALTER TABLE Comp_Entry
ADD FOREIGN KEY(CompetitionID) REFERENCES Comp__Competition(CompetitionID)

I get this error 我收到这个错误

#1005 - Can't create table 'chris_db.#sql-1464b_66' (errno: 150) (<a href="server_engines.php?engine=InnoDB&amp;page=Status&amp;token=7a7f690fbd1413e74979d79fa9044fb0">Details...</a>)

My Comp_Competition table has CompetitionID set as the primary key. 我的Comp_Competition表将CompetitionID设置为主键。 Is anyone familiar with this problem? 有人熟悉这个问题吗?

Your Comp_Entry table needs an existing index for CompetitionID. 您的Comp_Entry表需要CompetitionID的现有索引。 Also, your command has a double __ characters in the name for table Comp__Competition, but in your explanation you have only one. 此外,您的命令在表Comp__Competition的名称中有一个双__字符,但在您的解释中,您只有一个。 Which one is right? 哪一个是对的?

Because there is data in your table which does not feet to your constraint (for example nonexistent keys) 因为您的表中有数据不符合您的约束(例如不存在的键)

try to make 试着去做

     select * from Comp_Entry where CompetitionID not in 
        (select distinct CompetitionID from Comp__Competition)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM