MySQL - Foreign Keys on Foreign Keys
Somehow, up until now, I had never come across a situation where I even needed to consider setting a foreign key on a foreign key like the situation shown below: I wanted to see if it was possible, and if so, what implications it might have.
To test this, I ran the commands below on an Ubuntu 16.04 MySQL 5.7 database.
Now I have set up all three tables and have values in each, proving that you can set up foreign key on a foreign key. What will happen if I delete the row from the secondary table? Will the account row be deleted, or will it remain because the id still exists in the employee table?
It turns out, that this change does cascade down to the tertiary table as I had hoped. If you don't want this behavior and want the account to remain when an employee is no longer considered "active", then have the foreign key reference the employee table directly.
First published: 16th August 2018