Ang*_*.47 37 mysql foreign-keys mysql-error-1005
我在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;
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?错误状态:Can't create table './xxxxxxxx/blogReply.frm' (errno: 121)
nos*_*nos 120
检查所有约束是否确实拼写正确,还检查是否有任何其他表使用约束名称FK_userId或FK_blogId
错误121是外键约束问题.首先要检查的是你的外键定义是否正确(所有表和字段名称都是正确的,等等).
您也可以在创建表之前尝试禁用外键检查,如下所示:
SET FOREIGN_KEY_CHECKS = 0;
Run Code Online (Sandbox Code Playgroud)
当你重新启用密钥检查(将其设置为1)时,这会产生向下抛出错误的缺点,但是,如果是这种情况,那么这意味着你在某处干扰了创建外键的某些无效记录.
但是,如果您手动移动数据库文件(例如物理重命名data/your_database_name
目录),也会发生此问题.InnoDB无法将类似物理变化与表空间相关联,因此它与内部结构混杂在一起.
如果这是您所做的,那么最有效的解决方案是将旧数据库移回原来的位置,转储或导出它,然后DROP DATABASE
在重新导入之前对其进行处理.