0% found this document useful (0 votes)
37 views2 pages

Trigger: A Trigger Is A Stored Procedure in Database Which Automatically

A trigger is a stored procedure that automatically runs when a specific event occurs in a database, such as when a row is inserted or updated in a table. Triggers allow actions to be performed before or after these events. The CREATE TRIGGER statement is used to define triggers, specifying the trigger name, timing, triggering event like insert or update, associated table, and trigger body containing the actions. There are before and after triggers that run respectively before or after the triggering statement, and six types of triggers that can be defined for each table based on the timing and triggering event.

Uploaded by

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

Trigger: A Trigger Is A Stored Procedure in Database Which Automatically

A trigger is a stored procedure that automatically runs when a specific event occurs in a database, such as when a row is inserted or updated in a table. Triggers allow actions to be performed before or after these events. The CREATE TRIGGER statement is used to define triggers, specifying the trigger name, timing, triggering event like insert or update, associated table, and trigger body containing the actions. There are before and after triggers that run respectively before or after the triggering statement, and six types of triggers that can be defined for each table based on the timing and triggering event.

Uploaded by

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

Trigger: 

A trigger is a stored procedure in database which automatically


invokes whenever a special event in the database occurs. For example, a
trigger can be invoked when a row is inserted into a specified table or when
certain table columns are being updated. 
Syntax:
create trigger [trigger_name]
[before | after]
{insert | update | delete}
on [table_name]
[for each row]
[trigger_body]
Explanation of syntax:
1. create trigger [trigger_name]: Creates or replaces an existing trigger with the
trigger_name.
2. [before | after]: This specifies when the trigger will be executed.
3. {insert | update | delete}: This specifies the DML operation.
4. on [table_name]: This specifies the name of the table associated with the
trigger.
5. [for each row]: This specifies a row-level trigger, i.e., the trigger will be
executed for each row being affected.
6. [trigger_body]: This provides the operation to be performed as trigger is fired
BEFORE and AFTER of Trigger: 
BEFORE triggers run the trigger action before the triggering statement is run.
AFTER triggers run the trigger action after the triggering statement is run. 

delimiter $$
CREATE TRIGGER Check_age BEFORE INSERT ON employee
FOR EACH ROW
BEGIN
IF NEW.age < 25 THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'ERROR:
AGE MUST BE ATLEAST 25 YEARS!';
END IF;
END; $$
delimiter;
Types of Triggers – 
We can define 6 types of triggers for each table: 
1. AFTER INSERT activated after data is inserted into the table. 
 
2. AFTER UPDATE: activated after data in the table is modified. 
 
3. AFTER DELETE: activated after data is deleted/removed from the table. 
 
4. BEFORE INSERT: activated before data is inserted into the table. 
 
5. BEFORE UPDATE: activated before data in the table is modified. 
 
6. BEFORE DELETE: activated before data is deleted/removed from the table. 

You might also like