查询删除级联在子表中不成功

时间:2015-08-30 15:16:11

标签: mysql

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

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;

当我插入一行时,没有问题。 当我在父项上删除一行时,它上面的行仍然存在? 我输了还是错了?

DELETE FROM tbl_requestfix where id_request='001';

感谢您的帮助。非常感谢

1 个答案:

答案 0 :(得分:0)

您正在呼叫此父母:

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;

事实是,您应该能够在创建Child之前创建Parent。父母们来到孩子们面前。但是你不能先创建Parent:

  

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

所以我认为你需要重新考虑这个。

以下是一个例子:

架构:

-- 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 |
+----+----------------+----------+

在那里,父母的删除也与clobber孩子级联了