系统触发器
创建一个表,记录用户的系统操作
create table user_log(logid number,username varchar2(100),logondate date,logoffdate date,ip varchar2(100),logtype varchar2(100));--创建用户登录记录表
create sequence user_log_seq;--创建序列
CREATE OR REPLACE TRIGGER logon_trigger --创建登录触发器
AFTER LOGON
ON DATABASE
BEGIN
INSERT INTO user_log(logid,username,logondate,ip,logtype)
VALUES (user_log_seq.nextval,ORA_LOGIN_USER,SYSDATE,ORA_CLIENT_IP_ADDRESS,'LOGON') ;
END ;
/
创建登出触发器
CREATE OR REPLACE TRIGGER logoff_tgr
BEFORE LOGOFF
ON DATABASE
BEGIN
INSERT INTO user_log(logid,username,logoffdate,ip,logtype)
VALUES (user_log_seq.nextval,ORA_LOGIN_USER,SYSDATE,ORA_CLIENT_IP_ADDRESS,'LOGOFF') ;
END ;
/
Select * from user_logs;--查看用户登录记录表
create table db_event_log(eventId number,eventType varchar2(100),eventDate date,eventUser varchar2(100));--创建数据库日志表
create sequence db_event_log_seq;--创建序列
CREATE OR REPLACE TRIGGER startup_trigger--创建数据库启动触发器
AFTER STARTUP
ON DATABASE
BEGIN
INSERT INTO db_event_log(eventId,eventType,eventDate,eventUser)
VALUES (db_event_log_seq.nextval,'STARTUP',SYSDATE,ORA_LOGIN_USER) ;
COMMIT ;
END ;
/
调整sqlplus显示
col EVENTTYPE for a10
/
set lines 1000
/
CREATE OR REPLACE TRIGGER shutdown_trigger--创建数据库关机触发器
BEFORE SHUTDOWN
ON DATABASE
BEGIN
INSERT INTO db_event_log(eventId,eventType,eventDate,eventUser)
VALUES (db_event_log_seq.nextval,'SHUTDOWN',SYSDATE,ORA_LOGIN_USER) ;
COMMIT ;
END ;
/
Create table db_error(eid number,username varchar2(100),errorDate date,dbname varchar2(100),content varchar2(100));创建记录数据库故障的表
create sequence db_error_seq;创建序列记录报错
CREATE OR REPLACE TRIGGER error_trigger--创建触发器记录数据库鼓掌
AFTER SERVERERROR ON DATABASE
BEGIN
INSERT INTO db_error(eid,username,errorDate,dbname,content)
VALUES (db_error_seq.nextval,ORA_LOGIN_USER,SYSDATE,ORA_DATABASE_NAME,DBMS_UTILITY.format_error_stack) ;
END ;
/