Updated June 16, 2023
Introduction to SQL DROP TRIGGER
The DROP TRIGGER command in standard query language (SQL) removes one or more Data Manipulation Language (DML) or Data Definition Language (DDL) triggers from the current database. Triggers, which are a set of SQL statements or commands, are automatically invoked when a successful operation such as an update, create, delete, login, etc., is executed. There are two ways to remove triggers: by using the DROP TRIGGER command or by dropping them. Secondly, by using the DROP TABLE on the trigger table. In the latter case, dropping a table amounts to the removal of all associated triggers. While in the former case, dropping a trigger removes information about that specific trigger from the sysobjects and syscomments tables.
In this post, we will discuss the DROP TRIGGER command in detail with the help of some examples. Let’s begin by discussing the syntax and parameters used for writing it.
Syntax
The basic syntax used for writing a DROP TRIGGER command for dropping DML triggers in SQL is as follows :
DROP TRIGGER [ IF EXISTS ] [ schema_name. ] trigger_name;
Basic syntax used for writing a DROP TRIGGER command for dropping DDL triggers in SQL is as follows :
DROP TRIGGER [ IF EXISTS ]trigger_name
ON { DATABASE | ALL SERVER };
The basic syntax used for writing a DROP TRIGGER command for dropping logon triggers in SQL is as follows :
DROP TRIGGER [ IF EXISTS ] trigger_name
ON ALL SERVER;
Parameters
The parameters used in the syntaxes mentioned above are as follows :
- schema_name: Schema_name is the schema name from which we want to drop the trigger. It is an optional argument. By default, the current schema will be considered as the target schema.
- trigger_name: Trigger_name is the name of the trigger we want to remove or drop.
- IF EXISTS: If exists is a conditional clause that is used to ensure the existence of the said trigger.
- DATABASE: This keyword indicates that the trigger is dropped only on the current database.
- ALL SERVER: ALL SERVER keyword ensures that the trigger is dropped from the current server.
Examples to Implement SQL DROP TRIGGER
Below are the examples:
Example #1
SQL command to illustrate the dropping of DML triggers
Step 1: In order to illustrate the DROP Trigger command of DML type, let us create a dummy trigger called “DML_trigger”. We can use the following CREATE TRIGGER statement to create the said trigger.
Code:
CREATE TRIGGER DML_Trigger
ON dbo.books_audit_table
AFTER INSERT
AS
RAISERROR ('Please do not forget to current book status !!', 16,10);
Output:
Step 2: You can obtain the list of triggers created on a database object from the sys.triggers table or directly view them under the respective database object in the object explorer window. In the Triggers section of the books_audit_table, you can clearly observe the creation of a new trigger named “DML_Trigger.”
Step 3: In SQL Server, we must write the IF EXISTS condition in the following way. Although, in other database servers, such as MYSQL, we can directly mention it as shown in the syntax. Here is the complete DROP TRIGGER command for removing DML triggers.
Code:
IF EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[DML_Trigger]'))
DROP TRIGGER [dbo].[DML_Trigger]
GO
Output:
The query returned successfully. We can see in the object explorer that DML_Trigger is no longer part of books_audit_table.
Example #2
SQL command to illustrate the dropping of DDL triggers
Step 1: To illustrate the DROP Trigger command of the DDL type, let us create a dummy trigger called “security”. We can use the following CREATE TRIGGER statement to create the said trigger.
Code:
CREATE TRIGGER security
ON DATABASE
FOR CREATE_TABLE,ALTER_TABLE,DROP_TABLE
AS
PRINT 'Security Trigger for Data Definition Language commands has been activated!'
ROLLBACK;
Output:
Step 2: As promised earlier, we will use the sys.triggers table to see if the “security” DDL trigger has been created this time. Here is a SELECT statement to help us do that.
Step 3: The query shows us that the security trigger has been successfully created. Next, a basic DROP TRIGGER command lets us drop the “security” trigger.
Code:
DROP TRIGGER [security] ON DATABASE;
Output:
Here we have not used the IF EXISTS condition and directly dropped it.
Explanation: The above DROP TRIGGER command completed successfully. Ergo, we no longer have a “security” trigger in the sys.triggers table.
Example #3
SQL command to illustrate the dropping of logon triggers.
Step 1: To illustrate the DROP Trigger command of the logon type, let us create a dummy trigger called “logon_trigger”. We can use the following CREATE TRIGGER statement to create the said trigger.
Code:
CREATE TRIGGER logon_trigger
ON ALL SERVER
AFTER LOGON
AS
PRINT 'Logon trigger has been activated!'
ROLLBACK;
Output:
Explanation: The logon_trigger has been successfully created. Please note that while creating logon triggers, ensure you have added it to a safe list. Otherwise, you might not be able to access the SQL server at all.
Step 2: Here is a DROP TRIGGER command to drop logon_trigger.
Code:
DROP TRIGGER [logon_trigger]
ON ALL SERVER;
Output:
Explanation: The command has completed successfully, and it has dropped the logon_trigger. We can check it from the sys.triggers table.
Conclusion
DROP TRIGGER command in SQL is used to drop or remove different types of DML, DDL, and logon triggers on database objects and servers.
Recommended Articles
We hope that this EDUCBA information on “SQL DROP TRIGGER” was beneficial to you. You can view EDUCBA’s recommended articles for more information.