Query on delete cascade not success in child table

前端 未结 1 1537
南旧
南旧 2020-12-04 04:01

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

Parent

CREATE TABLE IF NOT EXISTS `tbl_requestfix` (
 `id_request` varchar         


        
相关标签:
1条回答
  • 2020-12-04 04:57

    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

    0 讨论(0)
提交回复
热议问题