MySQL 错误号 121

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1180660/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 13:43:59  来源:igfitidea点击:

MySQL errorno 121

mysqlforeign-keysmysql-error-1005

提问by Angel.King.47

I'm getting this error in MySQL create. I'm doing:

我在 MySQL 创建中收到此错误。我正在做:

CREATE TABLE `blogReply` (

    `Id`      INT(24)      NOT NULL AUTO_INCREMENT COMMENT 'Primary Key of This Table',
    `blogId`  INT(24)      NOT NULL COMMENT 'Blog where this reply was posted',
    `userId`  INT(24)      NULL COMMENT 'User the blog was posted by',
    `name`    VARCHAR(100) NULL DEFAULT 'Unknown' COMMENT 'The Name of the user that the reply was posted by',
    `email`   VARCHAR(100) NULL DEFAULT 'Unknown' COMMENT 'The Email of the user that the reply was posted by',
    `http`    VARCHAR(300) NULL DEFAULT 'Unknown' COMMENT 'The Webaddress of the user that the reply was posted by',
    `message` TEXT         NOT NULL COMMENT 'text of the blog',
    `votes`   INT(10)      DEFAULT 0 COMMENT 'Rating of the Blog',
    `ratedBy` TEXT         COMMENT 'People who have already Voted on this blog',
    `dateReg` BIGINT       NOT NULL COMMENT 'Date the User was Registered',

    PRIMARY KEY (`Id`),

    CONSTRAINT `FK_userId` FOREIGN KEY(`userId`)
        REFERENCES `user` (`Id`)
        ON DELETE SET NULL
        ON UPDATE CASCADE,

    CONSTRAINT `FK_blogId` FOREIGN KEY(`blogId`)
        REFERENCES `blog` (`Id`)
        ON DELETE CASCADE
        ON UPDATE CASCADE

) ENGINE = InnoDB;

Any Ideas? The Error States: Can't create table './xxxxxxxx/blogReply.frm' (errno: 121)

有任何想法吗?错误状态:Can't create table './xxxxxxxx/blogReply.frm' (errno: 121)

回答by nos

Check that all your constraints are really spelled out correctly, also check that there's not any other tables that uses the constraint names FK_userId or FK_blogId

检查所有约束是否真的拼写正确,还要检查是否有任何其他表使用约束名称 FK_userId 或 FK_blogId

回答by zombat

Error 121 is a foreign key constraint issue. The first thing to check is that your foreign key definitions are ok (all the tables and field names are correct, etc.).

错误 121 是外键约束问题。首先要检查的是您的外键定义是否正确(所有表和字段名称都正确等)。

You can try disabling foreign key checks before creating the table as well, like this:

您也可以在创建表之前尝试禁用外键检查,如下所示:

SET FOREIGN_KEY_CHECKS = 0;

That has the downside of throwing errors later when you re-enable your key checks (set it to 1), however, if this is the case, then it means you have some invalid records somewhere that are interfering with the creation of the foreign key.

当您重新启用密钥检查(将其设置为 1)时,这具有稍后抛出错误的缺点,但是,如果是这种情况,则意味着您在某处有一些无效记录干扰了外键的创建.

However, this issue can also occur if you've been manually moving database files around, such as physically renaming the data/your_database_namedirectory. InnoDB can't correlate physical changes like that to the tablespace, so it mucks with the internals.

但是,如果您一直手动移动数据库文件(例如物理重命名data/your_database_name目录),也会出现此问题。InnoDB 无法将像这样的物理更改与表空间相关联,因此它会破坏内部结构。

If this is what you did, the solution that works best is to move your old database back to where it was, take a dump or export of it, and do a DROP DATABASEon it before re-importing.

如果这是您所做的,最有效的解决方案是将旧数据库移回原来的位置,对其进行转储或导出,并DROP DATABASE在重新导入之前对其进行处理。

回答by user1570577

please check that your foreign key which you are creating is same in all aspects such as datatype with the referred table column . Each foreign key name should be unique for those tables in which it is created , it should not be used in anyother tables. for the above problem the Foreign key name "FK_userId" should not be used in any other table .

请检查您创建的外键是否在所有方面都相同,例如数据类型与引用的表列。每个外键名称对于创建它的那些表应该是唯一的,不应在任何其他表中使用。对于上述问题,不应在任何其他表中使用外键名称“FK_userId”。

回答by user55993

In I've this problem in mysql 5.5 but works fine in mysql 5.6. The problem was because the constraint name looks to be unique but if this is a long name and is truncated the become non unique, for example :

我在 mysql 5.5 中有这个问题,但在 mysql 5.6 中工作正常。问题是因为约束名称看起来是唯一的,但如果这是一个长名称并被截断,则变得不唯一,例如:

long_constraint_name_1, long_constraint_name_2may become long_constraint_name_

long_constraint_name_1long_constraint_name_2可能会变成long_constraint_name_