In what order are ON DELETE CASCADE constraints processed?

后端 未结 4 935
失恋的感觉
失恋的感觉 2021-01-02 05:20

Here is an example of what I\'ve got going on:

CREATE TABLE Parent (id BIGINT NOT NULL,
  PRIMARY KEY (id)) ENGINE=InnoDB;

CREATE TABLE Child (id BIGINT NOT         


        
4条回答
  •  温柔的废话
    2021-01-02 06:07

    @Matt Solnit first of all this is really a good question and as far as I know when a record from Parent is to be deleted then innodb first tries to identify which other tables holds references to it so that it can delete the record from them as well. In your case it is Child table and Uncle table, now it seems that in this case it decides to first delete record from Child table and thus it repeats the same process for Child and eventually fails as Uncle holds reference to Child table but neither "ON DELETE CASCADE" nor "ON DELETE SET NULL" is specified for fk_child FK in Uncle table. However, it does seems that if innodb first tries to delete record from the Uncle table then deletion should have happened smoothly. Well after giving a second thought I think that since innodb follows ACID model thus it chooses Child over Uncle to start the deletion process becuase if it starts with Uncle even then the deletion in Child might still have failed e.g. assume a Friend table that has fk_child key (similar to Uncle) without ON DELETE CASCADE, now this would still have caused the whole transaction to fail and therefore this looks right behaviour to me. In others words innodb starts with table that may cause a possible failure in the transaction but that is my theory in reality it might be a completely different story. :)

提交回复
热议问题