EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login

PostgreSQL DROP TRIGGER

Home » Data Science » Data Science Tutorials » PostgreSQL Tutorial » PostgreSQL DROP TRIGGER

PostgreSQL DROP TRIGGER

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:

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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();

Popular Course in this category
PostgreSQL Course (2 Courses, 1 Project)2 Online Courses | 1 Hands-on Project | 7+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (2,974 ratings)
Course Price

View Course

Related Courses

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

SELECT * FROM pg_trigger;

PostgreSQL DROP TRIGGER-1.1

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;

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:

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;

PostgreSQL DROP TRIGGER-1.3

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

SELECT * FROM Student_audit;

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:

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;

Output-1.5

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

SELECT * FROM Student_audit;

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:

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;

Output-1.7

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 –

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

PostgreSQL Course (2 Courses, 1 Project)

2 Online Courses

1 Hands-on Project

7+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
PostgreSQL Tutorial
  • Advanced
    • PostgreSQL Schema
    • Postgres List Schemas
    • PostgreSQL VARCHAR
    • Array in PostgreSQL
    • PostgreSQL DDL
    • PostgreSQL List Users
    • Postgres Default User
    • Postgres add user
    • PostgreSQL log_statement
    • PostgreSQL String Functions
    • PostgreSQL Compare Strings
    • PostgreSQL Text Search
    • PostgreSQL TEXT
    • PostgreSQL String Array
    • PostgreSQL Constraints
    • PostgreSQL UNIQUE Constraint
    • PostgreSQL INTERSECT
    • PostgreSQL Like
    • Cursors in PostgreSQL
    • PostgreSQL UNION ALL
    • Indexes in PostgreSQL
    • PostgreSQL Index Types
    • PostgreSQL REINDEX
    • PostgreSQL UNIQUE Index
    • PostgreSQL Clustered Index
    • PostgreSQL DROP INDEX
    • PostgreSQL DISTINCT
    • PostgreSQL FETCH
    • PostgreSQL RAISE EXCEPTION
    • PostgreSQL Auto Increment
    • Sequence in PostgreSQL
    • Wildcards in PostgreSQL
    • PostgreSQL Subquery
    • PostgreSQL Alias
    • PostgreSQL LIMIT
    • PostgreSQL Limit Offset
    • PostgreSQL LAG()
    • PostgreSQL Table
    • Postgres Show Tables
    • PostgreSQL Describe Table
    • PostgreSQL Lock Table
    • PostgreSQL ALTER TABLE
    • Postgres Rename Table
    • Postgres DROP Table
    • PostgreSQL Functions
    • PostgreSQL Math Functions
    • PostgreSQL Window Functions
    • Aggregate Functions in PostgreSQL
    • PostgreSQL Primary Key
    • Foreign Key in PostgreSQL
    • PostgreSQL Procedures
    • PostgreSQL Stored Procedures
    • PostgreSQL Views
    • PostgreSQL Materialized Views
    • Postgres Create View
    • PostgreSQL Triggers
    • PostgreSQL DROP TRIGGER
    • PostgreSQL Date Functions
    • PostgreSQL TO_DATE()
    • PostgreSQL Timestamp
    • PostgreSQL CURRENT_TIMESTAMP()
    • PostgreSQL Notify
    • PostgreSQL RANK()
    • PostgreSQL Select
    • PostgreSQL Average
    • PostgreSQL DATE_PART()
    • PostgreSQL EXECUTE
    • PostgreSQL COALESCE
    • PostgreSQL EXTRACT()
    • PostgreSQL Sort
    • PostgreSQL TO_CHAR
    • PostgreSQL Interval
    • PostgreSQL Number Types
    • PostgreSQL ROW_NUMBER
    • Alter Column in PostgreSQL
    • PostgreSQL Identity Column
    • PostgreSQL SPLIT_PART()
    • PostgreSQL CONCAT()
    • PostgreSQL replace
    • PostgreSQL TRIM()
    • PostgreSQL MAX
    • PostgreSQL DELETE
    • PostgreSQL Float
    • PostgreSQL OID
    • PostgreSQL log
    • PostgreSQL REGEXP_MATCHES()
    • PostgreSQL MD5 
    • PostgreSQL NOW()
    • PostgreSQL RANDOM
    • PostgreSQL round
    • PostgreSQL Trunc()
    • PostgreSQL TIME
    • PostgreSQL IS NULL
    • PostgreSQL CURRENT_TIME
    • PostgreSQL MOD()
    • Postgresql Count
    • PostgreSQL Datetime
    • PostgreSQL MIN()
    • PostgreSQL age()
    • PostgreSQL enum
    • PostgreSQL OR
    • PostgreSQL Wal
    • PostgreSQL NOT IN
    • PostgreSQL SET
    • PostgreSQL Current Date
    • PostgreSQL Compare Date
    • PostgreSQL SERIAL
    • PostgreSQL Database
    • PostgreSQL Clone Database
    • PostgreSQL Copy Database
    • PostgreSQL Restore Database
    • PostgreSQL DROP DATABASE
    • PostgreSQL ALTER DATABASE
    • Postgres DROP Database
    • Postgres Dump Database
    • PostgreSQL OFFSET
    • PostgreSQL GRANT
    • PostgreSQL COMMIT
    • PostgreSQL ROLLUP
    • PostgreSQL JSON
    • EXPLAIN ANALYZE in PostgreSQL
    • PostgreSQL Temporary Table
    • PostgreSQL cluster
    • PostgreSQL Replication
    • PostgreSQL Logical Replication
    • PostgreSQL flush privileges
    • PostgreSQL Tablespaces
    • CAST in PostgreSQL
    • PostgreSQL CTE
    • hstore in PostgreSQL
    • PostgreSQL DECODE()
    • PostgreSQL Vacuum
    • PostgreSQL EXCLUDE
    • Postgres Change Password
    • Postgres Delete Cascade
    • PostgreSQL EXCEPT
    • PostgreSQL Roles
    • PostgreSQL Link
    • PostgreSQL Partition
    • PostgreSQL column does not exist
    • PostgreSQL Log Queries
    • PostgreSQL escape single quote
    • PostgreSQL Query Optimization
    • PostgreSQL Character Varying
    • PostgreSQL Transaction
    • PostgreSQL Extensions
    • PostgreSQL Import CSV
    • PostgreSQL Client
    • PostgreSQL caching
    • PostgreSQL JDBC Driver
    • PostgreSQL Interview Questions
  • Basic
    • What is PostgreSQL
    • PostgreSQL Features
    • How to Install PostgreSQL
    • PostgreSQL Versions
    • PostgreSQL Architecture
    • PostgreSQL GUI
    • PostgreSQL Variables
    • PostgreSQL Data Types
    • PostgreSQL NOT NULL
    • PostgreSQL Integer
    • PostgreSQL Boolean
    • PostgreSQL NULLIF
    • PostgreSQL Administration
    • PostgreSQL Commands
    • PostgreSQL Operators
    • PostgreSQL IN Operator
  • Control Statement
    • PostgreSQL IF Statement
    • PostgreSQL if else
    • PostgreSQL CASE Statement
    • PostgreSQL LOOP
    • PostgreSQL For Loop
    • PostgreSQL While Loop
  • Joins
    • Joins in PostgreSQL
    • PostgreSQL Inner Join
    • PostgreSQL Outer Join
    • LEFT OUTER JOIN in PostgreSQL
    • PostgreSQL FULL OUTER JOIN
    • PostgreSQL LEFT JOIN
    • PostgreSQL Full Join
    • PostgreSQL Cross Join
    • PostgreSQL NATURAL JOIN
    • PostgreSQL UPDATE JOIN
  • Queries
    • PostgreSQL Queries
    • PostgreSQL WHERE Clause
    • PostgreSQL WITH Clause
    • PostgreSQL ORDER BY
    • PostgreSQL ORDER BY Random
    • PostgreSQL GROUP BY
    • PostgreSQL group_concat
    • PostgreSQL HAVING
    • PostgreSQL Recursive Query
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

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

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

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

EDUCBA

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

Forgot Password?

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

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

Special Offer - PostgreSQL Course (2 Courses, 1 Project) Learn More