Presentation is loading. Please wait.

Presentation is loading. Please wait.

Query-by-Example (QBE) Presented by: Wong, Lok Man CS 157A Dr. Sin Min Lee.

Similar presentations


Presentation on theme: "Query-by-Example (QBE) Presented by: Wong, Lok Man CS 157A Dr. Sin Min Lee."— Presentation transcript:

1 Query-by-Example (QBE) Presented by: Wong, Lok Man CS 157A Dr. Sin Min Lee

2 History of QBE History: QBE is the name of both a data-manipulation language and an early database system that included this language QBE was developed at IBM’s T.J. Watson Research Center (1970’s)

3 Content QBE Basic Structure Queries on One Relation Queries on Several Relations Condition Box Result Relation Ordering of the Display of Tuples Aggregate Operations Modification of Database QBE in Microsoft Access

4 Basic Structure QBE has two dimensional syntax: 1. Queries look like tables 2. QBE queries are expressed “by example” Non-procedural Queries are expressed using skeleton tables branchBranch-nameBranch-cityassets

5 Basic Structure User selects skeletons needed User fills in the skeletons with example row Example row consists of constants & example elements, which are domain variables Constants appear without any qualification QBE uses an underscore character “_” before domain variables, as in “_x”

6 Queries on One Relation Ex: Find all loan numbers at the Perryridge branch Bring up the skeleton of the loan relation P. – before the variable to display the value X – a variable Perryridge – branch-name Loanloan-numberbranch-nameamount P._xPerryridge

7 Queries on One Relation (cont.) Ex: Suppress duplicate elimination - insert “ALL” Ex: display entire loan relation – place single P. loanloan-numberbranch-nameamount P.ALLPerryridge loanloan-numberbranch-nameamount P.

8 Queries on One Relation (cont.) Ex: Find loan numbers of all loans with amount more than $700. Use arithmetic operations (=,, ¬) With no variable, displays all attributes Ex: Find names of all branches not in Brooklyn loanloan-numberbranch-nameamount P.>700 branchbranch-namebranch-cityassets P.¬Brooklyn

9 Queries on One Relation (cont.) Ex: Find the loan numbers of all loans made jointly to Smith and Jones. Only one P. is needed. Ex: Find all customers who live in the same city as Jones customercustomer-namecustomer-streetcustomer-city P._x_y Jones_y borrowerCustomer-nameloan-number “Smith”P._x “Jones”_x

10 Queries on Several Relations QBE allows queries to span several different relations The connection among the various relations are achieved through variables that force certain tuples to have the same value on certain attributes

11 Queries on Several Relation (cont.) Ex: Find the names of all the customers that have a loan from the Perryridge branch loanloan-numberbranch-nameamount _XPerryridge borrowercustomer-nameloan-number P._y_X

12 Queries on Several Relation (Cont.) Ex: Find the names of all customer with both and an account and a loan at the bank The “¬” symbol represents “there does not exist”. A “¬” under an attribute name stands for “not equal”. depositorcustomer-nameaccount-number P._x borrowercustomer-nameloan-number ¬ _x

13 Queries on Several Relation (Cont.) Ex: Display all customer-name values that appear in at least two tuples, with the second tuple having an account-number different from the first. depositorcustomer-nameaccount-number P._x_y _x ¬ _y

14 Condition Box QBE includes a condition box feature that allow the expression of general constraints over any of the domain variables. Logical operators in words and and or, or as symbols “&” and “|”, appear in a condition box Ex: Find the loan numbers of all loans made to Smith, to Jones, or both borrowercustomer-nameLoan-number _nP._x condition _n = Smith or _n = Jones

15 Condition Box (Cont.) Ex: Find all branches that have assets greater than those of at least one branch located in Brooklyn branchbranch-namebranch-cityassets P._x_y Brooklyn_z conditions _y > _z

16 Condition Box (Cont.) Ex: Find all account numbers of account with a balance between $1000 and $1500, but not exactly $1255 accountaccount-numberbranch-namebalance P._y_x conditions _x = ( > 999 and < 1501 and ¬ 1255)

17 Result Relation If the result of a query includes attributes from several relation schemas, we can declare a temporary result relation that includes all the attributes of the result. Ex: Create a skeleton table, called result, with attributes customer-name, account-number, and balance, at the Perryridge branch. (Two Steps) 1. accountaccount-numberbranch-namebalance _yPerryridge_z depositorcustomer-nameaccount-number _x_y

18 Result Relation (Cont.) Once the new skeleton table is created, write the query. 2. resultcustomer- name account- number balance resultcustomer- name account- number balance P._x_y_z

19 Ordering of the Display Tuples To gain control over the order in which tuples in a relation are displayed, indicate AO (ascending order) or Do (descending order). To specify the order in which the sorting should be carried, add an integer surrounded by parentheses after sort operator (AO(1) or DO(2)).

20 Ordering of the Display Tuples (Cont.) Ex: List all account numbers at the Downtown branch in ascending alphabetic order with their respective account balances in descending order. accountaccount-numberbranch-namebalance P.AO(1)DowntownP.DO(2)

21 Aggregate Operations Aggregate Operators include AVG, MIN, MAX, SUM, and CNT We must postfix these operators with “ALL.” to create a multiset on which the aggregate operation is evaluated Operator “UNQ.” is to specify that duplicates are to be eliminated To compute functions on groups of tuples, then use “G.”.

22 Aggregate Operations (Cont.) Ex: Find the total balance of all the accounts maintained at the Downtown branch Ex: Find the total number of customers who have an account at the bank accountaccount-numberbranch-namebalance DowntownP.SUM.ALL. depositorcustomer-nameaccount-number P.CNT.UNQ.

23 Aggregate Operations (Cont.) Ex: Find the average balance at each branch accountaccount-numberbranch-namebalance P.G.P.AVG.ALL._x

24 Ex: Find all customers who have accounts at each of the branches located in Brooklyn depositorcustomer-nameaccount-number P.G._X_y accountaccount-numberbranch-namebalance _y_z branchbranch-namebranch-cityassets _zBrooklyn _wBrooklyn conditions CNT.UNQ._z = CNT.UNQ._w

25 Modification of the Database Deletion, uses “D.” replacing “P.” Insertion, uses “I.” replacing “P.” Updates, uses “U.” replacing “P.”

26 Deletion Ex: Delete customer Roy Ex: Delete the branch-city value of the branch whose name is “San Jose” depositorcustomer-nameaccount-number D.Roy branchbranch-namebranch-cityassets San JoseD.

27 Deletion (Cont.) Ex: Delete all accounts at all branches located in Brooklyn depositorcustomer-nameaccount-number D._y branchbranch-namebranch-cityassets _xBrooklyn accountaccount-numberbranch-namebalance D._y_x

28 Insertion Ex: Insert the fact that account AM-8976 at the Downtown branch has a balance of $900. Ex: Insert information into the branch relation about a new branch with name “Capital”, city “Queens,” and asset “200,000” accountaccount-numberbranch-namebalance I.AM-8976Downtown900 branchbranch-namebranch-cityassets I.CapitalQueens200,000

29 Insertion (Cont.) Ex: Insert as a gift, for all loan customers of the Downtown branch, a new $300 savings account for every loan account that they have, with the loan number serving as the account number for the savings account.

30 Insertion (Cont.) accountaccount-numberbranch-namebalance I._xDowntown300 depositorcustomer-nameaccount-number I._y_x loanloan-numberbranch-nameamount _xDowntown borrowercustomer-nameLoan-number _y_x

31 Updates Ex: Update the asset value of the San Jose branch to $100,000,000. Ex: Update all balances with a 5% increase. Use “_x” to hold the old balances branchbranch-namebranch-cityassets San JoseU.100,000,000 accountaccount-numberbranch-namebalance U._x * 1.05

32 QBE in Microsoft Access Original QBE was designed for a text-based display environment Access QBE is a graphical display environment, called graphical query-by-example (GQBE) GQBE version attributes are written one below the other, instead of horizontally Instead of shared variable in QBE, GQBE uses lines to link attributes of two tables

33 QBE in Microsoft Access (Cont.) In GQBE links are automatically created, on the basis of the attribute name In GQBE attributes printed in a separate box is specified, called the design grid, instead of using “P.” for QBE.

34 THE END!!! THANKS FOR STAYING AWAKE! QUIZ TIME!!!


Download ppt "Query-by-Example (QBE) Presented by: Wong, Lok Man CS 157A Dr. Sin Min Lee."

Similar presentations


Ads by Google