Home » RDBMS Server » Server Administration » Database level Trigger
Database level Trigger [message #14200] Sun, 15 August 2004 15:39 Go to next message
Amarsey
Messages: 17
Registered: March 2002
Junior Member
I have a table  say B containing records from different tables, at least three tables. I want whatever changes made on the individual records in their tables to reflect in the  table B.

Is there a way to write a database trigger to do this work since any update to table B can come from either through sqlplus or forms6i.

Any help?

Thanks
Re: Database level Trigger [message #14202 is a reply to message #14200] Sun, 15 August 2004 21:55 Go to previous messageGo to next message
Maaher
Messages: 7065
Registered: December 2001
Senior Member
Well, you can add a before row trigger (insert/update/delete) on each of the seed tables. In each of those triggers you copy the content to the B table (+ eventually the action performed).

MHE
Re: Database level Trigger [message #14216 is a reply to message #14202] Mon, 16 August 2004 11:12 Go to previous messageGo to next message
Amarsey
Messages: 17
Registered: March 2002
Junior Member
Thanks for your quick response. One more thing, being a novice, i would like you to demonstrate the actual code since i have not code such triggers before.
example table A (id, name, nationality)
table B(id,grade,course,nationality).
assuming the nationality field of table A has been updated through a form, how does the update takes effect on table B column: Nationality through a row-level trigger.
example pls!
Thanks
Re: Database level Trigger [message #14228 is a reply to message #14216] Mon, 16 August 2004 21:53 Go to previous messageGo to next message
Barbara Boehmer
Messages: 9102
Registered: November 2002
Location: California, USA
Senior Member
scott@ORA92> -- starting data:
scott@ORA92> select * from a
  2  /

        ID NAME            NATIONALITY
---------- --------------- ---------------
         1 Godwin          USA

scott@ORA92> select * from b
  2  /

        ID G COURSE          NATIONALITY
---------- - --------------- ---------------
         1 A SQL and PL/SQL  USA


scott@ORA92> -- trigger:
scott@ORA92> create or replace trigger a_bur
  2    before update on a
  3    for each row
  4  begin
  5    update b
  6    set    nationality = :new.nationality
  7    where  id = :new.id;
  8  end a_bur;
  9  /

Trigger created.

scott@ORA92> show errors
No errors.


scott@ORA92> -- test:
scott@ORA92> update a set nationality = 'FRANCE'
  2  where  name = 'Godwin'
  3  /

1 row updated.


scott@ORA92> -- results:
scott@ORA92> select * from a
  2  /

        ID NAME            NATIONALITY
---------- --------------- ---------------
         1 Godwin          FRANCE

scott@ORA92> select * from b
  2  /

        ID G COURSE          NATIONALITY
---------- - --------------- ---------------
         1 A SQL and PL/SQL  FRANCE
Re: Database level Trigger [message #14229 is a reply to message #14216] Mon, 16 August 2004 22:20 Go to previous message
Maaher
Messages: 7065
Registered: December 2001
Senior Member
Here's a possible scenario for an audit table:
SQL> CREATE TABLE source_table( id   NUMBER
  2                           , name VARCHAR2(64)
  3                           , nat  VARCHAR2(2)
  4                           , CONSTRAINT st_pk PRIMARY KEY (id)
  5                           )
  6  /

Table created.

SQL> 
SQL> CREATE SEQUENCE aud_seq 
  2    START WITH 1 
  3    INCREMENT BY 1 
  4    MINVALUE 1 
  5    CACHE 20 
  6    NOCYCLE 
  7    NOORDER 
  8  /

Sequence created.

SQL> 
SQL> CREATE TABLE audit_table( aud_id    NUMBER
  2                          , tab_name  VARCHAR2(64)  NOT NULL
  3                          , operation VARCHAR2(1)
  4                          , tab_pk    NUMBER        NOT NULL
  5                          , name      VARCHAR2(64)
  6                          , nat       VARCHAR2(2)
  7                          , timestamp DATE          NOT NULL
  8                          , CONSTRAINT aud_pk PRIMARY KEY (aud_id)
  9                          )
 10  /

Table created.

SQL> 
SQL> CREATE TRIGGER st_bruid
  2    BEFORE INSERT OR UPDATE OR DELETE 
  3    ON source_table 
  4    FOR EACH ROW
  5  DECLARE
  6    v_id NUMBER;
  7  BEGIN
  8    -- generate primary key
  9    SELECT aud_seq.nextval INTO v_id FROM dual;
 10  
 11    IF INSERTING THEN
 12      INSERT INTO audit_table
 13      VALUES ( v_id  -- aud_id
 14             , 'SOURCE_TABLE' -- tab_name
 15             , 'I'            -- operation (I)nsert
 16             , :NEW.id        -- tab_pk
 17             , :NEW.name      -- name from source_table
 18             , :NEW.nat       -- nat(ionality) from source_table
 19             , SYSDATE        -- timestamp (use timestamp datatype on 9i)
 20             );
 21    ELSIF UPDATING THEN
 22      INSERT INTO audit_table
 23      VALUES ( v_id  -- aud_id
 24             , 'SOURCE_TABLE' -- tab_name
 25             , 'U'            -- operation (U)pdate
 26             , :NEW.id        -- tab_pk
 27             , :NEW.name      -- name from source_table
 28             , :NEW.nat       -- nat(ionality) from source_table
 29             , SYSDATE        -- timestamp (use timestamp datatype on 9i)
 30             );
 31    ELSIF DELETING THEN
 32      INSERT INTO audit_table
 33      VALUES ( v_id  -- aud_id
 34             , 'SOURCE_TABLE' -- tab_name
 35             , 'D'            -- operation (D)elete
 36             , :OLD.id        -- tab_pk
 37             , :OLD.name      -- name from source_table
 38             , :OLD.nat       -- nat(ionality) from source_table
 39             , SYSDATE        -- timestamp (use timestamp datatype on 9i)
 40             );
 41    END IF;    
 42  END;
 43  /

Trigger created.

SQL> INSERT INTO source_table values(1, 'Peter','GB');

1 row created.

SQL> UPDATE source_table SET nat = 'US' Where name = 'Peter';

1 row updated.

SQL> INSERT INTO source_table values(2,'Mark','FR');

1 row created.

SQL> INSERT INTO source_table values(3,'Frank','GB');

1 row created.

SQL> DELETE FROM source_table WHERE id = 2;

1 row deleted.

SQL> commit;

Commit complete.

SQL> SELECT *
  2    FROM audit_table
  3   ORDER BY timestamp;

   AUD_ID TAB_NAME             O    TAB_PK NAME       NA TIMESTAMP
--------- -------------------- - --------- ---------- -- --------------------
        1 SOURCE_TABLE         I         1 Peter      GB 17-AUG-2004 08:23:29
        2 SOURCE_TABLE         U         1 Peter      US 17-AUG-2004 08:23:33
        3 SOURCE_TABLE         I         2 Mark       FR 17-AUG-2004 08:23:38
        4 SOURCE_TABLE         I         3 Frank      GB 17-AUG-2004 08:23:43
        5 SOURCE_TABLE         D         2 Mark       FR 17-AUG-2004 08:23:46

SQL> 
MHE
Previous Topic: installation problems in Installing oracle9.2.0.4 in red hat 9i
Next Topic: ORA-6413 Connection not open
Goto Forum:
  


Current Time: Thu Jan 09 14:46:57 CST 2025