码迷,mamicode.com
首页 > 其他好文 > 详细

数据层次结构建模之一

时间:2015-06-17 14:56:09      阅读:111      评论:0      收藏:0      [点我收藏+]

标签:

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

技术分享

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
)

2,插入示例数据

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)

3,使用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

技术分享

4,查看查询嵌套的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

技术分享

5,查看每一行数据的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

技术分享

 

数据层次结构建模之一

标签:

原文地址:http://www.cnblogs.com/ljhdo/p/4582851.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!