0% found this document useful (0 votes)
66 views21 pages

MySQL Triggers

MySQL triggers allow automated execution of code before or after data changes in a database table. There are four types of triggers: before insert, before update, after insert, and after update. Triggers are defined using CREATE TRIGGER syntax specifying the trigger name, table name, and trigger action/timing. Triggers can perform validation checks, calculations, and log data changes to other tables. Examples demonstrate triggers firing before or after inserts or updates to modify or log data.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views21 pages

MySQL Triggers

MySQL triggers allow automated execution of code before or after data changes in a database table. There are four types of triggers: before insert, before update, after insert, and after update. Triggers are defined using CREATE TRIGGER syntax specifying the trigger name, table name, and trigger action/timing. Triggers can perform validation checks, calculations, and log data changes to other tables. Examples demonstrate triggers firing before or after inserts or updates to modify or log data.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

MySQL Triggers

Introduction
 A trigger is a MySQL program, which can be
executed automatically by the Oracle server
when an event is raised. MySQL Triggers
executes before or after an insert, update or
delete statement.
 MySQL Triggers are fired, when

a appropriate event occurs for the table.


MySQL Triggers are used to perform
verification checks on data values
for insertion and perform calculations.
Types of Triggers
 MySQL Triggers are of 4 types:
 Before Insert Trigger
 Before Update Trigger
 After Insert Trigger
 After Update Trigger
Syntax
 create trigger <trigger_name>
before insert
on <table_name> for each row
 BEGIN
 — variable declarations
 — trigger code
 END;
 trigger_name => The name of the trigger to be
created.
 table_name => The accurate table in the
database.
Triggers Functioning
 Database table – Set of database elements.
 User defined function – Which are defined by

the user.
 Error log – Error that created during error

logs.
 OSSES agent – Connection between specific

client and server.


Example
 In the above example, before insert, trigger
will fire the condition before inserting a
record inside the table or on the table, and
update command is performed on the column
salary(Set Salary=Salary-400 where
perks>500). Then the salary of all the
employee changes as all the employee perks
are greater then the applied condition.
Types
 MySQL Before update trigger will fired the
trigger before performing update records
values inside the table.
Syntax
 create trigger <trigger_name>
before update
on <table_name> for each row
 BEGIN
 — variable declarations
 — trigger code
 END;
 trigger_name => The name of the trigger to be
create.
 table_name => The accurate table in the database
Example
 In the above example, before update, MySQL
Triggers will fire the condition before
updating a record inside the table or on the
table, and update command is performed on
the column salary(update
if Salary=Salary>11000 and perks>500 then
change to 10000). Then the salary of all the
employee changes, as all the employee perks
are greater then the applied condition.
Types
 MySQL After Insert trigger will fire the trigger
after performing insert records values inside
the table.
Syntax
 create trigger <trigger_name>
AFTER INSERT
ON <table_name> FOR EACH ROW
 BEGIN
 — variable declarations
 — trigger code
 END;
 trigger_name =>The name of the trigger to be
create.
 table_name => The accurate table in the
database.
Example
 In the above example, after insert, trigger will
fire the condition after inserting a record
inside the table or on the table i.e, after
creating student_insert table, it will fire on
the student_log1 table and all the values will
display on student_log1 table which was
inserted in student_table. 
Types
 MySQL After update trigger of MySQL
Triggers will fire the trigger after performing
insert  records values inside the table.
Syntax
 create trigger <trigger_name>
AFTER UPDATE
ON <table_name> FOR EACH ROW
 BEGIN
 — variable declarations
 — trigger code
 END;

trigger_name => The name of the trigger to be create.


 table_name => The accurate table in the database.
Example
 In the above example, after update, trigger
will fire the condition after update a record
inside the table or on the table. i.e, after
creating student_update table it will fire on
the student_log2 table and all the values will
display on student_log2 table which was
inserted in student_table02.
Thank You

You might also like