Design1:数据层次结构建模之一

在现实世界中,有很多现象存在层次结构,比如,公司的人事职称是典型的层次结果,如下图

SQL Server是关系型DB,适合存储二维关系的数据,如何存储具有层次结构的数据了?需要使用一个字段ParentID表示上级ID,示例表结构如下

create table dbo.emph
(
    ID int not null primary key,
    ParentID int foreign key references dbo.emph(id),
    Descr varchar(100) not null
)

插入示例数据

insert into dbo.emph(id,ParentID,Descr)
values(1,null,'boss'),
(2,1,'M1'),(3,1,'M2'),(4,1,'M3'),(5,1,'M4'),
(6,2,'L11'),(20,2,'L12'),(7,2,'L13'),(8,2,'L14'),(9,2,'L15'),
(10,3,'L21'),(11,3,'L22'),(12,3,'L23'),(14,3,'L24'),
(15,6,'E111'),(16,6,'E112'),(17,6,'E113'),(18,6,'E114'),
(19,20,'E121'),(21,20,'E122'),(22,20,'E123')

一,递归查询

使用CTE递归查询M1手下的所有员工,包括Leader和Employee

;with cte(id,parentid,descr) as
(
    select id,parentid,descr
    from dbo.emph 
    where id=2

    union all

    select e.ID,e.ParentID,e.Descr
    from dbo.emph e
    inner join cte c on e.ParentID=c.id
)
select *
from cte
order by parentid

二,查看查询嵌套的Level

;with cte(id,parentid,descr,Level) as
(
    select id,parentid,descr,0 as Level
    from dbo.emph 
    where id=2

    union all

    select e.ID,e.ParentID,e.Descr,Level+1 as Level
    from dbo.emph e
    inner join cte c on e.ParentID=c.id
)
select *
from cte
order by parentid

 

三,查看每一个行数据的路径

查看每一行数据的Path,便于查看归属关系,path是使用ID来界定的

;with cte(path,id,parentid,descr,Level) as
(
select cast('\'+cast(id as varchar) as varchar(100)) as path, 
  id,parentid,descr,0 as Level from dbo.emph where id=2 union all select cast(c.path+'\'+ cast(e.ID as varchar) as varchar(100)) as path, e.ID,e.ParentID,e.Descr,Level+1 as Level from dbo.emph e inner join cte c on e.ParentID=c.id ) select * from cte order by parentid

 

推荐文档:

树形结构的数据库表Schema设计

posted @ 2015-06-17 14:22  悦光阴  阅读(1087)  评论(0编辑  收藏  举报