树型结构数据在数据库基本表中的存储及维护

来源:互联网 发布:淘宝卖家分享有礼 编辑:程序博客网 时间:2024/04/29 15:47
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

相关讨论连接:
早就想简单说说: 关于树型结构数据存储维护http://expert.csdn.net/Expert/TopicView1.asp?id=1677669

树型结构数据存储采用:
Tree(ID,ParentID,Remark)
如果仅对于存储来讲,无疑是最经济!
但是利用这样的结构,来提供一些基于稍微复杂点的查询的应用表现形式
效率应该说相当低下!
如: 查询某节点的路径等!
如要高效的查询,我们可以在维护数据时下点功夫!
我们以一个树型结构论坛的实现为例:

Tree(ID,ParentID,RootID,OrderID,MaxID,Indent,Title,Content,Remark)
      ID: Integer 帖子ID
ParentID: Integer 父贴ID
  RootID: Integer 根帖ID
 OrderID: Integer 同一个根帖中,帖子顺序ID
   MaxID: Integer 用于使新贴在顶部
  Indent: Integer 缩进量
   Title: Varchar 帖子标题
 Content: Varchar 帖子内容
  Remark: Varchar 除 ID,ParentID 外的贴子线索

这样的设计只要维护好每一个字段都为查询显示提高了效率!
请看下面的维护程序:
--==========================================
alter procedure AppSP_AddNew
@ID integer
,@Title varchar(8000) =null
,@Content varchar(8000)=null
as
--declare @id int
--set @id=0
if @ID=0
   begin
      insert into Tree (ParentID,OrderID,Indent,Title,Content)
                values (0,0,0,@Title,@Content)
      --把帖子顶到上面:
      update Tree
         set RootID = ID
             ,MaxId = (select max(id) from Tree)
       where RootID is null
   end
else
   begin
      --调整同一个"根帖"中,帖子的内部顺序:
      update Tree
         set OrderID = OrderID + 1
       where RootID = (select rootid
                         from tree
                        where ID = @id)
             and OrderID > (select OrderID
                              from Tree
                             where ID = @id
                           )
      --插入回复的帖子,同时维护 RootID,ParentID,OrderID,Indent,remark,Title,Content
      insert into Tree (RootID,ParentID,OrderID,Indent,remark,Title,Content)
           select RootID,@ID,OrderID+1,Indent + 1
                  ,case when remark is null then cast(parentid as varchar)
                        else remark + '-' + cast(parentid as varchar)
                   end
                  ,isnull(@Title,'Re: ' + Title),@Content
             from Tree
            where id=@id
      --把帖子顶到上面:
      update Tree
         set maxid = (select max(id)
                       from Tree
                     )
      where rootid = (select rootid
                        from tree
                       where id=@id
                     )
   end
--========================================

该程序用于
1.增加新贴:
 AppSP_AddNew 0,'第一个问题','地球是圆的吗?'
2.回复帖子:
 AppSP_AddNew 1,'Re: 第一个问题','地球是圆的!'

这样,只需简单查询:
select *,  remark  + '-' + cast(parentid as varchar) + '-' + cast(id as varchar) , space(indent) + '['
from tree
order by MaxID desc,orderid
就可高效的实现帖子列表及其线索,级别等!
虽然维护时增加了一些工作量!

--相关DDL脚本:
CREATE TABLE [Tree] (
 [ID] [int] IDENTITY (1, 1) NOT NULL ,
 [ParentID] [int] NULL ,
 [RootID] [int] NULL ,
 [OrderID] [int] NULL ,
 [MaxID] [int] NULL ,
 [Indent] [int] NULL ,
 [Title] [varchar] (50),
 [Content] [varchar] (200) ,
 [Remark] [varchar] (250) ,
 CONSTRAINT [PK_Tree] PRIMARY KEY  CLUSTERED
 (
  [ID]
 )  ON [PRIMARY]
) ON [PRIMARY]

<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击