Creating DDL and Database Event Triggers - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

Creating DDL and Database Event Triggers

Description:

Creating DDL and Database Event Triggers What Will I Learn? Describe events that cause DDL and database event triggers to fire Create a trigger for a DDL statement ... – PowerPoint PPT presentation

Number of Views:371
Avg rating:3.0/5.0
Slides: 20
Provided by: SSI58
Category:

less

Transcript and Presenter's Notes

Title: Creating DDL and Database Event Triggers


1
Creating DDL and Database Event Triggers
2
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
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
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
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
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
Example of a DDL Trigger
  • CREATE TABLE log_table(
  • user_name VARCHAR2(100),
  • event_date DATE,
  • detail VARCHAR2(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
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
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
Example 1 LOGON and LOGOFF Triggers
We can only use after with logon and before with
logoff, why?
11
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
Example 2 A SERVERERROR Trigger
  • We want to keep a log of any ORA-00942 errors
    which occur in our sessions

13
CALL Statements in a Trigger
  • There is no END statement, and no semicolon at
    the end of the CALL statement.

14
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
Mutating Table Example
16
Mutating Table Example
  • UPDATE employees SET salary 3400 WHERE
    last_name 'Davies'

17
Terminology
  • Key terms used in this lesson include
  • DDL Trigger
  • Database Event Trigger
  • CALL statement
  • Mutating Table

18
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
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
Write a Comment
User Comments (0)
About PowerShow.com