多种用户类型 - 数据库设计建议

tpt*_*cat 14 mysql database-design user-defined-type

我正在开发一个 Web 应用程序,该应用程序将支持每个用户具有相应角色的用户身份验证。此外,我的用户可以是不同的类型,并且具有与之关联的不同字段。每个用户拥有的某些字段是相同的,例如:

email, password, first_name, last_name, etc.
Run Code Online (Sandbox Code Playgroud)

但是不同用户类型的一些字段会有所不同。例如:

User Type: Instructor
Fields unique to this type of user
----------------------------------
hourly_rate, tax_status

==================================

User Type: Student   
Fields unique to this type of user
----------------------------------
instrument, monthly_charge, program

==================================

User Type: Employee
Fields unique to this type of user
----------------------------------
hourly_rate, location
Run Code Online (Sandbox Code Playgroud)

这是在这些类型的用户之间可以相似和独特的字段类型的简要示例。

我想到的可能设置是:

Table: `users`; contains all similar fields as well as a `user_type_id` column (a foreign key on `id` in `user_types`
Table: `user_types`; contains an `id` and a `type` (Student, Instructor, etc.)
Table: `students`; contains fields only related to students as well as a `user_id` column (a foreign key of `id` on `users`)
Table: `instructors`; contains fields only related to instructors as well as a `user_id` column (a foreign key of `id` on `users`)
etc. for all `user_types`
Run Code Online (Sandbox Code Playgroud)

或者:

Table: `users`; contains all possible columns for all users and allow columns that could be filled for one user type but not another to be NULL
Run Code Online (Sandbox Code Playgroud)

我的问题:其中一个是比另一个更好的方法还是都很糟糕,我应该完全考虑其他方法?

vij*_*ayp 11

我建议第一个是更好的方法,即

表:`用户`;包含所有类似的字段以及一个 `user_type_id` 列(`user_types` 中 `id` 上的外键
表:`user_types`;包含一个 `id` 和一个 `type`(学生、教师等)
表:`学生`;包含仅与学生相关的字段以及一个 `user_id` 列(`users` 上的 `id` 外键)
表:`导师`;包含仅与教师相关的字段以及一个 `user_id` 列(`users` 上的 `id` 外键)
等所有`user_types`

原因:-

学生、教师等表格中的列等用户相关数据将来可能会增加,因此使用第一种方法可以轻松管理。

并选择其他选项来创建一个列数过多的表,这对未来的数据库管理来说是不利的,而且随着列数的增加,将来会出现更多问题。