Presentation is loading. Please wait.

Presentation is loading. Please wait.

Creating DDL and Database Event Triggers. 2 home back first prev next last What Will I Learn? Describe events that cause DDL and database event triggers.

Similar presentations


Presentation on theme: "Creating DDL and Database Event Triggers. 2 home back first prev next last What Will I Learn? Describe events that cause DDL and database event triggers."— Presentation transcript:

1 Creating DDL and Database Event Triggers

2 2 home back first prev next last What Will I Learn? Describe events that cause DDL and database event triggers to fire Create a trigger for a DDL statement Create a trigger for a database event Describe the functionality of the CALL statement Describe the cause of a mutating table

3 3 home back first prev next last Why Learn It? What if you accidentally drop an important table? If you have a backup copy of the table data, you can certainly retrieve the lost data. But it may be important to know exactly when the table was dropped. For security reasons, a Database Administrator may want to keep an automatic record of who has logged into a database, and when. These are two examples of the uses of DDL and Database Event triggers.

4 4 home back first prev next last What are DDL and Database Event Triggers? DDL Triggers are fired by DDL statements: CREATE, ALTER or DROP. Database Event triggers are fired by non- SQL events in the database, for example: –A user connects to, or disconnects from, the database –The DBA starts up, or shuts down, the database –A specific exception is raised in a user session.

5 5 home back first prev next last Creating Triggers on DDL Statements Syntax: –ON DATABASE will fire the trigger for DDL on all schemas in the database –ON SCHEMA will fire the trigger only for DDL on objects in your own schema

6 6 home back first prev next last Example of a DDL Trigger We want to write a log record every time a new database object is created in our schema: The trigger will fire whenever any (type of) object is created. We cannot create a DDL trigger which refers to a specific database object. –But we can use ora_dict_obj_name event attribute to know the affected object name

7 7 home back first prev next last Example of a DDL Trigger CREATE TABLE log_table( user_name VARCHAR2(100), event_date DATE, detailVARCHAR2(400)); CREATE OR REPLACE TRIGGER log_create_trigg AFTER CREATE ON SCHEMA BEGIN INSERT INTO log_table (user_name, event_date, detail) VALUES (USER, SYSDATE, 'Changed object is: ' || ora_dict_obj_name); END;

8 8 home back first prev next last A Second Example of a DDL Trigger We want to prevent any objects being dropped from our schema. The trigger will fire whenever any (type of) object is dropped. Again, we cannot create a DDL trigger which refers to a specific database object.

9 9 home back first prev next last Creating Triggers on Database Events Syntax: –ON DATABASE will fire the trigger for events on all sessions in the database –ON SCHEMA will fire the trigger only for your own sessions.

10 10 home back first prev next last Example 1: LOGON and LOGOFF Triggers We can only use after with logon and before with logoff, why?

11 11 home back first prev next last Example 1: LOGON and LOGOFF Triggers We can trace users’ worksites by remembering their IP address CREATE OR REPLACE TRIGGER logon_trig AFTER LOGON ON SCHEMA BEGIN INSERT INTO log_table (user_name, event_date, detail) VALUES (USER, SYSDATE, 'Logging on from: ' || ora_client_ip_address); END; Here we used a event attribute called ora_client_ip_address

12 12 home back first prev next last Example 2: A SERVERERROR Trigger We want to keep a log of any ORA-00942 errors which occur in our sessions:

13 13 home back first prev next last CALL Statements in a Trigger There is no END; statement, and no semicolon at the end of the CALL statement.

14 14 home back first prev next last Mutating Tables and Row Triggers A mutating table is a table which is currently being modified by a DML statement. A row trigger cannot SELECT from a mutating table, because it would see an inconsistent set of data (the data in the table would be changing while the trigger was trying to read it). However, a row trigger can SELECT from a different table if needed. This restriction does not apply to DML statement triggers, only to DML row triggers.

15 15 home back first prev next last Mutating Table: Example

16 16 home back first prev next last Mutating Table: Example UPDATE employees SET salary = 3400 WHERE last_name = 'Davies';

17 17 home back first prev next last Terminology Key terms used in this lesson include: –DDL Trigger –Database Event Trigger –CALL statement –Mutating Table

18 18 home back first prev next last Summary In this lesson, you learned to: –Describe events that cause DDL and database event triggers to fire –Create a trigger for a DDL statement –Create a trigger for a database event –Describe the functionality of the CALL statement –Describe the cause of a mutating table

19 19 home back first prev next last Try It / Solve It The exercises in this lesson cover the following topics: –Describing events that cause DDL and database event triggers to fire –Creating a trigger for a DDL statement –Creating a trigger for a database event –Describing the functionality of the CALL statement –Describing the cause of a mutating table


Download ppt "Creating DDL and Database Event Triggers. 2 home back first prev next last What Will I Learn? Describe events that cause DDL and database event triggers."

Similar presentations


Ads by Google