Cannot add or update a child row: a foreign key constraint fails 60
mysql中建立数据库后,从外部导入mysql文件,建立数据表。在mysql文件中data_dictionary表建表结构如下:DROPTABLEIFEXISTS`dat...
mysql中建立数据库后,从外部导入mysql文件,建立数据表。
在mysql文件中data_dictionary表建表结构如下:
DROP TABLE IF EXISTS `data_dictionary`;
CREATE TABLE `data_dictionary` (
`data_id` int(11) NOT NULL auto_increment,
`data_name` varchar(40) default NULL,
`type` varchar(40) default NULL,
PRIMARY KEY (`data_id`)
) ENGINE=InnoDB AUTO_INCREMENT=157 DEFAULT CHARSET=utf8;
在mysql文件中paper_t表建表结构如下:
DROP TABLE IF EXISTS `paper_t`;
CREATE TABLE `paper_t` (
`paper_id` int(11) NOT NULL auto_increment,
`hard_grade` int(11) default NULL,
`score_all` int(11) default NULL,
`user_id` int(11) default NULL,
`data_create` date default NULL,
`paper_cont` text,
`data_id` int(11) NOT NULL,
`paper_name` varchar(100) default NULL,
PRIMARY KEY (`paper_id`),
KEY `Refdata_dictionary55` (`data_id`),
KEY `user_id` (`user_id`),
CONSTRAINT `paper_t_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`user_id`),
CONSTRAINT `Refdata_dictionary55` FOREIGN KEY (`data_id`) REFERENCES `data_dictionary` (`data_id`)
) ENGINE=InnoDB AUTO_INCREMENT=310 DEFAULT CHARSET=utf8;
在执行时弹出错误:
Cannot add or update a child row: a foreign key constraint fails (`onlineexamsystem`.`paper_t`, CONSTRAINT `Refdata_dictionary55` FOREIGN KEY
(`data_id`) REFERENCES `data_dictionary` (`data_id`))
后网上看了些资料,修改paper_t建表结构(外键)如下:
DROP TABLE IF EXISTS `paper_t`;
CREATE TABLE `paper_t` (
`paper_id` int(11) NOT NULL auto_increment,
`hard_grade` int(11) default NULL,
`score_all` int(11) default NULL,
`user_id` int(11) default NULL,
`data_create` date default NULL,
`paper_cont` text,
`data_id` int(11) NOT NULL,
`paper_name` varchar(100) default NULL,
PRIMARY KEY (`paper_id`),
KEY `Refdata_dictionary55` (`data_id`),
KEY `user_id` (`user_id`),
CONSTRAINT `paper_t_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`user_id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `Refdata_dictionary55` FOREIGN KEY (`data_id`) REFERENCES `data_dictionary` (`data_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=310 DEFAULT CHARSET=utf8;
再次执行,仍然弹出类似错误:
Cannot add or update a child row: a foreign key constraint fails (`onlineexamsystem`.`paper_t`, CONSTRAINT `Refdata_dictionary55` FOREIGN KEY
(`data_id`) REFERENCES `data_dictionary` (`data_id`) ON DELETE CASCADE ON UPDATE CASCADE)
求高手解决,谢了。急! 展开
在mysql文件中data_dictionary表建表结构如下:
DROP TABLE IF EXISTS `data_dictionary`;
CREATE TABLE `data_dictionary` (
`data_id` int(11) NOT NULL auto_increment,
`data_name` varchar(40) default NULL,
`type` varchar(40) default NULL,
PRIMARY KEY (`data_id`)
) ENGINE=InnoDB AUTO_INCREMENT=157 DEFAULT CHARSET=utf8;
在mysql文件中paper_t表建表结构如下:
DROP TABLE IF EXISTS `paper_t`;
CREATE TABLE `paper_t` (
`paper_id` int(11) NOT NULL auto_increment,
`hard_grade` int(11) default NULL,
`score_all` int(11) default NULL,
`user_id` int(11) default NULL,
`data_create` date default NULL,
`paper_cont` text,
`data_id` int(11) NOT NULL,
`paper_name` varchar(100) default NULL,
PRIMARY KEY (`paper_id`),
KEY `Refdata_dictionary55` (`data_id`),
KEY `user_id` (`user_id`),
CONSTRAINT `paper_t_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`user_id`),
CONSTRAINT `Refdata_dictionary55` FOREIGN KEY (`data_id`) REFERENCES `data_dictionary` (`data_id`)
) ENGINE=InnoDB AUTO_INCREMENT=310 DEFAULT CHARSET=utf8;
在执行时弹出错误:
Cannot add or update a child row: a foreign key constraint fails (`onlineexamsystem`.`paper_t`, CONSTRAINT `Refdata_dictionary55` FOREIGN KEY
(`data_id`) REFERENCES `data_dictionary` (`data_id`))
后网上看了些资料,修改paper_t建表结构(外键)如下:
DROP TABLE IF EXISTS `paper_t`;
CREATE TABLE `paper_t` (
`paper_id` int(11) NOT NULL auto_increment,
`hard_grade` int(11) default NULL,
`score_all` int(11) default NULL,
`user_id` int(11) default NULL,
`data_create` date default NULL,
`paper_cont` text,
`data_id` int(11) NOT NULL,
`paper_name` varchar(100) default NULL,
PRIMARY KEY (`paper_id`),
KEY `Refdata_dictionary55` (`data_id`),
KEY `user_id` (`user_id`),
CONSTRAINT `paper_t_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`user_id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `Refdata_dictionary55` FOREIGN KEY (`data_id`) REFERENCES `data_dictionary` (`data_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=310 DEFAULT CHARSET=utf8;
再次执行,仍然弹出类似错误:
Cannot add or update a child row: a foreign key constraint fails (`onlineexamsystem`.`paper_t`, CONSTRAINT `Refdata_dictionary55` FOREIGN KEY
(`data_id`) REFERENCES `data_dictionary` (`data_id`) ON DELETE CASCADE ON UPDATE CASCADE)
求高手解决,谢了。急! 展开
2个回答
展开全部
你这个感觉上应该是 想创建了一个 树形的表
FOREIGN KEY (`Cpno`) REFERENCES Course (`Cno`)
自己这个表的 cpno 是外键, 引用自己这个表
那么建立根节点数据的时候,是没有 父节点的。
可以尝试
insert into `mysql`.`course`(`Cno`,`Cname`,`Cpno`,`Coredit`) values ( '1','数据库', NULL, '4')
看看。
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询