Triggers in MySQL
Triggers in MySQL
Play Videox
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.
NOTE: We should know that MySQL doesn't support statement-level triggers. It provides
supports for row-level triggers only.
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.
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.
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:
1. (BEFOR | AFTER) table_name (INSERT | UPDATE | DELETE)
Thus,
1. CREATE TRIGGER trigger_name
2. (AFTER | BEFORE) (INSERT | UPDATE | DELETE)
3. ON table_name FOR EACH ROW
4. BEGIN
5. --variable declarations
6. --trigger code
7. END;
Different types of MySQL Triggers (with
examples)
Difficulty Level : Medium
Last Updated : 04 Jul, 2019
Read
Discuss
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.
There are 6 different types of triggers in MySQL:
1. Before Update Trigger:
As the name implies, it is a trigger which enacts before an update is invoked. If we
write an update statement, then the actions of the trigger will be performed before the
update is implemented.
Example:
Considering tables:
create table customer (acc_no integer primary key,
cust_name varchar(20),
avail_balance decimal);
create table mini_statement (acc_no integer,
avail_balance decimal,
foreign key(acc_no) references customer(acc_no)
on delete cascade);
Inserting values in them:
insert into customer values (1000, "Fanny", 7000);
insert into customer values (1001, "Peter", 12000);
Trigger to insert (old) values into a mini_statement record (including account number
and available balance as parameters) before updating any record in customer
record/table:
delimiter //
create trigger update_cus
-> before update on customer
-> for each row
-> begin
-> insert into mini_statement values (old.acc_no,
old.avail_balance);
-> end; //
Making updates to invoke trigger:
delimiter;
update customer set avail_balance = avail_balance + 3000 where
acc_no = 1001;
update customer set avail_balance = avail_balance + 3000 where
acc_no = 1000;
Output:
select *from mini_statement;
+--------+---------------+
| acc_no | avail_balance |
+--------+---------------+
| 1001 | 12000 |
| 1000 | 7000 |
+--------+---------------+
2 rows in set (0.0007 sec)
The SQL standard defines two types of triggers: row-level triggers and statement-
level triggers.
A row-level trigger is activated for each row that is inserted, updated, or deleted. For
example, if a table has 100 rows inserted, updated, or deleted, the trigger is
automatically invoked 100 times for the 100 rows affected.
A statement-level trigger is executed once for each transaction regardless of how
many rows are inserted, updated, or deleted.
Advantages of triggers
Disadvantages of triggers
Triggers can only provide extended validations, not all validations. For simple
validations, you can use the NOT NULL, UNIQUE, CHECK and FOREIGN
KEY constraints.
Triggers can be difficult to troubleshoot because they execute automatically in the
database, which may not invisible to the client applications.
Triggers may increase the overhead of the MySQL Server.