views:

337

answers:

2

I have the following table and trigger.

CREATE TABLE LogSchema (
    user varchar2(100),
    date_ date,
    operation varchar2(100),
    obj_type varchar2(100),
    obj_name varchar2(100)
);

CREATE OR REPLACE TRIGGER LogSchema
AFTER DDL ON USER1.SCHEMA 
/* can't use ON DATABASE or else it will log 
   everything that happens on all schemas*/
DECLARE
BEGIN
  INSERT INTO LogSchema VALUES
     (USER
       , SYSDATE
       , sys.sysevent
       , sys.dictionary_obj_type
       , sys.dictionary_obj_name);

END LogSchema;

I want to log every DDL action (create/drop table/procedure etc) that happens on USER1 schema. This trigger is only being executed if USER1 makes a DDL action, if USER2 makes a DDL action on USER1 schema like:

CREATE TABLE USER1.TEST (
        test varchar2(100)
);

it doesn't get logged. Anyone knows what is wrong with my trigger?

+3  A: 

The "ON user.schema" clause doesn't work quite the way you think. From the 10gR2 SQL Reference:

"The trigger fires whenever any user connected as schema initiates the triggering event."

(emphasis mine)

So, the trigger only logs DDL issue when connected AS USER1, not when DDL is performed on the schema USER1.

DCookie
+1 The syntax is confusing. In most situations SCHEMA strictly means the collection of objects owned by an account, and Hoffman's interpretation fits that usage. To conform to the actual implementation the syntax ought to be something like `AFTER DDL BY user1.USER`.
APC
Agree. It took me a while to figure it out myself through experimentation and THEN re-reading the docs.
DCookie
Thanks for the response, but how do I make a trigger that does what I want? =)
Hoffmann
+1  A: 

Why do you 'build your own' and not use standard database functionality like 'Auditing'?

Robert Merkwürdigeliebe
Homework assignment, I have to do the way the professor asks. The assignment was about triggers, not about database tools.
Hoffmann