简体   繁体   English

数据库设计模式帮助2个实体

[英]Database Design schema help on 2 entities

Let us say I have two different entities on a site. 我们假设我在网站上有两个不同的实体。

Teachers and Students . 教师和学生

They both login to the website.Right now I have the following tables 他们都登录到网站。现在我有以下表格

**Users**

 - id
 - email
 - password

**Students**

 - GPA
 - SAT
 - ACT
 - user_id references Users ID

**Teachers**

 - user_id references Users ID
 - classroom_no
 - salary
 - average_class_size

So when a Student registers I add a row in both the Users and Teacher table. 因此,当学生注册时,我在“用户”和“教师”表中都添加了一行。

When a Teacher registers, I add a row in both the Users and Teacher table. 当教师注册时,我在“用户”和“教师”表中都添加了一行。

Is it better to just have ONE table Users with the following fields? 拥有以下字段的ONE表用户更好吗?

**Users:**

 - id
 - ACT
 - GPA
 - SAT
 - average_class_size
 - salary
 - classroom_no
 - role (0 = teacher 1 = student)

even though some rows would refer to teachers (and thus not use the ACT, SAT, GPA fields) 即使某些行会引用教师(因此不使用ACT,SAT,GPA字段)

?? ??

Thanks! 谢谢!

If you are not familiar with database normalization, please read this . 如果您不熟悉数据库规范化,请阅读此内容

Unless performance is a serious issue, I would not look at using a single table for this structure. 除非性能是一个严重的问题,否则我不会考虑使用单个表来构建此结构。 As your project goes one, you will more than likely add more fields to each role and possibly add more roles as well. 随着您的项目进行,您很可能会为每个角色添加更多字段,并且可能还会添加更多角色。 It will become increasingly more difficult to maintain this. 维持这一点将变得越来越困难。

I would actually go a step further and introduce another concept called role into your application, along with the appropriate data model to persist it. 我实际上会更进一步,在你的应用程序中引入另一个名为role概念,以及相应的数据模型来保持它。

Your tables should be: 你的桌子应该是:

users:
 - id
 - email
 - password

roles:
 - id
 - type

user_roles:
 - user_id
 - role_id

role_teacher:
 - user_id
 - classroom_no
 - salary
 - average_class_size

role_student:
 - user_id
 - GPA
 - SAT
 - ACT

Having a separate role table will let you separate your authentication table (users) from the rest of the data. 拥有单独的角色表将允许您将身份验证表(用户)与其余数据分开。 This might become important if a user can be both a teacher and a student. 如果用户既可以是教师又可以是学生,这可能会变得很重要。

This should give you a basic understanding of how to structure your database. 这应该让您基本了解如何构建数据库。

You may find that you will begin to model some of the other concepts like classes in your system. 您可能会发现您将开始为系统中的类等其他概念建模。 So adding the appropriate tables may look something like: 因此,添加适当的表可能类似于:

role_teacher:
 - user_id
 - salary

classroom:
 - id
 - capacity

class:
 - id
 - name
 - teacher_id
 - classroom_id
 - start_date
 - end_date

class_enrollment:
 - class_id
 - student_id
 - enrollment_date
 - grade

Items like the teachers class room now moves to the class, allowing for the teacher to teach multiple classes (maybe you need to re-add the concept of "home room"). 像教师课堂这样的项目现在移动到课堂上,允许教师教授多个课程(也许你需要重新添加“家庭教室”的概念)。 The teachers average class size then just becomes a calculation (though you may want to still store it for the sake of efficiency). 然后教师的平均班级规模就变成了计算(尽管为了提高效率,你可能还想存储它)。

Hope this helps guide you in the right direction! 希望这有助于指导您朝着正确的方向前进!

You should to go with Teachers and Students tables. 您应该选择TeachersStudents表。 Tables in database should reflect real world model (whenever it's possible of course). 数据库中的表应该反映真实世界模型(当然可能的话)。 Merging everything into single User table brings very little value and introduces following issues: 将所有内容合并到单个User表中带来的价值非常小,并引入了以下问题:

  • redundancy (teacher doesn't need ACT, GPA and so forth fields as you mentioned) 冗余(教师不需要你提到的ACT,GPA等字段)
  • complexity (you'll need extra logic to determine whether user is student or teacher) 复杂性(你需要额外的逻辑来确定用户是学生还是老师)
  • ambiguity (is user a system user, or real world person data, or just login information...?), term is far too generic 歧义(用户是系统用户,还是现实世界的个人数据,或者只是登录信息......?),术语过于笼统

On top of that, I'd rename Users table to UserLogins - it indicates its purpose more clearly. 最重要的是,我将Users表重命名为UserLogins - 它更清楚地表明其目的。

My feeling is that it's better the way you have it - you have to maintain both tables so that they're in sync, but you've better encapsulated the role of "User", "Student", and "Teacher" this way. 我的感觉是你拥有它的方式更好 - 你必须维护两个表以便它们保持同步,但你最好用这种方式封装“User”,“Student”和“Teacher”的角色。 It also leaves you a better maintenance footprint - what if you want to allow a "Parent" to log in with a reference to their child? 它还为您提供了更好的维护工作 - 如果您希望允许“父级”使用对其子级的引用登录,该怎么办? What about a "Principal"? 那么“校长”怎么样? These are all users, but would quickly gum up the unified table. 这些都是用户,但很快就会破坏统一的表格。

There are a couple of ways to do this, but I think one table for users is the right way to go. 有几种方法可以做到这一点,但我认为一个用户表是正确的方法。 You could have the empty columns - it doesn't look the greatest, but if it works for you, it would allow minimal coding to get the values you need. 您可以拥有空列 - 它看起来不是最好的,但如果它适用于您,它将允许最少的编码来获得您需要的值。

Another option might be to have an attributes table { UserId, Attribute, Value }, then for example, you could have: User1 GMAT 710, User1 ACT 30, etc. So you could then get a list of all attributes for a user by select * from attributes where UserId = user.UserId or some such. 另一种选择可能是拥有一个属性表{UserId,Attribute,Value},然后你可以拥有:User1 GMAT 710,User1 ACT 30等等。这样你就可以通过选择获得用户的所有属性列表*来自UserId = user.UserId或其他类似的属性。 More code overhead, much cleaner. 更多的代码开销,更清洁。 This would also allow you to add more attributes in the future for whatever purpose, and keep your database the same. 这也允许您在将来为了任何目的添加更多属性,并保持数据库相同。

If one teacher can take multiple classes, then your design fails, as the teacher table will have a multiple entries.. 如果一位教师可以参加多个班级,那么您的设计将失败,因为教师表将有多个条目。

Instead Create one Teacher Table where we can store Teacher Table, create one intermedite table, where we can store the Teacher and His classes. 而是创建一个教师表,我们可以存储教师表,创建一个中间表,我们可以存储教师和他的课程。

Similarly, Teacher and User table should be separate.. Let a separate User Table exists where we can store the Use details.. 同样,教师和用户表应该是分开的。让我们存储一个单独的用户表,我们可以存储使用细节。

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

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