0% found this document useful (0 votes)
27 views

My SQL Part4

SQL triggers automatically invoke stored procedures when data modification events occur on a table. Triggers can be created to fire before or after insert, update, or delete statements using the CREATE TRIGGER statement. The trigger body contains the code to be executed in response to the triggering event. Triggers allow data integrity checks and modifications to other tables in response to changes made to the triggering table.

Uploaded by

mamash
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

My SQL Part4

SQL triggers automatically invoke stored procedures when data modification events occur on a table. Triggers can be created to fire before or after insert, update, or delete statements using the CREATE TRIGGER statement. The trigger body contains the code to be executed in response to the triggering event. Triggers allow data integrity checks and modifications to other tables in response to changes made to the triggering table.

Uploaded by

mamash
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 7

SQL Triggers

 Trigger: A trigger is a stored procedure in database which


 Syntax automatically invokes whenever a special event in the
database occurs. 
create trigger [trigger_name]  create trigger [trigger_name]: Creates or replaces an existing trigger with the
trigger_name.

[before | after]  [before | after]: This specifies when the trigger will be executed.
 {insert | update | delete}: This specifies the DML operation.

{insert | update | delete}  on [table_name]: This specifies the name of the table associated with the trigger.
 [for each row]: This specifies a row-level trigger, i.e., the trigger will be

on [table_name] 
executed for each row being affected.
[trigger_body]: This provides the operation to be performed as trigger is fired

[for each row] 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
[trigger_body] run. 
Using MySQL Triggers
Every trigger associated with a table
has a unique name and function based
on two factors:
1. Time. BEFORE or AFTER a
specific row event.
2. Event. INSERT, UPDATE or
DELETE.
Create Triggers
 Use the CREATE TRIGGER CREATE TRIGGER <trigger name> <trigger
time > <trigger event>
statement syntax to create a new
ON <table name>
trigger:
FOR EACH ROW
<trigger body>;

 The best practice is to name the


<trigger time>_<table name>_<trigger
trigger with the following
event>
information:
Before Insert Trigger
 Syntax  The BEFORE INSERT trigger gives
CREATE TRIGGER <trigger name> control over data modification
BEFORE INSERT before committing into a database
table. Capitalizing names for
ON <table name> consistency, checking the length of
FOR EACH ROW an input, or catching faulty inputs
with BEFORE INSERT triggers
<trigger body>;
further provides value limitations
before entering new data.
Create an AFTER INSERT Trigger
 Syntax  The AFTER INSERT trigger is
CREATE TRIGGER <trigger name> useful when the entered row
AFTER INSERT generates a value needed to update
another table.
ON <table name>
FOR EACH ROW
<trigger body>;
Create a BEFORE UPDATE Trigger
 Syntax  The BEFORE UPDATE triggers go
CREATE TRIGGER <trigger name> together with the BEFORE INSERT
BEFORE UPDATE triggers. If any restrictions exist
before inserting data, the limits
ON <table name> should be there before updating as
FOR EACH ROW well.
<trigger body>;
SQL Transactions

You might also like