Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding new fields vs creating separate table

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?

like image 657
Brandon Jackson Avatar asked Jul 03 '10 18:07

Brandon Jackson


People also ask

What is the difference between tables and fields?

A table has records (rows) and fields (columns). Fields have different types of data, such as text, numbers, dates, and hyperlinks. A record: Contains specific data, like information about a particular employee or a product.

Why is it better to have multiple separate tables?

Storing all data in one single table will be confusing, may have security issues and there will be duplication in recording. Multiple table helps in recording the data in more organized manner when there are multiple users. The data can be stored as per the category and there will be less chances of duplication.

How do you add a new field to a table?

On the Home tab, in the Views group, click View, and then click Datasheet View. On the Fields tab, in the Add & Delete group, click More Fields. Select a field in the More Fields list to insert the new column. Access places the field to the right of the column where your cursor is currently located.

How can you create a new table with existing data from another table?

You can also use the SQL CREATE TABLE AS statement to create a table from an existing table by copying the existing table's columns. It is important to note that when creating a table in this way, the new table will be populated with the records from the existing table (based on the SELECT Statement).


1 Answers

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...

like image 111
egrunin Avatar answered Sep 26 '22 01:09

egrunin