在数据库一表中设计一种表示树的方案

hahahahe 2009-03-29 03:20:51
在数据库一表中设计一种表示树的方案;并编写存储过程完成树的建立,结点的添加,删除,遍历等基本操作;
...全文
151 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
超级大笨狼 2009-03-31
  • 打赏
  • 举报
回复
<style>
body,td,th
{
font-size:12px;
vertical-align:top;
}
th
{
layout-flow:vertical-ideographic ;
}
</style>

<TABLE border=1>
<TR>
<TD colspan=3 align=center>两种树型结构SQL的对比.htm</TD>

</TR>
<TR>
<TH>名称</TH>
<TD>方法1:父子结构Tree1表</TD>
<TD>方法2:编码结构Tree2表</TD>
</TR>
<TR>
<TH>数据形式</TH>
<TD>
<xmp>
id pId content
--------------------------------------
1 0 节点
2 0 节点
...
11 1 节点
...
21 2 节点
...
121 12 节点
...
1110 110 节点
</xmp>
</TD>
<TD>
<xmp>
id nodeCode content
--------------------------------------
1 001 节点
2 002 节点
...
11 001001 节点
...
112 001001002 节点
...
1104 010010004 节点
...
</xmp>
</TD>
</TR>

<TR>
<TH>建表语句</TH>
<TD>
<xmp>
CREATE TABLE [dbo].[Tree1] (
[id] [int] IDENTITY (1, 1) NOT NULL ,
[pId] [int] NULL ,
[content] [varchar] (10) NULL
) ON [PRIMARY]
</xmp>
</TD>
<TD>
<xmp>
CREATE TABLE [dbo].[Tree2] (
[id] [int] IDENTITY (1, 1) NOT NULL ,
[nodeCode] [varchar] (120) NOT NULL ,
[content] [varchar] (10) NULL
) ON [PRIMARY]
</xmp>
</TD>
</TR>


<TR>
<TH>插入数据10+100+1000条,深度3,广度10</TH>
<TD>
<xmp>--truncate table Tree1
declare @E int--广度循环变量
declare @EE int--广度循环变量
declare @EEE int--广度循环变量
declare @pId int--父id
declare @content varchar(10)

set @content='节点'
--添加第1层10个节点
set @E=1
while @E<=10
begin
set @pId=0
insert tree1(pId,content) values(@pId,@content)
set @E=@E+1
end
--添加第2层100个节点
set @E=1
while @E<=10
begin
set @EE=1
while @EE<=10
begin
set @pId=@E
insert tree1(pId,content) values(@pId,@content)
set @EE=@EE+1
end
set @E=@E+1
end
--添加第3层1000个节点
set @E=1
while @E<=10
begin
set @EE=1
while @EE<=10
begin
set @EEE=1
while @EEE<=10
begin
set @pId=@E*10 + @EE
insert tree1(pId,content) values(@pId,@content)
set @EEE=@EEE+1
end
set @EE=@EE+1
end
set @E=@E+1
end
--select count(*) from tree1</xmp>

</TD>
<TD>
<xmp>--truncate table Tree2
declare @nodeCode varchar(30)
declare @content varchar(10)
declare @E int--广度循环变量
declare @EE int--广度循环变量
declare @EEE int--广度循环变量
set @content='节点'

--添加第1层10个节点
set @E=1
while @E<= 10
begin
set @nodeCode =right(rtrim(str(1000 + @E)),3)
insert Tree2(nodeCode,content) values(@nodeCode,@content)
set @E=@E+1
end
--添加第2层100个节点
set @E=1
while @E<= 10
begin
set @EE=1
while @EE<= 10
begin
set @nodeCode =right(rtrim(str(1000 + @E)),3)
set @nodeCode =@nodeCode + right(rtrim(str(1000 + @EE)),3)
insert Tree2(nodeCode,content) values(@nodeCode,@content)
set @EE=@EE+1
end
set @E=@E+1
end
--添加第3层1000个节点
set @E=1
while @E<= 10
begin
set @EE=1
while @EE<= 10
begin
set @EEE=1
while @EEE<= 10
begin
set @nodeCode =right(rtrim(str(1000 + @E)),3)
set @nodeCode =@nodeCode + right(rtrim(str(1000 + @EEE)),3)
insert Tree2(nodeCode,content) values(@nodeCode,@content)
set @EEE=@EEE+1
end
set @EE=@EE+1
end
set @E=@E+1
end
select count(*) from tree2</xmp>
</TD>
</TR>
<TR>
<TH>得到节点深度和排序的办法</TH>
<TD>
<xmp>
用函数,本例是邹建写的,效率比较高。
create function get_Deep_tree1()
returns @re table([id] int,[level] int,sid varchar(8000))
as
begin
declare @l int
set @l=0
insert @re select [id],@l,right(10000+[id],4)
from [tree1] where [pid]=0
while @@rowcount>0
begin
set @l=@l+1
insert @re select a.[id],@l,b.sid+right(10000+a.[id],4)
from [tree1] a,@re b
where a.[pid]=b.[id] and b.[level]=@l-1
end
return
end
go
</xmp>
</TD>
<TD>
<xmp>
节点深度就是编码长度/3
len(nodeCode)/3
排序只要order by nodeCode asc就可以得到
web叶面需要的
</xmp>
</TD>
</TR>

<TH>查询排序</TH>
<TD>
<xmp>
select a.id,a.content,b.[level]+1 深度
from [tree1] a,get_Deep_tree1() b
where a.[id]=b.[id]
order by b.sid
</xmp>
</TD>
<TD>
<xmp>select *,len(nodeCode)/3 深度 from tree2 order by nodeCode asc
</xmp>
</TD>
</TR>
<TR>
<TH>排序结果</TH>
<TD>
<xmp>
id content 深度
1 节点 1
11 节点 2
111 节点 3
..
120 节点 3
12 节点 2
...
1072 节点 3
</xmp>
</TD>
<TD>
<xmp>id nodeCode content 深度
1 001 节点 1
11 001001 节点 2
111 001001001 节点 3
...
210 001010010 节点 3
2 002 节点 1
21 002001 节点 2
211 002001001 节点 3
...
1088 010008008 节点 3
...
1101 010010001 节点 3
</xmp>
</TD>
</TR>
<TR>
<TH>查询时间</TH>
<TD>
<xmp>
30毫秒左右
</xmp>
</TD>
<TD>
<xmp>15毫秒左右
</xmp>
</TD>
</TR>
</TABLE>
xiaoxiangqing 2009-03-31
  • 打赏
  • 举报
回复
不错.
claro 2009-03-29
  • 打赏
  • 举报
回复
帮顶。
  • 打赏
  • 举报
回复
建议看看邹老大的第一本书(2000的那一本),上面对树形数据的存储查询解释的比较清楚
dawugui 2009-03-29
  • 打赏
  • 举报
回复

/*
标题:查询指定节点及其所有子节点的函数
作者:爱新觉罗.毓华(十八年风雨,守得冰山雪莲花开)
时间:2008-05-12
地点:广东深圳
*/

create table tb(id varchar(3) , pid varchar(3) , name varchar(10))
insert into tb values('001' , null , '广东省')
insert into tb values('002' , '001' , '广州市')
insert into tb values('003' , '001' , '深圳市')
insert into tb values('004' , '002' , '天河区')
insert into tb values('005' , '003' , '罗湖区')
insert into tb values('006' , '003' , '福田区')
insert into tb values('007' , '003' , '宝安区')
insert into tb values('008' , '007' , '西乡镇')
insert into tb values('009' , '007' , '龙华镇')
insert into tb values('010' , '007' , '松岗镇')
go

--查询指定节点及其所有子节点的函数
create function f_cid(@ID varchar(3)) returns @t_level table(id varchar(3) , level int)
as
begin
declare @level int
set @level = 1
insert into @t_level select @id , @level
while @@ROWCOUNT > 0
begin
set @level = @level + 1
insert into @t_level select a.id , @level
from tb a , @t_Level b
where a.pid = b.id and b.level = @level - 1
end
return
end
go

--调用函数查询001(广东省)及其所有子节点
select a.* from tb a , f_cid('001') b where a.id = b.id order by a.id
/*
id pid name
---- ---- ----------
001 NULL 广东省
002 001 广州市
003 001 深圳市
004 002 天河区
005 003 罗湖区
006 003 福田区
007 003 宝安区
008 007 西乡镇
009 007 龙华镇
010 007 松岗镇

(所影响的行数为 10 行)
*/

--调用函数查询002(广州市)及其所有子节点
select a.* from tb a , f_cid('002') b where a.id = b.id order by a.id
/*
id pid name
---- ---- ----------
002 001 广州市
004 002 天河区

(所影响的行数为 2 行)
*/

--调用函数查询003(深圳市)及其所有子节点
select a.* from tb a , f_cid('003') b where a.id = b.id order by a.id
/*
id pid name
---- ---- ----------
003 001 深圳市
005 003 罗湖区
006 003 福田区
007 003 宝安区
008 007 西乡镇
009 007 龙华镇
010 007 松岗镇

(所影响的行数为 7 行)
*/

drop table tb
drop function f_cid



/*
标题:查询指定节点及其所有父节点的函数
作者:爱新觉罗.毓华(十八年风雨,守得冰山雪莲花开)
时间:2008-05-12
地点:广东深圳
*/

create table tb(id varchar(3) , pid varchar(3) , name varchar(10))
insert into tb values('001' , null , '广东省')
insert into tb values('002' , '001' , '广州市')
insert into tb values('003' , '001' , '深圳市')
insert into tb values('004' , '002' , '天河区')
insert into tb values('005' , '003' , '罗湖区')
insert into tb values('006' , '003' , '福田区')
insert into tb values('007' , '003' , '宝安区')
insert into tb values('008' , '007' , '西乡镇')
insert into tb values('009' , '007' , '龙华镇')
insert into tb values('010' , '007' , '松岗镇')
go

--查询指定节点及其所有父节点的函数
create function f_pid(@id varchar(3)) returns @t_level table(id varchar(3))
as
begin
insert into @t_level select @id
select @id = pid from tb where id = @id and pid is not null
while @@ROWCOUNT > 0
begin
insert into @t_level select @id select @id = pid from tb where id = @id and pid is not null
end
return
end
go

--调用函数查询002(广州市)及其所有父节点
select a.* from tb a , f_pid('002') b where a.id = b.id order by a.id
/*
id pid name
---- ---- ----------
001 NULL 广东省
002 001 广州市

(所影响的行数为 2 行)
*/

--调用函数查询003(深圳市)及其所有父节点
select a.* from tb a , f_pid('003') b where a.id = b.id order by a.id
/*
id pid name
---- ---- ----------
001 NULL 广东省
003 001 深圳市

(所影响的行数为 2 行)
*/

--调用函数查询008(西乡镇)及其所有父节点
select a.* from tb a , f_pid('008') b where a.id = b.id order by a.id
/*
id pid name
---- ---- ----------
001 NULL 广东省
003 001 深圳市
007 003 宝安区
008 007 西乡镇

(所影响的行数为 4 行)
*/

drop table tb
drop function f_pid

22,211

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧