In Oracle, "merge into" is used to update data in a table. You can insert data from one table into another table. If the data is already in the inserted table, the data will be updated. If the data does not exist, new data will be added to the inserted table.
The operating environment of this tutorial: Windows 10 system, Oracle 11g version, Dell G3 computer.
What is the usage of merge into in Oracle
Oracle9g introduced the MERGE command. You can perform inserts and update operations on a table at the same time in one SQL statement. The MERGE command selects from one or more Select rows in the data source to update or insert into one or more tables. In Oracle 10g, MERGE has the following improvements:
1, UPDATE or INSERT clause is optional
2 , UPDATE and INSERT clauses can be added with WHERE clause
3. Use constant filter predicate in ON condition to insert all rows into the target table, without connecting the source table and target table
4. The UPDATE clause can be followed by a DELETE clause to remove some unnecessary rows
5. The source table is the table followed by the using keyword, and the target table is the table that will be merged into
6. All updates, inserts, and deletes in merge into are performed on the target table. Since the merge into operation table has been formulated, update, insert, and delete do not need to display the table name
7. In short, the function of merge into is to solve the problem of using table B and new table A data. If If it does not exist in table A, insert the data from table B into table A or insert data into a table. If the table already has the data, it will be updated. Otherwise, new data will be added.
Syntax:
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 ! ]
Next, let’s test it directly:
Requirement 1: Insert a piece of data into a table. If the data already exists in the table, update it, otherwise create a new one. Add
First create a table TEST_ONE
create table TEST_ONE( ID NUMBER not null primary key, NAME VARCHAR2(255), IP VARCHAR2(255), MEMO VARCHAR2(255))commit;
Add a few pieces of data as test data
INSERT INTO TEST_ONE (ID, NAME, IP, MEMO) VALUES (1, '2', '3', '周文军'); INSERT INTO TEST_ONE (ID, NAME, IP, MEMO) VALUES (2, '66', '366', '2656'); INSERT INTO TEST_ONE (ID, NAME, IP, MEMO) VALUES (3, '5656', '626', '2626'); INSERT INTO TEST_ONE (ID, NAME, IP, MEMO) VALUES (4, '5656', '2626', '626');
Okay, our data table has been built, as shown below:
If we need to add a new piece of data, we usually do it like this
INSERT INTO TEST_ONE (ID, NAME, IP, MEMO) VALUES (5, 'mrhu', '127.0.0.1.0', '王先生的IP');
But we hope to use the ID to judge first. If the data is not added and the data is updated, how to achieve it?
Then the merge into command comes, just type the code:
merge into TEST_ONE mtb using (select '5' as id, 'mrhu' as name,'127.0.0.1.0' as ip,'王先生的IP' as memo from dual)mmb on (mtb.id = mmb.id)when matched THENupdate set mtb.name = mmb.name,mtb.ip = mmb.ip,mtb.memo=mmb.memo when not matched theninsert (mtb.id, mtb.name,mtb.ip,mtb.memo) VALUES(mmb.id,mmb.name,mmb.ip,mmb.memo);
Run as follows:
Let’s take a look at the data in the table:
Data added successfully!
So how do we test updates? It's very simple. We change memo='Mr. Wang's IP' to memo='This beautiful girl's IP' for testing.
merge into TEST_ONE mtb using (select '5' as id, 'mrhu' as name,'127.0.0.1.0' as ip,'本大美女的IP' as memo from dual)mmb on (mtb.id = mmb.id)when matched THENupdate set mtb.name = mmb.name,mtb.ip = mmb.ip,mtb.memo=mmb.memo when not matched theninsert (mtb.id, mtb.name,mtb.ip,mtb.memo) VALUES(mmb.id,mmb.name,mmb.ip,mmb.memo);
Run as follows:
Let's take a look Data in the table:
The data has been updated successfully!
Requirement 2: Add the data in table A to table B, and it is required to judge by the primary key. If the data is included, update it, otherwise add new one
We create another table TEST_TWO As table B, TEST_ONE is used as table A
create table tes( ID NUMBER not null primary key, CODE VARCHAR2(255), MEMO VARCHAR2(255));commit;
Okay, table TEST_TWO is created, let’s add a piece of data first!
INSERT INTO ROOT.TEST_TWO (ID, CODE, MEMO) VALUES (5, 'mrhu', '隔壁老王的IP');
Let’s take a look at the data in TEST_TWO:
Let’s import the data in TEST_ONE into our newly created table. Through analysis, we found that the TEST_TWO table has There is a piece of data with ID 5. There is also a piece of data with ID 5 in TEST_ONE. The expected execution effect is that the memo field value of the data with TEST_TWOID 5 will be updated to 'the IP of this beautiful woman' in TEST_ONE, and other values will be added. operate.
Let’s write code to verify:
merge into TEST_TWO mtb using (select id,name,ip,memo from TEST_ONE) mmb on (mtb.id = mmb.id)when matched THENUPDATE set mtb.code = mmb.name,mtb.memo = mmb.memo when not matched THENinsert (mtb.id,mtb.code,mtb.memo) values (mmb.id,mmb.name,mmb.memo);
Let’s take a look at the effect:
The execution result is consistent with the expected result. Okay, how to use merge into? Have you learned it? If you like it, please follow and support it!
Recommended tutorial: "Oracle Video Tutorial"
The above is the detailed content of What is the usage of merge into in oracle. For more information, please follow other related articles on the PHP Chinese website!

方法:1、利用“select*from user_indexes where table_name=表名”语句查询表中索引;2、利用“select*from all_indexes where table_name=表名”语句查询所有索引。

oracle asm指的是“自动存储管理”,是一种卷管理器,可自动管理磁盘组并提供有效的数据冗余功能;它是做为单独的Oracle实例实施和部署。asm的优势:1、配置简单、可最大化推动数据库合并的存储资源利用;2、支持BIGFILE文件等。

在oracle中,可以利用“TO_SINGLE_BYTE(String)”将全角转换为半角;“TO_SINGLE_BYTE”函数可以将参数中所有多字节字符都替换为等价的单字节字符,只有当数据库字符集同时包含多字节和单字节字符的时候有效。

在Oracle中,可利用lsnrctl命令查询端口号,该命令是Oracle的监听命令;在启动、关闭或重启oracle监听器之前可使用该命令检查oracle监听器的状态,语法为“lsnrctl status”,结果PORT后的内容就是端口号。

在oracle中,可以利用“select ... From all_tab_columns where table_name=upper('表名') AND owner=upper('数据库登录用户名');”语句查询数据库表的数据类型。

在oracle中,可以利用“drop sequence sequence名”来删除sequence;sequence是自动增加数字序列的意思,也就是序列号,序列号自动增加不能重置,因此需要利用drop sequence语句来删除序列。

方法:1、利用“LOWER(字段值)”将字段转为小写,或者利用“UPPER(字段值)”将字段转为大写;2、利用“REGEXP_LIKE(字符串,正则表达式,'i')”,当参数设置为“i”时,说明进行匹配不区分大小写。

方法:1、利用“alter system set sessions=修改后的数值 scope=spfile”语句修改session参数;2、修改参数之后利用“shutdown immediate – startup”语句重启服务器即可生效。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version
Chinese version, very easy to use

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.
