MySQL 中的外键添加失败,错误代码 1005,编号 150

发布于 2024-11-09 11:22:28 字数 1205 浏览 5 评论 0原文

因此,我尝试向我的一个表中添加一个新的外键:

 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) ;

并且我不断收到以下错误:

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

我过去对此表和其他表进行了相当多的更改,这是第一个我曾经遇到过这个问题。有什么想法造成它吗?

更新

我的SHOW INNODB STATUS错误:

------------------------
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)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

强辩 2024-11-16 11:22:28

有一个 这里有很好的清单

下面是人们报告的可怕 errno 150 已知原因的运行列表:

  1. 两个关键字段的类型和/或大小不完全匹配。例如,如果是 INT(10),则关键字段也必须是 INT(10),而不是 INT(11) 或 TINYINT。您可能需要使用 SHOW CREATE TABLE 确认字段大小,因为查询浏览器有时会直观地显示 INT(10) 和 INT(11) 的 INTEGER。您还应该检查一个未签名,另一个未签名。它们都需要完全相同。 (有关签名与未签名的更多信息请参见此处)。
  2. 您尝试引用的关键字段之一没有索引和/或不是主键。如果关系中的某个字段不是主键,则必须为该字段创建索引。 (感谢 Venkatesh 和 Erichero 以及 Terminally Incoherent 提供的建议)
  3. 外键名称与已存在的键名称重复。检查外键名称在数据库中是否唯一。只需在密钥名称末尾添加一些随机字符即可进行测试。 (感谢 Niels 提供的建议)
  4. 您的一个或两个表是 MyISAM 表。为了使用外键,表必须都是 InnoDB。 (实际上,如果两个表都是 MyISAM,那么您不会收到错误消息 - 它只是不会创建键。)在查询浏览器中,您可以指定表类型。
  5. 您已指定级联 ON DELETE SET NULL,但相关关键字段设置为 NOT NULL。您可以通过更改级联或将字段设置为允许 NULL 值来解决此问题。 (感谢 Sammy 和 J Jammin)
  6. 确保表级别以及键列的各个字段级别的“字符集”和“整理”选项相同。 (感谢 FRR 提供此提示)
  7. 您的外键列有一个默认值(即 default=0)(感谢 Omar 的提示)
  8. 关系中的字段之一是组合(复合)键的一部分,并且没有自己的单独索引。即使该字段有一个索引作为复合键的一部分,您也必须仅为该键字段创建单独的索引才能在约束中使用它。 (感谢亚历克斯提供的提示)
  9. 您的 ALTER 语句中存在语法错误,或者您错误地输入了关系中的某个字段名称(感谢 Christian 和 Mateo 的提示)
  10. 您的外键名称超过了 64 个字符的最大长度。 (感谢 Nyleta 的提示)

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 its 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)
在你怀里撒娇 2024-11-16 11:22:28

根据我的经验,errno: 150 通常表示密钥表和相关表中 FOREIGN KEY 列的数据类型不相同。确保 CustomAccounts.CustomAccountIDExpenseBackTransactions.CustomAccountID 的类型完全相同,包括 UNSIGNED(如果适用)。

如果这没有帮助,请发布 SHOW CREATE TABLE ExpenseBackTransactions;SHOW CREATE TABLE CustomAccounts;

In my experience, the errno: 150 usually indicates that the data types of the FOREIGN KEY column in the key table and relating table are not identical. Make sure that CustomAccounts.CustomAccountID and ExpenseBackTransactions.CustomAccountIDare the exact same type, including UNSIGNED if it applies.

If that doesn't help, please post the SHOW CREATE TABLE ExpenseBackTransactions; and SHOW CREATE TABLE CustomAccounts;

Oo萌小芽oO 2024-11-16 11:22:28

陷阱 22. 外键需要索引。 MySQL 不会对该查询进行排序,因此在进行外键检查时索引就存在。因此,首先创建索引,然后在两个单独的查询中添加外键。

Catch 22. Foreign keys need indexes. MySQL doesn't order this query so that the index exists at the time it does it foreign key checks. Thus, first create the index, then add the foreign key in 2 separate queries.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文