Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should there be a separate table when mapping one to many relationship?

Tags:

sql

mysql

In terms of performance and scalability which one would be a better method to do one to many mappings in MySQL.

  • Using a separate column but sticking to 2 tables:

    (person) : id, name

    (phone) : id, number, type, person_id

  • Using a separate table:

    (person) : id, name

    (phone) : id, number, type

    (person_phone) : id, person_id, phone_id

like image 225
Shoaibi Avatar asked Aug 01 '11 13:08

Shoaibi


2 Answers

There's only one correct answer to this, and it's the first one.

The second of your ideas is how you model many-to-many relationships, not one-to-many.

like image 138
Dan Grossman Avatar answered Nov 20 '22 03:11

Dan Grossman


In terms of performance it is always cheaper to avoid joins, which add multiplicity to the amount of rows to be queried.

As long as a single phone will only be used by one employee (a true one-to-many) the first option is best.

like image 23
Eddie Avatar answered Nov 20 '22 04:11

Eddie