Mysql , php newbie here. Please be nice .
I have a list of colonies – colony1 , colony2,…, colony100.
Say Colony5 is nearby to colony4, colony9 and colony10.
Colony4 is nearby to colony5, colony9, colony10 and colony11 . Different colonies have different number of nearby colonies.
How do I store and fetch this data in mysql ?
Currently I am thinking a table that would look like this ->
id | colony name | nearby_colony_id_1 |nearby_colony_id_2 |nearby_colony_id_3 | nearby_colony_id_4
Is there a better way of doing this ?
I hope my question was clear.
You need two tables. The first will hold the colony specifications (id, name, …), the second will hold links between colonies. In the first table you will have one row per colony.
Ex:
In the second table, you will have a column for the id of the colony, a column for the id of the nearby colony. In this table, you will have one row per colony neighbour.
Ex (here colony1 has colony 2 and 3 as nearby colonies):
You can use foreign key to ensure that the colony referenced in table 2 does exist in table 1.