Chapter 8

advertisement
8
Chapter 8
Advanced SQL
Database Systems:
Design, Implementation, and Management,
Seventh Edition, Rob and Coronel
1
8
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
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
2
8
In this chapter, you will learn (continued):
• How to create and use updatable views
• How to create and use triggers and stored
procedures
• How to create embedded SQL
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
3
8
Relational Set Operators
• UNION
• INTERSECT
• MINUS
• Work properly if relations are unioncompatible
– Names of relation attributes must be the same
and their data types must be identical
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
4
8
UNION
• 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;
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
5
8
UNION (continued)
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
6
8
UNION ALL
• 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;
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
7
8
UNION ALL (continued)
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
8
8
INTERSECT
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
9
8
MINUS
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
10
8
Syntax Alternatives
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
11
8
Syntax Alternatives (continued)
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
12
8
SQL Join Operators
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
13
8
Cross Join
• Syntax:
– SELECT column-list FROM table1 CROSS
JOIN table2
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
14
8
Natural Join
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
15
8
JOIN USING Clause
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
16
8
JOIN ON Clause
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
17
8
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
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
18
8
Outer Joins (continued)
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
19
8
Outer Joins (continued)
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
20
8
Outer Joins (continued)
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
21
8
Subqueries and Correlated Queries
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
22
8
WHERE Subqueries
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
23
8
IN Subqueries
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
24
8
HAVING Subqueries
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
25
8
Multirow Subquery Operators:
ANY and ALL
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
26
8
FROM Subqueries
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
27
8
Attribute List Subqueries
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
28
8
Attribute List Subqueries (continued)
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
29
8
Correlated Subqueries
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
30
8
Correlated Subqueries (continued)
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
31
8
Date and Time Functions
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
32
8
Date and Time Functions (continued)
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
33
8
Date and Time Functions (continued)
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
34
Download