38

我在 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;

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

4

4 回答 4

123

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

于 2009-07-24T23:46:18.873 回答
7

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

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

SET FOREIGN_KEY_CHECKS = 0;

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

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

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

于 2009-07-24T23:40:01.880 回答
1

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 .

于 2013-12-11T12:02:05.683 回答
1

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 :

long_constraint_name_1, long_constraint_name_2 may become long_constraint_name_

于 2016-01-04T19:24:48.437 回答