... I want to keep the table entry if at least one of the foreign keys still exist
I want to delete messages if both users don't exist any longer, only.
It can't be achieved just by defining a constraint.
Possible procedure:
- Define a
marked_for_delete bit not null default 0
column feature. - Whenever you want to delete a user, mark it for deletion, by an update call.
- Define either a before or an after type of delete trigger on the table.
- In the trigger body, check if the combo of
fromEmail
andtoEmail
are marked for deletion, then execute adelete
statement on the parent table and the child combo will be deleted as your required condition matched.