树形结构与关系数据库之闭包表详解数据库
数据库 详解 结构 树形 关系数据库 之闭
2023-06-13 09:20:11 时间
闭包表记录了树中所有节点的关系,不仅仅只是直接父子关系,它需要使用2张表,除了节点表本身之外,还需要使用1张表来存储节祖先点和后代节点之间的关系(同时增加一行节点指向自身),
并且根据需要,可以增加一个字段,表示深度。因此这种方法数据量很多。设计的表结构如下:
create table `node` ( `id` int(10) not null, `name` varchar(50) default null, primary key (`id`) ) engine=innodb default charset=utf8; create table `tree_path` ( `anc` int(10) not null comment 祖先节点, `des` int(10) not null comment 子孙节点, `pc` int(1) default null comment 是否为父子节点 1/0, primary key (`anc`,`des`) ) engine=innodb default charset=utf8; -- 示例树 -- root(0) -- | -- a(1) -- |___a1(2) -- |___a2(3) -- b(4) -- |___b1(5) -- |___b2(6) -- |___c(7) -- |___c1(8) -- |___d(9) -- |___e(10)
insert into node(id,name) values (0,root); insert into node(id,name) values (1,a); insert into node(id,name) values (2,a1); insert into node(id,name) values (3,a2); insert into node(id,name) values (4,b); insert into node(id,name) values (5,b1); insert into node(id,name) values (6,b2); insert into node(id,name) values (7,c); insert into node(id,name) values (8,c1); insert into node(id,name) values (9,d); insert into node(id,name) values (10,e);
insert into tree_path(anc,des,pc) values (0,1,1); insert into tree_path(anc,des,pc) values (0,2,0); insert into tree_path(anc,des,pc) values (0,3,0); insert into tree_path(anc,des,pc) values (0,4,1); insert into tree_path(anc,des,pc) values (0,5,0); insert into tree_path(anc,des,pc) values (0,6,0); insert into tree_path(anc,des,pc) values (0,7,0); insert into tree_path(anc,des,pc) values (0,8,0); insert into tree_path(anc,des,pc) values (0,9,0); insert into tree_path(anc,des,pc) values (0,10,0); insert into tree_path(anc,des,pc) values (1,1,0); insert into tree_path(anc,des,pc) values (2,2,0); insert into tree_path(anc,des,pc) values (3,3,0); insert into tree_path(anc,des,pc) values (4,4,0); insert into tree_path(anc,des,pc) values (5,5,0); insert into tree_path(anc,des,pc) values (6,6,0); insert into tree_path(anc,des,pc) values (7,7,0); insert into tree_path(anc,des,pc) values (8,8,0); insert into tree_path(anc,des,pc) values (9,9,0); insert into tree_path(anc,des,pc) values (10,10,0); insert into tree_path(anc,des,pc) values (1,2,1); insert into tree_path(anc,des,pc) values (1,3,1); insert into tree_path(anc,des,pc) values (4,5,1); insert into tree_path(anc,des,pc) values (4,6,1); insert into tree_path(anc,des,pc) values (4,7,1); insert into tree_path(anc,des,pc) values (4,8,0); insert into tree_path(anc,des,pc) values (4,9,0); insert into tree_path(anc,des,pc) values (4,10,0); insert into tree_path(anc,des,pc) values (7,8,1); insert into tree_path(anc,des,pc) values (7,9,1); insert into tree_path(anc,des,pc) values (7,10,0); insert into tree_path(anc,des,pc) values (9,10,1); 删除子树 假设要删除子树#7 delete from tree_path where tree_path.des in (select t.des from tree_path t where t.anc=7) 移动子树 假设我们要把子树#7从节点#4移动到节点#1 1.分离子树,删除子树节点与其祖先的关系 delete from tree_path where tree_path.anc=7 2.将上一步分离出的子树用笛卡尔积嫁接到#1下 select super.anc, sub.des, case when super.anc=1 and sub.des=7 then 1 else 0 end pc from tree_path super cross join tree_path sub where super.des=1 and sub.anc=7 查询树形结构 xxxxxx
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/4846.html
相关文章
- Navicat Premium 15 Mac(数据库开发工具)中文激活版
- H2 数据库的 expected "identifier 错误
- MySQL数据库原理学习(二)
- MariaDB数据库删除用户命令
- mysql 8.0 登录报错详解数据库
- jdbc调用存储过程详解数据库
- Mysql基础详解数据库
- phpredis函数使用方法详解数据库
- Oracle获得日期中的年份详解数据库
- 大型数据库设计原则详解数据库
- PLSQL怎样导出oracle表结构和数据详解数据库
- mysql主从同步报slave_sql_running:no的解决方案详解数据库
- Oracle 表结构、索引以及分区信息查询详解数据库
- Oracle逻辑结构学习笔记详解数据库
- JDBC连接MYSQL数据库示例详解编程语言
- Hibernate控制台显示创建数据库表语句详解编程语言
- 为什么要用hibernate 与基于数据库表结构的项目开发详解编程语言
- MySQL Status Innodb_rows_read 数据库状态作用意思及如何正确
- Oracle数据库技术:一张图片带你瞬间掌握(oracle图片)
- 探索MySQL数据库之旅:使用Idea驱动(ideamysql驱动)
- acle数据库比较MSSQL与Oracle数据库优劣(mssqlor)
- MySQL数据库的设计优化技巧(mysql设计优化)
- 变数据库表结构操作步骤详解(25字)(mysql改)
- 的MSSQL数据库的安全管理提升MSSQL数据库安全性的重要步骤(mssql 正在使用)
- cmd快捷登录oracle数据库的窍门(cmd下进入oracle)
- MYSQL数据库助力下载电影App,高效运转数据管理系统(MYSQL下载电影app)
- 深入SQLServer跨数据库查询的详解