Functions and Procedures
Database System Concepts - 7th Edition 5.1 ©Silberschatz, Korth and Sudarshan
Functions and Procedures
Functions and procedures allow “business logic” to be stored in the
database and executed from SQL statements.
These can be defined either by the procedural component of SQL or by an
external programming language such as Java, C, or C++.
The syntax we present here is defined by the SQL standard.
Most databases implement nonstandard versions of this syntax.
Database System Concepts - 7th Edition 5.2 ©Silberschatz, Korth and Sudarshan
Declaring SQL Functions
Define a function that, given the name of a department, returns the count of
the number of instructors in that department.
create function dept_count (dept_name varchar(20))
returns integer
begin
declare d_count integer;
select count (* ) into d_count
from instructor
where instructor.dept_name = dept_name
return d_count;
end
The function dept_count can be used to find the department names and
budget of all departments with more that 12 instructors.
select dept_name, budget
from department
where dept_count (dept_name ) > 12
Database System Concepts - 7th Edition 5.3 ©Silberschatz, Korth and Sudarshan
Table Functions
The SQL standard supports functions that can return tables as results; such
functions are called table functions
Example: Return all instructors in a given department
create function instructor_of (dept_name char(20))
returns table (
ID varchar(5),
name varchar(20),
dept_name varchar(20),
salary numeric(8,2))
return table
(select ID, name, dept_name, salary
from instructor
where instructor.dept_name = instructor_of.dept_name)
Usage
select *
from table (instructor_of ('Music'))
Database System Concepts - 7th Edition 5.4 ©Silberschatz, Korth and Sudarshan
Language Constructs (Cont.)
For loop
Permits iteration over all results of a query
Example: Find the budget of all departments
declare n integer default 0;
for r as
select budget from department
where dept_name = 'Music'
do
set n = n + r.budget
end for
Database System Concepts - 7th Edition 5.8 ©Silberschatz, Korth and Sudarshan
Triggers
Database System Concepts - 7th Edition 5.11 ©Silberschatz, Korth and Sudarshan
Triggers
A trigger is a statement that is executed automatically by the system as a
side effect of a modification to the database.
To design a trigger mechanism, we must:
Specify the conditions under which the trigger is to be executed.
Specify the actions to be taken when the trigger executes.
Triggers introduced to SQL standard in SQL:1999, but supported even
earlier using non-standard syntax by most databases.
Syntax illustrated here may not work exactly on your database
system; check the system manuals
Database System Concepts - 7th Edition 5.12 ©Silberschatz, Korth and Sudarshan
When Not To Use Triggers (Cont.)
Risk of unintended execution of triggers, for example, when
Loading data from a backup copy
Replicating updates at a remote site
Trigger execution can be disabled before such actions.
Other risks with triggers:
Error leading to failure of critical transactions that set off the trigger
Cascading execution
Database System Concepts - 7th Edition 5.14 ©Silberschatz, Korth and Sudarshan
THANK YOU
Database System Concepts - 7th Edition 5.15 ©Silberschatz, Korth and Sudarshan