1 Introduction to Databases 2 2 Basic Relational Data Model 11



Download 1.01 Mb.
Page20/31
Date13.05.2017
Size1.01 Mb.
#17912
1   ...   16   17   18   19   20   21   22   23   ...   31

7.2Tuple Variables


Free variables in logical functions can in principle range over any type of value. A feature that distinguishes Relational Calculus from other similar calculi is that the free variables range over relations. More specifically, any free variable ranges over the extension of a designated relation, ie. the current set of tuples in the relation. Thus, a free variable may be instantiated with a tuple selected from the designated relation.

Suppose, for example, we introduced a variable C to range over the relation Customer, as in Figure 7 -1. Then C may be instantiated with any one of the three tuples at any one time. The example shows C instantiated with the second tuple. Equivalently, we may sometimes say that C ‘holds’ a value instead of being instantitated with that value5. In any case, because variables like C range over tuples (or is only permitted to hold a tuple), they are termed tuple variables.

F
igure
7-1 A Tuple Variable C ranging over the Customer Relation

A tuple has component parts, and unless we have a means of referring to such parts, the logical functions we formulate over relations will have limited expressive power. Given, for example, two variables X and Y that range over two different relations with a common domain, we may want to specify a condition where their current instantiations are such that the values under the common domain are identical. Thus while X (and Y) denote a tuple as a whole, we really wish to compare tuple component values. The syntactic mechanism provided for this purpose takes the form:



.

and is interpreted to mean the value associated with in the current instantiation of . Thus, assuming the instantiation of C as in Figure 7 -1:

C.C# = 2
C.Cname = ‘Martin’
…etc

This denotation of a particular data item within a tuple variable is often referred to as a projection of the tuple variable over a domain (eg. “C.Cname” is a projection of tuple variable C over the domain Cname).

Relational Calculus is a collection of rules of inference of the form:

:

where is a list of free variables and/or their projections that are referenced in . This list is thought of as the “target list” because the set of instantiations of the list items that makes true is the desired result. In other words, an inference rule may be thought of as a query, and may be informally understood as a request to find all variable instantiations that satisfy and, for each such instantiation, to extract the data items mentioned in .

For example, consider the inference rule in Figure 7 -2. It references one free variable, C, which ranges over Customer. The specifies items we are interested in - only the phone number in this case - but only of those tuples that satisfy the . In other words, the rule may be paraphrased as the query to “get the set of phone numbers of customers who live in London”. Note that the use of the variable C both in and in denotes the same instantiation, thereby ensuring that “C.Cphone” is extracted from the same tuple that satisfies the comparison “C.Ccity = London”. The computed set in this case would be {2263035, 2234391}, corresponding to the phone numbers in the first and last tuples - these being the only tuples satisfying “C.Ccity = London”.

F
igure
7-2 An inference rule over the Customer relation

The reader should note the simplicity and declarative character of the inference rule, which merely states the desired result (the ) and the conditions that must be satisfied (the ) for a value to be included in the result. Contrast this with relational algebra which would require the following construction:

select Customer where Ccity = ‘London’ giving X;
project X over Cphone giving Result

The above example only used a single variable. However, a single variable can only range over a single relation, while often the data items of interest are spread over more than one relation. In such cases, we will need more than one tuple variable.

Figure 7 -3 illustrates such a case involving two variables, P and T, ranging over relations Product and Transaction respectively. Note that the inference rule at the top of the figure


  • lists items from both variables in the target list (ie. P.Pname, T.C#)

  • compares in the logical expression projections of the two different variables over the same domain (T.P# = P.P#)

It further illustrates specific instantiations of each variable and evaluation of the logical expression in the context of these instantiations. In this case, the logical expression is true and therefore the items in the target list are extracted from the variables (shown in the “result” table). It is important to note that a given inference, as in this illustration, is entirely in the context of a specific instantiation of each tuple variable. It is meaningless, for example, to evaluate “T.P# = P.P#” using one instance of P and “P.Price > 1000” using another. The total number of inferences that can be attempted for any given rule is therefore the product of the cardinality of each variable’s range.

The inference rule in this example may be paraphrased as the query “find the customer numbers and product names, priced at more than 1000, that they purchased”. As an exercise, the reader should attempt to construct this query in relational algebra (hint: it will involve the basic operators Select, Project and Join).





Figure 7-3 Multiple variable inference




Download 1.01 Mb.

Share with your friends:
1   ...   16   17   18   19   20   21   22   23   ...   31




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

    Main page