I'm trying to model which countries border each other in MySQL. I have three tables:
nodes
-----
node_id MEDIUMINT
countries
---------
country_id MEDIUMINT (used as a foreign key for nodes.node_id)
country CHAR(64)
iso_code CHAR(2)
node_adjacency
--------------
node_id_1 MEDIUMINT (used as a foreign key for nodes.node_id)
node_id_2 MEDIUMINT (used as a foreign key for nodes.node_id)
I appreciate the nodes table is redundant in this example, but this is part of a larger architecture where nodes can represent many other items other than countries.
Here's some data (IDs (which appear in all three tables) and countries)
59 Bosnia and Herzegovina
86 Croatia
130 Hungary
178 Montenegro
227 Serbia
232 Slovenia
Croatia is bordered by all the other countries, and this is represented in the node_adjacency table as:
59 86
86 130
86 178
86 227
86 232
So Serbia's ID may appear as a node_id_1
or a node_id_2
. The data in this table is essentially non directed graph data.
Questions:
Given the name 'Croatia', what SQL should I use to retrieve its neighbours?
Bosnia and Herzegovina
Hungary
Montenegro
Serbia
Slovenia
Would there be any retrieval efficiency gains in storing the adjacency information as directed graph data? E.g. Croatia borders Hungary, and Hungary borders Croatia, essentially duplicating storage of the relationships:
86 130
130 86