Structured Query of the Oracle Handbook Series (1)

Source: Internet
Author: User

1) Prepare test data

If you have less time to talk, go straight to the subject. Create a simple employee table t_hierarchical:

  • Emp employee ID
  • Direct superior of Mgr employees (Mgr itself is also an employee)
  • Emp_name employee name

Insert some test data. Except for the big boss AA, other employees have their own managers.

 
 
  1. select emp, mgr, emp_name from t_hierarchical t;
 
 
  1. 1            AA 
  2. 2     1     BB 
  3. 3     2     CC 
  4. 4     3     DD 
  5. 5     2     EE 
  6. 6     3     FF 
2) CONNECT
 
 
  1. select emp, mgr, LEVEL from t_hierarchical t 
  2. CONNECT BY PRIOR emp=mgr 
  3. order by emp; 
  4.  
  5. 1           1 
  6. 2     1     2 
  7. 2     1     1 
  8. 3     2     1 
  9. 3     2     3 
  10. 3     2     2 
  11. 4     3     4 
  12. 4     3     1 
  13. 4     3     2 
  14. 4     3     3 
  15. 5     2     3 
  16. 5     2     2 
  17. 5     2     1 
  18. 6     3     2 
  19. 6     3     3 
  20. 6     3     4 
  21. 6     3     1 

To explain, connect by is used to specify the relationship between the parent and sub-records (PRIOR is explained more intuitively in the following example ). Take emp 2 as an example. It belongs to emp 1. If we use emp 1 as the root node, apparently LEVEL = 2. If we use emp 2 as the root node, LEVEL = 1, this is why the two rows of records in the co-colored logo section appear in the above query results, and so on.

3) START

Generally, we need more intuitive and practical results. This requires the start with clause in the structured query to specify the root node:

 
 
  1. select emp, mgr, LEVEL from t_hierarchical t 
  2. START WITH emp=1 
  3. CONNECT BY PRIOR emp=mgr; 
  4.  
  5. 1           1 
  6. 2     1     2 
  7. 3     2     3 
  8. 4     3     4 
  9. 6     3     4 
  10. 5     2     3 

Here we specify the root node as emp 1. This result is intuitive. For example, if emp 1 is the root node, emp 3 is in the third level (emp 1-emp 2-emp 3 ), here, I will add a description of the PRIOR keyword. My personal opinion: "PRIOR emp = mgr" indicates the emp number of the previous record = The mgr Number of the current record, which can be seen from the query results. In addition, obvious recursive traces can be found in the query results. For details, see numbers with different colors.

4) SYS_CONNECT_BY_PATH ()

I have to introduce very niubo's SYS_CONNECT_BY_PATH () function. We can get the hierarchy or tree structure path, as shown below:

 
 
  1. select emp, mgr, LEVEL, SYS_CONNECT_BY_PATH(emp,'/') path from t_hierarchical t 
  2. START WITH emp=1 
  3. CONNECT BY PRIOR emp=mgr; 
  4.  
  5. 1            1     /1 
  6. 2     1     2     /1/2 
  7. 3     2     3     /1/2/3 
  8. 4     3     4     /1/2/3/4 
  9. 6     3     4     /1/2/3/6 
  10. 5     2     3     /1/2/5 
5) CONNECT_BY_ISLEAF

CONNECT_BY_ISLEAF is a very useful virtual column. What is LEAF (LEAF), that is, no node belongs to this node:

 
 
  1. select emp, mgr, LEVEL, SYS_CONNECT_BY_PATH(emp,'/') path from t_hierarchical t 
  2. where CONNECT_BY_ISLEAF=1 
  3. START WITH emp=1 
  4. CONNECT BY PRIOR emp=mgr; 
  5.  
  6. 4     3     4     /1/2/3/4 
  7. 6     3     4     /1/2/3/6 
  8. 5     2     3     /1/2/5 


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.