Chapter 8 Advanced SQL 11Chapter 7 2013 Fall

  • Slides: 36
Download presentation
Chapter 8: Advanced SQL 註 : 於 11版為Chapter 7 楊立偉教授 台灣大學 管系 2013 Fall

Chapter 8: Advanced SQL 註 : 於 11版為Chapter 7 楊立偉教授 台灣大學 管系 2013 Fall 1

Processing Multiple Tables–Joins n Join–a relational operation that causes two or more tables with

Processing Multiple Tables–Joins n Join–a relational operation that causes two or more tables with a common domain to be combined into a single table or view n Equi-join–a join in which the joining condition is based on n Natural join–an equi-join in which one of the duplicate columns n Outer join–a join in which rows that do not have matching n Union join–includes all columns from each table in the join, and equality between values in the common columns; common columns appear redundantly in the result table is eliminated in the result table values in common columns are nonetheless included in the result table (as opposed to inner join, in which rows must have matching values in order to appear in the result table) an instance for each row of each table The common columns in joined tables are usually the primary key of the dominant table and the foreign key of the dependent table in 1: M relationships Chapter 8 2

Figure 8 -2 Visualization of different join types with results returned in shaded area

Figure 8 -2 Visualization of different join types with results returned in shaded area Chapter 8 3

SELECT Order. *, Customer. *, Product. * FROM Order JOIN Customer ON Order. c_id=Customer.

SELECT Order. *, Customer. *, Product. * FROM Order JOIN Customer ON Order. c_id=Customer. id JOIN Product ON Order. p_id=Product. id Equi-join的結果 最原始, 由等號連結 Natural join的結果 其中必有部份欄位之值 完全相同 (Join條件) 將之剔除不顯示 Chapter 8 4

SELECT Emp. *, Dept. * FROM Emp JOIN Dept ON Emp. dep_no=Dept. no ←注意這筆

SELECT Emp. *, Dept. * FROM Emp JOIN Dept ON Emp. dep_no=Dept. no ←注意這筆 Equi-join的結果 最原始, 由等號連結 Left outer join的結果 Left : 以左邊為主 Outer : 不管是否有關聯到, 均列出 Chapter 8 SELECT Emp. *, Dept. * FROM Emp LEFT OUTER JOIN Dept ON Emp. dep_no=Dept. no 5

SELECT Emp. *, Dept. * FROM Emp JOIN Dept ON Emp. dep_no=Dept. no ←注意這筆

SELECT Emp. *, Dept. * FROM Emp JOIN Dept ON Emp. dep_no=Dept. no ←注意這筆 Left inner join的結果 Left : 以左邊為主 Inner : 有關聯到的才列出 →結果又等同Equi-join SELECT Emp. *, Dept. * FROM Emp LEFT INNER JOIN Dept ON Emp. dep_no=Dept. no 預設就是inner 很少特別指定 Chapter 8 6

SELECT * FROM Customer_TPE SELECT * FROM Customer_HKG Union-join的結果 垂直合併 SELECT * FROM Customer_TPE

SELECT * FROM Customer_TPE SELECT * FROM Customer_HKG Union-join的結果 垂直合併 SELECT * FROM Customer_TPE UNION SELECT * FROM Customer_HKG Chapter 8 兩張表格必需聯集相容 Union Compatible →兩張表格有相同之欄位, 且相對應之欄位有相同值域 合併後的結果必需符合表格特徵 →任兩筆完全相同紀錄的會被合併 7

Figure 8 -1 Pine Valley Furniture Company Customer and Order tables with pointers from

Figure 8 -1 Pine Valley Furniture Company Customer and Order tables with pointers from customers to their orders (how Join works) 有10筆訂單 Chapter 8 有15個客戶 8

Natural Join Example n For each customer who placed an order, what is the

Natural Join Example n For each customer who placed an order, what is the customer’s name and order number? Join involves multiple tables in FROM clause SELECT CUSTOMER_T. CUSTOMER_ID, CUSTOMER_NAME, ORDER_ID FROM CUSTOMER_T NATURAL JOIN ORDER_T ON CUSTOMER_T. CUSTOMER_ID = ORDER_T. CUSTOMER_ID; ON clause performs the equality check for common columns of the two tables Chapter 8 Note: from Fig. 1, you see that only 10 Customers have links with orders Only 10 rows will be returned from this INNER join 9

Outer Join Example n List the customer name, ID number, and order number for

Outer Join Example n List the customer name, ID number, and order number for all customers. Include customer information even for customers that do have an order SELECT CUSTOMER_T. CUSTOMER_ID, CUSTOMER_NAME, ORDER_ID FROM CUSTOMER_T LEFT OUTER JOIN ORDER_T ON CUSTOMER_T. CUSTOMER_ID = ORDER_T. CUSTOMER_ID; LEFT OUTER JOIN syntax with ON causes customer data to appear even if there is no corresponding order data 會回傳 15筆 Chapter 8 10

Unlike INNER join, this will include customer rows with no matching order rows Results

Unlike INNER join, this will include customer rows with no matching order rows Results Chapter 8 11

Multiple Table Join Example n Assemble all information necessary to create an invoice for

Multiple Table Join Example n Assemble all information necessary to create an invoice for order number 1006 Four tables involved in this join SELECT CUSTOMER_T. CUSTOMER_ID, CUSTOMER_NAME, CUSTOMER_ADDRESS, CITY, SATE, POSTAL_CODE, ORDER_T. ORDER_ID, ORDER_DATE, QUANTITY, PRODUCT_DESCRIPTION, STANDARD_PRICE, (QUANTITY * UNIT_PRICE) FROM CUSTOMER_T, ORDER_LINE_T, PRODUCT_T WHERE CUSTOMER_T. CUSTOMER_ID = ORDER_LINE. CUSTOMER_ID AND ORDER_T. ORDER_ID = ORDER_LINE_T. ORDER_ID AND ORDER_LINE_T. PRODUCT_ID = PRODUCT_ID AND ORDER_T. ORDER_ID = 1006; Chapter 8 Each pair of tables requires an equality-check condition in the WHERE clause, matching primary keys against foreign keys 12

Multiple Table Join Example SELECT CUSTOMER_T. CUSTOMER_ID, CUSTOMER_NAME, CUSTOMER_ADDRESS, CITY, SATE, POSTAL_CODE, ORDER_T. ORDER_ID,

Multiple Table Join Example SELECT CUSTOMER_T. CUSTOMER_ID, CUSTOMER_NAME, CUSTOMER_ADDRESS, CITY, SATE, POSTAL_CODE, ORDER_T. ORDER_ID, ORDER_DATE, QUANTITY, PRODUCT_DESCRIPTION, STANDARD_PRICE, (QUANTITY * UNIT_PRICE) FROM CUSTOMER_T, ORDER_LINE_T, PRODUCT_T WHERE CUSTOMER_T. CUSTOMER_ID = ORDER_LINE. CUSTOMER_ID AND ORDER_T. ORDER_ID = ORDER_LINE_T. ORDER_ID AND ORDER_LINE_T. PRODUCT_ID = PRODUCT_ID AND ORDER_T. ORDER_ID = 1006; SELECT … 改用JOIN寫有同樣效果 FROM CUSTOMER_T AS C JOIN ORDER_LINE_T AS L ON C. CUSTOMER_ID = L. CUSTOMER_ID JOIN ORDER_T AS O ON O. ORDER_ID = L. ORDER_ID JOIN PRODUCT_T AS P ON L. PRODUCT_ID = P. PRODUCT_ID WHERE ORDER_T. ORDER_ID = 1006; Chapter 8 13

Figure 8 -4 Results from a four-table join From CUSTOMER_T table From ORDER_T table

Figure 8 -4 Results from a four-table join From CUSTOMER_T table From ORDER_T table Chapter 8 From PRODUCT_T table 14

Self-Join Example The same table is used on both sides of the join; distinguished

Self-Join Example The same table is used on both sides of the join; distinguished using table aliases Self-joins are usually used on tables with unary relationships. Chapter 8 15

Figure Example of a self-join Chapter 8 16

Figure Example of a self-join Chapter 8 16

Processing Multiple Tables Using Subqueries n Subquery 因為查詢的結果還是表格,因此可對結果再查詢 n n Options: n n placing

Processing Multiple Tables Using Subqueries n Subquery 因為查詢的結果還是表格,因此可對結果再查詢 n n Options: n n placing an inner query (SELECT statement) inside In a condition of the WHERE clause As a “table” of the FROM clause In the HAVING clause Subqueries can be: n n Noncorrelated–executed once for the entire outer query Correlated–executed once for each row returned by the outer query 每行資料都得執行一次子查詢 Chapter 8 17

Subquery Example n Show all customers who have placed an order The IN operator

Subquery Example n Show all customers who have placed an order The IN operator will test to see if the CUSTOMER_ID value of a row is included in the list returned from the subquery SELECT CUSTOMER_NAME FROM CUSTOMER_T WHERE CUSTOMER_ID IN (SELECT DISTINCT CUSTOMER_ID FROM ORDER_T); Subquery is embedded in parentheses. In this case it returns a list that will be used in the WHERE clause of the outer query Chapter 8 18

Join vs. Subquery n Some queries could be accomplished by either a join or

Join vs. Subquery n Some queries could be accomplished by either a join or a subquery Join version Subquery version Chapter 8 19

Figure Graphical depiction of two ways to answer a query with different types of

Figure Graphical depiction of two ways to answer a query with different types of joins Chapter 8 20

Figure Graphical depiction of two ways to answer a query with different types of

Figure Graphical depiction of two ways to answer a query with different types of joins Chapter 8 21

Correlated vs. Noncorrelated Subqueries n Noncorrelated subqueries: Do not depend on data from the

Correlated vs. Noncorrelated Subqueries n Noncorrelated subqueries: Do not depend on data from the outer query n Execute once for the entire outer query n n Correlated subqueries: Make use of data from the outer query n Execute once for each row of the outer query n n Chapter 8 Can use with EXISTS operator 可搭配使用 22

Figure 8 -6 a Processing a noncorrelated subquery No reference to data in outer

Figure 8 -6 a Processing a noncorrelated subquery No reference to data in outer query, so subquery executes once only Chapter 8 These are the only customers that have IDs in the ORDER_T table 23

Correlated Subquery Example n Show all orders that include furniture finished in natural ash

Correlated Subquery Example n Show all orders that include furniture finished in natural ash 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 Chapter 8 The subquery is testing for a value that comes from the outer query 24

Figure 8 -6 b Processing a correlated subquery Subquery refers to outer-query data, so

Figure 8 -6 b Processing a correlated subquery Subquery refers to outer-query data, so executes once for each row of outer query (需花 較多執行時間) Chapter 8 25

Another Subquery Example n Show all products whose standard price is higher than the

Another Subquery Example n Show all products whose standard price is higher than the average price SELECT PRODUCT_DESCRIPTION, STANDARD_PRICE FROM PRODUCT_T WHERE STANDARD_PRICE > (SELECT AVG(STANDARD_PRICE) AVGPRICE FROM PRODUCT_T) Chapter 8 26

Union Queries n Combine the output (union of multiple queries) together into a single

Union Queries n Combine the output (union of multiple queries) together into a single result table First query Combine Second query Chapter 8 27

Tips for Developing Queries n n n n Be familiar with the data model

Tips for Developing Queries n n n n 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 for each link 知道去哪查表 Fine tune with GROUP BY and HAING clauses if needed Chapter 8 28

Guidelines for Better Query Design n n n n Write simple queries 越簡單越好 Break

Guidelines for Better Query Design n n n n Write simple queries 越簡單越好 Break complex queries into multiple simple parts 把複雜查詢做拆解 If possible, avoid subquery and self-joins Create temporary tables for groups of queries Retrieve only the data you need i. e. 不取多餘的 欄位或資料 Consider the total query processing time Don’t have the DBMS sort without an index Learn and practice 對複雜查詢多試不同的寫法 Chapter 8 29

Ensuring Transaction Integrity n Transaction = A discrete unit of work that must be

Ensuring Transaction Integrity n Transaction = A discrete unit of work that must be completely processed or not processed at all 確保動作完成不被中斷分割 n n n May involve multiple updates If any update fails, then all other updates must be cancelled SQL commands for transactions n BEGIN TRANSACTION/END TRANSACTION n n COMMIT n n Marks boundaries of a transaction Makes all updates permanent ROLLBACK n Chapter 8 Cancels updates since the last COMMIT 30

Figure 8 -9 An SQL Transaction sequence (in pseudocode) Chapter 8 31

Figure 8 -9 An SQL Transaction sequence (in pseudocode) Chapter 8 31

Routines and Triggers n Routines Program modules that execute on demand n Include Functions

Routines and Triggers n Routines Program modules that execute on demand n Include Functions and Procedures Ex. 預先寫好的常用SQL指令 n n Triggers n Routines that execute in response to a database event (INSERT, UPDATE, or DELETE) Ex. 當INSERT至ORDER表格時,自動也 INSERT至ORDER_LOG表格 Chapter 8 32

Figure 8 -10 Triggers contrasted with stored procedures Procedures are called explicitly Source: adapted

Figure 8 -10 Triggers contrasted with stored procedures Procedures are called explicitly Source: adapted from Mullins, 1995. Chapter 8 Triggers are event-driven 33

Figure 8 -11 Simplified trigger syntax, SQL: 2008 Figure 8 -12 Create routine syntax,

Figure 8 -11 Simplified trigger syntax, SQL: 2008 Figure 8 -12 Create routine syntax, SQL: 2008 Chapter 8 34

Conditional Expressions Using Case Syntax This is available with newer versions of SQL, previously

Conditional Expressions Using Case Syntax This is available with newer versions of SQL, previously not part of the standard Chapter 8 35

Embedded and Dynamic SQL n Embedded SQL n n Including SQL statements in a

Embedded and Dynamic SQL n Embedded SQL n n Including SQL statements in a program 將SQL指令放在C或Java程式內一起使用 Dynamic SQL n use program to generate SQL code on the fly 於程式內即時產生所需的SQL指令 n Ex. 輸入客戶名稱檢查是否存在 SELECT count(*) FROM CUSTOMER WHERE NAME=$var_customer_name Chapter 8 36