Master-slave tree

Summary
Create the two tables MASTER and SLAVE. Every "master" has one or more "slaves". To see the whole hierarchy in a tree view follow the example.
create table master (master_id number(3), 
master varchar2(100));

insert into master values (1, 'master01');
insert into master values (2, 'master02');
insert into master values (3, 'master03');
insert into master values (4, 'master04');
commit;

create table slave (slave_id number(3), 
slave varchar2(100), master_id number(3));

insert into slave values (1, 'slave01', 1);
insert into slave values (2, 'slave02', 2);
insert into slave values (3, 'slave03', 1);
insert into slave values (4, 'slave04', 3);
insert into slave values (5, 'slave05', 1);
insert into slave values (6, 'slave06', 4);
insert into slave values (7, 'slave07', 4);
commit;
The tree view
select decode(slave, 'M', master, LPAD(' --->',5) || slave) 
from 
(select master.master, slave.slave from 
master, slave 
where master.master_id = slave.master_id
union 
select distinct(master) master, 'M' 
from master order by master asc);
Reviews
Filters
Search