DDL Triggers in PL/SQL


******************************************************

CREATE OR REPLACE TRIGGER BOSSTGR
BEFORE DROP ON ATHARV.SCHEMA
BEGIN
   DBMS_OUTPUT.PUT_LINE('DROPPED');
END;

******************************************************

CREATE TABLE ACCOUNTS(ACNO INT PRIMARY KEY,NAME VARCHAR(20),BALANCE INT);

CREATE TABLE TRANSACTIONS(TID INT PRIMARY KEY,ACNO REFERENCES ACCOUNTS(ACNO),TDATE DATE,AMOUNT INT,BALANCE INT );

DECLARE
A ACCOUNTS.ACNO%TYPE;
N ACCOUNTS.NAME%TYPE;
B ACCOUNTS.BALANCE%TYPE;
BEGIN
 INSERT INTO ACCOUNTS VALUES(&A,&N,&B);
COMMIT;
END;

 INSERT INTO ACCOUNTS VALUES(10,'A',50000);
 INSERT INTO ACCOUNTS VALUES(20,'B',75000);
 INSERT INTO ACCOUNTS VALUES(30,'C',10000);
 INSERT INTO ACCOUNTS VALUES(40,'D',92000);
 INSERT INTO ACCOUNTS VALUES(50,'E',48000);

******************************************************

PURPOSE : IF ANY KIND OF OPERATION IS PERFORMED IN ACCOUNTS TABLE
          IT SHOULD BE UPDATED IN TRANSCATION TABLE

CREATE SEQUENCE AC_SEQ;

CREATE OR REPLACE TRIGGER ACTR2
BEFORE UPDATE ON ACCOUNTS FOR EACH ROW
BEGIN
INSERT INTO TRANSACTIONS
        (TID,ACNO,TDATE,AMOUNT,BALANCE)
VALUES (AC_SEQ.NEXTVAL,:NEW.ACNO,SYSDATE,ABS(:NEW.BALANCE - :OLD.BALANCE),:NEW.BALANCE);
END;





















Previous
Next Post »