SQL statements in the Oracle Tree Structure

Source: Internet
Author: User

Oracle provides a tree structure for hierarchical queries:

Start with: specifies that the record is a condition for the root node. You can have multiple root nodes.

Connect by: Specifies the condition for the relationship between the parent node and the child node of the record. Find the condition of the parent node containing the child node

Prior: Specifies the reference of the parent node record.

Siblings: Specify the sort of the tree. The sequence of sibling nodes under the same parent node.

The storage structure of the tree in the database table is as follows:

 
Create Table tree (ID number not null, // primary key PID Number not null, // parent node ID order_id number not null, // sorting ID node_name varchar2 (100) not null, // node name del varchar2 (5), // Delete Id 1: Delete constraint pk_tree primary key (ID ));

The instance SQL statement is as follows:

Select * from tree where del <> 1 start with PID = 0 and del <> 1 connect by prior id = PID and del <> 1 order siblings by order_id

The query result is a set of records sorted by tree.

If logical deletion (false deletion) is used, del <> 1 in the preceding SQL statement has different meanings. The first step is to filter all records in the table that are not marked to be deleted (shorten the calculation range of the Spanning Tree ), the second is to filter the records of the root node that are not marked to be deleted, and the third is to filter records of the parent node that contains the child node that is not marked to be deleted.

 

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.