Mysql – Which of tables key to reference

database-designforeign keyMySQLprimary-key

MySQL.

There is a table users with autoincrement primary key id.

There is also a table employees (every employee is a user but not vice versa). The employees primary key is userid. The field userid is not autoincrement.

employees.userid is a foreign key for users.id.

Thus every employee's primary key value is also users primary key value.

Now consider a third table rows of which should refer to an employee.

The question: Should the third table refer to users.id or to employees.userid?

Best Answer

If the third table is related to employees and not to every user, you should reference employees.userid.

It makes sense for, say, a table of employee identity card numbers to reference "employees"."userid". But it makes sense for a table of email addresses to reference "users"."userid", because every user has an email address.