Foreign key constraints: When to use ON UPDATE and ON DELETE

I’m designing my database schema using MySQL Workbench, which is pretty cool because you can do diagrams and it converts them 😛

Anyways, I’ve decided to use InnoDB because of it’s Foreign Key support. One thing I noticed though is that it allows you to set On Update and on Delete options for foreign keys. Can someone explain where “Restrict”, “Cascade” and set null could be used in a simple example?

For example, say I have a user table which includes a userID. And say I have a message table message which is a many-to-many which has 2 foreign keys (which reference the same primary key, userID in the user table). Is setting the On Update and On Delete options any useful in this case? If so, which one do I choose? If this isn’t a good example, could you please come up with a good example to illustrate how these could be useful?

Thanks

3 Answers
3

Leave a Comment