Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

SQL Advanced (Triggers)

Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

DAY-29

TRIGGERS
IN
SQL

SHVETA MAINI
1

What is Trigger?
.
A trigger is a stored procedure in a
database that 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 specific table columns are
updated.
2

Syntax To Create Trigger


3

.
Explanation Of Syntax
CREATE TRIGGER trigger_name: Defines a new
trigger.
{BEFORE | AFTER | INSTEAD OF}: Specifies
when the trigger should fire (before, after, or
instead of the operation).
{INSERT | UPDATE | DELETE}: Specifies which
data modification event will activate the
trigger.
ON table_name: Specifies the table the trigger
is associated with.
[FOR EACH ROW]: (Optional) Specifies that the
trigger should execute for each affected row.
BEGIN ... END: The block where the SQL
actions to be executed by the trigger are
placed.
4

TYPES OF TRIGGER:-
5

(1.) BEFORE TRIGGER

Fires before an event (such as


INSERT, UPDATE, or DELETE) takes
place.
Can be used to validate or modify
data before it is actually written to
the database.
Example: A BEFORE INSERT trigger
can ensure that no negative values
are inserted in the price column.
6

(2.) AFTER TRIGGER

Fires after the event is completed.

Typically used for actions like


logging, updating related data, or
maintaining audit trails.

Example: An AFTER UPDATE trigger


could log changes to a record in an
audit table.
7

(3.) INSTEAD OF TRIGGER


Fires in place of the event (i.e.,
replaces the action).

Commonly used for views where


operations like INSERT, UPDATE, or
DELETE are not allowed directly.

Example: An INSTEAD OF DELETE


trigger might be used to archive data
before it is deleted.
8

TRIGGER BASED ON EVENT

1.) INSERT TRIGGER : -

Fires when a new row is inserted into


a table.
Can be used to perform validations or
updates based on the inserted data.
9

TRIGGER BASED ON EVENT

2.) UPDATE TRIGGER : -

Fires when an existing row is


modified.
Useful for tracking changes, updating
related tables, or validating data
before it's committed.
10

TRIGGER BASED ON EVENT

3.) DELETE TRIGGER : -

Fires when a row is deleted from a


table.
Often used for maintaining
referential integrity, archiving
deleted records, or updating related
tables.
11

Advantages Of Using Trigger

(1.) AUTOMATION:-
.
Triggers automate tasks such as logging,
validating, or updating related tables
without requiring manual intervention.

(2.) DATA INTEGRITY:-


.
Triggers help maintain consistency and
correctness of the data, ensuring that
business rules are applied automatically
whenever data is inserted, updated, or
deleted.
12

Advantages Of Using Trigger

(3.)
. REAL TIME EXECUTION:-
Triggers are event-driven, meaning they
execute immediately after a specified
event occurs. This ensures that the related
operations are always synchronized with
the data changes.

(4.) AUDITING AND LOGGING :-


.
Triggers can be used to automatically log
all changes to a table (inserts, updates,
deletes), providing an audit trail of
modifications.
13

BEST PRACTICES
.
Keep Triggers Simple: Complex trigger
logic can lead to performance
problems and debugging difficulties.
Keep the logic inside triggers as
simple as possible.

Avoid Nested Triggers: Some


database systems allow triggers to
call other triggers, potentially leading
to an infinite loop. Be cautious and
avoid nesting triggers.
I HOPE YOU FIND
THIS POST
HELPFUL !
DO HIT THE LIKE
BUTTON AND
SHARE IT
THANK YOU !!
SHVETA MAINI

You might also like