Open In App

PostgreSQL – DROP TRIGGER

In PostgreSQL, the DROP TRIGGER statement is used to drop a trigger from a table.

Syntax:
DROP TRIGGER [IF EXISTS] trigger_name 
ON table_name [ CASCADE | RESTRICT ];

Let’s analyze the above syntax:



Example:

First, create a function that validates the username of a staff. The username of staff must not be null and its length must be at least 8.



CREATE FUNCTION check_staff_user()
    RETURNS TRIGGER
AS $$
BEGIN
    IF length(NEW.username) < 8 OR NEW.username IS NULL THEN
        RAISE EXCEPTION 'The username cannot be less than 8 characters';
    END IF;
    IF NEW.NAME IS NULL THEN
        RAISE EXCEPTION 'Username cannot be NULL';
    END IF;
    RETURN NEW;
END;
$$
LANGUAGE plpgsql;

Second, create a new trigger on the staff table to check the username of a staff. This trigger will fire whenever you insert or update a row in the staff table.

CREATE TRIGGER username_check 
    BEFORE INSERT OR UPDATE
ON staff
FOR EACH ROW 
    EXECUTE PROCEDURE check_staff_user();

This will result in the following trigger creation:

Third, use the DROP TRIGGER statement to delete the username_check trigger:

DROP TRIGGER username_check
ON staff;

Output:

Article Tags :