程序代写代做 JDBC c++ database C Java Chapter 5: Advanced SQL

Chapter 5: Advanced SQL
■ Accessing SQL From a Programming Language ■ Functions and Procedural Constructs
■ Triggers
■ Recursive Queries
■ Advanced Aggregation Features ■ OLAP
Database System Concepts – 6th Edition
Modified by T. Suel for CS6083, NYU School of Engineering, Spring 2020 5.1 ©Silberschatz, Korth and Sudarshan

Accessing SQL From a Programming Language

Database System Concepts – 6th Edition
Modified by T. Suel for CS6083, NYU School of Engineering, Spring 2020 5.2 ©Silberschatz, Korth and Sudarshan

Accessing SQL From a Programming Language 

■ API(application-programinterface)foraprogramtointeractwitha database server
■ Applicationmakescallsto
● Connect with the database server
● Send SQL commands to the database server
● Fetch tuples of result one-by-one into program variables
■ Various tools:
● JDBC (Java Database Connectivity) works with Java
● ODBC (Open Database Connectivity) works with C, C++, C#, and Visual Basic. Other API’s such as ADO.NET sit on top of ODBC
● Embedded SQL
Database System Concepts – 6th Edition
Modified by T. Suel for CS6083, NYU School of Engineering, Spring 2020 5.3 ©Silberschatz, Korth and Sudarshan

JDBC
■ JDBC is a Java API for communicating with database systems supporting SQL.
■ JDBC supports a variety of features for querying and updating data, and for retrieving query results.
■ JDBC also supports metadata retrieval, such as querying about relations present in the database and the names and types of relation attributes.
■ Model for communicating with the database:
● Open a connection
● Create a “statement” object
● Execute queries using the Statement object to send queries and fetch results
● Exception mechanism to handle errors
Database System Concepts – 6th Edition
Modified by T. Suel for CS6083, NYU School of Engineering, Spring 2020 5.4 ©Silberschatz, Korth and Sudarshan

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, and
! check for errors
■ Applications such as GUI, spreadsheets, etc. can use ODBC
Database System Concepts – 6th Edition
Modified by T. Suel for CS6083, NYU School of Engineering, Spring 2020 5.5 ©Silberschatz, Korth and Sudarshan

Embedded SQL
■ The SQL standard defines embeddings of SQL in a variety of programming languages such as C, Java, and Cobol.
■ AlanguagetowhichSQLqueriesareembeddedisreferredtoasa 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/I.
■ EXEC SQL statement is used to identify embedded SQL request to the preprocessor
EXEC SQL END_EXEC
Note: this varies by language (for example, the Java embedding uses
 # SQL { …. }; )
Database System Concepts – 6th Edition
Modified by T. Suel for CS6083, NYU School of Engineering, Spring 2020 5.6
©Silberschatz, Korth and Sudarshan

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;
Database System Concepts – 6th Edition
Modified by T. Suel for CS6083, NYU School of Engineering, Spring 2020 5.7 ©Silberschatz, Korth and Sudarshan

Embedded SQL (Cont.)
■ To write an embedded SQL query, we use the declare c cursor for
statement. The variable c is used to identify the query ■ Example:
● Fromwithinahostlanguage,findtheIDandnameof students who have completed more than the number of credits stored in variable credit_amount in the host langue
● SpecifythequeryinSQLasfollows: EXEC SQL
declare c cursor for 

select ID, name

from student

where tot_cred > :credit_amount
END_EXEC
Database System Concepts – 6th Edition
Modified by T. Suel for CS6083, NYU School of Engineering, Spring 2020 5.8 ©Silberschatz, Korth and Sudarshan

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
Database System Concepts – 6th Edition
Modified by T. Suel for CS6083, NYU School of Engineering, Spring 2020 5.9 ©Silberschatz, Korth and Sudarshan

■ ■
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.
Database System Concepts – 6th Edition
Modified by T. Suel for CS6083, NYU School of Engineering, Spring 2020 5.10 ©Silberschatz, Korth and Sudarshan

Extensions to SQL
Database System Concepts – 6th Edition
Modified by T. Suel for CS6083, NYU School of Engineering, Spring 2020 5.11 ©Silberschatz, Korth and Sudarshan

Procedural Extensions and Stored Procedures
■ SQL provides a module language
● Permits definition of procedures in SQL, with if-then-else
statements, for and while loops, etc.
■ Stored Procedures
● Can store procedures in the database
● then execute them using the call statement
● permit external applications to operate on the database without knowing about internal details
■ Example: putObjectInShoppingCart() in e-commerce app
■ Object-oriented aspects of these features are covered in Chapter 22
(6th Edition, Object Based Databases)
Database System Concepts – 6th Edition
Modified by T. Suel for CS6083, NYU School of Engineering, Spring 2020 5.12 ©Silberschatz, Korth and Sudarshan

Functions and Procedures
■ SQL:1999 supports functions and procedures
● Functions/procedures can be written in SQL itself, or in an external
programming language.
● Functions are particularly useful with specialized data types such as images, geometric objects, or text.
! Example: functions to check if polygons overlap, compute distance between points, or compare images for similarity, or if an image contains a face, or what language a text is.
● Some database systems support table-valued functions, which can return a relation as a result.
■ SQL:1999 also supports a rich set of imperative constructs, including ● Loops, if-then-else, assignment
■ Many databases have proprietary procedural extensions to SQL that differ from SQL:1999.
Database System Concepts – 6th Edition
Modified by T. Suel for CS6083, NYU School of Engineering, Spring 2020 5.13 ©Silberschatz, Korth and Sudarshan

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
■ Find the department name and budget of all departments with more that 12 instructors.
select dept_name, budget

from department

where dept_count (dept_name ) > 1
Database System Concepts – 6th Edition
Modified by T. Suel for CS6083, NYU School of Engineering, Spring 2020 5.14 ©Silberschatz, Korth and Sudarshan

SQL functions (Cont.)
■ Compound statement: begin … end
● May contain multiple SQL statements between begin and
end.
■ returns — indicates the variable-type that is returned (e.g.,
integer)
■ return — specifies the values that are to be returned as result of invoking the function
■ SQL function are in fact parameterized views that generalize the regular notion of views by allowing parameters.
Database System Concepts – 6th Edition
Modified by T. Suel for CS6083, NYU School of Engineering, Spring 2020 5.15 ©Silberschatz, Korth and Sudarshan

Table Functions
■ SQL:2003 added functions that return a relation as a result ■ Example: Return all accounts owned by a given customer
create function instructors_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 = instructors_of.dept_name)
■ Usage select *

from table (instructors_of (‘Music’))
Database System Concepts – 6th Edition
Modified by T. Suel for CS6083, NYU School of Engineering, Spring 2020 5.16 ©Silberschatz, Korth and Sudarshan

SQL Procedures
■ The dept_count function could instead be written as procedure: create procedure dept_count_proc (in dept_name varchar(20), 

out d_count integer)

begin
select count(*) into d_count

from instructor

where instructor.dept_name = dept_count_proc.dept_name
end
■ Procedures can be invoked either from an SQL procedure or from embedded SQL, using the call statement.
declare d_count integer;

call dept_count_proc( ‘Physics’, d_count);
■ SQL:1999 allows more than one function/procedure of the same name (called name overloading), as long as the number of 
 arguments differ, or at least the types of the arguments differ
Database System Concepts – 6th Edition
Modified by T. Suel for CS6083, NYU School of Engineering, Spring 2020 5.17 ©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 – 6th Edition
Modified by T. Suel for CS6083, NYU School of Engineering, Spring 2020 5.18 ©Silberschatz, Korth and Sudarshan

Procedural Constructs (Cont.)
■ For loop
● Permits iteration over all results of a query
● Example:
declare n integer default 0;
 for r as

select budget from department
 where dept_name = ‘Music’

do

set n = n – r.budget

end for
Database System Concepts – 6th Edition
Modified by T. Suel for CS6083, NYU School of Engineering, Spring 2020 5.19 ©Silberschatz, Korth and Sudarshan

Procedural Constructs (cont.)
■ Conditional statements (if-then-else)

SQL:1999 also supports a case statement similar to C case statement
■ Example procedure: registers student after ensuring classroom capacity is not exceeded
● Returns0onsuccessand-1ifcapacityisexceeded
● Seebookfordetails
■ Signaling of exception conditions, and declaring handlers for exceptions
declare out_of_classroom_seats condition
 declare exit handler for out_of_classroom_seats
 begin

…

.. signal out_of_classroom_seats

end
● Thehandlerhereisexit–causesenclosingbegin..endtobeexited ● Otheractionspossibleonexception
Database System Concepts – 6th Edition
Modified by T. Suel for CS6083, NYU School of Engineering, Spring 2020 5.20 ©Silberschatz, Korth and Sudarshan

External Language Functions/Procedures
■ SQL:1999 permits the use of functions and procedures written in other languages such as C or C++
■ 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’
Database System Concepts – 6th Edition
Modified by T. Suel for CS6083, NYU School of Engineering, Spring 2020 5.21 ©Silberschatz, Korth and Sudarshan

External Language Routines (Cont.)
■ Benefits of external language functions/procedures:
● more efficient for many operations, and more expressive power.
■ Drawbacks
● Code to implement function may need to be loaded into database system and executed in the database system’s address space.
! risk of accidental corruption of database structures
! security risk, allowing users access to unauthorized data
● There are alternatives, which give good security at the cost of
potentially worse performance.
● Direct execution in the database system’s space is used when efficiency is more important than security.
Database System Concepts – 6th Edition
Modified by T. Suel for CS6083, NYU School of Engineering, Spring 2020 5.22 ©Silberschatz, Korth and Sudarshan

Security with External Language Routines
■ To deal with security problems ● Use sandbox techniques
! that is use a safe language like Java, which cannot be used to access/damage other parts of the database code.
● Or, run external language functions/procedures in a separate process, with no access to the database process’ memory.
! Parameters and results communicated via inter-process communication
■ Both have performance overheads
■ Many database systems support both above approaches as well as
direct executing in database system address space.
Database System Concepts – 6th Edition
Modified by T. Suel for CS6083, NYU School of Engineering, Spring 2020 5.23 ©Silberschatz, Korth and Sudarshan

Triggers
■ Atriggerisastatementthatisexecutedautomaticallybythe system as a side effect of a modification to the database.
■ To design a trigger mechanism, we must:
● Specify the conditions under which the trigger is to be
executed.
● Specify the actions to be taken when the trigger executes.
■ Triggers introduced to SQL standard in SQL:1999, but supported even earlier using non-standard syntax by most databases.
● Syntax illustrated here may not work exactly on your database system; check the system manuals
Database System Concepts – 6th Edition
Modified by T. Suel for CS6083, NYU School of Engineering, Spring 2020 5.24 ©Silberschatz, Korth and Sudarshan

Triggering Events and Actions in SQL
■ Triggering event can be insert, delete or update
■ Triggers on update can be restricted to specific attributes
● E.g., after update of takes on grade
■ Values of attributes before and after an update can be referenced
● referencing old row as : for deletes and updates
● referencing new row as : for inserts and updates
■ Triggers can be activated before an event, which can serve as extra
constraints. E.g. convert blank grades to null.
create trigger setnull_trigger before update of takes
 referencing new row as nrow

for each row

when (nrow.grade = ‘ ‘)

begin atomic

set nrow.grade = null;

end;
Database System Concepts – 6th Edition
Modified by T. Suel for CS6083, NYU School of Engineering, Spring 2020 5.25 ©Silberschatz, Korth and Sudarshan

Trigger to Maintain credits_earned value
■ create trigger credits_earned after update of takes on (grade)
 referencing new row as nrow

referencing old row as orow

for each row

when nrow.grade <> ’F’ and nrow.grade is not null
 and (orow.grade = ’F’ or orow.grade is null)

begin atomic

update student

set tot_cred= tot_cred + 

(select credits

from course

where course.course_id= nrow.course_id)

where student.id = nrow.id;
 end;
Database System Concepts – 6th Edition
Modified by T. Suel for CS6083, NYU School of Engineering, Spring 2020 5.26 ©Silberschatz, Korth and Sudarshan

Statement Level Triggers
■ Instead of executing a separate action for each affected row, a single action can be executed for all rows affected by a transaction
● Use
● Use table
for each statement instead of for each row
referencing old table or referencing new to refer to temporary tables (called transition
tables) containing the affected rows
● Can be more efficient when dealing with SQL
statements that update a large number of rows
Database System Concepts – 6th Edition
Modified by T. Suel for CS6083, NYU School of Engineering, Spring 2020 5.27 ©Silberschatz, Korth and Sudarshan

When Not To Use Triggers
■ Triggers were used earlier for tasks such as
● maintaining summary data (e.g., total salary of each department)
● Replicating databases by recording changes to special relations (called change or delta relations) and having a separate process that applies the changes over to a replica
■ There are better ways of doing these now:
● Databases today provide built in materialized view facilities to maintain summary
data
● Databases provide built-in support for replication
■ Encapsulation facilities can be used instead of triggers in many cases
● Define methods to update fields
● Carry out actions as part of the update methods instead of 
 through a trigger
■ Risk of unintended execution of triggers, for example, when
● loading data from a backup copy
● replicating updates at a remote site
● Trigger execution can be disabled before such actions.
■ Other risks with triggers:
● Error leading to failure of critical transactions that set off the trigger
● Cascading execution
Database System Concepts – 6th Edition
Modified by T. Suel for CS6083, NYU School of Engineering, Spring 2020 5.28 ©Silberschatz, Korth and Sudarshan

Recursive Queries
Database System Concepts – 6th Edition
Modified by T. Suel for CS6083, NYU School of Engineering, Spring 2020 5.29 ©Silberschatz, Korth and Sudarshan

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
Note: 1st printing of 6th ed erroneously used c_prereq in place of 
 rec_prereq in some places
Database System Concepts – 6th Edition
Modified by T. Suel for CS6083, NYU School of Engineering, Spring 2020 5.30 ©Silberschatz, Korth and Sudarshan

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
■ Computing transitive closure using iteration, adding successive tuples to c_prereq
● The next slide shows a prereq relation
● Each step of the iterative process constructs an extended version of c_prereq
from its recursive definition.
● The final result is called the fixed point of the recursive view definition.
■ Recursive views are required to be monotonic. That is, if we add tuples to prereq the view c_prereq contains all of the tuples it contained before, plus possibly more
Database System Concepts – 6th Edition
Modified by T. Suel for CS6083, NYU School of Engineering, Spring 2020 5.31 ©Silberschatz, Korth and Sudarshan

Example of Fixed-Point Computation
Database System Concepts – 6th Edition
Modified by T. Suel for CS6083, NYU School of Engineering, Spring 2020 5.32 ©Silberschatz, Korth and Sudarshan

Advanced Aggregation Features
Database System Concepts – 6th Edition
Modified by T. Suel for CS6083, NYU School of Engineering, Spring 2020 5.33 ©Silberschatz, Korth and Sudarshan

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
■ Anextraorderbyclauseisneededtogettheminsortedorder
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
Database System Concepts – 6th Edition
Modified by T. Suel for CS6083, NYU School of Engineering, Spring 2020 5.34 ©Silberschatz, Korth and Sudarshan

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;
Database System Concepts – 6th Edition
Modified by T. Suel for CS6083, NYU School of Engineering, Spring 2020 5.35 ©Silberschatz, Korth and Sudarshan

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
Database System Concepts – 6th Edition
Modified by T. Suel for CS6083, NYU School of Engineering, Spring 2020 5.36 ©Silberschatz, Korth and Sudarshan

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:
● Givenrelationsales(date,value)
select date, sum(value) over 

(order by date between rows 1 preceding and 1 following)

from sales
■ Examples of other window specifications:
● betweenrowsunboundedprecedingandcurrent ● rowsunboundedpreceding
● range between 10 preceding and current row
! All rows with values between current row value –10 to current value ● rangeinterval10daypreceding
! Not including current row
Database System Concepts – 6th Edition
Modified by T. Suel for CS6083, NYU School of Engineering, Spring 2020 5.37 ©Silberschatz, Korth and Sudarshan

OLAP
Database System Concepts – 6th Edition
Modified by T. Suel for CS6083, NYU School of Engineering, Spring 2020 5.38 ©Silberschatz, Korth and Sudarshan

Data Analysis and OLAP
■ Online Analytical Processing (OLAP)
● Interactiveanalysisofdata,allowingdatatobesummarizedand
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. ● Measureattributes
! measure some value
! can be aggregated upon
! e.g., the attribute number of the sales relation
● Dimensionattributes
! define the dimensions on which measure attributes (or
aggregates thereof) are viewed
! e.g., the attributes item_name, color, and size of the sales relation
Database System Concepts – 6th Edition
Modified by T. Suel for CS6083, NYU School of Engineering, Spring 2020 5.39 ©Silberschatz, Korth and Sudarshan

Example sales relation
… … … …
Modified by T. Suel for CS6083, NYU School o.f.E.ngineering, S.p.r.ing 2020 5.4..0. … ©Silberschatz, Korth and Sudarshan
Database System Concepts – 6th Edition

Cross Tabulation of sales by item_name and color clothes_size all
color
item_name
■ The table above is an example of a cross-tabulation (cross-tab), also referred to as a pivot-table.
● Valuesforoneofthedimensionattributesformtherowheaders
● Valuesforanotherdimensionattributeformthecolumnheaders
● Otherdimensionattributesarelistedontop
● Valuesinindividualcellsare(aggregatesof)thevaluesofthe
 dimension attributes that specify the cell.
Database System Concepts – 6th Edition
Modified by T. Suel for CS6083, NYU School of Engineering, Spring 2020 5.41 ©Silberschatz, Korth and Sudarshan
dark
pastel
white
total
skirt dress shirt pants
8 20 14 20
35 10 7 2
10 5 28 5
53 35 49 27
total
62
54
48
164

Data Cube
■ Adatacubeisamultidimensionalgeneralizationofacross-tab ■ Can have n dimensions; we show 3 below
■ Cross-tabs can be used as views on a data cube
Database System Concepts – 6th Edition
Modified by T. Suel for CS6083, NYU School of Engineering, Spring 2020 5.42 ©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 – 6th Edition
Modified by T. Suel for CS6083, NYU School of Engineering, Spring 2020 5.43 ©Silberschatz, Korth and Sudarshan

Cross Tabulation With Hierarchy
■ Cross-tabs can be easily extended to deal with hierarchies l Candrilldownorrolluponahierarchy
Database System Concepts – 6th Edition
Modified by T. Suel for CS6083, NYU School of Engineering, Spring 2020 5.44 ©Silberschatz, Korth and Sudarshan

Relational Representation of Cross-tabs
■ Cross-tabs can be represented as relations
l We use the value all is used to represent aggregates.
l The SQL standard actually uses null values in place of all despite confusion with regular null values.
Database System Concepts – 6th Edition
Modified by T. Suel for CS6083, NYU School of Engineering, Spring 2020 5.45
©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, 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), (item_name), (size),
(color, size), 
 (color), 

( ) }
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 – 6th Edition
Modified by T. Suel for CS6083, NYU School of Engineering, Spring 2020 5.46 ©Silberschatz, Korth and Sudarshan

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)
Database System Concepts – 6th Edition
Modified by T. Suel for CS6083, NYU School of Engineering, Spring 2020 5.47 ©Silberschatz, Korth and Sudarshan

■ ■
Online Analytical Processing Operations
Pivoting: changing the dimensions used in a cross-tab is called Slicing: creating a cross-tab for fixed values only
● Sometimescalleddicing,particularlywhenvaluesformultiple dimensions are fixed.
Rollup: moving from finer-granularity data to a coarser granularity granularity data to finer-granularity data

■ Drill down: The opposite operation – that of moving from coarser-
Database System Concepts – 6th Edition
Modified by T. Suel for CS6083, NYU School of Engineering, Spring 2020 5.48 ©Silberschatz, Korth and Sudarshan

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.
Database System Concepts – 6th Edition
Modified by T. Suel for CS6083, NYU School of Engineering, Spring 2020 5.49 ©Silberschatz, Korth and Sudarshan