简体   繁体   English

多角色数据库设计

[英]Multiple Role Database Design

I recently had this problem in designing a SQL database.我最近在设计 SQL 数据库时遇到了这个问题。

I want to create a database for a school, and of course not all users have the same role or privileges.我想为一个学校创建一个数据库,当然不是所有的用户都具有相同的角色或权限。

For example, there are teachers, headteacher, students and parents.例如,有老师、校长、学生和家长。

If I put all those in the same table and put a role column the table, then I can't be free to put any other columns for a specific role like I can't add a grade column for student because the other roles don't have grades.如果我将所有这些放在同一个表中并在表中放置一个角色列,那么我就不能随意为特定角色放置任何其他列,就像我无法为学生添加成绩列一样,因为其他角色没有没有成绩。

Also I can't put them in separate tables because in the log in I can't specify the role for this user and go to his table .此外,我无法将它们放在单独的表中,因为在登录时我无法为此用户指定角色并转到他的表。

What is the best way to do something like this?做这样的事情的最好方法是什么?

Use polymorphism on the user table.在用户表上使用多态。 Create a user table with basic authentication and common information like email and credentials.创建一个包含基本身份验证和常用信息(如电子邮件和凭据)的用户表。 Now create 2 columns authority_type, authority_id (naming can be changed).现在创建 2 列 authority_type,authority_id(命名可以更改)。

Now for every type of new role or privilege, create a table.现在,为每种类型的新角色或特权创建一个表。 For eg In your case, there will be a table for the teacher, headteacher, student and parent.例如,在您的情况下,将有一张桌子供老师、校长、学生和家长使用。 All have separate sets of attributes.都有单独的属性集。

Whenever saving a user record, you'll use its authority_type and its authoriy_id ( record foreign key of that other table ).每当保存用户记录时,您将使用其 authority_type 和其 authoriy_id (该其他表的记录外键)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM