Page 95 - DCAP402_DCAO204_DATABASE MANAGEMENT SYSTEM_MANAGING DATABASE
P. 95
Database Management Systems/Managing Database
Notes 6.4 Query-by-Example
Query-by-Example (QBE) is another language for querying (and, like SQL, for creating and
modifying) relational data. It is different from SQL, and from most other database query languages,
in having a graphical user interface that allows users to write queries by creating example tables
on the screen. A user needs minimal information to get started and the whole language contains
relatively few concepts. QBE is especially suited for queries that are not too complex and can be
expressed in terms of a few tables.
A user writes queries by creating example tables. QBE uses domain variables, as in the DRC, to
create example tables. The domain of a variable is determined by the column in which it appears,
and variable symbols are prefixed with underscore (_) to distinguish them from constants.
Constants, including strings, appear unquoted, in contrast to SQL. The fields that should appear
in the answer are specified by using the command P., which stands for print. The fields containing
this command are analogous to the target-list in the SELECT clause of an SQL query.
This unit introduce QBE through example queries involving just one relation. To print the
names and ages of all sailors, we would create the following example table:
Sailor sid sname rating age
P._N P._A
A variable that appears only once can be omitted; QBE supplies a unique new name internally.
Thus the previous query could also be written by omitting the variables N and A, leaving just P.
in the sname and age columns. The query corresponds to the following DRC query, obtained
from the QBE query by introducing existentially quantified domain variables for each field.
A large class of QBE queries can be translated to DRC in a direct manner. (Of course, queries
containing features such as aggregate operators cannot be expressed in DRC.) This unit shall
present DRC versions of several QBE queries. Although we will not define the translation from
QBE to DRC formally, the idea should be clear from the examples; intuitively, there is a term in
the DRC query for each row in the QBE query, and the terms are connected using^.
A convenient shorthand notation is that if we want to print all fields in some relation, we can
place P. under the name of the relation. This notation is like the SELECT * convention in SQL.
It is equivalent to placing a P. in every field:
Sailor sid sname rating age
P.
Selections are expressed by placing a constant in some field:
Sailor sid sname rating age
P. 10
Placing a constant, say 10, in a column is the same as placing the condition =10. This query is very
similar in form to the equivalent DRC query
We can use other comparison operations (<, >,<=,>=, ¬) as well.
88 LOVELY PROFESSIONAL UNIVERSITY