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

MySql Assignment 5

The document discusses implementing various row and statement level triggers in MySQL. It describes creating before and after triggers on insert, update and delete operations on an employee table to track changes. Sample trigger creation queries and expected outputs are provided.
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)
8 views

MySql Assignment 5

The document discusses implementing various row and statement level triggers in MySQL. It describes creating before and after triggers on insert, update and delete operations on an employee table to track changes. Sample trigger creation queries and expected outputs are provided.
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/ 11

Name: Parth Kiran Kulat

Roll No. 222036


PRN: 22210410

ASSIGNMENT 5
AIM: -
Database Trigger (Row level and Statement level triggers, Before and After
Triggers): Write a database trigger on Employee 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 to a new table when the Employee table is
updated. Employee (employee no, employee name, join_date, designation, salary).

OBJECTIVE: -
To implement row and statement level trigger.

THEORY: -
In programs sometimes it is required to execute certain code followed by certain
events and this requirement can be achieved in PL/SQL through triggers.
Triggers are stored programs that are fired automatically when some event occurs. The
code to be fired can be defined as per the requirement.
Oracle has also provided the facility to mention the event upon which the trigger needs
to be fire and the timing of the execution.
Triggers are stored programs, which are automatically executed or fired when some
events occur.
Triggers are, in fact, written to be executed in response to any of the following events −
1.A database manipulation (DML) statement (DELETE, INSERT, or UPDATE)
2.A database definition (DDL) statement (CREATE, ALTER, or DROP).
3.A database operation (SERVERERROR, LOGON, LOGOFF, STARTUP, or SHUTDOWN).
Triggers can be defined on the table, view, schema, or database with which the
event is associated.

The syntax for creating a trigger is −


CREATE [OR REPLACE ] TRIGGER trigger_name
{BEFORE | AFTER | INSTEAD OF }
{INSERT [OR] | UPDATE [OR] | DELETE}
[OF col_name]
ON table_name
[REFERENCING OLD AS
o NEW AS n] [FOR
EACH ROW]

COLLEGE: VIIT, Pune.


WHEN (condition)
DECLARE
Declaration-statements BEGIN
Executable-statements
EXCEPTION
Exception-handling-statements
END;

COLLEGE: VIIT, Pune.


Syntax Explanation:
BEFORE/ AFTER will specify the event timings. INSERT/UPDATE/LOGON/CREATE/etc.
will specify the event for which the trigger needs to be fired.
ON clause will specify on which object the above mentioned event is valid. For
example, this will be the table name on which the DML event may occur in the case
of DML Trigger.
Command "FOR EACH ROW" will specify the ROW level trigger.
WHEN clause will specify the additional condition in which the trigger needs to
fire.
The declaration part, execution part, exception handling part is same as that of the
other PL/SQL blocks. Declaration part and exception handling part are optional.

BENEFITS: -
1. Triggers can be written for the following purposes
2. Generating some derived column values automatically
3. Enforcing referential integrity
4.Event logging and storing information on table access
5. Auditing
6. Synchronous replication of tables
7. Imposing security authorizations 8.
Preventing invalid transactions

INPUT: -
TABLE EMPLOYEE_
Select * from employee_; TABLE
EMPLOYEE_TABLE
Create Table employee_table(employee_no int(5),employee_name
varchar(20),join_date varchar(20),designation varchar(20),salary int(10));
Select * from employee_table;

After INSERT
delimiter /
create trigger emp
AFTER INSERT on employee_ for each
row
begin
insert into employee_table
values(NEW.employee_no,NEW.employee_name,NEW.join_date,NE
W.designation,NEW.salary);
end;
/

COLLEGE: VIIT, Pune.


BEFORE DELETE
delimiter /
create trigger emp10
BEFORE DELETE on employee_ for each
row
begin
DELETE FROM EMPLOYEE_TABLE where
OLD.employee_no=EMPLOYEE_TABLE.employee_no; end;
/

AFTER DELETE
delimiter /
create trigger emp6
AFTER DELETE on employee_ for
each row
begin
DELETE FROM EMPLOYEE_TABLE where
OLD.employee_no=employee_table.employee_no; end;
/

BEFORE UPDATE
Delimiter /
Create trigger emp2
BEFORE UPDATE on employee_ for each
row
Begin
UPDATE EMPLOYEE_TABLE SET
employee_name=old.employee_name,join_date=old.join_date,desi
gnation=old.designation,salary=old.salary,employee_no=old.emplo yee_no where
employee_no=old.employee_no;
End;
/

AFTER UPDATE
delimiter /
create trigger emp11
AFTER UPDATE on employee_ for each
row
begin
UPDATE EMPLOYEE_TABLE SET
employee_name=NEW.employee_name,join_date=NEW.join_date,d
esignation=NEW.designation,salary=NEW.salary,employee_no=NE W.employee_no
where employee_no=old.employee_no;
COLLEGE: VIIT, Pune.
end;
/

INSERT, DELETE, UPDATE A ROW


Insert into employee_
values(11,'URAVI','11/10/1999','PUNE',40000); UPDATE
EMPLOYEE_ SET SALARY=30000 WHERE EMPLOYEE_NO=11;
delete from employee_ where employee_no=12; delete
from employee_ where employee_no=13; UPDATE
EMPLOYEE_ SET SALARY=200000 WHERE EMPLOYEE_NO=8;

DISPLAY THE TABLE


Select * from employee_; Select *
from employee_table;

OUTPUT: -

COLLEGE: VIIT, Pune.


COLLEGE: VIIT, Pune.
COLLEGE: VIIT, Pune.
Created Tables:
● Query (1):
○ CREATE TABLE EMPLOYEE_TABLE(
○ -> EMPLOYEE_NO INT,
○ -> EMPLOYEE_NAME VARCHAR(30),
○ -> JOIN_DATE VARCHAR(20),
○ -> DESIGNATION VARCHAR(30),
○ -> SALARY INT);

● Query (2):
○ CREATE TABLE Employee (
○ -> employee_no VARCHAR(255),
○ -> employee_name VARCHAR(255),
○ -> Join_date DATE,
○ -> designation VARCHAR(255),
○ -> salary INT
○ -> );

COLLEGE: VIIT, Pune.


Assignment Outcomes:
1. Trigger on INSERT AFTER:
Query:
● CREATE TRIGGER EMP_USER
● -> AFTER INSERT ON Employee
● -> FOR EACH ROW
● -> BEGIN
● -> INSERT INTO EMPLOYEE_TABLE
● -> VALUES(NEW.EMPLOYEE_NO, NEW.EMPLOYEE_NAME, NEW.JOIN_DATE,
NEW.DESIGNATION, NEW.SALARY);
● -> END;
● -> //

Output:

2. Trigger on INSERT BEFORE:


Query:
● CREATE TRIGGER validate_salary
● -> BEFORE INSERT ON Employee
● -> FOR EACH ROW
● -> BEGIN
● -> IF NEW.salary <= 0 THEN
● -> SIGNAL SQLSTATE '45000' SET message_text = 'Salary cannot 0 or less than zero';
● -> END IF;
● -> END;
● -> //

Output:

3. Trigger on UPDATE BEFORE


Query:
● delimiter //
● create trigger bef_update_trig
● -> BEFORE UPDATE ON Employee
● -> FOR EACH ROW
● -> BEGIN
● -> IF NEW.salary < 0 OR NEW.employee_name = NULL THEN
● -> SIGNAL SQLSTATE '45000' SET message_text = 'Salary cannot be less than or equal to
COLLEGE: VIIT, Pune.
zero;
● -> END IF;
● -> END;
● ->//

Output:

4. Trigger of AFTER UPDATE:


Query:
● CREATE TRIGGER AFTER_UPDATE
● -> AFTER UPDATE ON Employee
● -> FOR EACH ROW
● -> BEGIN
● -> INSERT INTO UPDATION_TABLE VALUES
● -> (CAST(OLD.salary AS UNSIGNED), CAST(NEW.salary AS UNSIGNED), NOW());
● -> END;
● -> /

Output:

5. Trigger on AFTER DELETE:


Query:
● CREATE TRIGGER update_history
● AFTER DELETE ON Employee
● FOR EACH ROW
● BEGIN
● INSERT INTO EMPLOYEE_TABLE
● VALUES (OLD.employee_no, OLD.employee_name, OLD.join_date, OLD.designation,
OLD.salary);
● END; //

COLLEGE: VIIT, Pune.


Output:

6. Trigger ON BEFORE DELETE:


Query:
● CREATE TRIGGER before_delete
● -> BEFORE DELETE ON Employee
● -> FOR EACH ROW
● -> BEGIN
● -> IF OLD.designation = 'PUNE' THEN
● -> SIGNAL SQLSTATE '45000' SET message_text = 'Cannot Delete The Record For The
Employees Whose Designation Is in Pune!!!';
● -> END IF;
● -> END;
● -> //

Output:

COLLEGE: VIIT, Pune.

You might also like