关于MySQL创建表时Error 1005: Can't create table (errno: 121)的解决办法

ERROR 1005: Can't create table (errno: 121)

errno 121 means a duplicate key error. Probably the table already exists in the InnoDB internal data dictionary, though the .frm file for the table has been deleted. This is the most common reason for getting errno 121 in table creation. Another possible reason is a name conflict in a foreign key constraint name. Constraint names must be unique in a database, like table names are. 


意思是:

1、表名重复

2、以该名字命名的表之前创建过后来删除了,但是对应的.frm文件还留在磁盘上

3、主键名字在全数据库范围内不是唯一的


以上是在网上找到的错误相关信息。


结合今天折腾半天找到的问题根源,总结如下:

首先,排查外键是否是全局唯一

使用如下命令,将$database 替换为要查询的数据库

select    
    concat(table_name, '.', column_name) as 'foreign key',     
    concat(referenced_table_name, '.', referenced_column_name) as 'references'  
from  
    information_schema.key_column_usage   
where  
    table_schema='$database' and  
    referenced_table_name is not null; 


如果没发现重复的外键名称;

则进行第二步查询,是否是约束重名

SELECT
    constraint_name,
    table_name
FROM
    information_schema.table_constraints
WHERE
    constraint_type = 'FOREIGN KEY'
AND table_schema = DATABASE()
ORDER BY
    constraint_name;

经过以上两步,基本可以发现是否是全局命名冲突引起的建表失败。


我的错误就是全局范围内出现约束重名。

好坑啊尴尬

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值