You are on page 1of 15

Advanced Database

Lab session
Triggers
Triggers
• A trigger is a special type of stored procedure that automatically runs
when an event occurs in the database server.
Types of SQL Triggers
1.Data Definition Language (DDL) Triggers
2.Data Manipulation Language (DML) Triggers
3.Logon Triggers
Cont…
The DML triggers can be classified into two types:
• After Triggers (FOR triggers): After trigger fires, when SQL Server completes
the triggering action successfully, that fired it. Generally, this trigger is executed
when a table completes an insert, update or delete operations. It is not supported
in views.
• Instead Of Triggers:
Instead of trigger
• The INSTEAD OF TRIGGER in SQL Server is a trigger that allows us to
skip DELETE, INSERT and UPDATE statements in a table and it executes other
statements defined in the trigger instead.
• Instead of Trigger fires before SQL Server begins to execute the triggering operation that
triggered it. It means that no condition constraint check is needed before the trigger runs.
As a result, even if the constraint check fails, this trigger will fire.
• The INSERT, DELETE and UPDATE statements don’t occur at all.
CREATE TRIGGER [SCHEMA_NAME].[YOUR_TRIGGER_NAME]
ON [ YOUR_TABLE_NAME | VIEW_NAME]
INSTEAD OF { [INSERT], [DELETE], [UPDATE] }
AS { YOUR_SQL_STATEMENTS }
Cont…
• DDL Triggers
• DDL triggers are fired in response to the DDL events, such as CREATE, ALTER,
and DROP statements. We can create these triggers at the database level or server
level, depending on the type of DDL events. It can also be executed in response to
certain system-defined stored procedures that do DDL-like operations.
• DDL triggers are classified into two types, which are as follows
1.Database-Scoped DDL Triggers:
1. are invoked by the events that modify the database schema
2. stores the triggers in the database and execute on DDL events, except those
related to temporary tables.
2.Server-Scoped DDL Triggers:
1. are invoked by DDL events at the server level.
2. are stored in the master database
• Types of DDL triggers in SQL Server?
• There are two types of DDLs triggers available in SQL Server. They are as
follows:
1.Database Scoped DDL Trigger
2.Server Scoped DDL Trigger
Example1: Create a trigger that will restrict creating a new
table on a specific database.
CREATE TRIGGER trRestrictCreateTable
ON DATABASE
FOR CREATE_TABLE
AS
BEGIN
• PRINT 'YOU CANNOT CREATE A TABLE IN THIS DATABASE’
ROLLBACK TRANSACTION
END
Example2: Create a trigger that will restrict ALTER
operations on a specific database table.
• CREATE TRIGGER trRestrictAlterTable
• ON DATABASE
• FOR ALTER_TABLE
• AS
• BEGIN
• PRINT 'YOU CANNOT ALTER TABLES'
• ROLLBACK TRANSACTION
• END
• DROP TRIGGER trRestrictCreateTable ON DATABASE
• DISABLE TRIGGER trRestrictDDLEvents ON DATABASE
• Server-scoped DDL Triggers in SQL Server:
• CREATE TRIGGER trServerScopedDDLTrigger
• ON ALL SERVER
• FOR CREATE_TABLE, ALTER_TABLE, DROP_TABLE
• AS
• BEGIN
• PRINT 'You cannot create, alter or drop a table in any database of this server'
• ROLLBACK TRANSACTION
• END
What Is The Use Of DDL Triggers?

• DDL triggers can be used to prevent modifications in the database schema. A


schema is a collection of objects such as tables, views, and so forth in a
database.
• Data Definition Language (DDL) triggers execute stored procedures when
DDL events such as CREATE, ALTER, and DROP statements occur in the
database or the server
• If you want to execute some code or message in response to a specific DDL
event.
• Audit or check the changes that the users are making to the database structure.
• DDL triggers can operate only on completion of the DDL events
• DDL triggers can be used to prevent modifications in the database schema. A
schema is a collection of objects such as tables, views, and so forth in a database.
• DDL triggers can invoke an event or display a message based on the
modifications attempted on the schema and are defined either at the database level
or at the server level
Cont…
Logon Triggers
• Logon triggers are fires in response to a LOGON event. The LOGON event
occurs when a user session is generated with an SQL Server instance, which is
made after the authentication process of logging is completed but before
establishing a user session.

You might also like