62

I have 3 types of users:

  • Admins
  • Suppliers
  • Employees

Each user type will have different user interface and access different types of data. Their only similarity is they are using one web application but they access totally different things. Is it better to put them all in one user table like tbl_users or is it better to create tbl_admins, tbl_suppliers, tbl_employees?

2

5 Answers 5

89

What you need to consider when designing tables is not necessarily what they'll have access to and how that is similar/dissimilar, but rather how the user levels themselves are similar/dissimilar.

For example, if the user types will have the same attributes (name, email, birthdate, etc), then they belong in one table together with a column indicating their privilege level.

This also facilitates changing privilege levels for a user, whereby you can make an ordinary Employee into an Admin, for example, by just updating the record in the user table.

If Suppliers are a different type of object with different attributes than the other two, Suppliers may belong in their own table.

Or, one more thing to consider: You might use a users table that holds only very limited information about users of all three types, and if the types have extended attributes that don't relate well to one another, you can store those in other tables with a foreign key back to the main users table.

6
  • 4
    Paragraph #2 above is the key: If the data you need for the three "things" is the same, then put them in the same table. If the data is different, then put them in different tables. The only case where this should be tricky is when the data is "mostly" the same, and you have to make judgement calls. Like, if there are 8 fields the same but 2 different, do you want to have 2 fields that are unused in some cases, or repeat 8 fields across two tables, or break the 2 fields out into a second "add-on" table?
    – Jay
    Dec 12, 2011 at 18:54
  • 9
    I think the last paragraph is the rule of thumb here. It make sense to separate the data this way to me. Have 1 user table with userTypes and then if there is data specific to a userType then compensate for the small change...it doesn't make database sense to have 3 different tables ...each table will have a fname and lname field at least even the suppliers. Apr 11, 2014 at 21:30
  • Perfect answer... as I was looking for..stackoverflow.com/questions/35316447/… Feb 10, 2016 at 16:09
  • Good answer as i'm myself looking on how to design my own tables. But there is another parameter to consider: how will your application evolve in the near future? Will you need to add more types of users? If yes, will your actual database design fit to your needs? That a point to consider.
    – Mooncake
    Aug 2, 2018 at 13:28
  • 1
    @DavidNorth That's probably down to preference and to whether or not you would end up joining the users table in anyway. That model assumes there will be a column like suppliers.user_id as a 1 to 1 FK to users.id. If you relate it all the way back to users, you almost certainly will need to join suppliers all the time. But many queries might only need suppliers attributes so it makes sense to define the FK there. Feb 11, 2020 at 22:01
23

There is also a third choice: put the columns that all users have in common into tbl_users, and create three tables for tbl_admins, tbl_suppliers and tbl_employees joining to tbl_users as 1 to 0..1. You should consider this choice as an alternative when the number of shared columns is significant.

2
  • Is it okay to have one to one relationship on tbl_user and another user type table? Dec 24, 2016 at 7:36
  • @AnggaAriWijaya - No, that would force the mapping to that user type. For example, if you had a one-to-one mapping between tbl_users and tbl_admins, then every user would now be an admin. You should probably just add the columns you wanted in this other table to tbl_users since every record was indirectly going to be forced to have those columns anyway. Oct 16, 2019 at 18:27
4

It depends on how similar their data structures are. If they are similar, then perhaps you could put them all in one table. But, if they have a lot of different fields and you'll end-up with lots of NULL values...and then it's better that they're all in separate tables.

1

Best to keep all your login info in one place. If you were ever to make a change to your login process, having 3 different tables would mean having to change the code in 3 separate places.

If a user can belong to more than one role, consider making a UserRoles table. Otherwise, adding an additional field to the existing table - such as RoleType - would help differentiate the different types of users.

1
  • 1
    unless you use OOP, where the parent class deals with the login, and the child just specifies which table to use, and any extra columns. Sep 30, 2015 at 1:55
0

You should just include them in one table and create a field/attribute that would be an indicator of whether the user is an Admin, Supplier or Employee.

It's simpler if you centralize it that way.

The concern on how/what they access would be under the software you develop. You can either fetch/constrict the UI[or whatever they access in the software system] basing from the type of user you have.

I usually just hide and show stuff according to the type of user I have

Hope this helps..

Not the answer you're looking for? Browse other questions tagged or ask your own question.