oracle--CREATE trigger implementation to track user login information

Source: Internet
Author: User

---Create log table records user logon information
CREATE TABLE User_log
(
user_id VARCHAR2 (30),
session_id Number (10),
HOST_NAME VARCHAR2 (30),
Last_module VARCHAR2 (30),
Logon_day DATE,
Logoff_day DATE,
Elapsed_minutes Number (10)
);

--Create a trigger after the user logs in to count the user's login information:

Create or Replace Trigger Logon_trigger
After logon on database
Begin
INSERT INTO User_log
Values
User
Sys_context (' Userenv ', ' SessionID '),
Sys_context (' Userenv ', ' host '),
Null
Sysdate,
Null
Null
);
End

SELECT * from User_log;

--Create a trigger before the user log out to count the information when the user logged out:

Create or Replace Trigger Logoff_trigger
Before logoff on database
Begin
Update User_log
Set last_module =
(SELECT module
From V$session
where Sys_context (' USERENV ', ' SESSIONID ') = Audsid)
where Sys_context (' USERENV ', ' SESSIONID ') = session_id;
Update User_log
Set logoff_day = Sysdate
where Sys_context (' USERENV ', ' SESSIONID ') = session_id;
Update User_log
Set elapsed_minutes = Round ((logoff_day-logon_day) * 1440)
where Sys_context (' USERENV ', ' SESSIONID ') = session_id;
End

oracle--CREATE trigger implementation to track user login information

Related Article

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.