Implementing multi-level trees in MS SQL Server

Introduction

Many projects now are in need of management of multi-tree level data. There are some ways to do this, but in this article I will introduce a good way to setup and install multi-tree level structures by using an MS SQL Server database. Our result is fully tested in MS SQL Server 2005/2008.

The tree will be shown and managed by the value of a column, I call it by "index-dictionary" term. By seeing this value or ordering this value we can understand the tree‘s structure.

My article will show you a simple way to work with tree data. This solution can deal with any tree-level you want, I will use a sample tree level = 3.

When you have a requirement which needs a tree data structure, you can use this solution and it will save you time. After installing this solution you don‘t need to pay attention to learn about CTE queries or other complicated ways to build a tree. Just select all rows and order by "index-dictionary" column, the tree is built up! Really easy.

This solution uses ordinal, alphabets, and Romans to mark the tree level:

  • Upper case letters such as A, B for level 0 - root level.
  • Roman letters for level 1
  • Numbers for level 2

You can download the SQL script or full DB restoration in the zip sample file.

Purpose and Design

Our purpose will be to create a multi-level tree and index the line by using multiple types of sequence numbers, such as ordinal numbers (1, 2, 3..), alphabets (A, B, C..), or Romans (I, II, III...). The result may be shown as:

Figure 1

To deploy this solution we will create a tree-data table in MS SQL Server using this structure:

Figure 2

Tree_ID is the auto increasing sequence. It is known as the tree NodeIDParent_ID is null or 0 when Tree_IDis the root of the tree. When Tree_ID is a child node, Parent_ID is the Tree_ID of the parent node.Seq_Index is an indicator for child-node sequence-number, a valid value is only a single-index: A, B, C or 1, 2, 3...Full_index shows the full-path dictionary to the child node from the root, such as: A.I.1, A.II.1, B.I.2, B.I.3...

Using the code

We will create a function and a Stored Procedure (SP) and separate them to three-groups. You can find all these functions and the SP in the scripts file or restore the full database.

Group 1: Functions

  • [count_tree_level]: Function to check level of tree-node.
  • [get_seq_by_level]: Function to calculate the seq_index by tree level.
  • [count_tree_full_index]: Function to calculate the full_index by tree_id.
  • [get_reverse_ascii]: Function to convert ordinal number to ASCII, example 1-> ‘A‘, 2 to ‘B‘. By using this function, you can convert an ordinal number to upper case characters or lower characters; this can be done by passing the ascii_pattern parameter.
  • [convert_integer_to_roma]: Function to convert ordinal number to Roman (this function I found on the internet).
  • [check_parent]: Function to return value 1 if Node_id is a child, or grand-child of the selectedparent_ID.

Group 2: SP to edit the tree

  • [insert_tree_node]: SP to insert a new tree node, also re-calculate directory
  • [remove_node]: SP to delete a tree-node, also re-calculate directory
  • [move_node_up]: SP to move-up a tree node, also re-calculate directory
  • [move_node_down]: SP to move down a tree node, also re-calculate directory

Group 3: Viewing the resultlt

  • [view_tree]: SP to view tree in order by index directory
  • [view_human_tree]: SP to view tree in order by index directory, it‘s well-known by human and same as Figure 1

Code Example

These actions below will demonstrate how to use the code to implement a multi-level tree structure. First of all, let it clear all data in DataTreeTbl. This code will insert three root nodes named "Tree A", "Tree B", and "Tree C".

go
exec dbo.insert_tree_node ‘Tree A‘,0
go
exec dbo.insert_tree_node ‘Tree B‘,0
go
exec dbo.insert_tree_node ‘Tree C‘,0
go go

From now, after running a code block, we will check again the tree by running the query:

select * from dbo.TreeDataTbl order by Full_index asc

The result after running that code:

Remember the Node ID, we will add child nodes to the above node.

First of all, we will add a first level child data to the root node (the Tree_id value is dependent on your real Tree_ID after running the code block above).

go
exec dbo.insert_tree_node ‘Tree A.I‘,73
go
exec dbo.insert_tree_node ‘Tree A.II‘,73
go
exec dbo.insert_tree_node ‘Tree B.I‘,74
go
exec dbo.insert_tree_node ‘Tree B.II‘,74
go
exec dbo.insert_tree_node ‘Tree C.I‘,75
go

The result is:

Now, we will add a second child level to the above tree:

go
exec dbo.insert_tree_node ‘Tree A.I.1‘,76
go
exec dbo.insert_tree_node ‘Tree A.I.2‘,76
go
exec dbo.insert_tree_node ‘Tree A.I.3‘,76
go
exec dbo.insert_tree_node ‘Tree A.II.1‘,77
go
exec dbo.insert_tree_node ‘Tree B.I.1‘,78
go
exec dbo.insert_tree_node ‘Tree B.I.2‘,78
go
exec dbo.insert_tree_node ‘Tree C.I.1‘,80
go
exec dbo.insert_tree_node ‘Tree C.I.2‘,80
go
exec dbo.insert_tree_node ‘Tree C.I.3‘,80
go

The result is:

Now we will edit this tree. First of all, we will move up the tree node 82. Let us run this code:

exec dbo.move_node_up 82

The result is Node Id=82 is moved above node ID=81. And full_Index is re-counted!

You can move up, move down any node. This time we will move down Tree_ID=74, a root node tree!

exec dbo.move_node_down 74

In result, you can see all nodes in "Tree B" is returned after "Tree C", and the index directory (Full_index) is re-counted also.

Now we will remove a node from a tree. I will try to remove the root node "Tree C" - Tree_ID=75.

exec dbo.remove_node 75

All Node C and its relationships are removed. "Tree B" is moved up and we re-count seq_Index andFull_index.

And finally, we will use two Stored Procedures to view the tree. Result when running SP [view_tree]:

Result when running SP [view_human_tree]:

Conclusion

As I mentioned above, we can use CTE queries to generate a tree. Let us use this CTE query:

WITH Tree_CTE(Tree_ID, Tree_name, Parent_ID, Seq_index, Full_index, Tree_level)
AS
(
    SELECT TreeDataTbl.*, 0  FROM TreeDataTbl WHERE Parent_ID =0
    UNION ALL
    SELECT ChildNode.*, Tree_level+1  FROM TreeDataTbl AS ChildNode
    INNER JOIN Tree_CTE
    ON ChildNode.Parent_ID = Tree_CTE.Tree_ID
)
SELECT * FROM Tree_CTE order by Tree_level

The tree-result is:

You can see the CTE method can count tree-level, but it cannot do as this solution can. All row levels are show in a sequence. In addition, CTE cannot help order row data in the same level by itself, you can only use fields to order. But by using this solution you can easily order the node position in a parent ID node.

This solution will give a tree-output visually. You can easily understand, see tree-structure, and count tree-level by seeing the output result.

Points of Interest

This solution may be more than three levels deep as you can see in the sample picture. If you want to change theseq_index of the tree level, customize it in the [get_seq_by_level] function. There are problems with the tree when using Roman characters: I, II, III, IV, V, VI, VII, VIII, IX, X... Number (9) in Roman = IX will occur before number (5) in Roman = V. You can find an easy way to fix this! This solution can be applied for many cases. A multi-level menu for applications or exporting reports...

Hope this article will help you gain the time when your project needs to be installed as a multi-level tree data.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

http://www.codeproject.com/Articles/162368/Implementing-a-Tree-Structure-with-Database

时间: 2024-10-16 09:18:21

Implementing multi-level trees in MS SQL Server的相关文章

MS SQL SERVER: msdb.dbo.MSdatatype_mappings & msdb.dbo.sysdatatypemappings

--SQL转Oracle/DB2的类型对应关系SELECT *FROM msdb.dbo.MSdatatype_mappings; --MS SQL SERVER更详细得显示了ORACLE/DB2各个数据库系统的类型对应SELECT *FROM msdb.dbo.sysdatatypemappings; dbms_name sql_type dest_type dest_prec dest_create_params dest_nullable DB2 bigint DECIMAL 19 3 t

MS SQL Server分析数据库的I/O性能

系统有的时候响应会明显很慢,有的时候可以是因为访问人数增加导致,有的时候可能因为数据库的磁盘I/O访问次数频繁导致.MS SQL Server提供了一些动态管理视图和函数供我们分析磁盘I/O性能. 1.sys.dm_io_virtual_file_stats SELECT DB_NAME(vfs.database_id) AS database_name , vfs.database_id , vfs.FILE_ID , io_stall_read_ms / NULLIF(num_of_read

ms sql server相关基础

在Java开发中 ms sql server 接触算是比较少的,本文记录一些ms sql server的基础知识. 1. 为表字段增加索引:create index user_openid on user(col1,col2); 删除索引:drop index users_openid on users; mysql 加索引的命令为:alter table user add index user_openid(openid) ,很容易和sql server的命令混淆. 2. win7 打开远程连

MS SQL Server2014链接MS SQL Server 2000

开发与企业应用中,好几个版本SQL Server相互链接.分布式读取与存储,需要实现sp_addlinkedserver.SQL Server 2000, SQL Server 2008, SQL Server 2012和SQL Server 2014: 但今天在本机的MS SQL Server 2014中,想添加SQL Server 2000的链接,使用下面的方法:<SQL Server创建链接服务器的一些存储过程和例子>http://www.cnblogs.com/insus/archiv

[MS SQL Server]SQL Server如何开启远程访问

在日常工作中,经常需要连接到远程的MS SQL Server数据库中.当然也经常会出现下面的连接错误. 解决方法: 1. 设置数据库允许远程连接,数据库实例名-->右键--->属性---Connections---->勾选 Allow remote connections to this server. 2. 开启TCP/IP协议: Sql Server Configuration Manager---> Protocols for SQLEXPRESS ---> TCP/I

MS SQL Server 数据库连接字符串

MS SQL Server 数据库连接字符串详解 C# 连接串设置超时时间 SqlConnection.ConnectionTimeout 与 SqlCommand.CommandTimeout 及SqlBulkCopy.BulkCopyTimeout属性 https://msdn.microsoft.com/zh-cn/library/8xx3tyca.aspx  SQL Server 连接池 (ADO.NET) http://www.docin.com/p-576271306.html 4.

MS SQL Server中的CONVERT日期格式化大全

CONVERT 函数将某种数据类型的表达式显式转换为另一种数据类型.SQL Server中 将日期格式化. SQL Server 支持使用科威特算法的阿拉伯样式中的数据格式. 在表中,左侧的两列表示将 datetime 或 smalldatetime 转换为字符数据的 style 值.给 style 值加 100,可获得包括世纪数位的四位年份 (yyyy). 不带世纪数位 (yy) 带世纪数位 (yyyy) 标准 输入/输出** - 0 或 100 (*) 默认值 mon dd yyyy hh:

学习使用MS SQL Server游标(CURSOR)

说实的,使用MS SQL Server这样久,游标一直没有使用过.以前实现相似的功能,都是使用WHILE循环加临时表来实现.刚才有参考网上示例练习写了一下.了解到游标概念与语法.下面代码示例中,先是宣告你在游标中需使用变量,也就是临时存储处理字段的数据.2. 宣告一个游标,并SELECT需要处理的数据集.3. 打开游标(#8行代码).4. 从游标中拿来FETCH NEXT 数据给变量赋值.5. 循环@@FETCH_STATUS = 0条件.6. 在循环块,可以处理第一笔的记录逻辑了.本示例中是P

MS Sql Server 中主从库的配置和使用介绍(转)

网站规模到了一定程度之后,该分的也分了,该优化的也做了优化,但是还是不能满足业务上对性能的要求:这时候我们可以考虑使用主从库. 主从库是两台服务器上的两个数据库,主库以最快的速度做增删改操作+最新数据的查询操作:从库负责查询较旧数据,做一些对实效性要求较小的分析,报表生成的工作.这样做将数据库的压力分担到两台服务器上从而保证整个系统响应的及时性. SQL Server提供了复制机制来帮我们实现主从库的机制.我们看下如何在sql server 2005中实践: 实践前需要新创建一个Test的数据库