Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 480: Database Systems Lecture 17 February 22, 2013.

Similar presentations


Presentation on theme: "CS 480: Database Systems Lecture 17 February 22, 2013."— Presentation transcript:

1 CS 480: Database Systems Lecture 17 February 22, 2013

2 QBE Recap: Queries on One Relation
Find the loan numbers of all loans made jointly to Smith and Jones. Find all customers who live in the same city as Jones.

3 QBE Recap: Queries on Several Relations
Find the names of all customers who have a loan from the Perryridge branch.

4 QBE Recap: The Condition Box
Allows the expression of constraints on domain variables that are either inconvenient or impossible to express within the skeleton tables. Complex conditions can be used in condition boxes Example: Find all branches that have assets greater than those of at least one branch located in Brooklyn.

5 QBE Recap: From QBE to DRC
Generally, QBE queries match existential DRC queries. Find all customers who live in the same city as Jones. { < x > |  n,s1,s2,y ( < x,s1,y >  customer  < n,s2,y >  customer  n = ‘Jones’ ) }

6 From DRC to QBE Consider the following generic existential DRC query:

7 From DRC to QBE Consider the following generic existential DRC query:
Each of the z’s is either an xi or a yi. Each θi is a comparison operator. Each vi or wi is either a constant, an xi or a yi.

8 From DRC to QBE 1. For each expression: < zi1,zi2,…,zik >  ri
Add a line with each of those variable in the ri skeleton table 2. For each comparison expression: vi θi wi Add that condition in the condition box.

9 From DRC to QBE 1. For each expression: < zi1,zi2,…,zik >  ri
Add a line with each of those variable in the ri skeleton table 2. For each comparison expression: vi θi wi Add that condition in the condition box. Condition box is not always necessary, this procedure is just a generic way of translating DRC to QBE.

10 Informal Procedure for Answering QBE Queries
Let Q be a QBE query (rows in skeleton tables) Create a tuple-variable for each row in Q. For each consistent substitution of each variable by a tuple, print the columns having a P. A substitution is consistent if: Values assigned to the same variable are equal. The constants in Q appear in the substitution.

11 The Result Relation So far, we’ve printed results from only one relation. We can use a special skeleton table called result, with attributes from separate relations. Example: Find the customer_name, account_number, and balance for all customers who have an account at the Perryridge branch. DEPOSITOR(customer_name,account_number) ACCOUNT(account_number,branch_name,balance) We need to: Join depositor and account. Project customer_name, account_number and balance. To accomplish this we: Create a skeleton table, called result, with attributes customer_name, account_number, and balance. Write the query.

12 The Result Relation Example: Find the customer_name, account_number, and balance for all customers who have an account at the Perryridge branch.

13 Aggregate Operations The aggregate operators are AVG, MAX, MIN, SUM, and CNT The above operators must be postfixed with “ALL” (e.g., SUM.ALL. or AVG.ALL._x) to ensure that duplicates are not eliminated. Example: Find the total balance of all the accounts maintained at the Perryridge branch.

14 Aggregate Operations UNQ is used to specify that we want to eliminate duplicates. Find the total number of customers having an account at the bank.

15 Query Examples (GROUP BY)
Find the average balance at each branch.

16 Query Examples (GROUP BY)
Find the average balance at each branch. The “G” in “P.G” is analogous to SQL’s group by construct. The “ALL” in the “P.AVG.ALL” entry in the balance column ensures that all balances are considered.

17 Query Examples (GROUP BY)
Find the average balance at each branch. The “G” in “P.G” is analogous to SQL’s group by construct. The “ALL” in the “P.AVG.ALL” entry in the balance column ensures that all balances are considered. To find the average account balance at only those branches where the average account balance is more than $1,200, we simply add the condition box:

18 Query Example Answering queries that have a “for all” in the calculus expression can become complicated in QBE. DEPOSITOR(customer_name, account_number) ACCOUNT(account_number, branch_name, balance) BRANCH(branch_name, branch_city, assets) Query: Find all customers who have an account at all branches located in Brooklyn. Approach: for each customer, find the number of branches in Brooklyn at which they have accounts, and compare with total number of branches in Brooklyn. QBE does not provide subquery functionality, so both above tasks have to be combined in a single query. Can be done for this query, but there are queries that require subqueries and cannot always be expressed in QBE.

19 Query Example Query: Find all customers who have
an account at all branches located in Brooklyn. CNT.UNQ.ALL._w specifies the number of distinct branches in Brooklyn. Note: The variable _w is not connected to other variables in the query. CNT.UNQ.ALL._z specifies the number of distinct branches in Brooklyn at which customer x has an account.

20 Database Modification – Deletion
Deletion of tuples from a relation is expressed by use of a D. command. In the case where we delete information in only some of the columns, null values, specified by –, are inserted. Delete customer Smith Delete the branch_city value of the branch whose name is “Perryridge”.

21 Deletion Delete all loans with a loan amount greater than $1300 and less than $1500. For consistency, we have to delete information from loan and borrower tables

22 Deletion Delete all accounts at branches located in Brooklyn.
If you delete the account, then one should delete the depositor information for consitency.

23 Insertion Insertion is done by placing the I. operator in the query expression. Insert the fact that account A-9732 at the Perryridge branch has a balance of $700.

24 Insertion Provide as a gift for all loan customers of the Perryridge branch, a new $200 savings account for every loan account they have, with the loan number serving as the account number for the new savings account.

25 Updates Use the U. operator to change a value in a tuple without changing all values in the tuple. QBE does not allow users to update the primary key fields. Update the asset value of the Perryridge branch to $10,000,000. Increase all balances by 5 percent.

26 Beer Drinkers Example #1
Retrieve the bars that serve a beer that ‘John Doe’ likes.

27 Beer Drinkers Example #1
Retrieve the bars that serve a beer that ‘John Doe’ likes. LIKES d_name be_name { < a > |  d, e ( < a,e >  serves  < d,e >  likes  d = ‘John Doe’ ) }

28 Beer Drinkers Example #1
Retrieve the bars that serve a beer that ‘John Doe’ likes. LIKES d_name be_name SERVES ba_name be_name { < a > |  d, e ( < a,e >  serves  < d,e >  likes  d = ‘John Doe’ ) }

29 Beer Drinkers Example #1
Retrieve the bars that serve a beer that ‘John Doe’ likes. LIKES d_name be_name John Doe SERVES ba_name be_name { < a > |  d, e ( < a,e >  serves  < d,e >  likes  d = ‘John Doe’ ) }

30 Beer Drinkers Example #1
Retrieve the bars that serve a beer that ‘John Doe’ likes. LIKES d_name be_name John Doe _e SERVES ba_name be_name { < a > |  d, e ( < a,e >  serves  < d,e >  likes  d = ‘John Doe’ ) }

31 Beer Drinkers Example #1
Retrieve the bars that serve a beer that ‘John Doe’ likes. LIKES d_name be_name John Doe _e SERVES ba_name be_name _e { < a > |  d, e ( < a,e >  serves  < d,e >  likes  d = ‘John Doe’ ) }

32 Beer Drinkers Example #1
Retrieve the bars that serve a beer that ‘John Doe’ likes. LIKES d_name be_name John Doe _e SERVES ba_name be_name P. _e { < a > |  d, e ( < a,e >  serves  < d,e >  likes  d = ‘John Doe’ ) }

33 Beer Drinkers Example #2
Retrieve the bars that serve ‘Bud’ and ‘Miller’. { < a > |  e1,e2 ( < a,e1 >  serves  < a,e2 >  serves  e1 = ‘Bud’  e2 = ‘Miller’ ) }

34 Beer Drinkers Example #2
Retrieve the bars that serve ‘Bud’ and ‘Miller’. SERVES ba_name be_name { < a > |  e1,e2 ( < a,e1 >  serves  < a,e2 >  serves  e1 = ‘Bud’  e2 = ‘Miller’ ) }

35 Beer Drinkers Example #2
Retrieve the bars that serve ‘Bud’ and ‘Miller’. SERVES ba_name be_name Bud Miller { < a > |  e1,e2 ( < a,e1 >  serves  < a,e2 >  serves  e1 = ‘Bud’  e2 = ‘Miller’ ) }

36 Beer Drinkers Example #2
Retrieve the bars that serve ‘Bud’ and ‘Miller’. SERVES ba_name be_name _a Bud Miller { < a > |  e1,e2 ( < a,e1 >  serves  < a,e2 >  serves  e1 = ‘Bud’  e2 = ‘Miller’ ) }

37 Beer Drinkers Example #2
Retrieve the bars that serve ‘Bud’ and ‘Miller’. SERVES ba_name be_name P._a Bud _a Miller { < a > |  e1,e2 ( < a,e1 >  serves  < a,e2 >  serves  e1 = ‘Bud’  e2 = ‘Miller’ ) }

38 Beer Drinkers Example #3
Retrieve the bars that serve ‘Bud’ or ‘Miller’. { < a > |  e ((e = ‘Bud’  e = ‘Miller’)  < a,e >  serves) }

39 Beer Drinkers Example #3
Retrieve the bars that serve ‘Bud’ or ‘Miller’. SERVES ba_name be_name { < a > |  e ((e = ‘Bud’  e = ‘Miller’)  < a,e >  serves) }

40 Beer Drinkers Example #3
Retrieve the bars that serve ‘Bud’ or ‘Miller’. SERVES ba_name be_name Bud Miller { < a > |  e ((e = ‘Bud’  e = ‘Miller’)  < a,e >  serves) }

41 Beer Drinkers Example #3
Retrieve the bars that serve ‘Bud’ or ‘Miller’. SERVES ba_name be_name _a1 Bud _a2 Miller { < a > |  e ((e = ‘Bud’  e = ‘Miller’)  < a,e >  serves) }

42 Beer Drinkers Example #3
Retrieve the bars that serve ‘Bud’ or ‘Miller’. SERVES ba_name be_name P._a1 Bud P._a2 Miller { < a > |  e ((e = ‘Bud’  e = ‘Miller’)  < a,e >  serves) }

43 Beer Drinkers Example #4
Retrieve the drinkers that frequent some bar that serves a beer they like. { < d > |  a, e ( < a,e >  serves  < d,e >  likes  < d,a >  frequents ) }

44 Beer Drinkers Example #4
Retrieve the drinkers that frequent some bar that serves a beer they like. LIKES d_name be_name SERVES ba_name be_name FREQUENTS d_name ba_name { < d > |  a, e ( < a,e >  serves  < d,e >  likes  < d,a >  frequents ) }

45 Beer Drinkers Example #4
Retrieve the drinkers that frequent some bar that serves a beer they like. LIKES d_name be_name _d _e SERVES ba_name be_name _a _e FREQUENTS d_name ba_name _d _a { < d > |  a, e ( < a,e >  serves  < d,e >  likes  < d,a >  frequents ) }

46 Beer Drinkers Example #4
Retrieve the drinkers that frequent some bar that serves a beer they like. LIKES d_name be_name P._d _e SERVES ba_name be_name _a _e FREQUENTS d_name ba_name _d _a { < d > |  a, e ( < a,e >  serves  < d,e >  likes  < d,a >  frequents ) }


Download ppt "CS 480: Database Systems Lecture 17 February 22, 2013."

Similar presentations


Ads by Google