0% found this document useful (0 votes)
10 views45 pages

CH 5

Chapter 5 discusses advanced SQL concepts including accessing SQL from programming languages, embedded SQL, functions and procedures, and triggers. It explains how to connect programming languages to SQL databases, the use of embedded SQL for executing queries, and the creation of functions and procedures to encapsulate business logic. Additionally, it covers the implementation of triggers for automatic execution of actions in response to database modifications.

Uploaded by

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

CH 5

Chapter 5 discusses advanced SQL concepts including accessing SQL from programming languages, embedded SQL, functions and procedures, and triggers. It explains how to connect programming languages to SQL databases, the use of embedded SQL for executing queries, and the creation of functions and procedures to encapsulate business logic. Additionally, it covers the implementation of triggers for automatic execution of actions in response to database modifications.

Uploaded by

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

Chapter 5: Advanced SQ

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

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
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
General Purpose Programs

 There are two standards for connecting to an SQL database and


performing queries and updates.
 JDBC is an application program interface for the Java language
 ODBC is an application program interface originally developed for
the C language, and subsequently extended to other languages
such as C++, C#, Ruby, Go, PHP, and Visual Basic.
 We do not cover these in class. JDBC is a reading assignment and
will be part of Homework 2.

Database System Concepts - 7th Edition 5.5 ©Silberschatz, Korth and Sudarshan
Embedded SQL
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.

Database System Concepts - 7th Edition 5.7 ©Silberschatz, Korth and Sudarshan
Embedded SQL -- Basic Idea

 Specify in the host language the designated SQL server (say X)


 In the host language define a regular SQL query (say Y)
 In the host language instruct server X to execute query Y
 Server X executes the query and stores the result in a
temporary table
 In the host language instruct the SQL server to move the
temporary table (or part of it) to a location in the host language

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.)

 To write an embedded SQL query, we use the


declare c cursor for <SQL query>
statement. The variable c is used to identify the query
 Example:
 From within a host language, find the ID and name of students who
have completed more than the number of credits stored in the
variable credit_amount in the host langue
 Specify the query in SQL as follows:
EXEC SQL
declare c cursor for
select ID, name
from student
where tot_cred > :credit_amount
END_EXEC

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

 To execute a query “c”, the open statement must be used, which


causes the database system to execute the query and to save the
results within a temporary relation
 For our example, we use:
EXEC SQL open c ;
The query uses the value of the host-language variable credit-amount
at the time the open statement is executed.
 The fetch statement causes the values of one tuple in the query result
to be placed on host language variables.
EXEC SQL fetch c into :si, :sn END_EXEC

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.)

 A variable called SQLSTATE in the SQL communication area (SQLCA)


is set to '02000' to indicate no more data is available
 The close statement causes the database system to delete the
temporary relation that holds the result of the query.
EXEC SQL close c ;
Note: above details vary with language. For example, the Java
embedding defines Java iterators to step through result tuples.

Database System Concepts - 7th Edition 5.12 ©Silberschatz, Korth and Sudarshan
Updates Through Embedded SQL

 Embedded SQL expressions for database modification (update, insert,


and delete)
 Can update tuples fetched by cursor by declaring that the cursor is for
update
EXEC SQL
declare c cursor for
select *
from instructor
where dept_name = 'Music'
for update
 We then iterate through the tuples by performing fetch operations on the
cursor (as illustrated earlier), and after fetching each tuple we execute the
following code:
update instructor
set salary = salary + 1000
where current of c

Database System Concepts - 7th Edition 5.13 ©Silberschatz, Korth and Sudarshan
Example Embedded SQL Query

 Query: “Find all students whose ID is a prime number”


 We have an SQL table Students with an ID attribute
 How can we print only IDs which are primes using Python?

Database System Concepts - 7th Edition 5.14 ©Silberschatz, Korth and Sudarshan
Example Embedded SQL Query (Cont.)

 Initially fetch all student IDs from the database:

 `students` is now a list containing names and IDs.


 Write isPrime(x) function. Simple implementation in Python:

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

 Finally, run isPrime(x) for all IDs


 Print student names who have prime IDs:

 Question: How could we achieve better performance if we know


the largest ID?

Database System Concepts - 7th Edition 5.16 ©Silberschatz, Korth and Sudarshan
Functions and Procedures
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.18 ©Silberschatz, Korth and Sudarshan
Declaring SQL Functions

 Using PostgreSQL, 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 as $$
declare d_count integer;
begin
select count (* ) into d_count
from instructor
where instructor.dept_name = dept_name
return d_count;
end;
$$ language plpgsql;
 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.19 ©Silberschatz, Korth and Sudarshan
Declaring SQL Functions in PostgreSQL

create function outer_function()


returns void as $BODY_OUT$
begin
raise notice ‘Outer function called’;
execute $BODY_IN$
create or replace function inner_function()
returns text as $$
begin
return ‘Inner function called’;
end;
$$ language plpgsql;
$BODY_IN$;
raise notice ‘%’, inner_function();
end;
$BODY_OUT$ language plpgsql;

Database System Concepts - 7th Edition 5.20 ©Silberschatz, Korth and Sudarshan
PostgreSQL “Create or Replace” function

 In PostgreSQL, it is a common practice to use create or replace instead of


create when defining a function.
• Functions can easily be updated without being dropped.
• Permissions are maintained after function replacement.
• Errors are avoided when redefining existing function.
• Version control is provided.
 It is recommended to use create when a function is going to be defined
only once.

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

Return all instructors in a given department


 Function declaration:
create function instructor_of (dept_name char(20))
returns table (
ID varchar(5),
name varchar(20),
dept_name varchar(20),
salary numeric(8,2)) as $$
begin
return query
(select ID, name, dept_name, salary
from instructor
where instructor.dept_name =
instructor_of.dept_name)
end;
$$ language plpgsql;
 Function usage
select *
from instructor_of ('Music’);
Database System Concepts - 7th Edition 5.23 ©Silberschatz, Korth and Sudarshan
Language Constructs (Cont.)

 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

 SQL allows us to define functions in a programming language such as


Java, C#, C or C++.
 Can be more efficient than functions defined in SQL, and
computations that cannot be carried out in SQL can be executed by
these functions.
 Declaring external language procedures and functions

create procedure dept_count_proc (dept_name varchar(20),


out count integer)
language C
as '/usr/avi/bin/dept_count_proc’, ‘dept_count_proc’;

create function dept_count(dept_name varchar(20))


returns integer
language C
as '/usr/avi/bin/dept_count’, ’dept_count’;

Database System Concepts - 7th Edition 5.30 ©Silberschatz, Korth and Sudarshan
Security with External Language Routines

 To deal with security problems, we can do on of the following:


 Use sandbox techniques
 That is, use a safe language like Java, which cannot be used to
access/damage other parts of the database code.
 Run external language functions/procedures in a separate process,
with no access to the database process’ memory.
 Parameters and results communicated via inter-process
communication
 Both have performance overheads
 Many database systems support both above approaches as well as direct
executing in database system address space.

Database System Concepts - 7th Edition 5.32 ©Silberschatz, Korth and Sudarshan
Triggers
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.34 ©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.36 ©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.37 ©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.38 ©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.39 ©Silberschatz, Korth and Sudarshan
Risks of Triggers

 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.40 ©Silberschatz, Korth and Sudarshan
End of Chapter 5

Database System Concepts, 7th Ed.


©Silberschatz, Korth and Sudarshan
See www.db-book.com for conditions on re-use
JDBC
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.43 ©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);
}
}

NOTE: Above syntax works with Java 7, and JDBC 4 onwards.


Resources opened in “try (….)” syntax (“try with resources”) are
automatically closed at the end of the try block

Database System Concepts - 7th Edition 5.44 ©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.45 ©Silberschatz, Korth and Sudarshan
JDBC Resources

 JDBC Basics Tutorial


• https://docs.oracle.com/javase/tutorial/jdbc/index.html

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

 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.49 ©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.
 The statement EXEC SQL is used in the host language to identify
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 { …. };

Database System Concepts - 7th Edition 5.50 ©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.
 The statement EXEC SQL is used in the host language to identify
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 { …. };

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

 Variables of the host language can be used within embedded SQL


statements. They are preceded by a colon (:) to distinguish from SQL
variables (e.g., :credit_amount )
 Variables used (as above) must be declared within DECLARE section.
The syntax for declaring the variables, however, follows the usual host
language syntax.
EXEC-SQL BEGIN DECLARE SECTION
int credit-amount ;
EXEC-SQL END DECLARE SECTION;
 To write an embedded SQL query, we use the
declare c cursor for <SQL query>
statement. The variable c is used to identify the query

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

You might also like