views:

48

answers:

1

I've been toying with MySQL today and reading their documentation, gotten some handy information of optimization and a lot of things I didn't know. Now I've been adding foreign keys to my web application because it add constrains and I see it quite helpful.

My doubt is, right now there is a Roles table and a Users table, I set a relation on user's RoleID that when the role ID is removed (from the Roles table, parent) it should the user is deleted.

Now I kinda have the answer but I prefer someone to confirm, the ID has to match? or does the foreign key takes action regardless if it matches or not?

+2  A: 

The foreign key constraints will only take place where the ID is matched. In other words, it won't remove users of different RoleIDs, only those users which have the RoleID of the deleted Role.

James Skidmore