开发者

MySQL relations for country region and town tables

开发者 https://www.devze.com 2023-02-12 04:52 出处:网络
I am trying to figure out the best method to relate country, region and town tables. On my website I want the user to be able to just enter a town. Then optionally country and region, both of which wi

I am trying to figure out the best method to relate country, region and town tables.

On my website I want the user to be able to just enter a town. Then optionally country and region, both of which will be required to be entered or not at all.

Currently my tables are as such

tbl>User has townID (FK)

tbl>town has id(PK) townName regionID(FK DEFAULT NULL)

tbl>region has id(PK) regionName countryID(FK NOT NULL)

tbl>country has id(PK) countryName

I thought to possibly further spit the user to town relation to:

tbl>User has locationID (FK)

tbl>location has id (PK) townID(FK) regionID(FK) countryID(FK)

But I think that is unnecessary and just further complicates the issue?

The country database is already populated. I intend to build up my own references of town > region > country relations as entered by users. So if a user enters a town with no region and country then it is entered into tbl>town without a regionID if there isn't already a town with the same name without a region ID. This is same for a town where a region and country ID has been entered by the user. Only I check that there isn't already a town > region > country relation that already exists before entering. Later on in the development of the site I will be providing Ajax suggestions for country/region based upon the town entered by a user.

So to the questions:

I can envisage pitfalls with this such as duplicate data or data possibly being overwritten. Is there a better way to construct the tables to fit in with my desired m开发者_运维知识库ethods?

This might get answered by the prior question: but is there anything I can do to reduce the PHP processing of the tables. Obviously I'd prefer to just insert with one PHP statement but I think there are too many caveats to do it at once.

Also as the users town entry may be null and may or may not contain a foreign key reference to a region how is it best to create a View that takes that into consideration?

As it will be hosted I would rather not be using MySQL functions.

Please let me know if you need any clarification. I really want to get this right the first time before continuing, so your help will be invaluable.


I don't think you reduce the code because it's much too explicit. You can change it, but it won't be better.


Accepting a town name without a region and country is like letting someone enter their first name without their middle or last. It's data, but it's not an identifier.

Fullerton's full name is "Fullerton, California, USA". By not requiring Fullerton's full name, you abandon foreign keys for data integrity. ("Fullerton, California, USA" is a city; "Fullerton, Alabama, USA" is not.) Good luck with that.

If you're going down this path, the best advice I can offer you is get rid of the id numbers. ISO publishes standard codes for countries and subdivisions of countries. You can look them up in Wikipedia. Storing natural keys will reduce the number of joins from 3 to zero. Zero joins is almost always going to out perform 3 joins.

You'll probably need to use outer joins to create your views.

0

精彩评论

暂无评论...
验证码 换一张
取 消