SQL 服务器:graph/hierarchy SQL 结构

SQL Server : graph/hierarchy SQL structure

经过N个小时的设计,如何将这样的结构保存到关系数据库(SQL服务器)。

我在这个结构上结束了,但它并不是很好。

create table [OperationalModel]
(
    [Id] int,
    [Name] varchar(150),
    [Code] varchar(10),
    [OrgId] int,
    [Vertex] int,
    [RelatedOrgIdOnSameVertex] int
);

insert into [dbo].[OperationalModel] 
values
    (1, 'x', 1, NULL),
    (1, 'x', 2, 1),
    (1, 'x', 3, 1),
    (1, 'x', 4, 2, 3),
    (1, 'x', 5, 2),
    (1, 'x', 6, 2),
    (1, 'x', 7, 3),
    (1, 'x', 8, 4);
.
.
.
.

谁知道如何在关系数据库(如 SQL 服务器中保存此结构?

谢谢

you need two table. 
one is for the nodes: 
Table: OperationalModel: 
Columns: 
[Id] int,
[Name] varchar(150),
[Code] varchar(10),

another is the edges: 
Table: Edges: Columns: 
Id, OperationalModelId, VertexId, Etc ... 

Data: insert into [dbo].[OperationalModel] values
(1,'x',1),
(1,'x',2),
(1,'x',3),
(1,'x',4),
(1,'x',5),
(1,'x',6),
(1,'x',7),
(1,'x',8);

Data For second table: As per the relationship.
insert into [dbo].[Edge] values
(1,1,2),
(2,1,3),
(3,2,1),
(4,2,4),
(5,3,1),
(5,3,4),
(5,3,5);

The Edge table will be Many to many relationship.

您指的是 SQL 服务器。

因此,如果您使用的是 SQL 2017,请使用内置的图形数据库支持。您将能够利用已有的查询系统。

https://docs.microsoft.com/en-us/sql/relational-databases/graphs/sql-graph-overview?view=sql-server-ver15