I have a directed graph with nodes. Call it a graph of cities and roads connecting them. The cities are named in a mostly but not guaranteed-unique way. A road is identified by the city it departs from, the direction in which it leaves the city, and the destination city. The number of roads out of a given city is variable. The roads are directional: if two cities are connected in both directions, there are two roads (this being the usual case).
Restructuring the database is an option.
Presently,
a city has:
a unique integer ID (autoincrement, there's no significance to it)
a name (mostly but not entirely unique)
a road has:
a unique integer ID (autoincrement, there's no significance to it)
a from ID (refers to city)
a to ID (refers to city)
My problem:
I am given a node name and the list of paths applying to it. The paths are identified by direction and destination, not by id. Is there an efficient way in SQL to write a query for a list of cities matching that description?
I'm not experienced in SQL. Finding a collection of cities with the given name, processing the results in a procedural language, and then doing followup queries against those particular cities to see if the roads match is easy, but is there a reasonable way to do that in one step with SQL?
Restructuring the database is an option.
Presently,
a city has:
a unique integer ID (autoincrement, there's no significance to it)
a name (mostly but not entirely unique)
a road has:
a unique integer ID (autoincrement, there's no significance to it)
a from ID (refers to city)
a to ID (refers to city)
My problem:
I am given a node name and the list of paths applying to it. The paths are identified by direction and destination, not by id. Is there an efficient way in SQL to write a query for a list of cities matching that description?
I'm not experienced in SQL. Finding a collection of cities with the given name, processing the results in a procedural language, and then doing followup queries against those particular cities to see if the roads match is easy, but is there a reasonable way to do that in one step with SQL?