EDUCBA

EDUCBA

MENUMENU
  • Explore
    • Lifetime Membership
    • All in One Bundles
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign up
Home Data Science Data Science Tutorials SQL Tutorial SQL DROP TRIGGER

SQL DROP TRIGGER

Priya Pedamkar
Article byPriya Pedamkar

Updated June 16, 2023

SQL DROP TRIGGER

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.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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:

SQL DROP TRIGGER1

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.”

SQL DROP TRIGGER2

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:

SQL DROP TRIGGER3

The query returned successfully. We can see in the object explorer that DML_Trigger is no longer part of books_audit_table.

SQL DROP TRIGGER4

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:

SQL DROP TRIGGER5

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.

DDL trigger

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:

security trigger

Here we have not used the IF EXISTS condition and directly dropped it.

IF EXISTS condition

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:

dummy trigger

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:

drop logon trigger

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.

  1. PostgreSQL Variables
  2. MySQL Trigger
  3. Uses of SQL
  4. EXPLAIN in SQL
ADVERTISEMENT
All in One Excel VBA Bundle
500+ Hours of HD Videos
15 Learning Paths
120+ Courses
Verifiable Certificate of Completion
Lifetime Access
ADVERTISEMENT
Financial Analyst Masters Training Program
2000+ Hours of HD Videos
43 Learning Paths
550+ Courses
Verifiable Certificate of Completion
Lifetime Access
ADVERTISEMENT
All in One Data Science Bundle
2000+ Hour of HD Videos
80 Learning Paths
400+ Courses
Verifiable Certificate of Completion
Lifetime Access
ADVERTISEMENT
All in One Software Development Bundle
5000+ Hours of HD Videos
149 Learning Paths
1050+ Courses
Verifiable Certificate of Completion
Lifetime Access
Primary Sidebar
Footer
Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
  • Blog as Guest
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

🚀 Cyber Monday Reloaded Price Drop! All in One Universal Bundle (3700+ Courses) @ 🎁 90% OFF - Ends in ENROLL NOW