You are on page 1of 24

Comparative Study

(Oracle with MS SQL Server)

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

This session provides knowledge


and comparative study of the topic
triggers using Oracle and MS SQL
Server. The session will help you
understand the commonalities and
the unique features in both the
RDBMS on the topic covered.

Objective

After

completing

this

session,

participants will be able to:


Understand triggers using Oracle
and MS SQL Server
Identify the similarities and unique
features between triggers in Oracle
and MS SQL Server

Check Your Knowledge


Hey guys!!!
Let me check your understanding on triggers that you learnt
in your primary database language.

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.

What is a MS SQL Trigger?


A trigger is a special kind of stored procedure that automatically
executes when an event occurs in the database server

Similarities in Oracle PL/SQL and MS


SQL
Oracle PL/SQL and MS SQL have many
similarities
Both Supports After and Instead Of Triggers
Both Supports DDL Triggers

Syntax for DDL Triggers


Following table shows the syntax for creating
DDL triggers in Oracle, SQL Server
Oracle
CREATE [OR REPLACE]
TRIGGER trigger_name
AFTER DDL on database
BEGIN
.
.
.
END;

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.

Example for DDL Triggers


Following table shows the example for
creating DDL triggers in Oracle, SQL Server
Oracle

SQL Server

Create or replace trigger


database_trigger
After DDL on database
Begin
dbms_ouput.put_line(database
trigger);
End;

create trigger database_trigger


On database
For create_table, alter_table,
drop_table
As
PRINT database trigger;

Note : Above triggers will be fired whenever the new table


is created or the existing table is dropped or modified.
10

Syntax for DML Triggers


Following table shows the syntax for creating
DML triggers in Oracle, SQL Server
Oracle
Create or replace trigger
trigger_name
BEFORE/AFTER /INSTEAD OF
INSERT/DELETE/UPDATE
On table_name
For each row
Begin
.
.
.
End;

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.

Example for DML Triggers


Following table shows the example for
creating DML triggers in Oracle, SQL Server
Oracle
Create or replace trigger
emp_trigger
After Insert ON Employee
For each row
Begin
Dbms_ouput.put_line(After insert
Trigger is fired);
End;

SQL Server
Create trigger emp_trigger ON
Employee
For insert
As
Print After insert Trigger is fired.

Note: These trigger will be fires for each row of insertion


in Employee table.

12

Syntax & Example for Dropping


Triggers
Following table shows the syntax & example
for dropping triggers
Oracle
DDL and DML

DDL

Drop trigger trigger_name;

Drop trigger trigger_name ON


DATABASE
Example:
Drop trigger database_trigger ON
DATABASE

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;

Disabling or enabling a trigger


Following table shows the syntax & example
for enabling or disabling the triggers
Oracle
DDL and DML Trigger
Alter TRIGGER trigger_name
[DISABLE|ENABLE];
Example:
Alter TRIGGER database_trigger
DISABLE;

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

Note : [,..n] indicates that all


specified triggers defined at the

Unique features in Oracle and MS SQL


Server
Following are some of the unique features in Oracle
and MS SQL Server in terms of Triggers.

15

Triggers typically need access to the


before image and after image of the
data that is being changed, that is
achieved by MS SQL Server using
INSERTED , DELETED tables. Where
as in Oracle it is achieved by :new
& :old bind variables.

No Before triggers in MS SQL Server

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

Lend a Hand Solution


Create table EMPLOYEE (id int , name char(4) , address
varchar(20));
Insert into EMPLOYEE (id,name,address) values(100,abc,xyz);
Create table emp_id(id int);

17

Lend a Hand Solution


Solution in Oracle:

Solution in SQL server:

Create or replace trigger


emp_trigger
After delete on employee
For each row
Begin
Insert into emp_id
values(:old.id);
End;

create trigger emp_trigger


on employee After delete
AS
Declare @eid int
Set @eid=(select empid
from deleted)
Insert into emp_id
values(@eid)

Execute below query:


Delete from employee where id=100;
The value 100 should be inserted into the emp_id table.
18

Questions

19

Check Your Understanding


1. What is a Trigger?
2. What are the similarities in Oracle PL/SQL and MS SQL?
3. What are DDL Triggers?
4. What are DML Triggers?

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

You might also like