Merge Into use

Oracle update syntax:
1. The general syntax
   update tab set col = .... [where ...] = can have subqueries, but must return a unique row for each column of the tab, where is the table that needs to be updated , part of the update must be added, otherwise the update of the correlated subquery will update the unmatched update to null, such as
  update tab a set a.col=(select b.col from b where a.id=b.id) where exists (select 1 from b where a.id=b.id) was written several times similarly

2. Improved syntax merge
  merge into tab
  using (table|view|subquery, etc.) --subquery needs to be parenthesized on (condition)
  when match then
   do update
  when no match then
  do insert                               

insert syntax is different from update syntax, please refer to the documentation for details, 10g also supports update, insert conditional update and insert, support update delete where, support only update or insert
can not be modified in using Similarly, each row must have a unique corresponding column.

If the above two grammars cannot find a unique corresponding one, the statement needs to be improved, such as adding rownum=1

3. Usage of update inline view
   update(select ...........Associated query) set target=source
  as update(select a.name,b.name from a,b where a.id=b.id) set a.name= b.name;
      requires unique construction to ensure unique correspondence. For example, the above must have a unique key for b.id, that is, a preserved key. For example, a unique index can be used. Before 11g, you can use hint: bypass_ujvc, so that a unique key is not required. But there may be problems, one-to-many will be updated many times, 11g this hint is invalid
  delete (select ....) is also possible, there are many requirements, you can see the sql document, insert (select ...) has more restrictions
  on the third type The method comes from an updatable view


. There are basically three sql writing methods for oracle update. The latter two are often used in optimization. Especially in the first type of update associative subquery, the source table does not use the index, so there are many updates, which is equivalent to nested loop. , definitely slow, and there is a where filter, multiple access to the source table 

--------------------------------- -------------------------------------------------- -------------------------------------------------- -----------------------------------------
 

The MERGE statement is a new syntax added to Oracle9i for merging UPDATE and INSERT statements. Through the MERGE statement, query another table according to the join condition of a table or subquery, UPDATE if the join condition matches, and execute INSERT if the join condition matches. This syntax only needs one full table scan to complete all the work, and the execution efficiency is higher than INSERT+UPDATE.

  */

  /*grammar:

  MERGE [INTO [schema .] table [t_alias]

  USING [schema .] { table | view | subquery } [t_alias]

  ON ( condition )

  WHEN MATCHED THEN merge_update_clause

  WHEN NOT MATCHED THEN merge_insert_clause;

  */

  grammar:

  MERGE INTO [your table-name] [rename your table here]

  USING ( [write your query here] )[rename your query-sql and using just like a table]

  ON ([conditional expression here] AND [...]...)

  WHEN MATHED THEN [here you can execute some update sql or something else ]

  WHEN NOT MATHED THEN [execute something else here ! ]

  /*

  Let's take the table in "Case Application in SQL" as an example. After creating another two tables fzq1 and fzq2

  */

  --All boys records

  create table fzq1 as select * from fzq where sex=1;

  --All girls records

  create table fzq2 as select * from fzq where sex=0;

  /* An example involving two table associations */

  --Update table fzq1 so that the chengji field in the records with the same id +1, and update the name field.

  -- If the ids are not the same, insert into table fzq1.

  --Add +1 to the grades recorded by boys in the fzq1 table, and insert girls into the table fzq1

  merge into fzq1 aa --fzq1 table is the table that needs to be updated

  using fzq bb -- association table

  on (aa.id=bb.id) --association condition

  when matched then -- match the associated conditions for update processing

  update set

  aa.chengji=bb.chengji+1,

  aa.name=bb.name -- This is just to show that multiple fields can be updated at the same time.

  when not matched then -- does not match the associated conditions, for insertion processing. If only for update, the following statement can be omitted.

  insert values( bb.id, bb.name, bb.sex,bb.kecheng,bb.chengji);

  --You can query the fzq1 table by yourself.

  /*Involving an example of association of multiple tables, we take three tables as an example, only for update processing, not for insertion processing. Of course, you can also only do insert processing */

  --Add 1 to the scores recorded by girls in the fzq1 table, without going directly to the sex field. Rather, fzq and fzq2 are associated.

  merge into fzq1 aa --fzq1 table is the table that needs to be updated

  using (select fzq.id,fzq.chengji

  from fzq join fzq2

  on fzq.id=fzq2.id) bb -- dataset

  on (aa.id=bb.id) --association condition

  when matched then -- match the associated conditions for update processing

  update set

  aa.chengji=bb.chengji+1

  --You can query the fzq1 table by yourself.

  /* Can't do things */

  merge into fzq1 aa

  using fzq bb

  on (aa.id=bb.id)

  when matched then

  update set

  aa.id=bb.id+1

  /*system hint:

  ORA-38104: Columns referenced in the ON Clause cannot be updated: "AA"."ID"

  We can't update fields in on (aa.id=bb.id) association condition */

  update fzq1

  set id=(select id+1 from fzq where fzq.id=fzq1.id)

  where id in

  (select id from fzq)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325944594&siteId=291194637