0% found this document useful (0 votes)
9 views7 pages

DBMS 8 Exp

DBMS 8 exp

Uploaded by

abinayadev04
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)
9 views7 pages

DBMS 8 Exp

DBMS 8 exp

Uploaded by

abinayadev04
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/ 7

Exp.

No: 8 TRIGGERS
Date:

Aim:
To create, implement and execute triggers in MySQL.

Triggers:
A MySQL trigger is a stored program (with queries) which is executed
automatically to respond to a specific event such as insertion, updation or
deletion occurring in a table.
Parts of Triggers:
The trigger has three basic parts:
a. Triggering event (or) statement.
b. Trigger restriction.
c. Trigger action (or) Trigger body.
a. Triggering Event (or) Statement:
It specifies the DML statement and fires the triggers body. It also
specifies the table to which the trigger is associated.
b. Trigger Restriction:
It specifies the restrictions on the trigger to be achieved.
c. Trigger Action (or) Trigger Body:
It is a PL/SQL block that is executed when the triggering statement is
used.
Types of Triggers:
1. Before Insert: It is activated before the insertion of data into the table.
2. After Insert: It is activated after the insertion of data into the table.
3. Before Update: It is activated before the update of data into the table.
4. After Update: It is activated after the update of data into the table.
5. Before Delete: It is activated before the deletion of data into the table.
6. After Delete: It is activated after the deletion of data into the table.
Syntax for creating a trigger:
CREATE TRIGGER trigger_name
{BEFORE | AFTER} {INSERT | UPDATE | DELETE}
ON table_name FOR EACH ROW
trigger_body;

Query Execution:
Create the table and inserting a values into it:
USE test;
CREATE TABLE Employee (
EmployeeID INT PRIMARY KEY,
Name VARCHAR(255),
Salary DECIMAL(10, 2)
);
INSERT INTO Employee (EmployeeID, Name, Salary)
VALUES
(1, 'Madhumitha', 50000),
(2, 'mohithaa', 60000),
(3, 'karthika', 55000),
(4, 'abi', 62000),
(5, 'sanga', 58000),
(6, 'pavani', 63000);

CREATE TABLE Employee_Log (


LogID INT AUTO_INCREMENT PRIMARY KEY,
EmployeeID INT,
Name VARCHAR(255),
Salary DECIMAL(10, 2));
After insert:
Create the trigger:
DELIMITER //
CREATE TRIGGER after_insert_trigger
AFTER INSERT ON Employee
FOR EACH ROW
BEGIN
INSERT INTO Employee_Log (EmployeeID, Name, Salary)
VALUES (NEW.EmployeeID, NEW.Name, NEW.Salary);
END;
//
DELIMITER ;

Inserting the Rows:


INSERT INTO Employee VALUES (8, 'bb', 45000);
select * from Employee_Log;

Output:

Before insert:
DELIMITER //
CREATE TRIGGER before_insert_trigger
BEFORE INSERT ON Employee
FOR EACH ROW
BEGIN
IF NEW.Salary < 50000 THEN
SET NEW.Salary = 50000; -- Set minimum salary to 50000
END IF;
END;
//
DELIMITER ;
INSERT INTO Employee VALUES (7, 'bb', 45000);
SELECT * FROM Employee;

Output:

Before update:
DELIMITER //
CREATE TRIGGER before_update_trigger
BEFORE UPDATE ON Employee
FOR EACH ROW
BEGIN
IF NEW.Salary < 50000 THEN
SET NEW.Salary = 50000;
END IF;
END;
//
DELIMITER ;
Output:

After update:
DELIMITER //
CREATE TRIGGER after_update_trigger
AFTER UPDATE ON Employee
FOR EACH ROW
BEGIN
INSERT INTO Employee_Log (EmployeeID, Name, Salary)
VALUES (NEW.EmployeeID, NEW.Name, NEW.Salary);
END;
//
DELIMITER ;
UPDATE Employee SET Salary = 45000 WHERE EmployeeID = 1;

Output:
Before delete:
DELIMITER //
CREATE TRIGGER before_delete_trigger
BEFORE DELETE ON Employee
FOR EACH ROW
BEGIN
INSERT INTO Employee_Log (EmployeeID, Name, Salary, Action)
VALUES (OLD.EmployeeID, OLD.Name, OLD.Salary, 'BEFORE DELETE');
END;
//
DELIMITER ;

Output:

After delete:
DELIMITER //
CREATE TRIGGER after_delete_trigger
AFTER DELETE ON Employee
FOR EACH ROW
BEGIN
INSERT INTO Employee_Log (EmployeeID, Name, Salary, Action)
VALUES (OLD.EmployeeID, OLD.Name, OLD.Salary, 'DELETE');
END;
//
DELIMITER ;

Output:

Result:
Thus, creating, implementing and executing triggers in MySQL was
executed and verified successfully.

You might also like