Download presentation
Presentation is loading. Please wait.
1
RELATIONAL CALCULUS Prof. Sin-Min Lee Department of Computer Science
2
Tuple Relational Calculus Relational model is based on the relational algebra, but we might equally well have said it is based on the relational calculus. While the algebra provides a collection of explicit operations, the calculus provides a notation for formulation the definition of that desired relation in terms of those given relations.
3
What is Tuple Relational Calculus? n It is a non procedural query language: Describes the desired information without giving a specific procedure for obtaining that information. n A query in tuple relational calculus is expresses as: {t | P(t)} {t | P(t)} This represents a set of all tuples t such that predicate P is true for t. This represents a set of all tuples t such that predicate P is true for t.
4
Sample Queries n Finding the branch – name, loan – number, and amount for loans of over $ 1200 {t | t loan ٨ t[amount] > 1200} {t | t loan ٨ t[amount] > 1200} n In English this query would mean: The set of tuples t where t belongs to the loan relation and the loan amount for each t is greater than $ 1200.
5
Sample queries(cont….) n Finding the loan – number for each loan of an amount greater than $1200. {t | s loan (t[loan – number] = s[loan – number] ۸ s[amount > 1200)} {t | s loan (t[loan – number] = s[loan – number] ۸ s[amount > 1200)} In English we would read the preceding statement as “ The set of all tuples t such that there exists a tuple s in relation loan for which the values of t and s for the loan – number attribute are equal, and the value of s for the amount attribute is greater than $1200.” In English we would read the preceding statement as “ The set of all tuples t such that there exists a tuple s in relation loan for which the values of t and s for the loan – number attribute are equal, and the value of s for the amount attribute is greater than $1200.”
6
Sample Queries(cont….) n The expression: {t | r customer (r[customer – name] = t[customer – name]) ٨ ( u branch (u[branch – city] = Brooklyn” s depositor (t[customer – name] = s[customer – name] w account (w[account – number- = s[account – number] ٨ w[branch – name] = u[branch – name]))))} {t | r customer (r[customer – name] = t[customer – name]) ٨ ( u branch (u[branch – city] = Brooklyn” s depositor (t[customer – name] = s[customer – name] w account (w[account – number- = s[account – number] ٨ w[branch – name] = u[branch – name]))))} In English, would mean “The set of all customers( i.e customer name tuples t) such that for all tuples u in the branch relation, if the value of u on attribute branch – city is Brooklyn, then the customer has an account at the branch whose name appears in the branch name attribute of u” In English, would mean “The set of all customers( i.e customer name tuples t) such that for all tuples u in the branch relation, if the value of u on attribute branch – city is Brooklyn, then the customer has an account at the branch whose name appears in the branch name attribute of u”
7
Formal Definition n As shown earlier, a tuple relational calculus expression is of the form {t | P(t)} {t | P(t)} Where P is a formula. Where P is a formula. n A formula may contain several tuple variables. n A tuple variable is said to be a free variable unless it is quantified by a З(there exists) or Џ(for all). n Tuple variables that are quantified by З or Џ are called bound variables. n For ex, in the expression: t loan ٨ s customer(t[branch – name] = s[branch – name]) t loan ٨ s customer(t[branch – name] = s[branch – name]) t is a free variable and s is a bound variable. t is a free variable and s is a bound variable.
8
Formal definition(cont….) n A formula in relational calculus is made up of atoms. n An atom has one of the following forms. 1. S Є r, where s is a tuple variable and r is a relation. 2. S[x] © u[y], where s and u are tuple variables, x is an attribute on which s is defined, y is an attribute on which u is defined, and © is the comparison operator. It is required that x and y have domains that can be compared using ©. 3. S[x] © c, where s is a tuple variable, x is an attribute on which s is defined, © is a comparison operator, and c is a constant in the domain of x.
9
Building a formula We build up a formula from atoms by using the following rules. We build up a formula from atoms by using the following rules. n An atom is a formula. n If P1 is a formula, then so are ¬P1 and (P1). n If P1 and P2 are formulae, then so are P1 ٧ P2, P1 ٨ P2, and P1 => P2. n If P1(s) is a formula containing a free tuple variable s, and r is a relation, then s r(P1(s)) and s (P1(s)) s r(P1(s)) and s (P1(s)) are also formulae are also formulae
10
Safety of Expressions – The concept of domain n A tuple relational calculus may generate an infinite relation. For ex: {t | ¬(t loan)} {t | ¬(t loan)} There are infinitely many tuples that are not in loan. There are infinitely many tuples that are not in loan. n To address this issue, we use the concept of domain of tuple relational formula, P. The domain of P, denoted dom(P), is a set of all value referenced by P.These include values mentioned in P itself, as well as the values that appear in a tuple of a relation mentioned in P. The domain of P, denoted dom(P), is a set of all value referenced by P.These include values mentioned in P itself, as well as the values that appear in a tuple of a relation mentioned in P. For ex: dom(t loan ٨ t[amount] > 1200) is the set containing 1200 as well as set of all values appearing in loan. For ex: dom(t loan ٨ t[amount] > 1200) is the set containing 1200 as well as set of all values appearing in loan.
11
Domain Relational Calculus This is the second form of relational calculus n Uses domain variables that take on values from an attribute domain, rather than values for an entire tuple. n Closely related to tuple relational calculus.
12
Formal Definition n A general expression in Domain relational calculus is of the form { | P(x1,x2,….,xn)} { | P(x1,x2,….,xn)} where x1, x2,….,xn represent domain variables. P represents a formula composed of atoms, as was the case in tuple relational calculus. where x1, x2,….,xn represent domain variables. P represents a formula composed of atoms, as was the case in tuple relational calculus.
13
An atom in Domain Relational Calculus An atom in domain relational calculus has one of the following forms: An atom in domain relational calculus has one of the following forms: n r, where r is a relation on n attributes and x1, x2, ….,xn are domain values or domain constraints. n X © y, where x and y are domain values, and © is a comparison operator (,>=). We require that attributes x and y have domains that can be compared by ©. n X © c, where x is a domain variable, © is a comparison operator, and c is a constant in the domain of the attribute for which x is a domain variable.
14
Example Queries n Find the loan number, branch name, and amount for loans over $1200. { | loan ٨ a > 1200} { | loan ٨ a > 1200} n Find all loan numbers for loans with an amount > 1200: { | b,a ( loan ٨ a > 1200)} { | b,a ( loan ٨ a > 1200)} n When we write b in domain calculus, b refers not to a tuple, but rather to a domain value. n The domain of b is unconstrained(unlike as in relational tuple calculus) until the sub formula loan constraints b to branch names that appear in the loan relation.
15
Example queries(cont….) n Find the names of all customers who have an account at all branches located in Brooklyn: { | n( customer) ٨ x,y,z ( branch ٨ y = “Brooklyn” => a,b( account ٨ depositor))} { | n( customer) ٨ x,y,z ( branch ٨ y = “Brooklyn” => a,b( account ٨ depositor))} In English, we interpret this expression as “The set of all(customer name) tuples c such that, for all (branch – name, branch – city, assets) tuples, x,y,z, if the branch city is Brooklyn, then the following is true: In English, we interpret this expression as “The set of all(customer name) tuples c such that, for all (branch – name, branch – city, assets) tuples, x,y,z, if the branch city is Brooklyn, then the following is true: 1. There exists a tuple in the relation account with account number a and branch name x. 2. There exists a tuple in the relation depositor with customer c and account number a.”
16
Safety of Expressions n As noted in tuple relational calculus expressions in the domain relational calculus may also generate an infinite relation. For ex: {{ | ¬ ( loan)} {{ | ¬ ( loan)} is unsafe, because it allows values in the result that are not in the domain of the expression. is unsafe, because it allows values in the result that are not in the domain of the expression.
17
Safety(cont….) An expression: An expression: { | P(x1,x2,….,xn)} { | P(x1,x2,….,xn)} is safe if all of the following hold. is safe if all of the following hold. n All values that appear in tuples of the expression are values from dom(P). n For every “there exists” subformula of the form Э x(P1(x)), the sub formula is true only if and only if there is a value x in dom(P1) such that P1(x) is true. n For every “for all” subformula of the form x(P1(x)), the subformula is true if and only if P1(x) is true for all values of x from dom(P1).
18
n Introduction n Tuple-Oriented Relational Calculus n Computational Capabilities n Domain-Oriented Relational Calculus
19
The calculus simply describes what the problem is, the algebra prescribes a procedure for solving that problem. Or we can say the algebra and the calculus are precisely equivalent to one another, because for every expression of the algebra, there is an equivalent expression in the calculus.
20
Relational calculus is founded on a branch of mathematical logic called the predicate calculus. The fundamental feature of the calculus as defined in reference is the notion of the tuple variable.
21
What is Tuple Variables? A tuple variable is a variable that “range over” a named relation: that is a variable whose only permitted values are tuples of the relation.
22
EXAMPLES To specify the range of a tuple variable T as the Staff relation, we write: RANGE OF T IS STAFF To express the the query ‘Find the set of all tuples T such that P(T) is true’ we can write { T | P(T) }
23
Free and Bound Variables Each occurrence of a tuple variable within a WFF ( Well- Formed Formula) is either free or bound. Here we mean an appearance of the variable name within the WFF under consideration.
24
A tuple variable T occurs within a WFF either in the context of an attribute context of an attribute reference of the form T.A (where A is an attribute of the relation over which T ranges) or as the variable immediately following one of the quantifiers EXISTS and FORALL
25
Rules n Within a simple comparison such as “T.A < U.A” all occurrences are free “T.A < U.A” all occurrences are free n Occurrences in the WFFs “(f)” and “NOT f” are free or bound according as they are free or bound in f. Occurrences in the WFFs “f AND g”, “NOT f” are free or bound according as they are free or bound in f. Occurrences in the WFFs “f AND g”, “f OR g”, and “IF f THEN g” are free or bound according as they are free or bound in f or g “f OR g”, and “IF f THEN g” are free or bound according as they are free or bound in f or g
26
Example n Simple Comparison: SX.S= = ‘S1’ SX.S= = ‘S1’ (Range of SX is S) here occurrence SX is free (Range of SX is S) here occurrence SX is free n Boolean WFFs: - PX.WEIGHT 25 - PX.WEIGHT 25 - NOT ( SX.CITY = ‘London’ ) - NOT ( SX.CITY = ‘London’ ) here PX and SX are free here PX and SX are free
27
QUANTIFIERS There are two quantifiers: 1/ EXISTS 1/ EXISTS 2/ FORALL 2/ FORALL
28
EXISTS EXISTS x (f) means there exists at least one value of the variable x such that the WFF f evaluates to true. If f is a WFF in which variable x is free and bound in f EXISTS x (f) means there exists at least one value of the variable x such that the WFF f evaluates to true. If f is a WFF in which variable x is free and bound in f
29
Example Let relation R contain the following tuples ( 1, 2, 3 ); ( 1, 2, 4 ); ( 1, 3, 4 ) ( 1, 2, 3 ); ( 1, 2, 4 ); ( 1, 3, 4 ) EXISTS T ( T[3] > 1 ) : true EXISTS T ( T[3] > 1 ) : true EXISTS T ( T[2] > 3 ) : false EXISTS T ( T[2] > 3 ) : false EXISTS T ( T[1] > 1 OR T[3] = 4 true EXISTS T ( T[1] > 1 OR T[3] = 4 true
30
FORALL FORALL FORALL x ( f ) means: For all values of the variable x, the WFF f evaluates are true.
31
Example FORALL PX ( PX.COLOR = ‘Red’ ) This WFF can be read as : For all P tuples, PX say, the COLOR value in that tuple is Red. ( The occurrence of PX in this example is bound)
32
More Example Let relation R contain the following tuples ( 1, 2, 3 ); ( 1, 2, 3 ); ( 1, 3, 4 ) ( 1, 2, 3 ); ( 1, 2, 3 ); ( 1, 3, 4 ) FORALL T ( T[1] > 1 )false FORALL T ( T[2] > 1 ) true FORALL T ( T[1] = 1 AND T[3] > 2 ) true true
33
TARGET ITEM COMMALISTS Each target item in a target item commalist is either a simple variable name such as T.A [ AS X ] Here T is a tuple variable, A is an attribute of the associated relation, and X is an attribute name for the corresponding attribute in the result of evaluating the target item commalist
34
What is the target item? Target Item that is just a variable name T is defined to be shorthand for the commalist of target items T.A1, T.A2, T.A3,……, T.An where A1, A2, … An are all of the attributes of the relation associated with T
35
Examples - ( SX.S# ) - ( SX.S# AS SNO ) - ( SX.S# AS SNO ) WHERE SX.CITY = ‘London’ SX.CITY = ‘London’ - ( SX.S# ) WHERE SX.CITY = ‘London’
36
Computational Capabilities We just simply extend the definition of comparands and to include a new category, scalar- expression, where the operands of such an expression in turn can include literal, attribute references, and/or aggregate function references. We just simply extend the definition of comparands and target-items to include a new category, scalar- expression, where the operands of such an expression in turn can include literal, attribute references, and/or aggregate function references.
37
Aggregate-Function is COUNT, SUM, AVG, MAX, or MIN Syntax for aggregate-function reference: aggregate-function (expression[,attribute]) * expression is an expression of the tuple calculus, and attribute is that attribute of that result relation over which the aggregation is to be done
38
Domain-Oriented Relational Calculus
39
In domain-oriented relational calculus, we use variables that take their values from domain instead of tuples of relations. In the domain-oriented relational calculus, we often want to test for a membership condition, to determine whether values belong to a relation.
40
Examples - ( SX ) denotes the set of all supplier numbers denotes the set of all supplier numbers - ( SX ) WHERE S ( S#: SX ) denotes the set of all supplier numbers in relation S denotes the set of all supplier numbers in relation S - ( SX ) WHERE S ( S#:SX, CITY: ‘London’ denotes that subset of those supplier numbers for which the city is London denotes that subset of those supplier numbers for which the city is London
41
DEFINITION (cont ’ d) n Target list: A condition in a relational calculus statement that restricts membership in a solution relation. n Existential quantifier: Relational calculus expression affirming the existence of a at least one row to which a condition applies. n Universal quantifier: Relational calculus expression stating that some condition applies to every row of some type.
42
CONSTRUCTION OF RELATIONAL CALCULUS In Relational Calculus, queries or expressions, are constructed as well-formed formulas. A well-formed formula (WFF) is an expression composed ultimately of atomic predicates. A WFF is itself a predicate, which assumes a true or false value depending on the argument values in its constituent atomic predicates. In Relational Calculus, queries or expressions, are constructed as well-formed formulas. A well-formed formula (WFF) is an expression composed ultimately of atomic predicates. A WFF is itself a predicate, which assumes a true or false value depending on the argument values in its constituent atomic predicates.
43
CONSTRUCTION OF RELATIONAL CALCULUS (cont ’ d) Figure 4.1 Truth tables for logical operators
44
CONSTRUCTION OF RELATIONAL CALCULUS (cont ’ d) For example, if a, b are two WFFs, then (a b) is also a WFF. (a b) is true for precisely those argument values of a and b for which both a and b are true. Figure 4.1 gives the basic set of logical operators that can be used to produce new WFFs from existing WFFs; it also shows the truth values of the new WFF as a consequence of the truth values of the components.
45
WAY OF CONSTRUCTING A RELATIONAL CALCULUS EXPRESSION We can construct a relational-calculus expression, or WFF, in the following ways: A predicate P is a WFFA predicate P is a WFF If P is a WFF, then so are (P) and ~(P)If P is a WFF, then so are (P) and ~(P) If P1 and P2 are WFF ’ s, then so are (P1 P2) andIf P1 and P2 are WFF ’ s, then so are (P1 P2) and (P1 V P2) (P1 V P2)
46
WAY OF CONSTRUCTING A RELATIONAL CALCULUS EXPRESSION (cont ’ d) If P is WFF in which t occurs as a free variable, thenIf P is WFF in which t occurs as a free variable, then so are t(P) and t(P) so are t(P) and t(P) nothing else is a WFFnothing else is a WFF Symbols and are called quantifiers. Symbol is called the existential quantifier and is interpreted or read as “ three exists ” and is called the universal quantifier and interpreted as “ for all. ”
47
WAY OF CONSTRUCTING A RELATIONAL CALCULUS EXPRESSION (cont ’ d) An example of existential quantification: Q(w, x) = ( y, z)(P(w, x, y, z)) n (w, x) is a fixed pair n If P(w, x, y, z) is true for some choice of a (y, z) pair, Q(w, x) is true. n Otherwise it is false. n w, x, y, z are free variables in P(w, x, y, z) n However, y and z are not free variables in the expression ( y, z)(P(w, x, y, z))
48
SELECTION VIA A PREDICATE The following example: {t |tank(t) tcolor(t) = “ red ” } n The automatic predicate tank(t) responds true when the tuple t is element of the tank relation The function tcolor(t) returns the tcolor value of the tuple t, and the atomic predicate tcolor(t) = “ red ” tests the returned value against a constant. The function tcolor(t) returns the tcolor value of the tuple t, and the atomic predicate tcolor(t) = “ red ” tests the returned value against a constant. n Therefore the complete predicate (WFF) isolates the tuples corresponding to red tanks.
49
SELECTION VIA A PREDICATE (cont ’ d) The need for two types of atomic predicates: n The first type tests membership in some named relation. Each table in the data base gives rise to an atomic predicate of the same name, which takes one argument. The predicate returns true when the argument is a tuple within the table, and it returns false in all other cases. For example, tank(t) is true when the tuple t is an element of the tank relation, and it ’ s false otherwise. For example, tank(t) is true when the tuple t is an element of the tank relation, and it ’ s false otherwise.
50
SELECTION VIA A PREDICATE (cont ’ d) The second type of atomic predicate permits judgments about the attribute values within tuples. When the argument is a tuple that contains an association over the named attribute, the the function returns the corresponding value. If the tuple doesn ’ t contain such an association, then the function returns the value null. The second type of atomic predicate permits judgments about the attribute values within tuples. When the argument is a tuple that contains an association over the named attribute, the the function returns the corresponding value. If the tuple doesn ’ t contain such an association, then the function returns the value null. For example, tcolor(t) returns the tcolor value of the tuple t, that is, red, blue, or green, if t contains a tcolor value. Otherwise, tcolor(t) returns null.
51
Universal queries Relational calculus expresses a universal query by assigning free variables to the relation providing the candidates. It then introduces a universally quantified component, stating ( t)((t is an anchor) => (t is reachable from the candidate)) To find the names of species represented in all tanks that contain representatives of all species with a diet of worms. Follow a stepwise construction proceeds: Q = {sname(s) | species(s) ( t)(P1(t) => ( f)(fish(f) snow(f) = snow(s) tno(f) = tno(t)))} snow(f) = snow(s) tno(f) = tno(t)))}
52
Universal queries (cont ’ d) if P1(t) is true when t is a tank that contains all the wormy species. P1(t) = tank(t) ( s1)(P2(s1) => ( f1)(fish(f1) snow(f1) = snow(s1) tno(f1) = tno(t))) snow(f1) = snow(s1) tno(f1) = tno(t))) P2(s1) is true when s1 is a wormy species. Therefore, P2(s1) = species(s1) sfood(s1) = “ worms ” Substituting P2 into P1 and P1 into Q give a single WFF that effectively filters the database for the requested species names.
53
DOMAIN RELATIONAL CALCULUS In domain relational calculus the variables range over a pool of values, the union of all domains in the database schema. The atomic predicates permit table membership tests on assembled tuples, and they allow direct -comparisons between the variables For example, the following expression requests the names of red tanks {x | ( w, y) (tank(tno = w, tname = x, tcolor = “ red ”, tvolume = y))}
54
DOMAIN RELATIONAL CALCULUS (cont ’ d) {x | ( w, y) (tank(tno = w, tname = x, tcolor = “ red ”, tvolume = y))} For a fixed candidate x, the variables w and y range over the pool of elements in the common domain and attempt to lock on to value such that the assembled tuple appears in the tank relation. If w and y can achieve such values, x is the name of a red tank.
55
VARIABLES A variables is free if it is not within the scope of a quantifier, otherwise, it is a bound variable. Restricting the scope of tuple variable is important. For example, r(a) 5 may trigger the searching of the entire world, that is, there is no end to values that are not equal to 5.
56
VARIABLES (cont ’ d) We can fix the problem in two ways: Add a range (scope) declaration before the WFF.Add a range (scope) declaration before the WFF. Add a predicate into the WFF that expressesAdd a predicate into the WFF that expresses r R, i.e.., tuple variable r belong to relation R. r R, i.e.., tuple variable r belong to relation R. The above methods guarantee us termination of the search process since we do not have to search beyond the relation itself.
57
CONTROL SUB-LANGUAGES Definition The Control Sub-Languages have been provided to allow users to access and control an Information System. In their simplest form they allow users to create, read, update, and destroy information held in that system, and furthermore they can be used to read, create, update, and destroy the description of that system.
58
CONTROL SUB-LANGUAGES (cont ’ d) In addition to these commonplace tasks, the language provides a set of powerful tools to allow users to identify the information that they require, thus ensuring that they do not have to bother with unnecessary details. Design Control Sub-Language: It is virtually impossible to design a standard language which
59
CONTROL SUB-LANGUAGES (cont ’ d) can be used successfully by all those who might wish to interact with an Information System. however, it is possible to design a basic language which satisfies the majority of users and to provide extensions to that basic language. This is the approach which has been adopted with the Control Sub Language.
60
CONTROL SUB-LANGUAGES (cont ’ d) Manipulating Entity Sets: The descriptions above tell us how Control Sub- Language provides facilities for a user to transfer information between a given entity set and this terminal. This works well providing the user whishes to access the whole of a single set.
61
CONTROL SUB-LANGUAGES (cont ’ d) However, if the user whishes to do more than this, then the user has, implicitly or explicitly, to create a new temporary set. This temporary set exists within the user ’ s work space and is linked to the entity sets of the Information Environment by the successive use of the relational operators.
62
CONTROL SUB-LANGUAGES (cont ’ d) These operators are: n Projection: {r[A]:r R} n Union: {t:t R V t S} n Difference: {t:t R t S} n Cartesian product: {(r || s) : r R s S} n Selection: {r:r R (r[A] opr)} where opr is V or r[B] n Intersection: {t:t R t S} n Join: {r || s): r R s S (r[A] s[B])} n Division: {r[A]: r R s[B] (r[A]} where (r[A]) are all tuples in r[A]. (r[A]) are all tuples in r[A].
63
SUMMARY Tuple-Oriented Relational Calculus Computational Capabilities Domain-Oriented Relational Calculus
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.