SlideShare a Scribd company logo
Database System Concepts, 7th
Ed.
©Silberschatz, Korth and Sudarshan
See www.db-book.com for conditions on re-use
Chapter 5: Advanced SQL
©Silberschatz, Korth and Sudarshan
5.2
Database System Concepts - 7th
Edition
Outline
 Accessing SQL From a Programming Language
 Functions and Procedures
 Triggers
 Recursive Queries
 Advanced Aggregation Features
©Silberschatz, Korth and Sudarshan
5.3
Database System Concepts - 7th
Edition
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.
A database programmer must have access to a general-purpose programming
language for at least two reasons
©Silberschatz, Korth and Sudarshan
5.4
Database System Concepts - 7th
Edition
Accessing SQL from a Programming Language (Cont.)
 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.
There are two approaches to accessing SQL from a general-purpose
programming language
©Silberschatz, Korth and Sudarshan
5.5
Database System Concepts - 7th
Edition
JDBC
©Silberschatz, Korth and Sudarshan
5.6
Database System Concepts - 7th
Edition
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
©Silberschatz, Korth and Sudarshan
5.7
Database System Concepts - 7th
Edition
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
©Silberschatz, Korth and Sudarshan
5.8
Database System Concepts - 7th
Edition
JDBC Code for Older Versions of Java/JDBC
public static void JDBCexample(String dbid, String userid, String passwd)
{
try {
Class.forName ("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection(
"jdbc:oracle:thin:@db.yale.edu:2000:univdb", userid, passwd);
Statement stmt = conn.createStatement();
… Do Actual Work ….
stmt.close();
conn.close();
}
catch (SQLException sqle) {
System.out.println("SQLException : " + sqle);
}
}
NOTE: Class.forName is not required from JDBC 4 onwards. The try with
resources syntax in prev slide is preferred for Java 7 onwards.
©Silberschatz, Korth and Sudarshan
5.9
Database System Concepts - 7th
Edition
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));
}
©Silberschatz, Korth and Sudarshan
5.10
Database System Concepts - 7th
Edition
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
©Silberschatz, Korth and Sudarshan
5.11
Database System Concepts - 7th
Edition
JDBC Code Details
 Getting result fields:
 rs.getString(“dept_name”) and rs.getString(1) equivalent if
dept_name is the first argument of select result.
 Dealing with Null values
int a = rs.getInt(“a”);
if (rs.wasNull()) Systems.out.println(“Got null value”);
©Silberschatz, Korth and Sudarshan
5.12
Database System Concepts - 7th
Edition
Prepared Statement
 PreparedStatement pStmt = conn.prepareStatement(
"insert into instructor values(?,?,?,?)");
pStmt.setString(1, "88877");
pStmt.setString(2, "Perry");
pStmt.setString(3, "Finance");
pStmt.setInt(4, 125000);
pStmt.executeUpdate();
pStmt.setString(1, "88878");
pStmt.executeUpdate();
 WARNING: always use prepared statements when taking an input from the
user and adding it to a query
 NEVER create a query by concatenating strings
 "insert into instructor values(' " + ID + " ', ' " + name + " ', " + " ' + dept
name + " ', " ' balance + ')“
 What if name is “D'Souza”?
©Silberschatz, Korth and Sudarshan
5.13
Database System Concepts - 7th
Edition
SQL Injection
 Suppose query is constructed using
 "select * from instructor where name = '" + name + "'"
 Suppose the user, instead of entering a name, enters:
 X' or 'Y' = 'Y
 then the resulting statement becomes:
 "select * from instructor where name = '" + "X' or 'Y' = 'Y" + "'"
 which is:
 select * from instructor where name = 'X' or 'Y' = 'Y'
 User could have even used
 X'; update instructor set salary = salary + 10000; --
 Prepared stament internally uses:
"select * from instructor where name = 'X' or 'Y' = 'Y'
 Always use prepared statements, with user inputs as parameters
©Silberschatz, Korth and Sudarshan
5.14
Database System Concepts - 7th
Edition
Metadata Features
 ResultSet metadata
 E.g.after executing query to get a ResultSet rs:
 ResultSetMetaData rsmd = rs.getMetaData();
for(int i = 1; i <= rsmd.getColumnCount(); i++) {
System.out.println(rsmd.getColumnName(i));
System.out.println(rsmd.getColumnTypeName(i));
}
 How is this useful?
©Silberschatz, Korth and Sudarshan
5.15
Database System Concepts - 7th
Edition
Metadata (Cont)
 Database metadata
 DatabaseMetaData dbmd = conn.getMetaData();
// Arguments to getColumns: Catalog, Schema-pattern, Table-pattern,
// and Column-Pattern
// Returns: One row for each column; row has a number of attributes
// such as COLUMN_NAME, TYPE_NAME
// The value null indicates all Catalogs/Schemas.
// The value “” indicates current catalog/schema
// The value “%” has the same meaning as SQL like clause
ResultSet rs = dbmd.getColumns(null, "univdb", "department", "%");
while( rs.next()) {
System.out.println(rs.getString("COLUMN_NAME"),
rs.getString("TYPE_NAME");
}
 And where is this useful?
©Silberschatz, Korth and Sudarshan
5.16
Database System Concepts - 7th
Edition
Metadata (Cont)
 Database metadata
 DatabaseMetaData dbmd = conn.getMetaData();
// Arguments to getTables: Catalog, Schema-pattern, Table-pattern,
// and Table-Type
// Returns: One row for each table; row has a number of attributes
// such as TABLE_NAME, TABLE_CAT, TABLE_TYPE, ..
// The value null indicates all Catalogs/Schemas.
// The value “” indicates current catalog/schema
// The value “%” has the same meaning as SQL like clause
// The last attribute is an array of types of tables to return.
// TABLE means only regular tables
ResultSet rs = dbmd.getTables (“”, "", “%", new String[] {“TABLES”});
while( rs.next()) {
System.out.println(rs.getString(“TABLE_NAME“));
}
 And where is this useful?
©Silberschatz, Korth and Sudarshan
5.17
Database System Concepts - 7th
Edition
Finding Primary Keys
 DatabaseMetaData dmd = connection.getMetaData();
// Arguments below are: Catalog, Schema, and Table
// The value “” for Catalog/Schema indicates current catalog/schema
// The value null indicates all catalogs/schemas
ResultSet rs = dmd.getPrimaryKeys(“”, “”, tableName);
while(rs.next()){
// KEY_SEQ indicates the position of the attribute in
// the primary key, which is required if a primary key has multiple
// attributes
System.out.println(rs.getString(“KEY_SEQ”),
rs.getString("COLUMN_NAME");
}
©Silberschatz, Korth and Sudarshan
5.18
Database System Concepts - 7th
Edition
Transaction Control in JDBC
 By default, each SQL statement is treated as a separate transaction that
is committed automatically
 bad idea for transactions with multiple updates
 Can turn off automatic commit on a connection
 conn.setAutoCommit(false);
 Transactions must then be committed or rolled back explicitly
 conn.commit(); or
 conn.rollback();
 conn.setAutoCommit(true) turns on automatic commit.
©Silberschatz, Korth and Sudarshan
5.19
Database System Concepts - 7th
Edition
Other JDBC Features
 Calling functions and procedures
 CallableStatement cStmt1 = conn.prepareCall("{? = call some
function(?)}");
 CallableStatement cStmt2 = conn.prepareCall("{call some
procedure(?,?)}");
 Handling large object types
 getBlob() and getClob() that are similar to the getString() method, but
return objects of type Blob and Clob, respectively
 get data from these objects by getBytes()
 associate an open stream with Java Blob or Clob object to update large
objects
 blob.setBlob(int parameterIndex, InputStream inputStream).
©Silberschatz, Korth and Sudarshan
5.20
Database System Concepts - 7th
Edition
JDBC Resources
 JDBC Basics Tutorial
• https://docs.oracle.com/javase/tutorial/jdbc/index.html
©Silberschatz, Korth and Sudarshan
5.21
Database System Concepts - 7th
Edition
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();
©Silberschatz, Korth and Sudarshan
5.22
Database System Concepts - 7th
Edition
ODBC
©Silberschatz, Korth and Sudarshan
5.23
Database System Concepts - 7th
Edition
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
©Silberschatz, Korth and Sudarshan
5.24
Database System Concepts - 7th
Edition
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 { …. };
©Silberschatz, Korth and Sudarshan
5.25
Database System Concepts - 7th
Edition
Embedded SQL (Cont.)
 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;
Here, server identifies the server to which a connection is to be
established.
 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, as
illustrated below. 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;
©Silberschatz, Korth and Sudarshan
5.26
Database System Concepts - 7th
Edition
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 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
©Silberschatz, Korth and Sudarshan
5.27
Database System Concepts - 7th
Edition
Embedded SQL (Cont.)
 The open statement for our example is as follows:
EXEC SQL open c ;
This statement causes the database system to execute the query and to
save the results within a temporary relation. 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
Repeated calls to fetch get successive tuples in the query result
©Silberschatz, Korth and Sudarshan
5.28
Database System Concepts - 7th
Edition
Embedded SQL (Cont.)
 A variable called SQLSTATE in the SQL communication area (SQLCA)
gets 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.
©Silberschatz, Korth and Sudarshan
5.29
Database System Concepts - 7th
Edition
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
©Silberschatz, Korth and Sudarshan
5.30
Database System Concepts - 7th
Edition
Functions and Procedures
©Silberschatz, Korth and Sudarshan
5.31
Database System Concepts - 7th
Edition
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.
©Silberschatz, Korth and Sudarshan
5.32
Database System Concepts - 7th
Edition
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
©Silberschatz, Korth and Sudarshan
5.33
Database System Concepts - 7th
Edition
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'))
©Silberschatz, Korth and Sudarshan
5.37
Database System Concepts - 7th
Edition
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
©Silberschatz, Korth and Sudarshan
5.40
Database System Concepts - 7th
Edition
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 SQLcan be executed by
these functions.
 Declaring external language procedures and functions
create procedure dept_count_proc(in dept_name varchar(20),
out count integer)
language C
external name '/usr/avi/bin/dept_count_proc'
create function dept_count(dept_name varchar(20))
returns integer
language C
external name '/usr/avi/bin/dept_count'
©Silberschatz, Korth and Sudarshan
5.42
Database System Concepts - 7th
Edition
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.
©Silberschatz, Korth and Sudarshan
5.43
Database System Concepts - 7th
Edition
Triggers
©Silberschatz, Korth and Sudarshan
5.44
Database System Concepts - 7th
Edition
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
©Silberschatz, Korth and Sudarshan
5.46
Database System Concepts - 7th
Edition
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;
©Silberschatz, Korth and Sudarshan
5.47
Database System Concepts - 7th
Edition
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
©Silberschatz, Korth and Sudarshan
5.48
Database System Concepts - 7th
Edition
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
©Silberschatz, Korth and Sudarshan
5.49
Database System Concepts - 7th
Edition
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
©Silberschatz, Korth and Sudarshan
5.50
Database System Concepts - 7th
Edition
Recursive Queries
©Silberschatz, Korth and Sudarshan
5.51
Database System Concepts - 7th
Edition
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
©Silberschatz, Korth and Sudarshan
5.52
Database System Concepts - 7th
Edition
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
©Silberschatz, Korth and Sudarshan
5.54
Database System Concepts - 7th
Edition
Example of Fixed-Point Computation
©Silberschatz, Korth and Sudarshan
5.55
Database System Concepts - 7th
Edition
Advanced Aggregation Features
©Silberschatz, Korth and Sudarshan
5.56
Database System Concepts - 7th
Edition
Ranking
 Ranking is done in conjunction with an order by specification.
 Suppose we are given a relation
student_grades(ID, GPA)
giving the grade-point average of each student
 Find the rank of each student.
 select ID, rank() over (order by GPA desc) as s_rank
from student_grades
 An extra order by clause is needed to get them in sorted order
select ID, rank() over (order by GPA desc) as s_rank
from student_grades
order by s_rank
 Ranking may leave gaps: e.g. if 2 students have the same top GPA, both have
rank 1, and the next rank is 3
• dense_rank does not leave gaps, so next dense rank would be 2
©Silberschatz, Korth and Sudarshan
5.57
Database System Concepts - 7th
Edition
Ranking
 Ranking can be done using basic SQL aggregation, but resultant query is
very inefficient
select ID, (1 + (select count(*)
from student_grades B
where B.GPA > A.GPA)) as s_rank
from student_grades A
order by s_rank;
©Silberschatz, Korth and Sudarshan
5.58
Database System Concepts - 7th
Edition
Ranking (Cont.)
 Ranking can be done within partition of the data.
 “Find the rank of students within each department.”
select ID, dept_name,
rank () over (partition by dept_name order by GPA desc)
as dept_rank
from dept_grades
order by dept_name, dept_rank;
 Multiple rank clauses can occur in a single select clause.
 Ranking is done after applying group by clause/aggregation
 Can be used to find top-n results
 More general than the limit n clause supported by many databases,
since it allows top-n within each partition
©Silberschatz, Korth and Sudarshan
5.59
Database System Concepts - 7th
Edition
Ranking (Cont.)
 Other ranking functions:
• percent_rank (within partition, if partitioning is done)
• cume_dist (cumulative distribution)
 fraction of tuples with preceding values
• row_number (non-deterministic in presence of duplicates)
 SQL:1999 permits the user to specify nulls first or nulls last
select ID,
rank ( ) over (order by GPA desc nulls last) as s_rank
from student_grades
©Silberschatz, Korth and Sudarshan
5.60
Database System Concepts - 7th
Edition
Ranking (Cont.)
 For a given constant n, the ranking the function ntile(n) takes the tuples in
each partition in the specified order, and divides them into n buckets with
equal numbers of tuples.
 E.g.,
select ID, ntile(4) over (order by GPA desc) as quartile
from student_grades;
©Silberschatz, Korth and Sudarshan
5.61
Database System Concepts - 7th
Edition
Windowing
 Used to smooth out random variations.
 E.g., moving average: “Given sales values for each date, calculate for
each date the average of the sales on that day, the previous day, and the
next day”
 Window specification in SQL:
• Given relation sales(date, value)
select date, sum(value) over
(order by date between rows 1 preceding and 1 following)
from sales
©Silberschatz, Korth and Sudarshan
5.62
Database System Concepts - 7th
Edition
Windowing
 Examples of other window specifications:
• between rows unbounded preceding and current
• rows unbounded preceding
• range between 10 preceding and current row
 All rows with values between current row value –10 to current value
• range interval 10 day preceding
 Not including current row
©Silberschatz, Korth and Sudarshan
5.63
Database System Concepts - 7th
Edition
Windowing (Cont.)
 Can do windowing within partitions
 E.g., Given a relation transaction (account_number, date_time, value),
where value is positive for a deposit and negative for a withdrawal
• “Find total balance of each account after each transaction on the
account”
select account_number, date_time,
sum (value) over
(partition by account_number
order by date_time
rows unbounded preceding)
as balance
from transaction
order by account_number, date_time
©Silberschatz, Korth and Sudarshan
5.64
Database System Concepts - 7th
Edition
OLAP
©Silberschatz, Korth and Sudarshan
5.65
Database System Concepts - 7th
Edition
Data Analysis and OLAP
 Online Analytical Processing (OLAP)
• Interactive analysis of data, allowing data to be summarized and
viewed in different ways in an online fashion (with negligible delay)
 Data that can be modeled as dimension attributes and measure attributes
are called multidimensional data.
• Measure attributes
 measure some value
 can be aggregated upon
 e.g., the attribute number of the sales relation
• Dimension attributes
 define the dimensions on which measure attributes (or aggregates
thereof) are viewed
 e.g., attributes item_name, color, and size of the sales relation
©Silberschatz, Korth and Sudarshan
5.66
Database System Concepts - 7th
Edition
Example sales relation
...
...
...
...
...
...
...
...
©Silberschatz, Korth and Sudarshan
5.67
Database System Concepts - 7th
Edition
Cross Tabulation of sales by item_name and color
 The table above is an example of a cross-tabulation (cross-tab), also
referred to as a pivot-table.
• Values for one of the dimension attributes form the row headers
• Values for another dimension attribute form the column headers
• Other dimension attributes are listed on top
• Values in individual cells are (aggregates of) the values of the
dimension attributes that specify the cell.
©Silberschatz, Korth and Sudarshan
5.68
Database System Concepts - 7th
Edition
Data Cube
 A data cube is a multidimensional generalization of a cross-tab
 Can have n dimensions; we show 3 below
 Cross-tabs can be used as views on a data cube
©Silberschatz, Korth and Sudarshan
5.70
Database System Concepts - 7th
Edition
Cross Tabulation With Hierarchy
 Cross-tabs can be easily extended to deal with hierarchies
• Can drill down or roll up on a hierarchy
©Silberschatz, Korth and Sudarshan
5.71
Database System Concepts - 7th
Edition
Relational Representation of Cross-tabs
 Cross-tabs can be represented
as relations
• We use the value all is used
to represent aggregates.
• The SQL standard actually
uses null values in place of
all despite confusion with
regular null values.
©Silberschatz, Korth and Sudarshan
5.72
Database System Concepts - 7th
Edition
Extended Aggregation to Support OLAP
 The cube operation computes union of group by’s on every subset of the
specified attributes
 Example relation for this section
sales(item_name, color, clothes_size, quantity)
 E.g., consider the query
select item_name, color, size, sum(number)
from sales
group by cube(item_name, color, size)
This computes the union of eight different groupings of the sales relation:
{ (item_name, color, size), (item_name, color),
(item_name, size), (color, size),
(item_name), (color),
(size), ( ) }
where ( ) denotes an empty group by list.
 For each grouping, the result contains the null value
for attributes not present in the grouping.
©Silberschatz, Korth and Sudarshan
5.73
Database System Concepts - 7th
Edition
Online Analytical Processing Operations
 Relational representation of cross-tab that we saw earlier, but with null in
place of all, can be computed by
 select item_name, color, sum(number)
from sales
group by cube(item_name, color)
 The function grouping() can be applied on an attribute
• Returns 1 if the value is a null value representing all, and returns 0 in all
other cases.
select item_name, color, size, sum(number),
grouping(item_name) as item_name_flag,
grouping(color) as color_flag,
grouping(size) as size_flag,
from sales
group by cube(item_name, color, size)
©Silberschatz, Korth and Sudarshan
5.74
Database System Concepts - 7th
Edition
Online Analytical Processing Operations
 Can use the function decode() in the select clause to replace
such nulls by a value such as all
• E.g., replace item_name in first query by
decode( grouping(item_name), 1, ‘all’, item_name)
©Silberschatz, Korth and Sudarshan
5.75
Database System Concepts - 7th
Edition
Extended Aggregation (Cont.)
 The rollup construct generates union on every prefix of specified list of
attributes
 E.g.,
select item_name, color, size, sum(number)
from sales
group by rollup(item_name, color, size)
• Generates union of four groupings:
{ (item_name, color, size), (item_name, color), (item_name), ( ) }
 Rollup can be used to generate aggregates at multiple levels of a
hierarchy.
 E.g., suppose table itemcategory(item_name, category) gives the category of
each item. Then
select category, item_name, sum(number)
from sales, itemcategory
where sales.item_name = itemcategory.item_name
group by rollup(category, item_name)
would give a hierarchical summary by item_name and by category.
©Silberschatz, Korth and Sudarshan
5.76
Database System Concepts - 7th
Edition
Extended Aggregation (Cont.)
 Multiple rollups and cubes can be used in a single group by clause
• Each generates set of group by lists, cross product of sets gives overall
set of group by lists
 E.g.,
select item_name, color, size, sum(number)
from sales
group by rollup(item_name), rollup(color, size)
generates the groupings
{item_name, ()} X {(color, size), (color), ()}
= { (item_name, color, size), (item_name, color), (item_name),
(color, size), (color), ( ) }
©Silberschatz, Korth and Sudarshan
5.77
Database System Concepts - 7th
Edition
Online Analytical Processing Operations
 Pivoting: changing the dimensions used in a cross-tab is called
 Slicing: creating a cross-tab for fixed values only
• Sometimes called dicing, particularly when values for multiple
dimensions are fixed.
 Rollup: moving from finer-granularity data to a coarser granularity
 Drill down: The opposite operation - that of moving from coarser-
granularity data to finer-granularity data
©Silberschatz, Korth and Sudarshan
5.78
Database System Concepts - 7th
Edition
OLAP Implementation
 The earliest OLAP systems used multidimensional arrays in memory to
store data cubes, and are referred to as multidimensional OLAP (MOLAP)
systems.
 OLAP implementations using only relational database features are called
relational OLAP (ROLAP) systems
 Hybrid systems, which store some summaries in memory and store the
base data and other summaries in a relational database, are called hybrid
OLAP (HOLAP) systems.
©Silberschatz, Korth and Sudarshan
5.79
Database System Concepts - 7th
Edition
OLAP Implementation (Cont.)
 Early OLAP systems precomputed all possible aggregates in order to
provide online response
 Space and time requirements for doing so can be very high
 2n
combinations of group by
 It suffices to precompute some aggregates, and compute others on
demand from one of the precomputed aggregates
 Can compute aggregate on (item_name, color) from an aggregate
on (item_name, color, size)
– For all but a few “non-decomposable” aggregates such as
median
– is cheaper than computing it from scratch
 Several optimizations available for computing multiple aggregates
 Can compute aggregate on (item_name, color) from an aggregate on
(item_name, color, size)
 Can compute aggregates on (item_name, color, size),
(item_name, color) and (item_name) using a single sorting
of the base data
©Silberschatz, Korth and Sudarshan
5.80
Database System Concepts - 7th
Edition
End of Chapter 5

More Related Content

PDF
SQL PROFESSIONAL FOR DATABASE MANAGEMENT SYSTEM
PPT
database management systems presentation ch5.ppt
PPTX
Jdbc
PPT
JDBC (2).ppt
PPT
JDBC.ppt JDBC_FM_2012_201JDBC_FM_2012_201
PPT
JDBC_Template for database connection using Spring
PPTX
Intonation to SQL using the query language postgresSQL.pptx
PPT
SQL PROFESSIONAL FOR DATABASE MANAGEMENT SYSTEM
database management systems presentation ch5.ppt
Jdbc
JDBC (2).ppt
JDBC.ppt JDBC_FM_2012_201JDBC_FM_2012_201
JDBC_Template for database connection using Spring
Intonation to SQL using the query language postgresSQL.pptx

Similar to ch5 - Copy.pptx advanced structure query language (20)

PDF
Lecture17
PPTX
Module 5 jdbc.ppt
PDF
Jdbc 1
PPT
Jdbc sasidhar
PPTX
Jdbc ja
PPTX
Sql.pptx
PPT
ch3.ppt
PPT
JDBC Connecticity.ppt
PPTX
PDF
Introduction to sql.pdf Database Systems
PPT
The SQL data-definition language (DDL) allows the specification of informatio...
PPT
SQL manages and queries database data.ppt
PPT
ER model and diagram in database system
PPT
DBMS_INTRODUCTION OF SQL
PPT
PPTX
ch3.pptx SQL in DBMS all Chapter with details
DOC
Java database connectivity notes for undergraduate
PPT
ch3.ppt
PDF
PPT
ch3.ppt
Lecture17
Module 5 jdbc.ppt
Jdbc 1
Jdbc sasidhar
Jdbc ja
Sql.pptx
ch3.ppt
JDBC Connecticity.ppt
Introduction to sql.pdf Database Systems
The SQL data-definition language (DDL) allows the specification of informatio...
SQL manages and queries database data.ppt
ER model and diagram in database system
DBMS_INTRODUCTION OF SQL
ch3.pptx SQL in DBMS all Chapter with details
Java database connectivity notes for undergraduate
ch3.ppt
ch3.ppt
Ad

More from OmerMohamed64 (12)

PDF
Electronic commerce courselecture one. Pdf
PPTX
Introduction of internet and technology.pptx
PDF
Electronic commerce Chapter three lecture.pdf
PDF
Introduction to informationsecurity lecture1.pdf
PPT
Informationsecurity management lecture.ppt
PPTX
ch21.pptx distribution database system storage
PPTX
ch23.pptx distrubuted database management
PPTX
ch4 - Copy.pptx intermediate structure query language
PDF
Subnetting-Addressing.pdf lectures for networks
PDF
Classless-Addressing.pdf.. network lecture
PPTX
ch10 - lecture big data - database concept 7th edition.pptx
PPT
chapter tow database system concepts seven edition
Electronic commerce courselecture one. Pdf
Introduction of internet and technology.pptx
Electronic commerce Chapter three lecture.pdf
Introduction to informationsecurity lecture1.pdf
Informationsecurity management lecture.ppt
ch21.pptx distribution database system storage
ch23.pptx distrubuted database management
ch4 - Copy.pptx intermediate structure query language
Subnetting-Addressing.pdf lectures for networks
Classless-Addressing.pdf.. network lecture
ch10 - lecture big data - database concept 7th edition.pptx
chapter tow database system concepts seven edition
Ad

Recently uploaded (20)

PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
A comparative analysis of optical character recognition models for extracting...
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPTX
A Presentation on Artificial Intelligence
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
Machine Learning_overview_presentation.pptx
PPTX
Big Data Technologies - Introduction.pptx
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
Cloud computing and distributed systems.
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Assigned Numbers - 2025 - Bluetooth® Document
Per capita expenditure prediction using model stacking based on satellite ima...
Encapsulation_ Review paper, used for researhc scholars
A comparative analysis of optical character recognition models for extracting...
MIND Revenue Release Quarter 2 2025 Press Release
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
“AI and Expert System Decision Support & Business Intelligence Systems”
A Presentation on Artificial Intelligence
Digital-Transformation-Roadmap-for-Companies.pptx
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Machine Learning_overview_presentation.pptx
Big Data Technologies - Introduction.pptx
Reach Out and Touch Someone: Haptics and Empathic Computing
20250228 LYD VKU AI Blended-Learning.pptx
Dropbox Q2 2025 Financial Results & Investor Presentation
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Unlocking AI with Model Context Protocol (MCP)
Cloud computing and distributed systems.
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
The Rise and Fall of 3GPP – Time for a Sabbatical?
Assigned Numbers - 2025 - Bluetooth® Document

ch5 - Copy.pptx advanced structure query language

  • 1. Database System Concepts, 7th Ed. ©Silberschatz, Korth and Sudarshan See www.db-book.com for conditions on re-use Chapter 5: Advanced SQL
  • 2. ©Silberschatz, Korth and Sudarshan 5.2 Database System Concepts - 7th Edition Outline  Accessing SQL From a Programming Language  Functions and Procedures  Triggers  Recursive Queries  Advanced Aggregation Features
  • 3. ©Silberschatz, Korth and Sudarshan 5.3 Database System Concepts - 7th Edition 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. A database programmer must have access to a general-purpose programming language for at least two reasons
  • 4. ©Silberschatz, Korth and Sudarshan 5.4 Database System Concepts - 7th Edition Accessing SQL from a Programming Language (Cont.)  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. There are two approaches to accessing SQL from a general-purpose programming language
  • 5. ©Silberschatz, Korth and Sudarshan 5.5 Database System Concepts - 7th Edition JDBC
  • 6. ©Silberschatz, Korth and Sudarshan 5.6 Database System Concepts - 7th Edition 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
  • 7. ©Silberschatz, Korth and Sudarshan 5.7 Database System Concepts - 7th Edition 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
  • 8. ©Silberschatz, Korth and Sudarshan 5.8 Database System Concepts - 7th Edition JDBC Code for Older Versions of Java/JDBC public static void JDBCexample(String dbid, String userid, String passwd) { try { Class.forName ("oracle.jdbc.driver.OracleDriver"); Connection conn = DriverManager.getConnection( "jdbc:oracle:thin:@db.yale.edu:2000:univdb", userid, passwd); Statement stmt = conn.createStatement(); … Do Actual Work …. stmt.close(); conn.close(); } catch (SQLException sqle) { System.out.println("SQLException : " + sqle); } } NOTE: Class.forName is not required from JDBC 4 onwards. The try with resources syntax in prev slide is preferred for Java 7 onwards.
  • 9. ©Silberschatz, Korth and Sudarshan 5.9 Database System Concepts - 7th Edition 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)); }
  • 10. ©Silberschatz, Korth and Sudarshan 5.10 Database System Concepts - 7th Edition 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
  • 11. ©Silberschatz, Korth and Sudarshan 5.11 Database System Concepts - 7th Edition JDBC Code Details  Getting result fields:  rs.getString(“dept_name”) and rs.getString(1) equivalent if dept_name is the first argument of select result.  Dealing with Null values int a = rs.getInt(“a”); if (rs.wasNull()) Systems.out.println(“Got null value”);
  • 12. ©Silberschatz, Korth and Sudarshan 5.12 Database System Concepts - 7th Edition Prepared Statement  PreparedStatement pStmt = conn.prepareStatement( "insert into instructor values(?,?,?,?)"); pStmt.setString(1, "88877"); pStmt.setString(2, "Perry"); pStmt.setString(3, "Finance"); pStmt.setInt(4, 125000); pStmt.executeUpdate(); pStmt.setString(1, "88878"); pStmt.executeUpdate();  WARNING: always use prepared statements when taking an input from the user and adding it to a query  NEVER create a query by concatenating strings  "insert into instructor values(' " + ID + " ', ' " + name + " ', " + " ' + dept name + " ', " ' balance + ')“  What if name is “D'Souza”?
  • 13. ©Silberschatz, Korth and Sudarshan 5.13 Database System Concepts - 7th Edition SQL Injection  Suppose query is constructed using  "select * from instructor where name = '" + name + "'"  Suppose the user, instead of entering a name, enters:  X' or 'Y' = 'Y  then the resulting statement becomes:  "select * from instructor where name = '" + "X' or 'Y' = 'Y" + "'"  which is:  select * from instructor where name = 'X' or 'Y' = 'Y'  User could have even used  X'; update instructor set salary = salary + 10000; --  Prepared stament internally uses: "select * from instructor where name = 'X' or 'Y' = 'Y'  Always use prepared statements, with user inputs as parameters
  • 14. ©Silberschatz, Korth and Sudarshan 5.14 Database System Concepts - 7th Edition Metadata Features  ResultSet metadata  E.g.after executing query to get a ResultSet rs:  ResultSetMetaData rsmd = rs.getMetaData(); for(int i = 1; i <= rsmd.getColumnCount(); i++) { System.out.println(rsmd.getColumnName(i)); System.out.println(rsmd.getColumnTypeName(i)); }  How is this useful?
  • 15. ©Silberschatz, Korth and Sudarshan 5.15 Database System Concepts - 7th Edition Metadata (Cont)  Database metadata  DatabaseMetaData dbmd = conn.getMetaData(); // Arguments to getColumns: Catalog, Schema-pattern, Table-pattern, // and Column-Pattern // Returns: One row for each column; row has a number of attributes // such as COLUMN_NAME, TYPE_NAME // The value null indicates all Catalogs/Schemas. // The value “” indicates current catalog/schema // The value “%” has the same meaning as SQL like clause ResultSet rs = dbmd.getColumns(null, "univdb", "department", "%"); while( rs.next()) { System.out.println(rs.getString("COLUMN_NAME"), rs.getString("TYPE_NAME"); }  And where is this useful?
  • 16. ©Silberschatz, Korth and Sudarshan 5.16 Database System Concepts - 7th Edition Metadata (Cont)  Database metadata  DatabaseMetaData dbmd = conn.getMetaData(); // Arguments to getTables: Catalog, Schema-pattern, Table-pattern, // and Table-Type // Returns: One row for each table; row has a number of attributes // such as TABLE_NAME, TABLE_CAT, TABLE_TYPE, .. // The value null indicates all Catalogs/Schemas. // The value “” indicates current catalog/schema // The value “%” has the same meaning as SQL like clause // The last attribute is an array of types of tables to return. // TABLE means only regular tables ResultSet rs = dbmd.getTables (“”, "", “%", new String[] {“TABLES”}); while( rs.next()) { System.out.println(rs.getString(“TABLE_NAME“)); }  And where is this useful?
  • 17. ©Silberschatz, Korth and Sudarshan 5.17 Database System Concepts - 7th Edition Finding Primary Keys  DatabaseMetaData dmd = connection.getMetaData(); // Arguments below are: Catalog, Schema, and Table // The value “” for Catalog/Schema indicates current catalog/schema // The value null indicates all catalogs/schemas ResultSet rs = dmd.getPrimaryKeys(“”, “”, tableName); while(rs.next()){ // KEY_SEQ indicates the position of the attribute in // the primary key, which is required if a primary key has multiple // attributes System.out.println(rs.getString(“KEY_SEQ”), rs.getString("COLUMN_NAME"); }
  • 18. ©Silberschatz, Korth and Sudarshan 5.18 Database System Concepts - 7th Edition Transaction Control in JDBC  By default, each SQL statement is treated as a separate transaction that is committed automatically  bad idea for transactions with multiple updates  Can turn off automatic commit on a connection  conn.setAutoCommit(false);  Transactions must then be committed or rolled back explicitly  conn.commit(); or  conn.rollback();  conn.setAutoCommit(true) turns on automatic commit.
  • 19. ©Silberschatz, Korth and Sudarshan 5.19 Database System Concepts - 7th Edition Other JDBC Features  Calling functions and procedures  CallableStatement cStmt1 = conn.prepareCall("{? = call some function(?)}");  CallableStatement cStmt2 = conn.prepareCall("{call some procedure(?,?)}");  Handling large object types  getBlob() and getClob() that are similar to the getString() method, but return objects of type Blob and Clob, respectively  get data from these objects by getBytes()  associate an open stream with Java Blob or Clob object to update large objects  blob.setBlob(int parameterIndex, InputStream inputStream).
  • 20. ©Silberschatz, Korth and Sudarshan 5.20 Database System Concepts - 7th Edition JDBC Resources  JDBC Basics Tutorial • https://docs.oracle.com/javase/tutorial/jdbc/index.html
  • 21. ©Silberschatz, Korth and Sudarshan 5.21 Database System Concepts - 7th Edition 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();
  • 22. ©Silberschatz, Korth and Sudarshan 5.22 Database System Concepts - 7th Edition ODBC
  • 23. ©Silberschatz, Korth and Sudarshan 5.23 Database System Concepts - 7th Edition 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
  • 24. ©Silberschatz, Korth and Sudarshan 5.24 Database System Concepts - 7th Edition 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 { …. };
  • 25. ©Silberschatz, Korth and Sudarshan 5.25 Database System Concepts - 7th Edition Embedded SQL (Cont.)  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; Here, server identifies the server to which a connection is to be established.  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, as illustrated below. 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;
  • 26. ©Silberschatz, Korth and Sudarshan 5.26 Database System Concepts - 7th Edition 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 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
  • 27. ©Silberschatz, Korth and Sudarshan 5.27 Database System Concepts - 7th Edition Embedded SQL (Cont.)  The open statement for our example is as follows: EXEC SQL open c ; This statement causes the database system to execute the query and to save the results within a temporary relation. 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 Repeated calls to fetch get successive tuples in the query result
  • 28. ©Silberschatz, Korth and Sudarshan 5.28 Database System Concepts - 7th Edition Embedded SQL (Cont.)  A variable called SQLSTATE in the SQL communication area (SQLCA) gets 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.
  • 29. ©Silberschatz, Korth and Sudarshan 5.29 Database System Concepts - 7th Edition 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
  • 30. ©Silberschatz, Korth and Sudarshan 5.30 Database System Concepts - 7th Edition Functions and Procedures
  • 31. ©Silberschatz, Korth and Sudarshan 5.31 Database System Concepts - 7th Edition 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.
  • 32. ©Silberschatz, Korth and Sudarshan 5.32 Database System Concepts - 7th Edition 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
  • 33. ©Silberschatz, Korth and Sudarshan 5.33 Database System Concepts - 7th Edition 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'))
  • 34. ©Silberschatz, Korth and Sudarshan 5.37 Database System Concepts - 7th Edition 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
  • 35. ©Silberschatz, Korth and Sudarshan 5.40 Database System Concepts - 7th Edition 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 SQLcan be executed by these functions.  Declaring external language procedures and functions create procedure dept_count_proc(in dept_name varchar(20), out count integer) language C external name '/usr/avi/bin/dept_count_proc' create function dept_count(dept_name varchar(20)) returns integer language C external name '/usr/avi/bin/dept_count'
  • 36. ©Silberschatz, Korth and Sudarshan 5.42 Database System Concepts - 7th Edition 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.
  • 37. ©Silberschatz, Korth and Sudarshan 5.43 Database System Concepts - 7th Edition Triggers
  • 38. ©Silberschatz, Korth and Sudarshan 5.44 Database System Concepts - 7th Edition 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
  • 39. ©Silberschatz, Korth and Sudarshan 5.46 Database System Concepts - 7th Edition 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;
  • 40. ©Silberschatz, Korth and Sudarshan 5.47 Database System Concepts - 7th Edition 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
  • 41. ©Silberschatz, Korth and Sudarshan 5.48 Database System Concepts - 7th Edition 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
  • 42. ©Silberschatz, Korth and Sudarshan 5.49 Database System Concepts - 7th Edition 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
  • 43. ©Silberschatz, Korth and Sudarshan 5.50 Database System Concepts - 7th Edition Recursive Queries
  • 44. ©Silberschatz, Korth and Sudarshan 5.51 Database System Concepts - 7th Edition 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
  • 45. ©Silberschatz, Korth and Sudarshan 5.52 Database System Concepts - 7th Edition 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
  • 46. ©Silberschatz, Korth and Sudarshan 5.54 Database System Concepts - 7th Edition Example of Fixed-Point Computation
  • 47. ©Silberschatz, Korth and Sudarshan 5.55 Database System Concepts - 7th Edition Advanced Aggregation Features
  • 48. ©Silberschatz, Korth and Sudarshan 5.56 Database System Concepts - 7th Edition Ranking  Ranking is done in conjunction with an order by specification.  Suppose we are given a relation student_grades(ID, GPA) giving the grade-point average of each student  Find the rank of each student.  select ID, rank() over (order by GPA desc) as s_rank from student_grades  An extra order by clause is needed to get them in sorted order select ID, rank() over (order by GPA desc) as s_rank from student_grades order by s_rank  Ranking may leave gaps: e.g. if 2 students have the same top GPA, both have rank 1, and the next rank is 3 • dense_rank does not leave gaps, so next dense rank would be 2
  • 49. ©Silberschatz, Korth and Sudarshan 5.57 Database System Concepts - 7th Edition Ranking  Ranking can be done using basic SQL aggregation, but resultant query is very inefficient select ID, (1 + (select count(*) from student_grades B where B.GPA > A.GPA)) as s_rank from student_grades A order by s_rank;
  • 50. ©Silberschatz, Korth and Sudarshan 5.58 Database System Concepts - 7th Edition Ranking (Cont.)  Ranking can be done within partition of the data.  “Find the rank of students within each department.” select ID, dept_name, rank () over (partition by dept_name order by GPA desc) as dept_rank from dept_grades order by dept_name, dept_rank;  Multiple rank clauses can occur in a single select clause.  Ranking is done after applying group by clause/aggregation  Can be used to find top-n results  More general than the limit n clause supported by many databases, since it allows top-n within each partition
  • 51. ©Silberschatz, Korth and Sudarshan 5.59 Database System Concepts - 7th Edition Ranking (Cont.)  Other ranking functions: • percent_rank (within partition, if partitioning is done) • cume_dist (cumulative distribution)  fraction of tuples with preceding values • row_number (non-deterministic in presence of duplicates)  SQL:1999 permits the user to specify nulls first or nulls last select ID, rank ( ) over (order by GPA desc nulls last) as s_rank from student_grades
  • 52. ©Silberschatz, Korth and Sudarshan 5.60 Database System Concepts - 7th Edition Ranking (Cont.)  For a given constant n, the ranking the function ntile(n) takes the tuples in each partition in the specified order, and divides them into n buckets with equal numbers of tuples.  E.g., select ID, ntile(4) over (order by GPA desc) as quartile from student_grades;
  • 53. ©Silberschatz, Korth and Sudarshan 5.61 Database System Concepts - 7th Edition Windowing  Used to smooth out random variations.  E.g., moving average: “Given sales values for each date, calculate for each date the average of the sales on that day, the previous day, and the next day”  Window specification in SQL: • Given relation sales(date, value) select date, sum(value) over (order by date between rows 1 preceding and 1 following) from sales
  • 54. ©Silberschatz, Korth and Sudarshan 5.62 Database System Concepts - 7th Edition Windowing  Examples of other window specifications: • between rows unbounded preceding and current • rows unbounded preceding • range between 10 preceding and current row  All rows with values between current row value –10 to current value • range interval 10 day preceding  Not including current row
  • 55. ©Silberschatz, Korth and Sudarshan 5.63 Database System Concepts - 7th Edition Windowing (Cont.)  Can do windowing within partitions  E.g., Given a relation transaction (account_number, date_time, value), where value is positive for a deposit and negative for a withdrawal • “Find total balance of each account after each transaction on the account” select account_number, date_time, sum (value) over (partition by account_number order by date_time rows unbounded preceding) as balance from transaction order by account_number, date_time
  • 56. ©Silberschatz, Korth and Sudarshan 5.64 Database System Concepts - 7th Edition OLAP
  • 57. ©Silberschatz, Korth and Sudarshan 5.65 Database System Concepts - 7th Edition Data Analysis and OLAP  Online Analytical Processing (OLAP) • Interactive analysis of data, allowing data to be summarized and viewed in different ways in an online fashion (with negligible delay)  Data that can be modeled as dimension attributes and measure attributes are called multidimensional data. • Measure attributes  measure some value  can be aggregated upon  e.g., the attribute number of the sales relation • Dimension attributes  define the dimensions on which measure attributes (or aggregates thereof) are viewed  e.g., attributes item_name, color, and size of the sales relation
  • 58. ©Silberschatz, Korth and Sudarshan 5.66 Database System Concepts - 7th Edition Example sales relation ... ... ... ... ... ... ... ...
  • 59. ©Silberschatz, Korth and Sudarshan 5.67 Database System Concepts - 7th Edition Cross Tabulation of sales by item_name and color  The table above is an example of a cross-tabulation (cross-tab), also referred to as a pivot-table. • Values for one of the dimension attributes form the row headers • Values for another dimension attribute form the column headers • Other dimension attributes are listed on top • Values in individual cells are (aggregates of) the values of the dimension attributes that specify the cell.
  • 60. ©Silberschatz, Korth and Sudarshan 5.68 Database System Concepts - 7th Edition Data Cube  A data cube is a multidimensional generalization of a cross-tab  Can have n dimensions; we show 3 below  Cross-tabs can be used as views on a data cube
  • 61. ©Silberschatz, Korth and Sudarshan 5.70 Database System Concepts - 7th Edition Cross Tabulation With Hierarchy  Cross-tabs can be easily extended to deal with hierarchies • Can drill down or roll up on a hierarchy
  • 62. ©Silberschatz, Korth and Sudarshan 5.71 Database System Concepts - 7th Edition Relational Representation of Cross-tabs  Cross-tabs can be represented as relations • We use the value all is used to represent aggregates. • The SQL standard actually uses null values in place of all despite confusion with regular null values.
  • 63. ©Silberschatz, Korth and Sudarshan 5.72 Database System Concepts - 7th Edition Extended Aggregation to Support OLAP  The cube operation computes union of group by’s on every subset of the specified attributes  Example relation for this section sales(item_name, color, clothes_size, quantity)  E.g., consider the query select item_name, color, size, sum(number) from sales group by cube(item_name, color, size) This computes the union of eight different groupings of the sales relation: { (item_name, color, size), (item_name, color), (item_name, size), (color, size), (item_name), (color), (size), ( ) } where ( ) denotes an empty group by list.  For each grouping, the result contains the null value for attributes not present in the grouping.
  • 64. ©Silberschatz, Korth and Sudarshan 5.73 Database System Concepts - 7th Edition Online Analytical Processing Operations  Relational representation of cross-tab that we saw earlier, but with null in place of all, can be computed by  select item_name, color, sum(number) from sales group by cube(item_name, color)  The function grouping() can be applied on an attribute • Returns 1 if the value is a null value representing all, and returns 0 in all other cases. select item_name, color, size, sum(number), grouping(item_name) as item_name_flag, grouping(color) as color_flag, grouping(size) as size_flag, from sales group by cube(item_name, color, size)
  • 65. ©Silberschatz, Korth and Sudarshan 5.74 Database System Concepts - 7th Edition Online Analytical Processing Operations  Can use the function decode() in the select clause to replace such nulls by a value such as all • E.g., replace item_name in first query by decode( grouping(item_name), 1, ‘all’, item_name)
  • 66. ©Silberschatz, Korth and Sudarshan 5.75 Database System Concepts - 7th Edition Extended Aggregation (Cont.)  The rollup construct generates union on every prefix of specified list of attributes  E.g., select item_name, color, size, sum(number) from sales group by rollup(item_name, color, size) • Generates union of four groupings: { (item_name, color, size), (item_name, color), (item_name), ( ) }  Rollup can be used to generate aggregates at multiple levels of a hierarchy.  E.g., suppose table itemcategory(item_name, category) gives the category of each item. Then select category, item_name, sum(number) from sales, itemcategory where sales.item_name = itemcategory.item_name group by rollup(category, item_name) would give a hierarchical summary by item_name and by category.
  • 67. ©Silberschatz, Korth and Sudarshan 5.76 Database System Concepts - 7th Edition Extended Aggregation (Cont.)  Multiple rollups and cubes can be used in a single group by clause • Each generates set of group by lists, cross product of sets gives overall set of group by lists  E.g., select item_name, color, size, sum(number) from sales group by rollup(item_name), rollup(color, size) generates the groupings {item_name, ()} X {(color, size), (color), ()} = { (item_name, color, size), (item_name, color), (item_name), (color, size), (color), ( ) }
  • 68. ©Silberschatz, Korth and Sudarshan 5.77 Database System Concepts - 7th Edition Online Analytical Processing Operations  Pivoting: changing the dimensions used in a cross-tab is called  Slicing: creating a cross-tab for fixed values only • Sometimes called dicing, particularly when values for multiple dimensions are fixed.  Rollup: moving from finer-granularity data to a coarser granularity  Drill down: The opposite operation - that of moving from coarser- granularity data to finer-granularity data
  • 69. ©Silberschatz, Korth and Sudarshan 5.78 Database System Concepts - 7th Edition OLAP Implementation  The earliest OLAP systems used multidimensional arrays in memory to store data cubes, and are referred to as multidimensional OLAP (MOLAP) systems.  OLAP implementations using only relational database features are called relational OLAP (ROLAP) systems  Hybrid systems, which store some summaries in memory and store the base data and other summaries in a relational database, are called hybrid OLAP (HOLAP) systems.
  • 70. ©Silberschatz, Korth and Sudarshan 5.79 Database System Concepts - 7th Edition OLAP Implementation (Cont.)  Early OLAP systems precomputed all possible aggregates in order to provide online response  Space and time requirements for doing so can be very high  2n combinations of group by  It suffices to precompute some aggregates, and compute others on demand from one of the precomputed aggregates  Can compute aggregate on (item_name, color) from an aggregate on (item_name, color, size) – For all but a few “non-decomposable” aggregates such as median – is cheaper than computing it from scratch  Several optimizations available for computing multiple aggregates  Can compute aggregate on (item_name, color) from an aggregate on (item_name, color, size)  Can compute aggregates on (item_name, color, size), (item_name, color) and (item_name) using a single sorting of the base data
  • 71. ©Silberschatz, Korth and Sudarshan 5.80 Database System Concepts - 7th Edition End of Chapter 5