Use of merge in Oracle DML statements

Source: Internet
Author: User
Tags commit one table

Copying data from one table to another, inserting new data, or replacing old data is a problem that every Oracle DBA will frequently encounter.

In the years before the oracle9i, we have to find out whether there is old data, if useful update replacement, or INSERT statement inserted, there are some tag variables and so on, cumbersome.

Now that oracle9i specifically provides the merge statement for this situation, making this work exceptionally easy, Oracle9i introduces the merge command, where you can perform inserts and updates operations on a table in one SQL statement. The merge command selects rows from one or more data sources to updating or inserting to one or more tables.

Oracle 10g Merge has the following improvements:

1. An UPDATE or INSERT clause is optional

2, UPDATE and INSERT clauses can be added to the WHERE clause

3. On conditions use constant filter verbs to insert all rows into the target table without the need to connect the source table and the target table

4, after the update clause can be followed by the DELETE clause to remove some unwanted rows

First create the sample table:

CREATE TABLE Products

(

product_id INTEGER,

Product_Name VARCHAR2 (60),

CATEGORY VARCHAR2 (60)

);

Insert into the products values (1501, ' VIVITAR 35MM ', ' Electrncs ');

Insert into the products values (1502, ' OLYMPUS IS50 ', ' Electrncs ');

Insert into the products of values (1600, ' Play GYM ', ' TOYS ');

Insert into the products values (1601, ' Lamaze ', ' TOYS ');

Insert into the products values (1666, ' HARRY POTTER ', ' DVD ');

Commit

CREATE TABLE NewProducts

(

product_id INTEGER,

Product_Name VARCHAR2 (60),

CATEGORY VARCHAR2 (60)

);

INSERT into newproducts values (1502, ' OLYMPUS CAMERA ', ' Electrncs ');

INSERT into newproducts values (1601, ' Lamaze ', ' TOYS ');

INSERT into newproducts values (1666, ' HARRY POTTER ', ' TOYS ');

INSERT into newproducts values (1700, ' Wait-INTERFACE ', ' books ');

Commit

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.