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;
这是哪里的错误啊?我没招儿了,望各位高手给指点一二啊,谢谢了!