Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
377 views
in Technique[技术] by (71.8m points)

mysql - Query on delete cascade not success in child table

I have created two table that have a condition like 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;

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;

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:

ERROR 1215 (HY000): Cannot add foreign key constraint

So I think you need to rethink this one.

Here is an example:

Schema:

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

Test the cascade:

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...