1. 程式人生 > >ORACLE 實用

ORACLE 實用

觸發器實現記錄對某張表做刪除修改操作的使用者資訊

資料庫觸發器是一個與表關聯的儲存下來的可以自動執行的PL/SQL程式。每當一個特定的資料操作語句在指定的表上發出時,Oracle自動的執行觸發器中定義的語句序列。比如我們可以使用觸發器實現讓使用者在週末無法對資料庫表進行修改。

下面的例子是建立一個資訊表,用來儲存對某張表做了刪除操作的使用者的相關資訊。

#觸發器監控表的修改和刪除操作

#1 建立接收刪除使用者的表資訊

SQL> create tabletab_mon_midify as (select sid,username,program,machine,'000.000.000.000' ipadd,sysdate moditime from v$session where0=1);

該表分別記錄了,刪除的使用者、機器名、ip及刪除時間等

#2 建立觸發器

SQL>create or replacetrigger tab_mon_mod_tr

before delete on Md_Accountsubject

for each row

begin

insert into tab_mon_midify

selectsid,username,program,machine,sys_context('userenv','ip_address'),sysdate

from v$session where audsid =userenv('sessionid');

end;

#3 做完以上操作,一旦修改,記錄會自動插入監控表裡面

觸發器實現禁止對某張表做修改

# 建立觸發器,指定欄位變化時,彈出報錯不允許修改

create or replace triggerForbidUpdate_COM_LINE_B

before update on HSS_TASK_ITEM_COM_LINE_B

for each row

begin

if

updating('STATUS_CODE') or updating('LAST_UPDATE_DATE') or updating('BATCH_NUM')

then

return;

else

Raise_application_error(-20527, 'thetrigger is added by HAND, Exception: In order to ensure the bill rowsinformation is correct, HSS_TASK_ITEM_COM_LINE_B can not be updated!!! ');

end if;

end;

--error_number是範圍在-20000到-20999之間的負整數

----刪除觸發器

 drop triggerForbidUpdate_COM_LINE_B;

當對錶HSS_TASK_ITEM_COM_LINE_B裡面除了STATUS_COD,LAST_UPDATE_DATE,BATCH_NUM這三個欄位以外的欄位進行修改時,彈出報錯-20527, 'thetrigger is added by HAND, Exception: In order to ensure the bill rowsinformation is correct, HSS_TASK_ITEM_COM_LINE_B can not be updated!!!,並會讓當前的修改操作失效。

ORACLE匯入與匯出

匯出與匯入分別都有三種方式:

--完全模式匯出

將整個資料庫內容匯出,但是操作時需要有特殊許可權。

命令格式:

exp 使用者名稱/密碼@例項名buffer=32000 匯出路徑 full=y;

例如:

exp dna/[email protected] buffer=1024 file=d:/97back1121.dmp full=y;

--使用者模式匯出

將指定使用者的所有物件進行匯出:

exp dna/[email protected] buffer=32000file=d:/test.dmp owner=dna

--表模式匯出

將使用者的所有表資料進行匯出:

exp dna/[email protected] buffer=32000file=d:/dna.dmp owner=dna tables=(dna)

--匯入

imp dna/[email protected] file= d:/dna.dmp log= d:/oracle/dna.log full=y ignore=y;

注意,在出時最好使用dba許可權,不然可能會出錯。

在匯入時,要注意表空間是否夠用,否則也有可能導致失敗。

ORACLE檢視鎖表情況

執行以下語句,可以檢視當前資料庫裡的鎖表情況,而且還有相應的解鎖語句在裡面。

SELECTdob.object_name table_name,

lo.locked_mode,

lo.session_id,

vss.serial#,

vps.spid,

vss.action,

vss.osuser,

vss.process,

vps.spid db_pid,

'alter system kill session ' || '''' ||lo.session_id || ',' || vss.serial# || ''';' kill_command

FROM v$locked_object lo, dba_objects dob,v$session vss, v$process vps

WHERE lo.object_id = dob.object_id

AND lo.session_id = vss.sid

AND vss.paddr = vps.addr

ORDER BY 2, 3, dob.object_name;

新建使用者,賦權,設定表空間

sqlplus/nolog

conn/as sysdba

--建立表空間

createtablespace dna_htqc datafile 'D:\oracle\dna_uat\dna_htqc.ora' size 500M;

--新建使用者並設定預設表空間

createuser dna_htqc identified by "dna_htqc" default tablespace dna_htqc;

--賦權

grant create session to dna_htqc;

grant create table to dna_htqc;

grant create tablespace to dna_htqc;

grant create view to dna_htqc;

grant resource to dna_htqc;

--修改表空間自動增長屬性:

alter database datafile'D:\ORACLE\HX_UAT\HX_UAT.ORA' autoextend on;

--修改表空間大小:

alterdatabase datafile'D:\ORACLE\AISINOFSSC.ORA' resize 1500m;

--修改使用者的預設表空間

alteruser dna_uat default tablespace dna_uat;

--檢視使用者表空間使用情況,以及是否有自動擴充套件能力

SELECTT.TABLESPACE_NAME,D.FILE_NAME,

D.AUTOEXTENSIBLE,D.BYTES,D.MAXBYTES,D.STATUS

FROMDBA_TABLESPACES T,DBA_DATA_FILES D

WHERET.TABLESPACE_NAME =D.TABLESPACE_NAME

ORDER BY TABLESPACE_NAME,FILE_NAME;

--檢視使用者

selectusername,default_tablespace from user_users;

--某個使用者在哪個表空間下:

selecttablespace_name,file_name,autoextensible from dba_data_files;

--該使用者下所有的表:

select * from user_tables;

資料庫連線相關

--當前連線數

selectcount(*) from v$process where program='Oracle.EXE(SHAD)';

selectcount(*) from v$process;

--最大連線數

selectvalue from v$parameter where name ='processes';

--修改最大連線數

altersystem set processes = 300 scope = spfile;

--最大連線

showparameter processes;

--併發連線數

selectcount(*) from v$session where status='ACTIVE';

--當前的session連線數

selectcount(*) from v$session;

--檢視當前有哪些使用者正在使用資料

SELECTosuser, a.username,cpu_time/executions/1000000||'s',b.sql_text,machine

fromv$session a, v$sqlarea b

wherea.sql_address =b.address order by cpu_time/executionsdesc;