Tom Kelliher, CS 318
Apr. 5, 2002
Read Chapter 10. Refer to the PostgreSQL online documentation for triggers and stored procedures Chapter 24 of the Programmer's Guide. Project implications: Your integrity constraints and FDs should be maintained via triggers and PL/pgSQL stored procedures.
Normal forms and schema synthesis/decomposition.
Stored procedures in PostgreSQL.
ON Event
IF PreCondition THEN
Action
where:
ON Insertion on Transcript
IF Course is Full THEN
Abort Transaction
What is an event? A single SQL statement or an entire transaction?
Obviously, immediate execution can't be paired with deferred consideration.
ON DELETE and
ON CASCADE clauses of UPDATE and DELETE statements
and foreign constraints.
CREATE TRIGGER syntax in PostgreSQL:
CREATE TRIGGER name { BEFORE | AFTER } { event [ OR ... ] }
ON table FOR EACH { ROW | STATEMENT }
EXECUTE PROCEDURE func ( arguments );
EVENT is one of INSERT, DELETE, or UPDATE.
STATEMENT level triggers are not currently supported.
Of course, the PL/pgSQL func must appear before the trigger naming
it.
CREATE FUNCTION RaiseCheck () RETURNS OPAQUE AS '
BEGIN
IF NEW.Salary > 1.05 * OLD.Salary THEN
-- Excessive salary raise - abort transaction.
RAISE EXCEPTION ''% given an excessive raise'', OLD.EmpName;
END IF;
-- Salary increase OK, proceed with transaction.
RETURN NEW;
END;
' LANGUAGE 'plpgsql';
CREATE TRIGGER LimitRaises BEFORE UPDATE ON Employee
FOR EACH ROW EXECUTE PROCEDURE RaiseCheck();