Home > database >  Research about the merge into the point
Research about the merge into the point

Time:10-06

Use the merge into statements recently discovered, update, delete, there are some subtle relationship between the conclusion and test process is listed as follows:
The test environment: oracle11g
Conclusions: in the merge into, when the update in the where clause to clause, only satisfy both on and the update of the where condition, will perform the delete clause,
 
- create a test table and insert the data
The CREATE TABLE t1 (
"ID" NUMBER (10) NOT NULL,
"NAME" VARCHAR2 (255 BYTE), NULL,
"AGE" NUMBER (10) NULL,
"SE" VARCHAR2 (255 - BYTE) NULL
)
;
INSERT INTO t1 VALUES (' 1201 ', 'Peter', '23' and 'male');
INSERT INTO t1 VALUES (' 6695 ', 'Lu', '12', 'getting');
INSERT INTO t1 VALUES (' 6720 ', 'Ka', '22', 'getting');

The CREATE TABLE t2 (
"ID" NUMBER NULL,
"NAME" VARCHAR2 (255 BYTE), NULL,
"AGE" NUMBER NULL,
"SE" VARCHAR2 (255 - BYTE) NULL
)
;
INSERT INTO t2 VALUES (' 1201 ', 'John', '47', 'male');
INSERT INTO t2 VALUES (' 1202 ', 'lili', '30', 'getting');


Results - 0
SQL> Select * from t1;

ID NAME AGE SE
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
1201 Peter 23 male
6695 Lu 12 female
6720 Ka 22 female
SQL> Select * from t2;

ID NAME AGE SE
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
1201 John 47 male
1202 lili 30 female


- test 1
The MERGE INTO t1 b
USING t2 e
ON (b.I D=e.I D)
The WHEN MATCHED THEN
The update
The set b.N AME=E.N AME,
B.A GE=E.A GE,
B.S E=E.S E
Where a t1. Name<> 'Peter'
Delete the where (t1) name='Peter')
WHEN NOT MATCHED THEN
Insert (b.I D, b.N AME, b.A GE, b.S E)
Values (E.I D, E.N AME, E.A GE, E.S E);

Results - 1
- create a test table and insert the data
The CREATE TABLE t1 (
"ID" NUMBER (10) NOT NULL,
"NAME" VARCHAR2 (255 BYTE), NULL,
"AGE" NUMBER (10) NULL,
"SE" VARCHAR2 (255 - BYTE) NULL
)
;
INSERT INTO t1 VALUES (' 1201 ', 'Peter', '23' and 'male');
INSERT INTO t1 VALUES (' 6695 ', 'Lu', '12', 'getting');
INSERT INTO t1 VALUES (' 6720 ', 'Ka', '22', 'getting');


The CREATE TABLE t2 (
"ID" NUMBER NULL,
"NAME" VARCHAR2 (255 BYTE), NULL,
"AGE" NUMBER NULL,
"SE" VARCHAR2 (255 - BYTE) NULL
)
;
INSERT INTO t2 VALUES (' 1201 ', 'John', '47', 'male');
INSERT INTO t2 VALUES (' 1202 ', 'lili', '30', 'getting');

- the results
SQL> Select * from t1;

ID NAME AGE SE
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
1201 Peter 23 male
6695 Lu 12 female
6720 Ka 22 female
SQL> Select * from t2;

ID NAME AGE SE
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
1201 John 47 male
1202 lili 30 female


- test 1
The MERGE INTO t1 b
USING t2 e
ON (b.I D=e.I D)
The WHEN MATCHED THEN
The update
The set b.N AME=E.N AME,
B.A GE=E.A GE,
B.S E=E.S E
Where a t1. Name<> 'Peter'
Delete the where (t1) name='Peter')
WHEN NOT MATCHED THEN
Insert (b.I D, b.N AME, b.A GE, b.S E)
Values (E.I D, E.N AME, E.A GE, E.S E);

Results - 1
SQL> Select * from t1;

ID NAME AGE SE
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
1201 Peter 23 male
6695 Lu 12 female
6720 Ka 22 female
1202 lili 30 female

Visible, through the above code, just the t2 id of 1202 records in the insert to the t1, no name for Peter's record in the t1 deletion, or delete statements are not performed,


The rollback back as a result,
Now in table t1 content is:
SQL> Select * from t1;

ID NAME AGE SE
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
1201 Peter 23 male
6695 Lu 12 female
6720 Ka 22 female

- test 2:
The MERGE INTO t1 b
USING t2 e
ON (b.I D=e.I D)
The WHEN MATCHED THEN
The update
The set b.N AME=E.N AME,
B.A GE=E.A GE,
B.S E=E.S E
Where a t1. Name='Peter'
Delete the where (t1) name='Peter')
WHEN NOT MATCHED THEN
Insert (b.I D, b.N AME, b.A GE, b.S E)
Values (E.I D, E.N AME, E.A GE, E.S E);

Results - 2:
SQL> Select * from t1;

ID NAME AGE SE
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
1201 John 47 male
6695 Lu 12 female
6720 Ka 22 female
1202 lili 30 female

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related