数据库设计:三种类型的用户,分开的还是一个表?

时间:2021-06-10 17:03:40

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?

每个用户类型都有不同的用户界面和访问不同类型的数据。它们唯一的相似之处是它们使用的是一个web应用程序,但它们访问的是完全不同的东西。是将它们都放在一个用户表中(比如tbl_users)更好,还是创建tbl_admins、tbl_suppliers、tbl_employees更好?

5 个解决方案

#1


45  

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.

这也促进了用户权限级别的更改,例如,您可以通过更新user表中的记录,将普通员工变成管理员。

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.

或者,思考一件事:你可以使用一个用户表,只有非常有限的信息所有三种类型的用户,如果类型扩展属性,关系不太好,你可以存储其他表的外键回到主用户表。

#2


8  

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.

还有第三种选择:将所有用户都拥有的列放入tbl_users中,并为tbl_admins、tbl_provider和tbl_employees创建三个表,将tbl_users连接为1到0. 1。当共享列的数量很重要时,您应该将此选择视为一种替代方法。

#3


2  

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.

这取决于他们的数据结构有多相似。如果它们是相似的,那么也许你可以把它们都放在一个表中。但是,如果它们有很多不同的字段,你就会得到很多空值…最好把它们放在不同的表格中。

#4


0  

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.

最好将所有登录信息保存在一个地方。如果要对登录过程进行更改,拥有3个不同的表意味着必须在3个不同的位置更改代码。

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.

如果用户可以属于多个角色,请考虑创建一个UserRoles表。否则,向现有表添加一个额外的字段——比如RoleType——将有助于区分不同类型的用户。

#5


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.

对他们访问的方式的关注会在你开发的软件下面。您可以根据您所拥有的用户类型来获取/压缩UI[或任何他们在软件系统中访问的内容]。

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

我通常只是根据我的用户类型来隐藏和显示内容

Hope this helps..

希望这可以帮助. .

#1


45  

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.

这也促进了用户权限级别的更改,例如,您可以通过更新user表中的记录,将普通员工变成管理员。

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.

或者,思考一件事:你可以使用一个用户表,只有非常有限的信息所有三种类型的用户,如果类型扩展属性,关系不太好,你可以存储其他表的外键回到主用户表。

#2


8  

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.

还有第三种选择:将所有用户都拥有的列放入tbl_users中,并为tbl_admins、tbl_provider和tbl_employees创建三个表,将tbl_users连接为1到0. 1。当共享列的数量很重要时,您应该将此选择视为一种替代方法。

#3


2  

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.

这取决于他们的数据结构有多相似。如果它们是相似的,那么也许你可以把它们都放在一个表中。但是,如果它们有很多不同的字段,你就会得到很多空值…最好把它们放在不同的表格中。

#4


0  

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.

最好将所有登录信息保存在一个地方。如果要对登录过程进行更改,拥有3个不同的表意味着必须在3个不同的位置更改代码。

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.

如果用户可以属于多个角色,请考虑创建一个UserRoles表。否则,向现有表添加一个额外的字段——比如RoleType——将有助于区分不同类型的用户。

#5


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.

对他们访问的方式的关注会在你开发的软件下面。您可以根据您所拥有的用户类型来获取/压缩UI[或任何他们在软件系统中访问的内容]。

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

我通常只是根据我的用户类型来隐藏和显示内容

Hope this helps..

希望这可以帮助. .