zoukankan      html  css  js  c++  java
  • Design1:数据层次结构建模之一

    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

    推荐文档:

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

  • 相关阅读:
    读 Zepto 源码之内部方法
    读Zepto源码之代码结构
    vue-auto-focus: 控制自动聚焦行为的 vue 指令
    vue-lazy-render: 延迟渲染大组件,增强页面切换流畅度
    用vue实现模态框组件
    谷歌插件Image downloader开发之popup
    关于const
    Python线程指南(转自AstralWind)
    PyQt中的图形绘制
    sizeof和strlen之间的区别
  • 原文地址:https://www.cnblogs.com/ljhdo/p/4582851.html
Copyright © 2011-2022 走看看