Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to split up models into multiple database tables? [closed]

I'm working with Ruby on Rails, but this question I think is broader than that and applies to database design generally.

When is it a good idea to split a single model up into multiple tables? For example, assume I have a User model, and the number of fields in the model is really starting to add up. For example, the User can enter his website, his birthday, his time zone, his etc etc.

Is there any advantage or disadvantage to splitting up the model, such that maybe the User table only has basic info like login and email, and then there is another table that every User has that is something like UserInfo, and another that is UserPermissions, and another that is UserPrivacySettings or something like that?

Edit: To add additional gloss on this, most of the fields are rarely accessed, except on pages specific to them. For example, things like birthday are only ever accessed if someone clicks through to a User's profile. Furthermore, some of the fields (which are rarely accessed) have the potential to be extremely large. Most of the fields have the potential to be either set to blank or nil.

like image 469
William Jones Avatar asked Feb 16 '10 07:02

William Jones


People also ask

Why do we seek to split up information into different tables rather than confine it to a single table?

In many cases, it may be best to split information into multiple related tables, so that there is less redundant data and fewer places to update.

Why is it better to have multiple separate tables?

It saves space and its flexible. Realistically, you cant really put everything in one table.

Why should we not store all the data in the single database table?

Answer and Explanation: 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.


2 Answers

Generally it is a good idea to put things which have a one-to-one relationship in the same table. Unless your userbase includes the Queen or Paddington Bear, a user has just one birthday, so that should be an attribute of the USERS table. Things which have a one-to-many relationship should be in separate tables. So, if a user can have multiple privacy settings by all means split them out.

Splitting one table into several tables can make queries more complicated or slower, if we want to retrieve all the user's information at once. On the other hand if we have a set of attributes which is only ever queried or updated in a discrete fashion then having a separate table to hold that data is a sound idea.

like image 135
APC Avatar answered Sep 23 '22 01:09

APC


This would be a situation for analysis.

When you find that a lot of the fields in such a table are NULLs, and can be grouped together (eg. UserContactInfo), it is time to look at extracting the information to its own table.

You want to avoid having a table with tens/hundreds of fields with only sparsely entered data.

Rather try to group the data logically, and crete the main table containging the fields that are mostly all populated. Then you can create subsets of data, almost as you would represent them on the UI, (Contact Info, Personal Interest, Work Related Info, etc) into seperate tables.

like image 20
Adriaan Stander Avatar answered Sep 26 '22 01:09

Adriaan Stander