简体   繁体   中英

How do I properly design a SQL database so that I can store nodes and connections to other nodes?

Say for example that I have a table of items like so:

  ID
------
  0
  1
  2
  3
  4

and each item in the table needs to be able to 'link' to N number of other items in that same table. So for example item 0 might link to item 2 and item 4. The first obvious solution seems to be to store some sort of array as another column, and that array can contain integers to 'link' to the other items in that same table. However SQL doesn't really have an array feature, and using a string will make doing specific queries take longer because I'll have to use LIKE %%.

So there must be some way to do it using another table to store the dynamic list of integers.

What's the normal way to store this sort of data in SQL?

It seems like you're trying to link database items via a many-to-many relationship .

To map this in a relational SQL database, you would have one table containing your nodes/items, where you can describe their "name" and any other attributes they have:

item_id | name
----------------
0       | test
1       | abcd
2       | node_2
3       | item_3
4       | item_4

And then you would have another table mapping elements ( 0->2 , 0->4 ) together, which describes a connection between two nodes in the graph, as the following connection table:

connection_id | item_id_1 | item_id_2
--------------------------------------
0             | 0         | 2
1             | 0         | 4

Each connection has it's own id for easy editing, and it links to the items via their ids. You can specify as many connections between nodes as you like in this table. This maps a one-way, many-to-many relationship. You can use a similar table to map two different entities together, by just mapping the ids.

As an example, you can then run a query to get the names of two joined items like so:

SELECT
    i1.name AS 'item_1_name'
    i2.name AS 'item_2_name'
FROM
    connection AS c
    INNER JOIN items AS i1
        ON i1.item_id = c.item_id_1
    INNER JOIN items AS i2
        ON i2.item_id = c.item_id_2

Which would return the following:

item_1_name | item_2_name
-------------------------
test        | node_2
test        | item_4

For more detailed info on setting up the tables, there is this stackoverflow question: How to make SQL many-to-many same-type relationship table although his answer about selecting them is vulnerable to SQL injection, so I'd be very careful with the PHP variables he mentions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM