Additions and deletions to change search oracle achieve sector

Always wanted to add or delete a department change search function, and finally found the additions and deletions to change search oracle achieve sector is very simple.

1, create a data table

A simple hierarchy of data tables field

id department ID

name department name

hierarchical levels (layers current sector belongs to the first sector: for example, a sector, two sectors, three sectors, etc.)

id parent_id immediate parent sector (example: two sectors: an information unit belongs Department: Company)

After the department table is created as shown:

 

2. After creating the table, will introduce a unique oracle query: hierarchical queries. Level is used to query processing department of additions and deletions to change search problem.

Do: directly attached to sql

select id, 
       levels,
       name ,
       parent_id
  from test       
 start with id = 1   
connect by prior id = parent_id

The above query is to query id is 1 (i.e., a company) all of the following sub-nodes, the query results are shown below.

 It can then be displayed at the front end. The picture shows the front end as shown in the results.

 

So how do indented according to the hierarchy? This time on the field levels play a role, indent to the corresponding results based on the size of the hierarchy. Specific code is not posted here.

Reference: https: //zhuanlan.zhihu.com/p/58563707 

 

2, finished the query, then say added, the new very simple.

The following statement can be used directly.

ID Number: not to say, there is no increment field in the oracle, so use a sequence of functions, specifically how to use, can not say here.

Level: for example, say value is 2 if you want to add a new division in the company (level 1), then the level of the new department. levels = levels + 1

Name: do not say

Parent ID: For example, to add a department in the company (id 1), then the parent of the new department ID to 1

INSERT  INTO fifo_test (ID, the LEVELS, NAME, PARENT_ID) values ( ' ID number ' , ' level ' , ' name ' , ' parent ID ' )

 

3, finished insert is then modified, this basically does not need to say, directly on the line following sql

 Update the FIFO the SET name = ' need to modify the department name '  the WHERE id = ' department id number '

 

4, modify the finish, the last is deleted, if said: "ah delete is also simple, direct delete this information based on the id departments on the line.". The idea is wrong, because you just delete the information of the current sector only, and does not delete the information sub-sector, the end result will lead to a lot of data this table appear useless.

The correct approach is shown in the following code:

delete test where id in(  
    select id from test  
      start with id=5  
      connect by prior id=parent_id )

Recall that just when the query is not also appear inside the sub-queries. The code above sub-query, the query time is used in the statement. This query means that the query information of child nodes of the current node. Therefore, as shown in the above statement, in use straight out of the statement will be able to delete the information of the current node and its child nodes. You will be able to achieve the correct function of the deleted.

Reference: https: //abcdedf.iteye.com/blog/1100873 

Guess you like

Origin www.cnblogs.com/masha2017/p/11240110.html