Chapter 5
Chapter 5
Database System Concepts - 7th Edition 5.2 ©Silberschatz, Korth and Sudarshan
Accessing SQL from a Programming Language
Database System Concepts - 7th Edition 5.3 ©Silberschatz, Korth and Sudarshan
MySQL with Python
Many open source libraries allow Python to interact with a MySQL database.
One of the most popular of these is PyMySQL, which can be installed using
pip install PyMySQL
The connection/cursor model is
used here.
Database System Concepts - 7th Edition 5.4 ©Silberschatz, Korth and Sudarshan
SQLite with Python
SQLite is a database
engine written in the C
programming language. It is
not a standalone app;
rather, it is a
library that software
developers embed in their
apps. As such, it belongs to
the family of embedded
databases.
Database System Concepts - 7th Edition 5.5 ©Silberschatz, Korth and Sudarshan
Functions and Procedures
Database System Concepts - 7th Edition 5.6 ©Silberschatz, Korth and Sudarshan
Functions and Procedures
Database System Concepts - 7th Edition 5.6 ©Silberschatz, Korth and Sudarshan
Functions and Procedures
Database System Concepts - 7th Edition 5.7 ©Silberschatz, Korth and Sudarshan
Declaring SQL Functions
Define a function that, given the name of a department, returns the count of
the number of instructors in that department.
create function dept_count (dept_name varchar(20))
returns integer
begin
declare d_count integer;
select count (* ) into d_count
from instructor
where instructor.dept_name = dept_name
return d_count;
end
The function dept_count can be used to find the department names and
budget of all departments with more that 12 instructors.
select dept_name, budget
from department
where dept_count (dept_name ) > 12
Database System Concepts - 7th Edition 5.8 ©Silberschatz, Korth and Sudarshan
Table Functions
The SQL standard supports functions that can return tables as results; such
functions are called table functions
Example: Return all instructors in a given department
create function instructor_of (dept_name varchar(20))
returns table (
ID varchar(5),
name varchar(20),
dept_name varchar(20),
salary numeric(8,2))
return table
(select ID, name, dept_name, salary
from instructor
where instructor.dept_name = instructor_of.dept_name)
Usage
select *
from table (instructor_of ('Music'))
Database System Concepts - 7th Edition 5.9 ©Silberschatz, Korth and Sudarshan
MySQL Procedures
it is unnecessary to
use delimiter because the
function definition contains no
internal ; statement delimiters:
Database System Concepts - 7th Edition 5.10 ©Silberschatz, Korth and Sudarshan
Delimiter in MySQL
https://dev.mysql.com/doc/refman/9.0/en/stored-programs-
defining.html
https://dev.mysql.com/doc/refman/9.0/en/create-procedure.html
Database System Concepts - 7th Edition 5.11 ©Silberschatz, Korth and Sudarshan
SQL Procedures
Database System Concepts - 7th Edition 5.12 ©Silberschatz, Korth and Sudarshan
MySQL Procedures
Database System Concepts - 7th Edition 5.13 ©Silberschatz, Korth and Sudarshan
Language Constructs for Procedures & Functions
SQL supports constructs that gives it almost all the power of a general-
purpose programming language.
• Warning: most database systems implement their own variant of the
standard syntax below.
Compound statement: begin … end,
• May contain multiple SQL statements between begin and end.
• Local variables can be declared within a compound statements
While and repeat statements:
• while boolean expression do
sequence of statements ;
end while
• repeat
sequence of statements ;
until boolean expression
end repeat
Database System Concepts - 7th Edition 5.14 ©Silberschatz, Korth and Sudarshan
Language Constructs (Cont.)
For loop
• Permits iteration over all results of a query
Example: Find the budget of all departments
Database System Concepts - 7th Edition 5.15 ©Silberschatz, Korth and Sudarshan
Procedures: MySQL Example
While statements:
1 1 2 3 5 8 13
, 2
........
, , ,
,
,
,
n = 5
i= 2 answer = 2
.
i=3 answer =
3 .
i: 4 answer :'
Database System Concepts - 7th Edition 5.16 ©Silberschatz, Korth and Sudarshan
Procedures: MySQL Example
Repeat statements:
Database System Concepts - 7th Edition 5.17 ©Silberschatz, Korth and Sudarshan
Procedures: MySQL Example
loop statements:
Database System Concepts - 7th Edition 5.18 ©Silberschatz, Korth and Sudarshan
Language Constructs – if-then-else
Database System Concepts - 7th Edition 5.19 ©Silberschatz, Korth and Sudarshan
Example procedure
Database System Concepts - 7th Edition 5.20 ©Silberschatz, Korth and Sudarshan
Procedures: MySQL Example (Page 203)
Database System Concepts - 7th Edition 5.21 ©Silberschatz, Korth and Sudarshan
Procedures: MySQL Example (Page 203)
Database System Concepts - 7th Edition 5.22 ©Silberschatz, Korth and Sudarshan
Procedures: MySQL Example (from its manual)
Database System Concepts - 7th Edition 5.23 ©Silberschatz, Korth and Sudarshan
Triggers
Database System Concepts - 7th Edition 5.24 ©Silberschatz, Korth and Sudarshan
Triggers
Database System Concepts - 7th Edition 5.25 ©Silberschatz, Korth and Sudarshan
Triggering Events and Actions in SQL
Database System Concepts - 7th Edition 5.26 ©Silberschatz, Korth and Sudarshan
Trigger in MySQL
delimiter //
create trigger trigger_name
trigger_time triger_event on table_name for each row
beign
…
end; //
delimiter ;
Database System Concepts - 7th Edition 5.27 ©Silberschatz, Korth and Sudarshan
Trigger in MySQL before insert
Database System Concepts - 7th Edition 5.28 ©Silberschatz, Korth and Sudarshan
Trigger to Maintain credits_earned value
Database System Concepts - 7th Edition 5.29 ©Silberschatz, Korth and Sudarshan
Trigger to Maintain credits_earned value
Database System Concepts - 7th Edition 5.30 ©Silberschatz, Korth and Sudarshan
Recursive Queries
Database System Concepts - 7th Edition 5.31 ©Silberschatz, Korth and Sudarshan
Recursion in SQL
Database System Concepts - 7th Edition 5.32 ©Silberschatz, Korth and Sudarshan
Recursion Example in MySQL
CTE
course-id
preregial
C3319
CS319
13315
Cs 190
/
Cs 101
29319
Database System Concepts - 7th Edition 5.33 ©Silberschatz, Korth and Sudarshan
Find all the direct and indirect supervisors of Bob
Database System Concepts - 7th Edition 5.34 ©Silberschatz, Korth and Sudarshan
Advanced Aggregation Features
Database System Concepts - 7th Edition 5.35 ©Silberschatz, Korth and Sudarshan
Ranking
Database System Concepts - 7th Edition 5.36 ©Silberschatz, Korth and Sudarshan
Modify the corresponding part in DDL.sql and smallRelationsInsertFile.sql
Database System Concepts - 7th Edition 5.37 ©Silberschatz, Korth and Sudarshan
Ranking: MySQL Example
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;
Database System Concepts - 7th Edition 5.39 ©Silberschatz, Korth and Sudarshan
Ranking (Cont.)
Database System Concepts - 7th Edition 5.40 ©Silberschatz, Korth and Sudarshan
Ranking (Cont.)
Database System Concepts - 7th Edition 5.41 ©Silberschatz, Korth and Sudarshan
Ranking (Cont.)
• https://dev.mysql.com/doc/refman/9.0/en/window-function-descriptions.html
SQL:1999 permits the user to specify nulls first or nulls last. (Not supported
by MySQL, but supported by PostgreSQL, SQLite)
select ID,
rank ( ) over (order by GPA desc nulls last) as s_rank
from student_grades
Database System Concepts - 7th Edition 5.42 ©Silberschatz, Korth and Sudarshan
Ranking (Cont.)
For a given constant n, the ranking 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;
Database System Concepts - 7th Edition 5.43 ©Silberschatz, Korth and Sudarshan
Windowing
Database System Concepts - 7th Edition 5.44 ©Silberschatz, Korth and Sudarshan
Windowing
Database System Concepts - 7th Edition 5.45 ©Silberschatz, Korth and Sudarshan
Windowing: MySQL
16
23
28
the window
do in
window
-
Database System Concepts - 7th Edition 5.46 ©Silberschatz, Korth and Sudarshan
Windowing (Cont.)
Database System Concepts - 7th Edition 5.47 ©Silberschatz, Korth and Sudarshan
Windowing (Cont.)
Database System Concepts - 7th Edition 5.48 ©Silberschatz, Korth and Sudarshan
Aggregation on Multidimensional Data
Database System Concepts - 7th Edition 5.49 ©Silberschatz, Korth and Sudarshan
Example sales relation
Database System Concepts - 7th Edition 5.52 ©Silberschatz, Korth and Sudarshan
Pivot table by MySQL
Database System Concepts - 7th Edition 5.53 ©Silberschatz, Korth and Sudarshan
Data Cube
Database System Concepts - 7th Edition 5.54 ©Silberschatz, Korth and Sudarshan
Hierarchies on Dimensions
Hierarchy on dimension attributes: lets dimensions to be viewed at
different levels of detail
•E.g., the dimension DateTime can be used to aggregate by hour of
day, date, day of week, month, quarter or year
Database System Concepts - 7th Edition 5.55 ©Silberschatz, Korth and Sudarshan
Cross Tabulation With Hierarchy
Database System Concepts - 7th Edition 5.56 ©Silberschatz, Korth and Sudarshan
Relational Representation of Cross-tabs
Database System Concepts - 7th Edition 5.57 ©Silberschatz, Korth and Sudarshan
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, size, quantity)
E.g., consider the query
select item_name, color, size, sum(quantity)
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.
Database System Concepts - 7th Edition 5.58 ©Silberschatz, Korth and Sudarshan
Extended Aggregation to Support OLAP
(Cont.)
Database System Concepts - 7th Edition 5.59 ©Silberschatz, Korth and Sudarshan
rollup in MySQL
Database System Concepts - 7th Edition 5.60 ©Silberschatz, Korth and Sudarshan
cube and rollup in PostgreSQL
Database System Concepts - 7th Edition 5.61 ©Silberschatz, Korth and Sudarshan
Online Analytical Processing Operations
Database System Concepts - 7th Edition 5.62 ©Silberschatz, Korth and Sudarshan
PostgreSQL:
Database System Concepts - 7th Edition 5.63 ©Silberschatz, Korth and Sudarshan
Database System Concepts - 7th Edition 5.64 ©Silberschatz, Korth and Sudarshan
Online Analytical Processing Operations
Let me summarize the concepts of OLAP and OLTP which you may have heard
(https://questions.finalroundai.com/1967/what-is-oltp-vs-olap).
OLTP and OLAP are two types of data processing that serve different purposes.
OLTP, or Online Transaction Processing, is a class of systems that manage
transaction-oriented applications on the internet. OLTP systems are primarily
used for recording day-to-day transactions such as updating, inserting, and
deleting data. They are characterized by a large number of short online
transactions and are designed to ensure data integrity in multi-access
environments. Examples are ATM transactions, order entry, retail sales, etc.
On the other hand, OLAP, or Online Analytical Processing, is a category of
software tools that enables analysts, managers, or executives to gain insight into
data through fast, interactive access to a variety of possible views of information.
Unlike OLTP where transactions are short and fast, OLAP is characterized by
complex queries with multi-dimensional view of data for analysis. It is typically
used in data mining or retrieving multi-dimensional information. OLAP helps top-
level executives to ascertain business-driving factors and trends.
Database System Concepts - 7th Edition 5.65 ©Silberschatz, Korth and Sudarshan
End of Chapter 5
Database System Concepts - 7th Edition 5.66 ©Silberschatz, Korth and Sudarshan
Homework:
Database System Concepts - 7th Edition 5.67 ©Silberschatz, Korth and Sudarshan