Oracle connect by prior 处理树状表
Posted 丹心石
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Oracle connect by prior 处理树状表相关的知识,希望对你有一定的参考价值。
--基础篇
--1.建表
create table department(departmentid integer,departmentname varchar2(30),upperdepartmentid integer,manager varchar2(30));
--2.建序列
create sequence department_seq start with 1 nocycle;
--3.建触发器
create trigger tr_department_id before insert on department
for each row
begin
select department_seq.nextval into :new.departmentid from dual;
end;
--4.建存储过程
create or replace procedure pr_add_department(deptName in varchar2,upperDeptName in varchar2) as
pid integer;
begin
if nvl(upperDeptName,\'0\')=\'0\' then
pid:=0;
else
select departmentid into pid from department where departmentname=upperDeptName;
end if;
insert into department(departmentname,upperdepartmentid) values(deptName,pid);
commit;
end;
--5.查询
select * from department;
--6.更新
update department set departmentname=\'会计电算化\',upperdepartmentid=9 where departmentid=10;
--7.添加树状表树根
begin
pr_add_department(\'江西财经学院\',\'\');
end;
--8.添加分支
begin
--pr_add_department(\'信息系\',\'江西财经学院\');
pr_add_department(\'会计专业\',\'江西财经学院\');
end;
--9.查询树状表 level 分层级
select departmentid,departmentname,upperdepartmentid,level
from department
connect by prior departmentid=upperdepartmentid
start with upperdepartmentid=0
oracle中connect by prior的使用
作用
connect by主要用于父子,祖孙,上下级等层级关系的查询
语法
{ CONNECT BY [ NOCYCLE ] condition [AND condition]... [ START WITH condition ]
| START WITH condition CONNECT BY [ NOCYCLE ] condition [AND condition]...}
解释:
start with: 指定起始节点的条件
connect by: 指定父子行的条件关系
prior: 查询父行的限定符,格式: prior column1 = column2 or column1 = prior column2 and ... ,
nocycle: 若数据表中存在循环行,那么不添加此关键字会报错,添加关键字后,便不会报错,但循环的两行只会显示其中的第一条
循环行: 该行只有一个子行,而且子行又是该行的祖先行
connect_by_iscycle: 前置条件:在使用了nocycle之后才能使用此关键字,用于表示是否是循环行,0表示否,1 表示是
connect_by_isleaf: 是否是叶子节点,0表示否,1 表示是
level: level伪列,表示层级,值越小层级越高,level=1为层级最高节点
例子
自定义数据
-- 创建表 create table employee( emp_id number(18), lead_id number(18), emp_name varchar2(200), salary number(10,2), dept_no varchar2(8) ); -- 添加数据 insert into employee values(‘1‘,0,‘king‘,‘1000000.00‘,‘001‘); insert into employee values(‘2‘,1,‘jack‘,‘50500.00‘,‘002‘); insert into employee values(‘3‘,1,‘arise‘,‘60000.00‘,‘003‘); insert into employee values(‘4‘,2,‘scott‘,‘30000.00‘,‘002‘); insert into employee values(‘5‘,2,‘tiger‘,‘25000.00‘,‘002‘); insert into employee values(‘6‘,3,‘wudde‘,‘23000.00‘,‘003‘); insert into employee values(‘7‘,3,‘joker‘,‘21000.00‘,‘003‘);commit;
查询以emp_id为0开始的节点的所有直属节点
select emp_id,lead_id,emp_name,prior emp_name as lead_name,salary from employee start with lead_id=0 connect by prior emp_id = lead_id -- 等同于 select emp_id,lead_id,emp_name,prior emp_name as lead_name,salary from employee start with emp_id=1 connect by prior emp_id = lead_id
查询以emp_id为6开始的节点
select emp_id,lead_id,emp_name,salary from employee start with emp_id=6 connect by prior lead_id=emp_id;
level伪列的使用,格式化层级
select lpad(‘ ‘,level*2,‘ ‘)||emp_name as name,emp_id,lead_id,salary,level from employee start with lead_id=0 connect by prior emp_id=lead_id
connect_by_root 查找根节点
select connect_by_root emp_name,emp_name,lead_id,salary from employee where dept_no=‘002‘ start with lead_id=1 connect by prior emp_id = lead_id;
标注循环行
-- 插入一条数据,与另一条emp_id=7的数据组成循环行 insert into employee values(‘3‘,7,‘joker_cycle‘,‘21000.00‘,‘003‘); commit; -- connect_by_iscycle("CYCLE"), connect by nocycle select emp_id,emp_name,lead_id,salary,connect_by_iscycle as cycle from employee start with lead_id=0 connect by nocycle prior emp_id = lead_id;
connect_by_isleaf 是否是叶子节点
select emp_id,emp_name,lead_id,salary,connect_by_isleaf from employee start with lead_id=0 connect by nocycle prior emp_id=lead_id;
以上是关于Oracle connect by prior 处理树状表的主要内容,如果未能解决你的问题,请参考以下文章
ORACLE:start with ... connect by prior