0% found this document useful (0 votes)
14 views3 pages

Trigger in SQL

Uploaded by

Mahesh Desai
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)
14 views3 pages

Trigger in SQL

Uploaded by

Mahesh Desai
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/ 3

GIRI’S TECH HUB PVT.

LTD-PUNE – 9175444433, 9049361265

Trigger IN SQL
Q. What Is The Trigger?
Trigger is a statement that a system executes automatically when there is any
modification to the database. In Trigger we first specify when the trigger is to
executed and then the action to the performed when the trigger executes.
Trigger also provide the some integrity constraints and referential constrains
that cannot be specified using the constraint mechanism of SQL.
Scenario where we can use the Trigger
Suppose we are adding row to the donors table that is some person has
donated blood. So we can design a trigger that will automatically add the value
of donated blood to the Blood record table. Or Suppose we have the scenario
we want to give the salary to employee from company account but we want to
deposit the 90% salary to salary account of employee and 10% salary to PF
account of employee so in this case we design the trigger to withdraw the
amount from company account and insert the 90% salary amount in
Employee table and 10% salary amount in PF table automatically.
Types of Triggers IN SQL
We can define the 6 types of triggers for each table.
1. AFTER INSERT: activated after data is inserted in to the table. Triggers get
executed automatically when we insert the data in table.
2. AFTER UPDATE: activated after data in the table is modified. Triggers get
executed automatically when we modify the data in table or update record in
table.
3. AFTER DELETE: activated after data is deleted or removed from the table.
Trigger get executed automatically when we delete the record from database
table.
4. BEFORE INSERT: activated before data is inserted in to the table.
5. BEFORE UPDATE: Activated before data is updated in database table.
6. BEFORE DELETE: activated before data is deleted from database table.
How to create the Trigger in MYSQL
If we want to create the trigger in MYSQL we have the following syntax

Visit: http://techhubindia.org Download APP : GIRI’S TECH HUB Page 1


GIRI’S TECH HUB PVT.LTD-PUNE – 9175444433, 9049361265

Consider we have the table name as Voter with field voterid , name and age
and we want to insert the record in voter table if voter age is greater than 18
and if user try to insert the record in voter table below 18 then system should
have generate the error message

Example
Steps
1) create the database table name as voter
mysql> create table voter(vid int(5) primary key auto_increment,name
varchar(200),age int(5));

2) Create the trigger with before insert type on voter table.

mysql> delimiter //
mysql> create trigger checkage BEFORE INSERT ON voter
-> FOR EACH ROW
-> BEGIN
-> DECLARE MESSAGE_TEXT VARCHAR(200);
-> IF NEW.age<18 THEN
-> SET MESSAGE_TEXT="age should be greater than 18";
-> END IF;
-> END
-> //
Query OK, 0 rows affected (0.02 sec)

Visit: http://techhubindia.org Download APP : GIRI’S TECH HUB Page 2


GIRI’S TECH HUB PVT.LTD-PUNE – 9175444433, 9049361265

mysql>

Visit: http://techhubindia.org Download APP : GIRI’S TECH HUB Page 3

You might also like