Presentation is loading. Please wait.

Presentation is loading. Please wait.

Relational Calculus. Relational calculus query specifies what is to be retrieved rather than how to retrieve it. – No description of how to evaluate a.

Similar presentations


Presentation on theme: "Relational Calculus. Relational calculus query specifies what is to be retrieved rather than how to retrieve it. – No description of how to evaluate a."— Presentation transcript:

1 Relational Calculus

2 Relational calculus query specifies what is to be retrieved rather than how to retrieve it. – No description of how to evaluate a query. In first-order logic (or predicate calculus), predicate is a truth-valued function with arguments. When we substitute values for the arguments, function yields an expression, called a proposition, which can be either true or false. 2

3 Relational Calculus If predicate contains a variable (e.g. ‘x is a member of staff’), there must be a range for x. When we substitute some values of this range for x, proposition may be true; for other values, it may be false. When applied to databases, relational calculus has forms: tuple and domain. 3

4 Tuple Relational Calculus Interested in finding tuples for which a predicate is true. Based on use of tuple variables. Tuple variable is a variable that ‘ranges over’ a named relation: i.e., variable whose only permitted values are tuples of the relation. Specify range of a tuple variable S as the Staff relation as: Staff(S) To find set of all tuples S such that P(S) is true: {S | P(S)} 4

5 Instances of Branch and Staff Relations 5

6 Tuple Relational Calculus - Example To find details of all staff earning more than £10,000: {S | Staff(S)  S.salary > 10000} To find a particular attribute, such as salary, write: {S.salary | Staff(S)  S.salary > 10000} 6

7 Tuple Relational Calculus Can use two quantifiers to tell how many instances the predicate applies to: – Existential quantifier  (‘there exists’) – Universal quantifier  (‘for all’) Tuple variables qualified by  or  are called bound variables, otherwise called free variables. 7

8 Tuple Relational Calculus Existential quantifier used in formulae that must be true for at least one instance, such as: Staff(S)  (  B)(Branch(B)  (B.branchNo = S.branchNo)  B.city = ‘London’) Means ‘There exists a Branch tuple with same branchNo as the branchNo of the current Staff tuple, S, and is located in London’. 8

9 Tuple Relational Calculus Universal quantifier is used in statements about every instance, such as:  B) (Branch(B)  B.city  ‘Paris’) Means ‘For all Branch tuples, the address is not in Paris’. Can also use ~(  B) (B.city = ‘Paris’) which means ‘There are no branches with an address in Paris’. 9

10 Tuple Relational Calculus Formulae should be unambiguous and make sense. A (well-formed) formula is made out of atoms: R ( S i ), where S i is a tuple variable and R is a relation S i.a 1  S j.a 2 S i.a 1  c Can recursively build up formulae from atoms: An atom is a formula If F 1 and F 2 are formulae, so are their conjunction, F 1  F 2 ; disjunction, F 1  F 2 ; and negation, ~F 1 If F is a formula with free variable X, then (  X)(F) and (  X)(F) are also formulae. 10

11 Example - Tuple Relational Calculus List the names of all managers who earn more than £25,000. {S.fName, S.lName | Staff(S)  S.position = ‘Manager’  S.salary > 25000} List the staff who manage properties for rent in Glasgow. {S | Staff(S)  (  P) (PropertyForRent(P)  (P.staffNo = S.staffNo)  P.city = ‘Glasgow’)} 11

12 Tuple Relational Calculus Expressions can generate an infinite set. For example: {S | ~Staff(S)} To avoid this, add restriction that all values in result must be values in the domain of the expression. 12

13 Relation Between SQL and TRC Target T corresponds to SELECT list: the query result contains the entire tuple Body split between two clauses: – Teaching – Teaching(T) corresponds to FROM clause – T.Semester = ‘F2000’ corresponds to WHERE clause Teaching {T | Teaching(T) AND T.Semester = ‘F2000’} SELECT * Teaching FROM Teaching T WHERE T.Semester = ‘F2000’ 13

14 Query Result The result of a TRC query with respect to a given database is the set of all choices of tuples for the variable T that make the query condition a true statement about the database. 14

15 Relation Between TRC and SQL (cont’d) List the names of professors who have taught MGT123 – In TRC: ProfessorTeaching {P.Name | Professor(P) AND  T  Teaching (P.Id = T.ProfId AND T.CrsCode = ‘MGT123’) } – In SQL: SELECT P.Name ProfessorTeaching FROM Professor P, Teaching T WHERE P.Id = T.ProfId AND T.CrsCode = ‘MGT123’ Core of SQL is merely a syntactic sugar on top of TRC 15

16 What Happened to Quantifiers in SQL? SQL has no quantifiers: how come? It uses conventions: – Convention 1. Universal quantifiers are not allowed – Convention 2. Make existential quantifiers implicit: Any tuple variable that does not occur in SELECT is assumed to be implicitly quantified with  Compare: ProfessorTeaching … } {P.Name | Professor(P) AND  T  Teaching … } and SELECT P.Name ProfessorTeaching FROM Professor P, Teaching T … … … Implicit  16

17 SQL uses a subset of TRC with simplifying conventions for quantification Restricts the use of quantification and negation (so TRC is more general in this respect) SQL uses aggregates, which are absent in TRC (and relational algebra, for that matter). SQL is extended with relational algebra operators (MINUS, UNION, JOIN, etc.) – This is just more syntactic sugar, but it makes queries easier to write Relation Between TRC and SQL (cont’d) 17

18 The Role of Relational Algebra in a DBMS 18

19 Correspondence Between SQL and Relational Algebra List courses taught in S2000 Tuple variables clarify meaning. Join condition “C.CrsCode=T.CrsCode” – relates facts to each other Selection condition “ T.Sem=‘S2000’ ” – eliminates irrelevant rows Equivalent (using natural join) to: SELECT C.CrsName CourseTeaching FROM Course C, Teaching T WHERE C.CrsCode=T.CrsCode AND T.Sem=‘S2000’ Course  CrsName (Course Teaching  Sem=‘S2000’ (Teaching) ) CourseTeaching  CrsName (  Sem=‘S2000’ (Course Teaching) ) 19


Download ppt "Relational Calculus. Relational calculus query specifies what is to be retrieved rather than how to retrieve it. – No description of how to evaluate a."

Similar presentations


Ads by Google