0

Let’s say I have 3 SQL tables

Users (id, name) Couples (id, title) UsersInCouples (id, user_id, couple_id)

When user is deleted I need to delete not only all rows with this user from UsersInCouples table, but also all other rows where couples are now left with only one user in it.

moonvader
  • 19,761
  • 18
  • 67
  • 116
  • ypu can use a trigger BEFORE DELTE or you try youtr luck with foreign KEy ONDELTECASCADE – nbk Aug 29 '20 at 10:18

1 Answers1

0

use on delete cascade

CREATE TABLE users (
    user_id INT PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(255) NOT NULL
    
);

    create table UsersInCouples (
   id, 
  user_id,
  couple_id,
  FOREIGN KEY (user_id)
        REFERENCES users (user_id)
        ON DELETE CASCADE
  )

delete from users
Zaynul Abadin Tuhin
  • 31,407
  • 5
  • 33
  • 63