1 Introduction to Databases 2 2 Basic Relational Data Model 11



Download 1.01 Mb.
Page29/31
Date13.05.2017
Size1.01 Mb.
#17912
1   ...   23   24   25   26   27   28   29   30   31

10.4The Prefix ALL


The prefix ALL can be applied to example elements. The occurrence of such an element in an arbitrary query row of an arbitrary relation denotes a set of values such that each, together with a particular instantiation of other items in the row, matches a tuple of the relation. As an example, consider the following relation and query:

F
igure
10-18 Example relation and query with ALL

In this case, there is only one other item in the query row: another element X. The set of values denoted by ‘All.Y’ therefore needs to be determined for each value that X takes. Thus,


  • when X = 1, there are two possible values for Y, ie. 1 and 2. Thus, ‘All.Y’ is the set {1,2}

  • when X = 2, there is only one value for Y, ie. the set {1}

  • when X = 3, there is also only one value for Y, ie. the set {2}

If the query items had been prefixed with ‘P.’, the result displayed would be:


R1







I1

I2



1

{1,2}




2

{1}




3

{2}




I
n the simplest case, a query row contains only one element prefixed with ALL. In this case, the element simply denotes the set of values in the corresponding domain. This is illustrated in Figure 10 -19 below.

Figure 10-19 Simple use of ALL

The use of ALL is more interesting when it involves multitable queries. For example, combining the query in Figure 10 -18 and Figure 10 -19 into a single query, we effectively restrict X to just the value 1. This is because ALL.Y occurs in both tables and must denote the same set, and the only set satisfying this is {1,2}.

It should be clear now that ALL is used in QBE in the same way that a universal quantifier is used in relational calculus with domain variables. To highlight this, consider the query:

“Get the names of customers who bought all types of the company’s product”

T
hree relations are required to resolve this query: Customer, Transaction and Product. The QBE query is shown in Figure 10 -20 which is also annotated with explanations.

Figure 10-20 The query “Get the names of customers who bought all types of the company’s product”

One final word about ALL: it does not remove duplicate values, in contrast to an unprefixed element which will return only unique matching values. This is illustrated in Figure 10 -21 below. We shall see in the next section how this property is used (if fact, is necessary) in order to answer certain classes of practical queries.



Figure 10-21 ALL does not remove duplicates!


10.5Library Functions


As with SQL, QBE also provides arithmetic operations and a number of built-in functions which are necessary to manipulate the values in ways not otherwise within the scope of relational calculus, eg. to count the number of occurrences of returned values or to sum them up. As you may expect by now, these operations are provided in the form of prefixes. For example, suppose we wish to know how many transactions were related to the purchase of a particular product, say product number 1. We can extract, for example, all customer numbers in transactions involving product number 1:


Transaction




Transaction (Query)




Transaction

C#

P#

Date

Qnt




C#

P#

Date

Qnt




C#

P#

Date

Qnt

1

1

21.01

20




P.All.X

1










1










1

2

23.01

30



















2










2

1

26.01

25



















1










1

1

29.01

20































But what we are really interested in is counting the number of such values. QBE allows us to do this with the prefix CNT (equivalent to the function COUNT in SQL), which counts the number of values matching the element it prefixes.

Thus the same query above, different only in the addition of the CNT prefix, achieves the desired result:




Transaction




Transaction (Query)




Transaction

C#

P#

Date

Qnt




C#

P#

Date

Qnt




C#

P#

Date

Qnt

1

1

21.01

20




P.CNT.All.X

1










3










1

2

23.01

30































2

1

26.01

25































1

1

29.01

20































Note that the use of ALL is necessary. If the example element was simply “P.CNT.X”, the result would be 2! This is because without the ALL prefix, the values matching the element X are returned with duplicate values removed (as illustrated earlier in Figure 10 -21).

Another frequently used function is SUM, which sums up the values matching the example element it prefixes. Suppose, we wish to know the total number of product number 1 that has been sold. Instead of counting the number of customers that purchased it, we sum instead the quantities recorded in the relevant transactions. Thus:




Transaction




Transaction (Query)




Transaction

C#

P#

Date

Qnt




C#

P#

Date

Qnt




C#

P#

Date

Qnt

1

1

21.01

20







1




P.SUM.All.X













65

1

2

23.01

30































2

1

26.01

25































1

1

29.01

20































QBE also allows us to group tuples in a relation based on a specified example element. That is, tuples with the same value of the example element are collected into one group (there will be as many groups as there are distinct values matching the example element). Grouping is specified using the G prefix (this is similar to the ‘Group By’ clause in SQL). Thus:


Transaction




Transaction (Query)







C#

P#

Date

Qnt




C#

P#

Date

Qnt




1

1

21.01

20

1

1

21.01

20




P.G.X













1

2

23.01

30

1

2

23.01

30



















1

1

29.01

20

2

1

26.01

25































1

1

29.01

20



















2

1

26.01

25

Aritmetic functions may be applied to groups. Thus, if we wanted to know the total number of items purchased by each customer, we can modify the above query as follows:




Transaction




Transaction (Query)




Transaction

C#

P#

Date

Qnt




C#

P#

Date

Qnt




C#

P#

Date

Qnt

1

1

21.01

20




P.G.X







P.SUM.All.B




1







70

1

2

23.01

30



















2







25

2

1

26.01

25































1

1

29.01

20































Groups may additionally be selected based on conditions that are specified in an additional column (this corresponds to the ‘Having clause’ of SQL). This additional conditions column may be created by means of a special menu item in the QBE interface.

Thus, if we are only interested in finding customers who have purchased more than 45 items, our query would be as follows:




Transaction




Transaction (Query)




Transaction

C#

P#

Date

Qnt




C#

P#

Date

Qnt

Conditions




C#

P#

Date

Qnt

1

1

21.01

20




P.G.X







All.B

SUM.All.B>45




1










1

2

23.01

30


































2

1

26.01

25


































1

1

29.01

20

































In summary, grouping and arithmetic functions can be used in combination to obtain useful derived values from the database.




Download 1.01 Mb.

Share with your friends:
1   ...   23   24   25   26   27   28   29   30   31




The database is protected by copyright ©ininet.org 2024
send message

    Main page