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

Slides:



Advertisements
Similar presentations
Chapter 5: Other Relational Languages
Advertisements

©Silberschatz, Korth and Sudarshan5.1Database System Concepts Chapter 5: Other Relational Languages Query-by-Example (QBE) Datalog.
Ver 1,12/09/2012Kode :CCs 111,Sistem basis DataFASILKOM Chapter 5: Other Relational Languages Database System Concepts, 5th Ed. ©Silberschatz, Korth and.
1 Query-by-Example (QBE). 2 v A “GUI” for expressing queries. –Based on the Domain Relational Calulus (DRC)! –Actually invented before GUIs. –Very convenient.
E-R Diagram for a Banking Enterprise
Domain Relational Calculus and Query-by-Example CS157a John Eagle.
BRANCH-SCHEME (BRANCH-NAME, ASSETS, BRANCH-CITY)
Chapter 2 Relational Model (part II) Hankz Hankui Zhuo
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Ordering the Display of Tuples List in alphabetic order the names of all customers having.
©Silberschatz, Korth and Sudarshan3.1Database System Concepts Extended Relational-Algebra-Operations Generalized Projection Outer Join Aggregate Functions.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
SQL Structured Query Language Meizhen Huang. Content (4.1 – 4.4) Background Parts of SQL Basic Structure Set Operations Aggregate Functions.
Temple University – CIS Dept. CIS331– Principles of Database Systems V. Megalooikonomou Query by example (based on notes by Silberchatz,Korth, and Sudarshan.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Modification of the Database – Deletion Delete all account records at the Perryridge branch.
SQL Neyha Amar CS 157A, Fall Inserting The insert statement is used to add a row of data into a table Strings should be enclosed in single quotes,
Chapter 5 Other Relational Languages By Cui, Can B.
Slides adapted from A. Silberschatz et al. Database System Concepts, 5th Ed. SQL - part 2 - Database Management Systems I Alex Coman, Winter 2006.
MySQL Tutorial (2) Introduction to Database. Banking Example branch (branch-name, branch-city, assets) customer (customer-name, customer-street, customer-city)
Query-by-Example (QBE) Presented by: Wong, Lok Man CS 157A Dr. Sin Min Lee.
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 5: Other Relational.
CIS552SQL1 Data Definition Language Insertions Basic Query Structure Set Operations Aggregate Functions Null Values Nested Subqueries Derived Relations.
Chapter 3: SQL Data Definition Language Data Definition Language Basic Structure of SQL Basic Structure of SQL Set Operations Set Operations Aggregate.
©Silberschatz, Korth and Sudarshan6.1Database System Concepts Chapter 6: Integrity and Security Domain Constraints Referential Integrity Assertions Triggers.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Null Values It is possible for tuples to have a null value for some of their attributes The.
©Silberschatz, Korth and Sudarshan5.1Database System Concepts Chapter 5: Other Relational Languages Query-by-Example (QBE) Datalog.
Lecture 6 Structured Query Language SQL Lecture 6 Structured Query Language SQL Instructor: Haya Sammaneh.
3.1 Chapter 3: SQL Schema used in examples p (omit 3.8.2, , 3.11)
Basic Structure Given sets A 1, A 2,..., A n a relation r is a subset of A 1 x A 2 x... x A n Thus a relation is a set of n-tuples (a 1, a 2,..., a n )
Chapter 5: Other Relational Languages. 5.2 Chapter 5: Other Relational Languages Tuple Relational Calculus Domain Relational Calculus Query-by-Example.
Structured Query Language 2 Presented by: Annisa, M.Kom. Source: Database System Concepts 5 th edition.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Modification of the Database – Deletion Delete all tuples from the loan relation. delete.
©Silberschatz, Korth and Sudarshan2.1Database System Concepts - 5 th Edition, Oct 5, 2006 Outer Join n An extension of the join operation that avoids loss.
Computing & Information Sciences Kansas State University Monday, 04 Feb 2008CIS 560: Database System Concepts Lecture 6 of 42 Monday, 04 February 2008.
©Silberschatz, Korth and Sudarshan5.1Database System Concepts Chapter 5: Other Relational Languages Query-by-Example (QBE)
Computing & Information Sciences Kansas State University Tuesday, 13 Feb 2007CIS 560: Database System Concepts Lecture 12 of 42 Tuesday, 13 February 2007.
Computing & Information Sciences Kansas State University Monday, 08 Sep 2008CIS 560: Database System Concepts Lecture 5 of 42 Monday, 08 September 2008.
Advanced Database Systems By- Jesmin Akhter Assistant Professor, IIT, Jahangirnagar University.
Chapter 8: SQL. Data Definition Modification of the Database Basic Query Structure Aggregate Functions.
IS 230Lecture 6Slide 1 Lecture 7 Advanced SQL Introduction to Database Systems IS 230 This is the instructor’s notes and student has to read the textbook.
Computing & Information Sciences Kansas State University Monday, 17 Sep 2007CIS 560: Database System Concepts Lecture 12 of 42 Monday, 18 February 2008.
Source: Database System Concepts, Silberschatz etc Edited: Wei-Pang Yang, IM.NDHU, Introduction to Database CHAPTER 5 Other Relational Languages.
Chapter 4: SQL Complex Queries Complex Queries Views Views Modification of the Database Modification of the Database Joined Relations Joined Relations.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Appendix C: Other Relational.
Database System Concepts, 5th Ed. Bin Mu at Tongji University Chapter 5: Other Relational Languages.
©Silberschatz, Korth and Sudarshan3.1Database System Concepts Extended Relational-Algebra-Operations Generalized Projection Aggregate Functions Outer Join.
Computing & Information Sciences Kansas State University Friday, 08 Sep 2006CIS 560: Database System Concepts Lecture 07 of 42 Tuesday, 30 January 2007.
Computing & Information Sciences Kansas State University Wednesday, 03 Sep 2008CIS 560: Database System Concepts Lecture 3 of 42 Wednesday, 03 September.
©Silberschatz, Korth and Sudarshan5.1Database System Concepts Chapter 5: Other Relational Languages Query-by-Example (QBE) Datalog.
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan Chapter 5: Other Relational Languages.
MySQL Tutorial Introduction to Database. Introduction of MySQL  MySQL is an SQL (Structured Query Language) based relational database management system.
Relational Algebra HW2 Turn in as a hardcopy at the start of next class period. You may work this assignment in groups.
SCUHolliday - coen 1787–1 Schedule Today: u Subqueries, Grouping and Aggregation. u Read Sections Next u Modifications, Schemas, Views. u Read.
Nested Subqueries in SQL By Kenneth Cheung CS 157A Section 2 Professor Lee.
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 5: Other Relational.
Query Languages Language in which user requests information from the database. Categories of languages Procedural Non-procedural, or declarative “Pure”
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 6: Formal Relational.
Aljabar Relasi.
CS 480: Database Systems Lecture 16 February 20,2013.
CS 480: Database Systems Lecture 13 February 13,2013.
Relational Model By Dr.S.Sridhar, Ph.D.(JNUD), RACI(Paris, NICE), RMR(USA), RZFM(Germany)
Schedule Today: Next After that Subqueries, Grouping and Aggregation.
Structured Query Language (SQL)
Chapter 2: Relational Model
Lecture 4 of 42 Relational Joins Wednesday, 30 January 2008
Chapter 5: Other Relational Languages
Instructor: Mohamed Eltabakh
Appendix C: Other Relational Languages
Introduction to Database
CS 405G: Introduction to Database Systems
Presentation transcript:

CS 480: Database Systems Lecture 17 February 22, 2013

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.

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

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.

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’ ) }

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

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.

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.

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.

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.

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.

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

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.

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.

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

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.

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:

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.

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.

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”.

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

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

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.

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.

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.

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

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’ ) }

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’ ) }

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’ ) }

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’ ) }

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’ ) }

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’ ) }

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’ ) }

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’ ) }

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’ ) }

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’ ) }

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’ ) }

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

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) }

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) }

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) }

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) }

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 ) }

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 ) }

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 ) }

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 ) }