在項(xiàng)目中一般需要對一些數(shù)據(jù)進(jìn)行處理,以下提供一些基本的SQL語句:
沿河網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)公司!從網(wǎng)頁設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、響應(yīng)式網(wǎng)站建設(shè)等網(wǎng)站項(xiàng)目制作,到程序開發(fā),運(yùn)營維護(hù)。創(chuàng)新互聯(lián)公司從2013年創(chuàng)立到現(xiàn)在10年的時間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)公司。
1.基于條件的插入和修改:需要在表中插入一條記錄,插入前根據(jù)key標(biāo)識判斷。如果標(biāo)識符不存在,則插入新紀(jì)錄,如果標(biāo)識符存在,則根據(jù)語句中所給的新值對原紀(jì)錄中的字段進(jìn)行更新:
merge into A using B on (A.key = B.key) when matched then update set A.name = B.name when not matched then insert into (A.key, A.name) values (B.key, B.name)
2.在分組和集合里統(tǒng)計(jì)分組:當(dāng)需要統(tǒng)計(jì)一個分組里的成員,或有多少個子分組,及其他基于集合的統(tǒng)計(jì)。在進(jìn)行基于集合的統(tǒng)計(jì)時,還需要動態(tài)包括或剔除滿足特定條件的記錄。
實(shí)例如下:基于職工在企業(yè)里的升職次數(shù),來統(tǒng)計(jì)企業(yè)每個員工經(jīng)歷過的工作崗位次數(shù)。
select jh.JobsHeld, count(*) as StaffCount from (select u.employee_id, count(*) as JobsHeld from (select employee_id from hr.employees union all select employee_id from hr.job_history) u group by u.employee_id) jh group by jh.JogsHeld
3.根據(jù)時間戳恢復(fù)數(shù)據(jù):
ALTER TABLE tablename ENABLE row movement ; flashback table tablename to timestamp to_timestamp('2012-09-13 13:00:00','yyyy-mm-dd hh34:mi:ss');
4.有一個類型為字符串的字段,要講其值遷移到新系統(tǒng)里的數(shù)值類型字段中:
create or replace function isnum(v_in varchar2) return varchar is val_err exception; pragma exception_init(val_err, -6502) --char to num conv.error scrub_num number; begin scrub_num := to_number(v_in) return 'Y'; exception when val_err then return 'N'; end;
5.解決死鎖問題:
(1).查看被鎖的表 select b.owner,b.object_name,a.session_id,a.locked_mode from v$locked_object a,dba_objects b where b.object_id = a.object_id; (2).查看引起死鎖的會話 select b.username,b.sid,b.serial#,logon_time from v$locked_object a,v$session b where a.session_id = b.sid order by b.logon_time; (3).殺掉死鎖 alter system kill session ‘sid,serial#’;
6.查看當(dāng)前連接用戶正在運(yùn)行的SQL語句:
select a.SID, a.USERNAME, b.SQL_TEXT from v$session a, v$sqltext_with_newlines b where a.SQL_ADDRESS = b.ADDRESS and a.SQL_HASH_VALUE = b.HASH_VALUE order by a.USERNAME, a.SID, b.piece
7.查詢最消耗資源的實(shí)時語句:
oracle 11g中,可以通過查詢V$SQL_MONITOR來監(jiān)控SQL查詢近乎實(shí)時的消耗資源 select * from ( select a.sid session_id,a.sql_id,a.status,a.cpu_time/1000000 cpu_sec,a.buffer_gets,a.disk_reads,b.sql_text sql_text from v$sql_monitor a,v$sql_b where a.sql_id=b.sql_id order by a.cpu_time desc) where rownum<=20;