mysql创建多个表的外键指向同一表的同一字段时,报错???

PJ_Richard 2016-07-25 04:04:31

看下面三个表的创建(大量字段省略,对此问题无影响)。

前两个表创建成功,但是当创建最后一个表时:[Err] 1005 - Can't create table 'company.tab_freeinfo' (errno: 121)
将最后一个表的typeId 不设为外键时,成功创建表。
两个表的外键不能指向同一个表的主键吗?
(大神们,不好意思,所有家当就25分!!)

create table tab_infoType(
id int primary key,
name varchar(32) not null
)

create table tab_payInfo(
id int auto_increment primary key,
typeId int ,
constraint fk_typeid foreign key(typeId)
references tab_infoType (id)
)

create table tab_freeInfo(
id int auto_increment primary key,
typeId int ,
constraint fk_typeid foreign key(typeId)
references tab_infoType (id)
)
...全文
289 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
PJ_Richard 2016-07-25
  • 打赏
  • 举报
回复
引用 3 楼 ACMAIN_CHM 的回复:
引用
create table tab_payInfo( id int auto_increment primary key, typeId int , constraint fk_typeid foreign key(typeId) references tab_infoType (id) ) create table tab_freeInfo( id int auto_increment primary key, typeId int , constraint fk_typeid foreign key(typeId) references tab_infoType (id) )
同名了。
外键名在整个数据库中唯一。。。。果然,是这样。。。
ACMAIN_CHM 2016-07-25
  • 打赏
  • 举报
回复
引用
create table tab_payInfo( id int auto_increment primary key, typeId int , constraint fk_typeid foreign key(typeId) references tab_infoType (id) ) create table tab_freeInfo( id int auto_increment primary key, typeId int , constraint fk_typeid foreign key(typeId) references tab_infoType (id) )
同名了。
PJ_Richard 2016-07-25
  • 打赏
  • 举报
回复
引用 1 楼 ACMAIN_CHM 的回复:
贴出show create table tab_infoType 以供分析,确认存储引擎为innodb而不是myisam


应该一定是innodb吧,不然第二个表也不能创建撒!
ACMAIN_CHM 2016-07-25
  • 打赏
  • 举报
回复
贴出show create table tab_infoType 以供分析,确认存储引擎为innodb而不是myisam

56,687

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧