Chapter 9
Chapter 9
INTERNATIONAL EDITION
Chapter 9
Advanced SQL
DATABASE SYSTEMS: Design Implementation and Management (Rob, 2
Coronel & Crockett 9781844807321)
2
DATABASE SYSTEMS: Design Implementation and Management (Rob, 2
Coronel & Crockett 9781844807321)
3
DATABASE SYSTEMS: Design Implementation and Management (Rob, 2
Coronel & Crockett 9781844807321)
4
DATABASE SYSTEMS: Design Implementation and Management (Rob, 2
Coronel & Crockett 9781844807321)
Relational Set Operators
• Work properly if relations are union-compatible
– Names of relation attributes must be the same and
their data types must be identical
• Inner join
– Only rows that meet a given criterion are selected
• Outer join
– Returns not only matching rows but the rows with
unmatched attribute values for one table or both
tables to be joined
5
DATABASE SYSTEMS: Design Implementation and Management (Rob, 2
Coronel & Crockett 9781844807321)
UNION
will exclude duplicate records!!!!
Query UNION Query
6
DATABASE SYSTEMS: Design Implementation and Management (Rob, 2
Coronel & Crockett 9781844807321)
UNION
will exclude duplicate records!!!!
• Example query:
– SELECT CUS_LNAME, CUS_FNAME,
CUS_INITIAL, CUS_AREACODE,
CUS_PHONE
FROM CUSTOMER
UNION
SELECT CUS_LNAME, CUS_FNAME,
CUS_INITIAL, CUS_AREACODE,
CUS_PHONE
FROM CUSTOMER_2;
7
DATABASE SYSTEMS: Design Implementation and Management (Rob, 2
Coronel & Crockett 9781844807321)
UNION
Table 1
8
DATABASE SYSTEMS: Design Implementation and Management (Rob, 2
Coronel & Crockett 9781844807321)
UNION
Table 2
Result
9
DATABASE SYSTEMS: Design Implementation and Management (Rob, 2
Coronel & Crockett 9781844807321)
UNION ALL
will retain the duplicate rows!!!!
• Example query:
– SELECT CUS_LNAME, CUS_FNAME,
CUS_INITIAL, CUS_AREACODE,
CUS_PHONE
FROM CUSTOMER
UNION ALL
SELECT CUS_LNAME, CUS_FNAME,
CUS_INITIAL, CUS_AREACODE,
CUS_PHONE
FROM CUSTOMER_2;
10
DATABASE SYSTEMS: Design Implementation and Management (Rob, 2
Coronel & Crockett 9781844807321)
UNION ALL
11
DATABASE SYSTEMS: Design Implementation and Management (Rob, 2
Coronel & Crockett 9781844807321)
INTERSECT
• Combine rows from two queries, returning only
the rows that appear in both sets
• Query INTERSECT query
• SELECT CUS_LNAME, CUS_FNAME,
CUS_INITIAL, CUS_AREACODE,
CUS_PHONE
FROM CUSTOMER
INTERSECT
SELECT CUS_LNAME, CUS_FNAME,
CUS_INITIAL, CUS_AREACODE,
CUS_PHONE
FROM CUSTOMER_2;
12
DATABASE SYSTEMS: Design Implementation and Management (Rob, 2
Coronel & Crockett 9781844807321)
MINUS
• Combines rows from two queries and returns
only the rows that appear in the first set but not
in the second
• Query MINUS Query
• SELECT CUS_LNAME, CUS_FNAME,
CUS_INITIAL, CUS_AREACODE,
CUS_PHONE
FROM CUSTOMER
MINUS
SELECT CUS_LNAME, CUS_FNAME,
CUS_INITIAL, CUS_AREACODE,
CUS_PHONE
FROM CUSTOMER_2;
13
DATABASE SYSTEMS: Design Implementation and Management (Rob, 2
Coronel & Crockett 9781844807321)
JOIN ON Clause
14
DATABASE SYSTEMS: Design Implementation and Management (Rob, 2
Coronel & Crockett 9781844807321)
Outer Joins
15
DATABASE SYSTEMS: Design Implementation and Management (Rob, 2
Coronel & Crockett 9781844807321)
17
DATABASE SYSTEMS: Design Implementation and Management (Rob, 2
Coronel & Crockett 9781844807321)
18
DATABASE SYSTEMS: Design Implementation and Management (Rob, 2
Coronel & Crockett 9781844807321)
Special operators
19
DATABASE SYSTEMS: Design Implementation and Management (Rob, 2
Coronel & Crockett 9781844807321)
20
DATABASE SYSTEMS: Design Implementation and Management (Rob, 2
Procedural SQL / Transact-SQL
Coronel & Crockett 9781844807321)
21
Transact-SQL – Anonymous T-SQL block
DATABASE SYSTEMS: Design Implementation and Management (Rob,
Coronel & Crockett 9781844807321)
2
with feedback message
• BEGIN starts the
block and END ends it
• Addition of a simple
feedback message
using the PRINT
command
22
DATABASE SYSTEMS: Design Implementation and Management (Rob, 2
Coronel & Crockett 9781844807321)
Triggers
• A trigger is procedural SQL code that is automatically
invoked by the RDBMS upon the occurrence of a
given data manipulation event. It is useful to remember
that:
– A trigger is invoked before or after a data row is inserted,
updated or deleted.
Trigger
24
DATABASE SYSTEMS: Design Implementation and Management (Rob, 2
Coronel & Crockett 9781844807321)
Types of Triggers
25
DATABASE SYSTEMS: Design Implementation and Management (Rob, 2
Coronel & Crockett 9781844807321)
Types of Triggers
26
DATABASE SYSTEMS: Design Implementation and Management (Rob, 2
Coronel & Crockett 9781844807321)
Stored Procedures
• Advantages
– Substantially reduces network traffic and increases
performance
• No transmission of individual SQL statements over network
– Help reduce code duplication by means of code
isolation and code sharing
• Minimize chance of errors and cost of application
development and maintenance
27
DATABASE SYSTEMS: Design Implementation and Management (Rob, 2
Example of Stored Procedure
Coronel & Crockett 9781844807321)
28
DATABASE SYSTEMS: Design Implementation and Management (Rob, 2
Coronel & Crockett 9781844807321)
Cursors
• A cursor is a special construct used in procedural SQL to
hold the data rows returned by an SQL query.
• It is an area of memory reserved for the output, like an
array of columns and rows
• Held in the server, not in the client’s computer
29
DATABASE SYSTEMS: Design Implementation and Management (Rob, 2
Coronel & Crockett 9781844807321)
Summary
30
DATABASE SYSTEMS: Design Implementation and Management (Rob, 2
Coronel & Crockett 9781844807321)
Summary
31
DATABASE SYSTEMS: Design Implementation and Management (Rob, 2
Coronel & Crockett 9781844807321)
Summary
• SQL functions are used to extract or transform
data
• Oracle sequences may be used to generate values
to be assigned to a record
• PL/SQL can be used to create triggers, stored
procedures, and PL/SQL functions
• A stored procedure is a named collection of SQL
statements
32
DATABASE SYSTEMS: Design Implementation and Management (Rob, 2
Coronel & Crockett 9781844807321)
Summary
33