Vous êtes sur la page 1sur 3

Assignment No : 5 Roll No : 25

--------------------------------------------------------------------------------
-------------------------------------
Aim : Write PL/SQL block to Implement Types of Triggers
--------------------------------------------------------------------------------
-------------------------------------
SQL*Plus: Release 9.0.1.0.1 - Production on Wed Sep 22 10:31:07 2010
(c) Copyright 2001 Oracle Corporation. All rights reserved.
Connected to:
Oracle9i Enterprise Edition Release 9.0.1.1.1 - Production
With the Partitioning option
JServer Release 9.0.1.1.1 - Production
SQL> SET SERVEROUTPUT ON;
SQL>CREATE OR REPLACE TRIGGER EMP_TRIG
2 BEFORE INSERT OR UPDATE OR DELETE
3 ON EMPLOYEE25 FOR EACH ROW
4 BEGIN
5 IF INSERTING THEN
6 IF(:NEW.EMP_SALARY<10000)THEN
7 RAISE_APPLICATION_ERROR(-20140,'SALARY SHOULD NOT BE LESS THAN 10000');
8 END IF;
9 END IF;
10 IF UPDATING THEN
11 IF(:OLD.EMP_SALARY<10000)THEN
12 RAISE_APPLICATION_ERROR(-20150,'SALARY SHOULD NOT BE LESS THAN 10000');
13 END IF;
14 END IF;
15 IF DELETING THEN
16 IF(:OLD.EMP_SALARY>10000)THEN
17 DBMS_OUTPUT.PUT_LINE('ENTRY IN TABLE IS DELETED SUCCESSFULLY');
18 END IF;
19 END IF ;
20 END;
21 /
Trigger created.
SQL> INSERT INTO EMPLOYEE25 VALUES(42,'ASHISH','PUNE','EMPLOYEE',5000,'21-FEB-09
');
INSERT INTO EMPLOYEE25 VALUES(42,'ASHISH','PUNE','EMPLOYEE',5000,'21-FEB-09')
*
ERROR at line 1:
ORA-20140: SALARY SHOULD NOT BE LESS THAN 10000
ORA-06512: at "SCOTT.EMP_TRIG", line 4
ORA-04088: error during execution of trigger 'SCOTT.EMP_TRIG'
SQL> UPDATE EMPLOYEE25 SET EMP_SALARY=8000 WHERE EMP_NAME='SWAPNIL';
UPDATE EMPLOYEE25 SET EMP_SALARY=8000 WHERE EMP_NAME='SWAPNIL'
*
ERROR at line 1:
ORA-20150: SALARY SHOULD NOT BE LESS THAN 10000
ORA-06512: at "SCOTT.EMP_TRIG", line 9
ORA-04088: error during execution of trigger 'SCOTT.EMP_TRIG'
SQL> DELETE FROM EMPLOYEE25 WHERE EMP_SALARY>10000;
ENTRY IN TABLE IS DELETED SUCCESSFULLY
1 row deleted.
SQL> CREATE TABLE EMPAUDIT(AUDIT_DATE VARCHAR2(20),AUDIT_USER VARCHAR2(20),
2 AUDIT_DESC VARCHAR2(20));
Table created.
SQL> CREATE OR REPLACE TRIGGER EMP25
2 AFTER INSERT OR UPDATE OR DELETE ON EMPLOYEE25
3 DECLARE
4 V_ACTION VARCHAR2(20);
5 BEGIN
6 IF INSERTING THEN
7 V_ACTION := 'ADDED EMPLOYEES';
8 ELSIF UPDATING THEN
9 V_ACTION := 'UPDATED EMPLOYEES';
10 ELSIF DELETING THEN
11 V_ACTION := 'DELETED EMPLOYEES';
12 END IF;
13 INSERT INTO EMPAUDIT VALUES(SYSDATE,USER,V_ACTION);
14 END;
15 /
Trigger created.

SQL> INSERT INTO EMPLOYEE25 VALUES(70,'VIPIN','MUMBAI','DIRECTOR',30000,'9-JA


1 row created.
SQL> INSERT INTO EMPLOYEE25 VALUES(71,'SAGAR','PUNE','EMPLOYEE',20000,'8-FEB-
1 row created.
SQL> SELECT * FROM EMPLOYEE25 ORDER BY EMP_ID ASC;
EMP_ID EMP_NAME ADDRESS EMP_POST EMP_SALARY JOIN_DATE
---------- ---------- ---------- ---------- ---------- ---------
25 SWAPNIL PUNE ASSISTANT 9999 01-JA
N-10
26 VISHAL MUMBAI EMPLOYEE 30000 02-FEB-
09
70 VIPIN MUMBAI DIRECTOR 30000 09-JA
N-08
71 SAGAR PUNE EMPLOYEE 30000 08-F
EB-09
SQL> UPDATE EMPLOYEE25 SET EMP_SALARY=30000 WHERE EMP_POST='EMPLOYEE';
2 rows updated.
SQL> SELECT * FROM EMPAUDIT;
AUDIT_DATE AUDIT_USER AUDIT_DESC
-------------------- -------------------- -------------------
22-SEP-10 SCOTT ADDED EMPLOYEES
22-SEP-10 SCOTT ADDED EMPLOYEES
22-SEP-10 SCOTT UPDATED EMPLOYEES
22-SEP-10 SCOTT DELETED EMPLOYEES