Extensions to SQL
©Silberschatz, Korth and Sudarshan
5.1
Database System Concepts - 6th Edition
Functions and Procedures
©Silberschatz, Korth and Sudarshan
5.2
Database System Concepts - 6th Edition
SQL Functions
delimiter //
create function disease_count (disease_name varchar(45)) returns int deterministic
begin
declare d_count int default 0;
select count(1) into d_count
from Diagnosis
where disease = disease_name;
return d_count;
end//
delimiter ;
select pname, diag_date� from Diagnosis as D� where disease_count (D.disease ) > 5;
©Silberschatz, Korth and Sudarshan
5.3
Database System Concepts - 6th Edition
SQL functions (Cont.)
©Silberschatz, Korth and Sudarshan
5.4
Database System Concepts - 6th Edition
Table Functions
returns table (
pname varchar(45),
disease varchar(45),
diagnosis_date date
)
return table� (select pname, disease, diagnosis� from Diagnosis� where Diagnosis.disease= diagnosed_with.disease)
select *� from table (diagnosed_with(‘Disease’))
©Silberschatz, Korth and Sudarshan
5.5
Database System Concepts - 6th Edition
SQL Procedures
delimiter //
create procedure disease_count_proc (in disease_name varchar(45), out d_count int)
begin
select count(1) into d_count
from Diagnosis
where Diagnosis.disease = disease_name;
end//
delimiter ;
set @d_count := 0 ;
call disease_count_proc('COVID', @d_count);
©Silberschatz, Korth and Sudarshan
5.6
Database System Concepts - 6th Edition
Language Constructs for Procedures & Functions
sequence of statements ;
end while
sequence of statements ;
until boolean expression
end repeat
©Silberschatz, Korth and Sudarshan
5.7
Database System Concepts - 6th Edition
Language Constructs (Cont.)
©Silberschatz, Korth and Sudarshan
5.8
Database System Concepts - 6th Edition
Language Constructs – if-then-else
if boolean expression � then statement or compound statement � elseif boolean expression � then statement or compound statement � else statement or compound statement � end if
©Silberschatz, Korth and Sudarshan
5.9
Database System Concepts - 6th Edition
External Language Routines
create procedure disease_count_proc(in disease_name varchar(20),� out count integer)�language C�external name ’ /usr/avi/bin/disease_count_proc’��create function disease_count(disease_name varchar(20))�returns integer�language C�external name ‘/usr/avi/bin/disease_count’
©Silberschatz, Korth and Sudarshan
5.10
Database System Concepts - 6th Edition
External Language Routines (Cont.)
©Silberschatz, Korth and Sudarshan
5.11
Database System Concepts - 6th Edition
Security with External Language Routines
©Silberschatz, Korth and Sudarshan
5.12
Database System Concepts - 6th Edition
Recursive Queries
©Silberschatz, Korth and Sudarshan
5.13
Database System Concepts - 6th Edition
Recursion in SQL
This example view, rec_prereq, is called the transitive closure of the prereq relation
©Silberschatz, Korth and Sudarshan
5.14
Database System Concepts - 6th Edition
Example – fixed point computation
rec_prereq table is initially empty. So rec_prereq table becomes the same as
course_id | prereq_id |
102 | 101 |
102 | 100 |
103 | 102 |
Prereq table
Iteration 1: req_prereq table
course_id | prereq_id |
102 | 101 |
102 | 100 |
103 | 102 |
Iteration 2: req_prereq table
course_id | prereq_id |
102 | 101 |
102 | 100 |
103 | 102 |
103 | 101 |
103 | 100 |
Two new tuples added due to join.
Iteration 3: req_prereq table
course_id | prereq_id |
102 | 101 |
102 | 100 |
103 | 102 |
103 | 101 |
103 | 100 |
No new tuples added. Reached fixed point
©Silberschatz, Korth and Sudarshan
5.15
Database System Concepts - 6th Edition
The Power of Recursion
©Silberschatz, Korth and Sudarshan
5.16
Database System Concepts - 6th Edition
The Power of Recursion
©Silberschatz, Korth and Sudarshan
5.17
Database System Concepts - 6th Edition
Advanced Aggregation Features
©Silberschatz, Korth and Sudarshan
5.18
Database System Concepts - 6th Edition
Ranking
select name, (1 + (select count(*)
from Patients as P2
where P2.name < P1.name)) as p_rank
from Patients as P1
order by p_rank;
©Silberschatz, Korth and Sudarshan
5.19
Database System Concepts - 6th Edition
Ranking Operator in SQL
select name, rank() over (order by name) as p_rank
from Patients
order by p_rank
©Silberschatz, Korth and Sudarshan
5.20
Database System Concepts - 6th Edition
Ranking (Cont.)
select name, rank() over (partition by age order by name ) as p_rank
from Patients
order by p_rank
©Silberschatz, Korth and Sudarshan
5.21
Database System Concepts - 6th Edition
name | age |
Alice | 30 |
Bob | 40 |
Carol | 30 |
David | 40 |
Evan | 30 |
Frank | 50 |
select name, rank() over (partition by age order by name ) as p_rank
from Patients
order by p_rank
©Silberschatz, Korth and Sudarshan
5.22
Database System Concepts - 6th Edition
Windowing
select date, sum(value) over � (order by date between rows 1 preceding and 1 following)� from sales
©Silberschatz, Korth and Sudarshan
5.23
Database System Concepts - 6th Edition
Windowing
©Silberschatz, Korth and Sudarshan
5.24
Database System Concepts - 6th Edition
Windowing (Cont.)
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.25
Database System Concepts - 6th Edition
OLAP
©Silberschatz, Korth and Sudarshan
5.26
Database System Concepts - 6th Edition
Advanced Aggregations for Data Analysis
©Silberschatz, Korth and Sudarshan
5.27
Database System Concepts - 6th Edition
Multidimensional Data & Schema
©Silberschatz, Korth and Sudarshan
5.28
Database System Concepts - 6th Edition
Multidimensional Data and�Schemas
©Silberschatz, Korth and Sudarshan
5.29
Database System Concepts - 6th Edition
Data Warehouse Schema
©Silberschatz, Korth and Sudarshan
5.30
Database System Concepts - 6th Edition
Multidimensional Data and�Warehouse Schemas
©Silberschatz, Korth and Sudarshan
5.31
Database System Concepts - 6th Edition
Database Support for Data Warehouses
©Silberschatz, Korth and Sudarshan
5.32
Database System Concepts - 6th Edition
OLAP Support In SQL
©Silberschatz, Korth and Sudarshan
5.33
Database System Concepts - 6th Edition
Example sales relation
...
...
...
...
...
...
...
...
This is a simplified version of the sales fact table joined with the dimension tables, and many attributes removed (and some renamed)�
©Silberschatz, Korth and Sudarshan
5.34
Database System Concepts - 6th Edition
Cross Tabulation of sales by item_name and color
©Silberschatz, Korth and Sudarshan
5.35
Database System Concepts - 6th Edition
Relational Representation of Cross-tabs
©Silberschatz, Korth and Sudarshan
5.36
Database System Concepts - 6th Edition
Data Cube
©Silberschatz, Korth and Sudarshan
5.37
Database System Concepts - 6th Edition
Cube Operator
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.
CUBE operator is not directly
Supported by Mysql
©Silberschatz, Korth and Sudarshan
5.38
Database System Concepts - 6th Edition
Roll Up Operator
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), ( ) }
.
©Silberschatz, Korth and Sudarshan
5.39
Database System Concepts - 6th Edition
Example in Mysql
'G','1','Al'
'G','1','Alex'
'G','0','Barbara'
'F','0','Jane'
'E','0','Mehdi'
'E','1','Mehdi'
'D','1','Mary'
'C','1','Bob'
'C','0','Jane'
'B','1','Jane'
'B','0','Mary'
'A','1','Alex'
'A','1','Mary'
'A','1','Mary'
select pname, test, result from Outcomes;
select test, result, count(*) from Outcomes
group by test, result
order by test desc;
'G','0','1'
'G','1','2'
'F','0','1'
'E','0','1'
'E','1','1'
'D','1','1'
'C','0','1'
'C','1','1'
'B','0','1'
'B','1','1'
'A','1','3'
select test, result, count(*) from Outcomes
group by test, result with Rollup
order by test desc;
'G','0','1'
'G','1','2'
'G',NULL,'3'
'F','0','1'
'F',NULL,'1'
'E','0','1'
'E','1','1'
'E',NULL,'2'
'D','1','1'
'D',NULL,'1'
'C','0','1'
'C','1','1'
'C',NULL,'2'
'B','0','1'
'B','1','1'
'B',NULL,'2'
'A','1','3'
'A',NULL,'3'
NULL,NULL,'14'
select test, result, count(*) from Outcomes
group by result, test with Rollup
order by result desc;
'A','1','3'
'B','1','1'
'C','1','1'
'D','1','1'
'E','1','1'
'G','1','2'
NULL,'1','9'
'B','0','1'
'C','0','1'
'E','0','1'
'F','0','1'
'G','0','1'
NULL,'0','5'
NULL,NULL,'14'
©Silberschatz, Korth and Sudarshan
5.40
Database System Concepts - 6th Edition
Hierarchies on Dimensions
©Silberschatz, Korth and Sudarshan
5.41
Database System Concepts - 6th Edition
Cross Tabulation With Hierarchy
©Silberschatz, Korth and Sudarshan
5.42
Database System Concepts - 6th Edition
Additional Operators
©Silberschatz, Korth and Sudarshan
5.43
Database System Concepts - 6th Edition
Extended Aggregation (Cont.)
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.44
Database System Concepts - 6th Edition
OLAP Implementation (Cont.)
©Silberschatz, Korth and Sudarshan
5.45
Database System Concepts - 6th Edition