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
Share with your friends: