Click here to Skip to main content
15,915,777 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hi everybody!

I have a many-to-many relationship problem:

The facts:

- A Prefecture has many Municipalities.
A Municipality belongs to a single Prefecture.
(one-to-may relationship)
- A Prefecture has many Cities.
A City belongs to a single Municipality.
(one-to-may relationship)
Now
- A Municipality can have one or more Cities.
- A City can have one or more Municipalities.
That is, Municipalities and Cities have a many-to-many relation.

I think of creating a tblMunicipalityCity junction table to serve the many-to-many relationship:

                                       tblMunicipalityCity
                                       -------------------
[tblPrefecture] PrefectureID      ->   PrefectureID     
[tblMunicipality] MunicipalityID  ->   MunicipalityID 
[tblCity] CityID                  ->   CityID  


Would you think this is a good solution?
Posted
Comments
Herman<T>.Instance 25-Aug-11 7:36am    
it can be a good solution. But I miss the clue in your story. how you want to show or use the data? What makes this solution valid?
Corbex 25-Aug-11 7:47am    
Thanks digimanus!

Well, this has to do with the address of say a store. A user should be able to search for stores in a certain city, municipality or prefecture.

The problem is the tblPrefecture table as it relates with a one-to-many relationship to both the tblMunicipality and tblCity tables.

I think I shouldn't include the PrefectureID field as a FK in tblMunicipality and tblCity tables.

Instead I think I should include it in tblMunicipalityCity junction table. What do you think?

Thanks again, Corbex
Herman<T>.Instance 25-Aug-11 7:57am    
I defenately would go for the junction table otherwist the citytable can't be matched with municipality table
Anil Honey 206 25-Aug-11 7:48am    
It is Good But Under CityId MunicipalityId Should Come Bcoz City is the Main Preference.In one City we can Get Lot of Municipalities.
Corbex 25-Aug-11 8:20am    
Thanks for your answer archanakumari!

The thing is that there are also Municipalities that have more than one Cities.
Also both Municipalities and Cities are related to Prefectures in a one-to-many relationship.
So when Municipalities and Cities are related to one-another through the tblMunicipalityCity junction table there shouldn't be allowed to differ in terms of the Prefecture relationship - that is, both Municipalities and Cities should be related to the SAME Prefecture.

Thanks again, Corbex

1 solution

Yes, go with your junction table, and remember to build your index according to the most common search. You might want to capture some search queries and see what your users are actually doing.

One thing I haven't regretted is the addition of CreatedBy, CreateDate, LastUpdateBy and LastUpdateDate (or similar) fields in the junction table as well. This is especially true when you have multiple admins/contributors.

Cheers.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900