Introduction to PostgreSQL DROP TRIGGER
The DROP trigger is used to remove the triggers that exist in the database. The PostgreSQL trigger function is the same as an ordinary function, but it gets invoked or performed automatically when we perform as 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 operation. Once we have the trigger created, then we can remove it from the database. To remove or drop the trigger from the database, the user needs to be the database owner.
Syntax:
Consider the following syntax to understand the PostgreSQL DROP Trigger statement:
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 of existence for 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 which user want 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, then 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.
How does the DROP TRIGGER Work in PostgreSQL?
- To DROP a trigger, we first need to create a new PostgreSQL trigger; we need to follow the following steps:
- Create a PostgreSQL trigger function with the help of the CREATE FUNCTION statement.
- Bind the PostgreSQL trigger function with the database table with the help of the CREATE TRIGGER statement.
- Once you have the trigger created, then by using the DROP trigger statement, you can simply delete the trigger.
- To remove or drop the trigger from the database, the user needs to be the database owner.
Examples of PostgreSQL DROP TRIGGER
To understand the example of the DROP trigger statement, let us create two sections.
1. Create a trigger and apply it to the table
Consider the following example where we will create a table named ‘Student’ and ‘Student_audit’. Whenever we update the Stud_first_name in the student table, then we log the changes in the ‘Student_audit’ table.
Consider the following CREATE TABLE statement, which will create a table named ‘Student’.
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’.
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:
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 first name of the student changes, then the above function inserts an old first name of the student in the Student_audit table along 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 performed the logging of the changes whenever we update the student’s first name.
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.
SELECT * FROM pg_trigger;
Now, we will insert a row into the Student table by using the INSERT INTO statement as follows:
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.
SELECT * FROM Student;
Now, we will update the first name of the student whose last name is ‘Bravo’ by using the UPDATE statement as follows:
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.
SELECT * FROM Student;
Now, Illustrate the content of the Student_audit table by using the following SQL statement and a snapshot.
SELECT * FROM Student_audit;
Now, we will again update the first name of the student whose last name is ‘Bravo’ by using the UPDATE statement as follows:
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.
SELECT * FROM Student;
Now, Illustrate the content of the Student_audit table by using the following SQL statement and a snapshot.
SELECT * FROM Student_audit;
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:
DROP TRIGGER stud_first_name_updates ON student;
Now, we will try to DROP the trigger, which does not exist in the database.
DROP TRIGGER test ON student;
Recommended Articles
This is a guide to PostgreSQL DROP TRIGGER. Here we also discuss the introduction and how the drop trigger works in postgresql and different examples and its code implementation. You may also have a look at the following articles to learn more –