Deklarera en referensåtgärd:PÅ DELETE CASCADE, till exempel:
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
);
Intressant nog verkar det som att specificera referensåtgärder i "shorthand" syntax för främmande nyckel inte fungerar (bekräftat under MySQL 5.5.30, 5.6.6 m9). Följande tolkas, men när user
raderas, motsvarande user_offer
raderas inte:
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)
);