CH 5
CH 5
Database System Concepts - 7th Edition 5.2 ©Silberschatz, Korth and Sudarshan
Accessing SQL from a Programming Language
Database System Concepts - 7th Edition 5.3 ©Silberschatz, Korth and Sudarshan
SQL from a Programming Language (Cont.)
Database System Concepts - 7th Edition 5.4 ©Silberschatz, Korth and Sudarshan
General Purpose Programs
Database System Concepts - 7th Edition 5.5 ©Silberschatz, Korth and Sudarshan
Embedded SQL
Embedded SQL
Database System Concepts - 7th Edition 5.7 ©Silberschatz, Korth and Sudarshan
Embedded SQL -- Basic Idea
Database System Concepts - 7th Edition 5.8 ©Silberschatz, Korth and Sudarshan
Embedded SQL Basic Language Structures
The statement EXEC SQL is used in the host language to identify the
embedded SQL request to the preprocessor
EXEC SQL <embedded statement>;
Note: this varies by language:
In some languages, like COBOL, the semicolon is replaced with
END-EXEC
In Java embedding uses # SQL { …. };
Before executing any SQL statements, the program must first connect
to the database. This is done using:
EXEC-SQL connect to server user user-name using password;
The, server identifies the database server to which a connection is to be
established.
Database System Concepts - 7th Edition 5.9 ©Silberschatz, Korth and Sudarshan
Embedded SQL (Cont.)
Database System Concepts - 7th Edition 5.10 ©Silberschatz, Korth and Sudarshan
Embedded SQL (Cont.)
To get all the relevant tuples, one must issue repeated calls to fetch to
get successive tuples in the query result
Database System Concepts - 7th Edition 5.11 ©Silberschatz, Korth and Sudarshan
Embedded SQL (Cont.)
Database System Concepts - 7th Edition 5.12 ©Silberschatz, Korth and Sudarshan
Updates Through Embedded SQL
Database System Concepts - 7th Edition 5.13 ©Silberschatz, Korth and Sudarshan
Example Embedded SQL Query
Database System Concepts - 7th Edition 5.14 ©Silberschatz, Korth and Sudarshan
Example Embedded SQL Query (Cont.)
Database System Concepts - 7th Edition 5.15 ©Silberschatz, Korth and Sudarshan
Example Embedded SQL Query (Cont.)
Database System Concepts - 7th Edition 5.16 ©Silberschatz, Korth and Sudarshan
Functions and Procedures
Functions and Procedures
Database System Concepts - 7th Edition 5.18 ©Silberschatz, Korth and Sudarshan
Declaring SQL Functions
Database System Concepts - 7th Edition 5.19 ©Silberschatz, Korth and Sudarshan
Declaring SQL Functions in PostgreSQL
Database System Concepts - 7th Edition 5.20 ©Silberschatz, Korth and Sudarshan
PostgreSQL “Create or Replace” function
Database System Concepts - 7th Edition 5.21 ©Silberschatz, Korth and Sudarshan
Table Functions
The SQL standard supports functions that can return tables as results
Such functions are called table functions
Syntax:
create function <name> (…)
returns table (
Attribute 1
Attribute 2
…
) as $$
return query
SQL query
end;
$$ language plpgsql;
Note that there is a difference between returns and returns
Database System Concepts - 7th Edition 5.22 ©Silberschatz, Korth and Sudarshan
Table Functions Example
For loop
Permits iteration over all results of a query
record is a placeholder data type for different sets of columns
during loop iteration
Example: Find the total budget of all departments
declare
n integer default 0;
r record;
begin
for r in
select budget from department
where dept_name = 'Music'
loop
n := n + r.budget
end loop;
Database System Concepts - 7th Edition 5.27 ©Silberschatz, Korth and Sudarshan
External Language Routines
Database System Concepts - 7th Edition 5.30 ©Silberschatz, Korth and Sudarshan
Security with External Language Routines
Database System Concepts - 7th Edition 5.32 ©Silberschatz, Korth and Sudarshan
Triggers
Triggers
Database System Concepts - 7th Edition 5.34 ©Silberschatz, Korth and Sudarshan
Trigger to Maintain credits_earned value
Database System Concepts - 7th Edition 5.36 ©Silberschatz, Korth and Sudarshan
Statement Level Triggers
Database System Concepts - 7th Edition 5.37 ©Silberschatz, Korth and Sudarshan
When Not To Use Triggers
Database System Concepts - 7th Edition 5.38 ©Silberschatz, Korth and Sudarshan
When Not To Use Triggers (Cont.)
Database System Concepts - 7th Edition 5.39 ©Silberschatz, Korth and Sudarshan
Risks of Triggers
Database System Concepts - 7th Edition 5.40 ©Silberschatz, Korth and Sudarshan
End of Chapter 5
Database System Concepts - 7th Edition 5.43 ©Silberschatz, Korth and Sudarshan
JDBC Code
Database System Concepts - 7th Edition 5.44 ©Silberschatz, Korth and Sudarshan
JDBC Subsections
Database System Concepts - 7th Edition 5.45 ©Silberschatz, Korth and Sudarshan
JDBC Resources
Database System Concepts - 7th Edition 5.46 ©Silberschatz, Korth and Sudarshan
SQLJ
JDBC is overly dynamic, errors cannot be caught by compiler
SQLJ: embedded SQL in Java
#sql iterator deptInfoIter ( String dept name, int avgSal);
deptInfoIter iter = null;
#sql iter = { select dept_name, avg(salary) from instructor
group by dept name };
while (iter.next()) {
String deptName = iter.dept_name();
int avgSal = iter.avgSal();
System.out.println(deptName + " " + avgSal);
}
iter.close();
Database System Concepts - 7th Edition 5.47 ©Silberschatz, Korth and Sudarshan
ODBC
ODBC
Database System Concepts - 7th Edition 5.49 ©Silberschatz, Korth and Sudarshan
Embedded SQL
Database System Concepts - 7th Edition 5.50 ©Silberschatz, Korth and Sudarshan
Embedded SQL
Database System Concepts - 7th Edition 5.51 ©Silberschatz, Korth and Sudarshan
Embedded SQL (Cont.)
Database System Concepts - 7th Edition 5.52 ©Silberschatz, Korth and Sudarshan