Delete command erases rows that shouldn't at first sight

I have the following 2 tables in MySQL:

CREATE TABLE event (
    id int NOT NULL,
    title varchar(255) NOT NULL,
    alias varchar(20) NOT NULL,
    PRIMARY KEY (id, alias),
    FOREIGN KEY (alias) REFERENCES user(alias) ON DELETE CASCADE
);
CREATE TABLE assistance (
    alias_second varchar(20) NOT NULL,
    id int NOT NULL,
    alias_main varchar(20) NOT NULL,
    PRIMARY KEY (alias_second, id, alias_main),
    FOREIGN KEY (alias_second) REFERENCES user(alias) ON DELETE CASCADE,
    FOREIGN KEY (id) REFERENCES event(id) ON DELETE CASCADE,
    FOREIGN KEY (alias_main) REFERENCES event(alias) ON DELETE CASCADE
);

And i have the following mock values in this last table:

| alias_second | id     | alias_main |
| ------------ | ------ | ---------- |
| Jack         | 1      | Alex       |
| John         | 1      | Alex       |
| Jack         | 2      | Alex       |

I don't understand why if I execute the following sentence: DELETE FROM event WHERE id = 1 AND alias = 'Alex'; the third row of the Assistance table ('Jack', 2, 'Alex') gets deleted too. So I would highly appreciate if someone could explain to me why does this happen and how to solve it.


I suspect you were probably looking for the composite foreign key, not the two separate foreign keys you have created.

Try changing -

FOREIGN KEY (id) REFERENCES event(id) ON DELETE CASCADE,
FOREIGN KEY (alias_main) REFERENCES event(alias) ON DELETE CASCADE
FOREIGN KEY (`id`,`alias_main`) REFERENCES event(`id`,`alias`) ON DELETE CASCADE