I have 3 tables, student, parent, and staff with PK studentid, parentid, staffid respectively. I want to create a member table, with PK memberid, username, password. One member can be a student or parent or staff. I want to join this member table and member related table (if member is student, student table and so on) with a foreign key.
Currently I’m using another column for member type and put it as
1 for student,
2 for parent and
3 for staff,
and doing program with php.
But I need another efficient way to create tables using relational structure.
I assume you are using MySQL, but other DBMS may be similar.
There is no way to do what you are asking with a single column or table. You have a few options to implement this using relational databases:
Option 1: Have your member table include three columns with relational constraints to the staff, parent and student tables. A user’s type is implied by which column is filled (leave the others
NULL)Option 2: Put the data you want to store on each member on the staff, parent and student tables (like the username and password)
Each has its own downsides, the first being you will have to check each column to find out what type of member the current individual is and join the correct table, or search three tables when you need log someone in. There is also not single pool of
memberid‘s for your members. (You could do something similar to what @Sherif suggested)