0% found this document useful (0 votes)
25 views33 pages

C05 Advanced SQL

Uploaded by

Sajjadur Rahman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views33 pages

C05 Advanced SQL

Uploaded by

Sajjadur Rahman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

Chapter 5: Advanced SQL

Database System Concepts, 7th Ed.


©Silberschatz, Korth and Sudarshan
See www.db-book.com for conditions on re-use
Outline

▪ Accessing SQL From a Programming Language


▪ Functions and Procedures
▪ Triggers
▪ Recursive Queries
▪ Advanced Aggregation Features

Database System Concepts - 7th Edition 5.2 ©Silberschatz, Korth and Sudarshan
Accessing SQL from a Programming Language

A database programmer must have access to a general-purpose programming language


for at least two reasons

▪ Not all queries can be expressed in SQL, since SQL does not provide the full
expressive power of a general-purpose language.
▪ Non-declarative actions -- such as printing a report, interacting with a user, or
sending the results of a query to a graphical user interface -- cannot be done
from within SQL.

Database System Concepts - 7th Edition 5.3 ©Silberschatz, Korth and Sudarshan
Accessing SQL from a Programming Language (Cont.)

There are two approaches to accessing SQL from a general-purpose programming


language

▪ A general-purpose program -- can connect to and communicate with a


database server using a collection of functions
▪ Embedded SQL -- provides a means by which a program can interact with a
database server.
• The SQL statements are translated at compile time into function calls.
• At runtime, these function calls connect to the database using an API
that provides dynamic SQL facilities.

Database System Concepts - 7th Edition 5.4 ©Silberschatz, Korth and Sudarshan
JDBC

Database System Concepts - 7th Edition 5.5 ©Silberschatz, Korth and Sudarshan
JDBC

▪ JDBC is a Java API for communicating with database systems supporting SQL.
▪ JDBC supports a variety of features for querying and updating data, and for
retrieving query results.
▪ JDBC also supports metadata retrieval, such as querying about relations present in
the database and the names and types of relation attributes.
▪ Model for communicating with the database:
• Open a connection
• Create a “statement” object
• Execute queries using the statement object to send queries and fetch results
• Exception mechanism to handle errors

Database System Concepts - 7th Edition 5.6 ©Silberschatz, Korth and Sudarshan
JDBC SUBSECTIONS

▪ Connecting to the Database


▪ Shipping SQL Statements to the Database System
▪ Exceptions and Resource Management
▪ Retrieving the Result of a Query
▪ Prepared Statements
▪ Callable Statements
▪ Metadata Features
▪ Other Features
▪ Database Access from Python

Database System Concepts - 7th Edition 5.7 ©Silberschatz, Korth and Sudarshan
ODBC

Database System Concepts - 7th Edition 5.8 ©Silberschatz, Korth and Sudarshan
ODBC

▪ Open DataBase Connectivity (ODBC) standard


• standard for application program to communicate with a database server.
• application program interface (API) to
▪ open a connection with a database,
▪ send queries and updates,
▪ get back results.
▪ Applications such as GUI, spreadsheets, etc. can use ODBC

Database System Concepts - 7th Edition 5.9 ©Silberschatz, Korth and Sudarshan
Embedded SQL

▪ The SQL standard defines embeddings of SQL in a variety of programming languages


such as C, C++, Java, Fortran, and PL/1,
▪ A language to which SQL queries are embedded is referred to as a host language, and
the SQL structures permitted in the host language comprise embedded SQL.
▪ The basic form of these languages follows that of the System R embedding of SQL into
PL/1.
▪ EXEC SQL statement is used in the host language to identify embedded SQL request
to the preprocessor
EXEC SQL <embedded SQL statement >;
Note: this varies by language:
• In some languages, like COBOL, the semicolon is replaced with END-EXEC
• In Java embedding uses # SQL { …. };

Database System Concepts - 7th Edition 5.10 ©Silberschatz, Korth and Sudarshan
Functions and Procedures

Database System Concepts - 7th Edition 5.11 ©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.12 ©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.13 ©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.14 ©Silberschatz, Korth and Sudarshan
SQL Procedures

▪ The dept_count function could instead be written as procedure:


create procedure dept_count_proc (in dept_name varchar(20),
out d_count integer)
begin
select count(*) into d_count
from instructor
where instructor.dept_name = dept_count_proc.dept_name
end
▪ The keywords in and out are parameters that are expected to have values assigned
to them and parameters whose values are set in the procedure in order to return
results.
▪ Procedures can be invoked either from an SQL procedure or from embedded SQL,
using the call statement.
declare d_count integer;
call dept_count_proc( 'Physics', d_count);

Database System Concepts - 7th Edition 5.15 ©Silberschatz, Korth and Sudarshan
SQL Procedures (Cont.)

▪ Procedures and functions can be invoked also from dynamic SQL


▪ SQL allows more than one procedure of the so long as the number of arguments
of the procedures with the same name is different.
▪ The name, along with the number of arguments, is used to identify the procedure.

Database System Concepts - 7th Edition 5.16 ©Silberschatz, Korth and Sudarshan
Language Constructs for Procedures & Functions

▪ SQL supports constructs that gives it almost all the power of a general-purpose
programming language.
• Warning: most database systems implement their own variant of the standard
syntax below.
▪ Compound statement: begin … end,
• May contain multiple SQL statements between begin and end.
• Local variables can be declared within a compound statements
▪ While and repeat statements:
• while boolean expression do
sequence of statements ;
end while

• repeat
sequence of statements ;
until boolean expression
end repeat

Database System Concepts - 7th Edition 5.17 ©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.18 ©Silberschatz, Korth and Sudarshan
Language Constructs – if-then-else

▪ Conditional statements (if-then-else)


if boolean expression
then statement or compound statement
elseif boolean expression
then statement or compound statement
else statement or compound statement
end if

Database System Concepts - 7th Edition 5.19 ©Silberschatz, Korth and Sudarshan
Example procedure

▪ Registers student after ensuring classroom capacity is not exceeded


• Returns 0 on success and -1 if capacity is exceeded
• See book (page 202) for details
▪ Signaling of exception conditions, and declaring handlers for exceptions
declare out_of_classroom_seats condition
declare exit handler for out_of_classroom_seats
begin

end
▪ The statements between the begin and the end can raise an exception by
executing “signal out_of_classroom_seats”
▪ The handler says that if the condition arises he action to be taken is to exit the
enclosing the begin end statement.

Database System Concepts - 7th Edition 5.20 ©Silberschatz, Korth and Sudarshan
Triggers

Database System Concepts - 7th Edition 5.21 ©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.22 ©Silberschatz, Korth and Sudarshan
Triggering Events and Actions in SQL

▪ Triggering event can be insert, delete or update


▪ Triggers on update can be restricted to specific attributes
• For example, after update of takes on grade
▪ Values of attributes before and after an update can be referenced
• referencing old row as : for deletes and updates
• referencing new row as : for inserts and updates
▪ Triggers can be activated before an event, which can serve as extra constraints.
For example, convert blank grades to null.
create trigger setnull_trigger before update of takes
referencing new row as nrow
for each row
when (nrow.grade = ' ')
begin atomic
set nrow.grade = null;
end;

Database System Concepts - 7th Edition 5.23 ©Silberschatz, Korth and Sudarshan
Trigger to Maintain credits_earned value

▪ create trigger credits_earned after update of takes on (grade)


referencing new row as nrow
referencing old row as orow
for each row
when nrow.grade <> 'F' and nrow.grade is not null
and (orow.grade = 'F' or orow.grade is null)
begin atomic
update student
set tot_cred= tot_cred +
(select credits
from course
where course.course_id= nrow.course_id)
where student.id = nrow.id;
end;

Database System Concepts - 7th Edition 5.24 ©Silberschatz, Korth and Sudarshan
Statement Level Triggers

▪ Instead of executing a separate action for each affected row, a single action can be
executed for all rows affected by a transaction
• Use for each statement instead of for each row
• Use referencing old table or referencing new table to refer to
temporary tables (called transition tables) containing the affected rows
• Can be more efficient when dealing with SQL statements that update a large
number of rows

Database System Concepts - 7th Edition 5.25 ©Silberschatz, Korth and Sudarshan
When Not To Use Triggers

▪ Triggers were used earlier for tasks such as


• Maintaining summary data (e.g., total salary of each department)
• Replicating databases by recording changes to special relations (called
change or delta relations) and having a separate process that applies the
changes over to a replica
▪ There are better ways of doing these now:
• Databases today provide built in materialized view facilities to maintain
summary data
• Databases provide built-in support for replication
▪ Encapsulation facilities can be used instead of triggers in many cases
• Define methods to update fields
• Carry out actions as part of the update methods instead of
through a trigger

Database System Concepts - 7th Edition 5.26 ©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.27 ©Silberschatz, Korth and Sudarshan
Recursive Queries

Database System Concepts - 7th Edition 5.28 ©Silberschatz, Korth and Sudarshan
Recursion in SQL

▪ SQL:1999 permits recursive view definition


▪ Example: find which courses are a prerequisite, whether directly or indirectly, for a
specific course
with recursive rec_prereq(course_id, prereq_id) as (
select course_id, prereq_id
from prereq
union
select rec_prereq.course_id, prereq.prereq_id,
from rec_rereq, prereq
where rec_prereq.prereq_id = prereq.course_id
)
select ∗
from rec_prereq;
This example view, rec_prereq, is called the transitive closure of the prereq
relation

Database System Concepts - 7th Edition 5.29 ©Silberschatz, Korth and Sudarshan
The Power of Recursion

▪ Recursive views make it possible to write queries, such as transitive closure


queries, that cannot be written without recursion or iteration.
• Intuition: Without recursion, a non-recursive non-iterative program can
perform only a fixed number of joins of prereq with itself
▪ This can give only a fixed number of levels of managers
▪ Given a fixed non-recursive query, we can construct a database with a
greater number of levels of prerequisites on which the query will not
work
▪ Alternative: write a procedure to iterate as many times as required
• See procedure findAllPrereqs in book

Database System Concepts - 7th Edition 5.30 ©Silberschatz, Korth and Sudarshan
The Power of Recursion

▪ Computing transitive closure using iteration, adding successive tuples to


rec_prereq
• The next slide shows a prereq relation
• Each step of the iterative process constructs an extended version of
rec_prereq from its recursive definition.
• The final result is called the fixed point of the recursive view definition.
▪ Recursive views are required to be monotonic. That is, if we add tuples to prereq
the view rec_prereq contains all of the tuples it contained before, plus possibly
more

Database System Concepts - 7th Edition 5.31 ©Silberschatz, Korth and Sudarshan
Example of Fixed-Point Computation

Database System Concepts - 7th Edition 5.32 ©Silberschatz, Korth and Sudarshan
End of Chapter 5

Database System Concepts - 7th Edition 5.33 ©Silberschatz, Korth and Sudarshan

You might also like