查询删除级联在子表中不成功 [英] Query on delete cascade not success in child table

查看:73
本文介绍了查询删除级联在子表中不成功的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了两个具有这样条件的表.

I have created two table that have a condition like this.

父母

CREATE TABLE IF NOT EXISTS `tbl_requestfix` (
 `id_request` varchar(10) NOT NULL,
 `waktu_tutup_request` datetime DEFAULT NULL,
 `id_complaint` varchar(10) NOT NULL,
  PRIMARY KEY (`id_request`),
  KEY `FK_tbl_requestfix_tbl_detail_complaint` (`id_complaint`),
    CONSTRAINT `FK_tbl_requestfix_tbl_detail_complaint` 
    FOREIGN KEY 
      (`id_complaint`) REFERENCES `tbl_detail_complaint` (`id_complaint`)
    ON DELETE  CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

孩子

CREATE TABLE IF NOT EXISTS `tbl_detail_complaint` (
  `id_complaint` varchar(10) NOT NULL,
  `complaint_2` text,
  `timestamp_2` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (`id_complaint`)
  ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

当我插入一行时,没有问题. 当我删除父行上的行时,子行上的行仍然存在吗? 我迷路了还是错了?

When I am insert a row, there is no problem. When I delete a row on parent, the row on child it still exist ? Am I lost or wrong ?

DELETE FROM tbl_requestfix where id_request='001';

感谢您的帮助.非常感谢

Thanks for the help. It so appreciated

推荐答案

您正在将此父对象称为:

You are calling this parent:

CREATE TABLE IF NOT EXISTS `tbl_requestfix` (
 `id_request` varchar(10) NOT NULL,
 `waktu_tutup_request` datetime DEFAULT NULL,
 `id_complaint` varchar(10) NOT NULL,
  PRIMARY KEY (`id_request`),
  KEY `FK_tbl_requestfix_tbl_detail_complaint` (`id_complaint`),
    CONSTRAINT `FK_tbl_requestfix_tbl_detail_complaint` 
    FOREIGN KEY 
      (`id_complaint`) REFERENCES `tbl_detail_complaint` (`id_complaint`)
    ON DELETE  CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

您在给这个孩子打电话:

You are calling this child:

CREATE TABLE IF NOT EXISTS `tbl_detail_complaint` (
  `id_complaint` varchar(10) NOT NULL,
  `complaint_2` text,
  `timestamp_2` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (`id_complaint`)
  ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

事实是,您应该能够在创建子级之前创建父级.父母先于孩子.但是您不能先创建父级:

The fact is that you should be able to create Parent prior to creating Child. Parents come before the kids. But you can't create Parent first:

错误1215(HY000):无法添加外键约束

ERROR 1215 (HY000): Cannot add foreign key constraint

所以我认为您需要重新考虑这一点.

So I think you need to rethink this one.

这里是一个例子:

-- drop table parent;
create table parent
(   -- assume your have only one parent, ok bad example, it's early
    id int auto_increment primary key,
    fullName varchar(100) not null
)ENGINE=InnoDB;

-- drop table child;
create table child
(   id int auto_increment primary key,
    fullName varchar(100) not null,
    myParent int not null,
    CONSTRAINT `mommy_daddy` FOREIGN KEY (myParent) REFERENCES parent(id)
        ON DELETE CASCADE ON UPDATE CASCADE     
)ENGINE=InnoDB;

测试级联:

insert parent(fullName) values ('Robert Smith'),('Kim Billings'); -- id's 1 and 2

insert child(fullName,myParent) values ('Little Bobby',1),('Sarah Smith',1);
insert child(fullName,myParent) values ('Scout Billings',2),('Bart Billings',2);

select * from child;
+----+----------------+----------+
| id | fullName       | myParent |
+----+----------------+----------+
|  1 | Little Bobby   |        1 |
|  2 | Sarah Smith    |        1 |
|  3 | Scout Billings |        2 |
|  4 | Bart Billings  |        2 |
+----+----------------+----------+

delete from parent where id=1;  -- delete Robert Smith

select * from child;
+----+----------------+----------+
| id | fullName       | myParent |
+----+----------------+----------+
|  3 | Scout Billings |        2 |
|  4 | Bart Billings  |        2 |
+----+----------------+----------+

在那儿,父母的删除也与孩子们的暴发一样

There, the delete of the parent cascaded to clobber kids too

这篇关于查询删除级联在子表中不成功的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆