Trigger VI (System trigger) (Learning Note)

Source: Internet
Author: User

system triggers

System triggers are used to monitor the access of database services for information such as open, close, error, or to monitor user behavior. If you are creating a system trigger, you can use the following syntax

CREATE [OR REPLACE] TRIGGERTrigger Name[before | after] [Database Events]  on [DATABASE | SCHEMA]    [When trigger condition]    [DECLARE]        [part of the program Declaration;]    BEGINpart of the program code; END [Trigger Name];

use an administrator to create a system trigger System trigger Events
NO. Event Trigger Time Describe
1 STARTUP After Triggers after the DB instance is started
2 SHUTDOWN Before Triggered before the DB instance is closed
3 Servererror After Triggered when an error occurs
4 LOGON After Triggered after a user logs on
5 LOGOFF BEFOR Trigger before user logoff

Example one , login log out logging function

--Create a sequenceCREATESEQUENCE user_log_seq;--Create a log tableCREATE TABLEUser_log (Logid Number CONSTRAINTPk_logoidPRIMARY KEY, usernameVARCHAR2( -) not NULL, logodate date, logoffdate date, IPVARCHAR2( -), LogTypeVARCHAR2( -));
 --  Create a login trigger   or  replace  trigger   Logon_trigger after logon  on  database  declare  begin  insert  into   User_log (logid,username,logodate,ip,logtype)  values  (User_log_ Seq.nextval,ora_login_user,sysdate,ora_client_ip_address, logon   " );  end  Logon_trigger; 
--Create a logout triggerCREATE OR REPLACE TRIGGERLogon_trigger before LOGOFF on DATABASEDECLAREBEGIN  INSERT  intoUser_log (Logid, USERNAME, Logoffdate, IP, LogType)VALUES(User_log_seq. Nextval, Ora_login_user, Sysdate, ora_client_ip_address,'LOGFF');ENDLogon_trigger;

Switch users to sign in

Use admin login to view the User_log table

SELECT *  from User_log;

Trigger VI (System trigger) (Learning Note)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.