I’ve got two foreign keys in a table. Let’s assume that table is called News and has foreign keys updatedById and createdById, both of which point to userId in table Users.
Now I want to set to NULL foreign keys when user is deleted, but when I try to set ON DELETE SET NULL in that relationships I get:
Introducing FOREIGN KEY constraint ‘FK_News_Users’ on table ‘News’ may
cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or
ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
I don’t understand why both foreign keys can’t set to null?
Multiple Cascading Actions
Possibly in situations like this you might want to consider to implement functionality to delete user logically rather then physically (e.g. by introducing a flag field Active or Deleted in
Userstable). That way all relationships stay intact and can be analyzed retrospectively.But if you still need to implement
ON DELETE SET NULLfor both FK’s you can use aFOR DELETEtrigger onUsertable like this: