Deklarasikan tindakan referensial:ON DELETE CASCADE, misalnya:
CREATE TABLE user (
user_id int PRIMARY KEY
);
CREATE TABLE offer (
offer_id int PRIMARY KEY
);
CREATE TABLE user_offer (
user_id int,
offer_id int,
PRIMARY KEY (user_id, offer_id),
FOREIGN KEY (user_id) REFERENCES user (user_id) ON DELETE CASCADE,
FOREIGN KEY (user_id) REFERENCES offer (offer_id) ON DELETE CASCADE
);
Cukup menarik, tampaknya menentukan tindakan referensial dalam sintaks kunci asing "shorthand" tidak berfungsi (dikonfirmasi di bawah MySQL 5.5.30, 5.6.6 m9). Berikut ini diuraikan, tetapi ketika user
dihapus, user_offer
yang sesuai tidak terhapus:
CREATE TABLE user_offer (
user_id int REFERENCES user (user_id) ON DELETE CASCADE,
offer_id int REFERENCES offer (offer_id) ON DELETE CASCADE,
PRIMARY KEY (user_id, offer_id)
);