Updated May 12, 2023
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.
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:
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:
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:
Now, Illustrate the content of the Student_audit table by using the following SQL statement and a snapshot.
Code:
SELECT * FROM Student_audit;
Output:
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:
Now, Illustrate the content of the Student_audit table by using the following SQL statement and a snapshot.
Code:
SELECT * FROM Student_audit;
Output:
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:
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.