错误代码:1005。无法创建表... (错误号:150)

发布于 2024-12-29 08:58:33 字数 756 浏览 0 评论 0原文

我在互联网上搜索了该问题的解决方案并检查了 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 技术交流群。

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

发布评论

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

评论(13

路弥 2025-01-05 08:58:33

错误代码:1005——代码中存在错误的主键引用

通常是由于引用的外键字段不存在。可能是您有拼写错误,或者检查大小写应该相同,或者字段类型不匹配。外键链接字段必须与定义完全匹配。

一些已知的原因可能是:

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

有关更多详细信息,请参阅: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:

  1. 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 as well and not BIGINT or SMALLINT or TINYINT. You should also check that one is not SIGNED and the other is UNSIGNED. They both need to be exactly the same.
  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.
  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.
  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.
  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.
  7. You have a default value (that is, default=0) on your foreign key column
  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.
  9. You have a syntax error in your ALTER statement or you have mistyped one of the field names in the relationship
  10. The name of your foreign key exceeds the maximum length of 64 characters.

For more details, refer to: MySQL Error Number 1005 Can’t create table

嗫嚅 2025-01-05 08:58:33

当将数据库从一台服务器导出到另一台服务器并且表默认按字母顺序列出时,也可能会发生这种情况。
因此,您的第一个表可能有另一个尚未创建的表的外键。在这种情况下,请禁用foreign_key_checks 并创建数据库。

只需将以下内容添加到您的脚本中:

SET FOREIGN_KEY_CHECKS=0;

它就会起作用。

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:

SET FOREIGN_KEY_CHECKS=0;

and it shall work.

新一帅帅 2025-01-05 08:58:33

有时是由于主表被删除(可能是通过禁用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.

短暂陪伴 2025-01-05 08:58:33

当外键和引用键的类型或长度不同时,通常会发生这种情况。

Very often it happens when the foreign key and the reference key don't have the same type or same length.

拔了角的鹿 2025-01-05 08:58:33

我有类似的错误。该问题与子表和父表没有相同的字符集和排序规则有关。这可以通过附加 ENGINE = InnoDB DEFAULT CHARACTER SET = utf8; 来修复

CREATE TABLE IF NOT EXISTS `country` (`id` INT(11) NOT NULL AUTO_INCREMENT,...) 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;

CREATE TABLE IF NOT EXISTS `country` (`id` INT(11) NOT NULL AUTO_INCREMENT,...) ENGINE = InnoDB DEFAULT CHARACTER SET = utf8;

... on the SQL statement means that there is some missing code.

毁梦 2025-01-05 08:58:33

外键必须与其引用的主键具有完全相同的类型。对于类型为“INT UNSIGNED NOT NULL”的示例,前键也必须为“INT UNSIGNED NOT NULL”

CREATE TABLE employees(
id_empl INT UNSIGNED NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id)
);
CREATE TABLE offices(
id_office INT UNSIGNED NOT NULL AUTO_INCREMENT,
id_empl INT UNSIGNED NOT NULL,
PRIMARY KEY(id),
CONSTRAINT `constraint1` FOREIGN KEY (`id_empl`) REFERENCES `employees` (`id_empl`) ON DELETE CASCADE ON UPDATE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='my offices';

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”

CREATE TABLE employees(
id_empl INT UNSIGNED NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id)
);
CREATE TABLE offices(
id_office INT UNSIGNED NOT NULL AUTO_INCREMENT,
id_empl INT UNSIGNED NOT NULL,
PRIMARY KEY(id),
CONSTRAINT `constraint1` FOREIGN KEY (`id_empl`) REFERENCES `employees` (`id_empl`) ON DELETE CASCADE ON UPDATE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='my offices';
謌踐踏愛綪 2025-01-05 08:58:33

错误代码:1005

我遇到了类似的问题,所以这里有一些我尝试过的事情(不按任何顺序,除了解决方案:))

  1. 更改了外键名称(它不起作用)
  2. 减少了外键长度
  3. 已验证数据类型(该死的没有错)
  4. 检查索引
  5. 检查排序规则(一切都很好,又该死)
  6. 截断表,没有什么用处
  7. 删除表并重新创建
  8. 尝试查看是否正在创建任何循环引用--- 一切都很好
  9. 最后,我看到我打开了两个编辑器。一个是在 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 :) )

  1. Changed the foreign key names (it didn't work)
  2. Reduced the foreign key length
  3. Verified the datatypes (darn nothing wrong)
  4. Check indexes
  5. Check the collations (everything fine, darn again)
  6. Truncated the table, of no good use
  7. Dropped the table and re-created
  8. Tried to see if any circular reference is being created --- all fine
  9. 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.

望她远 2025-01-05 08:58:33

我有同样的错误消息。最后我发现我在命令中拼错了表的名称:

ALTER TABLE `users` ADD FOREIGN KEY (country_id) REFERENCES country (id);

vs

ALTER TABLE `users` ADD FOREIGN KEY (country_id) REFERENCES countries (id);

我想知道为什么 MySQL 不能告诉这样的表不存在......

I had the very same error message. Finally I figured out I misspelled the name of the table in the command:

ALTER TABLE `users` ADD FOREIGN KEY (country_id) REFERENCES country (id);

versus

ALTER TABLE `users` ADD FOREIGN KEY (country_id) REFERENCES countries (id);

I wonder why on earth MySQL cannot tell such a table does not exist...

青春如此纠结 2025-01-05 08:58:33

刚才提到了MyISAM。只需尝试在语句末尾添加 ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCRMENT=2 ; 即可,假设您的其他表是使用 MyISAM 创建的。

CREATE TABLE IF NOT EXISTS `tablename` (
  `key` bigint(20) NOT NULL AUTO_INCREMENT,
  FOREIGN KEY `key` (`key`) REFERENCES `othertable`(`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;

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.

CREATE TABLE IF NOT EXISTS `tablename` (
  `key` bigint(20) NOT NULL AUTO_INCREMENT,
  FOREIGN KEY `key` (`key`) REFERENCES `othertable`(`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;
旧街凉风 2025-01-05 08:58:33

就我而言,当一个表是 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.

月光色 2025-01-05 08:58:33

在我的例子中发生了这种情况,因为约束声明中引用的表的名称不正确(我忘记了表名称中的大写):

ALTER TABLE `Window` ADD CONSTRAINT `Windows_ibfk_1` FOREIGN KEY (`WallId`) REFERENCES `Wall` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;

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

ALTER TABLE `Window` ADD CONSTRAINT `Windows_ibfk_1` FOREIGN KEY (`WallId`) REFERENCES `Wall` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;
在风中等你 2025-01-05 08:58:33

检查两个表是否具有相同的架构 InnoDB MyISAM。我在我的 InnoDB 案例中将它们设置为相同并工作

check both tables has same schema InnoDB MyISAM. I made them all the same in my case InnoDB and worked

演多会厌 2025-01-05 08:58:33

我的问题没有列出,这太愚蠢了......
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 composite PK 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.

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