ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
1005 - Can't create table 'xxx' (errno 150) 当你试图在mysql中创建一个外键的时候,这个出错会经常发生。 可应用操作系统:Windows、Mac、Linux、iOS 可应用 Navicat 产品:Navicat for MySQL、Navicat for MariaDB、Navicat Premium 可应用 Navicat 版本编号:全部 有些可能的情况会导致在 MySQL 数据库创建外键和表失败。这些错误都关系到MySQL 本身。 * * * * * 例子情况: 如果两个字段(字段名和外键名)使用不相容的字段类型,MySQL 会返回错误。 如果你使用 "On Delete Set Null",但字段不允许 null,MySQL 会返回错误。 * * * * * 150错误的常见原因列出来了,并且我以可能性的大小作了排序 已知的原因: 1, 两个字段的类型或者大小不严格匹配。 例如,如果一个是INT(10), 那么外键也必须设置成INT(10), 而不是 INT(11) 也不能是 TINYINT. 你得使用 SHOW 命令来查看字段的大小,因为一些查询浏览器有时候把 int(10) 和int(11) 都显示为integer。 另外,你还必须确定两个字段是否一个为 SIGNED,而另一个是UNSIGNED, 这两字段必须严格地一致匹配。 2, 你试图引用的其中一个外键没有建立起索引,或者不是一个primary key , 如果其中一个不是primary key 的话,你必须为它创建一个索引。 3, 外键的名字是一个已经存在的一个键值了,这个时候,你应该检查你的数据库以确保外健名字是唯一的,或者你在键名后面加上几个随机的字符以测试是否是这个原因。 121错误:重名错误。 外键和表一样,在同一个库中是不允许与其他外键重名的。 遇到这个错误请给你定义的外键换唯一无重复的名字。 4, 其中一个或者两个表是MyISAM引擎的表,若想要使用外键约束,必须是InnoDB引擎,(实际上,如果两个表都是MyISAM 引擎的,这个错误根本不会发生,但也不会产生外键),你可以通过查询浏览器来设置表的引擎类型 5, 你可能设置了ON DELETE SET NULL, 但是相关的键的字段又设置成了NOTS NULL 值。你可能通过修改cascade 的属性值或者把字段属性设置成 allow null 来搞定这个bug. 6, 请确定你的Charset 和 Collate 选项在表级和字段级上的一致 7, 你可能设置为外键设置了一个默认值,如 default=0 8, 在这个关系里面,其中的一个字段是一个混合键值中的一个,它没有自己独立的索引,这时,你必须为它创建一个独立的索引。 9, ALTER 声明中有语法错误 。 10, 要连接的两个表的编码格式不同 * * * * * 可能有各种情况下会导致同样的错误。欲了解更多信息,请随时访问 http://dev.mysql.com/doc/refman/5.1/en/cannot-create.html * * * * * Can’t create table ‘XX.frm’ (errno: 150) MySQL Error Number 1005 * * * * * [Err] 1005 - Can't create table 'blog_db.#sql-136c_4' (errno: 150) If you get this error while trying to create a foreign key, it can be pretty frustrating. The error about not being able to create a .frm file seems like it would be some kind of OS file permission error or something but this is not the case. This error has been reported as a bug on the MySQL developer list for ages, but it is actually just a misleading error message. In every case this is due to something about the relationship that MySQL doesn’t like. Unfortunately it doesn’t specify what the exact issue is. Here is a running list of causes that people have reported for the dreaded errno 150. I’ve tried to put them in order based on the frequency that I hear about a particular cause. You may want to start by running the MySQL command “SHOW INNODB STATUS” immediately after receiving the error. This command displays log info and error details. (Thanks Jonathan for the tip) Note: If your script runs fine on one server, but gives an error when you try to run it on a different server, then there is a good chance that #6 is the problem. Different versions of MySQL have different default charset setting. Known Causes: The two key fields type and/or size doesn’t match exactly. 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(我就是这里出错的,被引用表的字段为unsigned,而引用表中为signed). They both need to be exactly the same. (More about signed vs unsigned here). 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) 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) 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. 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) 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) You have a default value (ie default=0) on your foreign key column (Thanks to Omar for the tip) 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) 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) The name of your foreign key exceeds the max length of 64 chars. (Thanks to Nyleta for the tip) The MySQL documentation includes a page explaining requirements for foreign keys. Though they don’t specifically indicate it, these are all potential causes of errno 150. If you still haven’t solved your problem you may want to check there for deeper technical explainations.If you run into this error and find that it’s caused by something else, please leave a comment and I’ll add it to the list.