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 Code
public static void JDBCexample(String dbid, String userid, String passwd)
{
try (Connection conn = DriverManager.getConnection(
"jdbc:oracle:thin:@db.yale.edu:2000:univdb", userid, passwd);
Statement stmt = conn.createStatement();
)
{
… Do Actual Work ….
}
catch (SQLException sqle) {
System.out.println("SQLException : " + sqle);
}
}
Database System Concepts - 7th Edition 5.7 ©Silberschatz, Korth and Sudarshan
JDBC Code (Cont.)
Update to database
try {
stmt.executeUpdate(
"insert into instructor values('77987', 'Kim', 'Physics', 98000)");
} catch (SQLException sqle)
{
System.out.println("Could not insert tuple. " + sqle);
}
Execute query and fetch and print results
ResultSet rset = stmt.executeQuery(
"select dept_name, avg (salary)
from instructor
group by dept_name");
while (rset.next()) {
System.out.println(rset.getString("dept_name") + " " +
rset.getFloat(2));
}
Database System Concepts - 7th Edition 5.9 ©Silberschatz, Korth and Sudarshan
JDBC Resources
JDBC Basics Tutorial
• https://docs.oracle.com/javase/tutorial/jdbc/index.html
Database System Concepts - 7th Edition 5.10 ©Silberschatz, Korth and Sudarshan
ODBC
Database System Concepts - 7th Edition 5.11 ©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.12 ©Silberschatz, Korth and Sudarshan
Database System Concepts - 7th Edition 5.14 ©Silberschatz, Korth and Sudarshan
Database System Concepts - 7th Edition 5.15 ©Silberschatz, Korth and Sudarshan
Dynamic SQL
Dynamic SQL involves the creation and execution of SQL
statements at runtime.
Dynamic SQL allows developers to generate SQL statements
dynamically based on runtime conditions or user input.
Dynamic SQL is a good choice when the SQL statements need to
change in response to evolving needs or user inputs.
Database System Concepts - 7th Edition 5.16 ©Silberschatz, Korth and Sudarshan
Functions and Procedures
Database System Concepts - 7th Edition 5.18 ©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.19 ©Silberschatz, Korth and Sudarshan
Syntax:
CREATE [OR REPLACE] FUNCTION function_name
[(parameter_name type [, …])]
// this statement is must for functions
RETURN return_datatype
{IS | AS}
BEGIN
// program code
[EXCEPTION
exception_section;
END [function_name];
Database System Concepts - 7th Edition 5.20 ©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.21 ©Silberschatz, Korth and Sudarshan
SQL Procedures
A stored procedure is a prepared SQL code that you can save, so the code
can be reused over and over again.
Syntax
CREATE PROCEDURE procedure_name
Begin
sql_statement
End;
Execute a Stored Procedure
EXEC procedure_name;
Database System Concepts - 7th Edition 5.22 ©Silberschatz, Korth and Sudarshan
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
Database System Concepts - 7th Edition 5.23 ©Silberschatz, Korth and Sudarshan
SQL Procedures (Cont.)
Procedures can be invoked either from an SQL procedure or from
embedded SQL, using the call statement.
Procedures and functions can be invoked also from dynamic SQL
SQL allows more than one procedure with the same name, 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.24 ©Silberschatz, Korth and Sudarshan
Triggers
Database System Concepts - 7th Edition 5.25 ©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.
Database System Concepts - 7th Edition 5.26 ©Silberschatz, Korth and Sudarshan
Database System Concepts - 7th Edition 5.27 ©Silberschatz, Korth and Sudarshan
Database System Concepts - 7th Edition 5.30 ©Silberschatz, Korth and Sudarshan