0% found this document useful (0 votes)
9 views33 pages

Chapter 9

databases

Uploaded by

Long life
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views33 pages

Chapter 9

databases

Uploaded by

Long life
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

DATABASE SYSTEMS

DESIGN IMPLEMENTATION AND MANAGEMENT

INTERNATIONAL EDITION

ROB • CORONEL • CROCKETT

Chapter 9
Advanced SQL
DATABASE SYSTEMS: Design Implementation and Management (Rob, 2
Coronel & Crockett 9781844807321)

In this chapter, you will learn:

• About the relational set operators UNION,


UNION ALL, INTERSECT, and MINUS
• How to use the advanced SQL JOIN operator
syntax
• About the different types of subqueries and
correlated queries
• How to use SQL functions to manipulate dates,
strings, and other data

2
DATABASE SYSTEMS: Design Implementation and Management (Rob, 2
Coronel & Crockett 9781844807321)

In this chapter, you will learn

• How to create and use updatable views


• How to create and use triggers and stored
procedures
• How to create embedded SQL

3
DATABASE SYSTEMS: Design Implementation and Management (Rob, 2
Coronel & Crockett 9781844807321)

SQL JOIN OPERATORS

• The relational join operation merges rows


from 2 tables and returns the rows with one of
the following conditions
– Have common values in common columns (natural
joint)
– Meet a given join condition (equality or inequality)
– Have common values in common columns or have no
matching values

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

• If no common attribute exist in both tables


• Query will return only the rows that meet the
indicated join condition
SELECT INVOICE.INV_NUMBER, PRODUCT.P_CODE
FROM INVOICE JOIN PRODUCT ON
INVOICE.INV_NUMBER = PRODUCT.INV_NUMBER

14
DATABASE SYSTEMS: Design Implementation and Management (Rob, 2
Coronel & Crockett 9781844807321)

Outer Joins

• Returns not only matching rows, but also rows


with unmatched attribute values for one table or
both tables to be joined
• Three types
– Left
– Right
– Full

15
DATABASE SYSTEMS: Design Implementation and Management (Rob, 2
Coronel & Crockett 9781844807321)

LEFT Outer Joins

• Returns rows matching the join condition AND


rows in the left table with unmatched values in
the table
SELECT COLUMN LIST
FROM TABLE1LEFT JOIN TABLE2 ON JOIN
CONDITION
select * from PRODUCT LEFT OUTER JOIN
VENDOR ON PRODUCT.VEND_NUMBER =
VENDOR.VEND_NUMBER 16
DATABASE SYSTEMS: Design Implementation and Management (Rob, 2
Coronel & Crockett 9781844807321)

RIGTH Outer Joins

• Returns rows matching the join condition AND


rows in the RIGHT table with unmatched values
in the table
SELECT COLUMN LIST
FROM TABLE1RIGHT JOIN TABLE2 ON JOIN
CONDITION

17
DATABASE SYSTEMS: Design Implementation and Management (Rob, 2
Coronel & Crockett 9781844807321)

FULL Outer Joins

• Returns rows matching the join condition, but


also returns all of the rows with unmatched
values in the table on either side

SELECT COLUMN LIST


FROM TABLE1FULL JOIN TABLE2 ON JOIN
CONDITION

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)

Date and Time Functions

20
DATABASE SYSTEMS: Design Implementation and Management (Rob, 2
Procedural SQL / Transact-SQL
Coronel & Crockett 9781844807321)

• SQL-99 standard defined the • Oracle’s PSM is called


use of persistent stored Procedural SQL (PL/SQL).
modules PL/SQL can be used for:
– A persistent stored model – Anonymous PL/SQL blocks
(PSM) is a block of code – Triggers
(containing standard SQL – Stored procedures
statements and procedural
extensions) that is stored and – PL/SQL functions
executed at the DBMS server • Microsoft’s PSM is called
– The PSM represents business Transact-SQL (T-SQL)
logic that can be encapsulated,
stored and shared among
with the same functions as
multiple database users PL/SQL, but different
– DBA can assign access rights syntax

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.

• A trigger is associated with a database table.


• Each database table may have one or more triggers.
• A trigger is executed as part of the transaction that
triggered it. 23
DATABASE SYSTEMS: Design Implementation and Management (Rob, 2
Coronel & Crockett 9781844807321)

Trigger

• For example, when a new record (representing a


new worker) is added to the employees table,
new records should also be created in the tables
of the taxes, vacations and salaries.
• Triggers can also be used to log historical data,
for example to keep track of employees' previous
salaries or telephone numbers.

24
DATABASE SYSTEMS: Design Implementation and Management (Rob, 2
Coronel & Crockett 9781844807321)

Types of Triggers

• A statement-level trigger is assumed if you


omit the FOR EACH ROW keywords.
• This type of trigger is executed once, before or
after the triggering statement is completed.
• This is the default case.

25
DATABASE SYSTEMS: Design Implementation and Management (Rob, 2
Coronel & Crockett 9781844807321)

Types of Triggers

• A row-level trigger requires use of the FOR


EACH ROW keywords.
• This type of trigger is executed once for each
row affected by the triggering statement.
• In other words, if you update 10 rows, the trigger
executes 10 times.

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)

• Creation of stored procedure to increase discount


by 5% if quantity on hand is more than or equal to
twice the minimum quantity before reordering

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

• SQL provides relational set operators to combine


output of two queries to generate new relation
• Operations that join tables can be classified as
inner joins and outer joins
• Natural join returns all rows with matching values
in the matching columns and eliminates duplicate
columns

30
DATABASE SYSTEMS: Design Implementation and Management (Rob, 2
Coronel & Crockett 9781844807321)

Summary

• Joins may use keywords, such as USING and ON


• Subqueries and correlated queries are used when
it is necessary to process data based on other
processed data
• Most subqueries are executed in serial fashion

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

• When SQL statements are designed to return


more than one value inside the PL/SQL code,
cursor is needed
• Embedded SQL refers to use of SQL statements
within an application programming language

33

You might also like