thanks in advance for your input.
I have 3 objects:
- School
- Camp
- Coach
With the following relationships:
- A School can have multiple Camps.
- A School can have multiple Coaches.
- A Camp can have multiple Schools.
- A Camp can have multiple Coaches.
- A Coach can have multiple Schools.
- A Coach can have multiple Camps.
A many to many, School_Camp, obviously links a School to a Camp with an additional field for Date, to identify the year of the camp. But one camp can have multiple coaches.
::School_Camp::
- School_id
- Camp_id
- Date
Would it be better for me to set up another many to many, School_Camp_Coach, that links to the School_Camp and Coach tables?
::School_Camp_Coach::
- School_Camp_id
- Coach_id
If this is the more efficient way to do this… should I give School_Camp an independent id column that can quickly be referenced rather than using the three fields as an identifier?
::School_Camp::
- id*
- School_id
- Camp_id
- Date
OR
Is it better to just have ONE many to many table, School_Camp_Coach, with 3 foreign keys?
::School_Camp_Coach::
- School_id
- Camp_id
- Coach_id
- Date
The only issue I foresee with this, is that you’ll have multiple entries for the foreign keys, but with different Dates.
Thanks again.
It is not a matter of efficiency, it is a matter of correctness: the two options that you presented do not model the same relationship between the records in the tables.
Each record in the database means something. If the meaning of the record in the junction table is “X coached at the camp Y of the school Z”, then you should go with the option 2; if you are looking to model the meanings “X coached at the camp Y” independently of “X coached for school Z” and independently of “School Z ran camp Y”, then you should go with option 1.
In both cases you should give your junction records independent primary keys, rather than relying on three-way combination of IDs: it will simplify your life when you implement code that applies corrections to the junction table.