Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL InnoDB表添加外键错误号:150_Mysql_Sql_Entity Relationship_Foreign Key Relationship_Indexing - Fatal编程技术网

MySQL InnoDB表添加外键错误号:150

MySQL InnoDB表添加外键错误号:150,mysql,sql,entity-relationship,foreign-key-relationship,indexing,Mysql,Sql,Entity Relationship,Foreign Key Relationship,Indexing,嗯,我有两张桌子 公共事件 公共可可 publica\u evento\u grupoid\u eventoin是名为publica\u evento的第三个表的外键,但也是表publica\u identificationcaco\u publicacoidentificationcaco列的外键。问题是,我必须通过键id\u evento创建将publica\u evento\u grupo与publica\u identificationcaco\u publicaco关联的外键,但当我尝

嗯,我有两张桌子

公共事件 公共可可
publica\u evento\u grupo
id\u evento
in是名为publica\u evento的第三个表的外键,但也是表
publica\u identificationcaco\u publicaco
identificationcaco列的外键。问题是,我必须通过键
id\u evento
创建将publica\u evento\u grupopublica\u identificationcaco\u publicaco关联的外键,但当我尝试使用列
identificationcaco
创建另一个FK时,它会给出下面的错误号

 [Err] 1005 - Can't create table '#sql-1049_1980992' (errno: 150).
正如你所看到的,publica_IdentificationCaco_publicacao表有两个PK,这就是为什么它必须是2 FK才能关联它们,我还没有创建索引,因为据我所知,我只需要在添加FK约束后创建索引,我能够在evento\u grupoidentificatacao\u publicacao之间创建列
id\u evento
的FK约束,我不知道为什么只有列
identificatacao
给出了这个错误

编辑1:@RolandBouman我没有权限使用该命令SHOW ENGINE INNODB STATUS


编辑2:@Geoff\u Montee实际上您传递的命令起作用了,要理解我为什么使用该结构,请看这个问题

通常会发生错误,因为引用表和引用表之间存在类型不匹配。在本例中,类型似乎匹配

您是否考虑过对主复合键中的两个字段都设置约束?您可能必须首先删除
id\u evento
上的现有外键约束

ALTER TABLE publica_evento_grupo ADD FOREIGN KEY 
    (id_evento, identificacao) REFERENCES 
    publica_identificacao_publicacao (id_evento, identificacao);
这种情况下的错误似乎是因为您试图仅向复合主键的一部分添加外键约束

可可豆
是否存在于任何其他表格中?为什么只向复合主键的一部分添加外键约束

这样想吧

假设您在
publica\u evento\u grupo
表中有一个外键约束,它本身就是
identificationcaco
字段。让我们也假设在
publica\u identificationcaco\u publicaco
表中,您有
(1,“一些字符串”)
(2,“一些字符串”)
。因此,如果删除
(2,“某些字符串”)
,但保留
(1,“某些字符串”)
。在
publica\u evento\u grupo
表中引用
(1,“某些字符串”)
的行将抱怨,即使它们不应该抱怨


因此,在我看来,您应该向所有主键添加外键约束,或者不添加任何外键约束。

通常会发生此错误,因为引用表和引用表之间存在类型不匹配。在本例中,类型似乎匹配

您是否考虑过对主复合键中的两个字段都设置约束?您可能必须首先删除
id\u evento
上的现有外键约束

ALTER TABLE publica_evento_grupo ADD FOREIGN KEY 
    (id_evento, identificacao) REFERENCES 
    publica_identificacao_publicacao (id_evento, identificacao);
这种情况下的错误似乎是因为您试图仅向复合主键的一部分添加外键约束

可可豆
是否存在于任何其他表格中?为什么只向复合主键的一部分添加外键约束

这样想吧

假设您在
publica\u evento\u grupo
表中有一个外键约束,它本身就是
identificationcaco
字段。让我们也假设在
publica\u identificationcaco\u publicaco
表中,您有
(1,“一些字符串”)
(2,“一些字符串”)
。因此,如果删除
(2,“某些字符串”)
,但保留
(1,“某些字符串”)
。在
publica\u evento\u grupo
表中引用
(1,“某些字符串”)
的行将抱怨,即使它们不应该抱怨


因此,在我看来,您应该向所有主键添加外键约束,或者不添加任何外键约束。

没有实际的DDL,说起来并不容易。我建议:

SHOW ENGINE INNODB STATUS;
在您遇到此错误后立即执行。在输出中,查找如下所示的部分:

------------------------
LATEST FOREIGN KEY ERROR
------------------------
121026 22:40:18 Error in foreign key constraint of table test/#sql-154c_94:
foreign key(rid) references bla(id):
Cannot find an index in the referenced table where the
referenced columns appear as the first columns, or column types
in the table and the referenced table do not match for constraint.
Note that the internal storage type of ENUM and SET changed in
tables created with >= InnoDB-4.1.12, and such columns in old tables
cannot be referenced by such columns in new tables.
See http://dev.mysql.com/doc/refman/5.5/en/innodb-foreign-key-constraints.html
for correct foreign key definition.

您可以在那里找到有关外键定义错误的详细信息。

如果没有实际的DDL,很难说清楚。我建议:

SHOW ENGINE INNODB STATUS;
在您遇到此错误后立即执行。在输出中,查找如下所示的部分:

------------------------
LATEST FOREIGN KEY ERROR
------------------------
121026 22:40:18 Error in foreign key constraint of table test/#sql-154c_94:
foreign key(rid) references bla(id):
Cannot find an index in the referenced table where the
referenced columns appear as the first columns, or column types
in the table and the referenced table do not match for constraint.
Note that the internal storage type of ENUM and SET changed in
tables created with >= InnoDB-4.1.12, and such columns in old tables
cannot be referenced by such columns in new tables.
See http://dev.mysql.com/doc/refman/5.5/en/innodb-foreign-key-constraints.html
for correct foreign key definition.

您将在那里找到有关外键定义错误的详细信息。

在遇到此错误后立即执行
显示引擎INNODB状态
,并在输出中查找标题
最后一个外键错误
。可能这只是数据类型不匹配。在遇到此错误后立即执行
显示引擎INNODB状态
,并在输出中查找标题
最后一个外键错误
。可能这只是数据类型不匹配。大家都知道这是一个很好的命令。我敢打赌,这是因为他只想在主键的一部分上使用外键约束。从逻辑上讲,这对我来说似乎没有意义。嘿,谢谢。事实上,我没有仔细阅读这个问题。奇怪的是,Innodb实际上允许您在父表上任何索引的前缀上创建外键。即使这不是唯一的索引或主键,即使前缀不是唯一的。古怪,没有任何实际的用例,但事实上是允许的。(天知道为什么)人人都知道的好命令。我敢打赌,这是因为他只想在主键的一部分上使用外键约束。从逻辑上讲,这对我来说似乎没有意义。嘿,谢谢。事实上,我没有仔细阅读这个问题。奇怪的是,Innodb实际上允许您创建forei