溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

數(shù)據(jù)庫insert高級語法用法

發(fā)布時間:2021-08-23 17:33:32 來源:億速云 閱讀:343 作者:chen 欄目:關系型數(shù)據(jù)庫

這篇文章主要介紹“數(shù)據(jù)庫insert高級語法用法”,在日常操作中,相信很多人在數(shù)據(jù)庫insert高級語法用法問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”數(shù)據(jù)庫insert高級語法用法”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!

有這樣一個業(yè)務需求,要求根據(jù)不同條件將源數(shù)據(jù)插入到不同的表中,源數(shù)據(jù)表更新十分頻繁,這樣的一個業(yè)務需求應該怎么做呢?我先給出源表結構和需要插入的表結構:
更新十分頻繁的源表:
create table people(id number,name varchar2(20),sex char(3),address varchar2(500));

給源表插入數(shù)據(jù):
insert into people 
values(1,'張翠山','男','武當山');
insert into people 
values(2,'殷素素','女','天鷹教');
insert into people 
values(3,'張無忌','男','明教');
insert into people 
values(4,'趙敏','女','皇室');
insert into people 
values(5,'周芷若','女','峨眉山');
insert into people 
values(6,'成昆','男','少林寺');
commit;
查詢一下people結果,如圖所示:
數(shù)據(jù)庫insert高級語法用法

待插入的幾張表結構:
create table people_1(id number,name varchar2(20));
create table people_2(id number,sex char(3));
create table people_3(sex char(3),address varchar2(500));
現(xiàn)在的業(yè)務需求是,將ID<=4的記錄插入到people_1中,SEX='男'的插入到people_2,剩下的插入到people_3中。乍一看,此需求可以這樣做insert into people_1 select id,name from people where id<=4; insert into people_2 select id,sex from people where SEX='男'; insert into people_3 select sex,sddress from people where id>4 and SEX!='男';但是people是一個頻繁DML的表,如果分成這樣子三步執(zhí)行,在數(shù)據(jù)量特別大的情況下,每步插入操作會消耗較長時間,在這一段時間之內,又會有大批量數(shù)據(jù)改變,導致三步操作的表數(shù)據(jù)基數(shù)都不一樣,結果當然會有偏差。因此,在這里我們用到了insert的高級語法。
SQL如下:
insert all
when id<=4 then into people_1 values(id,name)
  when sex='男' then into people_2 values(id,sex)
    else into people_3 values(sex,address)
select * from people;
結果如下:
PEOPLE_1:                   PEOPLE_2:         PEOPLE_3:
數(shù)據(jù)庫insert高級語法用法       數(shù)據(jù)庫insert高級語法用法    數(shù)據(jù)庫insert高級語法用法

如果我只想ID=1在people_1中插入之后就不在people_2中插入,那么可以使用insert first,它會從前到后進行判斷,如果一條記錄在前面已經插入,則在之后略過此條記錄。
SQL如下:
insert first
when id<=4 then into people_1 values(id,name)
  when sex='男' then into people_2 values(id,sex)
    else into people_3 values(sex,address)
select * from people;
結果如下:
PEOPLE_1:                   PEOPLE_2:         PEOPLE_3:
數(shù)據(jù)庫insert高級語法用法   數(shù)據(jù)庫insert高級語法用法       數(shù)據(jù)庫insert高級語法用法

當然,這個是根據(jù)條件插入的,當沒有條件的時候,可以直接進行插入。
SQL如下:
insert all/first
into people_1 values(id,name)
into people_2 values(id,sex)
into people_3 values(sex,address)
select * from people;
其實很多時候,現(xiàn)實業(yè)務的要求oracle都為我們考慮到了,而且其性能通常強于我們自己寫的PL/SQL,更多的精彩業(yè)務實現(xiàn),還等著我們去探索。

到此,關于“數(shù)據(jù)庫insert高級語法用法”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續(xù)學習更多相關知識,請繼續(xù)關注億速云網站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經查實,將立刻刪除涉嫌侵權內容。

AI