MySQL。无法创建表 errno 150

发布于 2024-08-11 16:28:59 字数 1286 浏览 11 评论 0原文

我必须在 MySQL 中创建一个包含两个表的数据库,但脚本失败并显示 errno 150(外键问题)。我仔细检查了两个表上的外键字段是否相同,但没有发现任何错误。

这是脚本:

 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL';

 DROP SCHEMA IF EXISTS `testdb`;
 CREATE SCHEMA IF NOT EXISTS `testdb` DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ;
 USE `testdb`;

 DROP TABLE IF EXISTS `testdb`.`table1` ;

 CREATE  TABLE IF NOT EXISTS `testdb`.`table1` (
   `id` INT UNSIGNED NOT NULL ,
   `field1` VARCHAR(50) NULL ,
   PRIMARY KEY (`id`) )

 ENGINE = InnoDB;


 DROP TABLE IF EXISTS `testdb`.`table2` ;

 CREATE  TABLE IF NOT EXISTS `testdb`.`table2` (
   `id` INT NOT NULL AUTO_INCREMENT ,
   `field1` VARCHAR(50) NULL ,
   `date` DATE NULL ,
   `cnt` INT NULL ,
   PRIMARY KEY (`id`) ,
   INDEX `FK_table2_table1` (`field1` ASC) ,
   CONSTRAINT `FK_table2_table1`
   FOREIGN KEY (`field1`)
   REFERENCES `testdb`.`table1` (`field1` )
   ON DELETE NO ACTION
   ON UPDATE NO ACTION)

 ENGINE = InnoDB;

 SET SQL_MODE=@OLD_SQL_MODE;
 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;

我在 Windows 和 Ubuntu 中使用不同版本的 MySQL 进行了尝试,但没有成功。

有什么想法吗?

I have to create a database with two tables in MySQL, but the script fails with errno 150 (foreign key problem). I double-checked the foreign key fields to be the same on both tables, and I can't find any error.

Here is the script:

 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL';

 DROP SCHEMA IF EXISTS `testdb`;
 CREATE SCHEMA IF NOT EXISTS `testdb` DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ;
 USE `testdb`;

 DROP TABLE IF EXISTS `testdb`.`table1` ;

 CREATE  TABLE IF NOT EXISTS `testdb`.`table1` (
   `id` INT UNSIGNED NOT NULL ,
   `field1` VARCHAR(50) NULL ,
   PRIMARY KEY (`id`) )

 ENGINE = InnoDB;


 DROP TABLE IF EXISTS `testdb`.`table2` ;

 CREATE  TABLE IF NOT EXISTS `testdb`.`table2` (
   `id` INT NOT NULL AUTO_INCREMENT ,
   `field1` VARCHAR(50) NULL ,
   `date` DATE NULL ,
   `cnt` INT NULL ,
   PRIMARY KEY (`id`) ,
   INDEX `FK_table2_table1` (`field1` ASC) ,
   CONSTRAINT `FK_table2_table1`
   FOREIGN KEY (`field1`)
   REFERENCES `testdb`.`table1` (`field1` )
   ON DELETE NO ACTION
   ON UPDATE NO ACTION)

 ENGINE = InnoDB;

 SET SQL_MODE=@OLD_SQL_MODE;
 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;

I've tried it in Windows and Ubuntu with different versions of MySQL and didn't work.

Any ideas?

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

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

发布评论

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

评论(22

红衣飘飘貌似仙 2024-08-18 16:28:59

table1.field1 没有定义索引。

需要在 field1 上放置 FOREIGN KEY 约束。

有了这个:

 CREATE  TABLE IF NOT EXISTS `testdb`.`table1` (
   `id` INT UNSIGNED NOT NULL ,
   `field1` VARCHAR(50) NULL ,
   KEY ix_table1_field1 (field1),
   PRIMARY KEY (`id`) )
 ENGINE = InnoDB;

一切都应该按预期进行。

table1.field1 has no index defined on it.

It is required to place a FOREIGN KEY constraint on field1.

With this:

 CREATE  TABLE IF NOT EXISTS `testdb`.`table1` (
   `id` INT UNSIGNED NOT NULL ,
   `field1` VARCHAR(50) NULL ,
   KEY ix_table1_field1 (field1),
   PRIMARY KEY (`id`) )
 ENGINE = InnoDB;

Everything should then work as expected.

可是我不能没有你 2024-08-18 16:28:59

在使用 MySQL Workbench 和 MySQL 5.5.27 时,我遇到了类似的问题。就我而言,问题出在 INT 类型字段上。错误的是,在一张表中它是 INT UNSIGNED,而在引用表中它是 INT。

While working with MySQL Workbench and MySQL 5.5.27, I have encountered the similar problem. In my case issue was with INT type fields. Erroneously in one table it was INT UNSIGNED and in referencing table it was INT.

粉红×色少女 2024-08-18 16:28:59

根据 MySQL 的版本,您可能需要首先在 table1.field1 上创建索引。

Depending on the version of MySQL you may need to create an index on table1.field1 first.

泅渡 2024-08-18 16:28:59

这里的答案之一建议禁用外键完整性检查。这是一个坏主意。这里有两个可能的罪魁祸首:

  • 引用的主键和引用的外键索引之间的数据类型不匹配
  • 。您索引的任何外键都必须为 NOT NULL

One of the answers here suggests to disable the foreign key integrity check. This is a BAD idea. There are two likely culprits here:

  • Data type mismatch between referenced primary key and referencing foreign key
  • Indices. Any foreign keys which you index must be NOT NULL
枕花眠 2024-08-18 16:28:59

另一个提示:

即使您的数据类型看起来相同 - 在我的例子中,两列都有 VARCHAR(50) - 这还不够。

您还需要确保两列具有相同的 COLLATION

Another hint:

Even when your data types seem to be the same - in my case both columns had VARCHAR(50) - this is not enough.

You also need to make sure that both columns have the same COLLATION.

残月升风 2024-08-18 16:28:59

还有另一个原因,虽然与其他原因略有相似:我指的是一个表,结果证明它具有 MyISAM 引擎,而不是 InnoDB。

Yet another cause, although slightly similar to others: I was referring to a table that turned out to have the MyISAM engine, instead of InnoDB.

倾城月光淡如水﹏ 2024-08-18 16:28:59

如果您错误输入引用表的名称,MySQL 也会抛出此错误。我抓了一会儿头发,直到我意识到我错过了外键(column1)引用mistyped_table(column1)中的一个字母

MySQL will also throw this error if your mistyping the name of the referring table. I pulled my hair out for awhile until I realized I missed a letter in foreign key (column1) references mistyped_table(column1)

万劫不复 2024-08-18 16:28:59

一个选项(根据情况)是禁用 MySQL 完整性检查:

SET FOREIGN_KEY_CHECKS = 0;

An option (depending on the case) would be to disable the MySQL integrity check:

SET FOREIGN_KEY_CHECKS = 0;
风透绣罗衣 2024-08-18 16:28:59

如果不起作用,请尝试以下操作:

外键名称是已存在键的重复。检查外键名称在数据库中是否唯一。只需在密钥名称末尾添加一些随机字符即可进行测试。

If nothing works, try this:

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.

挥剑断情 2024-08-18 16:28:59

我在尝试使用外键引用非唯一字段时遇到此错误。 (显然是不允许的)

I got this error while trying to use a foreign key to reference a non-unique field. (which apparently is not allowed)

入怼 2024-08-18 16:28:59

就我而言,一个表在另一个尚不存在的表上使用外键约束。这是由于 makefile 很大而发生的,因此它并不像我预期的那么明显。

In my case, one table was using foreign key constraints on another table that didn't exist yet. This was happening due to a large makefile, so it wasn't as obvious as I would've expected.

去了角落 2024-08-18 16:28:59

如果有人仍然遇到问题,我尝试了上面的所有解决方案(除了 SET FOREIGN_KEY_CHECKS),但没有任何效果。问题是,当您引用第一个表时,某些数据库对表名区分大小写。我认为这很奇怪,因为我以前从未在 MySQL、Oracle 上看到过这种情况,现在在 MariaDB 上发生了这种情况。

例如:

如果CADASTRO_MAQUINAS不存在则创建表(
id VARCHAR(16),
主键(ID)
);

如果INFOS不存在则创建表(
Id_Maquina VARCHAR(16) NOT NULL,
CONSTRAINT FK_infos_cadastro_maquinas 外键 (Id_Maquina) 引用 CADASTRO_MAQUINAS(Id)
);

如果我尝试使用 cadastro_maquinas(小写)而不是 CADASTRO_MAQUINAS 创建第二个表,我将收到此错误。

In case somebody is still having problems with this, I tried all the solutions above (except for SET FOREIGN_KEY_CHECKS) and nothing worked. The problem was that when you reference the first table, some databases are case sensitive about the table names. I think this is weird since I never saw this before on MySQL, Oracle and now this happened for me on MariaDB.

For example:

Create table if not exists CADASTRO_MAQUINAS (
Id VARCHAR(16),
Primary Key (Id)
);

Create table if not exists INFOS (
Id_Maquina VARCHAR(16) NOT NULL,
CONSTRAINT FK_infos_cadastro_maquinas Foreign Key (Id_Maquina) references CADASTRO_MAQUINAS(Id)
);

If I try to create the second table using cadastro_maquinas (lower cases) instead of CADASTRO_MAQUINAS, I will receive this error.

拧巴小姐 2024-08-18 16:28:59

我正在使用 MySQL workBench。问题是您不能使用相同的外键名称,它们需要唯一。因此,如果多个表引用相同的外键,则每次都必须给出一个唯一的名称。

I was using MySQL workBench. THe issue is you cannot use the same foreign key name, they need to be unique. So if more than one table will reference the same foreign key, each time there must be a unique name given.

好多鱼好多余 2024-08-18 16:28:59

我的一张桌子上也出现了类似的错误。当检查的列排序规则不同时,一旦将两列更改为相同的排序规则类型,就会起作用。

阅读完此处的大部分建议解决方案后。我只是认为如果我列出所有可能引发此错误的可能性可能会有所帮助。

1、检查列的CASE
2、检查列的COLLATION
3、检查两个表中是否都为该列创建了键(Unique、Primary)

I had a similar error on one of my tables. When checked column Collation was different, which worked once changed both columns to the same Collation type.

After reading most of the suggested solution here. I just thought it might be helpful if I just list down all the possibilities which could throw this error.

1, Check CASE of the Column
2, Check COLLATION of Columns
3, Check if there is a key created in both tables for the column (Unique, Primary)

若水微香 2024-08-18 16:28:59

就我而言,我在其中一个表中获得了旧的表定义 MyISAM,显然我无法从另一个表中为其创建外键。也许这对某人有帮助。

因此,这可能是由于尝试检查的两个数据库/字段定义之间的不一致而发生的:

Field Type
Field Collation
Table Engine

In my case I got old table definition MyISAM in one of the tables and obviously I was unable to make foreign key to it from another table. Maybe this help someone.

So this may happen because of inconsistencies between two databases/fields definitions try to check:

Field Type
Field Collation
Table Engine
内心旳酸楚 2024-08-18 16:28:59

对我来说,问题在于在 CREATE TABLE 查询中使用 CONSTRAINT

For me, the problem was with using CONSTRAINT in the CREATE TABLE query.

暮年 2024-08-18 16:28:59

当尝试引用外键中的复合键时,您也可能会遇到相同的错误。

例如:

CREATE TABLE `article` (
  `id` int(10) unsigned NOT NULL,
  `type` enum('X','Y','Z') NOT NULL,
  PRIMARY KEY (`id`,`type`)
) ENGINE InnoDB;

CREATE TABLE `t1` (
  `user_id` int(10) unsigned NOT NULL,
  `type` enum('X','Y','Z') NOT NULL,
  `article_id` int(10) unsigned NOT NULL,
  CONSTRAINT `user_access_article_ibfk_2` FOREIGN KEY (`article_id`, `type`) REFERENCES `article` (`id`, `type`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB

在这种情况下,在 FK 定义中使用article_id 和 type 字段非常重要,其顺序与它们在文章表 PRIMARY KEY 定义中出现的顺序完全相同。

You also may encounter the same error when attempting to reference a composite key in your foreign key.

For example:

CREATE TABLE `article` (
  `id` int(10) unsigned NOT NULL,
  `type` enum('X','Y','Z') NOT NULL,
  PRIMARY KEY (`id`,`type`)
) ENGINE InnoDB;

CREATE TABLE `t1` (
  `user_id` int(10) unsigned NOT NULL,
  `type` enum('X','Y','Z') NOT NULL,
  `article_id` int(10) unsigned NOT NULL,
  CONSTRAINT `user_access_article_ibfk_2` FOREIGN KEY (`article_id`, `type`) REFERENCES `article` (`id`, `type`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB

In this case, it is important to use article_id and type field in the FK definition in the very same order as they appear in the article table PRIMARY KEY definition.

你列表最软的妹 2024-08-18 16:28:59

就我而言,可能是服务器错误删除了同名的表。
删除整个 shcema 并重新创建它解决了问题。

In my case was probably a server bug dropping a table with the same name.
Dropping the whole shcema and re-creating it solved the problem.

街道布景 2024-08-18 16:28:59

在非常奇怪的情况下,您的数据库可能会损坏。就我而言,表上没有任何外键,唯一的重命名表或更改引擎有帮助。

结果innoDB被破坏了,请参阅: https://dba.stackexchange.com/questions/86853/1025-error-on-rename-of-table-errno-150-table-was-deleted-while-尝试过吗?lq=1

In very strange cases your database might be broken. In my case I did not have any foreign keys on the table and the only renaming the table or changing engine helped.

Turned out innoDB was broken, see: https://dba.stackexchange.com/questions/86853/1025-error-on-rename-of-table-errno-150-table-was-deleted-while-tried-to-a?lq=1

携余温的黄昏 2024-08-18 16:28:59

如果您正在使用 mysql workbench 并在关系表中遇到此错误,则可能有一个快速修复方法:只需将其删除,然后让 mysql workbench 为您重新创建它。然后复制sql。解决了我的 errno 150 问题。

If you are working on mysql workbench and you get this error for a relationship table there may be a quick fix for you: just delete it and let mysql workbench recreate it for you. Then copy the sql. Fixed my errno 150 problem.

情痴 2024-08-18 16:28:59

当我遇到这个问题时,是因为我将第一个表中的 id 设置为无符号,而第二个表中的外键则不是。让它们都unsigned为我解决了这个问题。

When I had this problem it was because I had set the id in the first table to be unsigned whereas the foreign key in the second table was not. Making them both unsigned fixed it for me.

时光倒影 2024-08-18 16:28:59

始终首先创建主表/父表,然后创建详细信息/子表。

Always create master/parent tables first, and then create your detail/child tables.

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