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

MYSQL Triggers

The document outlines the use of triggers in a database, detailing the differences between BEFORE and AFTER triggers, along with their respective use cases. It provides examples of triggers for managing student grades based on marks and logging actions in a separate table. Additionally, it includes SQL commands for creating a student database, inserting values, and defining triggers for both insertion and updates.

Uploaded by

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

MYSQL Triggers

The document outlines the use of triggers in a database, detailing the differences between BEFORE and AFTER triggers, along with their respective use cases. It provides examples of triggers for managing student grades based on marks and logging actions in a separate table. Additionally, it includes SQL commands for creating a student database, inserting values, and defining triggers for both insertion and updates.

Uploaded by

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

Triggers

Event BEFORE AFTER

INSERT

UPDATE

DELETE

Security Alarm: When a door (table) is opened (INSERT), the alarm (trigger) goes off
(executes ac ons like sounding a siren and no fying the authori es).

Automa c Email: When you complete an online purchase (UPDATE order status), the system
automa cally sends you a confirma on email (trigger ac on).

Spreadsheet Formula: When you change a value in one cell (UPDATE), a formula in another
cell (trigger) automa cally recalculates the result.

OLD

 Refers to the exis ng row before the triggering event.

 Used in UPDATE and DELETE triggers.

 Not available in INSERT triggers (since there's no old row yet).

NEW

 Refers to the new row a er the triggering event.


 Used in INSERT and UPDATE triggers.

 Not available in DELETE triggers (since no new row is created).

Use BEFORE triggers when:

1. You want to modify data before it is wri en to the table


→ Example: auto-se ng mestamps, sani zing inputs, or adjus ng values.

2. You want to validate or prevent the opera on → You can use SIGNAL to raise an
error and stop the opera on if condi ons aren’t met.

3. You need access to and want to modify NEW values before they are stored.

Use AFTER triggers when:

1. You need to perform ac ons only a er the actual change is successfully made.
→ E.g., logging, no fica ons, interac ng with other tables that rely on the
updated data.

2. You don’t need to change the data being wri en, only react to it.

Trigger Type OLD Value NEW Value

BEFORE INSERT

AFTER INSERT

BEFORE UPDATE
AFTER UPDATE

BEFORE DELETE

AFTER DELETE

Use Case Trigger Type

Modify inserted/updated data BEFORE

Validate and prevent bad data BEFORE

Log the opera on AFTER

Chain ac ons a er data is wri en AFTER

Depend on the success of the opera on AFTER

CREATE DATABASE studentgrade;

USE studentgrade;

CREATE TABLE student (

id INT AUTO_INCREMENT PRIMARY KEY,

name VARCHAR(100),

marks INT,
grade CHAR(1)

);

DELIMITER $$

CREATE TRIGGER before_student_insert

BEFORE INSERT ON student

FOR EACH ROW

BEGIN

IF NEW.marks >= 90 THEN

SET NEW.grade = 'A';

ELSEIF NEW.marks >= 75 THEN

SET NEW.grade = 'B';

ELSEIF NEW.marks >= 60 THEN

SET NEW.grade = 'C';

ELSE

SET NEW.grade = 'D';

END IF;

END $$

DELIMITER ;

DELIMITER $$

CREATE TRIGGER before_student_update

BEFORE UPDATE ON student

FOR EACH ROW

BEGIN
IF NEW.marks >= 90 THEN

SET NEW.grade = 'A';

ELSEIF NEW.marks >= 75 THEN

SET NEW.grade = 'B';

ELSEIF NEW.marks >= 60 THEN

SET NEW.grade = 'C';

ELSE

SET NEW.grade = 'D';

END IF;

END $$

DELIMITER ;

Insert Value

INSERT INTO student (name, marks) VALUES ('Amit', 85);

Check value for insert

SELECT * FROM student;

Update Value

UPDATE student SET marks = 92 WHERE id = 1;

Check value for update

SELECT * FROM student;

See all triggers in this database

SHOW TRIGGERS FROM studentgrade;


Use Case for AFTER Triggers

Create a log table

CREATE TABLE student_log (

log_id INT AUTO_INCREMENT PRIMARY KEY,

student_id INT,

ac on_type VARCHAR(10), -- 'INSERT' or 'UPDATE'

name VARCHAR(100),

marks INT,

grade CHAR(1),

ac on_ me TIMESTAMP DEFAULT CURRENT_TIMESTAMP

);

Create AFTER INSERT Trigger

DELIMITER $$

CREATE TRIGGER a er_student_insert

AFTER INSERT ON student

FOR EACH ROW

BEGIN

INSERT INTO student_log (student_id, ac on_type, name, marks, grade)

VALUES (NEW.id, 'INSERT', NEW.name, NEW.marks, NEW.grade);

END $$
DELIMITER ;

Create AFTER UPDATE Trigger

DELIMITER $$

CREATE TRIGGER a er_student_update

AFTER UPDATE ON student

FOR EACH ROW

BEGIN

INSERT INTO student_log (student_id, ac on_type, name, marks, grade)

VALUES (NEW.id, 'UPDATE', NEW.name, NEW.marks, NEW.grade);

END $$

DELIMITER ;

You might also like