0

this question may asked by several people.But still I didn't get a right answer. So here it is

I want to load Continents, Countries and Cities to below combo boxes. So I need to make relation between those. Means city need be in a country and country belongs to a continent. So of course I am talking about a tree structure. So how should I implement this in MYSQL database in efficient way? How many tables need? How to do the relation among those?

Here is the image.

https://i.sstatic.net/st4oz.jpg

2
  • 2
    It's not as easy as it seems, if one wants to do it completely right. There are some countries which belong to two continents at least, i.e. Turkey and Russia. Commented Jul 10, 2014 at 16:02
  • 1
    @VMai There's always a party breaker :) Commented Jul 10, 2014 at 16:10

2 Answers 2

2

There are formal ways to represent arbitrary trees, but I think the following is simpler and should be sufficient:

CREATE TABLE Continents (
    id INT AUTO_INCREMENT NOT NULL,
    name VARCHAR(20) NOT NULL,
    PRIMARY KEY (id),
    UNIQUE (name)
)

CREATE TABLE Countries (
    id INT AUTO_INCREMENT NOT NULL,
    name VARCHAR(20) NOT NULL,
    continent INT NOT NULL,
    PRIMARY KEY (id),
    FOREIGN KEY (continent) REFERENCES Continents(id),
    UNIQUE (name)
)

CREATE TABLE Cities (
    id INT AUTO_INCREMENT NOT NULL,
    name VARCHAR(20) NOT NULL,
    country INT NOT NULL,
    PRIMARY KEY (id),
    FOREIGN KEY (country) REFERENCES Countries(id),
    UNIQUE (name)
)

I didn't test the code, so there may be some syntax errors. I hope the intent is clear, though.

@Vmai raises an excellent point in his comment on the question.
My solution would be to have the "problem countries" once in the Countries table for every continent they are in. (So Turkey would be twice in the database, once with continent set to the id of Asia, and once to the id of Europe. Same for Russia.) The same goes for cities, of course.

Sign up to request clarification or add additional context in comments.

10 Comments

Thank you. but what you mean by "There are formal ways to represent arbitrary trees"? Why is there any efficient method?
What about i.e. Omsk (Russia) and Moscow (Russia) :-) And what about Istanbul?
I mean there are ways to convert an arbitrarily deep tree to a database table, but I think those are unnecessarily complex for your case. Spltting the tree into multiple tables is IMHO much easier to read/comprehend, when you know the depth of the tree beforehand (and it makes sense to split the data - which I think it does, because a country is not a city is not a continent). @VMai
@VMai I answered your comments in reverse order by the way. I hope I didn't confuse you.
@VMai one formal way is as Hex494D49 suggests, another is Modified Preorder Tree Traversal. With formal I mean these ways have been mathematically proven and researched.
|
2

All you need is one table. As long as the structure of data is the same, there's no reason to have three tables. And this way, you don't care about the depth and how the locations administratively are organized.

+-------------------------+
| locations               |
+-------------------------+
| location_id   (int)     | primary key
| location_name (varchar) |
| parent_id     (int)     | index   
+-------------------------+

Or another solution

+-------------------------+
| locations               |
+-------------------------+
| location_id   (int)     | primary key
| location_name (varchar) |
| left          (int)     |
| right         (int)     |       
+-------------------------+

To be explained...

1 Comment

This model (maybe in combination with a closure table for easy access to complete paths) is more flexible. I. e. North America -> US -> California -> Los Angeles (there may be subdivisions between California and Los Angeles too ...) vs. Europe -> Luxembourg -> Luxembourg. But on a minor scale and for a special use case I've had implemented the solution of @11684 The root object was a federal state and the border cases appeared later. I didn't forgot them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.