⌘BABAKOV.NET Let's code!
100% [light] [ru]

Working with Trees in Oracle (PL/SQL)

Working with Trees in Oracle (PL/SQL) is explained in this publication. It describes how to work with specific methods that are present in the Oracle database, for working with Trees. So, let's get started...


--Creating a table for experimenting with trees.
create table my_tree (
  id number,
  id_parent number,
  nm varchar2(100)
  );



--Adding an index to our test table (for order).
create index idx_my_tree on my_tree(id,id_parent);


--Populating our test table with a classifier (product categories).
insert into MY_TREE values (1,null, \'Продукты\');
insert into MY_TREE values (2,1, \'Колбасы\');
insert into MY_TREE values (3,1, \'Полуфабоикаты\');
insert into MY_TREE values (4,1, \'Фрукты\');
insert into MY_TREE values (5,3, \'Котлеты\');
insert into MY_TREE values (6,3, \'Пельмени\');
insert into MY_TREE values (7,3, \'Манты\');
insert into MY_TREE values (8,3, \'Бифштексы\');
insert into MY_TREE values (9,2, \'Копченая\');
insert into MY_TREE values (10,2, \'Вареная\');
insert into MY_TREE values (11,4, \'Помидоры\');
insert into MY_TREE values (12,4, \'Огурцы\');
insert into MY_TREE values (13,4, \'Бананы\');
insert into MY_TREE values (14,2, \'Салями\');


--Looking at what we have in the table.
select * from MY_TREE;


--This is what we get:
"ID" "ID_PARENT" "NM"
1 "Продукты"
2 1 "Колбасы"
3 1 "Полуфабоикаты"
4 1 "Фрукты"
5 3 "Котлеты"
6 3 "Пельмени"
7 3 "Манты"
8 3 "Бифштексы"
9 2 "Копченая"
10 2 "Вареная"
11 4 "Помидоры"
12 4 "Огурцы"
13 4 "Бананы"
14 2 "Салями"


--In this form, it is not very convenient to work with the data, but Oracle has a helpful way CONNECT BY, which allows creating a hierarchy with one query to build all possible trees:
select id, id_parent, nm from my_tree connect by prior id = id_parent;


--This is the result we get:
"ID" "ID_PARENT" "NM"
2 1 "Колбасы"
9 2 "Копченая"
14 2 "Салями"
10 2 "Вареная"
4 1 "Фрукты"
11 4 "Помидоры"
13 4 "Бананы"
12 4 "Огурцы"
3 1 "Полуфабоикаты"
5 3 "Котлеты"
8 3 "Бифштексы"
7 3 "Манты"
6 3 "Пельмени"
9 2 "Копченая"
14 2 "Салями"
10 2 "Вареная"
5 3 "Котлеты"
8 3 "Бифштексы"
7 3 "Манты"
6 3 "Пельмени"
11 4 "Помидоры"
13 4 "Бананы"
12 4 "Огурцы"
1 "Продукты"
2 1 "Колбасы"
9 2 "Копченая"
14 2 "Салями"
10 2 "Вареная"
4 1 "Фрукты"
11 4 "Помидоры"
13 4 "Бананы"
12 4 "Огурцы"
3 1 "Полуфабоикаты"
5 3 "Котлеты"
8 3 "Бифштексы"
7 3 "Манты"
6 3 "Пельмени"


--Adding the START WITH clause, we get a more convenient view for working with.
select id, id_parent, nm from my_tree connect by prior id = id_parent start with id in 1;


--Now the result looks much more understandable and convenient:
"ID" "ID_PARENT" "NM"
1 "Продукты"
2 1 "Колбасы"
9 2 "Копченая"
10 2 "Вареная"
14 2 "Салями"
3 1 "Полуфабоикаты"
5 3 "Котлеты"
6 3 "Пельмени"
7 3 "Манты"
8 3 "Бифштексы"
4 1 "Фрукты"
11 4 "Помидоры"
12 4 "Огурцы"
13 4 "Бананы"


--Adding a pseudocolumn LEVEL:
select id, id_parent, nm, level from my_tree connect by prior id = id_parent start with id in 1;


--And now we can see which level each record belongs to:
"ID" "ID_PARENT" "NM" "LEVEL"
1 "Продукты" 1
2 1 "Колбасы" 2
9 2 "Копченая" 3
10 2 "Вареная" 3
14 2 "Салями" 3
3 1 "Полуфабоикаты" 2
5 3 "Котлеты" 3
6 3 "Пельмени" 3
7 3 "Манты" 3
8 3 "Бифштексы" 3
4 1 "Фрукты" 2
11 4 "Помидоры" 3
12 4 "Огурцы" 3
13 4 "Бананы" 3


--Adding a tree sort by name: (ordinary order by won't help here, do it like this)
select id, id_parent, nm, level from my_tree connect by prior id = id_parent start with id in 1 order siblings by nm;


--And now inside groups are sorted by name:
"ID" "ID_PARENT" "NM" "LEVEL"
1 "Продукты" 1
2 1 "Колбасы" 2
10 2 "Вареная" 3
9 2 "Копченая" 3
14 2 "Салями" 3
3 1 "Полуфабоикаты" 2
8 3 "Бифштексы" 3
5 3 "Котлеты" 3
7 3 "Манты" 3
6 3 "Пельмени" 3
4 1 "Фрукты" 2
13 4 "Бананы" 3
12 4 "Огурцы" 3
11 4 "Помидоры" 3


--Another very interesting function SYS_CONNECT_BY_PATH(имя_поля, \'разделитель\'), which allows displaying the name in the form of a path (like in Explorer).
select id, id_parent, SYS_CONNECT_BY_PATH(nm, \'>\') as path_nm, level from my_tree connect by prior id = id_parent start with id in 1 order siblings by nm;


--This is how it looks:
"ID" "ID_PARENT" "PATH_NM" "LEVEL"
1 ">Продукты" 1
2 1 ">Продукты>Колбасы" 2
10 2 ">Продукты>Колбасы>Вареная" 3
9 2 ">Продукты>Колбасы>Копченая" 3
14 2 ">Продукты>Колбасы>Салями" 3
3 1 ">Продукты>Полуфабоикаты" 2
8 3 ">Продукты>Полуфабоикаты>Бифштексы" 3
5 3 ">Продукты>Полуфабоикаты>Котлеты" 3
7 3 ">Продукты>Полуфабоикаты>Манты" 3
6 3 ">Продукты>Полуфабоикаты>Пельмени" 3
4 1 ">Продукты>Фрукты" 2
13 4 ">Продукты>Фрукты>Бананы" 3
12 4 ">Продукты>Фрукты>Огурцы" 3
11 4 ">Продукты>Фрукты>Помидоры" 3


--Another interesting function CONNECT_BY_ISLEAF, checks if there are children (if there are no children, then 1, if there are, then 0).
select id, id_parent, SYS_CONNECT_BY_PATH(nm, \'>\') as path_nm, level, CONNECT_BY_ISLEAF as leaf from my_tree connect by prior id = id_parent start with id in 1 order siblings by nm;


-- This is how it looks:
"ID" "ID_PARENT" "PATH_NM" "LEVEL" "LEAF"
1 ">Продукты" 1 0
2 1 ">Продукты>Колбасы" 2 0
10 2 ">Продукты>Колбасы>Вареная" 3 1
9 2 ">Продукты>Колбасы>Копченая" 3 1
14 2 ">Продукты>Колбасы>Салями" 3 1
3 1 ">Продукты>Полуфабоикаты" 2 0
8 3 ">Продукты>Полуфабоикаты>Бифштексы" 3 1
5 3 ">Продукты>Полуфабоикаты>Котлеты" 3 1
7 3 ">Продукты>Полуфабоикаты>Манты" 3 1
6 3 ">Продукты>Полуфабоикаты>Пельмени" 3 1
4 1 ">Продукты>Фрукты" 2 0
13 4 ">Продукты>Фрукты>Бананы" 3 1
12 4 ">Продукты>Фрукты>Огурцы" 3 1
11 4 ">Продукты>Фрукты>Помидоры" 3 1


--Now let's make a loop, and let's see how to avoid the error that this mess creates :)
update my_tree set id_parent = 2 where id = 2;


--Trying to make a selection starting from the second element:
select id, id_parent, SYS_CONNECT_BY_PATH(nm, \'>\') as path_nm, level, CONNECT_BY_ISLEAF as leaf from my_tree connect by prior id = id_parent start with id in 2 order siblings by nm;

--Getting an error like this:
ORA-01436: CONNECT BY loop in user data


--This can be avoided with the NOCYCLE command like this:
select id, id_parent, SYS_CONNECT_BY_PATH(nm, \'>\') as path_nm, level, CONNECT_BY_ISLEAF as leaf from my_tree connect by nocycle prior id = id_parent start with id in 2 order siblings by nm;


--And it will look like this:
"ID" "ID_PARENT" "PATH_NM" "LEVEL" "LEAF"
2 2 ">Колбасы" 1 0
10 2 ">Колбасы>Вареная" 2 1
9 2 ">Колбасы>Копченая" 2 1
14 2 ">Колбасы>Салями" 2 1
Well, that's it, in my opinion, everything necessary for working with trees in Oracle.

Comments (0)

No comments yet.

Leave a comment
Comment
Images (up to 5 MB)
Your comment will appear after moderation.