Skip to main content

Oracle Connect By Clause to Perform Hierarchical Queries.

 If we want to perform hierarchical queries we need to use connect by clause in our sql query.

Hierarchical query is any query wherin you want to display parent child relationship with the case that a parent may have multiple children and those have their own creating a tree like structure.

One classic scenario highlighted on almost all sites is employee table wherein employee's manager is stored in the employee row the manager id is again one of the employee in the employee table.

But we are not going to take it as you can reference it almost on any site that hosts oracle based content.
We are going to do a query based on DBA_DEPEDENCIES or ALL_DEPENDANCIES if you do not have access to former.

In DBA_DEPENDANCY table we have hierarchy of REFERENCED_NAME(parent) and the referring objects in column NAME(child ) that have dependency on the parent.

with this in mind we would need to set our connect by clause as prior NAME= REFERENCED_NAME
This confirms that REFRENCED_NAME is the parent and we are going to base this hierarchy on this relation.

Next addendum to connect by clause we are going to make is nocycle attribute. In Oracle there is a possibility of cyclical dependency in this case we would not have clear tree. Oracle throws an error when there is such cyclical data in your query. If you know this is the possibility its better to use nocycle clause. 

So here's the code that would print all the dependencies of table 'ORDER_HEADER_TABLE'.
select CONNECT_BY_ROOT REFERENCED_NAME  ||   sys_connect_by_path( name , '->' ),
d.* from dba_dependencies d
where owner = 'HR' 
connect by nocycle prior NAME= REFERENCED_NAME 
start with REFERENCED_NAME = 'ORDER_HEADER_TABLE' 
order by 1
Demo of DBA_DEPENDENCIES HIERARCHY
Demo of DBA_DEPENDENCIES HIERARCHY


SYS_CONNECT_BY_PATH gives you bread crumb like navigation from root node (in this case ORDER_HEADER_TABLE) to its child leaf nodes or intermediate nodes. So basically you have printed the hierarchy and path tracing from parent to child nodes. Hope you found this useful.


Comments

Popular posts from this blog

Use the Microsoft Visual Studio Code(VScode) with Java Maven project.

The VS Code is the go to editor now a days for the latest technologies and scripting options. VS Code as we all know works seamlessly with Python and JavaScripts, and offcourse the .NET family. But VS Code has many extensions which makes it big Java IDEs run for their money. Lets explore what we could do with VSCode if we are Java developer. Install VScode , no brainer. Jus type download Visual Studio code in google. Alrright once we have VSCode. Open it. Install Maven and Java extension. 2. Go to files and a small window will now appear on left side navigation bar. 3. Select New Maven project. 4. Select the archtype from dropdown. 5. As usual input the grpid, artifactid etc. 6. Done. Go through a video which has much detailed navigation flow. Till next time !!!

Oracle Analytical Functions : Tutorial Part 2 Covers sum avg lag lead.

In this tutorial video we have demonstrated how to use oracle analytical functions like lag , lead , sum and avg etc. Example 1. Use of oracle function sum to display running totals with the use of unbounded preceding. select sum(Salary) over (order by salary rows unbounded preceding) running_total , salary , e.* from employees e Identify gap in the contiguous sequences with the lag function. Ex. In Employees table we have contiguous sequence of employees but due to some anomaly we found that there is now gap in the sequences. Ex Employee Id 210 comes after 206 which is not contiguous. We can write a query as given below to identify such sequence gaps. Oracle Lag Function select * from ( select lag (e.employee_id , 1) over (order by e.employee_id) as prev_emp, e.* from employees e ) tmp where (tmp.employee_id - tmp.prev_emp) > 1

Cyclic blocking session removal script written by me for Oracle Database.

Plenty of times we have cyclic database blocking sessions. We have the script which runs in loop and kills them , only to see new blocking sessions have resurfaced. This is a classic scenario that happens many time due to poor application design. A was blocking B , B was blocking C. Unless you have some automated blocking session clearing script. You would run a script at point in time killing session of A. Giving the lock ownership to B.   B could realize it late that he has the lock and not commit his work. So DBA again goes in and sees block kills B's session. In meantime Frustrated A logs in and start his activity again queuing him up in wait for lock retrieval . This goes on and you end up running the script 5-10 times to kill these sessions to finally clear the blocks. Note this is not a deadlock which is normally apprehended and identified by oracle. In such case we had written below plsql to clear the sessions. This basically goes in and check for locks ever...