MySQL中的外键添加失败,错误代码为1005,数字为150 [英] Foreign Key add fails in MySQL with Error Code 1005, number 150

查看:260
本文介绍了MySQL中的外键添加失败,错误代码为1005,数字为150的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图添加一个新的外键到我的一个表中,像这样:

  ALTER TABLE UserTransactions `.`ExpenseBackTransactions` 
ADD CONSTRAINT`FK_EBTx_CustomAccountID`
FOREIGN KEY(`CustomAccountID`)
REFERENCES`UserTransactions`.`CustomAccounts`(`CustomAccountID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
ADD INDEX`FK_EBTx_CustomAccountID`(`CustomAccountID` ASC);

我收到以下错误:

 错误代码:1005 
无法创建表'./UserTransactions/#sql-187a_29.frm'(errno:150)

过去我对这个表和其他表做了很多改变,这是我第一次遇到这个问题。任何想法是什么造成的?



更新



< c> SHOW INNODB STATUS error:

  ------------ ------------ 
最新的外键错误
------------------------
110525 15:56:36表UserTransactions的外键约束错误/#sql-187a_2c:

FOREIGN KEY(`CustomAccountID`)
REFERENCES`UserTransactions`.`CustomAccounts`( `CustomAccountID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION
,ADD INDEX`FK_EBTx_CustomAccountID`(`CustomAccountID` ASC):
无法解析表名称接近:
(`CustomAccountID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION
,ADD INDEX`FK_EBTx_CustomAccountID`(`CustomAccountID` ASC)


解决方案

好的清单在这里
$ b


以下是人们为可怕的errno 150报告的已知原因的列表:


$ b


  1. 两个关键字段类型和/或大小并不完全匹配。例如,如果一个是INT(10),则关键字段也需要为INT(10),而不是INT(11)或TINYINT。您可能需要使用SHOW CREATE TABLE来确认字段大小,因为查询浏览器有时在视觉上只显示INT(10)和INT(11)的INTEGER。你也应该检查一个没有签名,另一个是UNSIGNED。他们都需要完全一样。 (更多关于signed和unsigned的信息)

  2. 您试图引用的关键字段之一没有索引和/或不是主键。如果关系中的某个字段不是主键,则必须为该字段创建一个索引。 (感谢Venkatesh和Erichero以及终端不相干这个提示)

  3. 外键名称是已经存在的键的副本。检查您的外键的名称在数据库中是唯一的。只需在您的密钥名称的末尾添加几个随机字符即可测试。 (感谢Niels的提示)

  4. 其中一个或两个表是MyISAM表。为了使用外键,表必须都是InnoDB。 (实际上,如果两个表都是MyISAM,那么你不会得到一个错误信息 - 它只是不会创建密钥)。在查询浏览器中,可以指定表类型。
  5. 您已经指定级联ON DELETE SET NULL,但是相关的键字段被设置为NOT NULL。您可以通过更改级联或将字段设置为允许NULL值来解决此问题。 (感谢Sammy和J Jammin)
  6. 确保字符集和分类选项在表级别以及关键字段的单个字段级别相同。 (感谢此技巧的FRR)

  7. 您的外键列上有一个默认值(即default = 0)(感谢Omar提示)
  8. 关系中的一个字段是组合(复合)键的一部分,并没有它自己的单独索引。即使该字段有一个索引作为组合键的一部分,但您必须为该键字段创建一个单独的索引才能在约束中使用该索引。 (感谢Alex的这个提示)

  9. 在你的ALTER语句中有一个语法错误,或者你在关系中输入了一个字段名称(感谢Christian& Mateo的提示)

  10. 您的外键名称超过了64个字符的最大长度。 (感谢Nyleta的提示)



So I'm attempting to add a new foreign key to one of my tables as such:

 ALTER TABLE `UserTransactions`.`ExpenseBackTransactions` 
   ADD CONSTRAINT `FK_EBTx_CustomAccountID`
   FOREIGN KEY (`CustomAccountID` )
   REFERENCES `UserTransactions`.`CustomAccounts` (`CustomAccountID`)
   ON DELETE NO ACTION
   ON UPDATE NO ACTION,
   ADD INDEX `FK_EBTx_CustomAccountID` (`CustomAccountID` ASC) ;

and I keep getting the following error:

Error Code: 1005
Can't create table './UserTransactions/#sql-187a_29.frm' (errno: 150)

I've done quite a bit of changes in the past to this and other tables, and this is the first time I've run into this issue. Any ideas what is causing it?

UPDATE

My SHOW INNODB STATUS error:

------------------------
LATEST FOREIGN KEY ERROR
------------------------
110525 15:56:36 Error in foreign key constraint of table UserTransactions/#sql-187a_2c:

  FOREIGN KEY (`CustomAccountID` )
  REFERENCES `UserTransactions`.`CustomAccounts` (`CustomAccountID` )
  ON DELETE NO ACTION
  ON UPDATE NO ACTION
, ADD INDEX `FK_EBTx_CustomAccountID` (`CustomAccountID` ASC):
Cannot resolve table name close to:
 (`CustomAccountID` )
  ON DELETE NO ACTION
  ON UPDATE NO ACTION
, ADD INDEX `FK_EBTx_CustomAccountID` (`CustomAccountID` ASC)

解决方案

There's a nice checklist here.

Below is a running list of known causes that people have reported for the dreaded errno 150:

  1. The two key fields type and/or size is not an exact match. For example, if one is INT(10) the key field needs to be INT(10) as well and not INT(11) or TINYINT. You may want to confirm the field size using SHOW CREATE TABLE because Query Browser will sometimes visually show just INTEGER for both INT(10) and INT(11). You should also check that one is not SIGNED and the other is UNSIGNED. They both need to be exactly the same. (More about signed vs unsigned here).
  2. One of the key field that you are trying to reference does not have an index and/or is not a primary key. If one of the fields in the relationship is not a primary key, you must create an index for that field. (thanks to Venkatesh and Erichero and Terminally Incoherent for this tip)
  3. The foreign key name is a duplicate of an already existing key. Check that the name of your foreign key is unique within your database. Just add a few random characters to the end of your key name to test for this. (Thanks to Niels for this tip)
  4. One or both of your tables is a MyISAM table. In order to use foreign keys, the tables must both be InnoDB. (Actually, if both tables are MyISAM then you won’t get an error message – it just won’t create the key.) In Query Browser, you can specify the table type.
  5. You have specified a cascade ON DELETE SET NULL, but the relevant key field is set to NOT NULL. You can fix this by either changing your cascade or setting the field to allow NULL values. (Thanks to Sammy and J Jammin)
  6. Make sure that the Charset and Collate options are the same both at the table level as well as individual field level for the key columns. (Thanks to FRR for this tip)
  7. You have a default value (ie default=0) on your foreign key column (Thanks to Omar for the tip)
  8. One of the fields in the relationship is part of a combination (composite) key and does not have it’s own individual index. Even though the field has an index as part of the composite key, you must create a separate index for only that key field in order to use it in a constraint. (Thanks to Alex for this tip)
  9. You have a syntax error in your ALTER statement or you have mistyped one of the field names in the relationship (Thanks to Christian & Mateo for the tip)
  10. The name of your foreign key exceeds the max length of 64 chars. (Thanks to Nyleta for the tip)

这篇关于MySQL中的外键添加失败,错误代码为1005,数字为150的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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