Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are advantages of using a one-to-one table relationship? (MySQL)

What are advantages of using a one-to-one table relationship as opposed to simply storing all the data in one table? I understand and make use of one-to-many, many-to-one, and many-to-many all the time, but implementing a one-to-one relationship seems like a tedious and unnecessary task, especially if you use naming conventions for relating (php) objects to database tables.

I couldn't find anything on the net or on this site that could supply a good real-world example of a one-to-one relationship. At first I thought it might be logical to separate 'users', for example, into two tables, one containing public information like an 'about me' for profile pages and one containing private information such as login/password, etc. But why go through all the trouble of using unnecessary JOINS when you can just choose which fields to select from that table anyway? If I'm displaying the user's profile page, obviously I would only SELECT id,username,email,aboutme etc. and not the fields containing their private info.

Anyone care to enlighten me with some real-world examples of one-to-one relationships?

like image 825
Lotus Notes Avatar asked Mar 26 '10 07:03

Lotus Notes


1 Answers

One possible use is when part of the information is optional. This way you don't need to have a bunch of nullable fields in one big table, but can separate it logically into the mandatory table and an optional table.

Other use is when some of the data is shared with different tables. For instance let's say you have a site where you sell computer parts. You could put the details that all components share into eg. "parts" table, but put the specifics in "motherboards", "cpus", etc. which would just use parts table with one-to-one relation.

like image 110
reko_t Avatar answered Oct 20 '22 05:10

reko_t