错误代码:1005。无法创建表... (错误号:150)
我在互联网上搜索了该问题的解决方案并检查了 StackOverflow 问题,但没有一个解决方案适合我的情况。
我想创建从表 sira_no 到 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)
我尝试向引用的表添加索引:
CREATE INDEX METAL_KODU_INDEX ON metal_kod (METAL_KODU);
我检查了两个表(字符集和排序规则)上的 METAL_KODU,但我找不到此问题的解决方案。我该如何解决这个问题?
这是 metal_kod 表:
METAL_KODU varchar(4) NO PRI
DURUM bit(1) NO
METAL_ISMI varchar(30) NO
AYAR_YOGUNLUK smallint(6) YES 100
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.
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 ;
This script returns:
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);
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?
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
错误代码:1005——代码中存在错误的主键引用
通常是由于引用的外键字段不存在。可能是您有拼写错误,或者检查大小写应该相同,或者字段类型不匹配。外键链接字段必须与定义完全匹配。
一些已知的原因可能是:
INT(10)
,则关键字段也需要是INT
,而不是BIGINT
或SMALLINT或<代码>TINYINT。您还应该检查其中一个不是
SIGNED
,另一个是UNSIGNED
。它们都需要完全相同。MyISAM
表。为了使用外键,表必须都是InnoDB
。 (实际上,如果两个表都是MyISAM
,那么您不会收到错误消息 - 它只是不会创建键。)在查询浏览器中,您可以指定表类型。ON
DELETE
SET
NULL
,但相关关键字段设置为NOT
NULL
。您可以通过更改级联或将字段设置为允许 NULL 值来解决此问题。ALTER
语句中存在语法错误,或者您错误地输入了关系中的字段名称之一有关更多详细信息,请参阅:MySQL 错误号 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)
the key field needs to beINT
as well and notBIGINT
orSMALLINT
orTINYINT
. You should also check that one is notSIGNED
and the other isUNSIGNED
. They both need to be exactly the same.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.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.ALTER
statement or you have mistyped one of the field names in the relationshipFor more details, refer to: MySQL Error Number 1005 Can’t create table
当将数据库从一台服务器导出到另一台服务器并且表默认按字母顺序列出时,也可能会发生这种情况。
因此,您的第一个表可能有另一个尚未创建的表的外键。在这种情况下,请禁用foreign_key_checks 并创建数据库。
只需将以下内容添加到您的脚本中:
它就会起作用。
This could also happen when exporting your database from one server to another and the tables are listed in alphabetical order by default.
So, your first table could have a foreign key of another table that is yet to be created. In such cases, disable foreign_key_checks and create the database.
Just add the following to your script:
and it shall work.
有时是由于主表被删除(可能是通过禁用foreign_key_checks),但外键CONSTRAINT仍然存在于其他表中。在我的情况下,我删除了表并尝试重新创建它,但它给我抛出了同样的错误。
因此,请尝试从所有表中删除所有外键约束(如果有),然后更新或创建表。
Sometimes it is due to the master table is dropped (maybe by disabling foreign_key_checks), but the foreign key CONSTRAINT still exists in other tables. In my case I had dropped the table and tried to recreate it, but it was throwing the same error for me.
So try dropping all the foreign key CONSTRAINTs from all the tables if there are any and then update or create the table.
当外键和引用键的类型或长度不同时,通常会发生这种情况。
Very often it happens when the foreign key and the reference key don't have the same type or same length.
我有类似的错误。该问题与子表和父表没有相同的字符集和排序规则有关。这可以通过附加 ENGINE = InnoDB DEFAULT CHARACTER SET = utf8; 来修复
... SQL 语句上意味着缺少一些代码。
I had a similar error. The problem had to do with the child and parent table not having the same charset and collation. This can be fixed by appending ENGINE = InnoDB DEFAULT CHARACTER SET = utf8;
... on the SQL statement means that there is some missing code.
外键必须与其引用的主键具有完全相同的类型。对于类型为“INT UNSIGNED NOT NULL”的示例,前键也必须为“INT UNSIGNED NOT NULL”
The foreign key has to have the exact same type as the primary key that it references. For the example has the type “INT UNSIGNED NOT NULL” the foreing key also have to “INT UNSIGNED NOT NULL”
错误代码:1005
我遇到了类似的问题,所以这里有一些我尝试过的事情(不按任何顺序,除了解决方案:))
最后,我看到我打开了两个编辑器。一个是在 PhpStorm (JetBrains) 中,另一个是 MySQL 工作台。 PhpStorm / MySQL Workbench 似乎创建了某种编辑锁。
我关闭了 PhpStorm 只是为了检查是否存在锁定(也可能是相反的情况)。这解决了我的问题。
Error Code: 1005
I had a similar issue, so here are few things that I did try (not in any order, except for the solution :) )
Finally, I saw that I had two editors open. One that in PhpStorm (JetBrains) and the other MySQL workbench. It seems that the PhpStorm / MySQL Workbench creates some kind of edit lock.
I closed PhpStorm just to check if locking was the case (it could have been the other way around). This solved my problem.
我有同样的错误消息。最后我发现我在命令中拼错了表的名称:
vs
我想知道为什么 MySQL 不能告诉这样的表不存在......
I had the very same error message. Finally I figured out I misspelled the name of the table in the command:
versus
I wonder why on earth MySQL cannot tell such a table does not exist...
刚才提到了MyISAM。只需尝试在语句末尾添加 ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCRMENT=2 ; 即可,假设您的其他表是使用 MyISAM 创建的。
MyISAM has been just mentioned. Simply try adding ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ; at the end of a statement, assuming that your other tables were created with MyISAM.
就我而言,当一个表是 InnoB 而另一个表是 MyISAM 时就会发生这种情况。通过 MySQL Workbench 更改一张表的引擎可以解决我的问题。
In my case, it happened when one table is InnoB and other is MyISAM. Changing engine of one table, through MySQL Workbench, solves for me.
在我的例子中发生了这种情况,因为约束声明中引用的表的名称不正确(我忘记了表名称中的大写):
It happened in my case, because the name of the table being referenced in the constraint declaration wasn't correct (I forgot the upper case in the table name):
检查两个表是否具有相同的架构 InnoDB MyISAM。我在我的 InnoDB 案例中将它们设置为相同并工作
check both tables has same schema InnoDB MyISAM. I made them all the same in my case InnoDB and worked
我的问题没有列出,这太愚蠢了......
将
FK
作为 PK 的表是一个复合PK
,声明如下:主键 (CNPJ
,CEP)
)并且它起作用了。我希望 CEP 字段在另一个表中为
FK
,但我陷入了这个错误,故事的寓意只是反转了上面的主键代码 (CEP
, <代码>CNPJ给他们的朋友小费。
My problem was not listed, it was something so silly .....
The table that has the
FK
as PK was a compositePK
that was declared like this: primary key (CNPJ
,CEP
)I wanted the CEP field to be
FK
in another table and I was stuck in this error, the moral of the story just inverted the code above for Primary key (CEP
,CNPJ
) and it worked.Get tip their friends.