0% found this document useful (0 votes)
36 views

Dbms 7 Trigger

Uploaded by

wacih58121
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Dbms 7 Trigger

Uploaded by

wacih58121
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

1. Write a update, delete trigger on clientmstr table.

The System should keep


track of the records that ARE BEING updated or deleted. The old value of updated or
deleted records should be added in audit_trade table. (separate implementation
using both row and statement triggers)

SQL> create table client_mstr_55 (accno number(4), name varchar(10));

Table created.
SQL> select * from client_mstr_55;

ACCNO NAME
---------- ----------
1001 RAJU
1002 PARAG
1004 BABURAO
1005 CHAITYA
1006 SAIRAJ

SQL> Create or replace trigger t2 after update or delete on client_mstr_55


2 for each row
3 Declare
4 op varchar(10);
5 Begin
6 if updating then
7 op:='update';
8 end if;
9 if deleting then
10 op:='Delete';
11 end if;
12 insert into audit_trade_55 values(:old.accno,op);
13 End;
14 /

SQL> update client_mstr_55 set name = 'RAJU' Where accno = 1001;

1 row updated.

SQL> update client_mstr_55 set name = 'BABURAO' Where accno = 1004;

1 row updated.

SQL> SELECT * FROM audit_trade_55;

ACCNO OP
---------- ----------
1001 update
1004 update

SQL> delete from client_mstr_55 where accno = 1003;

1 row deleted.

SQL> select * from audit_trade_55;

ACCNO OP
---------- ----------
1001 update
1004 update
1003 Delete
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
------------------------------

2. Write a before trigger for Insert, update event considering following


requirement:
Emp(e_no, e_name, salary)
I) Trigger action should be initiated when salary is tried to be inserted is less
than Rs. 50,000/-
II) Trigger action should be initiated when salary is tried to be updated for value
less than Rs. 50,000/-
Action should be rejection of update or Insert operation by displaying appropriate
error message. Also the new values expected to be inserted will be stored in new
table Tracking(e_no, salary).

SQL> create table emp_55(e_no NUMBER,e_name VARCHAR(10), salary NUMBER);


SQL> CREATE TABLE tracking_55(e_no NUMBER, salary NUMBER);

Table created.

SQL> CREATE OR REPLACE TRIGGER t3


2 BEFORE INSERT ON emp_55
3 FOR EACH ROW
4 BEGIN
5 IF :NEW.salary < 50000 THEN
6 RAISE_APPLICATION_ERROR(-20001, 'Error: Salary cannot be less than Rs.
50,000/-');
7 ELSE
8 INSERT INTO tracking_55 (e_no, salary) VALUES
(:NEW.e_no, :NEW.salary);
9 END IF;
10 END;
11 /

Trigger created.

SQL> INSERT INTO emp_55 VALUES (6,'BABU',25000);


INSERT INTO emp_55 VALUES (6,'BABU',25000)
*
ERROR at line 1:
ORA-20001: Error: Salary cannot be less than Rs. 50,000/-
ORA-06512: at "SYSTEM.T3", line 3
ORA-04088: error during execution of trigger 'SYSTEM.T3'

SQL> select * from emp_55;

E_NO E_NAME SALARY


---------- ---------- ----------
1 OM 60000
2 PRAG 50000
3 CHAITU 250000
4 POTE 60000
5 SAI 560000

SQL> INSERT INTO emp_55 VALUES (6,'HARSH',70000);

1 row created.

SQL> SELECT * FROM tracking_55;

E_NO SALARY
---------- ----------
6 20000
6 70000

SQL> CREATE OR REPLACE TRIGGER t3


2 BEFORE UPDATE OR INSERT ON emp_55
3 FOR EACH ROW
4 BEGIN
5 IF :NEW.salary < 50000 THEN
6 INSERT INTO tracking_55 (e_no, salary) VALUES
(:NEW.e_no, :NEW.salary);
7 RAISE_APPLICATION_ERROR(-20001, 'Error: Salary cannot be less than Rs.
50,000/-');
8 END IF;
9 END;
10 /
Trigger created.

SQL> update emp_55 set salary = 30000 where e_no = 1;


update emp_55 set salary = 30000 where e_no = 1
*
ERROR at line 1:
ORA-20001: Error: Salary cannot be less than Rs. 50,000/-
ORA-06512: at "SYSTEM.T3", line 4
ORA-04088: error during execution of trigger 'SYSTEM.T3'

SQL> update emp_55 set salary = 300000 where e_no = 1;

1 row updated.

-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
------------------------
3. Write a Database trigger for following requirements:
Employee salary of last three month is stored in the emp_sal table.
emp_sal(emp_no, sal1,sal2,sal3)
before inserting salary into emp_sal table, if salary of employee in any of the
last three month is greater than Rs. 50,000/- then entry of average salary along
with emp_no needs to be inserted into new table emp_new(emp_no, avg_sal).
SQL> CREATE TABLE emp_sal_55 (e_no NUMBER,sal1 NUMBER,sal2 NUMBER,sal3 NUMBER);

Table created.

SQL> CREATE TABLE emp_new_55 (e_no NUMBER, avg_sal NUMBER);


Table created.

LINE/COL ERROR
-------- -----------------------------------------------------------------
10/54 PLS-00049: bad bind variable 'NEW.EMP_NO'
SQL> CREATE OR REPLACE TRIGGER t4
2 BEFORE INSERT ON emp_sal_55
3 FOR EACH ROW
4 DECLARE
5 avg_salary NUMBER;
6 BEGIN
7 -- Calculate the average salary
8 avg_salary := (:NEW.sal1 + :NEW.sal2 + :NEW.sal3) / 3;
9
10 -- Check if any of the salaries is greater than 50000
11 IF :NEW.sal1 > 50000 OR :NEW.sal2 > 50000 OR :NEW.sal3 > 50000 THEN
12 -- Insert into emp_new_55 table
13 INSERT INTO emp_new_55 (e_no, avg_sal) VALUES (:NEW.e_no, avg_salary);
14 END IF;
15 END;
16 /

Trigger created.

SQL> INSERT INTO emp_sal_55 VALUES (1,30000,50000,40000);

1 row created.

SQL> INSERT INTO emp_sal_55 VALUES (2,30000,10000,4000);

1 row created.

SQL> INSERT INTO emp_sal_55 VALUES (3,13000,50000,4000);

1 row created.

SQL> INSERT INTO emp_sal_55 VALUES (4,13000,5000,4000);

1 row created.

SQL> SELECT * FROM emp_sal_55;

E_NO SAL1 SAL2 SAL3


---------- ---------- ---------- ----------
1 30000 50000 40000
2 30000 10000 4000
3 13000 50000 4000
4 13000 5000 4000

SQL> select * from emp_new_55;

no rows selected

SQL> INSERT INTO emp_sal_55 VALUES (5,130000,50000,40000);

1 row created.

SQL> INSERT INTO emp_sal_55 VALUES (5,13000,60000,70000);


1 row created.

SQL> INSERT INTO emp_sal_55 VALUES (5,30000,60000,7000);

1 row created.

SQL> SELECT * FROM emp_new_55;

E_NO AVG_SAL
---------- ----------
5 73333.3333
5 47666.6667
5 32333.3333

You might also like