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

Triggers in dbms

The document describes the creation and functionality of triggers in a database management system (DBMS) using SQL. It includes examples of triggers that enforce salary constraints during insert and update operations, as well as the implementation of backup and audit tables to maintain historical records of changes. The document also demonstrates how to modify triggers to include conditions for updating records and track transactions in an audit table.

Uploaded by

shravya.pattiri
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Triggers in dbms

The document describes the creation and functionality of triggers in a database management system (DBMS) using SQL. It includes examples of triggers that enforce salary constraints during insert and update operations, as well as the implementation of backup and audit tables to maintain historical records of changes. The document also demonstrates how to modify triggers to include conditions for updating records and track transactions in an audit table.

Uploaded by

shravya.pattiri
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 8

Triggers in dbms

SQL> create table emp1(eno number(3),ename varchar(5),salary number(7),dept


varchar(4));

Table created.

SQL> create or replace trigger emp1_trigger


2 before insert or update on emp1 for each row
3 begin
4 if(:new.salary<=0) then
5 raise_application_error(-20343,'salary cannot be less than 0');
6 end if;
7 end;
8 /

Trigger created.

SQL> insert into emp1 values(111,'A',0,'CSE');


insert into emp1 values(111,'A',0,'CSE')
*
ERROR at line 1:
ORA-20343: salary cannot be less than 0
ORA-06512: at "XE.EMP1_TRIGGER", line 3
ORA-04088: error during execution of trigger 'XE.EMP1_TRIGGER'

SQL> select * from emp1;

no rows selected.

SQL> insert into emp1 values(111,'A',1000,'CSE');

1 row created.

SQL> select * from emp1;

ENO ENAME SALARY DEPT


---------- ----- ---------- ----
111 A 1000 CSE

if we update salary as 0 then we get the same error

SQL> update emp1 set salary=0 where eno=111;


update emp1 set salary=0 where eno=111
*
ERROR at line 1:
ORA-20343: salary cannot be less than 0
ORA-06512: at "XE.EMP1_TRIGGER", line 3
ORA-04088: error during execution of trigger 'XE.EMP1_TRIGGER'

Back up db:

we need to create a back up for every insert or update table in a trigger so that i
will maintain a copy of the database tables which i have aleardy created.

SQL> create table emp1_copy(eno number(3),ename varchar(5),salary number(7),dept


varchar(4));

Table created.
Creating a Existing Trigger:

SQL> create or replace trigger emp1_trigger


2 before insert or update on emp1 for each row
3 begin
4 if(:new.salary<=0) then
5 raise_application_error(-20343,'salary cannot be less than 0');
6 end if;
7 insert into emp1_copy values(:new.eno, :new.ename, :new.salary, :new.dept);
8 end;
9 /

Trigger created.

SQL> select * from emp1;

ENO ENAME SALARY DEPT


---------- ----- ---------- ----
111 A 1000 CSE

SQL> delete from emp1;

1 row deleted.

Try to insert more rows:

SQL> insert into emp1 values(111,'A',10000,'CSE');

1 row created.

SQL> insert into emp1 values(222,'B',20000,'CSM');

1 row created.

SQL> insert into emp1 values(333,'c',30000,'IT');

1 row created.

SQL> insert into emp1 values(444,'D',40000,'ECE');

1 row created.

SQL> insert into emp1 values(555,'E',50000,'EEE');

1 row created.

SQL> insert into emp1 values(666,'F',60000,'ME');

1 row created.

SQL> select * from emp1;

ENO ENAME SALARY DEPT


---------- ----- ---------- ----
111 A 10000 CSE
222 B 20000 CSM
333 c 30000 IT
444 D 40000 ECE
555 E 50000 EEE
666 F 60000 ME

6 rows selected.

SQL> select * from emp1_copy;

ENO ENAME SALARY DEPT


---------- ----- ---------- ----
111 A 10000 CSE
222 B 20000 CSM
333 c 30000 IT
444 D 40000 ECE
555 E 50000 EEE
666 F 60000 ME

6 rows selected.

Rows are inserted parallelly to the copy table.

SQL> update emp1 set salary=55000 where eno=111;

1 row updated.

SQL> select * from emp1;

ENO ENAME SALARY DEPT


---------- ----- ---------- ----
111 A 55000 CSE
222 B 20000 CSM
333 c 30000 IT
444 D 40000 ECE
555 E 50000 EEE
666 F 60000 ME

6 rows selected.

SQL> select * from emp1_copy;

ENO ENAME SALARY DEPT


---------- ----- ---------- ----
111 A 10000 CSE
222 B 20000 CSM
333 c 30000 IT
444 D 40000 ECE
555 E 50000 EEE
666 F 60000 ME
111 A 55000 CSE

7 rows selected.

SQL> update emp1 set ename='x' where eno=111;

1 row updated.

SQL> select * from emp1;


ENO ENAME SALARY DEPT
---------- ----- ---------- ----
111 X 55000 CSE
222 B 20000 CSM
333 c 30000 IT
444 D 40000 ECE
555 E 50000 EEE
666 F 60000 ME

6 rows selected.

SQL> select * from emp1_copy;

ENO ENAME SALARY DEPT


---------- ----- ---------- ----
111 A 10000 CSE
222 B 20000 CSM
333 c 30000 IT
444 D 40000 ECE
555 E 50000 EEE
666 F 60000 ME
111 A 55000 CSE
111 X 55000 CSE

8 rows selected.

Back up table is always going to have a new entry for every insertion and updation
i.e we are having three entries with 111 with a new entry A,X and new record of
salary 55000.

ii) Apply the conditions ie, Insert, Update, Delete, Modify.

SQL> create table emp2(eno number(3),ename varchar(5),salary number(7),dept


varchar(4));

Table Created.
So this time only the salary attribute should be >0 ie :new.salary>0

SQL> create or replace trigger emp2_trigger


2 before insert or update on emp2 for each row
3 begin
4 if :new.salary<=0 then
5 raise_application_error(-20000,'salary should be greater than 0');
6 end if;
7 end;
8 /
Trigger created.

SQL> insert into emp2 values(222,'D',0,'CSE');


insert into emp2 values(222,'D',0,'CSE')
*
ERROR at line 1:
ORA-20000: salary should be greater than 0
ORA-06512: at "XE.EMP2_TRIGGER", line 3
ORA-04088: error during execution of trigger 'XE.EMP2_TRIGGER'

SQL> insert into emp2 values(222,'D',3000,'CSE');


1 row created.
SQL> select * from emp2;

ENO ENAME SALARY DEPT


---------- ----- ---------- ----
222 D 3000 CSE

Insert atleast 5 rows:

SQL> insert into emp2 values(333,'E',4000,'CSM');

1 row created.
SQL> insert into emp2 values(444,'F',5000,'ME');

1 row created.
SQL> select * from emp2;

ENO ENAME SALARY DEPT


---------- ----- ---------- ----
222 D 3000 CSE
222 D 3000 CSE
333 E 4000 CSM
444 F 5000 ME

modify the trigger for update:check the condition :new.salary> :old.salary


if you want to update the existing record than the new value is should be greater
than the existing value, check the condition is satisfied or not

SQL> create or replace trigger emp2_trigger


2 before insert or update on emp2 for each row
3 begin
4 if :new.salary<=0 then
5 raise_application_error(-20000,'salary should be greater than 0');
6 end if;
7 if updating and :new.salary<=:old.salary then
8 raise_application_error(-20000,'salary should be greater than older one');
9 end if;
10 end;
11 /

Trigger created.

SQL> update emp2 set salary=2000 where eno=333;


update emp2 set salary=2000 where eno=333
*
ERROR at line 1:
ORA-20000: salary should be greater than older one
ORA-06512: at "XE.EMP2_TRIGGER", line 6
ORA-04088: error during execution of trigger 'XE.EMP2_TRIGGER'

update the salary with the new one then check the record is existed in the database

SQL> update emp2 set salary=20000 where eno=333;

1 row updated.

SQL> select * from emp2;

ENO ENAME SALARY DEPT


---------- ----- ---------- ----
222 D 3000 CSE
222 D 3000 CSE
333 E 20000 CSM
444 F 5000 ME

suppose if you want to check the transaction after trigger

backup table for emp table for After Trigger event:

SQL>create table emp2_audit(trans_date date,trans_name varchar2(7),eno


number(3),ename varchar(5),salary number(7),dept varchar(4));

Table created.

SQL> create or replace trigger emp2_trigger1


2 after insert on emp2 for each row
3 Declare
4 Trans_name varchar2(7);
5 Begin
6 Trans_name:='insert';
7 insert into emp2_audit
values(sysdate,trans_name,:new.eno,:new.ename,:new.salary,:new.dept);
8 end;
9 /

Trigger created

SQL> insert into emp2 values(555,'G',6000,'IT');

1 row created.

SQL> select * from emp2;

ENO ENAME SALARY DEPT


---------- ----- ---------- ----
222 D 3000 CSE
222 D 3000 CSE
333 E 20000 CSM
444 F 5000 ME
555 G 6000 IT

SQL> select * from emp2_audit;

TRANS_DAT TRANS_N ENO ENAME SALARY DEPT


--------- ------- ---------- ----- ---------- ----
02-JUL-24 insert 555 G 6000 IT
one entry is also created for the audit table but with the date and the transaction
name is created

example:

insert into emp2_audit


values(sysdate,trans_name,:new.eno,:new.ename,:new.salary,:new.dept);

if you try to write the record as old instead of new than it shows the NULL values.
because their is no old values is recorded in the database system of transaction
name is created.
Let's modify the trigger again:

SQL> create or replace trigger emp2_trigger1


2 after insert or update on emp2 for each row
3 Declare
4 Trans_name varchar2(7);
5 Begin
6 if inserting then Trans_name:='insert';
7 else Trans_name:='update';
8 end if;
9 if inserting then
10 insert into emp2_audit
values(sysdate,trans_name,:new.eno,:new.ename,:new.salary,:new.dept);
11 else
12 insert into emp2_audit
values(sysdate,trans_name,:old.eno,:old.ename,:old.salary,:old.dept);
13 end if;
14 end;
15 /

Trigger created.

let's try to update something.

let's list the emp table

SQL> select * from emp2;

ENO ENAME SALARY DEPT


---------- ----- ---------- ----
222 D 3000 CSE
222 D 3000 CSE
333 E 20000 CSM
444 F 5000 ME
555 G 6000 IT

Remember that Trigger1 cannot be violated means that trigger1 condition must be
satisfied,if i want to update the salary must be greater than the older one.

SQL> update emp2 set salary=7000 where eno=555;

1 row updated.

SQL> select * from emp2;

ENO ENAME SALARY DEPT


---------- ----- ---------- ----
222 D 3000 CSE
222 D 3000 CSE
333 E 20000 CSM
444 F 5000 ME
555 G 7000 IT
SQL> select * from emp2_audit;

TRANS_DAT TRANS_N ENO ENAME SALARY DEPT


--------- ------- ---------- ----- ---------- ----
02-JUL-24 insert 555 G 6000 IT
02-JUL-24 update 555 G 6000 IT
SQL> update emp2 set salary=7500 where eno=555;

1 row updated.

SQL> select * from emp2_audit;

TRANS_DAT TRANS_N ENO ENAME SALARY DEPT


--------- ------- ---------- ----- ---------- ----
02-JUL-24 insert 555 G 6000 IT
02-JUL-24 update 555 G 6000 IT
02-JUL-24 update 555 G 7000 IT

You might also like