Triggers (1)
Triggers (1)
and Triggers
1
Content
2
1. PostgreSQL Procedural Language (PL)
PL = programming languages used by the database
engine → manipulate and extract data
PL execute directly inside the database engine, instead of
remotely in a separate application program.
− greatly improve performance by reducing the execution time
of your application
− provide a standard place to store functions that is accessible
to anyone who uses the database
3
Procedural languages
Standard PostgreSQL supports:
− PL/pgSQL
− PL/Tcl
− PL/Perl
− PL/Python
HOWEVER:
− very close to Oracle’s default procedural language, PL/SQL
file:///C:/Program%20Files/PostgreSQL/9.4/doc/postgresql/html/plpgsql.html
5
2.Trigger
A trigger defines an operation that is performed when a
specific event occurs on a table/view:
− inserts a new record / updates an existing record, or deletes
a record.
The function executed as a result of a trigger is called a
trigger function.
− Trigger function format looks similar to the stored procedure
function (same CREATE OR REPLACE FUNCTION
command)
− 2 two things:
− Trigger functions do not use input arguments in the function, but
rather are passed arguments from a trigger event
https://www.postgresql.org/docs/13/sql-createtrigger.html
7
CREATE TRIGGER command - Explain
Can occur either before or after the event occurs
(INSERT, UPDATE, DELETE, TRUNCATE on the table)
− multiple events can be specified using OR
Fire triggers:
− ROW: for each row that is affected by the event
file:///C:/Program%20Files/PostgreSQL/9.4/doc/postgresql/html/sql-
createtrigger.html
9
CREATE TRIGGER command
function: execute when the trigger is fired
− the arguments in the CREATE TRIGGER command are passed using
the TG_ARGV special variable
− …
10
file:///C:/Program%20Files/PostgreSQL/9.4/doc/postgresql/html/plpgsql-
11 trigger.html
4. Creating a Trigger Function
Code:
CREATE OR REPLACE FUNCTION trigger_func_name()
RETURNS trigger AS $$
[<variable declaration>]
BEGIN
<body code>
RETURN ....;
END;
$$ LANGUAGE plpgsql ;
12
4. Creating a Trigger Function
13
Example (previous class)
-- 2. Create a view from eduDB, named
student_class_shortinfos, this view contains:
student_id, firstname, lastname, gender, class
name.
16
Example (previous class)
17
Example (previous class)
-- define a trigger function
CREATE OR REPLACE FUNCTION
insert_view_student_class_shortinfos() RETURNS
trigger AS
$$
BEGIN
-- insert student
insert into student (student_id, last_name,
first_name , gender, dob) values (NEW.student_id,
NEW.last_name, NEW.first_name, NEW.gender, NEW.dob);
RETURN NEW;
END;
$$ LANGUAGE plpgsql ;
18
Remarks
RETURN in a trigger function
− NULL
− One record having the same structure as table record on which the
trigger is defined
Trigger « AFTER »:
− RETURN NULL; -- or RETURN NEW; RETURN OLD;
Trigger « BEFORE »
− RETURN NULL; : subsequent triggers are not fired, and the
INSERT/UPDATE/DELETE does not occur for this row
− Trigger BEFORE DELETE : RETURN OLD;
20
Exercise No 2 from last week
2) Add a new attribute (named: number_students, data type: integer) on
clazz table to store number of students in class.
Check values in this attribute before and after calling the function
update_number_students()
-- define a trigger
CREATE TRIGGER af_insert
AFTER INSERT ON student
FOR EACH ROW
WHEN (NEW.clazz_id IS NOT NULL)
EXECUTE PROCEDURE tf_af_insert();
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
22
Exercise
Given EduBD, write triggers to ensure the following requirement:
• If data on student table is changed, the number of students in
clazz table is always correct.
(delete a student, change student class)
23