Oracle在9i引入了merge命令,
通過(guò)這個(gè)merge你能夠在一個(gè)SQL語(yǔ)句中對(duì)一個(gè)表同時(shí)執(zhí)行inserts和updates操作. 當(dāng)然是update還是insert是依據(jù)于你的指定的條件判斷的,Merge into可以實(shí)現(xiàn)用B表來(lái)更新A表數(shù)據(jù)喧枷,如果A表中沒(méi)有惦银,則把B表的數(shù)據(jù)插入A表. MERGE命令從一個(gè)或多個(gè)數(shù)據(jù)源中選擇行來(lái)updating或inserting到一個(gè)或多個(gè)表
語(yǔ)法如下
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 ! ]
我們先看看一個(gè)簡(jiǎn)單的例子懈词,來(lái)介紹一個(gè)merge into的用法
merge into products p using newproducts np on (p.product_id = np.product_id)
when matched then
update set p.product_name = np.product_name
when not matched then
insert values(np.product_id, np.product_name, np.category)
在這個(gè)例子里蜻势。前面的merger into products using newproducts 表示的用newproducts表來(lái)merge到products表读恃,merge的匹配關(guān)系就是on后面的條件子句的內(nèi)容愕鼓,這里根據(jù)兩個(gè)表的product_id來(lái)進(jìn)行匹配雷滚,那么匹配上了我們的操作是就是when matched then的子句里的動(dòng)作了需曾,這里的動(dòng)作是update set p.product_name = np.product_name, 很顯然就是把newproduct里的內(nèi)容,賦值到product的product_name里祈远。如果沒(méi)有匹配上則insert這樣的一條語(yǔ)句進(jìn)去呆万。 大家看看這個(gè)merget inot的用法是不是一目了然了呀。這里merger的功能绊含,好比比較桑嘶,然后選擇更新或者是插入,是一系列的組合拳躬充,在做merge的時(shí)候逃顶,這樣同樣的情況下,merge的性能是優(yōu)于同等功能的update/insert語(yǔ)句的充甚。有人曾經(jīng)分析merge是批量處理對(duì)性能貢獻(xiàn)很大以政,個(gè)人覺(jué)得這個(gè)是沒(méi)有考據(jù)的。
我們也可以在using后面使用視圖或者子查詢伴找。比如我們把newproducts換成
merge into products p using (select * from newproducts) np on (p.product_id = np.product_id)
when matched then
update set p.product_name = np.product_name
when not matched then
insert values(np.product_id, np.product_name, np.category)
也是可以的盈蛮。
在Oracle 10g中MERGE有如下一些改進(jìn):
1、UPDATE或INSERT子句是可選的
2技矮、UPDATE和INSERT子句可以加WHERE子句
3抖誉、在ON條件中使用常量過(guò)濾謂詞來(lái)insert所有的行到目標(biāo)表中,不需要連接源表和目標(biāo)表
4、UPDATE子句后面可以跟DELETE子句來(lái)去除一些不需要的行
我們通過(guò)實(shí)例來(lái)一一看看如上的新特性
1. UPDATE或INSERT子句是可選的
在9i里由于必須insert into和update都要存在衰倦,也就是不是update就是insert袒炉,不支持單一的操作,雖然還是可以曲線救國(guó)樊零,呵呵 但是有些過(guò)于強(qiáng)勢(shì)了我磁。而10g里就是可選了孽文,能符合我們更多的需求了
比如上面的句子
我們可以只存在update或者insert
merge into products p using newproducts np on (p.product_id = np.product_id)
when matched then
update set p.product_name = np.product_name
這里,如果匹配就更新夺艰,不存在就不管了芋哭。
2. UPDATE和INSERT子句可以加WHERE子句
這也是一個(gè)功能性的改進(jìn),能夠符合我們更多的需求郁副,這個(gè)where的作用很明顯是一個(gè)過(guò)濾的條件减牺,是我們加入一些額外的條件,對(duì)只對(duì)滿足where條件的進(jìn)行更新和insert
merge into products p using (select * from newproducts) np on (p.product_id = np.product_id)
when matched then
update set p.product_name = np.product_name where np.product_name like 'OL%'
這里表示只是對(duì)product_name開頭是'OL'的匹配上的進(jìn)行update存谎,如果開頭不是'OL'的就是匹配了也不做什么事情烹植,insert里也可以加入where
比如
merge into products p using (select * from newproducts) np on (p.product_id = np.product_id)
when matched then
update set p.product_name = np.product_name where np.product_name like 'OL%'
when not matched then
insert values(np.product_id, np.product_name, np.category) where np.product_name like 'OL%'
這里注意比較一下,他們返回的結(jié)果行數(shù)愕贡,是有著差異的草雕。
3. 在ON條件中使用常量過(guò)濾謂詞來(lái)insert所有的行到目標(biāo)表中,不需要連接源表和目標(biāo)表
merge into products p using (select * from newproducts) np on (1=0)
when matched then
update set p.product_name = np.product_name
when not matched then
insert values(np.product_id, np.product_name, np.category)
個(gè)人覺(jué)得這個(gè)功能沒(méi)有太大的意義,我們的insert into本身就支持這樣的功能固以,沒(méi)有必要使用merge
4. UPDATE子句后面可以跟DELETE子句來(lái)去除一些不需要的行
delete只能和update配合墩虹,從而達(dá)到刪除滿足where條件的子句的紀(jì)錄
merge into products p using (select * from newproducts) np on (p.product_id = np.product_id)
when matched then
update set p.product_name = np.product_name delete where p.product_id = np.product_id where np.product_name like 'OL%'
when not matched then
insert values(np.product_id, np.product_name, np.category)
這里我們達(dá)到的目的就是 會(huì)把匹配的記錄的prodcut_name更新到product里,并且把product_name開頭為OL的刪除掉憨琳。
merge into也是一個(gè)dml語(yǔ)句诫钓,和其他的dml語(yǔ)句一樣需要通過(guò)rollback和commit 結(jié)束事務(wù)。
Merge是一個(gè)非常強(qiáng)大的功能篙螟,而且是我們需求里經(jīng)常會(huì)用到的一個(gè)有用的功能菌湃,所以我們一定要好好的學(xué)習(xí)到。