1

I tear my hair with the ON DELETE CASCADE Clause. In fact I have two tables I'm on linux and I have installed lamp with phpmyadmin.

That's the tables :

CREATE TABLE a(
    Idatable bigint(20) PRIMARY KEY NOT NULL AUTO_INCREMENT, 
    Nameatable varchar(50) NOT NULL
)ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE anObject(
    IdAnObject bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY,
    Nameobject varchar(20) NOT NULL
)ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE linkTable(
    Idatable bigint(20) NOT NULL REFERENCES a (Idatable),
    IdAnObject bigint(20) NOT NULL REFERENCES anObject (IdAnObject) ON DELETE CASCADE,
    PRIMARY KEY (Idatable, IdAnObject),
    CONSTRAINT linkTable_fk1
        FOREIGN KEY (Idatable) 
        REFERENCES a (Idatable),
    CONSTRAINT linkTable_fk2
        FOREIGN KEY (IdAnObject) 
        REFERENCES anObject (IdAnObject) ON DELETE CASCADE
)ENGINE=InnoDB DEFAULT CHARSET=latin1;

Unfortunately, it doesn't work because when I delete a row in the table 'linktable' the associated row in anObject isn't delete

Thank you

1
  • It's working perfectly as @ypercube points out. See here. – Vérace Apr 27 '16 at 15:10
3

The CASCADE clause works in the opposite direction. From the parent to the child table.

When you delete a row in the parent table, the related rows in the child table are deleted as well.

3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.