jordanhgl 2009-10-18 22:50
浏览 227
已采纳

mysql问题

com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException: [color=red][size=small]Cannot add or update a child row: a foreign key constraint fails (hgl/blog, CONSTRAINT FK_Relationship_1 FOREIGN KEY (category_id) REFERENCES category (id))[/size][/color]

at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1012)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:957)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3378)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3310)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1853)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1976)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2503)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1737)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2022)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1940)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1925)
at org.apache.tomcat.dbcp.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:102)
at com.hgl.blog.BlogServlet.doPost(BlogServlet.java:53)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
at java.lang.Thread.run(Unknown Source)
以上是错误信息,以下是我的数据库文件:
/*==============================================================*/
/* DBMS name: MySQL 5.0 /
/
Created on: 2009-10-18 16:51:42 /
/
==============================================================*/

drop table if exists blog;

drop table if exists category;

drop table if exists comment;

drop table if exists users;

/*==============================================================*/
/* Table: blog /
/
==============================================================*/
create table blog
(
id int not null,
category_id int,
title varchar(400),
content varchar(4000),
created_time datetime,
primary key (id)
);

/*==============================================================*/
/* Table: category /
/
==============================================================*/
create table category
(
id int not null,
name varchar(200),
level int,
primary key (id)
);

/*==============================================================*/
/* Table: comment /
/
==============================================================*/
create table comment
(
id int not null,
blog_id int,
username varchar(200),
content varchar(1000),
primary key (id)
);

/*==============================================================*/
/* Table: users /
/
==============================================================*/
create table users
(
id int not null,
username varchar(200),
password varchar(200),
primary key (id)
);

alter table blog add constraint FK_Relationship_1 foreign key (category_id)
references category (id) on delete restrict on update restrict;

alter table comment add constraint FK_Relationship_2 foreign key (blog_id)
references blog (id) on delete restrict on update restrict;

这是哪里的错误啊?我没招儿了,望各位高手给指点一二啊,谢谢了!

  • 写回答

4条回答 默认 最新

  • 成尚謙 2009-10-18 23:40
    关注

    你试图INSERT或UPDATE一个在category表中不存在的id(category_id)到blog表中。

    建议你检查一下程序,看看你要INSERT或UPDATE的id(category_id)是不是不存在或者还未创建。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)