mysql基础(六) 表删除时,报"Connot delete or update a parent row:a foreign key constraint fails"解决方案二
2015-12-09 17:59
811 查看
一、有两张表,结构如下:
其中表t_item的主键id是表t_bid的item_id字段的外键。那么在这种情况下,如果删除表t_item中的记录,并且该记录中的id主键被t_bid中的item_id字段所引用,就会抛出如下异常:
(1) 增加外键约束时声明级联删除,即:
触发器代码(MySQL):
这个问题在Hibernate中相对容易解决,只需设置cascade = “delete”即可。此时观察发出的sql语句:
column in mapping for entity"异常http://www.iteye.com/topic/786535
如果我们指定item_id字段值不能为null,那么在删除时会抛出如下异常:
t_item: t_bid: id int id int name varchar name varchar item_id int
其中表t_item的主键id是表t_bid的item_id字段的外键。那么在这种情况下,如果删除表t_item中的记录,并且该记录中的id主键被t_bid中的item_id字段所引用,就会抛出如下异常:
ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails
(`test/t_bid`, CONSTRAINT `fk_id` FOREIGN KEY (`id`) REFERENCES `t_item `(`id`))二、解决方法:级联删除,即在删除t_item表中的记录时同时删除t_bid表中的相关记录
(1) 增加外键约束时声明级联删除,即:
alter table t_bid add constraint fk_id foreign key(id) references key(id) on delete cascade;(2) 使用触发器:在删除t_item表中记录之前先删除与之相关的t_bid表中的记录。
触发器代码(MySQL):
delimiter // create trigger tri_delete before delete on t_item for each row begin delete from t_bid where id = old.id; end //Hibernate中的解决方案:
这个问题在Hibernate中相对容易解决,只需设置cascade = “delete”即可。此时观察发出的sql语句:
Hibernate: select item0_.id as id0_0_, item0_.name as name0_0_ from t_item item0_ where item0_.id=? Hibernate: select bids0_.item_id as item3_1_, bids0_.id as id1_, bids0_.id as id1_0_, bids0_.price as price1_0_, bids0_.item_id as item3_1_0_ from t_bid bids0_ where bids0_.item_id=? Hibernate: update t_bid set item_id=null where item_id=? Hibernate: delete from t_bid where id=? Hibernate: delete from t_bid where id=? Hibernate: delete from t_item where id=?发现在删除t_bid表中记录之前会先将它的item_id字段值设置为null,但如果我们在映射文件中设置item_id字段不能为null,即设置Bid.hbm.xml文件为:
<many-to-one name="item" column="item_id" class="po.Item" not-null="true"/>注意不能在Item.hbm.xml文件中进行如下设置(即在key元素中指定not-null="true"):
<set name="bids" cascade="all"> <key column="item_id" not-null="true"/> <one-to-many class="po.Bid"/> </set>这样会抛出"Repeated
column in mapping for entity"异常http://www.iteye.com/topic/786535
如果我们指定item_id字段值不能为null,那么在删除时会抛出如下异常:
org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch update··· Caused by: java.sql.BatchUpdateException: Data truncation: Column set to default value;
NULL supplied to NOT NULL column 'item_id' at row 1 •••此时的解决方法是设置inverse="true",这在Hibernate文档中有相应的描述:
Very Important Note: If the <key> column of a <one-to-many> association is declared NOT NULL, Hibernate may cause constraint violations when it creates or updates the association. To prevent this problem, you must use a bidirectional association with the many valued end (the set or bag) marked as inverse="true".观察此时发出的sql语句:
Hibernate: select item0_.id as id1_0_, item0_.name as name1_0_ from t_item item0_ where item0_.id=? Hibernate: select bids0_.item_id as item3_1_, bids0_.id as id1_, bids0_.id as id0_0_, bids0_.amount as amount0_0_, bids0_.item_id as item3_0_0_ from t_bid bids0_ where bids0_.item_id=? Hibernate: delete from t_bid where id=? Hibernate: delete from t_bid where id=? Hibernate: delete from t_item where id=?没有发出update语句。关于inverse="true"的理解:/article/3814593.html
相关文章推荐
- mysql基础——启动篇
- MYSQL使用手册
- MySQL第三方复制工具
- 基于zookeeper的MySQL主主负载均衡的简单实现
- MYSQL 分组合并函数
- MYSQL explain说明
- MySQL线程池(THREAD POOL)的处理
- Mysql笔记_陆续完善
- 使用mysqldump对MySQL的数据进行备份的操作教程
- MySQL性能优化的最佳经验
- mysql查询缓存打开、设置、参数查询、性能变量意思
- MySQL基于时间字段进行分区的方案总结
- 编写脚本令Xtrabackup对MySQL数据进行备份的教程
- mysql通过存储过程实现aes加解密
- vs2010 EF4.0 访问mysql
- mysql 内置的字符串处理函数
- mysql远程机器导入导出数据 锁表不锁表 部分数据或全部
- MySQL:按前缀批量删除表格
- Mysql 5.6 慢日志配制
- mysql通过binlog来恢复数据