chapter 7 © 2013 pearson education, inc. publishing as prentice hall 1 modern database management...
TRANSCRIPT
![Page 1: Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi](https://reader031.vdocuments.site/reader031/viewer/2022031909/56649eb15503460f94bb7ba3/html5/thumbnails/1.jpg)
Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall1
CHAPTER 7:ADVANCED SQL(PART 2)
Modern Database Management11th Edition
Jeffrey A. Hoffer, V. Ramesh, Heikki Topi
![Page 2: Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi](https://reader031.vdocuments.site/reader031/viewer/2022031909/56649eb15503460f94bb7ba3/html5/thumbnails/2.jpg)
Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall2
OBJECTIVES
Define terms
Write single and multiple table SQL queries
Note in SQL Handout 3 Define and use three types of joins
Self-Join Write noncorrelated and correlated
subqueries Establish referential integrity in SQL
![Page 3: Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi](https://reader031.vdocuments.site/reader031/viewer/2022031909/56649eb15503460f94bb7ba3/html5/thumbnails/3.jpg)
Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall
SELF-JOIN EXAMPLE
3
Self-joins are often used on tables with unary relationships
Note which one is which one!
3
The same table is used on both
sides of the join (remember the
ERD? – and think about tables); distinguished
using table aliases
![Page 4: Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi](https://reader031.vdocuments.site/reader031/viewer/2022031909/56649eb15503460f94bb7ba3/html5/thumbnails/4.jpg)
Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall
FIG 7-5 EXAMPLE OF A SELF-JOIN
4
![Page 5: Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi](https://reader031.vdocuments.site/reader031/viewer/2022031909/56649eb15503460f94bb7ba3/html5/thumbnails/5.jpg)
Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall5
PROCESSING MULTIPLE TABLES USING SUBQUERIES
Subquery–placing an inner query (SELECT statement) inside an outer query
Options:1. As a “table” of the FROM clause
In this case a view is created in the subquery and the view is then used in the main query
2. In a condition of the WHERE clause3. Within the HAVING clause
Subqueries can be: Noncorrelated–executed once for the entire
outer query Correlated–executed once for each row
returned by the outer query
![Page 6: Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi](https://reader031.vdocuments.site/reader031/viewer/2022031909/56649eb15503460f94bb7ba3/html5/thumbnails/6.jpg)
Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall6
What are the name and address of the customer who placed order #1008?
SELECT CUSTOMER_NAME, CUSTOMER_ADDRESS
FROM CUSTOMER_TWHERE CUSTOMER_T.CUSTOMER_ID =
(SELECT CUSTOMER_ID FROM ORDER_T
WHERE ORDER_ID = 1008);
Note: the value for ORDER_ID does NOT appear in the query result; it is used as the selection criterion in the inner query Data from a subquery cannot be included in the final results They are only used as ______
SUBQUERY EXAMPLE
![Page 7: Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi](https://reader031.vdocuments.site/reader031/viewer/2022031909/56649eb15503460f94bb7ba3/html5/thumbnails/7.jpg)
Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall
JOIN VS. SUBQUERY Some queries could be accomplished by either a
join or a subquery [Examine the query on last slide]
7
Join version
Subquery version
![Page 8: Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi](https://reader031.vdocuments.site/reader031/viewer/2022031909/56649eb15503460f94bb7ba3/html5/thumbnails/8.jpg)
Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall8
SUBQUERY EXAMPLE (CONT) – JOIN TABLES VS SUBQUERY
![Page 9: Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi](https://reader031.vdocuments.site/reader031/viewer/2022031909/56649eb15503460f94bb7ba3/html5/thumbnails/9.jpg)
Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall9
SUBQUERY EXAMPLE (CONT) – JOIN TABLES VS SUBQUERY
![Page 10: Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi](https://reader031.vdocuments.site/reader031/viewer/2022031909/56649eb15503460f94bb7ba3/html5/thumbnails/10.jpg)
Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall10
Show all customers who have placed an order
SELECT CUSTOMER_NAME FROM CUSTOMER_TWHERE CUSTOMER_ID IN
(SELECT DISTINCT CUSTOMER_ID FROM ORDER_T);
SUBQUERY – “IN”
Subquery is embedded in parentheses. In this case it returns a list that will be used in the WHERE clause of the outer query
The IN operator will test to see if the CUSTOMER_ID value of a row is included in the list returned from the subquery
NOT/ANY/ALL may be used in front of IN; logical operators =,<, > can be used
![Page 11: Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi](https://reader031.vdocuments.site/reader031/viewer/2022031909/56649eb15503460f94bb7ba3/html5/thumbnails/11.jpg)
Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall11
FIG 7-7 SUBQUERY – “NOT IN”
![Page 12: Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi](https://reader031.vdocuments.site/reader031/viewer/2022031909/56649eb15503460f94bb7ba3/html5/thumbnails/12.jpg)
Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall12
Show all products whose standard price is higher than the average price
ANOTHER SUBQUERY EXAMPLE
The WHERE clause normally cannot include aggregate functions, but because the aggregate is performed in the subquery its result can be used in the outer query’s WHERE clause
One column of the subquery is an aggregate function that has an alias name. That alias can then be referred to in the outer query
Subquery forms the derived table [view] used in the FROM clause of the outer query
Differentiate: column name (header) vs variable
![Page 13: Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi](https://reader031.vdocuments.site/reader031/viewer/2022031909/56649eb15503460f94bb7ba3/html5/thumbnails/13.jpg)
Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall13
CORRELATED VS. NONCORRELATED SUBQUERIES
Noncorrelated subqueries: Do not depend on data from the
outer query Execute once for the entire outer
query i.e. the subquery “first runs by
itself”, “then pass the result to outer query”
Correlated subqueries: Make use of data from the outer
query Execute once for each row of the
outer query Can use the EXISTS operator
![Page 14: Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi](https://reader031.vdocuments.site/reader031/viewer/2022031909/56649eb15503460f94bb7ba3/html5/thumbnails/14.jpg)
Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall14
EXISTS take a value of true if the subquery
returns an intermediate results table that contains one or more rows (a nonempty set),
false if no rows are returned (empty set) Query: What are the order IDs for all orders
that have included furniture finished in natural ash?SELECT DISTINCT ORDER_ID FROM ORDER_LINE_TWHERE EXISTS
(SELECT * FROM PRODUCT_TWHERE PRODUCT_ID =
ORDER_LINE_T.PRODUCT_IDAND PRODUCT_FINISH = ‘Natural
Ash’);Note “select *”…
SUBQUERY – “EXISTS”
![Page 15: Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi](https://reader031.vdocuments.site/reader031/viewer/2022031909/56649eb15503460f94bb7ba3/html5/thumbnails/15.jpg)
Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall15
Figure 7-8a Processing a noncorrelated subquery
A noncorrelated subquery processes completely before the outer query begins
![Page 16: Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi](https://reader031.vdocuments.site/reader031/viewer/2022031909/56649eb15503460f94bb7ba3/html5/thumbnails/16.jpg)
Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall16
Show all orders that include furniture finished in natural ash
SELECT DISTINCT OrderID FROM OrderLine_TWHERE EXISTS
(SELECT * FROM Product_T WHERE ProductID = OrderLine_T.ProductID AND Productfinish = ‘Natural ash’);
CORRELATED SUBQUERY EXAMPLE
The subquery is testing for a value that comes from the outer query
The EXISTS operator will return a TRUE value if the subquery resulted in a non-empty set, otherwise it returns a FALSE
A correlated subquery always refers to an attribute from a table referenced in the outer query
![Page 17: Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi](https://reader031.vdocuments.site/reader031/viewer/2022031909/56649eb15503460f94bb7ba3/html5/thumbnails/17.jpg)
Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall17
Figure 7-8b Processing a correlated subquery
Subquery refers to outer-query data, so executes once for each row of outer query
Note: Only the orders that involve products with Natural Ash will be included in the final results.
17Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall
![Page 18: Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi](https://reader031.vdocuments.site/reader031/viewer/2022031909/56649eb15503460f94bb7ba3/html5/thumbnails/18.jpg)
Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall18
UNION QUERIES Combine the output (union of multiple
queries) together into a single result table
First query
Second query
Combine
![Page 19: Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi](https://reader031.vdocuments.site/reader031/viewer/2022031909/56649eb15503460f94bb7ba3/html5/thumbnails/19.jpg)
Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall19
Figure 7-8 Combining queries using UNION
Note: with UNION queries, the quantity and data types of the attributes in the SELECT clauses of both queries must be identical
![Page 20: Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi](https://reader031.vdocuments.site/reader031/viewer/2022031909/56649eb15503460f94bb7ba3/html5/thumbnails/20.jpg)
Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall20
CONDITIONAL EXPRESSIONS USING CASE SYNTAX
This is available with newer versions of SQL, previously not part of the standard
Figure 7-9
![Page 21: Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi](https://reader031.vdocuments.site/reader031/viewer/2022031909/56649eb15503460f94bb7ba3/html5/thumbnails/21.jpg)
Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall21
TIPS FOR DEVELOPING QUERIES
Be familiar with the data model (entities and relationships)
Understand the desired results Know the attributes desired in result Identify the entities that contain desired
attributes Review ERD Construct a WHERE equality for each
link Fine tune with GROUP BY and HAVING
clauses if needed Consider the effect on unusual data
![Page 22: Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi](https://reader031.vdocuments.site/reader031/viewer/2022031909/56649eb15503460f94bb7ba3/html5/thumbnails/22.jpg)
Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall
QUERY EFFICIENCY CONSIDERATIONS Instead of SELECT *, identify the
specific attributes in the SELECT clause; this helps reduce network traffic of result set
Limit the number of subqueries; try to make everything done in a single query if possible
If data is to be used many times, make a separate query and store its results rather than performing the query repeatedly
22
![Page 23: Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi](https://reader031.vdocuments.site/reader031/viewer/2022031909/56649eb15503460f94bb7ba3/html5/thumbnails/23.jpg)
Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall
GUIDELINES FOR BETTER QUERY DESIGN
Write simple queries Break complex queries into multiple
simple parts Understand how indexes are used in query
processing Keep optimizer statistics up-to-date Use compatible data types for fields and
literals Don’t nest one query inside another query Don’t combine a query with itself (if
possible avoid self-joins)
23
![Page 24: Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi](https://reader031.vdocuments.site/reader031/viewer/2022031909/56649eb15503460f94bb7ba3/html5/thumbnails/24.jpg)
Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall
GUIDELINES FOR BETTER QUERY DESIGN (CONT.)
Create temporary tables for groups of queries
Combine update operations Retrieve only the data you need Don’t have the DBMS sort without an index Learn! Consider the total query processing time
for ad hoc queries
24
![Page 25: Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi](https://reader031.vdocuments.site/reader031/viewer/2022031909/56649eb15503460f94bb7ba3/html5/thumbnails/25.jpg)
Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall25
ENSURING TRANSACTION INTEGRITY Transaction = A discrete unit of work
that must be completely processed or not processed at all May involve multiple updates If any update fails, then all other updates
must be cancelled SQL commands for transactions
BEGIN TRANSACTION/END TRANSACTION Marks boundaries of a transaction
COMMIT Makes all updates permanent
ROLLBACK Cancels updates since the last COMMIT
![Page 26: Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi](https://reader031.vdocuments.site/reader031/viewer/2022031909/56649eb15503460f94bb7ba3/html5/thumbnails/26.jpg)
Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall26
Figure 7-12 An SQL Transaction sequence (in pseudocode)
Our lecture may end with this slide
![Page 27: Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi](https://reader031.vdocuments.site/reader031/viewer/2022031909/56649eb15503460f94bb7ba3/html5/thumbnails/27.jpg)
Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall27
DATA DICTIONARY FACILITIES System tables that store metadata Users usually can view some of these tables Users are restricted from updating them Some examples in Oracle 11g
DBA_TABLES – descriptions of tables DBA_CONSTRAINTS – description of constraints DBA_USERS – information about the users of the
system Examples in Microsoft SQL Server 2008
sys.columns – table and column definitions sys.indexes – table index information sys.foreign_key_columns – details about columns
in foreign key constraints
![Page 28: Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi](https://reader031.vdocuments.site/reader031/viewer/2022031909/56649eb15503460f94bb7ba3/html5/thumbnails/28.jpg)
Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall
SQL:2008 ENHANCEMENTS/EXTENSIONS User-defined data types (UDT) Subclasses of standard types or an object type
Analytical functions (for OLAP) CEILING, FLOOR, SQRT, RANK, DENSE_RANK,
ROLLUP, CUBE, SAMPLE, WINDOW–improved numerical analysis capabilities
New Data Types BIGINT, MULTISET (collection), XML
CREATE TABLE LIKE–create a new table similar to an existing one
MERGE
28
![Page 29: Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi](https://reader031.vdocuments.site/reader031/viewer/2022031909/56649eb15503460f94bb7ba3/html5/thumbnails/29.jpg)
Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall
Programming extensions Persistent Stored Modules (SQL/PSM)
Capability to create and drop code modules
New statements: CASE, IF, LOOP, FOR, WHILE, etc. Makes SQL into a procedural language
Oracle has propriety version called PL/SQL, and Microsoft SQL Server has Transact/SQL
29
SQL:2008 ENHANCEMENTS (CONT)