Professional Documents
Culture Documents
Triggers
LEVEL LEARNER
Icon Used
Hands on Exercise
Overview
Referenc
e
Lend A
Hand
Objective
2
Question
s
Points To
Ponder
Summar
y
Test Your
Understanding
Check Your
Knowledge
Overview
Objective
After
completing
this
session,
What is a trigger?
What are the different
types of triggers?
What are the advantages
of triggers?
Just a Minute
1. Which of the following statements are correct about
trigger?
a. Triggers are created & maintained by DB server.
b. Triggers are created & maintained by user.
c. Triggers are fired implicitly by server.
d. Triggers are fired explicitly by user.
2. Which of the following are valid DML Events?
a. INSERT
b. UPDATE
c. DELETE
d. ALL OF THE ABOVE
3. After creating the triggers , the triggers are disabled
by
default? ( True / False )
4. Can we create more than one triggers for the table?
6
What is Trigger?
What is a PL SQL Trigger?
Trigger, which is a procedure written in PL/SQL, Java, or C that run
(fire) implicitly whenever a table or view is modified or when some
user actions or database system actions occur.
SQL Server
Create trigger
[schema_name]trigger_name
ON
DATABASE
{For [Create_table [,]|
Drop_table[,]|Alter_table] }
AS
[Begin]
.
.
[End][;]
Note:
[] bracket shows optional part.
{} bracket shows mandatory part.
SQL Server
11
SQL Server
Create trigger
[schema_name]trigger_name
ON
table_name
{for | after |instead of}
{ [insert] [,] [update] [,]
[delete]}
As
[Begin]
.
.
.
[End][;]
Note:
[] bracket shows optional part.
SQL Server
Create trigger emp_trigger ON
Employee
For insert
As
Print After insert Trigger is fired.
12
DDL
Example:
Drop trigger employee_trigger;
Note: Drop syntax is same for
both type of trigger in Oracle.
13
SQL Server
DML
Drop trigger trigger_name
Example:
Drop trigger employee_trigger;
SQL Server
DDL Trigger
[DISABLE|ENABLE] TRIGGER
trigger_name [ ,...n ] ON
DATABASE[ ; ]
Example:
Disable trigger database_trigger on
Database;
DML Trigger
[DISABLE|ENABLE] TRIGGER
trigger_name [ ,...n ] ON
table_name[ ; ]
Example:
DISABLE TRIGGER emp_trigger ON
Employee
14
15
Lend a Hand
Write a query for the following requirement in
Oracle as well as in MS SQL Server
Create a table employee with id, name and
address.
Insert the values into the employee table.
Create a trigger which should insert the deleted id
into the other table emp_id.
16
17
Questions
19
20
Summary
Summarizing the topic in the below following:
Triggers are procedures that are stored in the database
and implicitly run, orfired, when something event
happens.
Triggers can be created for DDL Event or DML Event.
Oracle has two types of DML Triggers
Row level triggers
Statement level triggers
Oracle has Before , After and Instead of triggers , where
as MS SQL Server has after , Instead of triggers.
Both Oracle & MS SQL Server has the DML events INSERT,
UPDATE, DELETE
Syntax for Disabling , Enabling triggers
Syntax for Altering DDL & DML Triggers
21
Source
http://msdn.microsoft.com/en-IN/library/ms189799.aspx
http://docs.oracle.com/cd/E10405_01/doc/appdev.120/e10379/t
rig_stored_proc.htm
http://docs.oracle.com/cd/B19306_01/server.102/b14200/stat
ements_7004.htm
Disclaimer: Parts of the content of this course is based on the materials available from the
Web sites and books listed above. The materials that can be accessed from linked sites are
not maintained by Cognizant Academy and we are not responsible for the contents thereof.
All trademarks, service marks, and trade names in this course are the marks of the
respective owner(s).
22
Comparative Study
(Oracle with MS SQL
Server)
You have successfully completed
Triggers
Change Log
24
Version
Number
Changes made
V1.0
Initial Version
V1.1
Slide No.
Changed By
Effective
Date
Changes
Effected