It is very convenient to store the graph in the database: you have a table for nodes and a table for edges, which acts as a many-to-many relationship table between a node table and itself. Like this:
create table node ( id integer primary key ); create table edge ( start_id integer references node, end_id integer references node, primary key (start_id, end_id) );
However, there are a few sticky points regarding the storage of the chart this way.
First, the edges of this pattern are naturally directed β the beginning and the end are different. If your edges are not oriented, you will have to either be careful when writing queries, or store two records in a table for each edge, one in any direction (and then be careful when writing queries!). If you are storing one edge, I would suggest normalizing the saved form - perhaps always consider the node with the smallest identifier, which should be the initial one (and add a check constraint to the table to ensure it runs). You may have a truly disordered view if it has no edges, but they have a table of connections between them, but for me this does not seem like a great idea.
Secondly, the above diagram does not have the ability to represent multigraphy. You can expand it easily enough to do this; if the edges between a given pair of nodes are indistinguishable, it is easiest to add an account to each line of the edge, indicating how many edges exist between the mentioned nodes. If they are distinguishable, you will need to add something to the node table so that they can be distinguished - the identifier of the auto-generated edge may be the simplest.
However, even after disassembling the storage, you had a problem working with the schedule. If you want to do all your processing on objects in memory, and the database is intended solely for storage, then there is no problem. But if you want to make queries on a chart in a database, you will need to figure out how to do it in SQL, which does not have built-in support for charts and whose basic operations are not easy to adapt to working with charts. This can be done, especially if you have a database with recursive SQL support (PostgreSQL, Firebird, some of the proprietary databases), but this requires some thought. If you want to do this, my suggestion would be to post additional questions about specific queries.
Tom anderson
source share