TRIGGER
TRIGGER
Trigger
Trigger is a statement that a system executes automatically when there is any
modification to the database.
A trigger in MySQL is a set of SQL statements that reside in a system catalog. It is a special
type of stored procedure that is invoked automatically in response to an event. Each
trigger is associated with a table, which is activated on any DML statement such as INSERT,
UPDATE, or DELETE.
A trigger is called a special procedure because it cannot be called directly like a stored
procedure. The main difference between the trigger and procedure is that a trigger is called
automatically when a data modification event is made against a table. In contrast, a stored
procedure must be called explicitly.
Generally, triggers are of two types according to the SQL standard: row-level triggers and
statement-level triggers.
Row-Level Trigger: It is a trigger, which is activated for each row by a triggering statement
such as insert, update, or delete. For example, if a table has inserted, updated, or deleted
multiple rows, the row trigger is fired automatically for each row affected by
the insert, update, or delete statement.
Statement-Level Trigger: It is a trigger, which is fired once for each event that occurs on a
table regardless of how many rows are inserted, updated, or deleted.
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 in the table.
4. After Update: It is activated after the update of the data in the table.
5. Before Delete: It is activated before the data is removed from the table.
6. After Delete: It is activated after the deletion of data from the table.
When we use a statement that does not use INSERT, UPDATE or DELETE query to change
the data in a table, the triggers associated with the trigger will not be invoked.
• Faster application development. Because the database stores triggers, you do not
have to code the trigger actions into each database application.
• Global enforcement of business rules. Define a trigger once and then reuse it for any
application that uses the database.
• Easier maintenance. If a business policy changes, you need to change only the
corresponding trigger program instead of each application program.
• Improve performance in client/server environment. All rules run on the server before
the result returns.
o MySQL triggers do not allow to use of all validations; they only provide extended
validations. For example, we can use the NOT NULL, UNIQUE, CHECK and FOREIGN
KEY constraints for simple validations.
o Triggers are invoked and executed invisibly from the client application. Therefore, it
isn't easy to troubleshoot what happens in the database layer.
o Triggers may increase the overhead of the database server.
3 Mujahid Raza
Naming Conventions
Naming conventions are the set of rules that we follow to give appropriate unique names. It
saves our time to keep the work organize and understandable. Therefore, we must use a
unique name for each trigger associated with a table. However, it is a good practice to
have the same trigger name defined for different tables.
The following naming convention should be used to name the trigger in MySQL:
Thus,
Syntax : -
Delimiter //
CREATE TRIGGER trigger_name
(AFTER | BEFORE) (INSERT | UPDATE | DELETE)
ON table_name FOR EACH ROW
BEGIN
--variable declarations
--trigger code
END; //
We assume that you are habituated with "MySQL Stored Procedures". You can use the
following statements of MySQL procedure in triggers:
• Flow-of-control statements (IF, CASE, WHILE, LOOP, WHILE, REPEAT, LEAVE, ITERATE)
• Condition declarations
• Handler declarations
4 Mujahid Raza
The show trigger statement contains several columns in the result set. Let us explain each
column in detail.
o Trigger :- It is the name of the trigger that we want to create and must be unique
within the schema.
o Event :- It is the type of operation name that invokes the trigger. It can be either
INSERT, UPDATE, or DELETE operation.
o Statement :- It is the body of the trigger that contains logic for the trigger when it
activates.
o Timing :- It is the activation time of the trigger, either BEFORE or AFTER. It indicates
that the trigger will be invoked before or after each row of modifications occurs on
the table.
o Created :- It represents the time and date when the trigger is created.
o Definer :- It is the name of a user account that created the trigger and should be in
o Database Collation :- It determines the rules that compare and order the character
string. It is the collation of the database with which the trigger belongs.
5 Mujahid Raza
delimiter //
create trigger course_before_insert
before insert
on course for each row
begin
set new.create_date = sysdate();
end; //
delimiter //
create trigger course_before_insert1
before insert
on course1 for each row
begin
declare user_val varchar(50);
set new.create_date = sysdate();
select user() into user_val;
set new.user_info = user_val;
end; //
delimiter //
create trigger course_before_insert2
before insert
on course2 for each row
begin
declare user_val varchar(50);
set new.create_date = sysdate();
select user() into user_val;
set new.user_info = user_val;
insert into ref_course values(sysdate(), user_val);
end; //
record_insert_date record_insert_user
28-08-2023 root@localhost
28-08-2023 root@localhost
28-08-2023 root@localhost
delimiter //
create trigger to_update_others
before insert
on test1 for each row
begin
insert into test2 values('MRA', sysdate(), 78786);
insert into test3 values('MRA', sysdate(), 78786);
end; //
c1 c2 c3
John 28-08-2023 109803
John 28-08-2023 109803
c1 c2 c3
MRA 28-08-2023 78786
c1 c2 c3
MRA 28-08-2023 78786
delimiter //
create trigger to_update_others_table
before insert
on test1 for each row
begin
update test2 set c1 = 'abc' where c1 = 'MRA';
delete from test3 where c1 = 'MRA';
end; //
9 Mujahid Raza
AFTER DELETE
The AFTER DELETE Trigger in MySQL is invoked automatically whenever a delete event is fired on the
table.
Restrictions
o We can access the OLD rows but cannot update them in the AFTER DELETE trigger.
o We cannot access the NEW rows. It is because there are no NEW row exists.
o We cannot create an AFTER DELETE trigger on a VIEW.
delimiter //
after delete
begin
end; //
c1 c2 c3
After_delete 28-08-2023 57547
BEFORE DELETE
BEFORE DELETE Trigger in MySQL is invoked automatically whenever a delete operation is
fired on the table.
10 Mujahid Raza
delimiter //
create trigger before_delete_trigger
before delete
on test1 for each row
begin
insert into test3 values('After_delete', sysdate(), 57547);
end; //
delete from test1 where c1 = 'john';
select * from test3;
delimiter //
create trigger before_delete_trigger_observation1
before delete
on test11 for each row
begin
insert into test12(c1,c2,c3) values(old.c1,old.c2, old.c3);
end; //
insert into test11 values('Aligarh' ,sysdate(), 5346);
insert into test11 values('Delhi' ,sysdate(), 3528);
insert into test11 values('Agra' ,sysdate(), 7756);
insert into test11 values('Noida' ,sysdate(), 8928);
11 Mujahid Raza
c1 c2 c3
Aligarh 28-08-2023 5346
Delhi 28-08-2023 3528
Agra 28-08-2023 7756
Noida 28-08-2023 8928
c1 c2 c3
Agra 28-08-2023 7756
delimiter //
create trigger after_delete_trigger_observation2
after delete
on test11 for each row
begin
insert into test12(c1,c2,c3) values(old.c1,old.c2, old.c3);
end; //
c1 c2 c3
Agra 28-08-2023 7756
c1 c2 c3
Agra 28-08-2023 7756
Aligarh 28-08-2023 5346
Aligarh 28-08-2023 5346
12 Mujahid Raza
The AFTER UPDATE trigger in MySQL is invoked automatically whenever an UPDATE event is
fired on the table associated with the triggers.
delimiter //
create trigger after_update_trigger_
after update
on test11 for each row
begin
insert into test12(c1,c2,c3) values(old.c1,old.c2, old.c3);
end; //
c1 c2 c3
Delhi 28-08-2023 3528
Noida 28-08-2023 8928
c1 c2 c3
New York 28-08-2023 3528
Noida 28-08-2023 8928
c1 c2 c3
Agra 28-08-2023 7756
Aligarh 28-08-2023 5346
Aligarh 28-08-2023 5346
13 Mujahid Raza
BEFORE UPDATE
delimiter //
create trigger before_update_trigger_
before update
on test11 for each row
begin
insert into test12(c1,c2,c3) values(new.c1,new.c2, new.c3);
end; //
c1 c2 c3
New York 28-08-2023 3528
Rome 28-08-2023 8928
c1 c2 c3
Agra 28-08-2023 7756
Aligarh 28-08-2023 5346
Aligarh 28-08-2023 5346
Rome 28-08-2023 8928