<p>I am designing a contact management system and have come across an interesting issue regarding modeling geographic locations in a consistent way. I would like to be able to record locations associated with a particular person (mailing address(es) for work, school, home, etc.) My thought is to create a table of locales such as the following:</p>

<p><strong>Locales (ID, LocationName, ParentID)</strong> where autonomous locations (such as countries, e.g. USA) are parents of themselves. This way I can have an arbitrarily deep nesting of 'political units' (COUNTRY > STATE > CITY or COUNTRY > STATE > CITY > UNIVERSITY). Some queries will necessarily involve recursion.</p>

<p>I would appreciate any other recommendations or perhaps advice regarding predictable issues that I am likely to encounter with such a scheme.</p>

[{'answer_id': 50259, 'author': 'Kyle Cronin', 'author_id': 658, 'author_profile': 'https://Stackoverflow.com/users/658', 'pm_score': 3, 'selected': False, 'text': '<p>Sounds like a good approach to me. The one thing that I\'m not clear on when reading you post is what "parents of themselves" means - if this is to indicate that the locale does not have a parent, you\'re better off using null than the ID of itself.</p>\n'}, {'answer_id': 50269, 'author': 'Paul Tomblin', 'author_id': 3333, 'author_profile': 'https://Stackoverflow.com/users/3333', 'pm_score': 4, 'selected': True, 'text': '<p>You might want to have a look at Freebase.com as a site that\'s had some open discussion about what a "location" means and what it means when a location is included in another.  These sorts of questions can generate a lot of discussion.</p>\n\n<p>For example, there is the obvious "geographic nesting", but there are less obvious logical nestings.  For example, in a strictly geographic sense, Vatican City is nested within Italy.  But it\'s not nested politically.  Similarly, if your user is located in a research center that belongs to a university, but isn\'t located on the University\'s property, do you model that relationship or not?</p>\n'}, {'answer_id': 50429, 'author': 'JPLemme', 'author_id': 1019, 'author_profile': 'https://Stackoverflow.com/users/1019', 'pm_score': 2, 'selected': False, 'text': '<p>I think you might be overthinking this. There\'s a reason most systems just store addresses and maybe a table of countries. Here are some things to look out for:</p>\n\n<ol>\n<li><p>Would an address in the Bronx include the borough as a level in the hierarchy? Would an address in an unincorporated area eliminate the "city" level of the hierarchy? How do you model an address within a university vs an address that\'s not within one? You\'ll end up with a ragged hierarchy which will force you to traverse the tree every time you need to display an address in your application. If you have an "address book" page the performance hit could be significant.</p></li>\n<li><p>I\'m not sure that you even have just one hierarchy. Brown University has facilities in Providence, RI and Bristol, RI. The only clean solution would be to have a double hierarchy with two campuses that each belong to their respective cities in one hierarchy but that both belong to Brown University on the other hierarchy. (A university is fundamentally unlike a political region. You shouldn\'t really mix them.)</p></li>\n<li><p>What about zip codes? Some zip codes encompass multiple towns, other times a city is broken into multiple zip codes. And (rarely) some zip codes even cross state lines. (According to Wikipedia, at least...)</p></li>\n<li><p>How will you enter the data? Building out the database by parsing conventionally-formatted addresses can be difficult when you take into account vanity addresses, alternate names for certain streets, different international formats, etc. And I think that entering every address hierarchically would be a PITA.</p></li>\n<li><p>It sounds like you\'re trying to model the entire world in your application. Do you really want or need to maintain a table that could conceivable contain every city, state, province, postal code, and country in the world? (Or at least every one where you know somebody?) The only thing I can think of that this scheme would buy you is proximity, but if that\'s what you want I\'d just store state and country separately (and maybe the zip code) and add latitude and longitude data from Google.</p></li>\n</ol>\n\n<p>Sorry for the extreme pessimism, but I\'ve gone down that road myself. It\'s logically beautiful and elegant, but it doesn\'t work so well in practice.</p>\n'}, {'answer_id': 50689, 'author': 'andyuk', 'author_id': 2108, 'author_profile': 'https://Stackoverflow.com/users/2108', 'pm_score': 2, 'selected': False, 'text': '<p>I would think carefully about this since it may not be a necessary feature. \nWhy not just use a text field and let users type in an address?</p>\n\n<p>Remember the <a href="http://en.wikipedia.org/wiki/KISS_principle" rel="nofollow noreferrer">KISS principle</a> (Keep It Simple, Stupid). </p>\n'}, {'answer_id': 151817, 'author': 'Andrew not the Saint', 'author_id': 23670, 'author_profile': 'https://Stackoverflow.com/users/23670', 'pm_score': 2, 'selected': False, 'text': "<p>Here's a suggestion for a pretty flexible schema. An immediate warning: it could be too flexible/complex for what you actually need</p>\n\n<p>Location\n(LocationID, LocationName)\n-- Basic building block</p>\n\n<p>LocationGroup\n(LocationGroupID, LocationGroupName, ParentLocationGroupID)\n-- This can effective encapsulate multiple hierarchies. You have one root node and then you can create multiple independent branches. E.g. you can split by state first and then create several sub-hierarchies e.g. ZIP/city/xxxx</p>\n\n<p>LocationGroupLocation\n(LocationID, LocationGroupID)\n-- Here's how you link Location with one or more hierarchies. E.g. you can link your house to a ZIP, as well as a City... What you need to implement is a constraint that you should not be able to link up a location with any two hierarchies where one of them is a parent of the other (as the relationship is already implicit). </p>\n"}, {'answer_id': 151838, 'author': 'Dr8k', 'author_id': 6014, 'author_profile': 'https://Stackoverflow.com/users/6014', 'pm_score': 1, 'selected': False, 'text': '<p>I agree with the other posts that you need to be very careful here about your requirements. Location can become a tricky issue and this is why GIS systems are so complicted.</p>\n\n<p>If you are sure you just need a basic heirarchy structure, I have the following suggestions:</p>\n\n<ul>\n<li>I support the previous comment that root level items should not have themselves as the parent. Root level items should have a null value for the parent. Always be careful about putting data into a field that has no meaning (i.e. "special" value to represent no data). This practice is rarely necessarily and <strong>way</strong> overused in the devleoper community.</li>\n<li>Consider XPath / XML. This is Something to consider for bother recording the heirarchy structure, and for processing / parsing the data at retrieval. If you are using MSSQL Server, the XPath expressions in select statements are perfect for tasks such as returning the full location/heirarchy path of a record as the code is simple and the results are fast.</li>\n</ul>\n'}, {'answer_id': 151857, 'author': 'CAD bloke', 'author_id': 492, 'author_profile': 'https://Stackoverflow.com/users/492', 'pm_score': 1, 'selected': False, 'text': "<p>For Geographic locations you may wish to resolve an address to a Latitude, Longitude array (perhaps using Google maps etc.) to calculate proximities etc.. For Geopolitical nesting ... I'd go with the KISS response. </p>\n\n<p>If you really want to model it, perhaps you need the types to be more generic ... Country -> State -> County -> Borough -> Locality -> City -> Suburb -> Street or PO Box -> Number -> -> Appartment etc. -> Institution (University or Employer) -> Division -> Subdivision-1 -> subdivision-n ... Are you sure you can't do KISS?</p>\n"}, {'answer_id': 4046673, 'author': 'M.Torres', 'author_id': 383835, 'author_profile': 'https://Stackoverflow.com/users/383835', 'pm_score': 0, 'selected': False, 'text': "<p>I'm modeling an apps for global users and I have the same problems, but I think that this approach could already be in use in many enterprise. But why this problem don't have an universal solution? Or, has this problem one best solution that can be the start point or anybody in the world need think in a solution for it since beginnig?\nIn IT, we are making the same things any times and in many places, unfortunately. For exemplo, who are not have made more than one user, customer or product's database? And the worst, all enterprise in the world has made it. I think that could have universal solutions for universal problems.</p>\n"}]
['https://Stackoverflow.com/questions/50252', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3927/']