EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • 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 PostgreSQL Tutorial PostgreSQL DROP TRIGGER
 

PostgreSQL DROP TRIGGER

Sohel Sayyad
Article bySohel Sayyad
EDUCBA
Reviewed byRavi Rathore

Updated May 12, 2023

PostgreSQL DROP TRIGGER

 

 

Introduction to PostgreSQL DROP TRIGGER

The following article provides an outline for PostgreSQL DROP TRIGGER. The DROP trigger is used to remove the triggers in the database. The PostgreSQL trigger function is the same as a regular function. Still, it gets invoked or performed automatically when we perform a database operation, such as insert, update, or delete, and a defined event occurs. We can create a trigger for each row and each statement that executes for either row or once for all operations. Once the trigger is created, we can remove it from the database. The user needs to be the database owner to drag or drop the trigger from the database.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Syntax:

DROP TRIGGER [IF EXISTS] triggerName ON tableName [ CASCADE | RESTRICT ];

Explanation:

  • IF EXISTS: If we have defined the IF EXISTS keyword, the DROP statement will check for the existence of the trigger, and it will not throw an error even if the trigger does not exist in the database.
  • TriggerName: This defines the name of the trigger the user wants to remove.
  • TableName: This defines the name of the table on which we have created the trigger.
  • CASCADE: If we have defined the CASCADE keyword, the drop statement removes all dependent objects on the trigger.
  • RESTRICT: If we have defined the RESTRICT keyword, then the drop statement restricts the trigger’s removal if objects are dependent on it.

Code:

CREATE TABLE Student(
Stud_id serial PRIMARY KEY,
Stud_first_name varchar(255) NOT NULL,
Stud_last_name varchar(255) NOT NULL
);

Consider the following CREATE TABLE statement, which will create a table named ‘Student_audit’.

Code:

CREATE TABLE Student_audit(
Student_audit_id serial PRIMARY KEY,
Stud_id INT NOT NULL,
Stud_first_name varchar(255) NOT NULL,
Stud_first_name_updated_on TIMESTAMP(6) NOT NULL
);

Now we will create a function named ‘stud_first_name_update_logs’ as follows:

Code:

CREATE FUNCTION public.stud_first_name_update_logs()
RETURNS trigger
LANGUAGE 'plpgsql'
COST 100
VOLATILE NOT LEAKPROOF
AS $BODY$BEGIN
IF NEW.Stud_first_name <> OLD.Stud_first_name THEN
INSERT INTO Student_audit(Stud_id, Stud_first_name, Stud_first_name_updated_on)
VALUES (OLD.Stud_id, OLD.Stud_first_name,now());
END IF;
RETURN NEW;
END;$BODY$;
ALTER FUNCTION public.stud_first_name_update_logs()
OWNER TO postgres;

Whenever the student’s first name changes, the above function inserts an old first name of the student in the Student_audit table along with the time of change and its id and first name.

Now we will create and bind the PostgreSQL trigger named ‘stud_first_name_updates’ to the student table. The PostgreSQL trigger function is automatically invoked or performs the logging of the changes whenever we update the student’s first name.

Code:

CREATE TRIGGER stud_first_name_updates
BEFORE UPDATE
ON student
FOR EACH ROW
EXECUTE PROCEDURE stud_first_name_update_logs();

Illustrate the result of the above statement by using the following SQL statement and snapshot.

Code:

SELECT * FROM pg_trigger;

Output:

PostgreSQL DROP TRIGGER-1.1

Now, we will insert a row into the Student table by using the INSERT INTO statement as follows:

Code:

INSERT INTO Student(Stud_first_name, Stud_last_name)
VALUES
('Jacob', 'Petter'),
('David', 'Bravo');

Illustrate the content of the Student table by using the following SQL statement and a snapshot.

Code:

SELECT * FROM Student;

Output:

PostgreSQL DROP TRIGGER-1.2

Now, we will update the first name of the student whose last name is ‘Bravo’ by using the UPDATE statement as follows:

Code:

UPDATE Student
SET Stud_first_name = 'John'
WHERE Stud_last_name = 'Bravo';

Illustrate the content of the Student table by using the following SQL statement and a snapshot.

Code:

SELECT * FROM Student;

Output:

PostgreSQL DROP TRIGGER-1.3

Now, Illustrate the content of the Student_audit table by using the following SQL statement and a snapshot.

Code:

SELECT * FROM Student_audit;

Output:

PostgreSQL DROP TRIGGER-1.4

Now, we will again update the first name of the student whose last name is ‘Bravo’ by using the UPDATE statement as follows:

Code:

UPDATE Student
SET Stud_first_name = 'Mathias'
WHERE Stud_last_name = 'Bravo';

Illustrate the content of the Student table by using the following SQL statement and a snapshot.

Code:

SELECT * FROM Student;

Output:

Output-1.5

Now, Illustrate the content of the Student_audit table by using the following SQL statement and a snapshot.

Code:

SELECT * FROM Student_audit;

Output:

Output-1.6

2. DROP the Statement

In the above section, we have created a trigger stud_first_name_updates which we can remove by using the DROP trigger statement as follows:

Code:

DROP TRIGGER stud_first_name_updates  ON student;

Now, we will try to DROP the trigger, which does not exist in the database.

Code:

DROP TRIGGER test ON student;

Output:

Output-1.7

Recommended Articles

We hope that this EDUCBA information on “PostgreSQL DROP TRIGGER” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

  1. PostgreSQL Average
  2. Transaction PostgreSQL
  3. PostgreSQL Materialized Views
  4. PostgreSQL Recursive Query

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

© 2025 - 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
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
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 Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW