错误代码:1005.无法创建表“..."(错误号:150)
我在 Internet 上搜索了此问题的解决方案并检查了 StackOverflow 问题,但没有一个解决方案适用于我的情况.
I searched for a solution to this problem on the Internet and checked the Stack Overflow questions, but none of the solutions worked for my case.
我想创建一个从表 sira_no 到 metal_kod 的外键.
I want to create a foreign key from table sira_no to metal_kod.
ALTER TABLE sira_no
ADD CONSTRAINT METAL_KODU FOREIGN KEY(METAL_KODU)
REFERENCES metal_kod(METAL_KODU)
ON DELETE SET NULL
ON UPDATE SET NULL ;
此脚本返回:
Error Code: 1005. Can't create table 'ebs.#sql-f48_1a3' (errno: 150)
我尝试向引用的表添加索引:
I tried adding an index to the referenced table:
CREATE INDEX METAL_KODU_INDEX ON metal_kod (METAL_KODU);
我在两个表(字符集和排序规则)上都检查了 METAL_KODU,但找不到解决此问题的方法.我该如何解决这个问题?
I checked METAL_KODU on both tables (charset and collation), but I couldn't find a solution to this problem. How can I fix this problem?
这是 metal_kod 表:
Here is the metal_kod table:
METAL_KODU varchar(4) NO PRI
DURUM bit(1) NO
METAL_ISMI varchar(30) NO
AYAR_YOGUNLUK smallint(6) YES 100
Error Code: 1005 -- 您的代码中存在错误的主键引用
Error Code: 1005 -- there is a wrong primary key reference in your code
通常是由于引用的外键字段不存在.可能是您有拼写错误,或者检查大小写应该相同,或者字段类型不匹配.外键链接字段必须与定义完全匹配.
Usually it's due to a referenced foreign key field that does not exist. It might be you have a typo mistake, or check case it should be same, or there's a field-type mismatch. Foreign key-linked fields must match definitions exactly.
一些已知的原因可能是:
Some known causes may be:
- 这两个关键字段的类型和/或大小不完全匹配.例如,如果一个是
INT(10)
关键字段也需要是INT(10)
而不是INT(11)
或TINYINT
.您可能需要使用SHOW
CREATE
TABLE
来确认字段大小,因为查询浏览器有时会在视觉上仅显示INTEGER
INT(10)
和INT(11)
.您还应该检查一个不是SIGNED
,另一个是UNSIGNED
.它们都需要完全相同. - 您尝试引用的其中一个键字段没有索引和/或不是主键.如果关系中的某个字段不是主键,则必须为该字段创建索引.
- 外键名称与现有键重复.检查您的外键名称在您的数据库中是否唯一.只需在您的密钥名称末尾添加一些随机字符即可进行测试.
- 您的一个或两个表是
MyISAM
表.为了使用外键,表必须都是InnoDB
.(实际上,如果两个表都是MyISAM
,那么您不会收到错误消息 - 它只是不会创建键.)在查询浏览器中,您可以指定表类型. - 您已经指定了级联
ON
DELETE
SET
NULL
,但是相关的关键字段设置为NOT
NULL
.您可以通过更改级联或将字段设置为允许NULL
值来解决此问题. - 确保 Charset 和 Collate 选项在表级别以及键列的各个字段级别都相同.
- 您的外键列有一个默认值(即 default=0)
- 关系中的一个字段是组合(复合)键的一部分,没有自己的单独索引.即使该字段有一个索引作为组合键的一部分,您也必须仅为该键字段创建一个单独的索引,以便在约束中使用它.
- 您的
ALTER
语句中有语法错误,或者您在关系中输入了错误的字段名称 - 您的外键名称超过了 64 个字符的最大长度.
- The two key fields type and/or size doesn’t match exactly. For example, if one is
INT(10)
the key field needs to beINT(10)
as well and notINT(11)
orTINYINT
. You may want to confirm the field size usingSHOW
CREATE
TABLE
because Query Browser will sometimes visually show justINTEGER
for bothINT(10)
andINT(11)
. You should also check that one is notSIGNED
and the other isUNSIGNED
. They both need to be exactly the same. - 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.
- 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.
- One or both of your tables is a
MyISAM
table. In order to use foreign keys, the tables must both beInnoDB
. (Actually, if both tables areMyISAM
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 toNOT
NULL
. You can fix this by either changing your cascade or setting the field to allowNULL
values. - 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.
- You have a default value (that is, default=0) on your foreign key column
- 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.
- You have a syntax error in your
ALTER
statement or you have mistyped one of the field names in the relationship - The name of your foreign key exceeds the maximum length of 64 characters.
更多详情请参考:MySQL 错误号 1005 无法创建表
For more details, refer to: MySQL Error Number 1005 Can’t create table