How to model a MySQL structure to deal with authentication? With users information separated

纵然是瞬间 提交于 2019-12-24 08:13:12

问题


I need help to model a table structure for my database.

I have two tables: teacher and student. And to deal with authentication, I have another table called: user. I want to maintain the information about teacher and student separated of the information about login (user table).

For example:

     teacher
-----------------
|  id |   name  |
-----------------
| 10  |  lorem  |
| 20  |  ipsum  |
| 30  |  dolor  |
-----------------


    student
-----------------
|  id |   name  |
-----------------
|  5  |  amet   |
|  6  |  sitt   |
|  7  |  dolum  |
-----------------

Information separated in two tables. The user table:

     user
-----------------------------------------------------------------
|  id |   teacher_id  |  student_id |    login   |   password   |
-----------------------------------------------------------------
|  1  |     10        |             |  lorem_lo  |   *******    |
|  2  |     20        |             |  ipsum_lo  |   *******    |
|  3  |     30        |             |  dolor_lo  |   *******    |
|  4  |               |     5       |  amet_lo   |   *******    |
|  5  |               |     6       |  sitt_lo   |   *******    |
|  6  |               |     7       |  dolum_lo  |   *******    |
-----------------------------------------------------------------

The solution I thought was that. I ask your opinion about. Is that a good solution?

Thanks in advance.

P.S.: I'm using Mysql (InnoDB)


回答1:


To eliminate empty fields from user table, you can do this:

--------------------------------------------------
|  id  |  user  |  level  |  login  |  password  |
--------------------------------------------------

where user contains all your students and teachers ID's and level that contains 1 for teachers and 2 for students.

In this case, you can use level column to create access in your app.

Another method, is with one table... separate students and teacher trough levels but isn't best practice.




回答2:


The normalized structure should be a bit different. I would create the login table in a different way: id, person_id, role, login and password. That way you can have only one id column without separating teacher and students. in the role column you indicate if a person is a student or a teacher, and use only a foreign key of person_id for each line




回答3:


Yes, this looks satisfactory.

All you will need to do after logon is check whether you have a teacher_id or student_id and handle it appropriately.



来源:https://stackoverflow.com/questions/9603258/how-to-model-a-mysql-structure-to-deal-with-authentication-with-users-informati

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!