Foreign key with multiple references I have the following three tables in mysql database named "THE_COLLEGE"

mysql&gt; desc students;

+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| STU_Id   | char(5)     | NO   | PRI |         |       |
| STU_Name | varchar(20) | YES  |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+

2 rows in set (0.00 sec)
mysql&gt; desc staff;

+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| STF_Id   | char(5)     | NO   | PRI |         |       |
| STF_Name | varchar(20) | YES  |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+

2 rows in set (0.00 sec)
mysql&gt; desc users;

+------------+-------------+------+-----+---------+-------+
| Field      | Type        | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| Id         | char(5)     | NO   |     |         |       |
| user_Name  | varchar(20) | YES  |     | NULL    |       |
| Password   | varchar(20) | YES  |     | NULL    |       |
+------------+-------------+------+-----+---------+-------+

3 rows in set (0.02 sec)


If a new row want to be inserted into the table "users", that row must be found in students table or staff table.

Which means a staff or a student can be a user.

Is it possible?

I tried with following foreign keys.

ALTER TABLE users
ADD CONSTRAINT fky_stu
   FOREIGN KEY (ID)
   REFERENCES Students (STU_Id);

ALTER TABLE users
ADD CONSTRAINT fky_stf
   FOREIGN KEY (ID)
   REFERENCES Staff (STF_Id);


But the user table is allow to insert a row, if the row found in both tables (Student and Staff).

But I need to allow if the row found in any of the table (Student or Staff).
The comment from @ypercube is right.

You shall model an intermediary table to be able to enforce FKs:



A less formal model, in which FK enforcement would not be possible, could be: