C05 Advanced SQL
C05 Advanced SQL
Database System Concepts - 7th Edition 5.2 ©Silberschatz, Korth and Sudarshan
Accessing SQL from a Programming Language
▪ 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.)
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
Database System Concepts - 7th Edition 5.7 ©Silberschatz, Korth and Sudarshan
ODBC
Database System Concepts - 7th Edition 5.8 ©Silberschatz, Korth and Sudarshan
ODBC
Database System Concepts - 7th Edition 5.9 ©Silberschatz, Korth and Sudarshan
Embedded 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
Database System Concepts - 7th Edition 5.15 ©Silberschatz, Korth and Sudarshan
SQL Procedures (Cont.)
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
Database System Concepts - 7th Edition 5.18 ©Silberschatz, Korth and Sudarshan
Language Constructs – if-then-else
Database System Concepts - 7th Edition 5.19 ©Silberschatz, Korth and Sudarshan
Example procedure
Database System Concepts - 7th Edition 5.20 ©Silberschatz, Korth and Sudarshan
Triggers
Database System Concepts - 7th Edition 5.21 ©Silberschatz, Korth and Sudarshan
Triggers
Database System Concepts - 7th Edition 5.22 ©Silberschatz, Korth and Sudarshan
Triggering Events and Actions in SQL
Database System Concepts - 7th Edition 5.23 ©Silberschatz, Korth and Sudarshan
Trigger to Maintain credits_earned value
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
Database System Concepts - 7th Edition 5.26 ©Silberschatz, Korth and Sudarshan
When Not To Use Triggers (Cont.)
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
Database System Concepts - 7th Edition 5.29 ©Silberschatz, Korth and Sudarshan
The Power of Recursion
Database System Concepts - 7th Edition 5.30 ©Silberschatz, Korth and Sudarshan
The Power of Recursion
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