sql-server - SQL Server:图形/层次结构 SQL 结构
问题描述
经过N小时的设计,如何将这样的结构保存到关系数据库(SQL Server)中。
我结束了这个结构,但它并不是很好。
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 Server 等关系数据库中?
谢谢
解决方案
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.
推荐阅读
- php - 如何使用 PHP 制作多语言网站?
- android - 在 View 中实现旋转和缩放
- javascript - 如何将状态作为道具传递,以便您可以修改它们
- javascript - websocket连接在nginx龙卷风上立即关闭
- react-native - 反应本机获取请求网络错误
- python - Python:如何在 Windows 上与 unicode 文件名交互?(Python 2.7)
- javascript - 在 html onlick 标签中插入一个大的 JavaScript
- excel - 基于模板的字母生成程序
- django - Django 1.11:在 url 中的查询参数中浮动
- ocaml - Ocaml 和返回类型(图论)