添加新的字段,创建单独的表 [英] Adding new fields vs creating separate table

查看:91
本文介绍了添加新的字段,创建单独的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开展一个有几种类型的用户(学生和教师)的项目。目前存储用户的信息,使用两个表。 用户表存储所有用户具有的共同信息。 教师表存储只有教师使用外键将其与用户表相关联的信息。



用户




  • id

  • 名称

  • 电子邮件

  • 其他34个字段



教师




  • id

  • user_id

  • 主题

  • 其他17个字段



在数据库的其余部分,没有引用 teachers.id 。所有需要与用户相关的表使用 users.id 。由于用户在教师表中只能有一个相应的条目,应该将教师表中的字段移动到用户表中,并将其留为空白,否则不是老师的用户?



例如



用户




  • id

  • 名称

  • 电子邮件

  • 主题

  • 51个其他字段



这是一个表的太多字段吗?这样会妨碍性能吗?

解决方案

我认为这个设计是好的,假设大多数时候你只需要用户数据,并且您知道何时需要显示教师特定字段。



此外,只有通过执行 JOIN 才能获得教师,这可能会派上用场。



明天你可能有另一种不是老师的用户,你会很高兴分离。



已编辑添加:是的,这是一个继承模式,但是由于他没有说他使用什么语言,我不想浑水... ... ...


I am working on a project where there are several types of users (students and teachers). Currently to store the user's information, two tables are used. The users table stores the information that all users have in common. The teachers table stores information that only teachers have with a foreign key relating it to the users table.

users table

  • id
  • name
  • email
  • 34 other fields

teachers table

  • id
  • user_id
  • subject
  • 17 other fields

In the rest of the database, there are no references to teachers.id. All other tables who need to relate to a user use users.id. Since a user will only have one corresponding entry in the teachers table, should I just move the fields from the teachers table into the users table and leave them blank for users who aren't teachers?

e.g.

users

  • id
  • name
  • email
  • subject
  • 51 other fields

Is this too many fields for one table? Will this impede performance?

解决方案

I think this design is fine, assuming that most of the time you only need the user data, and that you know when you need to show the teacher-specific fields.

In addition, you get only teachers just by doing a JOIN, which might come in handy.

Tomorrow you might have another kind of user who is not a teacher, and you'll be glad of the separation.

Edited to add: yes, this is an inheritance pattern, but since he didn't say what language he was using I didn't want to muddy the waters...

这篇关于添加新的字段,创建单独的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆