CHAPTER 7: ADVANCED SQL
Modern ern Data tabas base e Manag agement ement
12 12th
th Edition
tion Global bal Edition tion
Je Jeff Hoffer, , Rame mesh sh Venkatar ataraman aman, , Heikk kki Topi
授課老師:楊立偉教授,台灣大學工管系 (13版於Chapter 6)
CHAPTER 7: ADVANCED SQL Modern ern Data tabas base e Manag - - PowerPoint PPT Presentation
CHAPTER 7: ADVANCED SQL Modern ern Data tabas base e Manag agement ement th Edition 12 12 th tion Global bal Edition tion Je Jeff Hoffer, , Rame mesh sh Venkatar ataraman aman, , Heikk kki Topi
12 12th
th Edition
tion Global bal Edition tion
授課老師:楊立偉教授,台灣大學工管系 (13版於Chapter 6)
Chapter 7 7-2
Chapter 7 7-3
Chapter 7 7-4
Chapter 7 7-5
Chapter 7 7-6
Emp Dept no name dept_no no name mgr_no 1 張三 1 1 會計部 3 2 李四 2 2 工程部 4 3 王五 1 4 毛六 2 5 陳七 3 no name dept_no no name mgr_no 1 張三 1 1 會計部 3 2 李四 2 2 工程部 4 3 王五 1 1 會計部 3 4 毛六 2 2 工程部 4 no name dept_no no name mgr_no 1 張三 1 1 會計部 3 2 李四 2 2 工程部 4 3 王五 1 1 會計部 3 4 毛六 2 2 工程部 4 5 陳七 3 null null null
Chapter 7 7-7
Emp Dept no name dept_no no name mgr_no 1 張三 1 1 會計部 3 2 李四 2 2 工程部 4 3 王五 1 4 毛六 2 5 陳七 3 no name dept_no no name mgr_no 1 張三 1 1 會計部 3 2 李四 2 2 工程部 4 3 王五 1 1 會計部 3 4 毛六 2 2 工程部 4
Chapter 7 7-8
Chapter 7 7-9
Chapter 7 7-10
有15個客戶 (9個客戶下過訂單; 其中1位下過2張訂單) 有10筆訂單
Chapter 7 7-11
For each customer who placed an order, what is
Chapter 7 7-12
Chapter 7 7-13
For each customer who placed an order, what is the
Note: From Fig. 7-1, you see that only 10 Customers have links with orders. Only 10 rows will be returned from this INNER join 通常直接寫JOIN, 較少使用NATURAL JOIN
Chapter 7 7-14
List the customer name, ID number, and order number
Chapter 7 7-15
Chapter 7 7-16
Assemble all information necessary to create an invoice for order
Chapter 7 7-17
17
Chapter 7 7-18
All rows returned from this query will pertain to Order ID 1006. Note that the full query results include columns from four different tables.
Chapter 7 7-19
Chapter 7 7-20
From Chapter 2 Unary 1:N
Chapter 7 7-21
Combine the output (union of multiple queries)
First query Second query Combine
以常數字串 增加一欄作為 說明文字
Chapter 7 7-22
Chapter 7 7-23
Subquery
placing an inner query (SELECT statement) inside an outer
Options:
As a “table” of the FROM clause 在FROM當新來源 Returning a field for the SELECT clause在SELECT子句使用 In a condition of the WHERE clause 在WHERE子句使用 Within the HAVING clause 在HAVING子句使用
Subqueries can be:
Noncorrelated–executed once for the entire outer query Correlated–executed once for each row returned by the outer
Chapter 7 7-24
在這個例子DISTINCT可加可不加,為什麼?
Chapter 7 7-25
Chapter 7 7-26
Chapter 7 7-27
Subquery寫法指示了DBMS運算順序;Join寫法將運算細節交給DBMS自行決定
Chapter 7 7-28
Chapter 7 7-29
Chapter 7 7-30
Chapter 7 7-31
Chapter 7 7-32
A noncorrelated subquery processes completely before the outer query begins. No reference to data in outer query, so subquery executes once only
Chapter 7 7-33
Show all orders that include furniture finished in natural
The EXISTS operator will return a TRUE value if the subquery resulted in a non-empty set,
A correlated subquery always refers to an attribute from a table referenced in the outer query
Chapter 7 7-34
Note: Only the
involve products with Natural Ash will be included in the final results.
Chapter 7 7-35
Chapter 7 7-36
Show all products whose standard price is higher than
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.
Chapter 7 7-37
先算出產品的平均單價,再依此為條件做篩選 SELECT PRODUCT_DESCRIPTION, STANDARD_PRICE FROM PRODUCT_T WHERE STANDARD_PRICE > (SELECT AVG(STANDARD_PRICE) AVGPRICE FROM PRODUCT_T)
Chapter 7 7-38
Chapter 7 7-39
Understand how indexes are used in query processing 了
Keep optimizer statistics up-to-date 現今DBMS會保持索引更新 Use compatible data types for fields and literals 避免DBMS 內部自動多作過多的型別轉換 Write simple queries 避免複雜查詢 (例如少用子查詢) Break complex queries into multiple simple parts 把複雜查 詢做拆解 (同下,少用巢狀查詢) Don’t nest one query inside another query; Don’t
Chapter 7 7-40
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! 對複雜查詢多試不同的寫法; 查閱各DBMS之技術手冊 Consider the total query processing time for ad hoc
Chapter 7 7-41
Chapter 7 7-42
Chapter 7 7-43
Instead of SELECT *, identify the specific attributes
Limit the number of subqueries; try to make
Chapter 7 7-44
Program modules that execute on demand
routines that execute in response to a database
Ex. 當INSERT至ORDER表格時,
Chapter 7 7-45
Source: adapted from Mullins, 1995.
Chapter 7 7-46
Example DML Trigger Example DDL Trigger
Chapter 7 7-47
Chapter 7 7-48
Chapter 7 7-49
Chapter 7 7-50
Including hard-coded SQL statements in a program
Ability for an application program to generate SQL
Ex. 輸入客戶名稱檢查是否存在 (0表示不存在)
Chapter 7 7-51
Transaction = A discrete unit of work that must be
確保動作完成不被中斷分割 May involve multiple updates If any update fails, then all other updates must be
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
在MySQL中可設 SET AutoCommit=0或1
Chapter 7 7-52
Chapter 7 7-53
When multiple users access the database…
Query the total balance
SELECT sum(amount)
Transfer $100 from bank
UPDATE account SET
UPDATE account SET
Timeline A tentative value will be retrieved
Chapter 7 7-54
4 properties that guarantee that database
Atomicity 不可分割性
Transaction cannot be subdivided; each transaction be "all or
Consistency 一致性
Constraints don't change from before transaction to after
Isolation 隔離性
Database changes not revealed to users until after transaction
Durability 持續性
Database changes are permanent; once a transaction has been
Chapter 7 7-55
User-defined data types (UDT) Subclasses of standard types or an object type Analytical functions (for OLAP) CEILING, FLOOR, SQRT, RANK, DENSE_RANK, ROLLUP,
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
Chapter 7 7-56
Programming extensions 增加程式化能力
Makes SQL into a procedural language Oracle has propriety version called PL/SQL, and Microsoft S
New statements: CASE, IF, LOOP, FOR, WHILE, etc. 增加語法 Persistent Stored Modules (SQL/PSM),may create and