Introduction
While working with any technology, we need to study its important aspects and features. When we talk about Database Technology, then we should very clear about the basics of the database to learn advanced features such as a stored procedure, functions, views, packages, sequence, synonyms and triggers. In this article, I will explain what is a trigger and the types of triggers.
Let’s begin...
What is a trigger?
A trigger is a special type of stored procedure in database that automatically invokes/runs/fires when an event occurs in the database server. A trigger uses the special table to keep a copy of the row which we have just inserted, deleted or modified.
What are the types of triggers?
There are three types of triggers in SQL Server.
- DDL Trigger
- DML Trigger
- Logon Trigger
DDL Trigger
DDL trigger runs when DDL events occur in the database. DDL events are CREATE, ALTER and DROP statements.
The DDL triggers are useful in the following cases:
- Record changes in the database schema
- Prevent some specific changes to the database schema
- Respond to a change in the database schema
To create a DDL trigger, use the following syntax
- CREATE TRIGGER <Trigger_Name>
- ON DATABASE
- FOR CREATE_TABLE,ALTER_TABLE,DROP_TABLE
- AS
- BEGIN
- <Trigger Body>
- END
We will see it practically, so first, open SQL Server Management Studio, take a new worksheet, and create a new database name, 'DEMOS', shown below:

Suppose you want to capture all the modifications made to the database index so that you can better monitor the performance of the database server which relates to these index changes.
First, create a new table named ‘Index_logs’ to log the index changes.
- CREATE TABLE Index_logs (
- log_id INT IDENTITY PRIMARY KEY,
- event_data XML NOT NULL,
- changed_by SYSNAME NOT NULL
- );
Also, create one more new table as ‘customers’ which will create the index.



Join the conversation! Your thoughts help the community grow.