0% found this document useful (0 votes)
27 views5 pages

Kirthana DBMS Ex8

This document demonstrates how to create triggers in a database using PL/SQL. It shows how to create triggers that fire before and after delete, insert, update operations on a student table. Logging of the operations is done to a transaction log table. The triggers successfully execute for all operations, logging the user, process and timestamp of the operation to the translog table. This confirms that triggers can be used to perform operations in the database before or after data modifications.

Uploaded by

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

Kirthana DBMS Ex8

This document demonstrates how to create triggers in a database using PL/SQL. It shows how to create triggers that fire before and after delete, insert, update operations on a student table. Logging of the operations is done to a transaction log table. The triggers successfully execute for all operations, logging the user, process and timestamp of the operation to the translog table. This confirms that triggers can be used to perform operations in the database before or after data modifications.

Uploaded by

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

CS22311-DATABASE MANAGEMENT SYSTEM LABORATORY

DATE:
EX.NO:

TRIGGERS

AIM:
To create and perform triggers in the database using PL/SQL.

(A.) Creating trigger before delete operation:

mysql> create table translog(user varchar(20),process varchar(20),dt date);


mysql> select * from student;
+ + + +
| roll | name | marks |
+ + + +
| 101 | ann | 200 |
| 102 | jose | 300 |
| 103 | john | 100 |
| 104 | maha | 500 |
| 105 | pavitra | 300 |
| 106 | priya | 600 |
+ + + +
6 rows in set (0.03 sec)

mysql> delimiter $
mysql> create trigger trigger1 before delete on student
-> for each row begin
-> insert into translog values(user(),'values deleted',now());
-> end $

mysql> delete from student where roll=102;$


mysql> select * from translog;$
+ + + +
| user | process | dt |
+ + + +
| root@localhost | values deleted | 2019-03-28 |
+ + + +
1 row in set (0.00 sec)

mysql> select * from student;


+ + + +
| roll | name | marks |
+ + + +
| 101 | ann | 200 |
| 103 | john | 100 |
| 104 | maha | 500 |

ROLL NO:2127220501175 Page | 1


| 105 | pavitra | 300 |
| 106 | priya | 600 |
+ + + +
4 rows in set (0.00 sec)

(B.)Creating trigger after delete operation:

mysql> drop trigger trigger1;


mysql> create trigger trigger2 after delete on student
-> for each row begin
-> insert into translog values(user(),'values deleted',now());
-> end$
mysql> delete from student where roll=103;

mysql> select * from translog;


+ + + +
| user | process | dt |
+ + + +
| root@localhost | values deleted | 2019-03-28 |
| root@localhost | values deleted | 2019-03-28 |
+ + + +
2 rows in set (0.00 sec)
mysql> select * from student;
+ + + +
| roll | name | marks |
+ + + +
| 101 | ann | 200 |
| 104 | maha | 500 |
| 105 | pavitra | 300 |
| 106 | priya | 600 |
+ + + +
3 rows in set (0.00 sec)

(C.) Creating trigger before insert operation:

mysql> drop trigger trigger2;


mysql> create trigger trigger3 before insert on student
-> for each row begin
-> insert into translog values(user(),'values added',now());
-> end$

mysql> insert into student values(107,"harsh",200);

mysql> select * from translog;


+ + + +
| user | process | dt |
+ + + +

ROLL NO:2127220501175 Page | 2


| root@localhost | values deleted | 2019-03-28 |
| root@localhost | values deleted | 2019-03-28 |
| root@localhost | values added | 2019-03-28 |
+ + + +
3 rows in set (0.00 sec)
mysql> select * from student;
+ + + +
| roll | name | marks |
+ + + +
| 101 | ann | 200 |
| 104 | maha | 500 |
| 105 | pavitra | 300 |
| 106 | priya | 600 |
| 107 | harsh | 200 |
+ + + +
4 rows in set (0.00 sec)

(D.) Creating trigger after insert operation:

mysql> drop trigger trigger3;

mysql> create trigger trigger4 after insert on student


-> for each row begin
-> insert into translog values(user(),"values added",now());
-> end$

mysql> insert into student values(108,"merin",350);

mysql> select * from translog;


+ + + +
| user | process | dt |
+ + + +
| root@localhost | values deleted | 2019-03-28 |
| root@localhost | values deleted | 2019-03-28 |
| root@localhost | values added | 2019-03-28 |
| root@localhost | values added | 2019-03-28 |
+ + + +
4 rows in set (0.00 sec)
mysql> select * from student;
+ + + +
| roll | name | marks |
+ + + +
| 101 | ann | 200 |
| 104 | maha | 500 |
| 105 | pavitra | 300 |
| 106 | priya | 600 |
| 107 | harsh | 200 |

ROLL NO:2127220501175 Page | 3


| 108 | merin | 350 |
+ + + +
5 rows in set (0.01 sec)

(E.) Creating trigger before update operation:

mysql> drop trigger trigger4;

mysql> create trigger trigger5 before update on student


-> for each row begin
-> insert into translog values(user(),'values updated',now());
-> end$

mysql> update student set marks=800 where roll=105;

mysql> select * from translog;


+ + + +
| user | process | dt |
+ + + +
| root@localhost | values deleted | 2019-03-28 |
| root@localhost | values deleted | 2019-03-28 |
| root@localhost | values added | 2019-03-28 |
| root@localhost | values added | 2019-03-28 |
| root@localhost | values updated | 2019-03-28 |
+ + + +

mysql> select * from student;


+ + + +
| roll | name | marks |
+ + + +
| 101 | ann | 200 |
| 104 | maha | 500 |
| 105 | pavitra | 800 |
| 106 | priya | 600 |
| 107 | harsh | 200 |
| 108 | merin | 350 |
+ + + +
5 rows in set (0.00 sec)

(F.) Creating trigger after update operation:


mysql> drop trigger trigger5;
mysql> create trigger trigger6 after update on student
-> for each row begin
-> insert into translog values(user(),'values updated',now());
-> end$
mysql> update student set marks=100 where roll=108;

ROLL NO:2127220501175 Page | 4


mysql> select * from translog;
+ + + +
| user | process | dt |
+ + + +
| root@localhost | values deleted | 2019-03-28 |
| root@localhost | values deleted | 2019-03-28 |
| root@localhost | values added | 2019-03-28 |
| root@localhost | values added | 2019-03-28 |
| root@localhost | values updated | 2019-03-28 |
| root@localhost | values updated | 2019-03-28 |
+ + + +

mysql> select * from student;


+ + + +
| roll | name | marks |
+ + + +
| 101 | ann | 200 |
| 104 | maha | 500 |
| 105 | pavitra | 800 |
| 106 | priya | 600 |
| 107 | harsh | 200 |
| 108 | merin | 100 |
+ + + +
5 rows in set (0.00 sec)

RESULT:
Thus the triggers have been successfully executed using PL/SQL.

ROLL NO:2127220501175 Page | 5

You might also like