Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Open Database Connectivity wikipedia , lookup
Entity–attribute–value model wikipedia , lookup
Microsoft SQL Server wikipedia , lookup
Microsoft Jet Database Engine wikipedia , lookup
Serializability wikipedia , lookup
Clusterpoint wikipedia , lookup
Extensible Storage Engine wikipedia , lookup
Concurrency control wikipedia , lookup
Object-relational impedance mismatch wikipedia , lookup
Database model wikipedia , lookup
Relational model wikipedia , lookup
Chapter 8: Advanced SQL 註 : 於11版為Chapter 7 楊立偉教授 台灣大學工管系 2015 Fall 1 Processing Multiple Tables–Joins Join–a relational operation that causes two or more tables with a common domain to be combined into a single table or view Equi-join–a join in which the joining condition is based on Natural join–an equi-join in which one of the duplicate columns Outer join–a join in which rows that do not have matching 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 Chapter 8 3 SELECT Order.*, Customer.*, Product.* FROM Order JOIN Customer ON Order.c_id=Customer.id JOIN Product ON Order.p_id=Product.id Customer id Name Gender 1 張三 男 2 李四 女 Product id Name 1 電腦 2 相機 Order c_id p_id date 1 2 20090910 2 1 20091015 c_id p_id date id Name 1 2 20090910 1 張三 2 1 20091015 2 李四 Gender 男 女 id Name 2 相機 1 電腦 Equi-join的結果 c_id p_id date id Name 1 2 20090910 1 張三 2 1 20091015 2 李四 X Gender 男 女 id Name 2 相機 1 電腦 X Natural join的結果 Chapter 8 最原始, 由等號連結 其中必有部份欄位之值 完全相同 (Join條件) 將之剔除不顯示 4 Emp no 1 2 3 4 5 Dept no name mgr_no 1 會計部 3 2 工程部 4 name dept_no 張三 1 李四 2 王五 1 毛六 2 陳七 3 no 1 2 3 4 name dept_no no name mgr_no Equi-join的結果 張三 1 1 會計部 3 最原始, 由等號連結 李四 2 2 工程部 4 王五 1 1 會計部 3 毛六 2 2 工程部 4 SELECT Emp.*, Dept.* FROM Emp JOIN Dept ON Emp.dep_no=Dept.no ←注意這筆 no name dept_no no 1 張三 1 1 2 李四 2 2 3 王五 1 1 4 毛六 2 2 5 陳七 3 null Chapter 8 name mgr_no 會計部 3 工程部 4 會計部 3 工程部 4 null null Left outer join的結果 Left : 以左邊為主 Outer : 不管是否有關聯到, 均列出 SELECT Emp.*, Dept.* FROM Emp LEFT OUTER JOIN Dept ON Emp.dep_no=Dept.no 5 Emp no 1 2 3 4 5 Dept no name mgr_no 1 會計部 3 2 工程部 4 name dept_no 張三 1 李四 2 王五 1 毛六 2 陳七 3 no 1 2 3 4 name dept_no no name mgr_no 張三 1 1 會計部 3 李四 2 2 工程部 4 王五 1 1 會計部 3 毛六 2 2 工程部 4 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 6 ON Emp.dep_no=Dept.no 預設就是inner 很少特別指定 Chapter 8 SELECT * FROM Customer_TPE SELECT * FROM Customer_HKG Customer_TPE id Name Gender 1 張三 男 2 李四 女 Customer_HKG id Name Gender 3 王五 女 4 毛六 男 id Name Gender 1 張三 男 2 李四 女 3 王五 女 4 毛六 男 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 customers to their orders (how Join works) 有10筆訂單 Chapter 8 有15個客戶 8 Natural Join Example 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 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 Chapter 8 11 Multiple Table Join Example 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_T, ORDER_LINE_T, PRODUCT_T WHERE CUSTOMER_T.CUSTOMER_ID = ORDER_T.CUSTOMER_ID AND ORDER_T.ORDER_ID = ORDER_LINE_T.ORDER_ID AND ORDER_LINE_T.PRODUCT_ID = PRODUCT.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, ORDER_DATE, QUANTITY, PRODUCT_DESCRIPTION, STANDARD_PRICE, (QUANTITY * UNIT_PRICE) FROM CUSTOMER_T, ORDER_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.PRODUCT_ID AND ORDER_T.ORDER_ID = 1006; SELECT … 改用JOIN寫有同樣效果 FROM CUSTOMER_T AS C JOIN ORDER_T AS O ON C.CUSTOMER_ID = O.CUSTOMER_ID JOIN ORDER_LINE_T AS L 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 Chapter 8 From PRODUCT_T table 14 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 Processing Multiple Tables Using Subqueries Subquery 因為查詢的結果還是表格,因此可對結果再查詢 Options: 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: Noncorrelated–executed once for the entire outer query Correlated–executed once for each row returned by the outer query 每行資料都得執行一次子查詢 Chapter 8 17 Subquery Example 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 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 joins Chapter 8 20 Figure Graphical depiction of two ways to answer a query with different types of joins Chapter 8 21 Correlated vs. Noncorrelated Subqueries Noncorrelated subqueries: Do not depend on data from the outer query Execute once for the entire outer query Correlated subqueries: Make use of data from the outer query Execute once for each row of the outer query Chapter 8 Can use with EXISTS operator 可搭配使用 22 Figure 8-6a 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 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-6b 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 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 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 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 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 Routines and Triggers Routines Program modules that execute on demand Include Functions and Procedures Ex. 預先寫好的常用SQL指令 Triggers Routines that execute in response to a database event (INSERT, UPDATE, or DELETE) Ex. 當INSERT至ORDER表格時,自動也 INSERT至ORDER_LOG表格 Chapter 8 30 Figure 8-10 Triggers contrasted with stored procedures Procedures are called explicitly Source: adapted from Mullins, 1995. Chapter 8 Triggers are event-driven 31 Figure 8-11 Simplified trigger syntax, SQL:2008 Figure 8-12 Create routine syntax, SQL:2008 Chapter 8 32 Conditional Expressions Using Case Syntax This is available with newer versions of SQL, previously not part of the standard Chapter 8 33 Embedded and Dynamic SQL Embedded SQL Including SQL statements in a program 將SQL指令放在C或Java程式內一起使用 Dynamic SQL use program to generate SQL code on the fly 於程式內即時產生所需的SQL指令 Ex. 輸入客戶名稱檢查是否存在 SELECT count(*) FROM CUSTOMER WHERE NAME=$var_customer_name Chapter 8 34 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 COMMIT Marks boundaries of a transaction Makes all updates permanent ROLLBACK Chapter 8 Cancels updates since the last COMMIT 35 Figure 8-9 An SQL Transaction sequence (in pseudocode) Chapter 8 36 Figure 11-9 Basic recovery techniques a) Rollback Chapter 11 37 Figure 11-9 Basic recovery techniques (cont.) b) Rollforward Chapter 11 38 Chapter 11 39 Why do we need transaction When multiple users access the database… Query the total balance Transfer $100 from bank A to B SELECT sum(amount) FROM account WHERE id=‘001’ A tentative value will be retrieved Timeline Chapter 8 UPDATE account SET amount=amount-100 WHERE id=‘001’ and bank=‘A’ UPDATE account SET amount=amount+100 WHERE id=‘001’ and bank=‘B’ 40 Transaction ACID properties 4 properties that guarantee that database transactions are processed reliably Atomicity 不可分割性 Consistency 一致性 Constraints don't change from before transaction to after transaction. i.e. database integrity remains. Isolation 隔離性 Transaction cannot be subdivided; each transaction be "all or nothing“ Database changes not revealed to users until after transaction has completed; tentative data can't be accessed. Durability 持續性 Chapter 8 Database changes are permanent; once a transaction has been committed, it will remain so, even in the event of power loss, crashes, or errors 41 CONTROL CONCURRENT ACCESS Problem–in a multi-user environment, simultaneous access to data can result in interference and data loss (lost update problem) Solution–Concurrency Control The process of managing simultaneous operations against a database so that data integrity is maintained and the operations do not interfere with each other in a multi-user environment 在網路、分散環境中特別重要且困難 →所以要用DBMS Chapter 11 © 2013 Pearson Education 42 Figure 11-10 Lost update (no concurrency control in effect) Simultaneous access causes updates to cancel each other. A similar problem is the inconsistent read problem. Chapter 11 © 2013 Pearson Education 43 CONCURRENCY CONTROL TECHNIQUES Serializability 循序存取 Finish one transaction before starting another Locking Mechanisms 鎖定 The most common way of achieving serialization Data that is retrieved for the purpose of updating is locked for the updater No other user can perform update until unlocked Chapter 11 © 2013 Pearson Education 44 Figure 11-11: Updates with locking (concurrency control) This prevents the lost update problem Chapter 11 © 2013 Pearson Education 45 LOCKING MECHANISMS Locking level: 鎖定的層級 (鎖越高可用性越低) Database–used during database updates Table–used for bulk updates Block or page–very commonly used Record–only requested row; fairly commonly used Field–requires significant overhead; impractical Types of locks: Shared lock 共享–Read but no update permitted. Used when just reading to prevent another user from placing an exclusive lock on the record Exclusive lock 互斥–No access permitted. Used when preparing to update Chapter 11 © 2013 Pearson Education 46 DEADLOCK An impasse that results when two or more transactions have locked common resources, and each waits for the other to unlock their resources 互相擁有部分對方所需資源的僵局 Figure 11-12 The problem of deadlock John and Marsha will wait forever for each other to release their locked resources! Chapter 11 © 2013 Pearson Education 47 MANAGING DEADLOCK Deadlock prevention: Lock all records required at the beginning of a transaction Two-phase locking protocol 兩階段鎖定法 Growing phase Shrinking phase May be difficult to determine all needed resources in advance Deadlock Resolution: Allow deadlocks to occur Mechanisms for detecting and breaking them Resource usage matrix 犧牲交易後重來 Chapter 11 © 2013 Pearson Education 48 VERSIONING 版本控制 Optimistic approach to concurrency control Instead of locking 樂觀策略, 不用鎖定 Assumption is that simultaneous updates will be infrequent 假設同時更新是較少發生的 Each transaction can attempt an update as it wishes 所以先更新再說; 有衝突時再解決 The system will reject an update when it senses a conflict Use of rollback and commit for this 用 rollback解決衝突 (選擇犧牲者) Chapter 11 © 2013 Pearson Education 49 Figure 11-14 The use of versioning Better performance than locking Chapter 11 © 2013 Pearson Education 50