Presentation is loading. Please wait.

Presentation is loading. Please wait.

Query By Example (QBE) Mallipeddi Venkata Harish Terence Gam Israel.

Similar presentations


Presentation on theme: "Query By Example (QBE) Mallipeddi Venkata Harish Terence Gam Israel."— Presentation transcript:

1 Query By Example (QBE) Mallipeddi Venkata Harish Terence Gam Israel

2 Introduction Query-by-Example (QBE ) is a language for querying (and also for inserting, updating data like SQL) relational data. The user creates a query by creating ‘example tables.’ Hence the name! Basic Idea: The user formulates the query by entering an example of a possible answer in the appropriate place in an empty table.

3 QBE vs. SQL – Difference in Approach
SQL query: “You describe to the DBMS how to get the data you want through the SQL query syntax.” QBE query: “You describe the data itself through example tables.” How to get? (SQL) vs. What to get? (QBE)

4 QBE QBE language involves relatively few concepts.  Hence, a user needs minimal information to get started in QBE. QBE is especially suited for queries that are not too complex and can be expressed in terms of a few tables.

5 QBE – A brief history QBE was originally developed by M. M. Zloof at the IBM Yorktown Heights Research Laboratory. QBE is quite old (created in 1970s). Not used much now. QBE’s influence can be found in DBMS products like Borland Paradox, MS Access, etc. Zloof specifically designed QBE for use with GUI – both the requests and results are specified in QBE by filling in tables on the screen.

6 QBE and DRC Relevance QBE uses domain variables, as in DRC, to create example tables. A large class of QBE queries can be translated to DRC in a direct manner, except of course aggregate queries! Idea: There is a term in the DRC query for each row in the QBE query, and the terms are connected by using ^

7 Basic QBE syntax Variables are prefixed with underscore to distinguish them from constants followed by the variable name. Variable name for a variable that appears only once in a query can be omitted. Constants, including strings, appear unquoted. Exception: String values with blank or special characters need to be quoted.

8 Basic QBE syntax The fields that should appear in the answer are specified by using command P. which stands for print. (analogous to SELECT clause in SQL) Comparison operators : <, <=, >, >=, ¬

9 Relations for this presentation
Sailors (sid: integer, sname: string, rating: integer, age: real) Boats (bid: integer, bname: string, color: string) Reserves (sid: integer, bid: integer, day: date)

10 Queries over single Relation
Print names and ages of all sailors {<N, A> | ∃I, T( <I, N, T, A>∈Sailors)} P._A P._N age rating sname sid Sailors

11 Queries over single Relation
Print all fields of the sailor with rating 10 {<I, N, 10, A>|∃I, N, A(<I, N, 10, A>∈Sailors)} 10 P. age rating sname sid Sailors

12 Duplicates and Order P.AO(1) P.AO(2) P. age rating sname sid Sailors
Duplicates can be eliminated by suing UNQ. under the relation name The answers can be ordered through the use of .AO (Ascending Order) or .DO (Descending Order) in conjunction with P. Print the names, ages and ratings of all sailors in ascending order by age, and for each age, in ascending order by rating P.AO(1) P.AO(2) P. age rating sname sid Sailors

13 Duplicates and Order 35.5 9 Horatio 10 Rusty 33.0 1 Brutus 63.5 3 Bob
55.5 8 Lubber 45.5 7 Dustin 25.5 Andy Art 16.0 Zorba Age rating sname

14 Queries over multiple Relation
We have to select tuples from the two relations with the value in the join column. We do this by placing the same variable in that columns of the two example relations P._S _Id age rating sname sid Sailors _Id day bid sid Reserve s

15 Queries over multiple Relation
Print the names of the sailor who has reserved a boat for 9/8/98 and who are older than 25 {<N>|∃Id, T, A, B(<Id, N, T, A>∈Sailors ^ A > 25 ^ <Id, B, ‘9/8/98’> ∈Reserves} >25 P._N _Id age rating sname sid Sailors ‘9/8/98’ _Id day bid sid Reserves

16 Queries over multiple Relation
Print the names of the sailor who has reserved some boat that is also reserved by the sailor with ID 22 {<S>|∃Id, T, A, B, D1, D2 (<Id, N, T, A>∈ Sailors ^ <Id, B, D1> ∈ Reserves ^ <22, B, D2> ∈ Reserves } _B _Id 22 day bid sid Reserves P._N _Id age rating sname sid Sailors

17 Negation in the Relation Name Column
Print the names of the sailors who do not have any reservation _Id day bid sid Reserves P._N _Id age rating sname sid Sailors

18 Negation in the Relation Name Column
Print the names of the sailors who are not both younger than 30 and rated higher than 4. This mechanism is similar to set-difference in relational algebra! (A-B) Note: Full mechanism of set-difference cannot be expressed in QBE unless we use views; because if we’ve more than one ¬, then the order is unpredictable! For example: (A-B)-C P._N _Id age rating sname sid Sailors <30 >4 _Id age rating sname sid Sailors

19 Aggregates QBE support aggregates operations such as AVG, COUNT, MAX, MIN and SUM. All the aggregate operations will not eliminate duplicates by default except count. To eliminate duplicates, the UNQ command must be added. For example: AVG.UNQ

20 Aggregates 35 7 Hora 44 10 Rud 58 45 Dustin 22 age rating sname sid

21 Aggregates P.AVG._A age rating sname sid Sailors
The result printed is This implies that the number 35 is counted twice while computing AVG. To print the AVG without the duplicates, P.AVG.UNQ has to be used.

22 Grouping QBE supports ‘grouping’ similar to ‘GROUP BY’ in SQL.
Use the G. command to group by that column. Note: In conjunction with G. , only columns with either G. or aggregate operations can be performed. This rule is very similar to that in SQL! Sailors sid sname rating age P.G.

23 Aggregates on Groups P.AVG._A _A G.P. age rating sname sid Sailors
The above query first groups sailors by their rating and for each rating group computes the average. To print the answers in sorted order by rating, we could use G.P.AO or G.P.DO. instead.

24 Condition Boxes Condition boxes are used in one of the following cases:- Express a condition involving 2 or more columns such as _R/_A > 0.2. Express a condition involving an aggregate operation on a group. Example, AVG._A>30. It is the same as in the “HAVING” clause in SQL. Express conditions involving the AND and OR operators

25 HAVING queries - Condition Boxes
The following query prints those ratings for which the average age is above 30 This is similar to a SQL query involving “HAVING” clause! Sailors sid sname rating age Conditions G.P. _A AVG._A>30

26 HAVING queries - Condition Boxes
The following query prints the “sids of sailors who have reserved all boats which have atleast one reservation.” Note this query does not mean “sids of sailors who have reserved all boats available!” _B2 here means total no. of distinct boats (bids) in the reservation table Sailors sid sname rating age P.G._Id Reserves sid bid day Conditions _Id _B1 _B2 COUNT.B1 = COUNT.B2

27 Condition Boxes (AND/OR)
The following query prints the names of the sailors who are less than 18 years old and their rating is greater than 30. Sailors sid sname rating age Conditions P. _R _A _A<18 AND _R>30

28 OR queries OR queries can be expressed in QBE without using condition boxes too! The following query prints the names of sailors whose age < 20 or age > 30. {<N> | ∃I1,N1, T1,A1, I2,N2, T2,A2 ( <I1,N1, T1,A1> ∈ Sailors(A1 < 30 ∧ N = N1 ) ∨ <I2,N2, T2,A2> ∈ Sailors(A2 > 20 ∧ N = N2))} Notice the use of output variable N as a free-variable! Sailors sid sname rating age P. <20 >30

29 AND queries AND queries can be expressed in QBE without using condition boxes too! The following query prints the names of sailors whose age < 20 and age > 30. {<N> | ∃I1,N1, T1,A1, N2, T2,A2 ( <I1,N1, T1,A1> ∈ Sailors(A1 < 30 ∧ N = N1 ) ^ <I1,N2, T2,A2> ∈ Sailors(A2 > 20 ∧ N = N2))} Notice the use of output variable N as a free-variable! Sailors sid sname rating age _Id _Id P. <20 >30

30 General rule The following general rules can be applied to convert from QBE to DRC and vice versa! Every row in QBE corresponds to one term in DRC and these terms are connected by ‘v’ Conditions within each row in QBE are connected by ‘^’ Examples: Refer to the AND/OR queries in the previous slides!

31 Unnamed Columns Apart from the existing columns, QBE allows extra unnamed columns in the example table! How are they useful?

32 Unnamed Columns Person P._W/_H _H _W P. Height Weight Name The unnamed column (the one in green) here has been used to print the ratio of weight to height of a person.

33 Unnamed Columns _D _Id day bid sid Reserves age P._D P. _Id rating sname sid Sailors Here, we want to print the sailor’s name and the day on which he has a reservation for a boat. But in QBE, printing (using P.) in different tables is disallowed. So how to print from two different tables? Solution: Use unnamed columns!

34 Insertion I. command is used to insert a tuple in QBE.
The above example table inserts a new tuple into Sailors table. 41 age 7 Janice 74 I. rating sname sid Sailors

35 Insertion of multiple tuples
_A age _N _Id I. rating sname sid Sailors _A _N _Id age Login Name Sid Students _A > 18 or _N LIKE ‘C%” Conditions This QBE query inserts multiple tuples into the Sailors table from Students table where the student is either more than 18 years old or the student’s name starts with ‘C’. Note: a null value would be inserted into the rating column!

36 Deletion Deletion is very similar to insertion. We use D. instead of I. The above QBE query deletes all sailors whose rating is greater than 5. age > 5 D. rating sname sid Sailors

37 Deletion – More examples
The following query deletes all reservations made by sailors whose rating is below 5. age < 5 _Id rating sname sid Sailors _Id D. day bid sid Reserves

38 Update Updating is done by using U. command.
The above query updates the age of a sailor whose sid = 22. U.19 age 22 rating sname sid Sailors

39 Update Note: Cannot update the primary key field of a table!
The above query does not make sense! age U.50 rating sname sid Sailors

40 Update The above query increments the age of a sailor with sid = 22 by 1. U._A+1 age 22 rating sname sid Sailors

41 Restrictions on update commands
The following are some restrictions on the use of update commands (I. , D. and U.) :- Restriction 1 1. Cannot mix I. , D. and U. in a single example table (or combine them with P.)

42 Restrictions on update commands
The following update is incorrect due to violation of restriction 1 :- U.19 <7 _Id2 >5 _Id1 D. age rating sname sid Sailors Here we are trying to delete all those sailors whose rating > 5 and update the ages of those sailors to 19 whose rating is less than 7. But, what about those sailors whose rating is in between 5 and 7?

43 Restrictions on update commands
2. Cannot specify these commands in an example table which contains G. command.

44 Restrictions on update commands
The following update is incorrect due to violation of restriction 2 :- G._A U._T+A age rating sname sid Sailors The intention here is to first group sailors according to their ages and then increase the rating of all sailors in each group by their age. But, this example table uses grouping and update in the same table. So, it is disallowed in QBE. Note: This can be done very easily without using G. !

45 Restrictions on update commands
3. Cannot insert, delete or update tuples based on values in fields of other tuples in the same table.

46 Restrictions on update commands
The following update is incorrect due to violation of restriction 3 :- _A 29 U._A+1 22 age rating sname sid Sailors This update is disallowed in QBE because here we’re trying to update Sailor (sid=22)’s age based on the age of Sailor (sid=29).

47 Relational completeness
QBE is ‘relationally complete.’ This means that any query expressed in relational algebra can always be expressed in QBE! Note: We also know that safe relational calculus is relationally complete too!

48 Proof - Relational completeness
Relational algebra supports the following operators:- Selection and projection (use example tables) Set operations - Union, Intersection, Set Difference and cross-product (use A v B ,A ^ B, A^ ¬B) Joins (use multiple example tables) Division (?) We’ve already shown how to achieve all the above operations’ in QBE implicitly, except for division!

49 Division in QBE We have already shown an example of division in QBE earlier. This example uses aggregate operations. But, this example can be re-written to eliminate aggregate operations. Though, this can be done only with the use of updates (creating a view). Hence, QBE is relationally complete only if update commands are taken into account!

50 Division in QBE Consider the query “Find the sailors who have reserved all boats” (division can be used to solve this in relational algebra). In order to get the QBE version of this query, we will first try to write the DRC version! { I,N,T,A | <I,N,T,A> ∈Sailors ∧∀<B,BN,C> ∈ Boats (∃<Ir,Br,D> ∈ Reserves(I = Ir ∧ Br = B))} Idea: Find the sailors such that for every boat they have a reservation on it!

51 Division in QBE QBE does not have the ∀ operation.
Therefore we re-write the previous DRC query as follows: { I,N,T,A | <I,N,T,A> ∈Sailors ∧ ¬ ∃<B,BN,C> ∈ Boats (∃<Ir,Br,D> ∈ Reserves(I = Ir ∧ Br = B))} Idea: Find the sailors such that for every boat they have a reservation on it!

52 Division in QBE { I,N,T,A | <I,N,T,A> ∈Sailors ∧ ¬ ∃<B,BN,C> ∈ Boats (∃<Ir,Br,D> ∈ Reserves(I = Ir ∧ Br = B))} Sailors sid sname rating age _Id P.S Reserves sid bid day _Id _B Boats bid bname color _B

53 Division in QBE There is something wrong with the above QBE query! (note: this query does not involves explicit views) _B does not appear in any positive row. QBE does not allow this. (of course this does not make sense!) So how to write the correct QBE query? Solution: create a view (using update operation: I.)

54 Division in QBE Sailors sid sname rating age _Id Boats bid bname color
Reserves sid bid day _Id _B BadSids Sid I. _Id

55 QBE – relational completeness
Note that this not an exhaustive proof. The idea is to show that all operations supported in relational algebra (including division) can be ported to QBE (with update commands included).

56 More commands We have not discussed in detail about other QBE features like :- Creating new tables Creating views (or snapshots) Dropping tables Please refer to further study resources for more information on these topics if interested.

57 Further study resources
Exercises found in Chapter 6 of our textbook (this chapter is available online at the book’s website) C.J.Date, An Introduction to Database Systems, 3rd Edition MS Access manual (teaches you how to make QBE-based queries in Access) notes/Chapter5/node2.html


Download ppt "Query By Example (QBE) Mallipeddi Venkata Harish Terence Gam Israel."

Similar presentations


Ads by Google