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条)

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP