Functions and Triggers PDF
Functions and Triggers PDF
Stphane Bressan
Introduction to Database Systems
Disclaimer
The objective of this lecture is neither to offer a exhaustive
presentation of the SQL standard for stored procedures and triggers
or a comprehensive presentation of the underlying concepts, nor to
serve as a complete tutorial for PostgreSQL stored procedures and
functions in PL/pgSQL and triggers.
Stored Procedures
Recall that most DBMS, including PostgreSQL, have a client/server
architecture
Application
code Data
Introduction to Database Systems
Stored Procedures
The SQL 1999 standard proposes a language for stored procedures
and triggers. PostgreSQL (and other DBMS) can store, share and
execute code on the server. PL/pgSQL is PostgreSQLs language
specifically designed to seamlessly embed SQL code and interact
with the database SQL code. PL/pgSQL partially complies with the
SQL standard. PL/pgSQL is similar to Oracle PL/SQL, SQLServer
Transact-SQL and DB2 SQL Procedural Language and other
languages implementing variants of the Persistent Stored Modules
portion of the SQL standard.
Introduction to Database Systems
Stored Procedures
Performance: Stored procedures are compiled. The code is cached
and shared by all users. The code is executed on the servers side,
usually a powerful machine. They incur generally fewer data transfer
across the network. The optimization of the SQL code they contain
is not adaptive. The clients computing power is underutilized.
Productivity: Stored procedures are shared and reused under access
control. The application logic that they encode is implemented and
maintained in a single place. The code is not portable from one
DBMS to the next. The languages and concepts are complicated.
Security: Data manipulation can be restricted to calling stored
procedures under strict access control. It is easy to make tragic
mistake while coding.
Introduction to Database Systems
Example Function
Example Function
Example Function
Cursors
Triggers
Triggers program the reaction to events happening to the database.
triggers
Triggers
Performance: Triggers are compiled. The code is cached and shared
by all users. The code is executed on the servers side, usually a
powerful machine. They incur no data transfer across the network.
The optimization of the SQL code they contain is not adaptive. The
clients computing power is not utilized.
Productivity: Triggers are applied to all interactions. The application
logic they encode is implemented and maintained in a single place.
The languages and concepts are complicated. The code is not
portable from one DBMS to the next. Interactions among triggers
(chain reactions) and between triggers, constraints and transactions
are difficult to control.
Security: Data manipulation can be restricted and transformations
automatically propagated. It is easy to make tragic mistake while
coding.
Introduction to Database Systems
Credits
Copyright 2017 by Stphane Bressan